diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-04-01 10:59:33 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-04-01 10:59:33 +0000 |
commit | 57a426b15becb6b91850a55986244e53b8b22773 (patch) | |
tree | cffc17f98425a0db20f277706df84b7c37ecde34 | |
parent | ba2fb719b92034e745b59d2c3b5b05209ce45352 (diff) | |
download | busybox-w32-57a426b15becb6b91850a55986244e53b8b22773.tar.gz busybox-w32-57a426b15becb6b91850a55986244e53b8b22773.tar.bz2 busybox-w32-57a426b15becb6b91850a55986244e53b8b22773.zip |
tcpsvd: fix line buffering, add firewall query code
-rw-r--r-- | ipsvd/ipsvd_perhost.c | 9 | ||||
-rw-r--r-- | ipsvd/ipsvd_perhost.h | 9 | ||||
-rw-r--r-- | ipsvd/tcpsvd.c | 42 |
3 files changed, 52 insertions, 8 deletions
diff --git a/ipsvd/ipsvd_perhost.c b/ipsvd/ipsvd_perhost.c index c6f7de339..1c5c12af5 100644 --- a/ipsvd/ipsvd_perhost.c +++ b/ipsvd/ipsvd_perhost.c | |||
@@ -1,3 +1,12 @@ | |||
1 | /* Based on ipsvd utilities written by Gerrit Pape <pape@smarden.org> | ||
2 | * which are released into public domain by the author. | ||
3 | * Homepage: http://smarden.sunsite.dk/ipsvd/ | ||
4 | * | ||
5 | * Copyright (C) 2007 Denis Vlasenko. | ||
6 | * | ||
7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. | ||
8 | */ | ||
9 | |||
1 | #include "busybox.h" | 10 | #include "busybox.h" |
2 | #include "ipsvd_perhost.h" | 11 | #include "ipsvd_perhost.h" |
3 | 12 | ||
diff --git a/ipsvd/ipsvd_perhost.h b/ipsvd/ipsvd_perhost.h index 05c939d89..26b4063ea 100644 --- a/ipsvd/ipsvd_perhost.h +++ b/ipsvd/ipsvd_perhost.h | |||
@@ -1,3 +1,12 @@ | |||
1 | /* Based on ipsvd utilities written by Gerrit Pape <pape@smarden.org> | ||
2 | * which are released into public domain by the author. | ||
3 | * Homepage: http://smarden.sunsite.dk/ipsvd/ | ||
4 | * | ||
5 | * Copyright (C) 2007 Denis Vlasenko. | ||
6 | * | ||
7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. | ||
8 | */ | ||
9 | |||
1 | struct hcc { | 10 | struct hcc { |
2 | char ip[32 - sizeof(int)]; | 11 | char ip[32 - sizeof(int)]; |
3 | int pid; | 12 | int pid; |
diff --git a/ipsvd/tcpsvd.c b/ipsvd/tcpsvd.c index df9821652..2a3cd3b01 100644 --- a/ipsvd/tcpsvd.c +++ b/ipsvd/tcpsvd.c | |||
@@ -2,7 +2,7 @@ | |||
2 | * which are released into public domain by the author. | 2 | * which are released into public domain by the author. |
3 | * Homepage: http://smarden.sunsite.dk/ipsvd/ | 3 | * Homepage: http://smarden.sunsite.dk/ipsvd/ |
4 | * | 4 | * |
5 | * Copyright (C) 2007 by Denis Vlasenko. | 5 | * Copyright (C) 2007 Denis Vlasenko. |
6 | * | 6 | * |
7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. | 7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
8 | */ | 8 | */ |
@@ -14,8 +14,17 @@ | |||
14 | * Code inside "#ifdef SSLSVD" is for sslsvd and is currently unused. | 14 | * Code inside "#ifdef SSLSVD" is for sslsvd and is currently unused. |
15 | * Code inside #if 0" is parts of original tcpsvd which are not implemented | 15 | * Code inside #if 0" is parts of original tcpsvd which are not implemented |
16 | * for busyboxed version. | 16 | * for busyboxed version. |
17 | * | ||
18 | * Output of verbose mode matches original (modulo bugs and | ||
19 | * unimplemented stuff). Unnatural splitting of IP and PORT | ||
20 | * is retained (personally I prefer one-value "IP:PORT" notation - | ||
21 | * it is a natural string representation of struct sockaddr_XX). | ||
22 | * | ||
23 | * TCPORIGDST{IP,PORT} is busybox-specific addition | ||
17 | */ | 24 | */ |
18 | 25 | ||
26 | #include <limits.h> | ||
27 | #include <linux/netfilter_ipv4.h> /* wants <limits.h> */ | ||
19 | #include "busybox.h" | 28 | #include "busybox.h" |
20 | #include "ipsvd_perhost.h" | 29 | #include "ipsvd_perhost.h" |
21 | 30 | ||
@@ -46,7 +55,7 @@ enum { | |||
46 | OPT_t = (1 << 10), | 55 | OPT_t = (1 << 10), |
47 | OPT_v = (1 << 11), | 56 | OPT_v = (1 << 11), |
48 | OPT_V = (1 << 12), | 57 | OPT_V = (1 << 12), |
49 | OPT_U = (1 << 13), | 58 | OPT_U = (1 << 13), /* from here: sslsvd only */ |
50 | OPT_slash = (1 << 14), | 59 | OPT_slash = (1 << 14), |
51 | OPT_Z = (1 << 15), | 60 | OPT_Z = (1 << 15), |
52 | OPT_K = (1 << 16), | 61 | OPT_K = (1 << 16), |
@@ -104,7 +113,7 @@ int tcpsvd_main(int argc, char **argv) | |||
104 | const char *instructs; | 113 | const char *instructs; |
105 | char *msg_per_host = NULL; | 114 | char *msg_per_host = NULL; |
106 | unsigned len_per_host = len_per_host; /* gcc */ | 115 | unsigned len_per_host = len_per_host; /* gcc */ |
107 | int need_addresses; | 116 | int need_hostnames, need_remote_ip; |
108 | int pid; | 117 | int pid; |
109 | int sock; | 118 | int sock; |
110 | int conn; | 119 | int conn; |
@@ -171,7 +180,9 @@ int tcpsvd_main(int argc, char **argv) | |||
171 | if (!argv[0][0] || LONE_CHAR(argv[0], '0')) | 180 | if (!argv[0][0] || LONE_CHAR(argv[0], '0')) |
172 | argv[0] = (char*)"0.0.0.0"; | 181 | argv[0] = (char*)"0.0.0.0"; |
173 | 182 | ||
174 | need_addresses = verbose || !(option_mask32 & OPT_E); | 183 | setlinebuf(stdout); |
184 | need_hostnames = verbose || !(option_mask32 & OPT_E); | ||
185 | need_remote_ip = max_per_host || need_hostnames; | ||
175 | 186 | ||
176 | #ifdef SSLSVD | 187 | #ifdef SSLSVD |
177 | sslser = user; | 188 | sslser = user; |
@@ -299,7 +310,7 @@ int tcpsvd_main(int argc, char **argv) | |||
299 | 310 | ||
300 | close(sock); | 311 | close(sock); |
301 | 312 | ||
302 | if (!max_per_host) | 313 | if (!max_per_host && need_remote_ip) |
303 | remote_ip = xmalloc_sockaddr2dotted_noport(&sock_adr.sa, sizeof(sock_adr)); | 314 | remote_ip = xmalloc_sockaddr2dotted_noport(&sock_adr.sa, sizeof(sock_adr)); |
304 | /* else it is already done */ | 315 | /* else it is already done */ |
305 | 316 | ||
@@ -311,7 +322,7 @@ int tcpsvd_main(int argc, char **argv) | |||
311 | printf("%s: info: pid %d from %s\n", applet_name, pid, remote_ip); | 322 | printf("%s: info: pid %d from %s\n", applet_name, pid, remote_ip); |
312 | } | 323 | } |
313 | 324 | ||
314 | if (need_addresses && (option_mask32 & OPT_h)) { | 325 | if (need_hostnames && (option_mask32 & OPT_h)) { |
315 | remote_hostname = xmalloc_sockaddr2host(&sock_adr.sa, sizeof(sock_adr)); | 326 | remote_hostname = xmalloc_sockaddr2host(&sock_adr.sa, sizeof(sock_adr)); |
316 | if (!remote_hostname) { | 327 | if (!remote_hostname) { |
317 | bb_error_msg("warning: cannot look up hostname for %s", remote_ip); | 328 | bb_error_msg("warning: cannot look up hostname for %s", remote_ip); |
@@ -324,7 +335,7 @@ int tcpsvd_main(int argc, char **argv) | |||
324 | * which doesn't know local ip) */ | 335 | * which doesn't know local ip) */ |
325 | getsockname(conn, &sock_adr.sa, &sockadr_size); | 336 | getsockname(conn, &sock_adr.sa, &sockadr_size); |
326 | 337 | ||
327 | if (need_addresses) { | 338 | if (need_hostnames) { |
328 | local_ip = xmalloc_sockaddr2dotted_noport(&sock_adr.sa, sockadr_size); | 339 | local_ip = xmalloc_sockaddr2dotted_noport(&sock_adr.sa, sockadr_size); |
329 | local_port = get_nport(&sock_adr.sa); | 340 | local_port = get_nport(&sock_adr.sa); |
330 | local_port = ntohs(local_port); | 341 | local_port = ntohs(local_port); |
@@ -337,6 +348,20 @@ int tcpsvd_main(int argc, char **argv) | |||
337 | 348 | ||
338 | if (!(option_mask32 & OPT_E)) { | 349 | if (!(option_mask32 & OPT_E)) { |
339 | /* setup ucspi env */ | 350 | /* setup ucspi env */ |
351 | |||
352 | /* Extract "original" destination addr:port | ||
353 | * from Linux firewall. Useful when you redirect | ||
354 | * an outbond connection to local handler, and it needs | ||
355 | * to know where it originally tried to connect */ | ||
356 | sockadr_size = sizeof(sock_adr); | ||
357 | if (getsockopt(conn, SOL_IP, SO_ORIGINAL_DST, &sock_adr.sa, &sockadr_size) == 0) { | ||
358 | char *ip = xmalloc_sockaddr2dotted_noport(&sock_adr.sa, sockadr_size); | ||
359 | port = get_nport(&sock_adr.sa); | ||
360 | port = ntohs(port); | ||
361 | xsetenv("TCPORIGDSTIP", ip); | ||
362 | xsetenv("TCPORIGDSTPORT", utoa(port)); | ||
363 | free(ip); | ||
364 | } | ||
340 | xsetenv("PROTO", "TCP"); | 365 | xsetenv("PROTO", "TCP"); |
341 | xsetenv("TCPLOCALIP", local_ip); | 366 | xsetenv("TCPLOCALIP", local_ip); |
342 | xsetenv("TCPLOCALPORT", utoa(local_port)); | 367 | xsetenv("TCPLOCALPORT", utoa(local_port)); |
@@ -431,7 +456,8 @@ int tcpsvd_main(int argc, char **argv) | |||
431 | } | 456 | } |
432 | 457 | ||
433 | /* | 458 | /* |
434 | tcpsvd [-hpEvv] [-c n] [-C n:msg] [-b n] [-u user] [-l name] [-i dir|-x cdb] [ -t sec] host port prog | 459 | tcpsvd [-hpEvv] [-c n] [-C n:msg] [-b n] [-u user] [-l name] |
460 | [-i dir|-x cdb] [ -t sec] host port prog | ||
435 | 461 | ||
436 | tcpsvd creates a TCP/IP socket, binds it to the address host:port, | 462 | tcpsvd creates a TCP/IP socket, binds it to the address host:port, |
437 | and listens on the socket for incoming connections. | 463 | and listens on the socket for incoming connections. |