source: Main/trunk/Server/server.c@ 20

Last change on this file since 20 was 20, checked in by Nishi, on Sep 14, 2024 at 6:59:15 PM

can show index

  • Property svn:keywords set to Id
File size: 9.3 KB
Line 
1/* $Id: server.c 20 2024-09-14 09:59:15Z 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#include "tw_module.h"
11#include "tw_version.h"
12
13#include <unistd.h>
14#include <string.h>
15#include <stdbool.h>
16#include <stdarg.h>
17
18#include <cm_string.h>
19#include <cm_log.h>
20
21#ifdef __MINGW32__
22#include <winsock2.h>
23#include <process.h>
24#define NO_IPV6
25#else
26#include <sys/select.h>
27#include <sys/socket.h>
28#include <arpa/inet.h>
29#include <netinet/in.h>
30#include <netinet/tcp.h>
31#endif
32
33extern struct tw_config config;
34extern char tw_server[];
35
36fd_set fdset;
37int sockcount = 0;
38
39#ifdef NO_IPV6
40#define SOCKADDR struct sockaddr_in
41#else
42#define SOCKADDR struct sockaddr_in6
43#endif
44SOCKADDR addresses[MAX_PORTS];
45int sockets[MAX_PORTS];
46
47void close_socket(int sock) {
48#ifdef __MINGW32__
49 closesocket(sock);
50#else
51 close(sock);
52#endif
53}
54
55int tw_server_init(void) {
56 int i;
57#ifdef __MINGW32__
58 WSADATA wsa;
59 WSAStartup(MAKEWORD(2, 0), &wsa);
60#endif
61 for(i = 0; config.ports[i] != -1; i++)
62 ;
63 sockcount = i;
64 for(i = 0; config.ports[i] != -1; i++) {
65#ifdef NO_IPV6
66 int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
67#else
68 int sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
69#endif
70#ifdef __MINGW32__
71 if(sock == INVALID_SOCKET)
72#else
73 if(sock < 0)
74#endif
75 {
76 cm_log("Server", "Socket creation failure");
77 return 1;
78 }
79 int yes = 1;
80 if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0) {
81 close_socket(sock);
82 cm_log("Server", "setsockopt failure (reuseaddr)");
83 return 1;
84 }
85 if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&yes, sizeof(yes)) < 0) {
86 close_socket(sock);
87 cm_log("Server", "setsockopt failure (nodelay)");
88 return 1;
89 }
90#ifndef NO_IPV6
91 int no = 0;
92 if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&no, sizeof(no)) < 0) {
93 close_socket(sock);
94 cm_log("Server", "setsockopt failure (IPv6)");
95 return 1;
96 }
97#endif
98 memset(&addresses[i], 0, sizeof(addresses[i]));
99#ifdef NO_IPV6
100 addresses[i].sin_family = AF_INET;
101 addresses[i].sin_addr.s_addr = INADDR_ANY;
102 addresses[i].sin_port = htons(config.ports[i]);
103#else
104 addresses[i].sin6_family = AF_INET6;
105 addresses[i].sin6_addr = in6addr_any;
106 addresses[i].sin6_port = htons(config.ports[i]);
107#endif
108 if(bind(sock, (struct sockaddr*)&addresses[i], sizeof(addresses[i])) < 0) {
109 close_socket(sock);
110 cm_log("Server", "Bind failure");
111 return 1;
112 }
113 if(listen(sock, 128) < 0) {
114 close_socket(sock);
115 cm_log("Server", "Listen failure");
116 return 1;
117 }
118 sockets[i] = sock;
119 }
120 return 0;
121}
122
123size_t tw_read(SSL* ssl, int s, void* data, size_t len) {
124 if(ssl == NULL) {
125 return recv(s, data, len, 0);
126 } else {
127 return SSL_read(ssl, data, len);
128 }
129}
130
131size_t tw_write(SSL* ssl, int s, void* data, size_t len) {
132 if(ssl == NULL) {
133 return send(s, data, len, 0);
134 } else {
135 return SSL_write(ssl, data, len);
136 }
137}
138
139#define ERROR_HTML \
140 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \
141 "<html>\n" \
142 " <head>\n" \
143 " <title>%s</title>\n" \
144 " </head>\n" \
145 " <body>\n" \
146 " <h1>%s</h1>\n" \
147 " <hr>\n" \
148 " ", \
149 address, \
150 "\n" \
151 " </body>\n" \
152 "</html>\n"
153
154void tw_process_page(SSL* ssl, int sock, const char* status, const char* type, const unsigned char* doc, size_t size) {
155 char construct[512];
156 sprintf(construct, "%llu", (unsigned long long)size);
157 tw_write(ssl, sock, "HTTP/1.1 ", 9);
158 tw_write(ssl, sock, (char*)status, strlen(status));
159 tw_write(ssl, sock, "\r\n", 2);
160 tw_write(ssl, sock, "Content-Type: ", 7 + 5 + 2);
161 tw_write(ssl, sock, (char*)type, strlen(type));
162 tw_write(ssl, sock, "\r\n", 2);
163 tw_write(ssl, sock, "Server: ", 6 + 2);
164 tw_write(ssl, sock, tw_server, strlen(tw_server));
165 tw_write(ssl, sock, "\r\n", 2);
166 tw_write(ssl, sock, "Content-Length: ", 7 + 7 + 2);
167 tw_write(ssl, sock, construct, strlen(construct));
168 tw_write(ssl, sock, "\r\n", 2);
169 tw_write(ssl, sock, "\r\n", 2);
170 size_t incr = 0;
171 while(1) {
172 tw_write(ssl, sock, (unsigned char*)doc + incr, size < 128 ? size : 128);
173 incr += 128;
174 if(size <= 128) break;
175 size -= 128;
176 }
177}
178
179const char* tw_http_status(int code) {
180 if(code == 200) {
181 return "200 OK";
182 } else if(code == 400) {
183 return "400 Bad Request";
184 } else if(code == 401) {
185 return "401 Unauthorized";
186 } else if(code == 403) {
187 return "403 Forbidden";
188 } else if(code == 404) {
189 return "404 Not Found";
190 } else {
191 return "400 Bad Request";
192 }
193}
194
195char* tw_http_default_error(int code, char* name, int port) {
196 char address[1024];
197 sprintf(address, "<address>%s Server at %s Port %d</address>", tw_server, name, port);
198
199 char* st = cm_strdup(tw_http_status(code));
200 char* st2;
201 int i;
202 for(i = 0; st[i] != 0; i++) {
203 if(st[i] == ' ') {
204 st2 = cm_strdup(st + i + 1);
205 break;
206 }
207 }
208 char* buffer = malloc(4096);
209 char* str = cm_strcat3(ERROR_HTML);
210 sprintf(buffer, str, st, st2);
211 free(str);
212 free(st);
213 return buffer;
214}
215
216void tw_http_error(SSL* ssl, int sock, int error, char* name, int port) {
217 char* str = tw_http_default_error(error, name, port);
218 tw_process_page(ssl, sock, tw_http_status(error), "text/html", str, strlen(str));
219 free(str);
220}
221
222void addstring(char** str, const char* add, ...) {
223 int i;
224 char cbuf[2];
225 cbuf[1] = 0;
226 va_list va;
227 va_start(va, add);
228 for(i = 0; add[i] != 0; i++) {
229 cbuf[0] = add[i];
230 if(add[i] == '%') {
231 i++;
232 if(add[i] == 's') {
233 char* tmp = *str;
234 *str = cm_strcat(tmp, va_arg(va, const char*));
235 free(tmp);
236 } else if(add[i] == 'h') {
237 char* h = cm_html_escape(va_arg(va, const char*));
238 char* tmp = *str;
239 *str = cm_strcat(tmp, h);
240 free(tmp);
241 free(h);
242 } else if(add[i] == 'd') {
243 int n = va_arg(va, int);
244 char* h = malloc(512);
245 sprintf(h, "%d", n);
246 char* tmp = *str;
247 *str = cm_strcat(tmp, h);
248 free(tmp);
249 free(h);
250 } else if(add[i] == '%') {
251 char* tmp = *str;
252 *str = cm_strcat(tmp, "%");
253 free(tmp);
254 }
255 } else {
256 char* tmp = *str;
257 *str = cm_strcat(tmp, cbuf);
258 free(tmp);
259 }
260 }
261}
262
263#ifdef __MINGW32__
264struct pass_entry {
265 int sock;
266 int port;
267 bool ssl;
268};
269
270unsigned int WINAPI tw_server_pass(void* ptr) {
271 int sock = ((struct pass_entry*)ptr)->sock;
272 bool ssl = ((struct pass_entry*)ptr)->ssl;
273 int port = ((struct pass_entry*)ptr)->port;
274 free(ptr);
275#else
276void tw_server_pass(int sock, bool ssl, int port) {
277#endif
278 char* name = config.hostname;
279
280 SSL_CTX* ctx = NULL;
281 SSL* s = NULL;
282 bool sslworks = false;
283 if(ssl) {
284 ctx = tw_create_ssl_ctx(port);
285 s = SSL_new(ctx);
286 SSL_set_fd(s, sock);
287 if(SSL_accept(s) <= 0) goto cleanup;
288 sslworks = true;
289 }
290 struct tw_http_request req;
291 struct tw_http_response res;
292 struct tw_tool tools;
293 res._processed = false;
294 tw_init_tools(&tools);
295 int ret = tw_http_parse(s, sock, &req);
296 if(ret == 0) {
297 int i;
298 for(i = 0; i < config.module_count; i++) {
299 tw_mod_request_t mod_req = (tw_mod_request_t)tw_module_symbol(config.modules[i], "mod_request");
300 if(mod_req != NULL) {
301 int ret = mod_req(&tools, &req, &res);
302 int co = ret & 0xff;
303 if(co == _TW_MODULE_PASS) continue;
304 if(co == _TW_MODULE_STOP) {
305 res._processed = true;
306 break;
307 }
308 if(co == _TW_MODULE_ERROR) {
309 tw_http_error(s, sock, (ret & 0xffff00) >> 8, name, port);
310 break;
311 }
312 }
313 }
314 if(!res._processed) {
315 char* str = malloc(1);
316 str[0] = 0;
317 addstring(&str, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n");
318 addstring(&str, "<html>\n");
319 addstring(&str, " <head>\n");
320 addstring(&str, " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
321 addstring(&str, " <title>Index of %h</title>\n", req.path);
322 addstring(&str, " </head>\n");
323 addstring(&str, " <body>\n");
324 addstring(&str, " <h1>Index of %h</h1>\n", req.path);
325 addstring(&str, " <hr>\n");
326 addstring(&str, " <table border=\"0\">\n");
327 addstring(&str, " <tr>\n");
328 addstring(&str, " <th></th>\n");
329 addstring(&str, " <th>Filename</th>\n");
330 addstring(&str, " </tr>\n");
331 addstring(&str, " </table>\n");
332 addstring(&str, " <hr>\n");
333 addstring(&str, " <address>%s Server at %s Port %d</address>\n", tw_server, name, port);
334 addstring(&str, " </body>\n");
335 addstring(&str, "</html>\n");
336 tw_process_page(s, sock, tw_http_status(200), "text/html", str, strlen(str));
337 free(str);
338 }
339 } else {
340 tw_http_error(s, sock, 400, name, port);
341 }
342cleanup:
343 if(sslworks) {
344 SSL_shutdown(s);
345 }
346 SSL_free(s);
347 close_socket(sock);
348#ifdef __MINGW32__
349 _endthreadex(0);
350#endif
351}
352
353void tw_server_loop(void) {
354 struct timeval tv;
355 while(1) {
356 FD_ZERO(&fdset);
357 int i;
358 for(i = 0; i < sockcount; i++) {
359 FD_SET(sockets[i], &fdset);
360 }
361 tv.tv_sec = 1;
362 tv.tv_usec = 0;
363 int ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
364 if(ret == -1) {
365 break;
366 } else if(ret > 0) {
367 /* connection */
368 int i;
369 for(i = 0; i < sockcount; i++) {
370 if(FD_ISSET(sockets[i], &fdset)) {
371 SOCKADDR claddr;
372 int clen = sizeof(claddr);
373 int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
374 cm_log("Server", "New connection accepted");
375#ifdef __MINGW32__
376 HANDLE thread;
377 struct pass_entry* e = malloc(sizeof(*e));
378 e->sock = sock;
379 e->ssl = config.ports[i] & (1ULL << 32);
380 e->port = config.ports[i];
381 thread = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL);
382#else
383 pid_t pid = fork();
384 if(pid == 0) {
385 tw_server_pass(sock, config.ports[i] & (1ULL << 32), config.ports[i]);
386 _exit(0);
387 } else {
388 close_socket(sock);
389 }
390#endif
391 }
392 }
393 }
394 }
395}
Note: See TracBrowser for help on using the repository browser.