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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
|
/* $Id: reqs.c,v 1.1.1.1 2000-02-16 17:32:23 sdyoung Exp $
*
* This is where all the work in tinyproxy is actually done. Incoming
* connections are added to the active list of connections and then the header
* is processed to determine what is being requested. tinyproxy then connects
* to the remote server and begins to relay the bytes back and forth between
* the client and the remote server. Basically, we sit there and sling bytes
* back and forth. Optionally, we can weed out headers we do not want to send,
* and also add a header of our own.
*
* Copyright (C) 1998 Steven Young
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
* Copyright (C) 2000 Chris Lightfoot (chris@ex-parrot.com)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#ifdef HAVE_CONFIG_H
#include <defines.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/uio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sysexits.h>
#include <assert.h>
#include "config.h"
#include "tinyproxy.h"
#include "sock.h"
#include "utils.h"
#include "conns.h"
#include "log.h"
#include "reqs.h"
#include "buffer.h"
#include "filter.h"
#include "uri.h"
#include "regexp.h"
/* chris - for asynchronous DNS */
#include "dnscache.h"
#include <adns.h>
extern adns_state adns;
#ifdef XTINYPROXY
static void Add_XTinyproxy_Header(struct conn_s *connptr)
{
char *header_line;
char ipaddr[PEER_IP_LENGTH];
int length;
assert(connptr);
if (!(header_line = xmalloc(sizeof(char) * 32)))
return;
length = sprintf(header_line, "X-Tinyproxy: %s\r\n",
getpeer_ip(connptr->client_fd, ipaddr));
unshift_buffer(connptr->cbuffer, header_line, length);
}
#endif
#define HTTPPATTERN "^([a-z]+)[ \t]+([^ \t]+)([ \t]+(HTTP/[0-9]+\\.[0-9]+))?"
#define NMATCH 4
#define METHOD_IND 1
#define URI_IND 2
#define VERSION_MARK 3
#define VERSION_IND 4
#define HTTP400ERROR "Unrecognizable request. Only HTTP is allowed."
#define HTTP500ERROR "Unable to connect to remote server."
#define HTTP503ERROR "Internal server error."
/*
* Parse a client HTTP request and then establish connection.
*/
static int clientreq(struct conn_s *connptr)
{
URI *uri = NULL;
char *inbuf, *buffer, *request, *port;
char *inbuf_ptr;
regex_t preg;
regmatch_t pmatch[NMATCH];
long len;
int fd, port_no;
char peer_ipaddr[PEER_IP_LENGTH];
assert(connptr);
getpeer_ip(connptr->client_fd, peer_ipaddr);
/* chris - getpeer_string could block, so for the moment take it out */
if ((len
= readline(connptr->client_fd, connptr->cbuffer, &inbuf)) <= 0) {
return len;
}
inbuf_ptr = inbuf + len - 1;
while (*inbuf_ptr == '\r' || *inbuf_ptr == '\n')
*inbuf_ptr-- = '\0';
/* Log the incoming connection */
if (!config.restricted) {
log("Connect: %s", peer_ipaddr);
log("Request: %s", inbuf);
}
if (regcomp(&preg, HTTPPATTERN, REG_EXTENDED | REG_ICASE) != 0) {
log("ERROR clientreq: regcomp");
return 0;
}
if (regexec(&preg, inbuf, NMATCH, pmatch, 0) != 0) {
log("ERROR clientreq: regexec");
regfree(&preg);
return -1;
}
regfree(&preg);
if (pmatch[VERSION_MARK].rm_so == -1)
connptr->simple_req = TRUE;
if (pmatch[METHOD_IND].rm_so == -1 || pmatch[URI_IND].rm_so == -1) {
log("ERROR clientreq: Incomplete line from %s (%s)",
peer_ipaddr, inbuf);
httperr(connptr, 400, HTTP400ERROR);
goto COMMON_EXIT;
}
len = pmatch[URI_IND].rm_eo - pmatch[URI_IND].rm_so;
if (!(buffer = xmalloc(len + 1))) {
log("ERROR clientreq: Cannot allocate buffer for request from %s",
peer_ipaddr);
httperr(connptr, 503, HTTP503ERROR);
goto COMMON_EXIT;
}
memcpy(buffer, inbuf + pmatch[URI_IND].rm_so, len);
buffer[len] = '\0';
if (!(uri = explode_uri(buffer))) {
safefree(buffer);
log("ERROR clientreq: Problem with explode_uri");
httperr(connptr, 503, HTTP503ERROR);
goto COMMON_EXIT;
}
safefree(buffer);
if (strcasecmp(uri->scheme, "http") != 0) {
char *error_string = xmalloc(strlen(uri->scheme) + 64);
sprintf(error_string, "Invalid scheme (%s). Only HTTP is allowed.",
uri->scheme);
httperr(connptr, 400, error_string);
safefree(error_string);
goto COMMON_EXIT;
}
if ((strlen(config.stathost) > 0) &&
strcasecmp(uri->authority, config.stathost) == 0) {
showstats(connptr);
goto COMMON_EXIT;
}
port_no = 80;
if ((port = strchr(uri->authority, ':'))) {
*port++ = '\0';
if (strlen(port) > 0)
port_no = atoi(port);
}
/* chris - so this can be passed on to clientreq_dnscomplete. */
connptr->port_no = port_no;
#ifdef FILTER_ENABLE
/* Filter domains out */
if (config.filter) {
if (filter_host(uri->authority)) {
log("ERROR clientreq: Filtered connection (%s)",
peer_ipaddr);
httperr(connptr, 404,
"Unable to connect to filtered host.");
goto COMMON_EXIT;
}
}
#endif /* FILTER_ENABLE */
/* Build a new request from the first line of the header */
if (!(request = xmalloc(strlen(inbuf) + 1))) {
log("ERROR clientreq: cannot allocate buffer for request from %s",
peer_ipaddr);
httperr(connptr, 503, HTTP503ERROR);
goto COMMON_EXIT;
}
/* We need to set the version number WE support */
memcpy(request, inbuf, pmatch[METHOD_IND].rm_eo);
request[pmatch[METHOD_IND].rm_eo] = '\0';
strcat(request, " ");
strcat(request, uri->path);
if (uri->query) {
strcat(request, "?");
strcat(request, uri->query);
}
strcat(request, " HTTP/1.0\r\n");
/* chris - If domain is in dotted-quad format or is already in the
* DNS cache, then go straight to WAITCONN.
*/
if (inet_aton(uri->authority, NULL) || lookup(NULL, uri->authority) == 0) {
if ((fd = opensock(uri->authority, port_no)) < 0) {
safefree(request);
httperr(connptr, 500,
"Unable to connect to host (cannot create sock)");
stats.num_badcons++;
goto COMMON_EXIT;
}
connptr->server_fd = fd;
connptr->type = WAITCONN;
}
/* Otherwise submit a DNS request and hope for the best. */
else {
if (adns_submit(adns, uri->authority, adns_r_a, adns_qf_quoteok_cname | adns_qf_cname_loose, connptr, &(connptr->adns_qu))) {
safefree(request);
httperr(connptr, 500, "Resolver error connecting to host");
stats.num_badcons++;
goto COMMON_EXIT;
} else {
#ifdef __DEBUG__
log("DNS request submitted");
#endif
/* copy domain for later caching */
connptr->domain = strdup(uri->authority);
connptr->type = DNS_WAITCONN;
}
}
#ifdef XTINYPROXY
/* Add a X-Tinyproxy header which contains our IP address */
if (config.my_domain
&& xstrstr(uri->authority, config.my_domain,
strlen(uri->authority), FALSE)) {
Add_XTinyproxy_Header(connptr);
}
#endif
/* Add the rewritten request to the buffer */
unshift_buffer(connptr->cbuffer, request, strlen(request));
/*
* HACK HACK HACK: When we're sending a POST there is no restriction
* on the length of the header. If we don't let all the header lines
* through, the POST will not work. This _definitely_ needs to be
* fixed. - rjkaes
*/
if (!xstrstr(inbuf, "POST ", 5, FALSE)) {
connptr->clientheader = TRUE;
}
COMMON_EXIT:
safefree(inbuf);
free_uri(uri);
return 0;
}
/* chris - added this to move a connection from the DNS_WAITCONN state
* to the WAITCONN state by connecting it to the server, once the name
* has been resolved.
*/
static int clientreq_dnscomplete(struct conn_s *connptr, struct in_addr *inaddr) {
int fd;
fd = opensock_inaddr(inaddr, connptr->port_no);
if (fd < 0) {
#ifdef __DEBUG__
log("Failed to open connection to server");
#endif
httperr(connptr, 500,
"Unable to connect to host (cannot create sock)");
stats.num_badcons++;
return 0;
} else {
#ifdef __DEBUG__
log("Connected to server");
#endif
connptr->server_fd = fd;
connptr->type = WAITCONN;
}
return 0;
}
/*
* Finish the client request
*/
static int clientreq_finish(struct conn_s *connptr)
{
int sockopt, len = sizeof(sockopt);
assert(connptr);
if (getsockopt
(connptr->server_fd, SOL_SOCKET, SO_ERROR, &sockopt, &len) < 0) {
log("ERROR clientreq_finish: getsockopt error (%s)",
strerror(errno));
return -1;
}
if (sockopt != 0) {
if (sockopt == EINPROGRESS)
return 0;
else if (sockopt == ETIMEDOUT) {
httperr(connptr, 408, "Connect Timed Out");
return 0;
} else {
log("ERROR clientreq_finish: could not create connection (%s)",
strerror(sockopt));
return -1;
}
}
connptr->type = RELAYCONN;
stats.num_opens++;
return 0;
}
/*
* Check to see if the line is allowed or not depending on the anonymous
* headers which are to be allowed.
*/
static int anonheader(char *line)
{
struct allowedhdr_s *allowedptr = allowedhdrs;
assert(line);
assert(allowedhdrs);
if (!xstrstr(line, "GET ", 4, FALSE)
|| !xstrstr(line, "POST ", 5, FALSE)
|| !xstrstr(line, "HEAD ", 5, FALSE))
return 1;
for (allowedptr = allowedhdrs; allowedptr;
allowedptr = allowedptr->next) {
if (!strncasecmp
(line, allowedptr->hdrname, strlen(allowedptr->hdrname))) {
return 1;
}
}
return 0;
}
/*
* Used to read in the lines from the header (client side) when we're doing
* the anonymous header reduction.
*/
static int readanonconn(struct conn_s *connptr)
{
char *line = NULL;
int retv;
assert(connptr);
if ((retv = readline(connptr->client_fd, connptr->cbuffer, &line)) <=
0) {
return retv;
}
if ((line[0] == '\n') || (strncmp(line, "\r\n", 2) == 0)) {
connptr->clientheader = TRUE;
} else if (!anonheader(line)) {
safefree(line);
return 0;
}
push_buffer(connptr->cbuffer, line, strlen(line));
return 0;
}
/*
* Read in the bytes from the socket
*/
static int readconn(int fd, struct buffer_s *buffptr)
{
int bytesin;
assert(fd >= 0);
assert(buffptr);
if ((bytesin = readbuff(fd, buffptr)) < 0) {
return -1;
}
#ifdef __DEBUG__
log("readconn [%d]: %d", fd, bytesin);
#endif
stats.num_rx += bytesin;
return bytesin;
}
/*
* Write the bytes from the buffer to the socket
*/
static int writeconn(int fd, struct buffer_s *buffptr)
{
int bytessent;
assert(fd >= 0);
assert(buffptr);
if ((bytessent = writebuff(fd, buffptr)) < 0) {
return -1;
}
stats.num_tx += bytessent;
return bytessent;
}
/*
* Factored out the common function to read from the client. It was used in
* two different places with no change, so no point in having the same code
* twice.
*/
static int read_from_client(struct conn_s *connptr, fd_set * readfds)
{
assert(connptr);
assert(readfds);
if (FD_ISSET(connptr->client_fd, readfds)) {
if (config.anonymous && !connptr->clientheader) {
if (readanonconn(connptr) < 0) {
shutdown(connptr->client_fd, 2);
shutdown(connptr->server_fd, 2);
connptr->type = FINISHCONN;
return -1;
}
} else if (readconn(connptr->client_fd, connptr->cbuffer) < 0) {
shutdown(connptr->client_fd, 2);
shutdown(connptr->server_fd, 2);
connptr->type = FINISHCONN;
return -1;
}
connptr->actiontime = time(NULL);
}
return 0;
}
/*
* Factored out the common write to client function since, again, it was used
* in two different places with no changes.
*/
static int write_to_client(struct conn_s *connptr, fd_set * writefds)
{
assert(connptr);
assert(writefds);
if (FD_ISSET(connptr->client_fd, writefds)) {
if (writeconn(connptr->client_fd, connptr->sbuffer) < 0) {
shutdown(connptr->client_fd, 2);
shutdown(connptr->server_fd, 2);
connptr->type = FINISHCONN;
return -1;
}
connptr->actiontime = time(NULL);
}
return 0;
}
/*
* All of the *_req functions handle the various stages a connection can go
* through. I moved them out from getreqs because they handle a lot of error
* code and it was indenting too far in. As you can see they are very simple,
* and are only called once from getreqs, hence the inlining.
*/
inline static void newconn_req(struct conn_s *connptr, fd_set * readfds)
{
assert(connptr);
assert(readfds);
if (FD_ISSET(connptr->client_fd, readfds)) {
if (clientreq(connptr) < 0) {
shutdown(connptr->client_fd, 2);
connptr->type = FINISHCONN;
return;
}
if (!connptr)
abort();
connptr->actiontime = time(NULL);
}
}
inline static void waitconn_req(struct conn_s *connptr, fd_set * readfds,
fd_set * writefds)
{
assert(connptr);
assert(readfds);
assert(writefds);
if (read_from_client(connptr, readfds) < 0)
return;
if (FD_ISSET(connptr->server_fd, readfds)
|| FD_ISSET(connptr->server_fd, writefds)) {
if (clientreq_finish(connptr) < 0) {
shutdown(connptr->server_fd, 2);
shutdown(connptr->client_fd, 2);
connptr->type = FINISHCONN;
return;
}
connptr->actiontime = time(NULL);
}
}
inline static void relayconn_req(struct conn_s *connptr, fd_set * readfds,
fd_set * writefds)
{
assert(connptr);
assert(readfds);
assert(writefds);
if (read_from_client(connptr, readfds) < 0)
return;
if (FD_ISSET(connptr->server_fd, readfds)) {
if (connptr->serverheader) {
if (readconn(connptr->server_fd, connptr->sbuffer) < 0) {
shutdown(connptr->server_fd, 2);
connptr->type = CLOSINGCONN;
return;
}
} else {
/*
* We need to read in the first line to rewrite the
* version back down to HTTP/1.0 (if needed)
*/
char *line = NULL, *ptr, *newline;
int retv;
if (
(retv =
readline(connptr->server_fd, connptr->sbuffer,
&line)) < 0) {
shutdown(connptr->server_fd, 2);
httperr(connptr, 500, "Server Closed Early");
return;
} else if (retv == 0)
return;
connptr->serverheader = TRUE;
if (strncasecmp(line, "HTTP/1.0", 8)) {
/* Okay, we need to rewrite it then */
if (!(ptr = strchr(line, ' '))) {
shutdown(connptr->server_fd, 2);
httperr(connptr, 500,
"There was Server Error");
return;
}
ptr++;
if (!(newline = xmalloc(strlen(line) + 1))) {
shutdown(connptr->server_fd, 2);
httperr(connptr, 503,
"No Memory Available");
return;
}
sprintf(newline, "HTTP/1.0 %s", ptr);
safefree(line);
line = newline;
}
push_buffer(connptr->sbuffer, line, strlen(line));
}
connptr->actiontime = time(NULL);
}
if (write_to_client(connptr, writefds) < 0)
return;
if (FD_ISSET(connptr->server_fd, writefds)) {
if (writeconn(connptr->server_fd, connptr->cbuffer) < 0) {
shutdown(connptr->server_fd, 2);
connptr->type = CLOSINGCONN;
return;
}
connptr->actiontime = time(NULL);
}
}
inline static void closingconn_req(struct conn_s *connptr, fd_set * writefds)
{
assert(connptr);
assert(writefds);
write_to_client(connptr, writefds);
}
/*
* Check against the valid subnet to see if we should allow the access
*/
static int validuser(int fd)
{
char ipaddr[PEER_IP_LENGTH];
assert(fd >= 0);
if (config.subnet == NULL)
return 1;
if (!strncmp(config.subnet, getpeer_ip(fd, ipaddr),
strlen(config.subnet))) {
return 1;
} else {
return 0;
}
}
/*
* Loop that checks for new connections, dispatches to the correct
* routines if bytes are pending, checks to see if it's time for a
* garbage collect.
*/
int getreqs(void)
{
static unsigned int garbc = 0;
fd_set readfds, writefds, exceptfds; /* chris - ADNS expects exceptfds */
struct conn_s *connptr;
int fd;
struct timeval tv, now; /* chris - for ADNS timeouts */
char peer_ipaddr[PEER_IP_LENGTH];
if (setup_fd < 0) {
log("ERROR getreqs: setup_fd not a socket");
return -1;
}
/* Garbage collect the dead connections and close any idle ones */
if (garbc++ >= GARBCOLL_INTERVAL) {
garbcoll();
garbc = 0;
}
conncoll();
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_SET(setup_fd, &readfds);
for (connptr = connections; connptr; connptr = connptr->next) {
#ifdef __DEBUG__
log("Connptr: %p - %d / client %d server %d", connptr,
connptr->type, connptr->client_fd, connptr->server_fd);
#endif
switch (connptr->type) {
case NEWCONN:
if (buffer_size(connptr->cbuffer) < MAXBUFFSIZE)
FD_SET(connptr->client_fd, &readfds);
else {
httperr(connptr, 414,
"Your Request is way too long.");
}
break;
/* no case here for DNS_WAITCONN */
case WAITCONN:
FD_SET(connptr->server_fd, &readfds);
FD_SET(connptr->server_fd, &writefds);
if (buffer_size(connptr->cbuffer) < MAXBUFFSIZE)
FD_SET(connptr->client_fd, &readfds);
break;
case RELAYCONN:
if (buffer_size(connptr->sbuffer) > 0)
FD_SET(connptr->client_fd, &writefds);
if (buffer_size(connptr->sbuffer) < MAXBUFFSIZE)
FD_SET(connptr->server_fd, &readfds);
if (buffer_size(connptr->cbuffer) > 0)
FD_SET(connptr->server_fd, &writefds);
if (buffer_size(connptr->cbuffer) < MAXBUFFSIZE)
FD_SET(connptr->client_fd, &readfds);
break;
case CLOSINGCONN:
if (buffer_size(connptr->sbuffer) > 0)
FD_SET(connptr->client_fd, &writefds);
else {
shutdown(connptr->client_fd, 2);
shutdown(connptr->server_fd, 2);
connptr->type = FINISHCONN;
}
break;
default:
break;
}
}
/* Set a 60 second time out */
tv.tv_sec = 1;//60;
tv.tv_usec = 0;
/* chris - Make ADNS do its stuff, too. */
{
struct timeval *tv_mod = &tv;
int foo = FD_SETSIZE;
gettimeofday(&now, NULL);
FD_ZERO(&exceptfds);
adns_beforeselect(adns, &foo, &readfds, &writefds, &exceptfds, &tv_mod, NULL, &now);
}
if (select(FD_SETSIZE, &readfds, &writefds, &exceptfds, &tv) < 0) {
#ifdef __DEBUG__
log("select error: %s", strerror(errno));
#endif
return 0;
}
/* chris - see whether any ADNS lookups have completed */
gettimeofday(&now, NULL);
adns_afterselect(adns, FD_SETSIZE, &readfds, &writefds, &exceptfds, &now);
for (connptr = connections; connptr; connptr = connptr->next) {
adns_answer *ans;
if (connptr->type == DNS_WAITCONN &&
adns_check(adns, &(connptr->adns_qu), &ans, (void**)&connptr) == 0) {
if (ans->status == adns_s_ok) {
if (connptr->domain) {
insert(ans->rrs.inaddr, connptr->domain);
free(connptr->domain);
}
clientreq_dnscomplete(connptr, ans->rrs.inaddr);
free(ans);
/* hack */
FD_SET(connptr->server_fd, &readfds);
#ifdef __DEBUG__
log("DNS resolution successful");
#endif
} else {
free(ans);
httperr(connptr, 500, "Unable to resolve hostname in URL");
#ifdef __DEBUG__
log("DNS resolution failed");
#endif
}
}
}
/* Check to see if there are new connections pending */
if (FD_ISSET(setup_fd, &readfds) && (fd = listen_sock()) >= 0) {
new_conn(fd); /* make a connection from the FD */
if (validuser(fd)) {
if (config.cutoffload && (load > config.cutoffload)) {
stats.num_refused++;
httperr(connptr, 503,
"tinyproxy is not accepting connections due to high system load");
}
} else {
httperr(connptr, 403,
"You are not authorized to use the service.");
log("AUTH Rejected connection from %s",
getpeer_ip(fd, peer_ipaddr));
}
}
/*
* Loop through the connections and dispatch them to the appropriate
* handler
*/
for (connptr = connections; connptr; connptr = connptr->next) {
switch (connptr->type) {
case NEWCONN:
newconn_req(connptr, &readfds);
break;
case WAITCONN:
waitconn_req(connptr, &readfds, &writefds);
break;
case RELAYCONN:
relayconn_req(connptr, &readfds, &writefds);
break;
case CLOSINGCONN:
closingconn_req(connptr, &writefds);
break;
default:
break;
}
}
return 0;
}
|