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