From 383a3ae3c0f99bde7c361d086eb1315ca8fc7ebf Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 5 Mar 2022 17:01:34 -0800 Subject: [PATCH] ... --- Makefile.am | 4 ++ configure.ac | 8 +++- inc/net.h | 25 +++++++++++- src/main.c | 4 +- src/net/listen.c | 26 +++++++++++++ src/net/loop.c | 5 +++ src/net/start.c | 98 +++++++++++++++++++++--------------------------- src/net/tcp.c | 19 ++++++++++ src/net/udp.c | 12 ++++++ 9 files changed, 141 insertions(+), 60 deletions(-) create mode 100644 src/net/listen.c create mode 100644 src/net/loop.c create mode 100644 src/net/tcp.c create mode 100644 src/net/udp.c diff --git a/Makefile.am b/Makefile.am index cbf7be5..13aa51c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/configure.ac b/configure.ac index c58bce1..7f5e084 100644 --- a/configure.ac +++ b/configure.ac @@ -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 diff --git a/inc/net.h b/inc/net.h index 9d19b32..9f82f62 100644 --- a/inc/net.h +++ b/inc/net.h @@ -11,6 +11,29 @@ #include #include -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 diff --git a/src/main.c b/src/main.c index 81272df..7aaf5fe 100644 --- a/src/main.c +++ b/src/main.c @@ -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 index 0000000..828f3f7 --- /dev/null +++ b/src/net/listen.c @@ -0,0 +1,26 @@ +#include + +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 index 0000000..8f7a672 --- /dev/null +++ b/src/net/loop.c @@ -0,0 +1,5 @@ +#include + +void net_loop() { + return; +} diff --git a/src/net/start.c b/src/net/start.c index ccf5c7b..3ba07a2 100644 --- a/src/net/start.c +++ b/src/net/start.c @@ -1,79 +1,65 @@ #include -#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;iai_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 + +#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 index 0000000..4511c6d --- /dev/null +++ b/src/net/udp.c @@ -0,0 +1,12 @@ +#include + +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; +} -- 2.39.5