Subversion Repositories Tewi

Rev

Rev 311 | Rev 314 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8 nishi 1
/* $Id: server.c 312 2024-10-13 18:17:37Z nishi $ */
2
 
16 nishi 3
#define SOURCE
4
 
43 nishi 5
#include "../config.h"
6
 
8 nishi 7
#include "tw_server.h"
8
 
43 nishi 9
#ifndef NO_SSL
12 nishi 10
#include "tw_ssl.h"
43 nishi 11
#endif
12
 
8 nishi 13
#include "tw_config.h"
16 nishi 14
#include "tw_http.h"
18 nishi 15
#include "tw_module.h"
16
#include "tw_version.h"
8 nishi 17
 
215 nishi 18
#if !defined(_MSC_VER) && !defined(__BORLANDC__)
8 nishi 19
#include <unistd.h>
212 nishi 20
#endif
312 nishi 21
#include <ctype.h>
8 nishi 22
#include <string.h>
11 nishi 23
#include <stdbool.h>
20 nishi 24
#include <stdarg.h>
43 nishi 25
#include <stdio.h>
26
#include <stdlib.h>
84 nishi 27
#include <errno.h>
212 nishi 28
#include <sys/types.h>
21 nishi 29
#include <sys/stat.h>
32 nishi 30
#include <time.h>
8 nishi 31
 
18 nishi 32
#include <cm_string.h>
8 nishi 33
#include <cm_log.h>
21 nishi 34
#include <cm_dir.h>
8 nishi 35
 
312 nishi 36
#ifdef __OS2__
37
#include <types.h>
38
#include <sys/time.h>
39
#define INCL_DOSPROCESS
40
#include <os2.h>
41
#include <process.h>
42
#define HANDLE void*
43
 
44
#include "strptime.h"
45
typedef int socklen_t;
46
 
47
#include <tcpustd.h>
48
#endif
49
 
311 nishi 50
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && !defined(__OS2__))
255 nishi 51
#ifndef NO_GETNAMEINFO
116 nishi 52
#include <ws2tcpip.h>
53
#include <wspiapi.h>
163 nishi 54
#endif
240 nishi 55
#ifdef USE_WINSOCK1
56
#include <winsock.h>
57
#else
8 nishi 58
#include <winsock2.h>
240 nishi 59
#endif
11 nishi 60
#include <process.h>
62 nishi 61
#include <windows.h>
32 nishi 62
 
63
#include "strptime.h"
216 nishi 64
typedef int socklen_t;
8 nishi 65
#else
118 nishi 66
#ifdef USE_POLL
187 nishi 67
#ifdef __PPU__
68
#include <net/poll.h>
69
#else
118 nishi 70
#include <poll.h>
187 nishi 71
#endif
118 nishi 72
#else
8 nishi 73
#include <sys/select.h>
118 nishi 74
#endif
8 nishi 75
#include <sys/socket.h>
76
#include <arpa/inet.h>
77
#include <netinet/in.h>
187 nishi 78
#ifndef __PPU__
8 nishi 79
#include <netinet/tcp.h>
187 nishi 80
#endif
255 nishi 81
#ifndef NO_GETNAMEINFO
116 nishi 82
#include <netdb.h>
8 nishi 83
#endif
163 nishi 84
#endif
8 nishi 85
 
189 nishi 86
#if defined(_PSP) || defined(__ps2sdk__)
182 nishi 87
#include "strptime.h"
88
#endif
89
 
97 nishi 90
#ifdef __HAIKU__
91
#include <OS.h>
92
#endif
93
 
303 nishi 94
#ifdef __USLC__
95
typedef int socklen_t;
96
#endif
97
 
212 nishi 98
#ifndef S_ISDIR
272 nishi 99
#define S_ISDIR(x) ((x) & _S_IFDIR)
212 nishi 100
#endif
101
 
8 nishi 102
extern struct tw_config config;
18 nishi 103
extern char tw_server[];
8 nishi 104
 
105
int sockcount = 0;
106
 
9 nishi 107
SOCKADDR addresses[MAX_PORTS];
108
int sockets[MAX_PORTS];
8 nishi 109
 
219 nishi 110
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
70 nishi 111
const char* reserved_names[] = {"CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"};
112
#endif
113
 
23 nishi 114
/* https://qiita.com/gyu-don/items/5a640c6d2252a860c8cd */
115
int tw_wildcard_match(const char* wildcard, const char* target) {
116
	const char *pw = wildcard, *pt = target;
117
 
118
	while(1) {
119
		if(*pt == 0) {
120
			while(*pw == '*') pw++;
121
			return *pw == 0;
122
		} else if(*pw == 0) {
123
			return 0;
124
		} else if(*pw == '*') {
125
			return *(pw + 1) == 0 || tw_wildcard_match(pw, pt + 1) || tw_wildcard_match(pw + 1, pt);
312 nishi 126
		} else if(*pw == '?' || (tolower(*pw) == tolower(*pt))) {
23 nishi 127
			pw++;
128
			pt++;
129
			continue;
130
		} else {
131
			return 0;
132
		}
133
	}
134
}
135
 
8 nishi 136
void close_socket(int sock) {
312 nishi 137
#ifdef __OS2__
138
	soclose(sock);
139
#elif defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
8 nishi 140
	closesocket(sock);
141
#else
142
	close(sock);
143
#endif
144
}
145
 
146
int tw_server_init(void) {
147
	int i;
312 nishi 148
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || (defined(__WATCOMC__) && !defined(__OS2__))
8 nishi 149
	WSADATA wsa;
240 nishi 150
#ifdef USE_WINSOCK1
151
	WSAStartup(MAKEWORD(1, 1), &wsa);
152
#else
8 nishi 153
	WSAStartup(MAKEWORD(2, 0), &wsa);
154
#endif
240 nishi 155
#endif
312 nishi 156
#ifdef __OS2__
157
	sock_init();
158
#endif
8 nishi 159
	for(i = 0; config.ports[i] != -1; i++)
160
		;
161
	sockcount = i;
162
	for(i = 0; config.ports[i] != -1; i++) {
212 nishi 163
		int yes = 1;
164
		int no = 0;
8 nishi 165
#ifdef NO_IPV6
166
		int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
167
#else
168
		int sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
169
#endif
215 nishi 170
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__)
8 nishi 171
		if(sock == INVALID_SOCKET)
172
#else
173
		if(sock < 0)
174
#endif
175
		{
176
			cm_log("Server", "Socket creation failure");
177
			return 1;
178
		}
179
		if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void*)&yes, sizeof(yes)) < 0) {
180
			close_socket(sock);
181
			cm_log("Server", "setsockopt failure (reuseaddr)");
182
			return 1;
183
		}
