...
authoralex <[email protected]>
Wed, 19 Jan 2022 08:30:47 +0000 (00:30 -0800)
committeralex <[email protected]>
Wed, 19 Jan 2022 08:30:47 +0000 (00:30 -0800)
Makefile.am
inc/main.h
inc/net.h [new file with mode: 0644]
inc/server.h [deleted file]
inc/shutdown.h
src/main.c
src/net/start.c [new file with mode: 0644]
src/shutdown.c

index b1672fa93ac124fbe966a751e657ee4df60838d1..49ef6d55b768fc250a25715011867dc07c9be2d0 100644 (file)
@@ -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 \
index 7953f8a68f0f9db5e02203d8dbd8c1d81a3edad5..c5828ce8d7c45846667c29ac5944a7d167029f20 100644 (file)
@@ -6,7 +6,7 @@
 #include<add.h>
 #include<feed.h>
 #include<init.h>
-#include<server.h>
+#include<net.h>
 #include<watch.h>
 
 int main(int,char**);
diff --git a/inc/net.h b/inc/net.h
new file mode 100644 (file)
index 0000000..b90aea5
--- /dev/null
+++ b/inc/net.h
@@ -0,0 +1,14 @@
+#ifndef __NET_H_
+#define __NET_H_
+
+#include<netdb.h>
+#include<stdio.h>
+#include<stdlib.h>
+#include<string.h>
+#include<sys/types.h>
+#include<sys/socket.h>
+#include<unistd.h>
+
+int start();
+
+#endif
diff --git a/inc/server.h b/inc/server.h
deleted file mode 100644 (file)
index 00d87c9..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-#ifndef __SERVER_H_
-#define __SERVER_H_
-
-int server_start();
-
-#endif
index d60cfdbec87b6a789ab8cd319253e97da28c061f..60ca25a49cc47014e36f324a568b7ca847846f93 100644 (file)
@@ -12,6 +12,6 @@
 #define SHUTDOWN_MESSAGE_LOGGING_SUCCESS "shutdown logging thread successfully\n"
 
 int shutdown_register();
-void shutdown();
+void shutdown_all();
 
 #endif
index d5e4df71a4d2352fea6cfd92bc78e1c2ea7dbc68..4f889897baf05a53e95535b4cb934a8fac8468a4 100644 (file)
@@ -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 (file)
index 0000000..a4c7dc7
--- /dev/null
@@ -0,0 +1,79 @@
+#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 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;
+}
index 445d67ff799fe9e1f8be80353b86c08853363bbd..47ad443eafc012a5451bb89b7d2ff233f65887b6 100644 (file)
@@ -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);