1 | /* $Id: server.c 17 2024-09-13 17:41:07Z nishi $ */
|
---|
2 |
|
---|
3 | #define SOURCE
|
---|
4 |
|
---|
5 | #include "tw_server.h"
|
---|
6 |
|
---|
7 | #include "tw_ssl.h"
|
---|
8 | #include "tw_config.h"
|
---|
9 | #include "tw_http.h"
|
---|
10 |
|
---|
11 | #include <unistd.h>
|
---|
12 | #include <string.h>
|
---|
13 | #include <stdbool.h>
|
---|
14 |
|
---|
15 | #include <cm_log.h>
|
---|
16 |
|
---|
17 | #ifdef __MINGW32__
|
---|
18 | #include <winsock2.h>
|
---|
19 | #include <process.h>
|
---|
20 | #define NO_IPV6
|
---|
21 | #else
|
---|
22 | #include <sys/select.h>
|
---|
23 | #include <sys/socket.h>
|
---|
24 | #include <arpa/inet.h>
|
---|
25 | #include <netinet/in.h>
|
---|
26 | #include <netinet/tcp.h>
|
---|
27 | #endif
|
---|
28 |
|
---|
29 | extern struct tw_config config;
|
---|
30 |
|
---|
31 | fd_set fdset;
|
---|
32 | int sockcount = 0;
|
---|
33 |
|
---|
34 | #ifdef NO_IPV6
|
---|
35 | #define SOCKADDR struct sockaddr_in
|
---|
36 | #else
|
---|
37 | #define SOCKADDR struct sockaddr_in6
|
---|
38 | #endif
|
---|
39 | SOCKADDR addresses[MAX_PORTS];
|
---|
40 | int sockets[MAX_PORTS];
|
---|
41 |
|
---|
42 | void close_socket(int sock) {
|
---|
43 | #ifdef __MINGW32__
|
---|
44 | closesocket(sock);
|
---|
45 | #else
|
---|
46 | close(sock);
|
---|
47 | #endif
|
---|
48 | }
|
---|
49 |
|
---|
50 | int tw_server_init(void) {
|
---|
51 | int i;
|
---|
52 | #ifdef __MINGW32__
|
---|
53 | WSADATA wsa;
|
---|
54 | WSAStartup(MAKEWORD(2, 0), &wsa);
|
---|
55 | #endif
|
---|
56 | for(i = 0; config.ports[i] != -1; i++)
|
---|
57 | ;
|
---|
58 | sockcount = i;
|
---|
59 | for(i = 0; config.ports[i] != -1; i++) {
|
---|
60 | #ifdef NO_IPV6
|
---|
61 | int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
---|
62 | #else
|
---|
63 | int sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
|
---|
64 | #endif
|
---|
65 | #ifdef __MINGW32__
|
---|
66 | if(sock == INVALID_SOCKET)
|
---|
67 | #else
|
---|
68 | if(sock < 0)
|
---|
69 | #endif
|
---|
70 | {
|
---|
71 | cm_log("Server", "Socket creation failure");
|
---|
72 | return 1;
|
---|
73 | }
|
---|
74 | int yes = 1;
|
---|
75 | if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0) {
|
---|
76 | close_socket(sock);
|
---|
77 | cm_log("Server", "setsockopt failure (reuseaddr)");
|
---|
78 | return 1;
|
---|
79 | }
|
---|
80 | if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&yes, sizeof(yes)) < 0) {
|
---|
81 | close_socket(sock);
|
---|
82 | cm_log("Server", "setsockopt failure (nodelay)");
|
---|
83 | return 1;
|
---|
84 | }
|
---|
85 | #ifndef NO_IPV6
|
---|
86 | int no = 0;
|
---|
87 | if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&no, sizeof(no)) < 0) {
|
---|
88 | close_socket(sock);
|
---|
89 | cm_log("Server", "setsockopt failure (IPv6)");
|
---|
90 | return 1;
|
---|
91 | }
|
---|
92 | #endif
|
---|
93 | memset(&addresses[i], 0, sizeof(addresses[i]));
|
---|
94 | #ifdef NO_IPV6
|
---|
95 | addresses[i].sin_family = AF_INET;
|
---|
96 | addresses[i].sin_addr.s_addr = INADDR_ANY;
|
---|
97 | addresses[i].sin_port = htons(config.ports[i]);
|
---|
98 | #else
|
---|
99 | addresses[i].sin6_family = AF_INET6;
|
---|
100 | addresses[i].sin6_addr = in6addr_any;
|
---|
101 | addresses[i].sin6_port = htons(config.ports[i]);
|
---|
102 | #endif
|
---|
103 | if(bind(sock, (struct sockaddr*)&addresses[i], sizeof(addresses[i])) < 0) {
|
---|
104 | close_socket(sock);
|
---|
105 | cm_log("Server", "Bind failure");
|
---|
106 | return 1;
|
---|
107 | }
|
---|
108 | if(listen(sock, 128) < 0) {
|
---|
109 | close_socket(sock);
|
---|
110 | cm_log("Server", "Listen failure");
|
---|
111 | return 1;
|
---|
112 | }
|
---|
113 | sockets[i] = sock;
|
---|
114 | }
|
---|
115 | return 0;
|
---|
116 | }
|
---|
117 |
|
---|
118 | size_t tw_read(SSL* ssl, int s, void* data, size_t len) {
|
---|
119 | if(ssl == NULL) {
|
---|
120 | return recv(s, data, len, 0);
|
---|
121 | } else {
|
---|
122 | return SSL_read(ssl, data, len);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | size_t tw_write(SSL* ssl, int s, void* data, size_t len) {
|
---|
127 | if(ssl == NULL) {
|
---|
128 | return send(s, data, len, 0);
|
---|
129 | } else {
|
---|
130 | return SSL_write(ssl, data, len);
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | #ifdef __MINGW32__
|
---|
135 | struct pass_entry {
|
---|
136 | int sock;
|
---|
137 | int port;
|
---|
138 | bool ssl;
|
---|
139 | };
|
---|
140 |
|
---|
141 | unsigned int WINAPI tw_server_pass(void* ptr) {
|
---|
142 | int sock = ((struct pass_entry*)ptr)->sock;
|
---|
143 | bool ssl = ((struct pass_entry*)ptr)->ssl;
|
---|
144 | int port = ((struct pass_entry*)ptr)->port;
|
---|
145 | free(ptr);
|
---|
146 | #else
|
---|
147 | void tw_server_pass(int sock, bool ssl, int port) {
|
---|
148 | #endif
|
---|
149 | char* name = config.hostname;
|
---|
150 |
|
---|
151 | SSL_CTX* ctx = NULL;
|
---|
152 | SSL* s = NULL;
|
---|
153 | bool sslworks = false;
|
---|
154 | if(ssl) {
|
---|
155 | ctx = tw_create_ssl_ctx(port);
|
---|
156 | s = SSL_new(ctx);
|
---|
157 | SSL_set_fd(s, sock);
|
---|
158 | if(SSL_accept(s) <= 0) goto cleanup;
|
---|
159 | sslworks = true;
|
---|
160 | }
|
---|
161 | struct tw_http_request req;
|
---|
162 | int ret = tw_http_parse(s, sock, &req);
|
---|
163 | if(ret == 0) {
|
---|
164 | }
|
---|
165 | cleanup:
|
---|
166 | if(sslworks) {
|
---|
167 | SSL_shutdown(s);
|
---|
168 | }
|
---|
169 | SSL_free(s);
|
---|
170 | close_socket(sock);
|
---|
171 | #ifdef __MINGW32__
|
---|
172 | _endthreadex(0);
|
---|
173 | #endif
|
---|
174 | }
|
---|
175 |
|
---|
176 | void tw_server_loop(void) {
|
---|
177 | struct timeval tv;
|
---|
178 | while(1) {
|
---|
179 | FD_ZERO(&fdset);
|
---|
180 | int i;
|
---|
181 | for(i = 0; i < sockcount; i++) {
|
---|
182 | FD_SET(sockets[i], &fdset);
|
---|
183 | }
|
---|
184 | tv.tv_sec = 1;
|
---|
185 | tv.tv_usec = 0;
|
---|
186 | int ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
|
---|
187 | if(ret == -1) {
|
---|
188 | break;
|
---|
189 | } else if(ret > 0) {
|
---|
190 | /* connection */
|
---|
191 | int i;
|
---|
192 | for(i = 0; i < sockcount; i++) {
|
---|
193 | if(FD_ISSET(sockets[i], &fdset)) {
|
---|
194 | SOCKADDR claddr;
|
---|
195 | int clen = sizeof(claddr);
|
---|
196 | int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
|
---|
197 | cm_log("Server", "New connection accepted");
|
---|
198 | #ifdef __MINGW32__
|
---|
199 | HANDLE thread;
|
---|
200 | struct pass_entry* e = malloc(sizeof(*e));
|
---|
201 | e->sock = sock;
|
---|
202 | e->ssl = config.ports[i] & (1ULL << 32);
|
---|
203 | e->port = config.ports[i];
|
---|
204 | thread = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL);
|
---|
205 | #else
|
---|
206 | pid_t pid = fork();
|
---|
207 | if(pid == 0) {
|
---|
208 | tw_server_pass(sock, config.ports[i] & (1ULL << 32), config.ports[i]);
|
---|
209 | _exit(0);
|
---|
210 | } else {
|
---|
211 | close_socket(sock);
|
---|
212 | }
|
---|
213 | #endif
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|