-
Notifications
You must be signed in to change notification settings - Fork 9
/
webserv.c
689 lines (575 loc) · 19.2 KB
/
webserv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
* =====================================================================================
*
* Filename: webserv.c
*
* Description: Web server for managing the web interface of the module
*
* Version: 0.1
* Created: 05/10/2010 14:12:24
* Revision: none
* Compiler: gcc
*
* Author: BlackLight (http://0x00.ath.cx), <[email protected]>
* Licence: GNU GPL v.3
* Company: DO WHAT YOU WANT CAUSE A PIRATE IS FREE, YOU ARE A PIRATE!
*
* =====================================================================================
*/
#include "spp_ai.h"
#include <alloca.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
/** \defgroup webserv Web server managing the web interface of the module
* @{ */
#define HTTP_RESPONSE_HEADERS_FORMAT "%s %d %s\r\n" \
"Date: %s\r\n" \
"Server: %s\r\n" \
"Content-Type: %s\r\n" \
"Content-Length: %lu\r\n\r\n"
#define HTTP_CGI_RESPONSE_HEADERS_FORMAT "%s %d %s\r\n" \
"Date: %s\r\n" \
"Server: %s\r\n" \
"Content-Length: %u\r\n"
#define HTTP_ERR_RESPONSE_FORMAT "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n" \
"<html><head>\n" \
"<title>%d %s</title>\n" \
"</head><body>\n" \
"<h1>%s</h1>\n" \
"<p>%s</p>\n" \
"<hr>\n" \
"<i>%s</i>\n" \
"</body></html>\n\n"
typedef struct {
int sd;
struct sockaddr_in client;
} conn_info;
/**
* \brief Escape a string to be used or included in an URL
* \param str String to be escaped
* \param out Pointer to the string containing the output (its length must be at least 3*length(str) + 2)
* \param str_len Length of the input string
* \return The length of the output string after the operation
*/
size_t
__AI_url_escape ( char *str, char **out, size_t str_len )
{
char escape_seq[5] = { 0 };
size_t i,
out_len = 0;
for ( i=0; i < str_len; i++ )
{
if (
( str[i] >= 'a' && str[i] <= 'z' ) ||
( str[i] >= 'A' && str[i] <= 'Z' ) ||
( str[i] >= '0' && str[i] <= '9' ) ||
str[i] == '_' || str[i] == '.' || str[i] == '/' ||
str[i] == '?' || str[i] == '=' )
{
(*out)[out_len++] = str[i];
} else {
snprintf ( escape_seq, sizeof ( escape_seq ), "%%%.2X", str[i] );
(*out)[out_len++] = escape_seq[0];
(*out)[out_len++] = escape_seq[1];
(*out)[out_len++] = escape_seq[2];
}
}
(*out)[out_len] = 0;
return out_len;
} /* ----- end of function __AI_url_escape ----- */
/**
* \brief Unescape a string in URL format
* \param str String to be unescaped
* \param out Pointer to the string containing the output (its length must be at least the same of str + 1)
* \param str_len Length of the input string
* \return The length of the output string after the operation
*/
PRIVATE size_t
__AI_url_unescape ( char *str, char **out, size_t str_len )
{
char escape_seq[5] = { 0 };
size_t i,
out_len = 0;
for ( i=0; i < str_len; i++ )
{
if ( str[i] == '%' && i < str_len-2 )
{
if (
(( str[i+1] >= '0' && str[i+1] <= '9' ) ||
( str[i+1] >= 'a' && str[i+1] <= 'z' ) ||
( str[i+1] >= 'A' && str[i+1] <= 'Z' )) &&
(( str[i+2] >= '0' && str[i+2] <= '9' ) ||
( str[i+2] >= 'a' && str[i+2] <= 'z' ) ||
( str[i+2] >= 'A' && str[i+2] <= 'Z' )))
{
escape_seq[0] = str[i+1];
escape_seq[1] = str[i+2];
escape_seq[2] = 0;
(*out)[out_len++] = (char) strtoul ( escape_seq, NULL, 16 );
i += 2;
continue;
}
}
(*out)[out_len++] = str[i];
}
(*out)[out_len] = 0;
return out_len;
} /* ----- end of function __AI_url_unescape ----- */
/**
* \brief Read a line from a file descriptor
* \param fp FILE descriptor
* \return A char* pointer to the read line (to be free-ed!!!)
*/
PRIVATE char*
__AI_getline ( FILE *fp )
{
char ch;
char *line = NULL;
int nbytes = 1;
if ( !feof ( fp ))
{
while (( ch = fgetc ( fp )) != '\n' && ch != '\0' && !feof ( fp ))
{
if ( ch == '\r' )
continue;
if ( !( line = (char*) realloc ( line, ++nbytes )))
{
AI_fatal_err ( "Fatal dynamic memory allocation", __FILE__, __LINE__ );
}
line[ nbytes - 2 ] = ch;
}
}
if ( nbytes > 1 )
{
line[ nbytes - 1 ] = 0;
}
return line;
} /* ----- end of function __AI_getline ----- */
/**
* \brief Manage a client connection
* \param arg void* reference to the client socket
*/
PRIVATE void*
__AI_webservlet_thread ( void *arg )
{
time_t ltime = time ( NULL );
struct stat st;
BOOL is_cgi = false;
FILE *sock = NULL,
*fp = NULL,
*pipe = NULL;
int i,
http_response_len = 0,
sd = ((conn_info*) arg)->sd,
nlines = 0,
nmatches = 0,
max_content_length = 0,
max_headers_length = 0,
read_bytes = 0,
req_file_absolute_path_size = 0;
char ch,
client_addr[INET_ADDRSTRLEN] = { 0 },
client_port[10] = { 0 },
*line = NULL,
*unescaped = NULL,
*cgi_cmd = NULL,
*query_string = NULL,
*http_response = NULL,
*http_headers = NULL,
*strtime = NULL,
**matches = NULL,
*req_file_absolute_path = NULL,
content_type[108] = { 0 },
extension[100] = { 0 },
http_ver[10] = { 0 },
req_file[1024] = { 0 };
max_content_length = strlen ( HTTP_ERR_RESPONSE_FORMAT ) + strlen ( config->webserv_banner ) + 1000;
max_headers_length = strlen ( HTTP_RESPONSE_HEADERS_FORMAT ) + strlen ( config->webserv_banner ) + 1000;
/* Setting environment variables */
inet_ntop ( AF_INET, &(((conn_info*) arg)->client.sin_addr.s_addr), client_addr, INET_ADDRSTRLEN );
snprintf ( client_port, sizeof ( client_port ), "%d", htons (((conn_info*) arg)->client.sin_port) );
setenv ( "CLIENT_PROTOCOL", "HTTP", 1 );
setenv ( "DOCUMENT_ROOT", config->webserv_dir, 1 );
setenv ( "GATEWAY_INTERFACE", "CGI/1.1", 1 );
setenv ( "REMOTE_ADDR", client_addr, 1 );
setenv ( "REMOTE_PORT", client_port, 1 );
if ( !( http_response = (char*) alloca ( max_content_length )))
{
pthread_exit ((void*) 0);
return (void*) 0;
}
if ( !( http_headers = (char*) alloca ( max_headers_length )))
{
pthread_exit ((void*) 0);
return (void*) 0;
}
if ( !( sock = fdopen ( sd, "r+" )))
{
pthread_exit ((void*) 0);
return (void*) 0;
}
req_file_absolute_path_size = sizeof ( req_file ) + strlen ( config->webserv_dir ) + 1;
if ( !( req_file_absolute_path = (char*) alloca ( req_file_absolute_path_size )))
{
pthread_exit ((void*) 0);
return (void*) 0;
}
for ( nlines=0; ( line = __AI_getline ( sock )); nlines++ )
{
if ( preg_match ( "^\\s*(GET|POST|HEAD)\\s+(/[^ \\?#]*)(\\?|#)?([^ ]+)?\\s*(HTTP/[0-9]\\.[0-9])?", line, &matches, &nmatches ) > 0 )
{
setenv ( "REQUEST_METHOD", matches[0], 1 );
if ( !strcmp ( matches[2], "?" ))
{
if ( strlen ( matches[3] ) > 0 )
{
query_string = strdup ( matches[3] );
setenv ( "QUERY_STRING", query_string, 1 );
}
}
if ( strlen ( matches[4] ) > 0 )
{
strncpy ( http_ver, matches[4], sizeof ( http_ver ));
} else {
strncpy ( http_ver, "HTTP/1.0", sizeof ( http_ver ));
}
setenv ( "SERVER_PROTOCOL", http_ver, 1 );
if ( !strcmp ( matches[1], "/" ))
{
free ( matches[1] );
matches[1] = strdup ( "/index.html" );
}
setenv ( "DOCUMENT_URI", matches[1], 1 );
setenv ( "DOCUMENT_URL", matches[1], 1 );
setenv ( "REQUEST_URI", matches[1], 1 );
setenv ( "URI", matches[1], 1 );
setenv ( "URL", matches[1], 1 );
snprintf ( req_file_absolute_path, req_file_absolute_path_size, "%s%s", config->webserv_dir, matches[1] );
if ( strcmp ( http_ver, "HTTP/1.0" ) && strcmp ( http_ver, "HTTP/1.1" ))
{
snprintf ( http_response, max_content_length, HTTP_ERR_RESPONSE_FORMAT,
400, "Bad Request", "Bad Request",
"The request could not be understood by the server due to malformed syntax",
config->webserv_banner );
ltime = time ( NULL );
strtime = strdup ( ctime ( <ime ));
strtime [ strlen(strtime) - 1 ] = 0;
snprintf ( http_headers, max_headers_length, HTTP_RESPONSE_HEADERS_FORMAT,
"HTTP/1.1", 400, "Bad Request", strtime,
config->webserv_banner, "text/html", (unsigned long int) strlen ( http_response ));
free ( strtime );
free ( line );
line = NULL;
continue;
}
for ( i=0; i < nmatches; i++ )
{
free ( matches[i] );
}
free ( matches );
matches = NULL;
if ( !( unescaped = (char*) alloca ( strlen ( req_file_absolute_path ) + 2 )))
{
pthread_exit ((void*) 0);
return (void*) 0;
}
/* Avoid directory traversal */
__AI_url_unescape ( req_file_absolute_path, &unescaped, strlen ( req_file_absolute_path ));
unescaped = str_replace_all ( unescaped, "../", "" );
strncpy ( req_file_absolute_path, unescaped, req_file_absolute_path_size );
free ( unescaped );
if ( stat ( req_file_absolute_path, &st ) < 0 )
{
snprintf ( http_response, max_content_length, HTTP_ERR_RESPONSE_FORMAT,
404, "Not Found", "Not Found",
"The requested resource was not found on the server",
config->webserv_banner );
ltime = time ( NULL );
strtime = strdup ( ctime ( <ime ));
strtime [ strlen(strtime) - 1 ] = 0;
snprintf ( http_headers, max_headers_length, HTTP_RESPONSE_HEADERS_FORMAT,
http_ver, 404, "Not Found", strtime,
config->webserv_banner, "text/html", (unsigned long int) strlen ( http_response ));
free ( strtime );
free ( line );
line = NULL;
continue;
}
if ( !( fp = fopen ( req_file_absolute_path, "r" ))) {
snprintf ( http_response, max_content_length, HTTP_ERR_RESPONSE_FORMAT,
403, "Forbidden", "Forbidden",
"The client does not have enough permissions for accessing the requested resource on the server",
config->webserv_banner );
ltime = time ( NULL );
strtime = strdup ( ctime ( <ime ));
strtime [ strlen(strtime) - 1 ] = 0;
snprintf ( http_headers, max_headers_length, HTTP_RESPONSE_HEADERS_FORMAT,
http_ver, 403, "Forbidden", strtime,
config->webserv_banner, "text/html", (unsigned long int) strlen ( http_response ));
free ( strtime );
free ( line );
line = NULL;
continue;
}
if ( preg_match ( "\\.([a-zA-Z0-9]+)$", req_file_absolute_path, &matches, &nmatches ) > 0 )
{
if ( strlen ( matches[0] ) < sizeof ( extension ))
{
strncpy ( extension, matches[0], sizeof ( extension ));
}
}
if ( !strcasecmp ( extension, "html" ))
{
strncpy ( content_type, "text/html", sizeof ( content_type ));
} else if ( !strcasecmp ( extension, "css" )) {
strncpy ( content_type, "text/css", sizeof ( content_type ));
} else if ( !strcasecmp ( extension, "js" )) {
strncpy ( content_type, "application/x-javascript", sizeof ( content_type ));
} else if ( !strcasecmp ( extension, "json" )) {
strncpy ( content_type, "application/json", sizeof ( content_type ));
} else if ( !strcasecmp ( extension, "xml" )) {
strncpy ( content_type, "application/xml", sizeof ( content_type ));
} else if ( !strcasecmp ( extension, "jpg" ) || !strcasecmp ( extension, "jpeg" )) {
strncpy ( content_type, "image/jpeg", sizeof ( content_type ));
} else if ( !strcasecmp ( extension, "cgi" )) {
/* If it's not executable, it's not a CGI */
if ( !( st.st_mode & S_IXOTH ))
strncpy ( content_type, "text/plain", sizeof ( content_type ));
else {
is_cgi = true;
http_response = NULL;
http_response_len = 1;
if ( !( cgi_cmd = (char*) alloca ( strlen ( req_file_absolute_path ) + 20 )))
{
pthread_exit ((void*) 0);
return (void*) 0;
}
sprintf ( cgi_cmd, "/bin/sh -c %s", req_file_absolute_path );
if ( !( pipe = popen ( cgi_cmd, "r" )))
{
pthread_exit ((void*) 0);
return (void*) 0;
}
while ( fread ( &ch, 1, 1, pipe ) > 0 )
{
if ( !( http_response = (char*) realloc ( http_response, ++http_response_len )))
{
pthread_exit ((void*) 0);
return (void*) 0;
}
http_response [ http_response_len - 2 ] = ch;
read_bytes++;
}
if ( http_response )
{
http_response [ http_response_len - 1 ] = 0;
}
pclose ( pipe );
if ( !http_response )
{
if ( !( http_response = (char*) malloc ( 2 )))
{
pthread_exit ((void*) 0);
return (void*) 0;
}
http_response[0] = 0;
}
}
} else if ( !strcasecmp ( extension, "gif" ) || !strcasecmp ( extension, "png" ) ||
!strcasecmp ( extension, "bmp" ) || !strcasecmp ( extension, "tif" ) ||
!strcasecmp ( extension, "ppm" )) {
snprintf ( content_type, sizeof ( content_type ),
"image/%s", extension );
} else if ( strlen ( extension ) == 0 || !strcasecmp ( extension, "txt" )) {
strncpy ( content_type, "text/plain", sizeof ( content_type ));
} else {
snprintf ( content_type, sizeof ( content_type ), "application/%s", extension );
}
if ( !is_cgi )
{
if ( !( http_response = (char*) alloca ( st.st_size + 2 )))
{
pthread_exit ((void*) 0);
return (void*) 0;
}
memset ( http_response, 0, st.st_size + 2 );
fread ( http_response, st.st_size, 1, fp );
fclose ( fp );
}
ltime = time ( NULL );
strtime = strdup ( ctime ( <ime ));
strtime [ strlen(strtime) - 1 ] = 0;
if ( is_cgi )
{
snprintf ( http_headers, max_headers_length, HTTP_CGI_RESPONSE_HEADERS_FORMAT,
http_ver, 200, "Found", strtime, config->webserv_banner,
/* strlen ( http_response )); */
read_bytes );
} else {
read_bytes = (unsigned int) st.st_size;
snprintf ( http_headers, max_headers_length, HTTP_RESPONSE_HEADERS_FORMAT,
http_ver, 200, "Found", strtime, config->webserv_banner,
content_type,
/* strlen ( http_response )); */
(unsigned long int) read_bytes );
}
free ( strtime );
free ( line );
line = NULL;
continue;
} else {
if ( nlines == 0 )
{
snprintf ( http_response, max_content_length, HTTP_ERR_RESPONSE_FORMAT,
405, "Method Not Allowed", "Method Not Allowed",
"The requested HTTP method is not allowed",
config->webserv_banner );
ltime = time ( NULL );
strtime = strdup ( ctime ( <ime ));
strtime [ strlen(strtime) - 1 ] = 0;
snprintf ( http_headers, max_headers_length, HTTP_RESPONSE_HEADERS_FORMAT,
"HTTP/1.1", 405, "Method Not Allowed", strtime, "text/html",
config->webserv_banner, (unsigned long int) strlen ( http_response ));
free ( strtime );
free ( line );
line = NULL;
continue;
} else if ( preg_match ( "\\s*Content-Length\\s*:\\s*([0-9]+)", line, &matches, &nmatches ) > 0 ) {
setenv ( "CONTENT_LENGTH", matches[0], 1 );
for ( i=0; i < nmatches; i++ )
free ( matches[i] );
free ( matches );
matches = NULL;
} else if ( preg_match ( "\\s*Content-Type\\s*:\\s*(.+?)\r?\n?$", line, &matches, &nmatches ) > 0 ) {
setenv ( "CONTENT_TYPE", matches[0], 1 );
for ( i=0; i < nmatches; i++ )
free ( matches[i] );
free ( matches );
matches = NULL;
} else if ( preg_match ( "\\s*Accept\\s*:\\s*(.+?)\r?\n?$", line, &matches, &nmatches ) > 0 ) {
setenv ( "HTTP_ACCEPT", matches[0], 1 );
for ( i=0; i < nmatches; i++ )
free ( matches[i] );
free ( matches );
matches = NULL;
} else if ( preg_match ( "\\s*Accept-Charset\\s*:\\s*(.+?)\r?\n?$", line, &matches, &nmatches ) > 0 ) {
setenv ( "HTTP_ACCEPT_CHARSET", matches[0], 1 );
for ( i=0; i < nmatches; i++ )
free ( matches[i] );
free ( matches );
matches = NULL;
} else if ( preg_match ( "\\s*Accept-Encoding\\s*:\\s*(.+?)\r?\n?$", line, &matches, &nmatches ) > 0 ) {
setenv ( "HTTP_ACCEPT_ENCODING", matches[0], 1 );
for ( i=0; i < nmatches; i++ )
free ( matches[i] );
free ( matches );
matches = NULL;
} else if ( preg_match ( "\\s*Accept-Language\\s*:\\s*(.+?)\r?\n?$", line, &matches, &nmatches ) > 0 ) {
setenv ( "HTTP_ACCEPT_LANGUAGE", matches[0], 1 );
for ( i=0; i < nmatches; i++ )
free ( matches[i] );
free ( matches );
matches = NULL;
} else if ( preg_match ( "\\s*Connection\\s*:\\s*(.+?)\r?\n?$", line, &matches, &nmatches ) > 0 ) {
setenv ( "HTTP_CONNECTION", matches[0], 1 );
for ( i=0; i < nmatches; i++ )
free ( matches[i] );
free ( matches );
matches = NULL;
} else if ( preg_match ( "\\s*Cookie\\s*:\\s*(.+?)\r?\n?$", line, &matches, &nmatches ) > 0 ) {
setenv ( "HTTP_COOKIE", matches[0], 1 );
for ( i=0; i < nmatches; i++ )
free ( matches[i] );
free ( matches );
matches = NULL;
} else if ( preg_match ( "\\s*Reason\\s*:\\s*(.+?)\r?\n?$", line, &matches, &nmatches ) > 0 ) {
setenv ( "HTTP_REASON", matches[0], 1 );
for ( i=0; i < nmatches; i++ )
free ( matches[i] );
free ( matches );
matches = NULL;
} else if ( preg_match ( "\\s*User-Agent\\s*:\\s*(.+?)\r?\n?$", line, &matches, &nmatches ) > 0 ) {
setenv ( "HTTP_USER_AGENT", matches[0], 1 );
for ( i=0; i < nmatches; i++ )
free ( matches[i] );
free ( matches );
matches = NULL;
} else if ( preg_match ( "\\s*Referrer\\s*:\\s*(.+?)\r?\n?$", line, &matches, &nmatches ) > 0 ) {
setenv ( "HTTP_REFERRER", matches[0], 1 );
for ( i=0; i < nmatches; i++ )
free ( matches[i] );
free ( matches );
matches = NULL;
}
}
free ( line );
line = NULL;
}
/* fprintf ( sock, "%s%s", http_headers, http_response ); */
fprintf ( sock, "%s", http_headers );
fwrite ( http_response, read_bytes, 1, sock );
fclose ( sock );
close ( sd );
free ( arg );
if ( query_string )
free ( query_string );
if ( is_cgi )
free ( http_response );
pthread_exit ( 0 );
return (void*) 0;
} /* ----- end of function __AI_webservlet_thread ----- */
/**
* \brief Thread running the code of the web server for the web interface of the module
*/
void*
AI_webserv_thread ( void *arg )
{
int on = 1,
sd,
sockaddr_size = sizeof ( struct sockaddr );
struct sockaddr_in addr;
pthread_t servlet_thread;
pthread_attr_t attr;
conn_info *conn;
if (( sd = socket ( AF_INET, SOCK_STREAM, 0 )) < 0 )
{
AI_fatal_err ( "Error while creating webserver socket", __FILE__, __LINE__ );
}
if ( setsockopt ( sd, SOL_SOCKET, SO_REUSEADDR, (void*) &on, sizeof ( on )) < 0 )
{
AI_fatal_err ( "Error while setting SO_REUSEADDR on the socket", __FILE__, __LINE__ );
}
memset ( &addr, 0, sizeof ( struct sockaddr_in ));
addr.sin_family = AF_INET;
addr.sin_port = htons ( config->webserv_port );
addr.sin_addr.s_addr = INADDR_ANY;
if ( bind ( sd, (struct sockaddr*) &addr, sizeof ( addr )) < 0 )
{
AI_fatal_err ( "Error while binding socket", __FILE__, __LINE__ );
}
if ( listen ( sd, 100 ) < 0 )
{
AI_fatal_err ( "Error while setting the socket in listen mode", __FILE__, __LINE__ );
}
pthread_attr_init ( &attr );
pthread_attr_setdetachstate ( &attr, PTHREAD_CREATE_DETACHED );
while ( 1 )
{
if ( !( conn = (conn_info*) malloc ( sizeof ( conn_info ))))
continue;
memset ( conn, 0, sizeof ( conn_info ));
if (( conn->sd = accept ( sd, (struct sockaddr*) &(conn->client), (socklen_t*) &sockaddr_size )) < 0 )
continue;
if ( pthread_create ( &servlet_thread, &attr, __AI_webservlet_thread, (void*) conn ) != 0 )
{
AI_fatal_err ( "Error while creating the webservlet thread", __FILE__, __LINE__ );
}
}
close ( sd );
pthread_exit ((void*) 0);
return (void*) 0;
} /* ----- end of function AI_webserv_thread ----- */
/** @} */