275 nishi 184
#if !defined(__PPU__) && !defined(__minix)
8 nishi 185
		if(setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void*)&yes, sizeof(yes)) < 0) {
186
			close_socket(sock);
187
			cm_log("Server", "setsockopt failure (nodelay)");
188
			return 1;
189
		}
187 nishi 190
#endif
8 nishi 191
#ifndef NO_IPV6
192
		if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&no, sizeof(no)) < 0) {
193
			close_socket(sock);
194
			cm_log("Server", "setsockopt failure (IPv6)");
195
			return 1;
196
		}
197
#endif
198
		memset(&addresses[i], 0, sizeof(addresses[i]));
199
#ifdef NO_IPV6
200
		addresses[i].sin_family = AF_INET;
201
		addresses[i].sin_addr.s_addr = INADDR_ANY;
214 nishi 202
		addresses[i].sin_port = htons(config.ports[i] & 0xffff);
8 nishi 203
#else
204
		addresses[i].sin6_family = AF_INET6;
205
		addresses[i].sin6_addr = in6addr_any;
214 nishi 206
		addresses[i].sin6_port = htons(config.ports[i] & 0xffff);
8 nishi 207
#endif
208
		if(bind(sock, (struct sockaddr*)&addresses[i], sizeof(addresses[i])) < 0) {
209
			close_socket(sock);
210
			cm_log("Server", "Bind failure");
211
			return 1;
212
		}
213
		if(listen(sock, 128) < 0) {
214
			close_socket(sock);
215
			cm_log("Server", "Listen failure");
216
			return 1;
217
		}
218
		sockets[i] = sock;
219
	}
220
	return 0;
221
}
9 nishi 222
 
16 nishi 223
size_t tw_read(SSL* ssl, int s, void* data, size_t len) {
43 nishi 224
#ifndef NO_SSL
16 nishi 225
	if(ssl == NULL) {
226
		return recv(s, data, len, 0);
227
	} else {
228
		return SSL_read(ssl, data, len);
229
	}
43 nishi 230
#else
231
	return recv(s, data, len, 0);
232
#endif
16 nishi 233
}
234
 
235
size_t tw_write(SSL* ssl, int s, void* data, size_t len) {
43 nishi 236
#ifndef NO_SSL
16 nishi 237
	if(ssl == NULL) {
238
		return send(s, data, len, 0);
239
	} else {
240
		return SSL_write(ssl, data, len);
241
	}
43 nishi 242
#else
243
	return send(s, data, len, 0);
244
#endif
16 nishi 245
}
246
 
20 nishi 247
#define ERROR_HTML \
18 nishi 248
	"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \
249
	"<html>\n" \
250
	"	<head>\n" \
20 nishi 251
	"		<title>%s</title>\n" \
18 nishi 252
	"	</head>\n" \
253
	"	<body>\n" \
20 nishi 254
	"		<h1>%s</h1>\n" \
18 nishi 255
	"		<hr>\n" \
256
	"		", \
257
	    address, \
258
	    "\n" \
259
	    "	</body>\n" \
260
	    "</html>\n"
261
 
32 nishi 262
void _tw_process_page(SSL* ssl, int sock, const char* status, const char* type, FILE* f, const unsigned char* doc, size_t size, char** headers, time_t mtime, time_t cmtime) {
18 nishi 263
	char construct[512];
212 nishi 264
	size_t incr;
32 nishi 265
	if(mtime != 0 && cmtime != 0 && mtime <= cmtime) {
266
		status = "304 Not Modified";
267
		type = NULL;
268
		size = 0;
269
		headers = NULL;
270
		f = NULL;
271
		doc = NULL;
272
	}
215 nishi 273
#if defined(_MSC_VER) || defined(__BORLANDC__)
212 nishi 274
	sprintf(construct, "%lu", (unsigned long)size);
275
#else
18 nishi 276
	sprintf(construct, "%llu", (unsigned long long)size);
212 nishi 277
#endif
18 nishi 278
	tw_write(ssl, sock, "HTTP/1.1 ", 9);
279
	tw_write(ssl, sock, (char*)status, strlen(status));
280
	tw_write(ssl, sock, "\r\n", 2);
24 nishi 281
	if(type != NULL) {
282
		tw_write(ssl, sock, "Content-Type: ", 7 + 5 + 2);
283
		tw_write(ssl, sock, (char*)type, strlen(type));
284
		tw_write(ssl, sock, "\r\n", 2);
285
	}
18 nishi 286
	tw_write(ssl, sock, "Server: ", 6 + 2);
287
	tw_write(ssl, sock, tw_server, strlen(tw_server));
288
	tw_write(ssl, sock, "\r\n", 2);
24 nishi 289
	if(size != 0) {
290
		tw_write(ssl, sock, "Content-Length: ", 7 + 7 + 2);
291
		tw_write(ssl, sock, construct, strlen(construct));
292
		tw_write(ssl, sock, "\r\n", 2);
32 nishi 293
		if(mtime != 0) {
294
			struct tm* tm = gmtime(&mtime);
295
			char date[513];
296
			strftime(date, 512, "%a, %d %b %Y %H:%M:%S GMT", tm);
297
			tw_write(ssl, sock, "Last-Modified: ", 5 + 8 + 2);
298
			tw_write(ssl, sock, date, strlen(date));
299
			tw_write(ssl, sock, "\r\n", 2);
300
		}
24 nishi 301
	}
302
	if(headers != NULL) {
212 nishi 303
		int i;
24 nishi 304
		for(i = 0; headers[i] != NULL; i += 2) {
305
			tw_write(ssl, sock, headers[i], strlen(headers[i]));
306
			tw_write(ssl, sock, ": ", 2);
307
			tw_write(ssl, sock, headers[i + 1], strlen(headers[i + 1]));
308
			tw_write(ssl, sock, "\r\n", 2);
309
		}
310
	}
18 nishi 311
	tw_write(ssl, sock, "\r\n", 2);
24 nishi 312
	if(doc == NULL && f == NULL) return;
212 nishi 313
	incr = 0;
18 nishi 314
	while(1) {
22 nishi 315
		if(f != NULL) {
21 nishi 316
			char buffer[128];
317
			fread(buffer, size < 128 ? size : 128, 1, f);
318
			tw_write(ssl, sock, buffer, size < 128 ? size : 128);
22 nishi 319
		} else {
21 nishi 320
			tw_write(ssl, sock, (unsigned char*)doc + incr, size < 128 ? size : 128);
321
		}
18 nishi 322
		incr += 128;
19 nishi 323
		if(size <= 128) break;
18 nishi 324
		size -= 128;
325
	}
326
}
327
 
