udhcpc: reuse string constant; remove unneeded memset(0)
[oweals/busybox.git] / networking / nc_bloaty.c
index 260d2057a0d2dbb8bcd4dcd34d785459556ee7b7..d184f689b57479f26e8ca33e5cfc0fc4475597fc 100644 (file)
@@ -1,9 +1,9 @@
 /* Based on netcat 1.10 RELEASE 960320 written by hobbit@avian.org.
  * Released into public domain by the author.
  *
- * Copyright (C) 2007 Denis Vlasenko.
+ * Copyright (C) 2007 Denys Vlasenko.
  *
- * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 
 /* Author's comments from nc 1.10:
  * - source routing
  * - multiple DNS checks
  * Functionalty which is different from nc 1.10:
- * - Prog in '-e prog' can have prog's parameters and options.
+ * - PROG in '-e PROG' can have ARGS (and options).
  *   Because of this -e option must be last.
- * - nc doesn't redirect stderr to the network socket for the -e prog.
+//TODO: remove -e incompatibility?
+ * - we don't redirect stderr to the network socket for the -e PROG.
+ *   (PROG can do it itself if needed, but sometimes it is NOT wanted!)
+ * - numeric addresses are printed in (), not [] (IPv6 looks better),
+ *   port numbers are inside (): (1.2.3.4:5678)
+ * - network read errors are reported on verbose levels > 1
+ *   (nc 1.10 treats them as EOF)
+ * - TCP connects from wrong ip/ports (if peer ip:port is specified
+ *   on the command line, but accept() says that it came from different addr)
+ *   are closed, but we don't exit - we continue to listen/accept.
  */
 
-/* done in nc.c: #include "busybox.h" */
+/* done in nc.c: #include "libbb.h" */
+
+//usage:#if ENABLE_NC_110_COMPAT
+//usage:
+//usage:#define nc_trivial_usage
+//usage:       "[OPTIONS] HOST PORT  - connect"
+//usage:       IF_NC_SERVER("\n"
+//usage:       "nc [OPTIONS] -l -p PORT [HOST] [PORT]  - listen"
+//usage:       )
+//usage:#define nc_full_usage "\n\n"
+//usage:       "       -e PROG Run PROG after connect (must be last)"
+//usage:       IF_NC_SERVER(
+//usage:     "\n       -l      Listen mode, for inbound connects"
+//usage:       )
+//usage:     "\n       -p PORT Local port"
+//usage:     "\n       -s ADDR Local address"
+//usage:     "\n       -w SEC  Timeout for connects and final net reads"
+//usage:       IF_NC_EXTRA(
+//usage:     "\n       -i SEC  Delay interval for lines sent" /* ", ports scanned" */
+//usage:       )
+//usage:     "\n       -n      Don't do DNS resolution"
+//usage:     "\n       -u      UDP mode"
+//usage:     "\n       -v      Verbose"
+//usage:       IF_NC_EXTRA(
+//usage:     "\n       -o FILE Hex dump traffic"
+//usage:     "\n       -z      Zero-I/O mode (scanning)"
+//usage:       )
+//usage:#endif
+
+/*   "\n       -r              Randomize local and remote ports" */
+/*   "\n       -g gateway      Source-routing hop point[s], up to 8" */
+/*   "\n       -G num          Source-routing pointer: 4, 8, 12, ..." */
+/*   "\nport numbers can be individual or ranges: lo-hi [inclusive]" */
+
+/* -e PROG can take ARGS too: "nc ... -e ls -l", but we don't document it
+ * in help text: nc 1.10 does not allow that. We don't want to entice
+ * users to use this incompatibility */
 
