...
authoralex <[email protected]>
Sun, 6 Mar 2022 01:01:34 +0000 (17:01 -0800)
committeralex <[email protected]>
Sun, 6 Mar 2022 01:01:34 +0000 (17:01 -0800)
Makefile.am
configure.ac
inc/net.h
src/main.c
src/net/listen.c [new file with mode: 0644]
src/net/loop.c [new file with mode: 0644]
src/net/start.c
src/net/tcp.c [new file with mode: 0644]
src/net/udp.c [new file with mode: 0644]

index cbf7be581642a046f24f04cf8b537209d39a674a..13aa51c0212262bf018ddc20c977beb4983eaffc 100644 (file)
@@ -33,7 +33,11 @@ seederd_SOURCES = \
        src/log.c \
        src/main.c \
        src/meta.c \
+       src/net/listen.c \
+       src/net/loop.c \
        src/net/start.c \
+       src/net/tcp.c \
+       src/net/udp.c \
        src/opt/config.c \
        src/opt/env.c \
        src/opt/feed.c \
index c58bce154ee4a22c4192e7111655fb03a0b59311..7f5e0849685a5650c982ef83aa877ee8fdaf09d7 100644 (file)
@@ -64,14 +64,18 @@ AC_CHECK_LIB([pthread],[pthread_create])
 AC_CHECK_LIB([sodium],[crypto_hash_sha256_init])
 
 # Checks for header files.
-AC_CHECK_HEADERS([stdlib.h string.h])
+AC_CHECK_HEADERS([fcntl.h inttypes.h netdb.h stddef.h stdint.h stdlib.h string.h sys/socket.h unistd.h])
 
 # Checks for typedefs, structures, and compiler characteristics.
 AC_TYPE_SIZE_T
+AC_TYPE_SSIZE_T
+AC_TYPE_UINT64_T
+AC_TYPE_UINT8_T
 
 # Checks for library functions.
 AC_FUNC_MALLOC
-AC_CHECK_FUNCS([atexit])
+AC_FUNC_MKTIME
+AC_CHECK_FUNCS([atexit memmove memset mkdir setenv socket strchr strdup strndup strpbrk strrchr strstr strtoul strtoull])
 
 AC_CONFIG_FILES([Makefile
                 bench/Makefile
index 9d19b32045112214d0297b797dfc6307f06cdafe..9f82f628f5c2c3683c2f153cc6e5c2f575ea7cb5 100644 (file)
--- a/inc/net.h
+++ b/inc/net.h
 #include<sys/socket.h>
 #include<unistd.h>
 
-int start();
+struct net_info {
+       int tcp;
+       int udp;
+};
+
+#define NET_MESSAGE_SHUTDOWN_FAILED "net thread shutdown failed\n"
+
+#define HINTS_INIT(hints,socktype) { \
+       memset(&hints,0,sizeof(struct addrinfo)); \
+\
+       hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ \
+       hints.ai_socktype = socktype; /*  */ \
+       hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */ \
+       hints.ai_protocol = 0; \
+       hints.ai_canonname = NULL; \
+       hints.ai_addr = NULL; \
+       hints.ai_next = NULL; \
+}
+
+int net_listen_sock();
+void net_loop();
+int net_start();
+int net_tcp();
+int net_udp();
 
 #endif
index 81272dfb5152cfe4db270eb35a407e6a20090ed3..7aaf5fe7ae17b275ce268279465788e6d1623e68 100644 (file)
@@ -7,7 +7,9 @@ int main(int argc, char **argv) {
        if(feeds()<0) { return EXIT_FAILURE; }
        if(watch()<0) { return EXIT_FAILURE; }
 
-       if(start()<0) { return EXIT_FAILURE; }
+       if(net_start()<0) { return EXIT_FAILURE; }
+
+       while(1) { }
 
        return EXIT_SUCCESS;
 }
diff --git a/src/net/listen.c b/src/net/listen.c
new file mode 100644 (file)
index 0000000..828f3f7
--- /dev/null
@@ -0,0 +1,26 @@
+#include<net.h>
+
+int net_listen_sock(struct addrinfo *hints) {
+       struct addrinfo *p, *res;
+       int sock_fd;
+
+       if(getaddrinfo(NULL,global_options.port,hints,&res)!=0) {
+               perror("getaddrinfo");
+               return -1;
+       }
+
+       for(p=res;p!=NULL;p=p->ai_next) {
+               sock_fd = socket(p->ai_family,p->ai_socktype,p->ai_protocol);
+               if(sock_fd<0) { continue; }
+
+               if(bind(sock_fd,p->ai_addr,p->ai_addrlen)==0) { break; /* Success */ }
+
+               close(sock_fd);
+       }
+
+       free(res);
+
+       if(p==NULL) { return -1; }
+
+       return sock_fd;
+}
diff --git a/src/net/loop.c b/src/net/loop.c
new file mode 100644 (file)
index 0000000..8f7a672
--- /dev/null
@@ -0,0 +1,5 @@
+#include<net.h>
+
+void net_loop() {
+       return;
+}
index ccf5c7b690d814c8ce9b93c1c64841365b1a6bdf..3ba07a2a0489cc4312f34dec91010958602172cf 100644 (file)
@@ -1,79 +1,65 @@
 #include<net.h>
 
-#define MAX_BACKLOG 10
-
-static int listen_sock();
-static int tcp();
-static int udp();
-
-#define HINTS_INIT(hints,socktype) { \
-       memset(&hints,0,sizeof(struct addrinfo)); \
-\
-       hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ \
-       hints.ai_socktype = socktype; /*  */ \
-       hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */ \
-       hints.ai_protocol = 0; \
-       hints.ai_canonname = NULL; \
-       hints.ai_addr = NULL; \
-       hints.ai_next = NULL; \
-}
+static pthread_t *net_threads;
 