32 nishi 328
void tw_process_page(SSL* ssl, int sock, const char* status, const char* type, FILE* f, const unsigned char* doc, size_t size, time_t mtime, time_t cmtime) { _tw_process_page(ssl, sock, status, type, f, doc, size, NULL, mtime, cmtime); }
24 nishi 329
 
18 nishi 330
const char* tw_http_status(int code) {
20 nishi 331
	if(code == 200) {
332
		return "200 OK";
166 nishi 333
	} else if(code == 301) {
167 nishi 334
		return "301 Moved Permanently";
24 nishi 335
	} else if(code == 308) {
336
		return "308 Permanent Redirect";
20 nishi 337
	} else if(code == 400) {
18 nishi 338
		return "400 Bad Request";
20 nishi 339
	} else if(code == 401) {
340
		return "401 Unauthorized";
341
	} else if(code == 403) {
342
		return "403 Forbidden";
343
	} else if(code == 404) {
344
		return "404 Not Found";
161 nishi 345
	} else if(code == 500) {
346
		return "500 Internal Server Error";
18 nishi 347
	} else {
348
		return "400 Bad Request";
349
	}
350
}
351
 
123 nishi 352
char* tw_http_default_error(int code, char* name, int port, struct tw_config_entry* vhost) {
18 nishi 353
	char address[1024];
212 nishi 354
	char* st;
355
	char* st2;
356
	char* buffer;
357
	char* str;
358
	int i;
20 nishi 359
 
123 nishi 360
	if((vhost->hideport == -1 ? config.root.hideport : vhost->hideport) == 1) {
361
		sprintf(address, "<address>%s Server at %s</address>", tw_server, name, port);
362
	} else {
363
		sprintf(address, "<address>%s Server at %s Port %d</address>", tw_server, name, port);
364
	}
365
 
212 nishi 366
	st = cm_strdup(tw_http_status(code));
20 nishi 367
	for(i = 0; st[i] != 0; i++) {
368
		if(st[i] == ' ') {
369
			st2 = cm_strdup(st + i + 1);
370
			break;
371
		}
18 nishi 372
	}
212 nishi 373
	buffer = malloc(4096);
374
	str = cm_strcat3(ERROR_HTML);
20 nishi 375
	sprintf(buffer, str, st, st2);
376
	free(str);
377
	free(st);
378
	return buffer;
18 nishi 379
}
380
 
123 nishi 381
void tw_http_error(SSL* ssl, int sock, int error, char* name, int port, struct tw_config_entry* vhost) {
382
	char* str = tw_http_default_error(error, name, port, vhost);
32 nishi 383
	tw_process_page(ssl, sock, tw_http_status(error), "text/html", NULL, str, strlen(str), 0, 0);
18 nishi 384
	free(str);
385
}
386
 
20 nishi 387
void addstring(char** str, const char* add, ...) {
388
	int i;
389
	char cbuf[2];
212 nishi 390
	va_list va;
20 nishi 391
	cbuf[1] = 0;
392
	va_start(va, add);
393
	for(i = 0; add[i] != 0; i++) {
394
		cbuf[0] = add[i];
395
		if(add[i] == '%') {
396
			i++;
397
			if(add[i] == 's') {
398
				char* tmp = *str;
399
				*str = cm_strcat(tmp, va_arg(va, const char*));
400
				free(tmp);
401
			} else if(add[i] == 'h') {
402
				char* h = cm_html_escape(va_arg(va, const char*));
403
				char* tmp = *str;
404
				*str = cm_strcat(tmp, h);
405
				free(tmp);
406
				free(h);
21 nishi 407
			} else if(add[i] == 'l') {
408
				char* h = cm_url_escape(va_arg(va, const char*));
409
				char* tmp = *str;
410
				*str = cm_strcat(tmp, h);
411
				free(tmp);
412
				free(h);
20 nishi 413
			} else if(add[i] == 'd') {
414
				int n = va_arg(va, int);
415
				char* h = malloc(512);
212 nishi 416
				char* tmp = *str;
20 nishi 417
				sprintf(h, "%d", n);
418
				*str = cm_strcat(tmp, h);
419
				free(tmp);
420
				free(h);
421
			} else if(add[i] == '%') {
422
				char* tmp = *str;
423
				*str = cm_strcat(tmp, "%");
424
				free(tmp);
425
			}
426
		} else {
427
			char* tmp = *str;
428
			*str = cm_strcat(tmp, cbuf);
429
			free(tmp);
430
		}
431
	}
74 nishi 432
	va_end(va);
20 nishi 433
}
434
 