-#define SLEAZE_PORT 31337               /* for UDP-scan RTT trick, change if ya want */
-#define BIGSIZ 8192                     /* big buffers */
+enum {
+       SLEAZE_PORT = 31337,               /* for UDP-scan RTT trick, change if ya want */
+       BIGSIZ = 8192,                     /* big buffers */
+
+       netfd = 3,
+       ofd = 4,
+};
 
 struct globals {
-       int netfd;
-       int ofd;                     /* hexdump output fd */
+       /* global cmd flags: */
+       unsigned o_verbose;
+       unsigned o_wait;
+#if ENABLE_NC_EXTRA
+       unsigned o_interval;
+#endif
+
+       /*int netfd;*/
+       /*int ofd;*/                     /* hexdump output fd */
 #if ENABLE_LFS
 #define SENT_N_RECV_M "sent %llu, rcvd %llu\n"
        unsigned long long wrote_out;          /* total stdout bytes */
        unsigned long long wrote_net;          /* total net bytes */
 #else
-#define SENT_N_RECV_M "sent %u, rcvd %u"
+#define SENT_N_RECV_M "sent %u, rcvd %u\n"
        unsigned wrote_out;          /* total stdout bytes */
        unsigned wrote_net;          /* total net bytes */
 #endif
-       /* ouraddr is never NULL and goes thru three states as we progress:
+       /* ouraddr is never NULL and goes through three states as we progress:
         1 - local address before bind (IP/port possibly zero)
         2 - local address after bind (port is nonzero)
         3 - local address after connect??/recv/accept (IP and port are nonzero) */
@@ -68,15 +125,7 @@ struct globals {
        /* remend is set after connect/recv/accept to the actual ip:port of peer */
        struct len_and_sockaddr remend;
 
-       /* global cmd flags: */
-       unsigned o_verbose;
-       unsigned o_wait;
-#if ENABLE_NC_EXTRA
-       unsigned o_interval;
-#endif
-
        jmp_buf jbuf;                /* timer crud */
-       unsigned char *stage;        /* hexdump line buffer */
 
        /* will malloc up the following globals: */
        fd_set ding1;                /* for select loop */
@@ -86,16 +135,12 @@ struct globals {
 };
 
 #define G (*ptr_to_globals)
-
-#define netfd      (G.netfd     )
-#define ofd        (G.ofd       )
 #define wrote_out  (G.wrote_out )
 #define wrote_net  (G.wrote_net )
 #define ouraddr    (G.ouraddr   )
 #define themaddr   (G.themaddr  )
 #define remend     (G.remend    )
 #define jbuf       (G.jbuf      )
-#define stage      (G.stage     )
 #define ding1      (G.ding1     )
 #define ding2      (G.ding2     )
 #define bigbuf_in  (G.bigbuf_in )
@@ -107,6 +152,10 @@ struct globals {
 #else
 #define o_interval 0
 #endif
+#define INIT_G() do { \
+       SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
+} while (0)
+
 
 /* Must match getopt32 call! */
 enum {
@@ -125,42 +174,50 @@ enum {
 
 #define o_nflag   (option_mask32 & OPT_n)
 #define o_udpmode (option_mask32 & OPT_u)
-#if ENABLE_NC_EXTRA
-#define o_wfile   (option_mask32 & OPT_o)
+#if ENABLE_NC_SERVER
 #define o_listen  (option_mask32 & OPT_l)
-#define o_zero    (option_mask32 & OPT_z)
 #else
-#define o_wfile   0
 #define o_listen  0
+#endif
+#if ENABLE_NC_EXTRA
+#define o_ofile   (option_mask32 & OPT_o)
+#define o_zero    (option_mask32 & OPT_z)
+#else
+#define o_ofile   0
 #define o_zero    0
 #endif
 
-/* Debug macro: squirt whatever message and sleep a bit so we can see it go
- by.  need to call like Debug((stuff)) [with no ; ] so macro args match!
- Beware: writes to stdOUT... */
+/* Debug: squirt whatever message and sleep a bit so we can see it go by. */
+/* Beware: writes to stdOUT... */
 #if 0
-#define Debug(x) printf x; printf("\n"); fflush(stdout); sleep(1);
+#define Debug(...) do { printf(__VA_ARGS__); printf("\n"); fflush_all(); sleep(1); } while (0)
 #else
-#define Debug(x)        /* nil... */
+#define Debug(...) do { } while (0)
 #endif
 
-#define holler_error(...)  do { if (o_verbose) bb_error_msg(__VA_ARGS__); } while(0)
-#define holler_perror(...) do { if (o_verbose) bb_perror_msg(__VA_ARGS__); } while(0)
+#define holler_error(...)  do { if (o_verbose) bb_error_msg(__VA_ARGS__); } while (0)
+#define holler_perror(...) do { if (o_verbose) bb_perror_msg(__VA_ARGS__); } while (0)
 
 /* catch: no-brainer interrupt handler */
 static void catch(int sig)
 {
-       errno = 0;
        if (o_verbose > 1)                /* normally we don't care */
                fprintf(stderr, SENT_N_RECV_M, wrote_net, wrote_out);
        fprintf(stderr, "punt!\n");
+       kill_myself_with_sig(sig);
 }
 
-/* timeout and other signal handling cruft */
-static void tmtravel(int sig)
+/* unarm  */
+static void unarm(void)
 {
        signal(SIGALRM, SIG_IGN);
        alarm(0);
+}
+
+/* timeout and other signal handling cruft */
+static void tmtravel(int sig UNUSED_PARAM)
+{
+       unarm();
        longjmp(jbuf, 1);
 }
 
@@ -171,13 +228,6 @@ static void arm(unsigned secs)
        alarm(secs);
 }
 
-/* unarm  */
-static void unarm(void)
-{
-       signal(SIGALRM, SIG_IGN);
-       alarm(0);
-}
-
 /* findline:
  find the next newline in a buffer; return inclusive size of that "line",
  or the entire buffer size, so the caller knows how much to then write().
@@ -195,12 +245,12 @@ static unsigned findline(char *buf, unsigned siz)
                if (*p == '\n') {
                        x = (int) (p - buf);
                        x++;                        /* 'sokay if it points just past the end! */
-Debug(("findline returning %d", x))
+Debug("findline returning %d", x);
                        return x;
                }
                p++;
        } /* for */
-Debug(("findline returning whole thing: %d", siz))
+Debug("findline returning whole thing: %d", siz);
        return siz;
 } /* findline */
 
