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

Last change on this file since 22 was 22, checked in by Nishi, on Sep 14, 2024 at 10:25:38 PM

icon works

  • Property svn:keywords set to Id
File size: 13.7 KB
RevLine 
[8]1/* $Id: server.c 22 2024-09-14 13:25:38Z nishi $ */
2
[16]3#define SOURCE
4
[8]5#include "tw_server.h"
6
[12]7#include "tw_ssl.h"
[8]8#include "tw_config.h"
[16]9#include "tw_http.h"
[18]10#include "tw_module.h"
11#include "tw_version.h"
[8]12
13#include <unistd.h>
14#include <string.h>
[11]15#include <stdbool.h>
[20]16#include <stdarg.h>
[21]17#include <sys/stat.h>
[8]18
[18]19#include <cm_string.h>
[8]20#include <cm_log.h>
[21]21#include <cm_dir.h>
[8]22
23#ifdef __MINGW32__
24#include <winsock2.h>
[11]25#include <process.h>
[8]26#else
27#include <sys/select.h>
28#include <sys/socket.h>
29#include <arpa/inet.h>
30#include <netinet/in.h>
31#include <netinet/tcp.h>
32#endif
33
34extern struct tw_config config;
[18]35extern char tw_server[];
[8]36
37fd_set fdset;
38int sockcount = 0;
39
[9]40SOCKADDR addresses[MAX_PORTS];
41int sockets[MAX_PORTS];
[8]42
43void close_socket(int sock) {
44#ifdef __MINGW32__
45 closesocket(sock);
46#else
47 close(sock);
48#endif
49}
50
51int tw_server_init(void) {
52 int i;
53#ifdef __MINGW32__
54 WSADATA wsa;
55 WSAStartup(MAKEWORD(2, 0), &wsa);
56#endif
57 for(i = 0; config.ports[i] != -1; i++)
58 ;
59 sockcount = i;
60 for(i = 0; config.ports[i] != -1; i++) {
61#ifdef NO_IPV6
62 int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
63#else
64 int sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
65#endif
66#ifdef __MINGW32__
67 if(sock == INVALID_SOCKET)
68#else
69 if(sock < 0)
70#endif
71 {
72 cm_log("Server", "Socket creation failure");
73 return 1;
74 }
75 int yes = 1;
76 if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0) {
77 close_socket(sock);
78 cm_log("Server", "setsockopt failure (reuseaddr)");
79 return 1;
80 }
81 if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&yes, sizeof(yes)) < 0) {
82 close_socket(sock);
83 cm_log("Server", "setsockopt failure (nodelay)");
84 return 1;
85 }
86#ifndef NO_IPV6
87 int no = 0;
88 if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&no, sizeof(no)) < 0) {
89 close_socket(sock);
90 cm_log("Server", "setsockopt failure (IPv6)");
91 return 1;
92 }
93#endif
94 memset(&addresses[i], 0, sizeof(addresses[i]));
95#ifdef NO_IPV6
96 addresses[i].sin_family = AF_INET;
97 addresses[i].sin_addr.s_addr = INADDR_ANY;
98 addresses[i].sin_port = htons(config.ports[i]);
99#else
100 addresses[i].sin6_family = AF_INET6;
101 addresses[i].sin6_addr = in6addr_any;
102 addresses[i].sin6_port = htons(config.ports[i]);
103#endif
104 if(bind(sock, (struct sockaddr*)&addresses[i], sizeof(addresses[i])) < 0) {
105 close_socket(sock);
106 cm_log("Server", "Bind failure");
107 return 1;
108 }
109 if(listen(sock, 128) < 0) {
110 close_socket(sock);
111 cm_log("Server", "Listen failure");
112 return 1;
113 }
114 sockets[i] = sock;
115 }
116 return 0;
117}
[9]118
[16]119size_t tw_read(SSL* ssl, int s, void* data, size_t len) {
120 if(ssl == NULL) {
121 return recv(s, data, len, 0);
122 } else {
123 return SSL_read(ssl, data, len);
124 }
125}
126
127size_t tw_write(SSL* ssl, int s, void* data, size_t len) {
128 if(ssl == NULL) {
129 return send(s, data, len, 0);
130 } else {
131 return SSL_write(ssl, data, len);
132 }
133}
134
[20]135#define ERROR_HTML \
[18]136 "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \
137 "<html>\n" \
138 " <head>\n" \
[20]139 " <title>%s</title>\n" \
[18]140 " </head>\n" \
141 " <body>\n" \
[20]142 " <h1>%s</h1>\n" \
[18]143 " <hr>\n" \
144 " ", \
145 address, \
146 "\n" \
147 " </body>\n" \
148 "</html>\n"
149
[21]150void tw_process_page(SSL* ssl, int sock, const char* status, const char* type, FILE* f, const unsigned char* doc, size_t size) {
[18]151 char construct[512];
152 sprintf(construct, "%llu", (unsigned long long)size);
153 tw_write(ssl, sock, "HTTP/1.1 ", 9);
154 tw_write(ssl, sock, (char*)status, strlen(status));
155 tw_write(ssl, sock, "\r\n", 2);
156 tw_write(ssl, sock, "Content-Type: ", 7 + 5 + 2);
157 tw_write(ssl, sock, (char*)type, strlen(type));
158 tw_write(ssl, sock, "\r\n", 2);
159 tw_write(ssl, sock, "Server: ", 6 + 2);
160 tw_write(ssl, sock, tw_server, strlen(tw_server));
161 tw_write(ssl, sock, "\r\n", 2);
162 tw_write(ssl, sock, "Content-Length: ", 7 + 7 + 2);
163 tw_write(ssl, sock, construct, strlen(construct));
164 tw_write(ssl, sock, "\r\n", 2);
165 tw_write(ssl, sock, "\r\n", 2);
166 size_t incr = 0;
167 while(1) {
[22]168 if(f != NULL) {
[21]169 char buffer[128];
170 fread(buffer, size < 128 ? size : 128, 1, f);
171 tw_write(ssl, sock, buffer, size < 128 ? size : 128);
[22]172 } else {
[21]173 tw_write(ssl, sock, (unsigned char*)doc + incr, size < 128 ? size : 128);
174 }
[18]175 incr += 128;
[19]176 if(size <= 128) break;
[18]177 size -= 128;
178 }
179}
180
181const char* tw_http_status(int code) {
[20]182 if(code == 200) {
183 return "200 OK";
184 } else if(code == 400) {
[18]185 return "400 Bad Request";
[20]186 } else if(code == 401) {
187 return "401 Unauthorized";
188 } else if(code == 403) {
189 return "403 Forbidden";
190 } else if(code == 404) {
191 return "404 Not Found";
[18]192 } else {
193 return "400 Bad Request";
194 }
195}
196
197char* tw_http_default_error(int code, char* name, int port) {
198 char address[1024];
199 sprintf(address, "<address>%s Server at %s Port %d</address>", tw_server, name, port);
[20]200
201 char* st = cm_strdup(tw_http_status(code));
202 char* st2;
203 int i;
204 for(i = 0; st[i] != 0; i++) {
205 if(st[i] == ' ') {
206 st2 = cm_strdup(st + i + 1);
207 break;
208 }
[18]209 }
[20]210 char* buffer = malloc(4096);
211 char* str = cm_strcat3(ERROR_HTML);
212 sprintf(buffer, str, st, st2);
213 free(str);
214 free(st);
215 return buffer;
[18]216}
217
218void tw_http_error(SSL* ssl, int sock, int error, char* name, int port) {
219 char* str = tw_http_default_error(error, name, port);
[21]220 tw_process_page(ssl, sock, tw_http_status(error), "text/html", NULL, str, strlen(str));
[18]221 free(str);
222}
223
[20]224void addstring(char** str, const char* add, ...) {
225 int i;
226 char cbuf[2];
227 cbuf[1] = 0;
228 va_list va;
229 va_start(va, add);
230 for(i = 0; add[i] != 0; i++) {
231 cbuf[0] = add[i];
232 if(add[i] == '%') {
233 i++;
234 if(add[i] == 's') {
235 char* tmp = *str;
236 *str = cm_strcat(tmp, va_arg(va, const char*));
237 free(tmp);
238 } else if(add[i] == 'h') {
239 char* h = cm_html_escape(va_arg(va, const char*));
240 char* tmp = *str;
241 *str = cm_strcat(tmp, h);
242 free(tmp);
243 free(h);
[21]244 } else if(add[i] == 'l') {
245 char* h = cm_url_escape(va_arg(va, const char*));
246 char* tmp = *str;
247 *str = cm_strcat(tmp, h);
248 free(tmp);
249 free(h);
[20]250 } else if(add[i] == 'd') {
251 int n = va_arg(va, int);
252 char* h = malloc(512);
253 sprintf(h, "%d", n);
254 char* tmp = *str;
255 *str = cm_strcat(tmp, h);
256 free(tmp);
257 free(h);
258 } else if(add[i] == '%') {
259 char* tmp = *str;
260 *str = cm_strcat(tmp, "%");
261 free(tmp);
262 }
263 } else {
264 char* tmp = *str;
265 *str = cm_strcat(tmp, cbuf);
266 free(tmp);
267 }
268 }
269}
270
[22]271char* tw_get_mime(const char* ext, struct tw_config_entry* vhost_entry) {
272 char* mime = "application/octet-stream";
273 if(ext == NULL) return mime;
274 bool set = false;
275 int i;
276 for(i = 0; i < vhost_entry->mime_count; i++) {
277 if(strcmp(vhost_entry->mimes[i].ext, "all") == 0 || (ext != NULL && strcmp(vhost_entry->mimes[i].ext, ext) == 0)) {
278 mime = vhost_entry->mimes[i].mime;
279 set = true;
280 }
281 }
282 if(!set) {
283 for(i = 0; i < config.root.mime_count; i++) {
284 if(strcmp(config.root.mimes[i].ext, "all") == 0 || (ext != NULL && strcmp(config.root.mimes[i].ext, ext) == 0)) {
285 mime = config.root.mimes[i].mime;
286 }
287 }
288 }
289 return mime;
290}
291
292char* tw_get_icon(const char* mime, struct tw_config_entry* vhost_entry) {
293 char* icon = "";
294 if(mime == NULL) return "";
295 bool set = false;
296 int i;
297 for(i = 0; i < vhost_entry->icon_count; i++) {
298 if(strcmp(vhost_entry->icons[i].mime, "all") == 0 || (mime != NULL && strcmp(vhost_entry->icons[i].mime, mime) == 0)) {
299 icon = vhost_entry->icons[i].icon;
300 set = true;
301 }
302 }
303 if(!set) {
304 for(i = 0; i < config.root.icon_count; i++) {
305 if(strcmp(config.root.icons[i].mime, "all") == 0 || (mime != NULL && strcmp(config.root.icons[i].mime, mime) == 0)) {
306 icon = config.root.icons[i].icon;
307 }
308 }
309 }
310 return icon;
311}
312
[11]313#ifdef __MINGW32__
314struct pass_entry {
315 int sock;
[12]316 int port;
[11]317 bool ssl;
[21]318 SOCKADDR addr;
[11]319};
320
321unsigned int WINAPI tw_server_pass(void* ptr) {
322 int sock = ((struct pass_entry*)ptr)->sock;
323 bool ssl = ((struct pass_entry*)ptr)->ssl;
[14]324 int port = ((struct pass_entry*)ptr)->port;
[21]325 SOCKADDR addr = ((struct pass_entry*)ptr)->addr;
[12]326 free(ptr);
[11]327#else
[21]328void tw_server_pass(int sock, bool ssl, int port, SOCKADDR addr) {
[11]329#endif
[13]330 char* name = config.hostname;
331
[12]332 SSL_CTX* ctx = NULL;
333 SSL* s = NULL;
[15]334 bool sslworks = false;
[12]335 if(ssl) {
336 ctx = tw_create_ssl_ctx(port);
337 s = SSL_new(ctx);
338 SSL_set_fd(s, sock);
339 if(SSL_accept(s) <= 0) goto cleanup;
[15]340 sslworks = true;
[12]341 }
[16]342 struct tw_http_request req;
[20]343 struct tw_http_response res;
344 struct tw_tool tools;
345 res._processed = false;
346 tw_init_tools(&tools);
[16]347 int ret = tw_http_parse(s, sock, &req);
[17]348 if(ret == 0) {
[21]349 char* vhost = cm_strdup(config.hostname);
[20]350 int i;
[22]351 for(i = 0; req.headers[i] != NULL; i += 2) {
352 if(cm_strcaseequ(req.headers[i], "Host")) {
[21]353 free(vhost);
354 vhost = req.headers[i + 1];
355 break;
356 }
357 }
358 cm_log("Server", "Host is %s", vhost);
359 int port = s == NULL ? 80 : 443;
360 char* host = cm_strdup(vhost);
[22]361 for(i = 0; vhost[i] != 0; i++) {
362 if(vhost[i] == ':') {
[21]363 host[i] = 0;
364 port = atoi(host + i + 1);
365 break;
366 }
367 }
368 cm_log("Server", "Hostname is `%s', port is `%d'", host, port);
369 struct tw_config_entry* vhost_entry = tw_vhost_match(host, port);
[20]370 for(i = 0; i < config.module_count; i++) {
371 tw_mod_request_t mod_req = (tw_mod_request_t)tw_module_symbol(config.modules[i], "mod_request");
372 if(mod_req != NULL) {
373 int ret = mod_req(&tools, &req, &res);
374 int co = ret & 0xff;
375 if(co == _TW_MODULE_PASS) continue;
376 if(co == _TW_MODULE_STOP) {
377 res._processed = true;
378 break;
379 }
380 if(co == _TW_MODULE_ERROR) {
381 tw_http_error(s, sock, (ret & 0xffff00) >> 8, name, port);
382 break;
383 }
384 }
385 }
386 if(!res._processed) {
[21]387 cm_log("Server", "Document root is %s", vhost_entry->root == NULL ? "not set" : vhost_entry->root);
388 char* path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path);
389 cm_log("Server", "Filesystem path is %s", path);
390 struct stat st;
[22]391 if(stat(path, &st) == 0) {
392 if(!tw_permission_allowed(path, addr, req, vhost_entry)) {
[21]393 tw_http_error(s, sock, 403, name, port);
[22]394 } else if(S_ISDIR(st.st_mode)) {
[21]395 char* str = malloc(1);
396 str[0] = 0;
397 char** items = cm_scandir(path);
398 addstring(&str, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n");
399 addstring(&str, "<html>\n");
400 addstring(&str, " <head>\n");
401 addstring(&str, " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
402 addstring(&str, " <title>Index of %h</title>\n", req.path);
403 addstring(&str, " </head>\n");
404 addstring(&str, " <body>\n");
405 addstring(&str, " <h1>Index of %h</h1>\n", req.path);
406 addstring(&str, " <hr>\n");
407 addstring(&str, " <table border=\"0\">\n");
408 addstring(&str, " <tr>\n");
409 addstring(&str, " <th></th>\n");
410 addstring(&str, " <th>Filename</th>\n");
411 addstring(&str, " </tr>\n");
[22]412 if(items != NULL) {
413 for(i = 0; items[i] != NULL; i++) {
414 char* ext = NULL;
415 int j;
416 for(j = strlen(items[i]) - 1; j >= 0; j--) {
417 if(items[i][j] == '.') {
418 ext = cm_strdup(items[i] + j);
419 break;
420 }
421 }
422 char* mime = tw_get_mime(ext, vhost_entry);
423 if(strcmp(items[i], "../") == 0) {
424 mime = "misc/parent";
425 } else if(items[i][strlen(items[i]) - 1] == '/') {
426 mime = "misc/dir";
427 }
428 char* icon = tw_get_icon(mime, vhost_entry);
429 if(ext != NULL) free(ext);
430 char* itm = cm_strdup(items[i]);
431 if(strlen(itm) >= 32) {
432 if(itm[strlen(itm) - 1] == '/') {
433 itm[31] = 0;
434 itm[30] = '/';
435 itm[29] = '.';
436 itm[28] = '.';
437 itm[27] = '.';
438 } else {
439 itm[31] = 0;
440 itm[30] = '.';
441 itm[29] = '.';
442 itm[28] = '.';
443 }
444 }
[21]445 addstring(&str, "<tr>\n");
[22]446 addstring(&str, " <td><img src=\"%s\" alt=\"icon\"></td>\n", icon);
447 addstring(&str, " <td><a href=\"%l\"><code>%h</code></a></td>\n", items[i], itm);
[21]448 addstring(&str, "</tr>\n");
[22]449 free(itm);
[21]450 }
451 }
452 addstring(&str, " </table>\n");
453 addstring(&str, " <hr>\n");
454 addstring(&str, " <address>%s Server at %s Port %d</address>\n", tw_server, name, port);
455 addstring(&str, " </body>\n");
456 addstring(&str, "</html>\n");
457 tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str));
458 free(str);
[22]459 } else {
[21]460 char* ext = NULL;
[22]461 for(i = strlen(req.path) - 1; i >= 0; i--) {
462 if(req.path[i] == '.') {
[21]463 ext = cm_strdup(req.path + i);
464 break;
465 }
466 }
[22]467 char* mime = tw_get_mime(ext, vhost_entry);
[21]468 if(ext != NULL) free(ext);
469 FILE* f = fopen(path, "rb");
470 tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size);
471 fclose(f);
472 }
[22]473 } else {
[21]474 tw_http_error(s, sock, 404, name, port);
475 }
476 free(path);
[20]477 }
[21]478 free(vhost);
479 free(host);
[22]480 } else if(ret == -1) {
[18]481 } else {
482 tw_http_error(s, sock, 400, name, port);
[17]483 }
[12]484cleanup:
[16]485 if(sslworks) {
[15]486 SSL_shutdown(s);
487 }
488 SSL_free(s);
[11]489 close_socket(sock);
490#ifdef __MINGW32__
491 _endthreadex(0);
492#endif
493}
494
495void tw_server_loop(void) {
[9]496 struct timeval tv;
[11]497 while(1) {
[10]498 FD_ZERO(&fdset);
499 int i;
[11]500 for(i = 0; i < sockcount; i++) {
[10]501 FD_SET(sockets[i], &fdset);
502 }
[9]503 tv.tv_sec = 1;
504 tv.tv_usec = 0;
[10]505 int ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
[11]506 if(ret == -1) {
[9]507 break;
[11]508 } else if(ret > 0) {
[9]509 /* connection */
510 int i;
[11]511 for(i = 0; i < sockcount; i++) {
512 if(FD_ISSET(sockets[i], &fdset)) {
[9]513 SOCKADDR claddr;
514 int clen = sizeof(claddr);
515 int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
[12]516 cm_log("Server", "New connection accepted");
[11]517#ifdef __MINGW32__
518 HANDLE thread;
519 struct pass_entry* e = malloc(sizeof(*e));
520 e->sock = sock;
521 e->ssl = config.ports[i] & (1ULL << 32);
[12]522 e->port = config.ports[i];
[21]523 e->addr = claddr;
[11]524 thread = (HANDLE)_beginthreadex(NULL, 0, tw_server_pass, e, 0, NULL);
525#else
526 pid_t pid = fork();
527 if(pid == 0) {
[21]528 tw_server_pass(sock, config.ports[i] & (1ULL << 32), config.ports[i], claddr);
[11]529 _exit(0);
530 } else {
531 close_socket(sock);
532 }
533#endif
[9]534 }
535 }
536 }
537 }
538}
Note: See TracBrowser for help on using the repository browser.