From 3de78a863a14104a43cdb575dfc586fce3a8129b Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 19 Jan 2022 00:30:47 -0800 Subject: [PATCH] ... --- Makefile.am | 4 +-- inc/main.h | 2 +- inc/net.h | 14 +++++++++ inc/server.h | 6 ---- inc/shutdown.h | 2 +- src/main.c | 4 +-- src/net/start.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ src/shutdown.c | 4 +-- 8 files changed, 100 insertions(+), 15 deletions(-) create mode 100644 inc/net.h delete mode 100644 inc/server.h create mode 100644 src/net/start.c diff --git a/Makefile.am b/Makefile.am index b1672fa..49ef6d5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -33,6 +33,7 @@ seederd_SOURCES = \ src/log.c \ src/main.c \ src/meta.c \ + src/net/start.c \ src/opt/config.c \ src/opt/env.c \ src/opt/feed.c \ @@ -49,7 +50,6 @@ seederd_SOURCES = \ src/rss/header.c \ src/rss/info.c \ src/rss/init.c \ - src/server/start.c \ src/session.c \ src/setup.c \ src/shutdown.c \ @@ -80,9 +80,9 @@ seederd_SOURCES += \ inc/log.h \ inc/main.h \ inc/meta.h \ + inc/net.h \ inc/opt.h \ inc/rss.h \ - inc/server.h \ inc/session.h \ inc/setup.h \ inc/shutdown.h \ diff --git a/inc/main.h b/inc/main.h index 7953f8a..c5828ce 100644 --- a/inc/main.h +++ b/inc/main.h @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include int main(int,char**); diff --git a/inc/net.h b/inc/net.h new file mode 100644 index 0000000..b90aea5 --- /dev/null +++ b/inc/net.h @@ -0,0 +1,14 @@ +#ifndef __NET_H_ +#define __NET_H_ + +#include +#include +#include +#include +#include +#include +#include + +int start(); + +#endif diff --git a/inc/server.h b/inc/server.h deleted file mode 100644 index 00d87c9..0000000 --- a/inc/server.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef __SERVER_H_ -#define __SERVER_H_ - -int server_start(); - -#endif diff --git a/inc/shutdown.h b/inc/shutdown.h index d60cfdb..60ca25a 100644 --- a/inc/shutdown.h +++ b/inc/shutdown.h @@ -12,6 +12,6 @@ #define SHUTDOWN_MESSAGE_LOGGING_SUCCESS "shutdown logging thread successfully\n" int shutdown_register(); -void shutdown(); +void shutdown_all(); #endif diff --git a/src/main.c b/src/main.c index d5e4df7..4f88989 100644 --- a/src/main.c +++ b/src/main.c @@ -9,9 +9,7 @@ int main(int argc, char **argv) { if(feeds()<0) { return EXIT_FAILURE; } if(watch()<0) { return EXIT_FAILURE; } - while(1) { } - - if(server_start()<0) { return EXIT_FAILURE; } + if(start()<0) { return EXIT_FAILURE; } return EXIT_SUCCESS; } diff --git a/src/net/start.c b/src/net/start.c new file mode 100644 index 0000000..a4c7dc7 --- /dev/null +++ b/src/net/start.c @@ -0,0 +1,79 @@ +#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 int tcp() { + struct addrinfo hints; + int sock_fd; + + HINTS_INIT(hints,SOCK_STREAM); + sock_fd = listen_sock(&hints); + if(sock_fd<0) { return -1; } + + return 1; +} + +static int udp() { + struct addrinfo hints; + int sock_fd; + + HINTS_INIT(hints,SOCK_DGRAM); + sock_fd = listen_sock(&hints); + if(sock_fd<0) { return -1; } + + return 1; +} + + +int start() { + if(tcp()<0) { return -1; } + if(udp()<0) { return -1; } + + return 1; +} + +static int listen_sock(struct addrinfo *hints) { + struct addrinfo *p, *res; + int sock_fd; + + if(getaddrinfo(NULL,"5150",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; } + + if(listen(sock_fd,MAX_BACKLOG)!=0) { + perror("listen"); + return -1; + } + + return sock_fd; +} diff --git a/src/shutdown.c b/src/shutdown.c index 445d67f..47ad443 100644 --- a/src/shutdown.c +++ b/src/shutdown.c @@ -3,7 +3,7 @@ static void shutdown_logging(); int shutdown_register() { - if(0!=atexit(&shutdown)) { + if(0!=atexit(&shutdown_all)) { perror("atexit"); return -1; } @@ -11,7 +11,7 @@ int shutdown_register() { return 1; } -void shutdown() { +void shutdown_all() { shutdown_logging(); log_msg(SHUTDOWN_MESSAGE_COMPLETE); -- 2.39.5