]> Nishi Git Mirror - tewi.git/commitdiff
can listen now
authorNishi <nishi@nishi.boats>
Fri, 13 Sep 2024 11:18:23 +0000 (11:18 +0000)
committerNishi <nishi@nishi.boats>
Fri, 13 Sep 2024 11:18:23 +0000 (11:18 +0000)
git-svn-id: file:///raid/svn-personal/tewi/trunk@8 8739d7e6-ffea-ec47-b151-bdff447c6205

Platform/win64.mk
Server/Makefile
Server/main.c
Server/server.c [new file with mode: 0644]
Server/tw_server.h [new file with mode: 0644]

index 94763f9cff8d5f8d1faf43a8ecb2455d8a557aeb..a374bf76c2d795cfef9188aa528784a42db6509a 100644 (file)
@@ -4,5 +4,5 @@ CC = x86_64-w64-mingw32-gcc
 AR = x86_64-w64-mingw32-ar
 CFLAGS = -g -std=c99 -DPREFIX=\"$(PREFIX)\" -I $(PWD)/Common -I $(PWD)/openssl/include
 LDFLAGS = -L $(PWD)/openssl/lib
-LIBS =
+LIBS = -lws2_32
 EXEC = .exe
index 26a59e1d779fbed1017d98a98ecef023f3730160..efaea2db44e18e1dcb2b41e94cecd1cefbd85c3b 100644 (file)
@@ -5,7 +5,7 @@ include $(PWD)/Platform/$(PLATFORM).mk
 .PHONY: all clean
 .SUFFIXES: .c .o
 
-OBJS = version.o main.o config.o
+OBJS = version.o main.o config.o server.o
 
 all: tewi$(EXEC)
 
index 614f5e4430e9e12beba6845901573dd5ab5f7b0a..da854d74e2059bff4cc57ff067e8ace5de73457e 100644 (file)
@@ -9,6 +9,7 @@
 #include <cm_log.h>
 
 #include "tw_config.h"
+#include "tw_server.h"
 #include "tw_version.h"
 
 extern bool cm_do_log;
@@ -43,5 +44,9 @@ int main(int argc, char** argv) {
                fprintf(stderr, "Could not read the config\n");
                return 1;
        }
+       if(tw_server_init() != 0) {
+               fprintf(stderr, "Could not initialize the server\n");
+               return 1;
+       }
        cm_log("Daemon", "Ready");
 }
diff --git a/Server/server.c b/Server/server.c
new file mode 100644 (file)
index 0000000..af96368
--- /dev/null
@@ -0,0 +1,110 @@
+/* $Id$ */
+
+#include "tw_server.h"
+
+#include "tw_config.h"
+
+#include <unistd.h>
+#include <string.h>
+
+#include <cm_log.h>
+
+#ifdef __MINGW32__
+#include <winsock2.h>
+#define NO_IPV6
+#else
+#include <sys/select.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#endif
+
+extern struct tw_config config;
+
+fd_set fdset;
+int sockcount = 0;
+int sockets[MAX_PORTS];
+
+#ifdef NO_IPV6
+struct sockaddr_in addresses[MAX_PORTS];
+#else
+struct sockaddr_in6 addresses[MAX_PORTS];
+#endif
+
+void close_socket(int sock) {
+#ifdef __MINGW32__
+       closesocket(sock);
+#else
+       close(sock);
+#endif
+}
+
+int tw_server_init(void) {
+       int i;
+#ifdef __MINGW32__
+       WSADATA wsa;
+       WSAStartup(MAKEWORD(2, 0), &wsa);
+#endif
+       FD_ZERO(&fdset);
+       for(i = 0; config.ports[i] != -1; i++)
+               ;
+       sockcount = i;
+       for(i = 0; config.ports[i] != -1; i++) {
+#ifdef NO_IPV6
+               int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+#else
+               int sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
+#endif
+#ifdef __MINGW32__
+               if(sock == INVALID_SOCKET)
+#else
+               if(sock < 0)
+#endif
+               {
+                       cm_log("Server", "Socket creation failure");
+                       return 1;
+               }
+               int yes = 1;
+               if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0) {
+                       close_socket(sock);
+                       cm_log("Server", "setsockopt failure (reuseaddr)");
+                       return 1;
+               }
+               if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&yes, sizeof(yes)) < 0) {
+                       close_socket(sock);
+                       cm_log("Server", "setsockopt failure (nodelay)");
+                       return 1;
+               }
+#ifndef NO_IPV6
+               int no = 0;
+               if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&no, sizeof(no)) < 0) {
+                       close_socket(sock);
+                       cm_log("Server", "setsockopt failure (IPv6)");
+                       return 1;
+               }
+#endif
+               memset(&addresses[i], 0, sizeof(addresses[i]));
+#ifdef NO_IPV6
+               addresses[i].sin_family = AF_INET;
+               addresses[i].sin_addr.s_addr = INADDR_ANY;
+               addresses[i].sin_port = htons(config.ports[i]);
+#else
+               addresses[i].sin6_family = AF_INET6;
+               addresses[i].sin6_addr = in6addr_any;
+               addresses[i].sin6_port = htons(config.ports[i]);
+#endif
+               if(bind(sock, (struct sockaddr*)&addresses[i], sizeof(addresses[i])) < 0) {
+                       close_socket(sock);
+                       cm_log("Server", "Bind failure");
+                       return 1;
+               }
+               if(listen(sock, 128) < 0) {
+                       close_socket(sock);
+                       cm_log("Server", "Listen failure");
+                       return 1;
+               }
+               sockets[i] = sock;
+       }
+       return 0;
+}
diff --git a/Server/tw_server.h b/Server/tw_server.h
new file mode 100644 (file)
index 0000000..12bde4c
--- /dev/null
@@ -0,0 +1,8 @@
+/* $Id$ */
+
+#ifndef __TW_SERVER_H__
+#define __TW_SERVER_H__
+
+int tw_server_init(void);
+
+#endif