Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 1 | /* Based on netcat 1.10 RELEASE 960320 written by hobbit@avian.org. |
| 2 | * Released into public domain by the author. |
| 3 | * |
Denis Vlasenko | 7cff01e | 2008-02-02 16:23:43 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007 Denys Vlasenko. |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 5 | * |
| 6 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
| 7 | */ |
| 8 | |
| 9 | /* Author's comments from nc 1.10: |
| 10 | * ===================== |
| 11 | * Netcat is entirely my own creation, although plenty of other code was used as |
| 12 | * examples. It is freely given away to the Internet community in the hope that |
| 13 | * it will be useful, with no restrictions except giving credit where it is due. |
| 14 | * No GPLs, Berkeley copyrights or any of that nonsense. The author assumes NO |
| 15 | * responsibility for how anyone uses it. If netcat makes you rich somehow and |
| 16 | * you're feeling generous, mail me a check. If you are affiliated in any way |
| 17 | * with Microsoft Network, get a life. Always ski in control. Comments, |
| 18 | * questions, and patches to hobbit@avian.org. |
| 19 | * ... |
| 20 | * Netcat and the associated package is a product of Avian Research, and is freely |
| 21 | * available in full source form with no restrictions save an obligation to give |
| 22 | * credit where due. |
| 23 | * ... |
| 24 | * A damn useful little "backend" utility begun 950915 or thereabouts, |
| 25 | * as *Hobbit*'s first real stab at some sockets programming. Something that |
| 26 | * should have and indeed may have existed ten years ago, but never became a |
| 27 | * standard Unix utility. IMHO, "nc" could take its place right next to cat, |
| 28 | * cp, rm, mv, dd, ls, and all those other cryptic and Unix-like things. |
| 29 | * ===================== |
| 30 | * |
| 31 | * Much of author's comments are still retained in the code. |
| 32 | * |
| 33 | * Functionality removed (rationale): |
| 34 | * - miltiple-port ranges, randomized port scanning (use nmap) |
| 35 | * - telnet support (use telnet) |
| 36 | * - source routing |
| 37 | * - multiple DNS checks |
| 38 | * Functionalty which is different from nc 1.10: |
Denys Vlasenko | 77cc2c5 | 2010-06-27 04:22:02 +0200 | [diff] [blame] | 39 | * - Prog in '-e PROG' can have prog's parameters and options. |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 40 | * Because of this -e option must be last. |
Denys Vlasenko | 77cc2c5 | 2010-06-27 04:22:02 +0200 | [diff] [blame] | 41 | * - nc doesn't redirect stderr to the network socket for the -e PROG. |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 42 | * - numeric addresses are printed in (), not [] (IPv6 looks better), |
| 43 | * port numbers are inside (): (1.2.3.4:5678) |
| 44 | * - network read errors are reported on verbose levels > 1 |
| 45 | * (nc 1.10 treats them as EOF) |
| 46 | * - TCP connects from wrong ip/ports (if peer ip:port is specified |
| 47 | * on the command line, but accept() says that it came from different addr) |
| 48 | * are closed, but nc doesn't exit - continues to listen/accept. |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 49 | */ |
| 50 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 51 | /* done in nc.c: #include "libbb.h" */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 52 | |
Denys Vlasenko | 77cc2c5 | 2010-06-27 04:22:02 +0200 | [diff] [blame] | 53 | //usage:#if ENABLE_NC_110_COMPAT |
| 54 | //usage: |
| 55 | //usage:#define nc_trivial_usage |
| 56 | //usage: "[OPTIONS] HOST PORT - connect" |
| 57 | //usage: IF_NC_SERVER("\n" |
| 58 | //usage: "nc [OPTIONS] -l -p PORT [HOST] [PORT] - listen") |
| 59 | //usage:#define nc_full_usage "\n\n" |
| 60 | //usage: "Options:" |
| 61 | //usage: "\n -e PROG Run PROG after connect (must be last)" |
| 62 | //usage: IF_NC_SERVER( |
| 63 | //usage: "\n -l Listen mode, for inbound connects" |
| 64 | //usage: ) |
| 65 | //usage: "\n -p PORT Local port" |
| 66 | //usage: "\n -s ADDR Local address" |
| 67 | //usage: "\n -w SEC Timeout for connects and final net reads" |
| 68 | //usage: IF_NC_EXTRA( |
| 69 | //usage: "\n -i SEC Delay interval for lines sent" /* ", ports scanned" */ |
| 70 | //usage: ) |
| 71 | //usage: "\n -n Don't do DNS resolution" |
| 72 | //usage: "\n -u UDP mode" |
| 73 | //usage: "\n -v Verbose" |
| 74 | //usage: IF_NC_EXTRA( |
| 75 | //usage: "\n -o FILE Hex dump traffic" |
| 76 | //usage: "\n -z Zero-I/O mode (scanning)" |
| 77 | //usage: ) |
| 78 | //usage:#endif |
| 79 | |
| 80 | /* "\n -r Randomize local and remote ports" */ |
| 81 | /* "\n -g gateway Source-routing hop point[s], up to 8" */ |
| 82 | /* "\n -G num Source-routing pointer: 4, 8, 12, ..." */ |
| 83 | /* "\nport numbers can be individual or ranges: lo-hi [inclusive]" */ |
| 84 | |
| 85 | /* -e PROG can take ARGS too: "nc ... -e ls -l", but we don't document it |
| 86 | * in help text: nc 1.10 does not allow that. We don't want to entice |
| 87 | * users to use this incompatibility */ |
| 88 | |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 89 | enum { |
| 90 | SLEAZE_PORT = 31337, /* for UDP-scan RTT trick, change if ya want */ |
| 91 | BIGSIZ = 8192, /* big buffers */ |
| 92 | |
| 93 | netfd = 3, |
| 94 | ofd = 4, |
| 95 | }; |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 96 | |
| 97 | struct globals { |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 98 | /* global cmd flags: */ |
| 99 | unsigned o_verbose; |
| 100 | unsigned o_wait; |
| 101 | #if ENABLE_NC_EXTRA |
| 102 | unsigned o_interval; |
| 103 | #endif |
| 104 | |
| 105 | /*int netfd;*/ |
| 106 | /*int ofd;*/ /* hexdump output fd */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 107 | #if ENABLE_LFS |
| 108 | #define SENT_N_RECV_M "sent %llu, rcvd %llu\n" |
| 109 | unsigned long long wrote_out; /* total stdout bytes */ |
| 110 | unsigned long long wrote_net; /* total net bytes */ |
| 111 | #else |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 112 | #define SENT_N_RECV_M "sent %u, rcvd %u\n" |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 113 | unsigned wrote_out; /* total stdout bytes */ |
| 114 | unsigned wrote_net; /* total net bytes */ |
| 115 | #endif |
Bernhard Reutner-Fischer | a985d30 | 2008-02-11 11:44:38 +0000 | [diff] [blame] | 116 | /* ouraddr is never NULL and goes through three states as we progress: |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 117 | 1 - local address before bind (IP/port possibly zero) |
| 118 | 2 - local address after bind (port is nonzero) |
| 119 | 3 - local address after connect??/recv/accept (IP and port are nonzero) */ |
| 120 | struct len_and_sockaddr *ouraddr; |
| 121 | /* themaddr is NULL if no peer hostname[:port] specified on command line */ |
| 122 | struct len_and_sockaddr *themaddr; |
| 123 | /* remend is set after connect/recv/accept to the actual ip:port of peer */ |
| 124 | struct len_and_sockaddr remend; |
| 125 | |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 126 | jmp_buf jbuf; /* timer crud */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 127 | |
| 128 | /* will malloc up the following globals: */ |
| 129 | fd_set ding1; /* for select loop */ |
| 130 | fd_set ding2; |
| 131 | char bigbuf_in[BIGSIZ]; /* data buffers */ |
| 132 | char bigbuf_net[BIGSIZ]; |
| 133 | }; |
| 134 | |
| 135 | #define G (*ptr_to_globals) |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 136 | #define wrote_out (G.wrote_out ) |
| 137 | #define wrote_net (G.wrote_net ) |
| 138 | #define ouraddr (G.ouraddr ) |
| 139 | #define themaddr (G.themaddr ) |
| 140 | #define remend (G.remend ) |
| 141 | #define jbuf (G.jbuf ) |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 142 | #define ding1 (G.ding1 ) |
| 143 | #define ding2 (G.ding2 ) |
| 144 | #define bigbuf_in (G.bigbuf_in ) |
| 145 | #define bigbuf_net (G.bigbuf_net) |
| 146 | #define o_verbose (G.o_verbose ) |
| 147 | #define o_wait (G.o_wait ) |
| 148 | #if ENABLE_NC_EXTRA |
| 149 | #define o_interval (G.o_interval) |
| 150 | #else |
| 151 | #define o_interval 0 |
| 152 | #endif |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 153 | #define INIT_G() do { \ |
| 154 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 155 | } while (0) |
| 156 | |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 157 | |
| 158 | /* Must match getopt32 call! */ |
| 159 | enum { |
| 160 | OPT_h = (1 << 0), |
| 161 | OPT_n = (1 << 1), |
| 162 | OPT_p = (1 << 2), |
| 163 | OPT_s = (1 << 3), |
| 164 | OPT_u = (1 << 4), |
| 165 | OPT_v = (1 << 5), |
| 166 | OPT_w = (1 << 6), |
| 167 | OPT_l = (1 << 7) * ENABLE_NC_SERVER, |
| 168 | OPT_i = (1 << (7+ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA, |
| 169 | OPT_o = (1 << (8+ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA, |
| 170 | OPT_z = (1 << (9+ENABLE_NC_SERVER)) * ENABLE_NC_EXTRA, |
| 171 | }; |
| 172 | |
| 173 | #define o_nflag (option_mask32 & OPT_n) |
| 174 | #define o_udpmode (option_mask32 & OPT_u) |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 175 | #if ENABLE_NC_SERVER |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 176 | #define o_listen (option_mask32 & OPT_l) |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 177 | #else |
| 178 | #define o_listen 0 |
| 179 | #endif |
| 180 | #if ENABLE_NC_EXTRA |
| 181 | #define o_ofile (option_mask32 & OPT_o) |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 182 | #define o_zero (option_mask32 & OPT_z) |
| 183 | #else |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 184 | #define o_ofile 0 |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 185 | #define o_zero 0 |
| 186 | #endif |
| 187 | |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 188 | /* Debug: squirt whatever message and sleep a bit so we can see it go by. */ |
| 189 | /* Beware: writes to stdOUT... */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 190 | #if 0 |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 191 | #define Debug(...) do { printf(__VA_ARGS__); printf("\n"); fflush_all(); sleep(1); } while (0) |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 192 | #else |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 193 | #define Debug(...) do { } while (0) |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 194 | #endif |
| 195 | |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 196 | #define holler_error(...) do { if (o_verbose) bb_error_msg(__VA_ARGS__); } while (0) |
| 197 | #define holler_perror(...) do { if (o_verbose) bb_perror_msg(__VA_ARGS__); } while (0) |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 198 | |
| 199 | /* catch: no-brainer interrupt handler */ |
| 200 | static void catch(int sig) |
| 201 | { |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 202 | if (o_verbose > 1) /* normally we don't care */ |
| 203 | fprintf(stderr, SENT_N_RECV_M, wrote_net, wrote_out); |
| 204 | fprintf(stderr, "punt!\n"); |
Denis Vlasenko | 400d8bb | 2008-02-24 13:36:01 +0000 | [diff] [blame] | 205 | kill_myself_with_sig(sig); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 208 | /* unarm */ |
| 209 | static void unarm(void) |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 210 | { |
| 211 | signal(SIGALRM, SIG_IGN); |
| 212 | alarm(0); |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* timeout and other signal handling cruft */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 216 | static void tmtravel(int sig UNUSED_PARAM) |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 217 | { |
| 218 | unarm(); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 219 | longjmp(jbuf, 1); |
| 220 | } |
| 221 | |
| 222 | /* arm: set the timer. */ |
| 223 | static void arm(unsigned secs) |
| 224 | { |
| 225 | signal(SIGALRM, tmtravel); |
| 226 | alarm(secs); |
| 227 | } |
| 228 | |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 229 | /* findline: |
| 230 | find the next newline in a buffer; return inclusive size of that "line", |
| 231 | or the entire buffer size, so the caller knows how much to then write(). |
| 232 | Not distinguishing \n vs \r\n for the nonce; it just works as is... */ |
| 233 | static unsigned findline(char *buf, unsigned siz) |
| 234 | { |
| 235 | char * p; |
| 236 | int x; |
| 237 | if (!buf) /* various sanity checks... */ |
| 238 | return 0; |
| 239 | if (siz > BIGSIZ) |
| 240 | return 0; |
| 241 | x = siz; |
| 242 | for (p = buf; x > 0; x--) { |
| 243 | if (*p == '\n') { |
| 244 | x = (int) (p - buf); |
| 245 | x++; /* 'sokay if it points just past the end! */ |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 246 | Debug("findline returning %d", x); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 247 | return x; |
| 248 | } |
| 249 | p++; |
| 250 | } /* for */ |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 251 | Debug("findline returning whole thing: %d", siz); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 252 | return siz; |
| 253 | } /* findline */ |
| 254 | |
| 255 | /* doexec: |
| 256 | fiddle all the file descriptors around, and hand off to another prog. Sort |
| 257 | of like a one-off "poor man's inetd". This is the only section of code |
| 258 | that would be security-critical, which is why it's ifdefed out by default. |
| 259 | Use at your own hairy risk; if you leave shells lying around behind open |
| 260 | listening ports you deserve to lose!! */ |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 261 | static int doexec(char **proggie) NORETURN; |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 262 | static int doexec(char **proggie) |
| 263 | { |
| 264 | xmove_fd(netfd, 0); |
| 265 | dup2(0, 1); |
| 266 | /* dup2(0, 2); - do we *really* want this? NO! |
| 267 | * exec'ed prog can do it yourself, if needed */ |
| 268 | execvp(proggie[0], proggie); |
Denys Vlasenko | 41ddd9f | 2010-06-25 01:46:53 +0200 | [diff] [blame] | 269 | bb_perror_msg_and_die("can't execute '%s'", proggie[0]); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | /* connect_w_timeout: |
| 273 | return an fd for one of |
| 274 | an open outbound TCP connection, a UDP stub-socket thingie, or |
| 275 | an unconnected TCP or UDP socket to listen on. |
| 276 | Examines various global o_blah flags to figure out what to do. |
| 277 | lad can be NULL, then socket is not bound to any local ip[:port] */ |
| 278 | static int connect_w_timeout(int fd) |
| 279 | { |
| 280 | int rr; |
| 281 | |
| 282 | /* wrap connect inside a timer, and hit it */ |
| 283 | arm(o_wait); |
| 284 | if (setjmp(jbuf) == 0) { |
Denis Vlasenko | 7cff01e | 2008-02-02 16:23:43 +0000 | [diff] [blame] | 285 | rr = connect(fd, &themaddr->u.sa, themaddr->len); |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 286 | unarm(); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 287 | } else { /* setjmp: connect failed... */ |
| 288 | rr = -1; |
| 289 | errno = ETIMEDOUT; /* fake it */ |
| 290 | } |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 291 | return rr; |
| 292 | } |
| 293 | |
| 294 | /* dolisten: |
| 295 | listens for |
| 296 | incoming and returns an open connection *from* someplace. If we were |
| 297 | given host/port args, any connections from elsewhere are rejected. This |
| 298 | in conjunction with local-address binding should limit things nicely... */ |
| 299 | static void dolisten(void) |
| 300 | { |
| 301 | int rr; |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 302 | |
| 303 | if (!o_udpmode) |
| 304 | xlisten(netfd, 1); /* TCP: gotta listen() before we can get */ |
| 305 | |
| 306 | /* Various things that follow temporarily trash bigbuf_net, which might contain |
| 307 | a copy of any recvfrom()ed packet, but we'll read() another copy later. */ |
| 308 | |
| 309 | /* I can't believe I have to do all this to get my own goddamn bound address |
| 310 | and port number. It should just get filled in during bind() or something. |
| 311 | All this is only useful if we didn't say -p for listening, since if we |
| 312 | said -p we *know* what port we're listening on. At any rate we won't bother |
| 313 | with it all unless we wanted to see it, although listening quietly on a |
| 314 | random unknown port is probably not very useful without "netstat". */ |
| 315 | if (o_verbose) { |
| 316 | char *addr; |
Denis Vlasenko | a771e7c | 2009-04-21 23:48:38 +0000 | [diff] [blame] | 317 | getsockname(netfd, &ouraddr->u.sa, &ouraddr->len); |
| 318 | //if (rr < 0) |
| 319 | // bb_perror_msg_and_die("getsockname after bind"); |
Denis Vlasenko | 7cff01e | 2008-02-02 16:23:43 +0000 | [diff] [blame] | 320 | addr = xmalloc_sockaddr2dotted(&ouraddr->u.sa); |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 321 | fprintf(stderr, "listening on %s ...\n", addr); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 322 | free(addr); |
| 323 | } |
| 324 | |
| 325 | if (o_udpmode) { |
| 326 | /* UDP is a speeeeecial case -- we have to do I/O *and* get the calling |
| 327 | party's particulars all at once, listen() and accept() don't apply. |
| 328 | At least in the BSD universe, however, recvfrom/PEEK is enough to tell |
| 329 | us something came in, and we can set things up so straight read/write |
| 330 | actually does work after all. Yow. YMMV on strange platforms! */ |
| 331 | |
| 332 | /* I'm not completely clear on how this works -- BSD seems to make UDP |
| 333 | just magically work in a connect()ed context, but we'll undoubtedly run |
| 334 | into systems this deal doesn't work on. For now, we apparently have to |
| 335 | issue a connect() on our just-tickled socket so we can write() back. |
| 336 | Again, why the fuck doesn't it just get filled in and taken care of?! |
| 337 | This hack is anything but optimal. Basically, if you want your listener |
| 338 | to also be able to send data back, you need this connect() line, which |
| 339 | also has the side effect that now anything from a different source or even a |
| 340 | different port on the other end won't show up and will cause ICMP errors. |
| 341 | I guess that's what they meant by "connect". |
| 342 | Let's try to remember what the "U" is *really* for, eh? */ |
| 343 | |
| 344 | /* If peer address is specified, connect to it */ |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 345 | remend.len = LSA_SIZEOF_SA; |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 346 | if (themaddr) { |
| 347 | remend = *themaddr; |
Denis Vlasenko | 7cff01e | 2008-02-02 16:23:43 +0000 | [diff] [blame] | 348 | xconnect(netfd, &themaddr->u.sa, themaddr->len); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 349 | } |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 350 | /* peek first packet and remember peer addr */ |
| 351 | arm(o_wait); /* might as well timeout this, too */ |
| 352 | if (setjmp(jbuf) == 0) { /* do timeout for initial connect */ |
| 353 | /* (*ouraddr) is prefilled with "default" address */ |
| 354 | /* and here we block... */ |
| 355 | rr = recv_from_to(netfd, NULL, 0, MSG_PEEK, /*was bigbuf_net, BIGSIZ*/ |
Denis Vlasenko | 7cff01e | 2008-02-02 16:23:43 +0000 | [diff] [blame] | 356 | &remend.u.sa, &ouraddr->u.sa, ouraddr->len); |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 357 | if (rr < 0) |
| 358 | bb_perror_msg_and_die("recvfrom"); |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 359 | unarm(); |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 360 | } else |
| 361 | bb_error_msg_and_die("timeout"); |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 362 | /* Now we learned *to which IP* peer has connected, and we want to anchor |
| 363 | our socket on it, so that our outbound packets will have correct local IP. |
| 364 | Unfortunately, bind() on already bound socket will fail now (EINVAL): |
Denis Vlasenko | 7cff01e | 2008-02-02 16:23:43 +0000 | [diff] [blame] | 365 | xbind(netfd, &ouraddr->u.sa, ouraddr->len); |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 366 | Need to read the packet, save data, close this socket and |
| 367 | create new one, and bind() it. TODO */ |
| 368 | if (!themaddr) |
Denis Vlasenko | 7cff01e | 2008-02-02 16:23:43 +0000 | [diff] [blame] | 369 | xconnect(netfd, &remend.u.sa, ouraddr->len); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 370 | } else { |
| 371 | /* TCP */ |
| 372 | arm(o_wait); /* wrap this in a timer, too; 0 = forever */ |
| 373 | if (setjmp(jbuf) == 0) { |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 374 | again: |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 375 | remend.len = LSA_SIZEOF_SA; |
Denis Vlasenko | 7cff01e | 2008-02-02 16:23:43 +0000 | [diff] [blame] | 376 | rr = accept(netfd, &remend.u.sa, &remend.len); |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 377 | if (rr < 0) |
| 378 | bb_perror_msg_and_die("accept"); |
Denys Vlasenko | 866710a | 2010-01-08 16:09:45 +0100 | [diff] [blame] | 379 | if (themaddr) { |
| 380 | int sv_port, port, r; |
| 381 | |
| 382 | sv_port = get_nport(&remend.u.sa); /* save */ |
| 383 | port = get_nport(&themaddr->u.sa); |
| 384 | if (port == 0) { |
| 385 | /* "nc -nl -p LPORT RHOST" (w/o RPORT!): |
| 386 | * we should accept any remote port */ |
| 387 | set_nport(&remend, 0); /* blot out remote port# */ |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 388 | } |
Denys Vlasenko | 866710a | 2010-01-08 16:09:45 +0100 | [diff] [blame] | 389 | r = memcmp(&remend.u.sa, &themaddr->u.sa, remend.len); |
| 390 | set_nport(&remend, sv_port); /* restore */ |
| 391 | if (r != 0) { |
| 392 | /* nc 1.10 bails out instead, and its error message |
| 393 | * is not suppressed by o_verbose */ |
| 394 | if (o_verbose) { |
| 395 | char *remaddr = xmalloc_sockaddr2dotted(&remend.u.sa); |
| 396 | bb_error_msg("connect from wrong ip/port %s ignored", remaddr); |
| 397 | free(remaddr); |
| 398 | } |
| 399 | close(rr); |
| 400 | goto again; |
| 401 | } |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 402 | } |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 403 | unarm(); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 404 | } else |
| 405 | bb_error_msg_and_die("timeout"); |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 406 | xmove_fd(rr, netfd); /* dump the old socket, here's our new one */ |
| 407 | /* find out what address the connection was *to* on our end, in case we're |
| 408 | doing a listen-on-any on a multihomed machine. This allows one to |
| 409 | offer different services via different alias addresses, such as the |
| 410 | "virtual web site" hack. */ |
Denis Vlasenko | a771e7c | 2009-04-21 23:48:38 +0000 | [diff] [blame] | 411 | getsockname(netfd, &ouraddr->u.sa, &ouraddr->len); |
| 412 | //if (rr < 0) |
| 413 | // bb_perror_msg_and_die("getsockname after accept"); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 414 | } |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 415 | |
| 416 | if (o_verbose) { |
| 417 | char *lcladdr, *remaddr, *remhostname; |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 418 | |
| 419 | #if ENABLE_NC_EXTRA && defined(IP_OPTIONS) |
| 420 | /* If we can, look for any IP options. Useful for testing the receiving end of |
| 421 | such things, and is a good exercise in dealing with it. We do this before |
| 422 | the connect message, to ensure that the connect msg is uniformly the LAST |
| 423 | thing to emerge after all the intervening crud. Doesn't work for UDP on |
| 424 | any machines I've tested, but feel free to surprise me. */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 425 | char optbuf[40]; |
Denis Vlasenko | c4f12f5 | 2008-05-12 14:35:56 +0000 | [diff] [blame] | 426 | socklen_t x = sizeof(optbuf); |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 427 | |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 428 | rr = getsockopt(netfd, IPPROTO_IP, IP_OPTIONS, optbuf, &x); |
Denis Vlasenko | f6b4685 | 2009-04-25 13:16:53 +0000 | [diff] [blame] | 429 | if (rr >= 0 && x) { /* we've got options, lessee em... */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 430 | bin2hex(bigbuf_net, optbuf, x); |
| 431 | bigbuf_net[2*x] = '\0'; |
| 432 | fprintf(stderr, "IP options: %s\n", bigbuf_net); |
| 433 | } |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 434 | #endif |
| 435 | |
| 436 | /* now check out who it is. We don't care about mismatched DNS names here, |
| 437 | but any ADDR and PORT we specified had better fucking well match the caller. |
| 438 | Converting from addr to inet_ntoa and back again is a bit of a kludge, but |
| 439 | gethostpoop wants a string and there's much gnarlier code out there already, |
| 440 | so I don't feel bad. |
| 441 | The *real* question is why BFD sockets wasn't designed to allow listens for |
| 442 | connections *from* specific hosts/ports, instead of requiring the caller to |
| 443 | accept the connection and then reject undesireable ones by closing. |
| 444 | In other words, we need a TCP MSG_PEEK. */ |
| 445 | /* bbox: removed most of it */ |
Denis Vlasenko | 7cff01e | 2008-02-02 16:23:43 +0000 | [diff] [blame] | 446 | lcladdr = xmalloc_sockaddr2dotted(&ouraddr->u.sa); |
| 447 | remaddr = xmalloc_sockaddr2dotted(&remend.u.sa); |
| 448 | remhostname = o_nflag ? remaddr : xmalloc_sockaddr2host(&remend.u.sa); |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 449 | fprintf(stderr, "connect to %s from %s (%s)\n", |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 450 | lcladdr, remhostname, remaddr); |
| 451 | free(lcladdr); |
| 452 | free(remaddr); |
| 453 | if (!o_nflag) |
| 454 | free(remhostname); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | /* udptest: |
| 459 | fire a couple of packets at a UDP target port, just to see if it's really |
| 460 | there. On BSD kernels, ICMP host/port-unreachable errors get delivered to |
| 461 | our socket as ECONNREFUSED write errors. On SV kernels, we lose; we'll have |
| 462 | to collect and analyze raw ICMP ourselves a la satan's probe_udp_ports |
| 463 | backend. Guess where one could swipe the appropriate code from... |
| 464 | |
| 465 | Use the time delay between writes if given, otherwise use the "tcp ping" |
| 466 | trick for getting the RTT. [I got that idea from pluvius, and warped it.] |
| 467 | Return either the original fd, or clean up and return -1. */ |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 468 | #if ENABLE_NC_EXTRA |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 469 | static int udptest(void) |
| 470 | { |
| 471 | int rr; |
| 472 | |
| 473 | rr = write(netfd, bigbuf_in, 1); |
| 474 | if (rr != 1) |
| 475 | bb_perror_msg("udptest first write"); |
| 476 | |
| 477 | if (o_wait) |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 478 | sleep(o_wait); // can be interrupted! while (t) nanosleep(&t)? |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 479 | else { |
| 480 | /* use the tcp-ping trick: try connecting to a normally refused port, which |
| 481 | causes us to block for the time that SYN gets there and RST gets back. |
| 482 | Not completely reliable, but it *does* mostly work. */ |
| 483 | /* Set a temporary connect timeout, so packet filtration doesnt cause |
| 484 | us to hang forever, and hit it */ |
| 485 | o_wait = 5; /* enough that we'll notice?? */ |
Denis Vlasenko | 7cff01e | 2008-02-02 16:23:43 +0000 | [diff] [blame] | 486 | rr = xsocket(ouraddr->u.sa.sa_family, SOCK_STREAM, 0); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 487 | set_nport(themaddr, htons(SLEAZE_PORT)); |
| 488 | connect_w_timeout(rr); |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 489 | /* don't need to restore themaddr's port, it's not used anymore */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 490 | close(rr); |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 491 | o_wait = 0; /* restore */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | rr = write(netfd, bigbuf_in, 1); |
| 495 | return (rr != 1); /* if rr == 1, return 0 (success) */ |
| 496 | } |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 497 | #else |
| 498 | int udptest(void); |
| 499 | #endif |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 500 | |
| 501 | /* oprint: |
| 502 | Hexdump bytes shoveled either way to a running logfile, in the format: |
| 503 | D offset - - - - --- 16 bytes --- - - - - # .... ascii ..... |
| 504 | where "which" sets the direction indicator, D: |
| 505 | 0 -- sent to network, or ">" |
| 506 | 1 -- rcvd and printed to stdout, or "<" |
| 507 | and "buf" and "n" are data-block and length. If the current block generates |
| 508 | a partial line, so be it; we *want* that lockstep indication of who sent |
| 509 | what when. Adapted from dgaudet's original example -- but must be ripping |
| 510 | *fast*, since we don't want to be too disk-bound... */ |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 511 | #if ENABLE_NC_EXTRA |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 512 | static void oprint(int direction, unsigned char *p, unsigned bc) |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 513 | { |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 514 | unsigned obc; /* current "global" offset */ |
| 515 | unsigned x; |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 516 | unsigned char *op; /* out hexdump ptr */ |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 517 | unsigned char *ap; /* out asc-dump ptr */ |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 518 | unsigned char stage[100]; |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 519 | |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 520 | if (bc == 0) |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 521 | return; |
| 522 | |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 523 | obc = wrote_net; /* use the globals! */ |
| 524 | if (direction == '<') |
| 525 | obc = wrote_out; |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 526 | stage[0] = direction; |
| 527 | stage[59] = '#'; /* preload separator */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 528 | stage[60] = ' '; |
| 529 | |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 530 | do { /* for chunk-o-data ... */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 531 | x = 16; |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 532 | if (bc < 16) { |
| 533 | /* memset(&stage[bc*3 + 11], ' ', 16*3 - bc*3); */ |
| 534 | memset(&stage[11], ' ', 16*3); |
| 535 | x = bc; |
| 536 | } |
Denis Vlasenko | c4f12f5 | 2008-05-12 14:35:56 +0000 | [diff] [blame] | 537 | sprintf((char *)&stage[1], " %8.8x ", obc); /* xxx: still slow? */ |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 538 | bc -= x; /* fix current count */ |
| 539 | obc += x; /* fix current offset */ |
| 540 | op = &stage[11]; /* where hex starts */ |
| 541 | ap = &stage[61]; /* where ascii starts */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 542 | |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 543 | do { /* for line of dump, however long ... */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 544 | *op++ = 0x20 | bb_hexdigits_upcase[*p >> 4]; |
| 545 | *op++ = 0x20 | bb_hexdigits_upcase[*p & 0x0f]; |
| 546 | *op++ = ' '; |
| 547 | if ((*p > 31) && (*p < 127)) |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 548 | *ap = *p; /* printing */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 549 | else |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 550 | *ap = '.'; /* nonprinting, loose def */ |
| 551 | ap++; |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 552 | p++; |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 553 | } while (--x); |
| 554 | *ap++ = '\n'; /* finish the line */ |
| 555 | xwrite(ofd, stage, ap - stage); |
| 556 | } while (bc); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 557 | } |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 558 | #else |
Denis Vlasenko | 828e2a9 | 2007-07-13 12:37:31 +0000 | [diff] [blame] | 559 | void oprint(int direction, unsigned char *p, unsigned bc); |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 560 | #endif |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 561 | |
| 562 | /* readwrite: |
| 563 | handle stdin/stdout/network I/O. Bwahaha!! -- the select loop from hell. |
| 564 | In this instance, return what might become our exit status. */ |
| 565 | static int readwrite(void) |
| 566 | { |
| 567 | int rr; |
| 568 | char *zp = zp; /* gcc */ /* stdin buf ptr */ |
| 569 | char *np = np; /* net-in buf ptr */ |
| 570 | unsigned rzleft; |
| 571 | unsigned rnleft; |
| 572 | unsigned netretry; /* net-read retry counter */ |
| 573 | unsigned wretry; /* net-write sanity counter */ |
| 574 | unsigned wfirst; /* one-shot flag to skip first net read */ |
| 575 | |
| 576 | /* if you don't have all this FD_* macro hair in sys/types.h, you'll have to |
| 577 | either find it or do your own bit-bashing: *ding1 |= (1 << fd), etc... */ |
| 578 | FD_SET(netfd, &ding1); /* global: the net is open */ |
| 579 | netretry = 2; |
| 580 | wfirst = 0; |
| 581 | rzleft = rnleft = 0; |
| 582 | if (o_interval) |
| 583 | sleep(o_interval); /* pause *before* sending stuff, too */ |
| 584 | |
| 585 | errno = 0; /* clear from sleep, close, whatever */ |
| 586 | /* and now the big ol' select shoveling loop ... */ |
| 587 | while (FD_ISSET(netfd, &ding1)) { /* i.e. till the *net* closes! */ |
| 588 | wretry = 8200; /* more than we'll ever hafta write */ |
| 589 | if (wfirst) { /* any saved stdin buffer? */ |
| 590 | wfirst = 0; /* clear flag for the duration */ |
| 591 | goto shovel; /* and go handle it first */ |
| 592 | } |
| 593 | ding2 = ding1; /* FD_COPY ain't portable... */ |
| 594 | /* some systems, notably linux, crap into their select timers on return, so |
| 595 | we create a expendable copy and give *that* to select. */ |
| 596 | if (o_wait) { |
| 597 | struct timeval tmp_timer; |
| 598 | tmp_timer.tv_sec = o_wait; |
| 599 | tmp_timer.tv_usec = 0; |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 600 | /* highest possible fd is netfd (3) */ |
| 601 | rr = select(netfd+1, &ding2, NULL, NULL, &tmp_timer); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 602 | } else |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 603 | rr = select(netfd+1, &ding2, NULL, NULL, NULL); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 604 | if (rr < 0 && errno != EINTR) { /* might have gotten ^Zed, etc */ |
| 605 | holler_perror("select"); |
| 606 | close(netfd); |
| 607 | return 1; |
| 608 | } |
| 609 | /* if we have a timeout AND stdin is closed AND we haven't heard anything |
| 610 | from the net during that time, assume it's dead and close it too. */ |
| 611 | if (rr == 0) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 612 | if (!FD_ISSET(STDIN_FILENO, &ding1)) |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 613 | netretry--; /* we actually try a coupla times. */ |
| 614 | if (!netretry) { |
| 615 | if (o_verbose > 1) /* normally we don't care */ |
| 616 | fprintf(stderr, "net timeout\n"); |
| 617 | close(netfd); |
| 618 | return 0; /* not an error! */ |
| 619 | } |
| 620 | } /* select timeout */ |
| 621 | /* xxx: should we check the exception fds too? The read fds seem to give |
| 622 | us the right info, and none of the examples I found bothered. */ |
| 623 | |
| 624 | /* Ding!! Something arrived, go check all the incoming hoppers, net first */ |
| 625 | if (FD_ISSET(netfd, &ding2)) { /* net: ding! */ |
| 626 | rr = read(netfd, bigbuf_net, BIGSIZ); |
| 627 | if (rr <= 0) { |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 628 | if (rr < 0 && o_verbose > 1) { |
| 629 | /* nc 1.10 doesn't do this */ |
| 630 | bb_perror_msg("net read"); |
| 631 | } |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 632 | FD_CLR(netfd, &ding1); /* net closed, we'll finish up... */ |
| 633 | rzleft = 0; /* can't write anymore: broken pipe */ |
| 634 | } else { |
| 635 | rnleft = rr; |
| 636 | np = bigbuf_net; |
| 637 | } |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 638 | Debug("got %d from the net, errno %d", rr, errno); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 639 | } /* net:ding */ |
| 640 | |
| 641 | /* if we're in "slowly" mode there's probably still stuff in the stdin |
| 642 | buffer, so don't read unless we really need MORE INPUT! MORE INPUT! */ |
| 643 | if (rzleft) |
| 644 | goto shovel; |
| 645 | |
| 646 | /* okay, suck more stdin */ |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 647 | if (FD_ISSET(STDIN_FILENO, &ding2)) { /* stdin: ding! */ |
| 648 | rr = read(STDIN_FILENO, bigbuf_in, BIGSIZ); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 649 | /* Considered making reads here smaller for UDP mode, but 8192-byte |
| 650 | mobygrams are kinda fun and exercise the reassembler. */ |
| 651 | if (rr <= 0) { /* at end, or fukt, or ... */ |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 652 | FD_CLR(STDIN_FILENO, &ding1); /* disable and close stdin */ |
Denis Vlasenko | f6b4685 | 2009-04-25 13:16:53 +0000 | [diff] [blame] | 653 | close(STDIN_FILENO); |
| 654 | // Does it make sense to shutdown(net_fd, SHUT_WR) |
| 655 | // to let other side know that we won't write anything anymore? |
| 656 | // (and what about keeping compat if we do that?) |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 657 | } else { |
| 658 | rzleft = rr; |
| 659 | zp = bigbuf_in; |
| 660 | } |
| 661 | } /* stdin:ding */ |
| 662 | shovel: |
| 663 | /* now that we've dingdonged all our thingdings, send off the results. |
| 664 | Geez, why does this look an awful lot like the big loop in "rsh"? ... |
| 665 | not sure if the order of this matters, but write net -> stdout first. */ |
| 666 | |
| 667 | /* sanity check. Works because they're both unsigned... */ |
| 668 | if ((rzleft > 8200) || (rnleft > 8200)) { |
| 669 | holler_error("bogus buffers: %u, %u", rzleft, rnleft); |
| 670 | rzleft = rnleft = 0; |
| 671 | } |
| 672 | /* net write retries sometimes happen on UDP connections */ |
| 673 | if (!wretry) { /* is something hung? */ |
| 674 | holler_error("too many output retries"); |
| 675 | return 1; |
| 676 | } |
| 677 | if (rnleft) { |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 678 | rr = write(STDOUT_FILENO, np, rnleft); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 679 | if (rr > 0) { |
Denis Vlasenko | c4f12f5 | 2008-05-12 14:35:56 +0000 | [diff] [blame] | 680 | if (o_ofile) /* log the stdout */ |
| 681 | oprint('<', (unsigned char *)np, rr); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 682 | np += rr; /* fix up ptrs and whatnot */ |
| 683 | rnleft -= rr; /* will get sanity-checked above */ |
| 684 | wrote_out += rr; /* global count */ |
| 685 | } |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 686 | Debug("wrote %d to stdout, errno %d", rr, errno); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 687 | } /* rnleft */ |
| 688 | if (rzleft) { |
| 689 | if (o_interval) /* in "slowly" mode ?? */ |
| 690 | rr = findline(zp, rzleft); |
| 691 | else |
| 692 | rr = rzleft; |
| 693 | rr = write(netfd, zp, rr); /* one line, or the whole buffer */ |
| 694 | if (rr > 0) { |
Denis Vlasenko | c4f12f5 | 2008-05-12 14:35:56 +0000 | [diff] [blame] | 695 | if (o_ofile) /* log what got sent */ |
| 696 | oprint('>', (unsigned char *)zp, rr); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 697 | zp += rr; |
| 698 | rzleft -= rr; |
| 699 | wrote_net += rr; /* global count */ |
| 700 | } |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 701 | Debug("wrote %d to net, errno %d", rr, errno); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 702 | } /* rzleft */ |
| 703 | if (o_interval) { /* cycle between slow lines, or ... */ |
| 704 | sleep(o_interval); |
| 705 | errno = 0; /* clear from sleep */ |
| 706 | continue; /* ...with hairy select loop... */ |
| 707 | } |
| 708 | if ((rzleft) || (rnleft)) { /* shovel that shit till they ain't */ |
| 709 | wretry--; /* none left, and get another load */ |
| 710 | goto shovel; |
| 711 | } |
| 712 | } /* while ding1:netfd is open */ |
| 713 | |
| 714 | /* XXX: maybe want a more graceful shutdown() here, or screw around with |
| 715 | linger times?? I suspect that I don't need to since I'm always doing |
| 716 | blocking reads and writes and my own manual "last ditch" efforts to read |
| 717 | the net again after a timeout. I haven't seen any screwups yet, but it's |
| 718 | not like my test network is particularly busy... */ |
| 719 | close(netfd); |
| 720 | return 0; |
| 721 | } /* readwrite */ |
| 722 | |
| 723 | /* main: now we pull it all together... */ |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 724 | int nc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 725 | int nc_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 726 | { |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 727 | char *str_p, *str_s; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 728 | IF_NC_EXTRA(char *str_i, *str_o;) |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 729 | char *themdotted = themdotted; /* gcc */ |
| 730 | char **proggie; |
| 731 | int x; |
| 732 | unsigned o_lport = 0; |
| 733 | |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 734 | INIT_G(); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 735 | |
| 736 | /* catch a signal or two for cleanup */ |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 737 | bb_signals(0 |
| 738 | + (1 << SIGINT) |
| 739 | + (1 << SIGQUIT) |
| 740 | + (1 << SIGTERM) |
| 741 | , catch); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 742 | /* and suppress others... */ |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 743 | bb_signals(0 |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 744 | #ifdef SIGURG |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 745 | + (1 << SIGURG) |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 746 | #endif |
Denis Vlasenko | 25591c3 | 2008-02-16 22:58:56 +0000 | [diff] [blame] | 747 | + (1 << SIGPIPE) /* important! */ |
| 748 | , SIG_IGN); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 749 | |
| 750 | proggie = argv; |
| 751 | while (*++proggie) { |
| 752 | if (strcmp(*proggie, "-e") == 0) { |
| 753 | *proggie = NULL; |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 754 | proggie++; |
| 755 | goto e_found; |
| 756 | } |
| 757 | } |
| 758 | proggie = NULL; |
| 759 | e_found: |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 760 | |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 761 | // -g -G -t -r deleted, unimplemented -a deleted too |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 762 | opt_complementary = "?2:vv:w+"; /* max 2 params; -v is a counter; -w N */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 763 | getopt32(argv, "hnp:s:uvw:" IF_NC_SERVER("l") |
| 764 | IF_NC_EXTRA("i:o:z"), |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 765 | &str_p, &str_s, &o_wait |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 766 | IF_NC_EXTRA(, &str_i, &str_o, &o_verbose)); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 767 | argv += optind; |
| 768 | #if ENABLE_NC_EXTRA |
| 769 | if (option_mask32 & OPT_i) /* line-interval time */ |
| 770 | o_interval = xatou_range(str_i, 1, 0xffff); |
| 771 | #endif |
| 772 | //if (option_mask32 & OPT_l) /* listen mode */ |
| 773 | //if (option_mask32 & OPT_n) /* numeric-only, no DNS lookups */ |
| 774 | //if (option_mask32 & OPT_o) /* hexdump log */ |
| 775 | if (option_mask32 & OPT_p) { /* local source port */ |
| 776 | o_lport = bb_lookup_port(str_p, o_udpmode ? "udp" : "tcp", 0); |
| 777 | if (!o_lport) |
| 778 | bb_error_msg_and_die("bad local port '%s'", str_p); |
| 779 | } |
| 780 | //if (option_mask32 & OPT_r) /* randomize various things */ |
| 781 | //if (option_mask32 & OPT_u) /* use UDP */ |
| 782 | //if (option_mask32 & OPT_v) /* verbose */ |
Denis Vlasenko | 1d42665 | 2008-03-17 09:09:09 +0000 | [diff] [blame] | 783 | //if (option_mask32 & OPT_w) /* wait time */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 784 | //if (option_mask32 & OPT_z) /* little or no data xfer */ |
| 785 | |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 786 | /* We manage our fd's so that they are never 0,1,2 */ |
| 787 | /*bb_sanitize_stdio(); - not needed */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 788 | |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 789 | if (argv[0]) { |
| 790 | themaddr = xhost2sockaddr(argv[0], |
| 791 | argv[1] |
| 792 | ? bb_lookup_port(argv[1], o_udpmode ? "udp" : "tcp", 0) |
| 793 | : 0); |
| 794 | } |
| 795 | |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 796 | /* create & bind network socket */ |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 797 | x = (o_udpmode ? SOCK_DGRAM : SOCK_STREAM); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 798 | if (option_mask32 & OPT_s) { /* local address */ |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 799 | /* if o_lport is still 0, then we will use random port */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 800 | ouraddr = xhost2sockaddr(str_s, o_lport); |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 801 | #ifdef BLOAT |
| 802 | /* prevent spurious "UDP listen needs !0 port" */ |
| 803 | o_lport = get_nport(ouraddr); |
| 804 | o_lport = ntohs(o_lport); |
| 805 | #endif |
Denis Vlasenko | 7cff01e | 2008-02-02 16:23:43 +0000 | [diff] [blame] | 806 | x = xsocket(ouraddr->u.sa.sa_family, x, 0); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 807 | } else { |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 808 | /* We try IPv6, then IPv4, unless addr family is |
| 809 | * implicitly set by way of remote addr/port spec */ |
| 810 | x = xsocket_type(&ouraddr, |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 811 | (themaddr ? themaddr->u.sa.sa_family : AF_UNSPEC), |
Denis Vlasenko | 5c51a7c | 2007-06-05 20:08:11 +0000 | [diff] [blame] | 812 | x); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 813 | if (o_lport) |
| 814 | set_nport(ouraddr, htons(o_lport)); |
| 815 | } |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 816 | xmove_fd(x, netfd); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 817 | setsockopt_reuseaddr(netfd); |
| 818 | if (o_udpmode) |
| 819 | socket_want_pktinfo(netfd); |
Denis Vlasenko | f6b4685 | 2009-04-25 13:16:53 +0000 | [diff] [blame] | 820 | if (!ENABLE_FEATURE_UNIX_LOCAL |
| 821 | || o_listen |
| 822 | || ouraddr->u.sa.sa_family != AF_UNIX |
| 823 | ) { |
| 824 | xbind(netfd, &ouraddr->u.sa, ouraddr->len); |
| 825 | } |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 826 | #if 0 |
| 827 | setsockopt(netfd, SOL_SOCKET, SO_RCVBUF, &o_rcvbuf, sizeof o_rcvbuf); |
| 828 | setsockopt(netfd, SOL_SOCKET, SO_SNDBUF, &o_sndbuf, sizeof o_sndbuf); |
| 829 | #endif |
| 830 | |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 831 | #ifdef BLOAT |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 832 | if (OPT_l && (option_mask32 & (OPT_u|OPT_l)) == (OPT_u|OPT_l)) { |
| 833 | /* apparently UDP can listen ON "port 0", |
| 834 | but that's not useful */ |
| 835 | if (!o_lport) |
| 836 | bb_error_msg_and_die("UDP listen needs nonzero -p port"); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 837 | } |
Denis Vlasenko | 4cf1d08 | 2008-03-12 23:13:50 +0000 | [diff] [blame] | 838 | #endif |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 839 | |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 840 | FD_SET(STDIN_FILENO, &ding1); /* stdin *is* initially open */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 841 | if (proggie) { |
| 842 | close(0); /* won't need stdin */ |
| 843 | option_mask32 &= ~OPT_o; /* -o with -e is meaningless! */ |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 844 | } |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 845 | #if ENABLE_NC_EXTRA |
| 846 | if (o_ofile) |
| 847 | xmove_fd(xopen(str_o, O_WRONLY|O_CREAT|O_TRUNC), ofd); |
| 848 | #endif |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 849 | |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 850 | if (o_listen) { |
| 851 | dolisten(); |
| 852 | /* dolisten does its own connect reporting */ |
| 853 | if (proggie) /* -e given? */ |
| 854 | doexec(proggie); |
| 855 | x = readwrite(); /* it even works with UDP! */ |
| 856 | } else { |
| 857 | /* Outbound connects. Now we're more picky about args... */ |
| 858 | if (!themaddr) |
| 859 | bb_error_msg_and_die("no destination"); |
| 860 | |
| 861 | remend = *themaddr; |
| 862 | if (o_verbose) |
Denis Vlasenko | 7cff01e | 2008-02-02 16:23:43 +0000 | [diff] [blame] | 863 | themdotted = xmalloc_sockaddr2dotted(&themaddr->u.sa); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 864 | |
| 865 | x = connect_w_timeout(netfd); |
| 866 | if (o_zero && x == 0 && o_udpmode) /* if UDP scanning... */ |
| 867 | x = udptest(); |
| 868 | if (x == 0) { /* Yow, are we OPEN YET?! */ |
| 869 | if (o_verbose) |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 870 | fprintf(stderr, "%s (%s) open\n", argv[0], themdotted); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 871 | if (proggie) /* exec is valid for outbound, too */ |
| 872 | doexec(proggie); |
| 873 | if (!o_zero) |
| 874 | x = readwrite(); |
| 875 | } else { /* connect or udptest wasn't successful */ |
| 876 | x = 1; /* exit status */ |
| 877 | /* if we're scanning at a "one -v" verbosity level, don't print refusals. |
| 878 | Give it another -v if you want to see everything. */ |
| 879 | if (o_verbose > 1 || (o_verbose && errno != ECONNREFUSED)) |
Denis Vlasenko | 19507f0 | 2007-04-06 10:41:05 +0000 | [diff] [blame] | 880 | bb_perror_msg("%s (%s)", argv[0], themdotted); |
Denis Vlasenko | 29fe726 | 2007-04-05 20:26:28 +0000 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | if (o_verbose > 1) /* normally we don't care */ |
| 884 | fprintf(stderr, SENT_N_RECV_M, wrote_net, wrote_out); |
| 885 | return x; |
| 886 | } |