Selaa lähdekoodia

vccd: now usable but no user or others

intirain 4 vuotta sitten
vanhempi
sitoutus
6c36ad6bba
7 muutettua tiedostoa jossa 357 lisäystä ja 12 poistoa
  1. 2 2
      Makefile
  2. 57 0
      include/klist.h
  3. 38 0
      include/vcc/vccd.h
  4. 1 1
      include/vcc/version.h
  5. 69 0
      klist.c
  6. 5 9
      main.c
  7. 185 0
      vcc.c

+ 2 - 2
Makefile

@@ -4,8 +4,8 @@ VCCD_PATCH 		= 0
 VCCD_COMPILE_TIME 	= $(shell date)
 VCCD_COMPILE_BY 	= $(shell whoami)
 
-srcs 			= main.c 
-objs 			= main.o 
+srcs 			= main.c vcc.c klist.c 
+objs 			= main.o vcc.o klist.o 
 target 			= vccd
 CFLAGS 			= -Wall -Werror -Iinclude -g 
 MAKEFLAGS 		= --no-print-directory

+ 57 - 0
include/klist.h

@@ -0,0 +1,57 @@
+/* vccd/include/klist.h 
+ * 
+ * This file is part of vccd. 
+ * 
+ * vccd is free software: you can redistribute it and/or modify 
+ * it under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation, either version 3 of the License, or 
+ * (at your option) any later version. 
+ * 
+ * vccd is distributed in the hope that it will be useful, 
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+ * GNU General Public License for more details. 
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with vccd. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#ifndef __KLIST_H
+#define __KLIST_H
+
+#include <vcc/vcc.h>
+
+
+struct klist_node {
+	long 			value;
+	void 			*data;
+	struct klist_node 	*sending;
+
+	struct klist_node 	*next, *prev;
+};
+
+
+
+struct klist_node *klist_find_value(struct klist_node *head, int value);
+
+void klist_init(struct klist_node *head);
+
+void klist_add(struct klist_node *head, struct klist_node *new);
+void klist_del(struct klist_node *head, struct klist_node *will_del);
+
+#define klist_empty(list) ((list)->next == (list))
+
+#define KLIST_NODE_INIT(__x) { \
+	.value = 0, \
+	.data = NULL, \
+	.sending = NULL, \
+	.next = __x, \
+	.prev = __x, \
+}
+
+
+#endif
+

+ 38 - 0
include/vcc/vccd.h

@@ -0,0 +1,38 @@
+/* vccd/include/vcc/vccd.h 
+ * 
+ * This file is part of vccd. 
+ * 
+ * vccd is free software: you can redistribute it and/or modify 
+ * it under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation, either version 3 of the License, or 
+ * (at your option) any later version. 
+ * 
+ * vccd is distributed in the hope that it will be useful, 
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+ * GNU General Public License for more details. 
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with vccd. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#ifndef __VCC_VCCD_H
+#define __VCC_VCCD_H
+
+#include <stdio.h>
+#include <arpa/inet.h>
+
+#define vcc_log(msg, ...) 	fprintf(stderr, msg, ##__VA_ARGS__)
+
+int vcc_new_connection(int fd, struct in_addr *addr);
+int vcc_close_connection(int fd);
+
+int vcc_write_packages(int fd);
+int vcc_new_msg(int fd, char *msg);
+
+
+#endif
+

+ 1 - 1
include/vcc/version.h

@@ -1 +1 @@
-#define VCCD_VERSION "vccd 0.1.0 Fri Mar  4 22:04:09 CST 2022 root"
+#define VCCD_VERSION "vccd 0.1.0 Sat Mar  5 15:51:51 CST 2022 root"

+ 69 - 0
klist.c

@@ -0,0 +1,69 @@
+/* vccd/klist.c 
+ * 
+ * This file is part of vccd. 
+ * 
+ * vccd is free software: you can redistribute it and/or modify 
+ * it under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation, either version 3 of the License, or 
+ * (at your option) any later version. 
+ * 
+ * vccd is distributed in the hope that it will be useful, 
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+ * GNU General Public License for more details. 
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with vccd. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#include <sys/types.h>
+#include <stddef.h>
+#include <klist.h>
+
+
+
+struct klist_node *klist_find_value(struct klist_node *head, int value) {
+	struct klist_node *t;
+
+	for (t = head; t; t = t->next) 
+		if (t->value == value) 
+			return t;
+
+	return NULL;
+}
+
+
+void klist_init(struct klist_node *head) {
+	head->next = head;
+	head->prev = head;
+	head->value = 0;
+	head->data = NULL;
+
+	return;
+}
+
+
+void klist_add(struct klist_node *head, struct klist_node *new) {
+	/* head->prev: the last. */
+	new->prev = head->prev;
+	new->next = head;
+
+	/* head->prev->next: (was) the head */
+	head->prev->next = new;
+	head->prev = new;
+
+	return;
+}
+
+
+void klist_del(struct klist_node *head, struct klist_node *will_del) {
+	will_del->prev->next = will_del->next;
+	will_del->next->prev = will_del->prev;
+
+	return;
+}
+
+

+ 5 - 9
main.c

@@ -34,6 +34,7 @@
 #include <poll.h>
 
 #include <vcc/vcc.h>
+#include <vcc/vccd.h>
 #include <vcc/version.h>
 
 
@@ -43,8 +44,6 @@
  * but that only makes things more difficult and hard to understand 
  * that's why we just fork() and exit() */
 
-#define vcc_log(msg, ...) 	fprintf(stderr, msg, ##__VA_ARGS__)
-
 /* this is the shortest name i think. it stores the fd of the server 
  * socket */
 int fd;
