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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
|
/**
* httpap.c
* HTTP Application Proxy for Onion Routing
*
* Matej Pfajfar <mp292@cam.ac.uk>
*/
/*
* Changes :
* $Log$
* Revision 1.8 2002/09/09 04:10:58 arma
* port to actual BSD
*
* (hey nick, does this break the os x build?)
*
* you still need to add some stuff to the ./configure commandline...
* anybody know a better solution?
*
* Revision 1.7 2002/09/03 18:44:23 nickm
* Port to MacOS X
*
* Revision 1.6 2002/08/24 07:56:22 arma
* proxies send port in host order as ascii string
*
* Revision 1.5 2002/07/20 02:01:18 arma
* bugfixes: don't hang waiting for new children to die; accept HTTP/1.1
*
* Revision 1.4 2002/07/19 18:48:19 arma
* slightly less noisy
*
* Revision 1.3 2002/07/12 18:14:16 montrose
* removed loglevel from global namespace. severity level is set using log() with a NULL format argument now. example: log(LOG_ERR,NULL);
*
* Revision 1.2 2002/07/02 09:16:16 arma
* httpap now prepends dest_addr and dest_port strings with their length.
*
* also, it now sets the listening socket option SO_REUSEADDR
*
* Revision 1.1.1.1 2002/06/26 22:45:50 arma
* initial commit: current code
*
* Revision 1.4 2002/06/14 20:45:26 mp292
* Extra debugging message.
*
* Revision 1.3 2002/04/02 14:27:33 badbytes
* Final finishes.
*
* Revision 1.2 2002/03/12 23:40:58 mp292
* Tested.
*
* Revision 1.1 2002/03/11 00:21:53 mp292
* Coding completed. Pending testing.
*
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <ctype.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdarg.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "../common/log.h"
#include "../common/config.h"
#include "../common/ss.h"
#include "../common/utils.h"
#include "../common/version.h"
#include "httpap.h"
#include "http.h"
struct timeval conn_tout;
struct timeval *conn_toutp = &conn_tout;
/* valid command-line options */
static const char *args = "hf:p:l:";
/* valid config file options */
static config_opt_t options[] =
{
{"OnionProxy", CONFIG_TYPE_INT, {0}, 0},
{"MaxConn", CONFIG_TYPE_INT, {0}, 0},
{"Anonimize", CONFIG_TYPE_INT, {0}, 0},
{"ConnTimeout", CONFIG_TYPE_INT, {0}, 0},
{0}
};
enum opts {
OnionProxy=0,MaxConn, Anonimize, ConnTimeout
};
/* number of open connections */
int connections=0;
/* prints help on using httpap */
void print_usage()
{
char *program = "httpap";
printf("\n%s - HTTP application proxy for Onion Routing.\nUsage : %s -f config [-p port -l loglevel -h]\n-h : display this help\n-f config : config file\n-p port : port number which %s should bind to\n-l loglevel : logging threshold; one of alert|crit|err|warning|notice|info|debug\n\n", program,program,program);
}
/* used for reaping zombie processes */
void sigchld_handler(int s)
{
while((waitpid (-1, NULL, WNOHANG)) > 0) {
// while (wait(NULL) > 0);
connections--;
}
}
int handle_connection(int new_sock, struct hostent *local, struct sockaddr_in remote, uint16_t op_port)
{
int retval = 0;
int i;
char islocal = 0; /* is the accepted connection local? */
char *cp; /* character pointer used for checking whether the connection is local */
unsigned char *line; /* one line of input */
int len; /* length of the line */
uint16_t stringlen; /* used for sending how long a string is before the actual string */
unsigned char *http_ver; /* HTTP version of the incoming request */
unsigned char *addr; /* destination address */
unsigned char *port; /* destination port */
unsigned char *header_name; /* name of a request header */
uint16_t portn; /* destination port converted into an integer */
char *errtest; /* error check when converting the port into an integer */
ss_t ss; /* standard structure */
unsigned char errcode; /* error code returned by the onion proxy */
int sop; /* socket for connecting to the onion proxy */
struct sockaddr_in op_addr; /* onion proxy address */
/* for use with select() */
fd_set mask,rmask;
int maxfd;
unsigned char buf[1024]; /* data buffer */
log(LOG_DEBUG, "handle_connection() : Local address = %s.", inet_ntoa(*(struct in_addr *)local->h_addr));
log(LOG_DEBUG, "handle_connection() : Remote address = %s.", inet_ntoa(remote.sin_addr));
/* first check that the connection is from the local host, otherwise it will be rejected */
if (*(uint32_t *)&remote.sin_addr == inet_addr("127.0.0.1"))
islocal = 1;
for (i=0; (local->h_addr_list[i] != NULL) && (!islocal); i++)
{
cp = local->h_addr_list[i];
log(LOG_DEBUG,"handle_connection() : Checking if connection is from address %s.",inet_ntoa(*(struct in_addr *)cp));
if (!memcmp(&remote.sin_addr, cp, sizeof(struct in_addr)))
islocal = 1;
}
/* bypass this check for testing purposes */
islocal = 1;
/* reject a non-local connection */
if (!islocal)
{
close(new_sock);
return 0;
}
/* get the request-line */
retval = http_get_line(new_sock, &line, &len, conn_toutp);
if (retval == -1)
{
log(LOG_DEBUG,"handle_connection : Malformed input or connection lost.");
write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
close(new_sock);
return -1;
}
log(LOG_DEBUG,"handle_connection : Received this from client : %s.", line);
/* check the HTTP version */
retval = http_get_version(line, &http_ver);
if (retval == -1)
{
log(LOG_DEBUG,"handle_connection : Unable to extract the HTTP version of the incoming request.");
write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
return -1;
}
log(LOG_DEBUG,"handle_connection : Client's version is : %s.",http_ver);
// if (strcmp(http_ver, HTTPAP_VERSION)) /* not supported */
// {
// log(LOG_DEBUG,"handle_connection : Client's version is %s, I only support HTTP/1.0.",http_ver);
// write_tout(new_sock, HTTPAP_STATUS_LINE_VERSION_NOT_SUPPORTED, strlen(HTTPAP_STATUS_LINE_VERSION_NOT_SUPPORTED), conn_toutp);
// return -1;
// }
free((void *)http_ver);
/* extract the destination address and port */
retval = http_get_dest(line, &addr, &port);
if (retval == -1)
{
log(LOG_DEBUG,"handle_connection : Unable to extract destination address and port number.");
write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
return -1;
}
if (!port) /* no destination port specified, assume the default */
{
port = (unsigned char *)malloc(6);
if (!port)
{
log(LOG_ERR,"Insufficient memory.");
write_tout(new_sock, HTTPAP_STATUS_LINE_UNEXPECTED, strlen(HTTPAP_STATUS_LINE_UNEXPECTED), conn_toutp);
return -1;
}
snprintf(port,6,"%u",HTTPAP_DEFAULT_HTTP_PORT);
}
else
{
log(LOG_DEBUG,"handle_connection() : Destination address is %s.",addr);
log(LOG_DEBUG,"handle_connection() : Destination port is %s.",port);
/* convert the port to an integer */
portn = (uint16_t)strtoul(port,&errtest,0);
if ((*port == '\0') || (*errtest != '\0')) /* port conversion was unsuccessful */
{
log(LOG_DEBUG,"handle_connection : Unable to convert destination port.");
write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
return -1;
}
/* convert to network order and write back to a string */
free((void *)port);
port = (unsigned char *)malloc(6);
if (!port)
{
log(LOG_ERR,"Insufficient memory.");
write_tout(new_sock, HTTPAP_STATUS_LINE_UNEXPECTED, strlen(HTTPAP_STATUS_LINE_UNEXPECTED), conn_toutp);
return -1;
}
snprintf(port,6,"%u",portn);
}
/* create a standard structure */
ss.version = VERSION;
ss.protocol = SS_PROTOCOL_HTTP;
ss.retry_count = 0;
ss.addr_fmt = SS_ADDR_FMT_ASCII_HOST_PORT;
/* open a socket for connecting to the proxy */
sop = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if (sop < 0)
{
log(LOG_DEBUG,"handle_connection() : Error opening socket.");
write_tout(new_sock, HTTPAP_STATUS_LINE_UNEXPECTED, strlen(HTTPAP_STATUS_LINE_UNEXPECTED), conn_toutp);
return -1;
}
log(LOG_DEBUG,"handle_connection() : Socket opened.");
memset((void *)&op_addr,0,sizeof(op_addr)); /* clear the structure first */
/* set up the sockaddr_in structure */
op_addr.sin_family=AF_INET;
op_addr.sin_port=htons(op_port);
memcpy((void *)&op_addr.sin_addr,local->h_addr,local->h_length);
log(LOG_DEBUG,"handle_connection() : Trying to connect to %s at port %u.",inet_ntoa(*((struct in_addr *)local->h_addr)),op_port);
/* try to connect */
retval = connect(sop,(struct sockaddr *)&op_addr,sizeof(op_addr));
if (retval == -1)
{
log(LOG_DEBUG,"handle_connection() : Connection to the onion proxy failed.");
write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
close(sop);
return -1;
}
/* send the standard structure and the destination address+port */
retval = write_tout(sop,(unsigned char *)&ss, sizeof(ss), conn_toutp);
if (retval < sizeof(ss))
{
write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
close(sop);
return -1;
}
/* patch so the OP doesn't have to guess how long the string is. Note
* we're *no longer* sending the NULL character. */
stringlen = htons(strlen(addr));
write_tout(sop,(char *)&stringlen,sizeof(uint16_t), conn_toutp);
retval = write_tout(sop,addr,strlen(addr), conn_toutp);
if (retval < strlen(addr))
{
write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
close(sop);
return -1;
}
/* patch so the OP doesn't have to guess how long the string is. Note
* we're *no longer* sending the NULL character. */
stringlen = htons(strlen(port));
write_tout(sop,(char *)&stringlen,sizeof(short int), conn_toutp);
retval = write_tout(sop,port,strlen(port), conn_toutp);
if (retval < strlen(port))
{
write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
close(sop);
return -1;
}
/* wait for a return code */
retval = read_tout(sop, &errcode, 1, MSG_WAITALL, conn_toutp);
if (retval < 1)
{
write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
close(sop);
return -1;
}
if (!errcode) /* onion proxy says OK */
{
/* send the request-line */
retval = write_tout(sop, line, strlen(line), conn_toutp);
if (retval < strlen(line))
{
write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
close(new_sock);
return -1;
}
free((void *)line);
/* read the request headers (if any) and sanitize if necessary */
while(1)
{
retval = http_get_line(new_sock, &line, &len, conn_toutp);
if (retval == -1)
{
log(LOG_DEBUG,"handle_connection() : Malformed input or connection lost.");
write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
close(new_sock);
return -1;
}
log(LOG_DEBUG,"handle_connection() : Received this from client : %s.", line);
if (len == 2) /* empty line (CRLF only) signifying the end of headers */
{
log(LOG_DEBUG,"handle_connection() : Empty line received.");
retval = write_tout(sop,line,strlen(line),conn_toutp);
if (retval < strlen(line))
{
write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
close(new_sock);
return -1;
}
free((void *)line);
break;
}
else /* process the header */
{
retval = http_get_header_name(line, &header_name);
if (retval == -1)
{
log(LOG_DEBUG,"handle_connection : Unable to extract header name.");
write_tout(new_sock, HTTPAP_STATUS_LINE_BAD_REQUEST, strlen(HTTPAP_STATUS_LINE_BAD_REQUEST), conn_toutp);
return -1;
}
log(LOG_DEBUG,"handle_connection : Identified the header as %s.", header_name);
/* discard the Proxy-Connection header */
if (!strcmp(header_name,HTTPAP_HEADER_PROXY_CONNECTION))
free((void *)line);
else if (options[Anonimize].r.i) /* did the user request anonimization? */
{
if (!strcmp(header_name,HTTPAP_HEADER_USER_AGENT))
free((void *)line);
else if (!strcmp(header_name, HTTPAP_HEADER_REFERER))
free((void *)line);
else
{
retval = write_tout(sop, line, strlen(line), conn_toutp);
if (retval < strlen(line))
{
write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
close(new_sock);
return -1;
}
}
}
else
{
retval = write_tout(sop, line, strlen(line), conn_toutp);
if (retval < strlen(line))
{
write_tout(new_sock, HTTPAP_STATUS_LINE_UNAVAILABLE, strlen(HTTPAP_STATUS_LINE_UNAVAILABLE), conn_toutp);
close(new_sock);
return -1;
}
}
free((void *)header_name);
}
}
/* forward data in both directions until one of the principals closes it */
/* set up for select() */
log(LOG_DEBUG,"Header processed, forwarding data in both directions.");
FD_ZERO(&mask);
FD_ZERO(&rmask);
FD_SET(new_sock, &mask);
FD_SET(sop, &mask);
if (sop > new_sock)
maxfd = sop;
else
maxfd = new_sock;
while(1)
{
rmask = mask;
retval = select(maxfd+1,&rmask,NULL,NULL,NULL);
if (retval < 0)
{
log(LOG_DEBUG,"handle_connection() : select() returned a negative integer");
break;
}
if (FD_ISSET(sop,&rmask)) /* data from the onion proxy */
{
retval = read_tout(sop,buf,1024,0,conn_toutp);
if (retval <= 0)
{
log(LOG_DEBUG,"handle_connection : Conection to the onion proxy lost.");
close(sop);
close(new_sock);
break;
}
// log(LOG_DEBUG,"handle_connection() : Received %u bytes from the onion proxy.",retval);
retval = write_tout(new_sock, buf, retval, conn_toutp);
if (retval <= 0)
{
log(LOG_DEBUG, "handle_connection : Connection to the client lost.");
close(sop);
close(new_sock);
break;
}
}
if (FD_ISSET(new_sock, &rmask))
{
retval = read_tout(new_sock,buf,1024,0,conn_toutp);
if (retval <= 0)
{
log(LOG_DEBUG,"handle_connection : Conection to the client lost.");
close(sop);
close(new_sock);
break;
}
log(LOG_DEBUG,"handle_connection() : Received %u bytes from the client.",retval);
retval = write_tout(sop, buf, retval, conn_toutp);
if (retval <= 0)
{
log(LOG_DEBUG, "handle_connection : Connection to the onion proxy lost.");
close(sop);
close(new_sock);
break;
}
}
}
}
else
{
log(LOG_DEBUG,"handle_connection() : Onion proxy returned a non-zero error code (%d)!", errcode);
write_tout(new_sock, HTTPAP_STATUS_LINE_UNEXPECTED, strlen(HTTPAP_STATUS_LINE_UNEXPECTED), conn_toutp);
close(sop);
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{
int loglevel = LOG_DEBUG;
int retval = 0;
char c; /* command-line option */
int one=1;
/* configuration file */
char *conf_filename = NULL;
FILE *cf = NULL;
struct hostent *local_host;
char local_hostname[512];
struct sockaddr_in local, remote; /* local and remote address info */
int request_sock; /* where we listen for connections */
int new_sock; /* for accepted connections */
size_t sin_size; /* for accept() calls */
u_short p; /* http proxy port */
u_short op_port; /* onion proxy port */
/* used for reaping zombie processes */
struct sigaction sa;
char *errtest = NULL; /* for detecting strtoul() errors */
/* set default listening port */
p = htons(HTTPAP_LISTEN_PORT);
/* deal with program arguments */
if ((argc < 2) && (argc > 5)) /* to few or too many arguments*/
{
print_usage();
return -1;
}
opterr = 0;
while ((c = getopt(argc,argv,args)) != -1)
{
switch(c)
{
case 'f': /* config file */
conf_filename = optarg;
break;
case 'p':
p = htons((u_short)strtoul(optarg,&errtest,0));
if (errtest == optarg) /* error */
{
log(LOG_ERR,"Error : -p must be followed by an unsigned positive integer value.");
print_usage();
return -1;
}
break;
case 'h':
print_usage();
return 0;
break;
case 'l':
if (!strcmp(optarg,"emerg"))
loglevel = LOG_EMERG;
else if (!strcmp(optarg,"alert"))
loglevel = LOG_ALERT;
else if (!strcmp(optarg,"crit"))
loglevel = LOG_CRIT;
else if (!strcmp(optarg,"err"))
loglevel = LOG_ERR;
else if (!strcmp(optarg,"warning"))
loglevel = LOG_WARNING;
else if (!strcmp(optarg,"notice"))
loglevel = LOG_NOTICE;
else if (!strcmp(optarg,"info"))
loglevel = LOG_INFO;
else if (!strcmp(optarg,"debug"))
loglevel = LOG_DEBUG;
else
{
log(LOG_ERR,"Error : argument to -l must be one of alert|crit|err|warning|notice|info|debug.");
print_usage();
return -1;
}
break;
case '?':
if (isprint(c))
log(LOG_ERR,"Missing argument or unknown option '-%c'.",optopt);
else
log(LOG_ERR,"Unknown option character 'x%x'.",optopt);
print_usage();
return -1;
break;
default:
abort();
}
}
log(loglevel,NULL); /* assign severity level for logger */
/* the -f option is mandatory */
if (conf_filename == NULL)
{
log(LOG_ERR,"You must specify a config file with the -f option. See help (-h).");
return -1;
}
/* load config file */
cf = open_config(conf_filename);
if (!cf)
{
log(LOG_ERR,"Could not open configuration file %s.",conf_filename);
return -1;
}
retval = parse_config(cf,options);
if (retval)
return -1;
if (options[OnionProxy].err != 1)
{
log(LOG_ERR,"The OnionProxy option is mandatory.");
return -1;
}
if (options[MaxConn].err != 1)
{
log(LOG_ERR,"The MaxConn option is mandatory.");
return -1;
}
if (options[Anonimize].err != 1)
{
log(LOG_ERR,"The Anonimize option is mandatory.");
return -1;
}
else if ((options[Anonimize].r.i != 0) && (options[Anonimize].r.i != 1))
{
log(LOG_ERR,"The Anonimize option takes the values 1 or 0.");
return -1;
}
if (options[ConnTimeout].err != 1)
{
conn_tout.tv_sec = HTTPAP_DEFAULT_CONN_TIMEOUT;
}
else
{
if (!options[ConnTimeout].r.i)
conn_toutp = NULL;
else
conn_tout.tv_sec = options[ConnTimeout].r.i;
}
conn_tout.tv_usec = 0;
op_port = (u_short)options[OnionProxy].r.i;
/* get local address so that we know where to get the onion proxy when we need it */
retval = gethostname(local_hostname, (size_t)512);
if (retval < 0)
{
log(LOG_ERR,"Error getting local hostname");
return -1;
}
local_host = gethostbyname(local_hostname);
if (!local_host)
{
log(LOG_ERR,"Error getting local address.");
return -1;
}
log(LOG_DEBUG,"main() : Got local address : %s.",local_hostname);
/* get the server up and running */
request_sock = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);
if (request_sock < 0)
{
log(LOG_ERR,"Error opening socket.");
return -1;
}
log(LOG_DEBUG,"Socket opened.");
memset((void *)&local,0,sizeof(local)); /* clear the structure first */
/* set up the sockaddr_in structure */
local.sin_family=AF_INET;
local.sin_addr.s_addr = INADDR_ANY;
local.sin_port=p; /* p is already in network order */
setsockopt(request_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
/* bind it to the socket */
retval = bind(request_sock,(struct sockaddr *)&local, sizeof(local));
if (retval < 0)
{
log(LOG_ERR,"Error binding socket to local port %d.",ntohs(p));
return retval;
}
log(LOG_DEBUG,"Socket bound to port %d.",ntohs(p));
/* listen for connections */
retval = listen(request_sock,SOMAXCONN);
if (retval < 0)
{
log(LOG_ERR,"Could not listen for connections.");
return retval;
}
log(LOG_DEBUG,"Listening for connections.");
/* server should now be up and running */
/* install the signal handler for making sure zombie processes are killed */
sa.sa_handler = sigchld_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
retval = sigaction(SIGCHLD,&sa,NULL);
if (retval < 0)
{
log(LOG_ERR,"Could not install a signal handler.");
return -1;
}
/* main server loop */
/* I use a forking server technique - this isn't the most efficient way to do it,
* but it is simpler. */
while(1)
{
sin_size = sizeof(struct sockaddr_in);
new_sock = accept(request_sock,(struct sockaddr *)&remote,&sin_size);
if (new_sock == -1)
{
if (errno != EINTR)
log(LOG_ERR,"Could not accept socket connection.");
else
log(LOG_DEBUG,"Interrupt received.");
continue;
}
if (connections >= options[MaxConn].r.i)
{
log(LOG_NOTICE,"Number of maximum connections reached. Rejecting incoming request.");
close(new_sock);
continue;
}
log(LOG_DEBUG,"Accepted a connection from %s.",inet_ntoa(remote.sin_addr));
connections++;
if (!fork()) /* this is the child process */
{
close(request_sock); /* the child doesn't need the request socket anymore */
/* Main logic of httpap. */
retval = handle_connection(new_sock, local_host, remote, op_port);
/* End main logic */
exit(retval); /* done, exit */
}
close(new_sock); /* don't need this anymore */
}
return retval;
}
|