1 | /* $Id: server.c 8 2024-09-13 11:18:23Z nishi $ */
|
---|
2 |
|
---|
3 | #include "tw_server.h"
|
---|
4 |
|
---|
5 | #include "tw_config.h"
|
---|
6 |
|
---|
7 | #include <unistd.h>
|
---|
8 | #include <string.h>
|
---|
9 |
|
---|
10 | #include <cm_log.h>
|
---|
11 |
|
---|
12 | #ifdef __MINGW32__
|
---|
13 | #include <winsock2.h>
|
---|
14 | #define NO_IPV6
|
---|
15 | #else
|
---|
16 | #include <sys/select.h>
|
---|
17 | #include <sys/socket.h>
|
---|
18 | #include <arpa/inet.h>
|
---|
19 | #include <netinet/in.h>
|
---|
20 | #include <netinet/tcp.h>
|
---|
21 | #endif
|
---|
22 |
|
---|
23 | extern struct tw_config config;
|
---|
24 |
|
---|
25 | fd_set fdset;
|
---|
26 | int sockcount = 0;
|
---|
27 | int sockets[MAX_PORTS];
|
---|
28 |
|
---|
29 | #ifdef NO_IPV6
|
---|
30 | struct sockaddr_in addresses[MAX_PORTS];
|
---|
31 | #else
|
---|
32 | struct sockaddr_in6 addresses[MAX_PORTS];
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | void close_socket(int sock) {
|
---|
36 | #ifdef __MINGW32__
|
---|
37 | closesocket(sock);
|
---|
38 | #else
|
---|
39 | close(sock);
|
---|
40 | #endif
|
---|
41 | }
|
---|
42 |
|
---|
43 | int tw_server_init(void) {
|
---|
44 | int i;
|
---|
45 | #ifdef __MINGW32__
|
---|
46 | WSADATA wsa;
|
---|
47 | WSAStartup(MAKEWORD(2, 0), &wsa);
|
---|
48 | #endif
|
---|
49 | FD_ZERO(&fdset);
|
---|
50 | for(i = 0; config.ports[i] != -1; i++)
|
---|
51 | ;
|
---|
52 | sockcount = i;
|
---|
53 | for(i = 0; config.ports[i] != -1; i++) {
|
---|
54 | #ifdef NO_IPV6
|
---|
55 | int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
---|
56 | #else
|
---|
57 | int sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
|
---|
58 | #endif
|
---|
59 | #ifdef __MINGW32__
|
---|
60 | if(sock == INVALID_SOCKET)
|
---|
61 | #else
|
---|
62 | if(sock < 0)
|
---|
63 | #endif
|
---|
64 | {
|
---|
65 | cm_log("Server", "Socket creation failure");
|
---|
66 | return 1;
|
---|
67 | }
|
---|
68 | int yes = 1;
|
---|
69 | if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0) {
|
---|
70 | close_socket(sock);
|
---|
71 | cm_log("Server", "setsockopt failure (reuseaddr)");
|
---|
72 | return 1;
|
---|
73 | }
|
---|
74 | if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&yes, sizeof(yes)) < 0) {
|
---|
75 | close_socket(sock);
|
---|
76 | cm_log("Server", "setsockopt failure (nodelay)");
|
---|
77 | return 1;
|
---|
78 | }
|
---|
79 | #ifndef NO_IPV6
|
---|
80 | int no = 0;
|
---|
81 | if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&no, sizeof(no)) < 0) {
|
---|
82 | close_socket(sock);
|
---|
83 | cm_log("Server", "setsockopt failure (IPv6)");
|
---|
84 | return 1;
|
---|
85 | }
|
---|
86 | #endif
|
---|
87 | memset(&addresses[i], 0, sizeof(addresses[i]));
|
---|
88 | #ifdef NO_IPV6
|
---|
89 | addresses[i].sin_family = AF_INET;
|
---|
90 | addresses[i].sin_addr.s_addr = INADDR_ANY;
|
---|
91 | addresses[i].sin_port = htons(config.ports[i]);
|
---|
92 | #else
|
---|
93 | addresses[i].sin6_family = AF_INET6;
|
---|
94 | addresses[i].sin6_addr = in6addr_any;
|
---|
95 | addresses[i].sin6_port = htons(config.ports[i]);
|
---|
96 | #endif
|
---|
97 | if(bind(sock, (struct sockaddr*)&addresses[i], sizeof(addresses[i])) < 0) {
|
---|
98 | close_socket(sock);
|
---|
99 | cm_log("Server", "Bind failure");
|
---|
100 | return 1;
|
---|
101 | }
|
---|
102 | if(listen(sock, 128) < 0) {
|
---|
103 | close_socket(sock);
|
---|
104 | cm_log("Server", "Listen failure");
|
---|
105 | return 1;
|
---|
106 | }
|
---|
107 | sockets[i] = sock;
|
---|
108 | }
|
---|
109 | return 0;
|
---|
110 | }
|
---|