22 nishi 435
char* tw_get_mime(const char* ext, struct tw_config_entry* vhost_entry) {
436
	char* mime = "application/octet-stream";
437
	bool set = false;
438
	int i;
212 nishi 439
	if(ext == NULL) return mime;
22 nishi 440
	for(i = 0; i < vhost_entry->mime_count; i++) {
23 nishi 441
		if(strcmp(vhost_entry->mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(vhost_entry->mimes[i].ext, ext))) {
22 nishi 442
			mime = vhost_entry->mimes[i].mime;
443
			set = true;
444
		}
445
	}
446
	if(!set) {
447
		for(i = 0; i < config.root.mime_count; i++) {
23 nishi 448
			if(strcmp(config.root.mimes[i].ext, "all") == 0 || (ext != NULL && tw_wildcard_match(config.root.mimes[i].ext, ext))) {
22 nishi 449
				mime = config.root.mimes[i].mime;
450
			}
451
		}
452
	}
453
	return mime;
454
}
455
 
456
char* tw_get_icon(const char* mime, struct tw_config_entry* vhost_entry) {
457
	char* icon = "";
458
	bool set = false;
459
	int i;
212 nishi 460
	if(mime == NULL) return "";
22 nishi 461
	for(i = 0; i < vhost_entry->icon_count; i++) {
23 nishi 462
		if(strcmp(vhost_entry->icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(vhost_entry->icons[i].mime, mime))) {
22 nishi 463
			icon = vhost_entry->icons[i].icon;
464
			set = true;
465
		}
466
	}
467
	if(!set) {
468
		for(i = 0; i < config.root.icon_count; i++) {
23 nishi 469
			if(strcmp(config.root.icons[i].mime, "all") == 0 || (mime != NULL && tw_wildcard_match(config.root.icons[i].mime, mime))) {
22 nishi 470
				icon = config.root.icons[i].icon;
471
			}
472
		}
473
	}
474
	return icon;
475
}
476
 
11 nishi 477
struct pass_entry {
478
	int sock;
12 nishi 479
	int port;
11 nishi 480
	bool ssl;
21 nishi 481
	SOCKADDR addr;
11 nishi 482
};
483
 
