Debian Codegraph @

tunnel.c

raw ◀ ▶
Source reconstructed at patch state 0 of 5 — not the installed (fully patched) source.
5 Debian patches touch this file
1 /*
2 tunnel.c
3
4 Copyright (C) 1999, 2000 Lars Brinkhoff. See COPYING for terms and conditions.
5
6 See tunnel.h for some documentation about the programming interface.
7 */
8
9 #include <time.h>
10 #include <stdio.h>
11 #include <netdb_.h>
12 #include <fcntl.h>
13 #include <stdlib.h>
14 #include <sys/poll_.h>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <netinet/tcp.h>
18
19 #include "http.h"
20 #include "tunnel.h"
21 #include "common.h"
22
23 /* #define IO_COUNT_HTTP_HEADER */
24 /* #define USE_SHUTDOWN */
25
26 #define READ_TRAIL_TIMEOUT (1 * 1000) /* milliseconds */
27 #define ACCEPT_TIMEOUT 10 /* seconds */
28
29 #define min(a, b) ((a) < (b) ? (a) : (b))
30 #define TUNNEL_IN 1
31 #define TUNNEL_OUT 2
32
33 #if SIZEOF_CHAR == 1
34 typedef unsigned char Request;
35 #else
36 #error "FIXME: Can't handle SIZEOF_CHAR != 1"
37 #endif
38
39 #if SIZEOF_SHORT == 2
40 typedef unsigned short Length;
41 #else
42 #error "FIXME: Can't handle SIZEOF_SHORT != 2"
43 #endif
44
45 enum tunnel_request
46 {
47 TUNNEL_SIMPLE = 0x40,
48 TUNNEL_OPEN = 0x01,
49 TUNNEL_DATA = 0x02,
50 TUNNEL_PADDING = 0x03,
51 TUNNEL_ERROR = 0x04,
52 TUNNEL_PAD1 = TUNNEL_SIMPLE | 0x05,
53 TUNNEL_CLOSE = TUNNEL_SIMPLE | 0x06,
54 TUNNEL_DISCONNECT = TUNNEL_SIMPLE | 0x07
55 };
56
57 static inline const char *
58 REQ_TO_STRING (Request request)
59 {
60 switch (request)
61 {
62 case TUNNEL_OPEN: return "TUNNEL_OPEN";
63 case TUNNEL_DATA: return "TUNNEL_DATA";
64 case TUNNEL_PADDING: return "TUNNEL_PADDING";
65 case TUNNEL_ERROR: return "TUNNEL_ERROR";
66 case TUNNEL_PAD1: return "TUNNEL_PAD1";
67 case TUNNEL_CLOSE: return "TUNNEL_CLOSE";
68 case TUNNEL_DISCONNECT: return "TUNNEL_DISCONNECT";
69 default: return "(unknown)";
70 }
71 }
72
73 struct tunnel
74 {
75 int in_fd, out_fd;
76 int server_socket;
77 Http_destination dest;
78 struct sockaddr_in address;
79 size_t bytes;
80 size_t content_length;
81 char buf[65536];
82 char *buf_ptr;
83 size_t buf_len;
84 int padding_only;
85 size_t in_total_raw;
86 size_t in_total_data;
87 size_t out_total_raw;
88 size_t out_total_data;
89 time_t out_connect_time;
90 int strict_content_length;
91 int keep_alive;
92 int max_connection_age;
93 };
94
95 static const size_t sizeof_header = sizeof (Request) + sizeof (Length);
96
97 static inline int
98 tunnel_is_disconnected (Tunnel *tunnel)
99 {
100 return tunnel->out_fd == -1;
101 }
102
103 static inline int
104 tunnel_is_connected (Tunnel *tunnel)
105 {
106 return !tunnel_is_disconnected (tunnel);
107 }
108
109 static inline int
110 tunnel_is_server (Tunnel *tunnel)
111 {
112 return tunnel->server_socket != -1;
113 }
114
115 static inline int
116 tunnel_is_client (Tunnel *tunnel)
117 {
118 return !tunnel_is_server (tunnel);
119 }
120
121 #if 1
122 static int
123 get_proto_number (const char *name)
124 {
125 struct protoent *p;
126 int number;
127
128 p = getprotobyname (name);
129 if (p == NULL)
130 number = -1;
131 else
132 number = p->p_proto;
133 endprotoent ();
134
135 return number;
136 }
137 #endif
138
139 static int
140 tunnel_in_setsockopts (int fd)
141 {
142 #ifdef SO_RCVLOWAT
143 int tcp = get_proto_number ("tcp");
144
145 if (tcp != -1)
146 {
147 int i, n;
148
149 i = 1;
150 if (setsockopt (fd,
151 tcp,
152 SO_RCVLOWAT,
153 (void *)&i,
154 sizeof i) == -1)
155 {
156 log_debug ("tunnel_in_setsockopts: non-fatal SO_RCVLOWAT error: %s",
157 strerror (errno));
158 }
159 n = sizeof i;
160 getsockopt (fd,
161 tcp,
162 SO_RCVLOWAT,
163 (void *)&i,
164 &n);
165 log_debug ("tunnel_out_setsockopts: SO_RCVLOWAT: %d", i);
166 }
167 #endif /* SO_RCVLOWAT */
168
169 return 0;
170 }
171
172 static int
173 tunnel_out_setsockopts (int fd)
174 {
175 #ifdef SO_SNDLOWAT
176 {
177 int tcp = get_proto_number ("tcp");
178 int i, n;
179
180 if (tcp != -1)
181 {
182 i = 1;
183 if (setsockopt (fd,
184 tcp,
185 SO_SNDLOWAT,
186 (void *)&i,
187 sizeof i) == -1)
188 {
189 log_debug ("tunnel_out_setsockopts: "
190 "non-fatal SO_SNDLOWAT error: %s",
191 strerror (errno));
192 }
193 n = sizeof i;
194 getsockopt (fd,
195 tcp,
196 SO_SNDLOWAT,
197 (void *)&i,
198 &n);
199 log_debug ("tunnel_out_setsockopts: non-fatal SO_SNDLOWAT: %d", i);
200 }
201 }
202 #endif /* SO_SNDLOWAT */
203
204 #ifdef SO_LINGER
205 {
206 struct linger l;
207 int n;
208
209 l.l_onoff = 1;
210 l.l_linger = 20 * 100; /* linger for 20 seconds */
211 if (setsockopt (fd,
212 SOL_SOCKET,
213 SO_LINGER,
214 (void *)&l,
215 sizeof l) == -1)
216 {
217 log_debug ("tunnel_out_setsockopts: non-fatal SO_LINGER error: %s",
218 strerror (errno));
219 }
220 n = sizeof l;
221 getsockopt (fd,
222 SOL_SOCKET,
223 SO_LINGER,
224 (void *)&l,
225 &n);
226 log_debug ("tunnel_out_setsockopts: SO_LINGER: onoff=%d linger=%d",
227 l.l_onoff, l.l_linger);
228 }
229 #endif /* SO_LINGER */
230
231 #ifdef TCP_NODELAY
232 {
233 int tcp = get_proto_number ("tcp");
234 int i, n;
235
236 if (tcp != -1)
237 {
238 i = 1;
239 if (setsockopt (fd,
240 tcp,
241 TCP_NODELAY,
242 (void *)&i,
243 sizeof i) == -1)
244 {
245 log_debug ("tunnel_out_setsockopts: "
246 "non-fatal TCP_NODELAY error: %s",
247 strerror (errno));
248 }
249 n = sizeof i;
250 getsockopt (fd,
251 tcp,
252 TCP_NODELAY,
253 (void *)&i,
254 &n);
255 log_debug ("tunnel_out_setsockopts: non-fatal TCP_NODELAY: %d", i);
256 }
257 }
258 #else
259 #ifdef SO_SNDBUF
260 {
261 int i, n;
262
263 i = 0;
264 if (setsockopt (fd,
265 SOL_SOCKET,
266 SO_SNDBUF,
267 (void *)&i,
268 sizeof i) == -1)
269 {
270 log_debug ("tunnel_out_setsockopts: non-fatal SO_SNDBUF error: %s",
271 strerror (errno));
272 }
273 n = sizeof i;
274 getsockopt (fd,
275 SOL_SOCKET,
276 SO_SNDBUF,
277 (void *)&i,
278 &n);
279 log_debug ("tunnel_out_setsockopts: SO_SNDBUF: %d", i);
280 }
281 #endif /* SO_SNDBUF */
282 #endif /* TCP_NODELAY */
283
284 #ifdef SO_KEEPALIVE
285 {
286 int i, n;
287
288 i = 1;
289 if (setsockopt (fd,
290 SOL_SOCKET,
291 SO_KEEPALIVE,
292 (void *)&i,
293 sizeof i) == -1)
294 {
295 log_debug ("tunnel_out_setsockopts: non-fatal SO_KEEPALIVE error: %s",
296 strerror (errno));
297 }
298 n = sizeof i;
299 getsockopt (fd,
300 SOL_SOCKET,
301 SO_KEEPALIVE,
302 (void *)&i,
303 &n);
304 log_debug ("tunnel_out_setsockopts: SO_KEEPALIVE: %d", i);
305 }
306 #endif /* SO_KEEPALIVE */
307
308 return 0;
309 }
310
311 static void
312 tunnel_out_disconnect (Tunnel *tunnel)
313 {
314 if (tunnel_is_disconnected (tunnel))
315 return;
316
317 #ifdef DEBUG_MODE
318 if (tunnel_is_client (tunnel) &&
319 tunnel->bytes != tunnel->content_length + 1)
320 log_error ("tunnel_out_disconnect: warning: "
321 "bytes=%d != content_length=%d",
322 tunnel->bytes, tunnel->content_length + 1);
323 #endif
324
325 close (tunnel->out_fd);
326 tunnel->out_fd = -1;
327 tunnel->bytes = 0;
328 tunnel->buf_ptr = tunnel->buf;
329 tunnel->buf_len = 0;
330
331 log_debug ("tunnel_out_disconnect: output disconnected");
332 }
333
334 static void
335 tunnel_in_disconnect (Tunnel *tunnel)
336 {
337 if (tunnel->in_fd == -1)
338 return;
339
340 close (tunnel->in_fd);
341 tunnel->in_fd = -1;
342
343 log_debug ("tunnel_in_disconnect: input disconnected");
344 }
345
346 static int
347 tunnel_out_connect (Tunnel *tunnel)
348 {
349 ssize_t n;
350
351 if (tunnel_is_connected (tunnel))
352 {
353 log_debug ("tunnel_out_connect: already connected");
354 tunnel_out_disconnect (tunnel);
355 }
356
357 tunnel->out_fd = do_connect (&tunnel->address);
358 if (tunnel->out_fd == -1)
359 {
360 log_error ("tunnel_out_connect: do_connect (%d.%d.%d.%d:%u) error: %s",
361 ntohl (tunnel->address.sin_addr.s_addr) >> 24,
362 (ntohl (tunnel->address.sin_addr.s_addr) >> 16) & 0xff,
363 (ntohl (tunnel->address.sin_addr.s_addr) >> 8) & 0xff,
364 ntohl (tunnel->address.sin_addr.s_addr) & 0xff,
365 ntohs (tunnel->address.sin_port),
366 strerror (errno));
367 return -1;
368 }
369
370 tunnel_out_setsockopts (tunnel->out_fd);
371
372 #ifdef USE_SHUTDOWN
373 shutdown (tunnel->out_fd, 0);
374 #endif
375
376 /* + 1 to allow for TUNNEL_DISCONNECT */
377 n = http_post (tunnel->out_fd,
378 &tunnel->dest,
379 tunnel->content_length + 1);
380 if (n == -1)
381 return -1;
382 #ifdef IO_COUNT_HTTP_HEADER
383 tunnel->out_total_raw += n;
384 log_annoying ("tunnel_out_connect: out_total_raw = %u",
385 tunnel->out_total_raw);
386 #endif
387
388 tunnel->bytes = 0;
389 tunnel->buf_ptr = tunnel->buf;
390 tunnel->buf_len = 0;
391 tunnel->padding_only = TRUE;
392 time (&tunnel->out_connect_time);
393
394 log_debug ("tunnel_out_connect: output connected");
395
396 return 0;
397 }
398
399 static int
400 tunnel_in_connect (Tunnel *tunnel)
401 {
402 Http_response *response;
403 ssize_t n;
404
405 log_verbose ("tunnel_in_connect()");
406
407 if (tunnel->in_fd != -1)
408 {
409 log_error ("tunnel_in_connect: already connected");
410 return -1;
411 }
412
413 tunnel->in_fd = do_connect (&tunnel->address);
414 if (tunnel->in_fd == -1)
415 {
416 log_error ("tunnel_in_connect: do_connect() error: %s",
417 strerror (errno));
418 return -1;
419 }
420
421 tunnel_in_setsockopts (tunnel->in_fd);
422
423 if (http_get (tunnel->in_fd, &tunnel->dest) == -1)
424 return -1;
425
426 #ifdef USE_SHUTDOWN
427 if (shutdown (tunnel->in_fd, 1) == -1)
428 {
429 log_error ("tunnel_in_connect: shutdown() error: %s",
430 strerror (errno));
431 return -1;
432 }
433 #endif
434
435 n = http_parse_response (tunnel->in_fd, &response);
436 if (n <= 0)
437 {
438 if (n == 0)
439 log_error ("tunnel_in_connect: no response; peer "
440 "closed connection");
441 else
442 log_error ("tunnel_in_connect: no response; error: %s",
443 strerror (errno));
444 }
445 else if (response->major_version != 1 ||
446 (response->minor_version != 1 &&
447 response->minor_version != 0))
448 {
449 log_error ("tunnel_in_connect: unknown HTTP version: %d.%d",
450 response->major_version, response->minor_version);
451 n = -1;
452 }
453 else if (response->status_code != 200)
454 {
455 log_error ("tunnel_in_connect: HTTP error %d", response->status_code);
456 errno = http_error_to_errno (-response->status_code);
457 n = -1;
458 }
459
460 if (response)
461 http_destroy_response (response);
462
463 if (n > 0)
464 {
465 #ifdef IO_COUNT_HTTP_HEADER
466 tunnel->in_total_raw += n;
467 log_annoying ("tunnel_in_connect: in_total_raw = %u",
468 tunnel->in_total_raw);
469 #endif
470 }
471 else
472 {
473 return n;
474 }
475
476 log_debug ("tunnel_in_connect: input connected");
477 return 1;
478 }
479
480 static inline ssize_t
481 tunnel_write_data (Tunnel *tunnel, void *data, size_t length)
482 {
483 if (write_all (tunnel->out_fd, data, length) == -1)
484 {
485 log_error ("tunnel_write_data: write error: %s", strerror (errno));
486 return -1;
487 }
488 tunnel->bytes += length;
489 return length;
490 }
491
492 static int
493 tunnel_write_request (Tunnel *tunnel, Request request,
494 void *data, Length length)
495 {
496 if (tunnel->bytes + sizeof request +
497 (data ? sizeof length + length : 0) > tunnel->content_length)
498 tunnel_padding (tunnel, tunnel->content_length - tunnel->bytes);
499
500 #if 1 /* FIXME: this is a kludge */
501 {
502 time_t t;
503
504 time (&t);
505 if (tunnel_is_client (tunnel) &&
506 tunnel_is_connected (tunnel) &&
507 t - tunnel->out_connect_time > tunnel->max_connection_age)
508 {
509 char c = TUNNEL_DISCONNECT;
510
511 log_debug ("tunnel_write_request: connection > %d seconds old",
512 tunnel->max_connection_age);
513
514 if (tunnel->strict_content_length)
515 {
516 int l = tunnel->content_length - tunnel->bytes - 1;
517
518 log_debug ("tunnel_write_request: write padding (%d bytes)",
519 tunnel->content_length - tunnel->bytes - 1);
520 if (l > 3)
521 {
522 char c;
523 short s;
524 int i;
525
526 c = TUNNEL_PADDING;
527 tunnel_write_data (tunnel, &c, sizeof c);
528
529 s = htons(l-2);
530 tunnel_write_data (tunnel, &s, sizeof s);
531
532 l -= 2;
533 c = 0;
534 for (i=0; i<l; i++)
535 tunnel_write_data (tunnel, &c, sizeof c);
536 }
537 else
538 {
539 char c = TUNNEL_PAD1;
540 int i;
541
542 for (i=0; i<l; i++)
543 tunnel_write_data (tunnel, &c, sizeof c);
544 }
545 }
546
547 log_debug ("tunnel_write_request: closing old connection");
548 if (tunnel_write_data (tunnel, &c, sizeof c) <= 0)
549 return -1;
550 tunnel_out_disconnect (tunnel);
551 }
552 }
553 #endif
554
555 if (tunnel_is_disconnected (tunnel))
556 {
557 if (tunnel_is_client (tunnel))
558 {
559 if (tunnel_out_connect (tunnel) == -1)
560 return -1;
561 }
562 else
563 {
564 #if 0
565 log_error ("tunnel_write_request: output is disconnected");
566 errno = EIO;
567 return -1;
568 #else
569 if (tunnel_accept (tunnel) == -1)
570 return -1;
571 #endif
572 }
573 }
574
575 if (request != TUNNEL_PADDING && request != TUNNEL_PAD1)
576 tunnel->padding_only = FALSE;
577
578 if (tunnel_write_data (tunnel, &request, sizeof request) == -1)
579 {
580 if (errno != EPIPE)
581 return -1;
582
583 tunnel_out_disconnect (tunnel);
584 if (tunnel_is_client (tunnel))
585 tunnel_out_connect (tunnel);
586 else
587 {
588 log_error ("tunnel_write_request: couldn't write request: "
589 "output is disconnected");
590 errno = EIO;
591 return -1;
592 }
593 /* return tunnel_write_request (tunnel, request, data, length); */
594 if (tunnel_write_data (tunnel, &request, sizeof request) == -1)
595 return -1;
596 }
597
598 if (data)
599 {
600 Length network_length = htons ((short)length);
601 if (tunnel_write_data (tunnel,
602 &network_length,
603 sizeof network_length) == -1)
604 return -1;
605
606 #ifdef DEBUG_MODE
607 if (request == TUNNEL_DATA && debug_level >= 5)
608 {
609 log_annoying ("tunnel_write_request: TUNNEL_DATA:");
610 dump_buf (debug_file, data, (size_t)length);
611 }
612 #endif
613
614 if (tunnel_write_data (tunnel, data, (size_t)length) == -1)
615 return -1;
616 }
617
618 if (data)
619 {
620 tunnel->out_total_raw += 3 + length;
621
622 if (request == TUNNEL_DATA)
623 log_verbose ("tunnel_write_request: %s (%d)",
624 REQ_TO_STRING (request), length);
625 else
626 log_debug ("tunnel_write_request: %s (%d)",
627 REQ_TO_STRING (request), length);
628 }
629 else
630 {
631 tunnel->out_total_raw += 1;
632 log_debug ("tunnel_write_request: %s", REQ_TO_STRING (request));
633 }
634
635 log_annoying ("tunnel_write_data: out_total_raw = %u",
636 tunnel->out_total_raw);
637
638 #ifdef DEBUG_MODE
639 if (tunnel->bytes > tunnel->content_length)
640 log_debug ("tunnel_write_request: tunnel->bytes > tunnel->content_length");
641 #endif
642
643 if (tunnel->bytes >= tunnel->content_length)
644 {
645 char c = TUNNEL_DISCONNECT;
646 tunnel_write_data (tunnel, &c, sizeof c);
647 tunnel_out_disconnect (tunnel);
648 #if 0
649 if (tunnel_is_server (tunnel))
650 tunnel_accept (tunnel);
651 #endif
652 }
653
654 return 0;
655 }
656
657 int
658 tunnel_connect (Tunnel *tunnel)
659 {
660 char auth_data[1] = { 42 }; /* dummy data, not used by server */
661
662 log_verbose ("tunnel_connect()");
663
664 if (tunnel_is_connected (tunnel))
665 {
666 log_error ("tunnel_connect: already connected");
667 errno = EINVAL;
668 return -1;
669 }
670
671 if (tunnel_write_request (tunnel, TUNNEL_OPEN,
672 auth_data, sizeof auth_data) == -1)
673 return -1;
674
675 if (tunnel_in_connect (tunnel) <= 0)
676 return -1;
677
678 return 0;
679 }
680
681 static inline int
682 tunnel_write_or_padding (Tunnel *tunnel, Request request, void *data,
683 size_t length)
684 {
685 static char padding[65536];
686 size_t n, remaining;
687 char *wdata = data;
688
689 for (remaining = length; remaining > 0; remaining -= n, wdata += n)
690 {
691 if (tunnel->bytes + remaining > tunnel->content_length - sizeof_header &&
692 tunnel->content_length - tunnel->bytes > sizeof_header)
693 n = tunnel->content_length - sizeof_header - tunnel->bytes;
694 else if (remaining > tunnel->content_length - sizeof_header)
695 n = tunnel->content_length - sizeof_header;
696 else
697 n = remaining;
698
699 if (n > 65535)
700 n = 65535;
701
702 if (request == TUNNEL_PADDING)
703 {
704 if (n + sizeof_header > remaining)
705 n = remaining - sizeof_header;
706 if (tunnel_write_request (tunnel, request, padding, n) == -1)
707 break;
708 n += sizeof_header;
709 }
710 else
711 {
712 if (tunnel_write_request (tunnel, request, wdata, n) == -1)
713 break;
714 }
715 }
716
717 return length - remaining;
718 }
719
720 ssize_t
721 tunnel_write (Tunnel *tunnel, void *data, size_t length)
722 {
723 ssize_t n;
724
725 n = tunnel_write_or_padding (tunnel, TUNNEL_DATA, data, length);
726 tunnel->out_total_data += length;
727 log_verbose ("tunnel_write: out_total_data = %u", tunnel->out_total_data);
728 return n;
729 }
730
731 ssize_t
732 tunnel_padding (Tunnel *tunnel, size_t length)
733 {
734 if (length < sizeof_header + 1)
735 {
736 int i;
737
738 for (i = 0; i < length; i++)
739 tunnel_write_request (tunnel, TUNNEL_PAD1, NULL, 0);
740 return length;
741 }
742
743 return tunnel_write_or_padding (tunnel, TUNNEL_PADDING, NULL, length);
744 }
745
746 int
747 tunnel_close (Tunnel *tunnel)
748 {
749 struct pollfd p;
750 char buf[10240];
751 ssize_t n;
752
753 if (tunnel->strict_content_length)
754 {
755 log_debug ("tunnel_close: write padding (%d bytes)",
756 tunnel->content_length - tunnel->bytes - 1);
757 tunnel_padding (tunnel, tunnel->content_length - tunnel->bytes - 1);
758 }
759
760 log_debug ("tunnel_close: write TUNNEL_CLOSE request");
761 tunnel_write_request (tunnel, TUNNEL_CLOSE, NULL, 0);
762
763 tunnel_out_disconnect (tunnel);
764
765 log_debug ("tunnel_close: reading trailing data from input ...");
766 p.fd = tunnel->in_fd;
767 p.events = POLLIN;
768 while (poll (&p, 1, READ_TRAIL_TIMEOUT) > 0)
769 {
770 if (p.revents & POLLIN)
771 {
772 n = read (tunnel->in_fd, buf, sizeof buf);
773 if (n > 0)
774 {
775 log_annoying ("read (%d, %p, %d) = %d",
776 tunnel->in_fd, buf, sizeof buf, n);
777 continue;
778 }
779 else if (n == -1 && errno == EAGAIN)
780 continue;
781 else if (n == -1)
782 log_debug ("tunnel_close: ... error: %s", strerror (errno));
783 else
784 log_debug ("tunnel_close: ... done (tunnel closed)");
785 }
786 if (p.revents & POLLHUP)
787 log_debug ("POLLHUP");
788 if (p.revents & POLLERR)
789 log_debug ("POLLERR");
790 if (p.revents & POLLNVAL)
791 log_debug ("POLLNVAL");
792 break;
793 }
794
795 tunnel_in_disconnect (tunnel);
796
797 tunnel->buf_len = 0;
798 tunnel->in_total_raw = 0;
799 tunnel->in_total_data = 0;
800 tunnel->out_total_raw = 0;
801 tunnel->out_total_data = 0;
802
803 return 0;
804 }
805
806 static int
807 tunnel_read_request (Tunnel *tunnel, enum tunnel_request *request,
808 unsigned char *buf, size_t *length)
809 {
810 Request req;
811 Length len;
812 ssize_t n;
813
814 log_annoying ("read (%d, %p, %d) ...", tunnel->in_fd, &req, 1);
815 n = read (tunnel->in_fd, &req, 1);
816 log_annoying ("... = %d", n);
817 if (n == -1)
818 {
819 if (errno != EAGAIN)
820 log_error ("tunnel_read_request: error reading request: %s",
821 strerror (errno));
822 return n;
823 }
824 else if (n == 0)
825 {
826 log_debug ("tunnel_read_request: connection closed by peer");
827 tunnel_in_disconnect (tunnel);
828
829 if (tunnel_is_client (tunnel)
830 && tunnel_in_connect (tunnel) == -1)
831 return -1;
832
833 errno = EAGAIN;
834 return -1;
835 }
836 *request = req;
837 tunnel->in_total_raw += n;
838 log_annoying ("request = 0x%x (%s)", req, REQ_TO_STRING (req));
839
840 if (req & TUNNEL_SIMPLE)
841 {
842 log_annoying ("tunnel_read_request: in_total_raw = %u",
843 tunnel->in_total_raw);
844 log_debug ("tunnel_read_request: %s", REQ_TO_STRING (req));
845 *length = 0;
846 return 1;
847 }
848
849 n = read_all (tunnel->in_fd, &len, 2);
850 if (n <= 0)
851 {
852 log_error ("tunnel_read_request: error reading request length: %s",
853 strerror (errno));
854 if (n == 0)
855 errno = EIO;
856 return -1;
857 }
858 len = ntohs (len);
859 *length = len;
860 tunnel->in_total_raw += n;
861 log_annoying ("length = %d", len);
862
863 if (len > 0)
864 {
865 n = read_all (tunnel->in_fd, buf, (size_t)len);
866 if (n <= 0)
867 {
868 log_error ("tunnel_read_request: error reading request data: %s",
869 strerror (errno));
870 if (n == 0)
871 errno = EIO;
872 return -1;
873 }
874 tunnel->in_total_raw += n;
875 log_annoying ("tunnel_read_request: in_total_raw = %u",
876 tunnel->in_total_raw);
877 }
878
879 if (req == TUNNEL_DATA)
880 log_verbose ("tunnel_read_request: %s (%d)",
881 REQ_TO_STRING (req), len);
882 else
883 log_debug ("tunnel_read_request: %s (%d)",
884 REQ_TO_STRING (req), len);
885
886 return 1;
887 }
888
889 ssize_t
890 tunnel_read (Tunnel *tunnel, void *data, size_t length)
891 {
892 enum tunnel_request req;
893 size_t len;
894 ssize_t n;
895
896 if (tunnel->buf_len > 0)
897 {
898 n = min (tunnel->buf_len, length);
899 memcpy (data, tunnel->buf_ptr, n);
900 tunnel->buf_ptr += n;
901 tunnel->buf_len -= n;
902 return n;
903 }
904
905 if (tunnel->in_fd == -1)
906 {
907 if (tunnel_is_client (tunnel))
908 {
909 if (tunnel_in_connect (tunnel) == -1)
910 return -1;
911 }
912 else
913 {
914 #if 1
915 if (tunnel_accept (tunnel) == -1)
916 return -1;
917 #else
918 errno = EAGAIN;
919 return -1;
920 #endif
921 }
922
923 errno = EAGAIN;
924 return -1;
925 }
926
927 if (tunnel->out_fd == -1 && tunnel_is_server (tunnel))
928 {
929 tunnel_accept (tunnel);
930 errno = EAGAIN;
931 return -1;
932 }
933
934 if (tunnel_read_request (tunnel, &req, tunnel->buf, &len) <= 0)
935 {
936 log_annoying ("tunnel_read_request returned <= 0, returning -1");
937 return -1;
938 }
939
940 switch (req)
941 {
942 case TUNNEL_OPEN:
943 /* do something with tunnel->buf */
944 break;
945
946 case TUNNEL_DATA:
947 tunnel->buf_ptr = tunnel->buf;
948 tunnel->buf_len = len;
949 tunnel->in_total_data += len;
950 log_verbose ("tunnel_read: in_total_data = %u", tunnel->in_total_data);
951 return tunnel_read (tunnel, data, length);
952
953 case TUNNEL_PADDING:
954 /* discard data */
955 break;
956
957 case TUNNEL_PAD1:
958 /* do nothing */
959 break;
960
961 case TUNNEL_ERROR:
962 tunnel->buf[len] = 0;
963 log_error ("tunnel_read: received error: %s", tunnel->buf);
964 errno = EIO;
965 return -1;
966
967 case TUNNEL_CLOSE:
968 return 0;
969
970 case TUNNEL_DISCONNECT:
971 tunnel_in_disconnect (tunnel);
972
973 if (tunnel_is_client (tunnel)
974 && tunnel_in_connect (tunnel) == -1)
975 return -1;
976
977 errno = EAGAIN;
978 return -1;
979
980 default:
981 log_error ("tunnel_read: protocol error: unknown request 0x%02x", req);
982 errno = EINVAL;
983 return -1;
984 }
985
986 errno = EAGAIN;
987 return -1;
988 }
989
990 int
991 tunnel_pollin_fd (Tunnel *tunnel)
992 {
993 if (tunnel_is_server (tunnel) &&
994 (tunnel->in_fd == -1 || tunnel->out_fd == -1))
995 {
996 if (tunnel->in_fd == -1)
997 log_verbose ("tunnel_pollin_fd: in_fd = -1; returning server_socket = %d",
998 tunnel->server_socket);
999 else
1000 log_verbose ("tunnel_pollin_fd: out_fd = -1; returning server_socket = %d",
1001 tunnel->server_socket);
1002 return tunnel->server_socket;
1003 }
1004 else if (tunnel->in_fd != -1)
1005 return tunnel->in_fd;
1006 else
1007 {
1008 log_error ("tunnel_pollin_fd: returning -1");
1009 return -1;
1010 }
1011 }
1012
1013 /*
1014 If the write connection is up and needs padding to the block length
1015 specified in the second argument, send some padding.
1016 */
1017
1018 int
1019 tunnel_maybe_pad (Tunnel *tunnel, size_t length)
1020 {
1021 size_t padding;
1022
1023 if (tunnel_is_disconnected (tunnel) ||
1024 tunnel->bytes % length == 0 ||
1025 tunnel->padding_only)
1026 return 0;
1027
1028 padding = length - tunnel->bytes % length;
1029 if (padding > tunnel->content_length - tunnel->bytes)
1030 padding = tunnel->content_length - tunnel->bytes;
1031
1032 return tunnel_padding (tunnel, padding);
1033 }
1034
1035 #if 0
1036 ssize_t
1037 old_parse_header (int s, int *type)
1038 {
1039 static const char *end_of_header = "\r\n\r\n";
1040 ssize_t n, len = 0;
1041 char c;
1042 int i;
1043
1044 *type = -1;
1045
1046 n = read_all (s, &c, 1);
1047 if (n != 1)
1048 return -1;
1049 len += n;
1050
1051 if (c == 'P')
1052 *type = TUNNEL_IN;
1053 else if (c == 'G')
1054 *type = TUNNEL_OUT;
1055 else
1056 {
1057 log_error ("parse_header: unknown HTTP request starting with '%c'", c);
1058 errno = EINVAL;
1059 return -1;
1060 }
1061
1062 i = 0;
1063 while (i < 4)
1064 {
1065 n = read_all (s, &c, 1);
1066 if (n != 1 && errno != EAGAIN)
1067 return n;
1068 len += n;
1069
1070 if (c == end_of_header[i])
1071 i++;
1072 else
1073 i = 0;
1074 }
1075
1076 return len;
1077 }
1078 #endif
1079
1080 int
1081 tunnel_accept (Tunnel *tunnel)
1082 {
1083 if (tunnel->in_fd != -1 && tunnel->out_fd != -1)
1084 {
1085 log_debug ("tunnel_accept: tunnel already established");
1086 return 0;
1087 }
1088
1089 while (tunnel->in_fd == -1 || tunnel->out_fd == -1)
1090 {
1091 struct sockaddr_in addr;
1092 Http_request *request;
1093 struct pollfd p;
1094 ssize_t m;
1095 int len;
1096 int n;
1097 int s;
1098
1099 p.fd = tunnel->server_socket;
1100 p.events = POLLIN;
1101 n = poll (&p, 1, (tunnel->in_fd != -1 || tunnel->out_fd != -1 ?
1102 ACCEPT_TIMEOUT * 1000 : -1));
1103 if (n == -1)
1104 {
1105 log_error ("tunnel_accept: poll error: %s", strerror (errno));
1106 return -1;
1107 }
1108 else if (n == 0)
1109 {
1110 log_error ("tunnel_accept: poll timed out");
1111 break;
1112 }
1113
1114 len = sizeof addr;
1115 s = accept (tunnel->server_socket, (struct sockaddr *)&addr, &len);
1116 if (s == -1)
1117 {
1118 log_error ("tunnel_accept: accept error: %s", strerror (errno));
1119 return -1;
1120 }
1121
1122 log_notice ("connection from %d.%d.%d.%d:%u",
1123 ntohl (addr.sin_addr.s_addr) >> 24,
1124 (ntohl (addr.sin_addr.s_addr) >> 16) & 0xff,
1125 (ntohl (addr.sin_addr.s_addr) >> 8) & 0xff,
1126 ntohl (addr.sin_addr.s_addr) & 0xff,
1127 ntohs (addr.sin_port));
1128
1129 m = http_parse_request (s, &request);
1130 if (m <= 0)
1131 return m;
1132
1133 if (request->method == -1)
1134 {
1135 log_error ("tunnel_accept: error parsing header: %s",
1136 strerror (errno));
1137 close (s);
1138 }
1139 else if (request->method == HTTP_POST ||
1140 request->method == HTTP_PUT)
1141 {
1142 if (tunnel->in_fd == -1)
1143 {
1144 tunnel->in_fd = s;
1145
1146 #ifdef IO_COUNT_HTTP_HEADER
1147 tunnel->in_total_raw += m; /* from parse_header() */
1148 log_annoying ("tunnel_accept: in_total_raw = %u",
1149 tunnel->in_total_raw);
1150 #endif
1151
1152 fcntl (tunnel->in_fd,
1153 F_SETFL,
1154 fcntl (tunnel->in_fd, F_GETFL) | O_NONBLOCK);
1155
1156 tunnel_in_setsockopts (tunnel->in_fd);
1157
1158 log_debug ("tunnel_accept: input connected");
1159 }
1160 else
1161 {
1162 log_error ("rejected tunnel_in: already got a connection");
1163 close (s);
1164 }
1165 }
1166 else if (request->method == HTTP_GET)
1167 {
1168 if (tunnel->out_fd == -1)
1169 {
1170 char str[1024];
1171
1172 tunnel->out_fd = s;
1173
1174 tunnel_out_setsockopts (tunnel->out_fd);
1175
1176 sprintf (str,
1177 "HTTP/1.1 200 OK\r\n"
1178 /* "Date: %s\r\n" */
1179 /* "Server: %s\r\n" */
1180 /* "Last-Modified: %s\r\n" */
1181 /* "ETag: %s\r\n" */
1182 /* "Accept-Ranges: %s\r\n" */
1183 "Content-Length: %d\r\n"
1184 "Connection: close\r\n"
1185 "Pragma: no-cache\r\n"
1186 "Cache-Control: no-cache, no-store, must-revalidate\r\n"
1187 "Expires: 0\r\n" /* FIXME: "0" is not a legitimate HTTP date. */
1188 "Content-Type: text/html\r\n"
1189 "\r\n",
1190 /* +1 to allow for TUNNEL_DISCONNECT */
1191 tunnel->content_length + 1);
1192 if (write_all (tunnel->out_fd, str, strlen (str)) <= 0)
1193 {
1194 log_error ("tunnel_accept: couldn't write GET header: %s",
1195 strerror (errno));
1196 close (tunnel->out_fd);
1197 tunnel->out_fd = -1;
1198 }
1199 else
1200 {
1201 tunnel->bytes = 0;
1202 tunnel->buf_len = 0;
1203 tunnel->buf_ptr = tunnel->buf;
1204 #ifdef IO_COUNT_HTTP_HEADER
1205 tunnel->out_total_raw += strlen (str);
1206 log_annoying ("tunnel_accept: out_total_raw = %u",
1207 tunnel->out_total_raw);
1208 #endif
1209 log_debug ("tunnel_accept: output connected");
1210 }
1211 }
1212 else
1213 {
1214 log_error ("tunnel_accept: rejected tunnel_out: "
1215 "already got a connection");
1216 close (s);
1217 }
1218 }
1219 else
1220 {
1221 log_error ("tunnel_accept: unknown header type");
1222 log_debug ("tunnel_accept: closing connection");
1223 close (s);
1224 }
1225
1226 http_destroy_request (request);
1227 }
1228
1229 if (tunnel->in_fd == -1 || tunnel->out_fd == -1)
1230 {
1231 log_error ("tunnel_accept: in_fd = %d, out_fd = %d",
1232 tunnel->in_fd, tunnel->out_fd);
1233
1234 if (tunnel->in_fd != -1)
1235 close (tunnel->in_fd);
1236 tunnel->in_fd = -1;
1237 log_debug ("tunnel_accept: input disconnected");
1238
1239 tunnel_out_disconnect (tunnel);
1240
1241 return -1;
1242 }
1243
1244 return 0;
1245 }
1246
1247 Tunnel *
1248 tunnel_new_server (const char *host, int port, size_t content_length)
1249 {
1250 Tunnel *tunnel;
1251 struct in_addr addr;
1252 struct hostent *hp;
1253
1254 if (host == NULL)
1255 addr.s_addr = INADDR_ANY;
1256 else if ((addr.s_addr = inet_addr (host)) == INADDR_NONE)
1257 {
1258 hp = gethostbyname (host);
1259 if (hp == NULL || hp->h_addrtype != AF_INET)
1260 return NULL;
1261 memcpy (&addr, hp->h_addr, hp->h_length);
1262 }
1263
1264 tunnel = malloc (sizeof (Tunnel));
1265 if (tunnel == NULL)
1266 return NULL;
1267
1268 /* If content_length is 0, a value must be determined automatically. */
1269 /* For now, a default value will do. */
1270 if (content_length == 0)
1271 content_length = DEFAULT_CONTENT_LENGTH;
1272
1273 tunnel->in_fd = -1;
1274 tunnel->out_fd = -1;
1275 tunnel->server_socket = -1;
1276 tunnel->dest.host_name = host;
1277 tunnel->dest.host_port = port;
1278 tunnel->buf_ptr = tunnel->buf;
1279 tunnel->buf_len = 0;
1280 /* -1 to allow for TUNNEL_DISCONNECT */
1281 tunnel->content_length = content_length - 1;
1282 tunnel->in_total_raw = 0;
1283 tunnel->in_total_data = 0;
1284 tunnel->out_total_raw = 0;
1285 tunnel->out_total_data = 0;
1286 tunnel->strict_content_length = FALSE;
1287 tunnel->bytes = 0;
1288
1289 tunnel->server_socket = server_socket (addr, tunnel->dest.host_port, 1);
1290 if (tunnel->server_socket == -1)
1291 {
1292 log_error ("tunnel_new_server: server_socket (%d) = -1",
1293 tunnel->dest.host_port);
1294 tunnel_destroy (tunnel);
1295 return NULL;
1296 }
1297
1298 return tunnel;
1299 }
1300
1301 Tunnel *
1302 tunnel_new_client (const char *host, int host_port,
1303 const char *proxy, int proxy_port,
1304 size_t content_length)
1305 {
1306 const char *remote;
1307 int remote_port;
1308 Tunnel *tunnel;
1309
1310 log_verbose ("tunnel_new_client (\"%s\", %d, \"%s\", %d, %d)",
1311 host, host_port, proxy ? proxy : "(null)", proxy_port,
1312 content_length);
1313
1314 tunnel = malloc (sizeof (Tunnel));
1315 if (tunnel == NULL)
1316 {
1317 log_error ("tunnel_new_client: out of memory");
1318 return NULL;
1319 }
1320
1321 tunnel->in_fd = -1;
1322 tunnel->out_fd = -1;
1323 tunnel->server_socket = -1;
1324 tunnel->dest.host_name = host;
1325 tunnel->dest.host_port = host_port;
1326 tunnel->dest.proxy_name = proxy;
1327 tunnel->dest.proxy_port = proxy_port;
1328 tunnel->dest.proxy_authorization = NULL;
1329 tunnel->dest.user_agent = NULL;
1330 /* -1 to allow for TUNNEL_DISCONNECT */
1331 tunnel->content_length = content_length - 1;
1332 tunnel->buf_ptr = tunnel->buf;
1333 tunnel->buf_len = 0;
1334 tunnel->in_total_raw = 0;
1335 tunnel->in_total_data = 0;
1336 tunnel->out_total_raw = 0;
1337 tunnel->out_total_data = 0;
1338 tunnel->strict_content_length = FALSE;
1339 tunnel->bytes = 0;
1340
1341 if (tunnel->dest.proxy_name == NULL)
1342 {
1343 remote = tunnel->dest.host_name;
1344 remote_port = tunnel->dest.host_port;
1345 }
1346 else
1347 {
1348 remote = tunnel->dest.proxy_name;
1349 remote_port = tunnel->dest.proxy_port;
1350 }
1351
1352 if (set_address (&tunnel->address, remote, remote_port) == -1)
1353 {
1354 log_error ("tunnel_new_client: set_address: %s", strerror (errno));
1355 free (tunnel);
1356 return NULL;
1357 }
1358
1359 return tunnel;
1360 }
1361
1362 void
1363 tunnel_destroy (Tunnel *tunnel)
1364 {
1365 if (tunnel_is_connected (tunnel) || tunnel->in_fd != -1)
1366 tunnel_close (tunnel);
1367
1368 if (tunnel->server_socket != -1)
1369 close (tunnel->server_socket);
1370
1371 free (tunnel);
1372 }
1373
1374 static int
1375 tunnel_opt (Tunnel *tunnel, const char *opt, void *data, int get_flag)
1376 {
1377 if (strcmp (opt, "strict_content_length") == 0)
1378 {
1379 if (get_flag)
1380 *(int *)data = tunnel->strict_content_length;
1381 else
1382 tunnel->strict_content_length = *(int *)data;
1383 }
1384 else if (strcmp (opt, "keep_alive") == 0)
1385 {
1386 if (get_flag)
1387 *(int *)data = tunnel->keep_alive;
1388 else
1389 tunnel->keep_alive = *(int *)data;
1390 }
1391 else if (strcmp (opt, "max_connection_age") == 0)
1392 {
1393 if (get_flag)
1394 *(int *)data = tunnel->max_connection_age;
1395 else
1396 tunnel->max_connection_age = *(int *)data;
1397 }
1398 else if (strcmp (opt, "proxy_authorization") == 0)
1399 {
1400 if (get_flag)
1401 {
1402 if (tunnel->dest.proxy_authorization == NULL)
1403 *(char **)data = NULL;
1404 else
1405 *(char **)data = strdup (tunnel->dest.proxy_authorization);
1406 }
1407 else
1408 {
1409 if (tunnel->dest.proxy_authorization != NULL)
1410 free ((char *)tunnel->dest.proxy_authorization);
1411 tunnel->dest.proxy_authorization = strdup ((char *)data);
1412 if (tunnel->dest.proxy_authorization == NULL)
1413 return -1;
1414 }
1415 }
1416 else if (strcmp (opt, "user_agent") == 0)
1417 {
1418 if (get_flag)
1419 {
1420 if (tunnel->dest.user_agent == NULL)
1421 *(char **)data = NULL;
1422 else
1423 *(char **)data = strdup (tunnel->dest.user_agent);
1424 }
1425 else
1426 {
1427 if (tunnel->dest.user_agent != NULL)
1428 free ((char *)tunnel->dest.user_agent);
1429 tunnel->dest.user_agent = strdup ((char *)data);
1430 if (tunnel->dest.user_agent == NULL)
1431 return -1;
1432 }
1433 }
1434 else
1435 {
1436 errno = EINVAL;
1437 return -1;
1438 }
1439
1440 return 0;
1441 }
1442
1443 int
1444 tunnel_setopt (Tunnel *tunnel, const char *opt, void *data)
1445 {
1446 return tunnel_opt (tunnel, opt, data, FALSE);
1447 }
1448
1449 int
1450 tunnel_getopt (Tunnel *tunnel, const char *opt, void *data)
1451 {
1452 return tunnel_opt (tunnel, opt, data, TRUE);
1453 }