@@ -148,7 +147,6 @@ int main(int argc, const char **argv) {
 	__uint32_t 	size;
 	int 		usr_fd, nfds_total, nfds_ready, i;
 	char 		*buf;
-	const char 	msg[] = {"Hello, I'm the server. \n"};
 
 	struct sockaddr_in usr_addr;
 
@@ -178,7 +176,7 @@ int main(int argc, const char **argv) {
 				continue;
 			}
 
-			vcc_log("New connection from %s\n", inet_ntoa(usr_addr.sin_addr));
+			vcc_new_connection(usr_fd, &usr_addr.sin_addr);
 
 			if (next_fd >= POLLFDS) {
 				for (next_fd = 1; fds[next_fd].fd == -1 && next_fd < POLLFDS; next_fd++) 
@@ -209,7 +207,7 @@ int main(int argc, const char **argv) {
 				size = read(fds[i].fd, buf, REQ_SIZE);
 
 				if (!size) {
-					vcc_log("Closed. \n");
+					vcc_close_connection(fds[i].fd);
 
 					fds[i].fd = -1;
 					free(buf);
@@ -217,7 +215,7 @@ int main(int argc, const char **argv) {
 					continue;
 				}
 
-				vcc_log("message received: %s\n", buf);
+				vcc_new_msg(fds[i].fd, buf);
 
 				free(buf);
 
@@ -225,9 +223,7 @@ int main(int argc, const char **argv) {
 			}
 
 			if (fds[i].revents & POLLOUT) {
-				write(fds[i].fd, msg, sizeof(msg));
-
-				vcc_log("message sent. \n");
+				vcc_write_packages(fds[i].fd);
 
 				fds[i].events = POLLIN;
 			}

+ 185 - 0
vcc.c

@@ -0,0 +1,185 @@
+/* vccd/vcc.c 
+ * 
+ * This file is part of vccd. 
+ * 
+ * vccd is free software: you can redistribute it and/or modify 
+ * it under the terms of the GNU General Public License as published by 
+ * the Free Software Foundation, either version 3 of the License, or 
+ * (at your option) any later version. 
+ * 
+ * vccd is distributed in the hope that it will be useful, 
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of 
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
+ * GNU General Public License for more details. 
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with vccd. If not, see <https://www.gnu.org/licenses/>
+ */ 
+
+
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <string.h>
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <poll.h>
+
+#include <vcc/vcc.h>
+#include <vcc/vccd.h>
+#include <vcc/version.h>
+#include <klist.h>
+
+
+struct klist_node connections = KLIST_NODE_INIT(&connections);
+
+
+int vcc_new_connection(int fd, struct in_addr *addr) {
+	struct klist_node *n;
+
+	vcc_log("New connection from %s\n", inet_ntoa(*addr));
+
+	if (unlikely(!(n = malloc(sizeof(struct klist_node))))) {
+		vcc_log("*** malloc() failed\n");
+
+		return 1;
+	}
+
+	klist_init(n);
+	n->data = addr;
+	n->value = fd;
+
+	if (unlikely(!(n->sending = malloc(sizeof(struct klist_node))))) {
+		vcc_log("*** malloc() failed\n");
+
+		return 1;
+	}
+
+	klist_init(n->sending);	
+	klist_add(&connections, n);
+
+	return 0;
+}
+
+
+int vcc_close_connection(int fd) {
+	struct klist_node *n, *t;
+
+	for (n = connections.next; n != &connections 
+			&& n->value != fd; n = n->next) 
+		;
+
+	klist_del(&connections, n);
+
+	for (t = n->sending->next; t != n->sending; t = n->next) 
+		free(t->data);
+
+	free(n->sending);
+
+	vcc_log("Client closed. \n");
+
+	return 0;
+}
+
+
+struct klist_node *find_connection(int fd) {
+	struct klist_node *n;
+
+	for (n = connections.next; n != &connections
+			&& n->value != fd; n = n->next) 
+		;
+
+	if (unlikely(n == &connections)) {
+		vcc_log("--- connection not found: %d\n", fd);
+
+		return NULL;
+	}
+
+	return n;
+}
+
+
+int write_packages(struct klist_node *node) {
+	void *p;
+
+	if (unlikely(!node->sending)) 
+		return 0;
+
+	if (unlikely(klist_empty(node->sending))) 
+		return 0;
+	
+	p = node->sending->next->data;
+
+	write(node->value, p, REQ_SIZE);
+
+	free(p);
+	klist_del(node->sending, node->sending->next);
+
+	return 0;
+}
+
+int vcc_write_packages(int fd) {
+	return write_packages(find_connection(fd));
+}
+
+
+int vcc_send_req(struct klist_node *node, void *p) {
+	struct klist_node *n;
+
+	if (unlikely(!(n = malloc(sizeof(struct klist_node))))) {
+		vcc_log("*** malloc() failed\n");
+
+		return 1;
+	}
+
+	n->data = p;
+	klist_add(node->sending, n);
+
+	return 0;
+}
+
+
+/* send to each user include who sends this message */
+
+int vcc_message_broadcast(char *msg) {
+	struct klist_node *n;
+	int i;
+	char *buf;
+
+	for (n = connections.next, i = 0; n != &connections; n = n->next, i++) {
+		if (unlikely(!(buf = malloc(REQ_SIZE)))) {
+			vcc_log("*** malloc() failed\n");
+
+			continue;
+		}
+
+		strcpy(buf, msg);
+
+		vcc_send_req(n, buf);
+	}
+
+	vcc_log("Message sent to %d users. \n", i);
+
+	return 0;
+}
+
+
+int vcc_new_msg(int fd, char *msg) {
+	if (((unsigned char *) msg)[0] == 0xffu) 
+		return 0;
+
+	vcc_log("New message: %s\n", msg);
+	vcc_message_broadcast(msg);
+
+	return 0;
+}
+
+