@@ -210,15 +260,14 @@ Debug(("findline returning whole thing: %d", siz))
  that would be security-critical, which is why it's ifdefed out by default.
  Use at your own hairy risk; if you leave shells lying around behind open
  listening ports you deserve to lose!! */
-static int doexec(char **proggie) ATTRIBUTE_NORETURN;
+static int doexec(char **proggie) NORETURN;
 static int doexec(char **proggie)
 {
        xmove_fd(netfd, 0);
        dup2(0, 1);
        /* dup2(0, 2); - do we *really* want this? NO!
         * exec'ed prog can do it yourself, if needed */
-       execvp(proggie[0], proggie);
-       bb_perror_msg_and_die("exec");
+       BB_EXECVP_or_die(proggie);
 }
 
 /* connect_w_timeout:
@@ -234,12 +283,12 @@ static int connect_w_timeout(int fd)
        /* wrap connect inside a timer, and hit it */
        arm(o_wait);
        if (setjmp(jbuf) == 0) {
-               rr = connect(fd, &themaddr->sa, themaddr->len);
+               rr = connect(fd, &themaddr->u.sa, themaddr->len);
+               unarm();
        } else { /* setjmp: connect failed... */
                rr = -1;
                errno = ETIMEDOUT; /* fake it */
        }
-       unarm();
        return rr;
 }
 
@@ -251,7 +300,6 @@ static int connect_w_timeout(int fd)
 static void dolisten(void)
 {
        int rr;
-       const char *errmsg = errmsg; /* gcc */
 
        if (!o_udpmode)
                xlisten(netfd, 1); /* TCP: gotta listen() before we can get */
@@ -267,11 +315,11 @@ static void dolisten(void)
         random unknown port is probably not very useful without "netstat". */
        if (o_verbose) {
                char *addr;
-               rr = getsockname(netfd, &ouraddr->sa, &ouraddr->len);
-               if (rr < 0)
-                       bb_perror_msg_and_die("getsockname after bind");
-               addr = xmalloc_sockaddr2dotted(&ouraddr->sa, ouraddr->len);
-               fprintf(stderr, "listening on [%s] ...\n", addr);
+               getsockname(netfd, &ouraddr->u.sa, &ouraddr->len);
+               //if (rr < 0)
+               //      bb_perror_msg_and_die("getsockname after bind");
+               addr = xmalloc_sockaddr2dotted(&ouraddr->u.sa);
+               fprintf(stderr, "listening on %s ...\n", addr);
                free(addr);
        }
 
@@ -295,48 +343,79 @@ static void dolisten(void)
                 Let's try to remember what the "U" is *really* for, eh? */
 
                /* If peer address is specified, connect to it */
+               remend.len = LSA_SIZEOF_SA;
                if (themaddr) {
                        remend = *themaddr;
-                       xconnect(netfd, &themaddr->sa, themaddr->len);
-                       rr = 0;
-               } else { /* peek first packet and remember peer addr */
-                       arm(o_wait);                /* might as well timeout this, too */
-                       if (setjmp(jbuf) == 0) {       /* do timeout for initial connect */
-                               /* (*ouraddr) is prefilled with "default" address */
-                               /* and here we block... */
-                               rr = recv_from_to(netfd, NULL, 0, MSG_PEEK, /*was bigbuf_net, BIGSIZ*/
-                                       &remend.sa, &ouraddr->sa, ouraddr->len);
-                               if (rr < 0)
-                                       bb_perror_msg_and_die("recvfrom");
-                       } else
-                               bb_error_msg_and_die("timeout");
-                       unarm();
-                       rr = connect(netfd, &remend.sa, ouraddr->len);
-                       errmsg = "connect";
+                       xconnect(netfd, &themaddr->u.sa, themaddr->len);
                }
+               /* peek first packet and remember peer addr */
+               arm(o_wait);                /* might as well timeout this, too */
+               if (setjmp(jbuf) == 0) {       /* do timeout for initial connect */
+                       /* (*ouraddr) is prefilled with "default" address */
+                       /* and here we block... */
+                       rr = recv_from_to(netfd, NULL, 0, MSG_PEEK, /*was bigbuf_net, BIGSIZ*/
+                               &remend.u.sa, &ouraddr->u.sa, ouraddr->len);
+                       if (rr < 0)
+                               bb_perror_msg_and_die("recvfrom");
+                       unarm();
+               } else
+                       bb_error_msg_and_die("timeout");
+/* Now we learned *to which IP* peer has connected, and we want to anchor
+our socket on it, so that our outbound packets will have correct local IP.
+Unfortunately, bind() on already bound socket will fail now (EINVAL):
+       xbind(netfd, &ouraddr->u.sa, ouraddr->len);
+Need to read the packet, save data, close this socket and
+create new one, and bind() it. TODO */
+               if (!themaddr)
+                       xconnect(netfd, &remend.u.sa, ouraddr->len);
        } else {
                /* TCP */
                arm(o_wait); /* wrap this in a timer, too; 0 = forever */
                if (setjmp(jbuf) == 0) {
+ again:
                        remend.len = LSA_SIZEOF_SA;
-                       rr = accept(netfd, &remend.sa, &remend.len);
+                       rr = accept(netfd, &remend.u.sa, &remend.len);
+                       if (rr < 0)
+                               bb_perror_msg_and_die("accept");
+                       if (themaddr) {
+                               int sv_port, port, r;
+
+                               sv_port = get_nport(&remend.u.sa); /* save */
+                               port = get_nport(&themaddr->u.sa);
+                               if (port == 0) {
+                                       /* "nc -nl -p LPORT RHOST" (w/o RPORT!):
+                                        * we should accept any remote port */
+                                       set_nport(&remend.u.sa, 0); /* blot out remote port# */
+                               }
+                               r = memcmp(&remend.u.sa, &themaddr->u.sa, remend.len);
+                               set_nport(&remend.u.sa, sv_port); /* restore */
+                               if (r != 0) {
+                                       /* nc 1.10 bails out instead, and its error message
+                                        * is not suppressed by o_verbose */
+                                       if (o_verbose) {
+                                               char *remaddr = xmalloc_sockaddr2dotted(&remend.u.sa);
+                                               bb_error_msg("connect from wrong ip/port %s ignored", remaddr);
+                                               free(remaddr);
+                                       }
+                                       close(rr);
+                                       goto again;
+                               }
+                       }
+                       unarm();
                } else
                        bb_error_msg_and_die("timeout");
-               unarm();
-               errmsg = "accept";
-               if (rr >= 0) {
-                       close(netfd); /* dump the old socket */
-                       netfd = rr; /* here's our new one */
-                       /* find out what address the connection was *to* on our end, in case we're
-                        doing a listen-on-any on a multihomed machine.  This allows one to
-                        offer different services via different alias addresses, such as the
-                        "virtual web site" hack. */
-                       rr = getsockname(netfd, &ouraddr->sa, &ouraddr->len);
-                       errmsg = "getsockname after accept";
-               }
+               xmove_fd(rr, netfd); /* dump the old socket, here's our new one */
+               /* find out what address the connection was *to* on our end, in case we're
+                doing a listen-on-any on a multihomed machine.  This allows one to
+                offer different services via different alias addresses, such as the
+                "virtual web site" hack. */
+               getsockname(netfd, &ouraddr->u.sa, &ouraddr->len);
+               //if (rr < 0)
+               //      bb_perror_msg_and_die("getsockname after accept");
        }