-static int tcp() {
-       struct addrinfo hints;
-       int sock_fd;
+static void net_shutdown();
+static void *net_spawn(void*);
+static void net_stop();
 
-       HINTS_INIT(hints,SOCK_STREAM);
-       sock_fd = listen_sock(&hints);
-       if(sock_fd<0) { return -1; }
+static void net_shutdown(void *p) {
+       struct net_info *info = (struct net_info*)p;
 
-       if(listen(sock_fd,MAX_BACKLOG)!=0) {
-               perror("listen");
-               return -1;
-       }
+       close(info->tcp);
+       close(info->udp);
 
-       return 1;
+       return;
 }
 
-static int udp() {
-       struct addrinfo hints;
-       int sock_fd;
+static void *net_spawn(void *p) {
+       struct net_info info;
 
-       HINTS_INIT(hints,SOCK_DGRAM);
-       sock_fd = listen_sock(&hints);
-       if(sock_fd<0) { return -1; }
+       if((info.tcp = net_tcp())<0) { return NULL; }
+       if((info.udp = net_udp())<0) { return NULL; }
 
-       return 1;
-}
+       pthread_cleanup_push(&net_shutdown,&info);
 
+       while(1) {
+               pthread_testcancel();
+               net_loop();
+       }
 
-int start() {
-       if(tcp()<0) { return -1; }
-       if(udp()<0) { return -1; }
+       pthread_cleanup_pop(1);
 
-       return 1;
+       return NULL;
 }
 
-static int listen_sock(struct addrinfo *hints) {
-       struct addrinfo *p, *res;
-       int sock_fd;
+int net_start() {
+       global_options.worker_threads = 1;
+       net_threads = calloc(sizeof(pthread_t),global_options.worker_threads);
+       for(size_t i=0;i<global_options.worker_threads;i++) {
+               if(pthread_create(&(net_threads[i]),NULL,&net_spawn,NULL)!=0) {
+                       perror("pthread_create");
+                       return -1;
+               }
+       }
 
-       if(getaddrinfo(NULL,global_options.port,hints,&res)!=0) {
-               perror("getaddrinfo");
+       if(0!=atexit(&net_stop)) {
+               perror("atexit");
                return -1;
        }
 
-       for(p=res;p!=NULL;p=p->ai_next) {
-               sock_fd = socket(p->ai_family,p->ai_socktype,p->ai_protocol);
-               if(sock_fd<0) { continue; }
-
-               if(bind(sock_fd,p->ai_addr,p->ai_addrlen)==0) { break; /* Success */ }
+       return 1;
+}
 
-               close(sock_fd);
+static void net_stop() {
+       for(size_t i=0;i<global_options.worker_threads;i++) {
+               pthread_cancel(net_threads[i]);
        }
 
-       free(res);
-
-       if(p==NULL) { return -1; }
-
-       return sock_fd;
+       for(size_t i=0;i<global_options.worker_threads;i++) {
+               if(pthread_join(net_threads[i],NULL)!=0) {
+                       log_err(NET_MESSAGE_SHUTDOWN_FAILED);
+               }
+       }
 }
+
diff --git a/src/net/tcp.c b/src/net/tcp.c
new file mode 100644 (file)
index 0000000..f609b24
--- /dev/null
@@ -0,0 +1,19 @@
+#include<net.h>
+
+#define MAX_BACKLOG 10
+
+int net_tcp() {
+       struct addrinfo hints;
+       int sock_fd;
+
+       HINTS_INIT(hints,SOCK_STREAM);
+       sock_fd = net_listen_sock(&hints);
+       if(sock_fd<0) { return -1; }
+
+       if(listen(sock_fd,MAX_BACKLOG)!=0) {
+               perror("listen");
+               return -1;
+       }
+
+       return sock_fd;
+}
diff --git a/src/net/udp.c b/src/net/udp.c
new file mode 100644 (file)
index 0000000..4511c6d
--- /dev/null
@@ -0,0 +1,12 @@
+#include<net.h>
+
+int net_udp() {
+       struct addrinfo hints;
+       int sock_fd;
+
+       HINTS_INIT(hints,SOCK_DGRAM);
+       sock_fd = net_listen_sock(&hints);
+       if(sock_fd<0) { return -1; }
+
+       return sock_fd;
+}