219 nishi 484
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
216 nishi 485
#define NO_RETURN_THREAD
215 nishi 486
void tw_server_pass(void* ptr) {
97 nishi 487
#elif defined(__HAIKU__)
488
int32_t tw_server_pass(void* ptr) {
187 nishi 489
#elif defined(_PSP) || defined(__PPU__)
183 nishi 490
int tw_server_pass(void* ptr) {
105 nishi 491
#endif
219 nishi 492
#if defined(__HAIKU__) || defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
212 nishi 493
#define FREE_PTR
11 nishi 494
	int sock = ((struct pass_entry*)ptr)->sock;
495
	bool ssl = ((struct pass_entry*)ptr)->ssl;
14 nishi 496
	int port = ((struct pass_entry*)ptr)->port;
21 nishi 497
	SOCKADDR addr = ((struct pass_entry*)ptr)->addr;
11 nishi 498
#else
216 nishi 499
#define NO_RETURN_THREAD
105 nishi 500
	void tw_server_pass(int sock, bool ssl, int port, SOCKADDR addr) {
11 nishi 501
#endif
212 nishi 502
	SSL* s = NULL;
43 nishi 503
#ifndef NO_SSL
12 nishi 504
	SSL_CTX* ctx = NULL;
15 nishi 505
	bool sslworks = false;
12 nishi 506
	if(ssl) {
507
		ctx = tw_create_ssl_ctx(port);
508
		s = SSL_new(ctx);
509
		SSL_set_fd(s, sock);
510
		if(SSL_accept(s) <= 0) goto cleanup;
15 nishi 511
		sslworks = true;
12 nishi 512
	}
43 nishi 513
#endif
212 nishi 514
	char* name = config.hostname;
116 nishi 515
	char address[513];
212 nishi 516
	int ret;
517
	struct tw_http_request req;
518
	struct tw_http_response res;
519
	struct tw_tool tools;
255 nishi 520
	char* addrstr;
521
#ifndef NO_GETNAMEINFO
116 nishi 522
	struct sockaddr* sa = (struct sockaddr*)&addr;
523
	getnameinfo(sa, sizeof(addr), address, 512, NULL, 0, NI_NUMERICHOST);
257 nishi 524
#else
525
		addrstr = inet_ntoa(addr.sin_addr);
526
		strcpy(address, addrstr);
527
		address[strlen(addrstr)] = 0;
163 nishi 528
#endif
212 nishi 529
#ifdef FREE_PTR
530
	free(ptr);
531
#endif
116 nishi 532
 
20 nishi 533
	res._processed = false;
534
	tw_init_tools(&tools);
212 nishi 535
	ret = tw_http_parse(s, sock, &req);
17 nishi 536
	if(ret == 0) {
116 nishi 537
		char date[513];
538
		time_t t = time(NULL);
539
		struct tm* tm = localtime(&t);
212 nishi 540
		char* useragent = cm_strdup("");
541
		int i;
542
		char* tmp;
543
		char* tmp2;
544
		char* tmp3;
545
		char* tmp4;
546
		char* tmp5;
547
		char* log;
548
		char* vhost;
549
		time_t cmtime;
550
		bool rej;
551
		char* host;
552
		int port;
293 nishi 553
		char* chrootpath;
212 nishi 554
		struct tw_config_entry* vhost_entry;
116 nishi 555
		strftime(date, 512, "%a, %d %b %Y %H:%M:%S %Z", tm);
556
 
117 nishi 557
		for(i = 0; req.headers[i] != NULL; i += 2) {
558
			if(cm_strcaseequ(req.headers[i], "User-Agent")) {
116 nishi 559
				free(useragent);
560
				useragent = cm_strdup(req.headers[i + 1]);
561
			}
562
		}
563
 
212 nishi 564
		tmp = cm_strcat3(address, " - [", date);
565
		tmp2 = cm_strcat3(tmp, "] \"", req.method);
566
		tmp3 = cm_strcat3(tmp2, " ", req.path);
567
		tmp4 = cm_strcat3(tmp3, " ", req.version);
568
		tmp5 = cm_strcat3(tmp4, "\" \"", useragent);
569
		log = cm_strcat(tmp5, "\"");
116 nishi 570
		free(tmp);
571
		free(tmp2);
572
		free(tmp3);
573
		free(tmp4);
574
		free(tmp5);
575
		free(useragent);
576
		cm_force_log(log);
577
		free(log);
578
 
212 nishi 579
		vhost = cm_strdup(config.hostname);
580
		cmtime = 0;
70 nishi 581
		if(req.headers != NULL) {
64 nishi 582
			for(i = 0; req.headers[i] != NULL; i += 2) {
583
				if(cm_strcaseequ(req.headers[i], "Host")) {
584
					free(vhost);
585
					vhost = cm_strdup(req.headers[i + 1]);
586
				} else if(cm_strcaseequ(req.headers[i], "If-Modified-Since")) {
587
					struct tm tm;
212 nishi 588
					time_t t;
589
					struct tm* btm;
64 nishi 590
					strptime(req.headers[i + 1], "%a, %d %b %Y %H:%M:%S GMT", &tm);
304 nishi 591
#if defined(__MINGW32__) || defined(_PSP) || defined(__PPU__) || defined(__ps2sdk__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__USLC__)
212 nishi 592
					t = 0;
593
					btm = localtime(&t);
64 nishi 594
					cmtime = mktime(&tm);
595
					cmtime -= (btm->tm_hour * 60 + btm->tm_min) * 60;
32 nishi 596
#else
140 nishi 597
						cmtime = timegm(&tm);
32 nishi 598
#endif
64 nishi 599
				}
21 nishi 600
			}
601
		}
212 nishi 602
		rej = false;
21 nishi 603
		cm_log("Server", "Host is %s", vhost);
212 nishi 604
		port = s == NULL ? 80 : 443;
605
		host = cm_strdup(vhost);
22 nishi 606
		for(i = 0; vhost[i] != 0; i++) {
607
			if(vhost[i] == ':') {
21 nishi 608
				host[i] = 0;
609
				port = atoi(host + i + 1);
610
				break;
611
			}
612
		}
136 nishi 613
		name = host;
21 nishi 614
		cm_log("Server", "Hostname is `%s', port is `%d'", host, port);
212 nishi 615
		vhost_entry = tw_vhost_match(host, port);
161 nishi 616
#ifdef HAS_CHROOT
293 nishi 617
		chrootpath = vhost_entry->chroot_path != NULL ? vhost_entry->chroot_path : config.root.chroot_path;
161 nishi 618
		if(chrootpath != NULL) {
619
			if(chdir(chrootpath) == 0) {
620
				if(chroot(".") == 0) {
621
					cm_log("Server", "Chroot successful");
622
				}
623
			} else {
624
				cm_log("Server", "chdir() failed, cannot chroot");
625
				tw_http_error(s, sock, 500, name, port, vhost_entry);
626
				rej = true;
627
			}
628
		}
629
#endif
20 nishi 630
		for(i = 0; i < config.module_count; i++) {
631
			tw_mod_request_t mod_req = (tw_mod_request_t)tw_module_symbol(config.modules[i], "mod_request");
632
			if(mod_req != NULL) {
633
				int ret = mod_req(&tools, &req, &res);
634
				int co = ret & 0xff;
141 nishi 635
				if(co == _TW_MODULE_PASS) {
636
					continue;
637
				} else if(co == _TW_MODULE_STOP) {
638
					/* Handle response here ... */
20 nishi 639
					res._processed = true;
640
					break;
141 nishi 641
				} else if(co == _TW_MODULE_STOP2) {
642
					res._processed = true;
643
					break;
644
				} else if(co == _TW_MODULE_ERROR) {
123 nishi 645
					tw_http_error(s, sock, (ret & 0xffff00) >> 8, name, port, vhost_entry);
20 nishi 646
					break;
647
				}
648
			}
649
		}
650
		if(!res._processed) {
212 nishi 651
			char* path;
652
			char* rpath;
653
			struct stat st;
215 nishi 654
			char* slash;
21 nishi 655
			cm_log("Server", "Document root is %s", vhost_entry->root == NULL ? "not set" : vhost_entry->root);
212 nishi 656
			path = cm_strcat(vhost_entry->root == NULL ? "" : vhost_entry->root, req.path);
21 nishi 657
			cm_log("Server", "Filesystem path is %s", path);
219 nishi 658
#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
216 nishi 659
			for(i = strlen(path) - 1; i >= 0; i--) {
660
				if(path[i] == '/') {
215 nishi 661
					path[i] = 0;
216 nishi 662
				} else {
215 nishi 663
					break;
664
				}
665
			}
666
#endif
219 nishi 667
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
212 nishi 668
			rpath = cm_strdup(path);
72 nishi 669
			for(i = strlen(rpath) - 1; i >= 0; i--) {
73 nishi 670
				if(rpath[i] == '/') {
671
					int j;
672
					for(j = i + 1; rpath[j] != 0; j++) {
673
						if(rpath[j] == ':' || rpath[j] == '.') {
674
							rpath[j] = 0;
675
							break;
676
						}
677
					}
71 nishi 678
					break;
679
				}
680
			}
70 nishi 681
			for(i = 0; i < sizeof(reserved_names) / sizeof(reserved_names[0]); i++) {
682
				char* n = cm_strcat("/", reserved_names[i]);
71 nishi 683
				if(cm_nocase_endswith(rpath, n)) {
124 nishi 684
					tw_http_error(s, sock, 403, name, port, vhost_entry);
70 nishi 685
					free(n);
686
					rej = true;
687
					cm_log("Server", "XP Patch ; rejecting access to device");
688
					break;
689
				}
690
				free(n);
691
			}
71 nishi 692
			free(rpath);
70 nishi 693
#endif
694
			if(!rej && stat(path, &st) == 0) {
22 nishi 695
				if(!tw_permission_allowed(path, addr, req, vhost_entry)) {
123 nishi 696
					tw_http_error(s, sock, 403, name, port, vhost_entry);
22 nishi 697
				} else if(S_ISDIR(st.st_mode)) {
24 nishi 698
					if(req.path[strlen(req.path) - 1] != '/') {
215 nishi 699
						char* headers[3] = {"Location", NULL, NULL};
700
						headers[1] = cm_strcat(req.path, "/");
61 nishi 701
						cm_log("Server", "Accessing directory without the slash at the end");
166 nishi 702
						_tw_process_page(s, sock, tw_http_status(301), NULL, NULL, NULL, 0, headers, 0, 0);
24 nishi 703
						free(headers[1]);
704
					} else {
705
						char** indexes = vhost_entry->index_count == 0 ? config.root.indexes : vhost_entry->indexes;
706
						int index_count = vhost_entry->index_count == 0 ? config.root.index_count : vhost_entry->index_count;
707
						bool found = false;
708
						for(i = 0; i < index_count; i++) {
709
							char* p = cm_strcat3(path, "/", indexes[i]);
710
							FILE* f = fopen(p, "rb");
711
							if(f != NULL) {
712
								char* ext = NULL;
713
								int j;
212 nishi 714
								struct stat st;
715
								char* mime;
24 nishi 716
								for(j = strlen(p) - 1; j >= 0; j--) {
717
									if(p[j] == '.') {
718
										ext = cm_strdup(p + j);
719
										break;
720
									} else if(p[j] == '/') {
721
										break;
722
									}
22 nishi 723
								}
24 nishi 724
								stat(p, &st);
212 nishi 725
								mime = tw_get_mime(ext, vhost_entry);
32 nishi 726
								tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, 0, 0);
24 nishi 727
								fclose(f);
74 nishi 728
								if(ext != NULL) free(ext);
24 nishi 729
								free(p);
730
								found = true;
731
								break;
22 nishi 732
							}
24 nishi 733
							free(p);
734
						}
735
						if(!found) {
736
							char* str = malloc(1);
212 nishi 737
							char** items;
738
							int readme;
739
							char** readmes;
740
							int readme_count;
741
							int hp;
24 nishi 742
							str[0] = 0;
212 nishi 743
							items = cm_scandir(path);
122 nishi 744
							addstring(&str, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n");
24 nishi 745
							addstring(&str, "<html>\n");
746
							addstring(&str, "	<head>\n");
747
							addstring(&str, "		<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
122 nishi 748
							addstring(&str, "		<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n");
24 nishi 749
							addstring(&str, "		<title>Index of %h</title>\n", req.path);
750
							addstring(&str, "	</head>\n");
751
							addstring(&str, "	<body>\n");
752
							addstring(&str, "		<h1>Index of %h</h1>\n", req.path);
753
							addstring(&str, "		<hr>\n");
754
							addstring(&str, "		<table border=\"0\">\n");
755
							addstring(&str, "			<tr>\n");
756
							addstring(&str, "				<th></th>\n");
757
							addstring(&str, "				<th>Filename</th>\n");
28 nishi 758
							addstring(&str, "				<th>MIME</th>\n");
759
							addstring(&str, "				<th>Size</th>\n");
24 nishi 760
							addstring(&str, "			</tr>\n");
212 nishi 761
							readme = -1;
762
							readmes = vhost_entry->readme_count == 0 ? config.root.readmes : vhost_entry->readmes;
763
							readme_count = vhost_entry->readme_count == 0 ? config.root.readme_count : vhost_entry->readme_count;
24 nishi 764
							if(items != NULL) {
29 nishi 765
								int phase = 0;
766
							doit:
24 nishi 767
								for(i = 0; items[i] != NULL; i++) {
33 nishi 768
									int j;
212 nishi 769
									char* ext;
770
									char* itm;
771
									char* icon;
28 nishi 772
									char* fpth = cm_strcat3(path, "/", items[i]);
773
									struct stat s;
774
									char size[512];
212 nishi 775
									char* showmime;
776
									char* mime;
28 nishi 777
									size[0] = 0;
778
									stat(fpth, &s);
29 nishi 779
									if(phase == 0 && !S_ISDIR(s.st_mode)) {
780
										free(fpth);
781
										continue;
782
									} else if(phase == 1 && S_ISDIR(s.st_mode)) {
783
										free(fpth);
784
										continue;
785
									}
33 nishi 786
									if(readme == -1) {
787
										for(j = 0; j < readme_count; j++) {
788
											if(strcmp(items[i], readmes[j]) == 0) {
789
												readme = j;
790
												break;
791
											}
792
										}
793
										if(readme != -1) {
794
											free(fpth);
795
											continue;
796
										}
797
									}
212 nishi 798
									if(s.st_size < NUM1024) {
31 nishi 799
										sprintf(size, "%d", (int)s.st_size);
212 nishi 800
									} else if(s.st_size < NUM1024 * 1024) {
29 nishi 801
										sprintf(size, "%.1fK", (double)s.st_size / 1024);
212 nishi 802
									} else if(s.st_size < NUM1024 * 1024 * 1024) {
29 nishi 803
										sprintf(size, "%.1fM", (double)s.st_size / 1024 / 1024);
212 nishi 804
									} else if(s.st_size < NUM1024 * 1024 * 1024 * 1024) {
29 nishi 805
										sprintf(size, "%.1fG", (double)s.st_size / 1024 / 1024 / 1024);
212 nishi 806
									} else if(s.st_size < NUM1024 * 1024 * 1024 * 1024 * 1024) {
29 nishi 807
										sprintf(size, "%.1fT", (double)s.st_size / 1024 / 1024 / 1024 / 1024);
28 nishi 808
									}
809
 
810
									free(fpth);
811
 
212 nishi 812
									ext = NULL;
24 nishi 813
									for(j = strlen(items[i]) - 1; j >= 0; j--) {
814
										if(items[i][j] == '.') {
815
											ext = cm_strdup(items[i] + j);
816
											break;
817
										} else if(items[i][j] == '/') {
818
											break;
819
										}
820
									}
212 nishi 821
									showmime = "";
822
									mime = tw_get_mime(ext, vhost_entry);
24 nishi 823
									if(strcmp(items[i], "../") == 0) {
824
										mime = "misc/parent";
29 nishi 825
										size[0] = 0;
24 nishi 826
									} else if(items[i][strlen(items[i]) - 1] == '/') {
827
										mime = "misc/dir";
29 nishi 828
										size[0] = 0;
829
									} else {
28 nishi 830
										showmime = mime;
24 nishi 831
									}
212 nishi 832
									icon = tw_get_icon(mime, vhost_entry);
24 nishi 833
									if(ext != NULL) free(ext);
212 nishi 834
									itm = cm_strdup(items[i]);
24 nishi 835
									if(strlen(itm) >= 32) {
836
										if(itm[strlen(itm) - 1] == '/') {
837
											itm[31] = 0;
838
											itm[30] = '/';
839
											itm[29] = '.';
840
											itm[28] = '.';
841
											itm[27] = '.';
842
										} else {
843
											itm[31] = 0;
844
											itm[30] = '.';
845
											itm[29] = '.';
846
											itm[28] = '.';
847
										}
848
									}
849
									addstring(&str, "<tr>\n");
850
									addstring(&str, "	<td><img src=\"%s\" alt=\"icon\"></td>\n", icon);
851
									addstring(&str, "	<td><a href=\"%l\"><code>%h</code></a></td>\n", items[i], itm);
60 nishi 852
									addstring(&str, "	<td><code>  %h  </code></td>\n", showmime);
853
									addstring(&str, "	<td><code>  %s  </code></td>\n", size);
24 nishi 854
									addstring(&str, "</tr>\n");
855
									free(itm);
22 nishi 856
								}
29 nishi 857
								phase++;
858
								if(phase != 2) goto doit;
859
								for(i = 0; items[i] != NULL; i++) free(items[i]);
860
								free(items);
22 nishi 861
							}
24 nishi 862
							addstring(&str, "		</table>\n");
33 nishi 863
							if(readme != -1) {
212 nishi 864
								struct stat s;
865
								FILE* fr;
866
								char* fpth;
33 nishi 867
								addstring(&str, "<hr>\n");
212 nishi 868
								fpth = cm_strcat3(path, "/", readmes[readme]);
33 nishi 869
								stat(fpth, &s);
212 nishi 870
								fr = fopen(fpth, "r");
33 nishi 871
								if(fr != NULL) {
872
									char* rmbuf = malloc(s.st_size + 1);
873
									rmbuf[s.st_size] = 0;
874
									fread(rmbuf, s.st_size, 1, fr);
875
									addstring(&str, "<pre><code>%h</code></pre>\n", rmbuf);
876
									fclose(fr);
70 nishi 877
									free(rmbuf);
33 nishi 878
								}
66 nishi 879
								free(fpth);
33 nishi 880
							}
24 nishi 881
							addstring(&str, "		<hr>\n");
212 nishi 882
							hp = vhost_entry->hideport == -1 ? config.root.hideport : vhost_entry->hideport;
123 nishi 883
							if(hp == 0) {
884
								addstring(&str, "		<address>%s Server at %s Port %d</address>\n", tw_server, name, port);
885
							} else {
886
								addstring(&str, "		<address>%s Server at %s</address>\n", tw_server, name, port);
887
							}
24 nishi 888
							addstring(&str, "	</body>\n");
889
							addstring(&str, "</html>\n");
32 nishi 890
							tw_process_page(s, sock, tw_http_status(200), "text/html", NULL, str, strlen(str), 0, 0);
24 nishi 891
							free(str);
21 nishi 892
						}
893
					}
22 nishi 894
				} else {
21 nishi 895
					char* ext = NULL;
212 nishi 896
					char* mime;
897
					FILE* f;
22 nishi 898
					for(i = strlen(req.path) - 1; i >= 0; i--) {
899
						if(req.path[i] == '.') {
21 nishi 900
							ext = cm_strdup(req.path + i);
901
							break;
24 nishi 902
						} else if(req.path[i] == '/') {
903
							break;
21 nishi 904
						}
905
					}
212 nishi 906
					mime = tw_get_mime(ext, vhost_entry);
21 nishi 907
					if(ext != NULL) free(ext);
212 nishi 908
					f = fopen(path, "rb");
173 nishi 909
					if(f == NULL) {
910
						tw_http_error(s, sock, 403, name, port, vhost_entry);
911
					} else {
912
						tw_process_page(s, sock, tw_http_status(200), mime, f, NULL, st.st_size, st.st_mtime, cmtime);
913
						fclose(f);
914
					}
21 nishi 915
				}
22 nishi 916
			} else {
161 nishi 917
				if(!rej) {
918
					tw_http_error(s, sock, 404, name, port, vhost_entry);
919
				}
21 nishi 920
			}
921
			free(path);
20 nishi 922
		}
21 nishi 923
		free(vhost);
924
		free(host);
22 nishi 925
	} else if(ret == -1) {
18 nishi 926
	} else {
123 nishi 927
		tw_http_error(s, sock, 400, name, port, &config.root);
17 nishi 928
	}
70 nishi 929
	tw_free_request(&req);
12 nishi 930
cleanup:
43 nishi 931
#ifndef NO_SSL
16 nishi 932
	if(sslworks) {
15 nishi 933
		SSL_shutdown(s);
934
	}
935
	SSL_free(s);
46 nishi 936
#endif
11 nishi 937
	close_socket(sock);
219 nishi 938
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
216 nishi 939
	_endthread();
100 nishi 940
#elif defined(__HAIKU__)
105 nishi 941
		exit_thread(0);
11 nishi 942
#endif
216 nishi 943
#ifndef NO_RETURN_THREAD
214 nishi 944
	return 0;
215 nishi 945
#endif
11 nishi 946
}
947
 
62 nishi 948
#ifdef SERVICE
949
extern SERVICE_STATUS status;
950
extern SERVICE_STATUS_HANDLE status_handle;
951
#endif
952
 
219 nishi 953
#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
65 nishi 954
struct thread_entry {
101 nishi 955
#ifdef __HAIKU__
956
	thread_id thread;
957
#else
65 nishi 958
	HANDLE handle;
101 nishi 959
#endif
65 nishi 960
	bool used;
961
};
962
#endif
963
 
183 nishi 964
extern int running;
965
 
11 nishi 966
void tw_server_loop(void) {
68 nishi 967
	int i;
212 nishi 968
#ifndef USE_POLL
213 nishi 969
	fd_set fdset;
970
	struct timeval tv;
212 nishi 971
#endif
219 nishi 972
#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
65 nishi 973
	struct thread_entry threads[2048];
70 nishi 974
	for(i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) {
65 nishi 975
		threads[i].used = false;
976
	}
977
#endif
118 nishi 978
#ifdef USE_POLL
302 nishi 979
	struct pollfd* pollfds = malloc(sizeof(*pollfds) * sockcount);
118 nishi 980
	for(i = 0; i < sockcount; i++) {
981
		pollfds[i].fd = sockets[i];
982
		pollfds[i].events = POLLIN | POLLPRI;
983
	}
984
#endif
183 nishi 985
	while(running) {
212 nishi 986
		int ret;
118 nishi 987
#ifdef USE_POLL
212 nishi 988
		ret = poll(pollfds, sockcount, 1000);
118 nishi 989
#else
990
			FD_ZERO(&fdset);
991
			for(i = 0; i < sockcount; i++) {
992
				FD_SET(sockets[i], &fdset);
993
			}
994
			tv.tv_sec = 1;
995
			tv.tv_usec = 0;
86 nishi 996
#ifdef __HAIKU__
212 nishi 997
			ret = select(32, &fdset, NULL, NULL, &tv);
86 nishi 998
#else
212 nishi 999
			ret = select(FD_SETSIZE, &fdset, NULL, NULL, &tv);
86 nishi 1000
#endif
118 nishi 1001
#endif
11 nishi 1002
		if(ret == -1) {
219 nishi 1003
#if !defined(__MINGW32__) && !defined(_MSC_VER) && !defined(__BORLANDC__) && !defined(__WATCOMC__)
296 nishi 1004
			if(errno == EINTR) continue;
84 nishi 1005
			cm_log("Server", "Select failure: %s", strerror(errno));
1006
#endif
9 nishi 1007
			break;
70 nishi 1008
		} else if(ret == 0) {
62 nishi 1009
#ifdef SERVICE
70 nishi 1010
			if(status.dwCurrentState == SERVICE_STOP_PENDING) {
62 nishi 1011
				break;
1012
			}
1013
#endif
11 nishi 1014
		} else if(ret > 0) {
9 nishi 1015
			/* connection */
1016
			int i;
11 nishi 1017
			for(i = 0; i < sockcount; i++) {
118 nishi 1018
				bool cond;
1019
#ifdef USE_POLL
1020
				cond = pollfds[i].revents & POLLIN;
1021
#else
1022
					cond = FD_ISSET(sockets[i], &fdset);
1023
#endif
1024
				if(cond) {
9 nishi 1025
					SOCKADDR claddr;
182 nishi 1026
					socklen_t clen = sizeof(claddr);
9 nishi 1027
					int sock = accept(sockets[i], (struct sockaddr*)&claddr, &clen);
219 nishi 1028
#if defined(__MINGW32__) || defined(__HAIKU__) || defined(_PSP) || defined(__PPU__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
212 nishi 1029
					int j;
1030
					struct pass_entry* e = malloc(sizeof(*e));
12 nishi 1031
					cm_log("Server", "New connection accepted");
11 nishi 1032
					e->sock = sock;
215 nishi 1033
#if defined(_MSC_VER) || defined(__BORLANDC__)
212 nishi 1034
					e->ssl = config.ports[i] & (1UL << 31);
1035
#else
1036
					e->ssl = config.ports[i] & (1ULL << 31);
1037
#endif
12 nishi 1038
					e->port = config.ports[i];
21 nishi 1039
					e->addr = claddr;
97 nishi 1040
#endif
219 nishi 1041
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__BORLANDC__) || defined(__WATCOMC__)
312 nishi 1042
#ifdef __OS2__
1043
					_beginthread(tw_server_pass, 0, 0, e);
1044
#else
212 nishi 1045
					_beginthread(tw_server_pass, 0, e);
312 nishi 1046
#endif
187 nishi 1047
#elif defined(_PSP) || defined(__PPU__)
183 nishi 1048
						tw_server_pass(e);
97 nishi 1049
#elif defined(__HAIKU__)
183 nishi 1050
					for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
1051
						if(threads[j].used) {
1052
							thread_info info;
1053
							bool kill = false;
1054
							if(get_thread_info(threads[j].thread, &info) == B_OK) {
1055
							} else {
1056
								kill = true;
101 nishi 1057
							}
183 nishi 1058
							if(kill) {
1059
								threads[j].used = false;
101 nishi 1060
							}
1061
						}
183 nishi 1062
					}
1063
					for(j = 0; j < sizeof(threads) / sizeof(threads[0]); j++) {
1064
						if(!threads[j].used) {
1065
							threads[j].thread = spawn_thread(tw_server_pass, "Tewi HTTPd", 60, e);
1066
							threads[j].used = true;
1067
							resume_thread(threads[j].thread);
1068
							break;
1069
						}
1070
					}
11 nishi 1071
#else
1072
					pid_t pid = fork();
1073
					if(pid == 0) {
89 nishi 1074
						int j;
1075
						for(j = 0; j < sockcount; j++) close_socket(sockets[j]);
212 nishi 1076
						tw_server_pass(sock, config.ports[i] & (1ULL << 31), config.ports[i], claddr);
11 nishi 1077
						_exit(0);
1078
					} else {
1079
						close_socket(sock);
1080
					}
1081
#endif
9 nishi 1082
				}
1083
			}
1084
		}
1085
	}
255 nishi 1086
	for(i = 0; i < sockcount; i++) {
253 nishi 1087
		close_socket(sockets[i]);
1088
	}
1089
	cm_force_log("Server is down");
9 nishi 1090
}