-       if (rr < 0)
-               bb_perror_msg_and_die(errmsg);
+
+       if (o_verbose) {
+               char *lcladdr, *remaddr, *remhostname;
 
 #if ENABLE_NC_EXTRA && defined(IP_OPTIONS)
        /* If we can, look for any IP options.  Useful for testing the receiving end of
@@ -344,18 +423,14 @@ static void dolisten(void)
         the connect message, to ensure that the connect msg is uniformly the LAST
         thing to emerge after all the intervening crud.  Doesn't work for UDP on
         any machines I've tested, but feel free to surprise me. */
-       if (o_verbose) {
                char optbuf[40];
-               int x = sizeof(optbuf);
+               socklen_t x = sizeof(optbuf);
+
                rr = getsockopt(netfd, IPPROTO_IP, IP_OPTIONS, optbuf, &x);
-               if (rr < 0)
-                       bb_perror_msg("getsockopt failed");
-               else if (x) {    /* we've got options, lessee em... */
-                       bin2hex(bigbuf_net, optbuf, x);
-                       bigbuf_net[2*x] = '\0';
+               if (rr >= 0 && x) {    /* we've got options, lessee em... */
+                       *bin2hex(bigbuf_net, optbuf, x) = '\0';
                        fprintf(stderr, "IP options: %s\n", bigbuf_net);
                }
-       }
 #endif
 
        /* now check out who it is.  We don't care about mismatched DNS names here,
@@ -368,11 +443,10 @@ static void dolisten(void)
         accept the connection and then reject undesireable ones by closing.
         In other words, we need a TCP MSG_PEEK. */
        /* bbox: removed most of it */
-       if (o_verbose) {
-               char *lcladdr = xmalloc_sockaddr2dotted(&ouraddr->sa, ouraddr->len);
-               char *remaddr = xmalloc_sockaddr2dotted(&remend.sa, remend.len);
-               char *remhostname = o_nflag ? remaddr : xmalloc_sockaddr2host(&remend.sa, remend.len);
-               fprintf(stderr, "connect to [%s] from %s [%s]\n",
+               lcladdr = xmalloc_sockaddr2dotted(&ouraddr->u.sa);
+               remaddr = xmalloc_sockaddr2dotted(&remend.u.sa);
+               remhostname = o_nflag ? remaddr : xmalloc_sockaddr2host(&remend.u.sa);
+               fprintf(stderr, "connect to %s from %s (%s)\n",
                                lcladdr, remhostname, remaddr);
                free(lcladdr);
                free(remaddr);
@@ -391,6 +465,7 @@ static void dolisten(void)
  Use the time delay between writes if given, otherwise use the "tcp ping"
  trick for getting the RTT.  [I got that idea from pluvius, and warped it.]
  Return either the original fd, or clean up and return -1. */
+#if ENABLE_NC_EXTRA
 static int udptest(void)
 {
        int rr;
@@ -400,7 +475,7 @@ static int udptest(void)
                bb_perror_msg("udptest first write");
 
        if (o_wait)
-               sleep(o_wait);
+               sleep(o_wait); // can be interrupted! while (t) nanosleep(&t)?
        else {
        /* use the tcp-ping trick: try connecting to a normally refused port, which
         causes us to block for the time that SYN gets there and RST gets back.
@@ -408,17 +483,20 @@ static int udptest(void)
        /* Set a temporary connect timeout, so packet filtration doesnt cause
         us to hang forever, and hit it */
                o_wait = 5;                     /* enough that we'll notice?? */
-               rr = xsocket(ouraddr->sa.sa_family, SOCK_STREAM, 0);
-               set_nport(themaddr, htons(SLEAZE_PORT));
+               rr = xsocket(ouraddr->u.sa.sa_family, SOCK_STREAM, 0);
+               set_nport(&themaddr->u.sa, htons(SLEAZE_PORT));
                connect_w_timeout(rr);
-//need to restore port?
+               /* don't need to restore themaddr's port, it's not used anymore */
                close(rr);
-               o_wait = 0;                     /* reset it */
+               o_wait = 0; /* restore */
        }
 
        rr = write(netfd, bigbuf_in, 1);
        return (rr != 1); /* if rr == 1, return 0 (success) */
 }
+#else
+int udptest(void);
+#endif
 
 /* oprint:
  Hexdump bytes shoveled either way to a running logfile, in the format:
@@ -430,73 +508,56 @@ static int udptest(void)
  a partial line, so be it; we *want* that lockstep indication of who sent
  what when.  Adapted from dgaudet's original example -- but must be ripping
  *fast*, since we don't want to be too disk-bound... */
-static void oprint(int which, char *buf, int n)
+#if ENABLE_NC_EXTRA
+static void oprint(int direction, unsigned char *p, unsigned bc)
 {
-       int bc;                 /* in buffer count */
-       int obc;                /* current "global" offset */
-       int soc;                /* stage write count */
-       unsigned char *p;       /* main buf ptr; m.b. unsigned here */
+       unsigned obc;           /* current "global" offset */
+       unsigned x;
        unsigned char *op;      /* out hexdump ptr */
-       unsigned char *a      /* out asc-dump ptr */
-       int x;
+       unsigned char *ap;      /* out asc-dump ptr */
+       unsigned char stage[100];
 
-       if (n == 0)
+       if (bc == 0)
                return;
 
-       op = stage;
-       if (which) {
-               *op = '<';
-               obc = wrote_out;                /* use the globals! */
-       } else {
-               *op = '>';
-               obc = wrote_net;
-       }
-       op++;                                /* preload "direction" */
-       *op = ' ';
-       p = (unsigned char *) buf;
-       bc = n;
-       stage[59] = '#';                /* preload separator */
+       obc = wrote_net; /* use the globals! */
+       if (direction == '<')
+               obc = wrote_out;
+       stage[0] = direction;
+       stage[59] = '#'; /* preload separator */
        stage[60] = ' ';
 
-       while (bc) {                        /* for chunk-o-data ... */
+       do {    /* for chunk-o-data ... */
                x = 16;
-               soc = 78;                        /* len of whole formatted line */
-               if (bc < x) {
-                       soc = soc - 16 + bc;        /* fiddle for however much is left */
-                       x = (bc * 3) + 11;        /* 2 digits + space per, after D & offset */
-                       op = &stage[x];
-                       x = 16 - bc;
-                       while (x) {
-                               *op++ = ' ';                /* preload filler spaces */
-                               *op++ = ' ';
-                               *op++ = ' ';
-                               x--;
-                       }
-                       x = bc;                        /* re-fix current linecount */
-               } /* if bc < x */
-
-               bc -= x;                        /* fix wrt current line size */
-               sprintf(&stage[2], "%8.8x ", obc);                /* xxx: still slow? */
-               obc += x;                        /* fix current offset */
-               op = &stage[11];                /* where hex starts */
-               a = &stage[61];                /* where ascii starts */
+               if (bc < 16) {
+                       /* memset(&stage[bc*3 + 11], ' ', 16*3 - bc*3); */
+                       memset(&stage[11], ' ', 16*3);
+                       x = bc;
+               }
+               sprintf((char *)&stage[1], " %8.8x ", obc);  /* xxx: still slow? */
+               bc -= x;          /* fix current count */
+               obc += x;         /* fix current offset */
+               op = &stage[11];  /* where hex starts */
+               ap = &stage[61];  /* where ascii starts */
 
-               while (x) {  /* for line of dump, however long ... */
+               do {  /* for line of dump, however long ... */
                        *op++ = 0x20 | bb_hexdigits_upcase[*p >> 4];
                        *op++ = 0x20 | bb_hexdigits_upcase[*p & 0x0f];
                        *op++ = ' ';
                        if ((*p > 31) && (*p < 127))
-                               *a = *p;                /* printing */
+                               *ap = *p;   /* printing */
                        else
-                               *a = '.';                /* nonprinting, loose def */
-                       a++;
+                               *ap = '.';  /* nonprinting, loose def */
+                       ap++;
                        p++;
-                       x--;
-               } /* while x */
-               *a = '\n';                        /* finish the line */
-               xwrite(ofd, stage, soc);
-       } /* while bc */
+               } while (--x);
+               *ap++ = '\n';  /* finish the line */
+               xwrite(ofd, stage, ap - stage);
+       } while (bc);
 }
+#else
+void oprint(int direction, unsigned char *p, unsigned bc);
+#endif
 
 /* readwrite:
  handle stdin/stdout/network I/O.  Bwahaha!! -- the select loop from hell.
@@ -536,9 +597,10 @@ static int readwrite(void)
                        struct timeval tmp_timer;
                        tmp_timer.tv_sec = o_wait;
                        tmp_timer.tv_usec = 0;
-                       rr = select(16, &ding2, NULL, NULL, &tmp_timer);
+               /* highest possible fd is netfd (3) */
+                       rr = select(netfd+1, &ding2, NULL, NULL, &tmp_timer);
                } else
-                       rr = select(16, &ding2, NULL, NULL, NULL);
+                       rr = select(netfd+1, &ding2, NULL, NULL, NULL);
                if (rr < 0 && errno != EINTR) {                /* might have gotten ^Zed, etc */
                        holler_perror("select");
                        close(netfd);
@@ -547,7 +609,7 @@ static int readwrite(void)
        /* if we have a timeout AND stdin is closed AND we haven't heard anything
         from the net during that time, assume it's dead and close it too. */
                if (rr == 0) {
-                       if (!FD_ISSET(0, &ding1))
+                       if (!FD_ISSET(STDIN_FILENO, &ding1))
                                netretry--;                        /* we actually try a coupla times. */
                        if (!netretry) {
                                if (o_verbose > 1)                /* normally we don't care */
@@ -563,13 +625,17 @@ static int readwrite(void)
                if (FD_ISSET(netfd, &ding2)) {                /* net: ding! */
                        rr = read(netfd, bigbuf_net, BIGSIZ);
                        if (rr <= 0) {
+                               if (rr < 0 && o_verbose > 1) {
+                                       /* nc 1.10 doesn't do this */
+                                       bb_perror_msg("net read");
+                               }
                                FD_CLR(netfd, &ding1);                /* net closed, we'll finish up... */
                                rzleft = 0;                        /* can't write anymore: broken pipe */
                        } else {
                                rnleft = rr;
                                np = bigbuf_net;
                        }
-Debug(("got %d from the net, errno %d", rr, errno))
+Debug("got %d from the net, errno %d", rr, errno);
                } /* net:ding */
 
        /* if we're in "slowly" mode there's probably still stuff in the stdin
@@ -578,13 +644,16 @@ Debug(("got %d from the net, errno %d", rr, errno))
                        goto shovel;
 
        /* okay, suck more stdin */
-               if (FD_ISSET(0, &ding2)) {                /* stdin: ding! */
-                       rr = read(0, bigbuf_in, BIGSIZ);
+               if (FD_ISSET(STDIN_FILENO, &ding2)) {                /* stdin: ding! */
+                       rr = read(STDIN_FILENO, bigbuf_in, BIGSIZ);
        /* Considered making reads here smaller for UDP mode, but 8192-byte
         mobygrams are kinda fun and exercise the reassembler. */
                        if (rr <= 0) {                        /* at end, or fukt, or ... */
-                               FD_CLR(0, &ding1);                /* disable and close stdin */
-                               close(0);
+                               FD_CLR(STDIN_FILENO, &ding1);                /* disable and close stdin */
+                               close(STDIN_FILENO);
+// Does it make sense to shutdown(net_fd, SHUT_WR)
+// to let other side know that we won't write anything anymore?
+// (and what about keeping compat if we do that?)
                        } else {
                                rzleft = rr;
                                zp = bigbuf_in;
@@ -606,15 +675,15 @@ Debug(("got %d from the net, errno %d", rr, errno))
                        return 1;
                }
                if (rnleft) {
-                       rr = write(1, np, rnleft);
+                       rr = write(STDOUT_FILENO, np, rnleft);
                        if (rr > 0) {
-                               if (o_wfile)
-                                       oprint(1, np, rr);                /* log the stdout */
+                               if (o_ofile) /* log the stdout */
+                                       oprint('<', (unsigned char *)np, rr);
                                np += rr;                        /* fix up ptrs and whatnot */
                                rnleft -= rr;                        /* will get sanity-checked above */
                                wrote_out += rr;                /* global count */
                        }
-Debug(("wrote %d to stdout, errno %d", rr, errno))
+Debug("wrote %d to stdout, errno %d", rr, errno);
                } /* rnleft */
                if (rzleft) {
                        if (o_interval)                        /* in "slowly" mode ?? */
@@ -623,13 +692,13 @@ Debug(("wrote %d to stdout, errno %d", rr, errno))
                                rr = rzleft;
                        rr = write(netfd, zp, rr);        /* one line, or the whole buffer */
                        if (rr > 0) {
-                               if (o_wfile)
-                                       oprint(0, zp, rr);                /* log what got sent */
+                               if (o_ofile) /* log what got sent */
+                                       oprint('>', (unsigned char *)zp, rr);
                                zp += rr;
                                rzleft -= rr;
                                wrote_net += rr;                /* global count */
                        }
-Debug(("wrote %d to net, errno %d", rr, errno))
+Debug("wrote %d to net, errno %d", rr, errno);
                } /* rzleft */
                if (o_interval) {                        /* cycle between slow lines, or ... */
                        sleep(o_interval);
@@ -652,48 +721,49 @@ Debug(("wrote %d to net, errno %d", rr, errno))
 } /* readwrite */
 
 /* main: now we pull it all together... */
-int nc_main(int argc, char **argv);
-int nc_main(int argc, char **argv)
+int nc_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int nc_main(int argc UNUSED_PARAM, char **argv)
 {
-       char *str_p, *str_s, *str_w;
-       USE_NC_EXTRA(char *str_i;)
+       char *str_p, *str_s;
+       IF_NC_EXTRA(char *str_i, *str_o;)
        char *themdotted = themdotted; /* gcc */
        char **proggie;
        int x;
        unsigned o_lport = 0;
 
-       /* I was in this barbershop quartet in Skokie IL ... */
-       /* round up the usual suspects, i.e. malloc up all the stuff we need */
-       PTR_TO_GLOBALS = xzalloc(sizeof(G));
+       INIT_G();
 
        /* catch a signal or two for cleanup */
-       signal(SIGINT, catch);
-       signal(SIGQUIT, catch);
-       signal(SIGTERM, catch);
+       bb_signals(0
+               + (1 << SIGINT)
+               + (1 << SIGQUIT)
+               + (1 << SIGTERM)
+               , catch);
        /* and suppress others... */
+       bb_signals(0
 #ifdef SIGURG
-       signal(SIGURG, SIG_IGN);
+               + (1 << SIGURG)
 #endif
-       signal(SIGPIPE, SIG_IGN); /* important! */
+               + (1 << SIGPIPE) /* important! */
+               , SIG_IGN);
 
        proggie = argv;
        while (*++proggie) {
                if (strcmp(*proggie, "-e") == 0) {
                        *proggie = NULL;
-                       argc = proggie - argv;
                        proggie++;
                        goto e_found;
                }
        }
        proggie = NULL;
  e_found:
-               
+
        // -g -G -t -r deleted, unimplemented -a deleted too
-       opt_complementary = "?2:vv"; /* max 2 params, -v is a counter */
-       getopt32(argc, argv, "hnp:s:uvw:" USE_NC_SERVER("l")
-                       USE_NC_EXTRA("i:o:z"),
-                       &str_p, &str_s, &str_w
-                       USE_NC_EXTRA(, &str_i, &stage, &o_verbose));
+       opt_complementary = "?2:vv:w+"; /* max 2 params; -v is a counter; -w N */
+       getopt32(argv, "hnp:s:uvw:" IF_NC_SERVER("l")
+                       IF_NC_EXTRA("i:o:z"),
+                       &str_p, &str_s, &o_wait
+                       IF_NC_EXTRA(, &str_i, &str_o), &o_verbose);
        argv += optind;
 #if ENABLE_NC_EXTRA
        if (option_mask32 & OPT_i) /* line-interval time */
@@ -710,55 +780,72 @@ int nc_main(int argc, char **argv)
        //if (option_mask32 & OPT_r) /* randomize various things */
        //if (option_mask32 & OPT_u) /* use UDP */
        //if (option_mask32 & OPT_v) /* verbose */
-       if (option_mask32 & OPT_w) { /* wait time */
-               o_wait = xatoi_u(str_w);
-       }
+       //if (option_mask32 & OPT_w) /* wait time */
        //if (option_mask32 & OPT_z) /* little or no data xfer */
 
-       bb_sanitize_stdio();
+       /* We manage our fd's so that they are never 0,1,2 */
+       /*bb_sanitize_stdio(); - not needed */
+
+       if (argv[0]) {
+               themaddr = xhost2sockaddr(argv[0],
+                       argv[1]
+                       ? bb_lookup_port(argv[1], o_udpmode ? "udp" : "tcp", 0)
+                       : 0);
+       }
 
        /* create & bind network socket */
+       x = (o_udpmode ? SOCK_DGRAM : SOCK_STREAM);
        if (option_mask32 & OPT_s) { /* local address */
-               /* if o_port is still 0, then we will use random port */
+               /* if o_lport is still 0, then we will use random port */
                ouraddr = xhost2sockaddr(str_s, o_lport);
-               netfd = xsocket(ouraddr->sa.sa_family, o_udpmode ? SOCK_DGRAM : SOCK_STREAM, 0); //// 0?
+#ifdef BLOAT
+               /* prevent spurious "UDP listen needs !0 port" */
+               o_lport = get_nport(ouraddr);
+               o_lport = ntohs(o_lport);
+#endif
+               x = xsocket(ouraddr->u.sa.sa_family, x, 0);
        } else {
-               netfd = xsocket_type(&ouraddr, o_udpmode ? SOCK_DGRAM : SOCK_STREAM);
+               /* We try IPv6, then IPv4, unless addr family is
+                * implicitly set by way of remote addr/port spec */
+               x = xsocket_type(&ouraddr,
+                               (themaddr ? themaddr->u.sa.sa_family : AF_UNSPEC),
+                               x);
                if (o_lport)
-                       set_nport(ouraddr, htons(o_lport));
+                       set_nport(&ouraddr->u.sa, htons(o_lport));
        }
+       xmove_fd(x, netfd);
        setsockopt_reuseaddr(netfd);
        if (o_udpmode)
                socket_want_pktinfo(netfd);
-       xbind(netfd, &ouraddr->sa, ouraddr->len);
+       if (!ENABLE_FEATURE_UNIX_LOCAL
+        || o_listen
+        || ouraddr->u.sa.sa_family != AF_UNIX
+       ) {
+               xbind(netfd, &ouraddr->u.sa, ouraddr->len);
+       }
 #if 0
        setsockopt(netfd, SOL_SOCKET, SO_RCVBUF, &o_rcvbuf, sizeof o_rcvbuf);
        setsockopt(netfd, SOL_SOCKET, SO_SNDBUF, &o_sndbuf, sizeof o_sndbuf);
 #endif
 
-       if (o_udpmode) {      /* apparently UDP can listen ON */
-               if (!o_lport) /* "port 0",  but that's not useful */
-                       bb_error_msg_and_die("UDP listen needs -p arg");
+#ifdef BLOAT
+       if (OPT_l && (option_mask32 & (OPT_u|OPT_l)) == (OPT_u|OPT_l)) {
+               /* apparently UDP can listen ON "port 0",
+                but that's not useful */
+               if (!o_lport)
+                       bb_error_msg_and_die("UDP listen needs nonzero -p port");
        }
+#endif
 
-       FD_SET(0, &ding1);                        /* stdin *is* initially open */
+       FD_SET(STDIN_FILENO, &ding1);                        /* stdin *is* initially open */
        if (proggie) {
                close(0); /* won't need stdin */
                option_mask32 &= ~OPT_o; /* -o with -e is meaningless! */
-               ofd = 0;
-       }
-       if (o_wfile) {
-               ofd = xopen(stage, O_WRONLY|O_CREAT|O_TRUNC);
-               stage = xzalloc(100);
-       }
-
-       if (argv[0]) {
-               themaddr = xhost2sockaddr(argv[0],
-                       argv[1]
-                       ? bb_lookup_port(argv[1], o_udpmode ? "udp" : "tcp", 0)
-                       : 0);
-///what if sa_family won't match??
        }
+#if ENABLE_NC_EXTRA
+       if (o_ofile)
+               xmove_fd(xopen(str_o, O_WRONLY|O_CREAT|O_TRUNC), ofd);
+#endif
 
        if (o_listen) {
                dolisten();
@@ -769,18 +856,18 @@ int nc_main(int argc, char **argv)
        } else {
                /* Outbound connects.  Now we're more picky about args... */
                if (!themaddr)
-                       bb_error_msg_and_die("no destination");
+                       bb_show_usage();
 
                remend = *themaddr;
                if (o_verbose)
-                       themdotted = xmalloc_sockaddr2dotted(&themaddr->sa, themaddr->len);
+                       themdotted = xmalloc_sockaddr2dotted(&themaddr->u.sa);
 
                x = connect_w_timeout(netfd);
                if (o_zero && x == 0 && o_udpmode)        /* if UDP scanning... */
                        x = udptest();
                if (x == 0) {                        /* Yow, are we OPEN YET?! */
                        if (o_verbose)
-                               fprintf(stderr, "%s [%s] open\n", argv[0], themdotted);
+                               fprintf(stderr, "%s (%s) open\n", argv[0], themdotted);
                        if (proggie)                        /* exec is valid for outbound, too */
                                doexec(proggie);
                        if (!o_zero)
@@ -790,7 +877,7 @@ int nc_main(int argc, char **argv)
                        /* if we're scanning at a "one -v" verbosity level, don't print refusals.
                         Give it another -v if you want to see everything. */
                        if (o_verbose > 1 || (o_verbose && errno != ECONNREFUSED))
-                               bb_perror_msg("%s [%s]", argv[0], themdotted);
+                               bb_perror_msg("%s (%s)", argv[0], themdotted);
                }
        }
        if (o_verbose > 1)                /* normally we don't care */