home *** CD-ROM | disk | FTP | other *** search
/ Hacks & Cracks / Hacks_and_Cracks.iso / hackersguides-&-software / ncnt090.zip / netcat.c < prev    next >
C/C++ Source or Header  |  1996-07-26  |  59KB  |  1,710 lines

  1. /* Netcat 1.00 951010
  2.  
  3.    A damn useful little "backend" utility begun 950915 or thereabouts,
  4.    as *Hobbit*'s first real stab at some sockets programming.  Something that
  5.    should have and indeed may have existed ten years ago, but never became a
  6.    standard Unix utility.  IMHO, "nc" could take its place right next to cat,
  7.    cp, rm, mv, dd, ls, and all those other cryptic and Unix-like things.
  8.  
  9.    Read the README for the whole story, doc, applications, etc.
  10.  
  11.    Layout:
  12.     conditional includes:
  13.     includes:
  14.     handy defines:
  15.     globals:
  16.     malloced globals:
  17.     cmd-flag globals:
  18.     support routines:
  19.     main:
  20.  
  21.   todo:
  22.     more of the portability swamp, and an updated generic.h
  23.     frontend progs to generate various packets, raw or otherwise...
  24.     char-mode [cbreak, fcntl-unbuffered, etc...]
  25.     connect-to-all-A-records hack
  26.   bluesky:
  27.     RAW mode!
  28.     backend progs to grab a pty and look like a real telnetd?!
  29. */
  30.  
  31. #include "generic.h"        /* same as with L5, skey, etc */
  32.  
  33. /* conditional includes -- a very messy section: */
  34. /* #undef _POSIX_SOURCE        /* might need this for something? */
  35. #define HAVE_BIND        /* XXX -- for now, see below... */
  36. #define HAVE_HELP        /* undefine if you dont want the help text */
  37. /* #define ANAL            /* if you want case-sensitive DNS matching */
  38. #ifdef HAVE_STDLIB_H
  39. #include <stdlib.h>
  40. #else
  41. #include <malloc.h>        /* xxx: or does it live in sys/ ?? */
  42. #endif
  43.  
  44. /* have to do this *before* including types.h. xxx: Linux still has it wrong */
  45. #ifdef FD_SETSIZE        /* should be in types.h, butcha never know. */
  46. #undef FD_SETSIZE        /* if we ever need more than 16 active */
  47. #endif                /* fd's, something is horribly wrong! */
  48. #ifdef WIN32
  49. #define FD_SETSIZE 64        /* WIN32 does this as an array not a bitfield and it likes 64 */
  50. #else
  51. #define FD_SETSIZE 16        /* <-- this'll give us a long anyways, wtf */
  52. #endif
  53. #include <sys/types.h>        /* *now* do it.  Sigh, this is broken */
  54.  
  55.  
  56. #ifdef WIN32
  57. #undef HAVE_RANDOM
  58. #undef IP_OPTIONS
  59. #undef SO_REUSEPORT
  60. #endif
  61.  
  62.  
  63. #ifdef HAVE_RANDOM
  64. #define SRAND srandom
  65. #define RAND random
  66. #else
  67. #define SRAND srand
  68. #define RAND rand
  69. #endif /* HAVE_RANDOM */
  70.  
  71. /* xxx: these are rsh leftovers, move to new generic.h */
  72. /* will we even need any nonblocking shit?  Doubt it. */
  73. /* get FIONBIO from sys/filio.h, so what if it is a compatibility feature */
  74. /* #include <sys/filio.h> */
  75. /*
  76. #include <sys/ioctl.h>
  77. #include <sys/file.h>
  78. */
  79.  
  80. /* includes: */
  81.  
  82. #ifdef WIN32
  83. #include "getopt.h"
  84. #define sleep            _sleep
  85. #define strcasecmp        strcmpi
  86. #define EADDRINUSE        WSAEADDRINUSE
  87. #define ETIMEDOUT        WSAETIMEDOUT
  88. #define ECONNREFUSED    WSAECONNREFUSED
  89. #endif
  90.  
  91. #ifndef WIN32
  92. #include <sys/time.h>        /* timeval, time_t */
  93. #else
  94. #include <time.h>
  95. #endif
  96.  
  97. #include <setjmp.h>        /* jmp_buf et al */
  98.  
  99. #ifndef WIN32
  100. #include <sys/socket.h>        /* basics, SO_ and AF_ defs, sockaddr, ... */
  101. #include <netinet/in.h>        /* sockaddr_in, htons, in_addr */
  102. #include <netinet/in_systm.h>    /* misc crud that netinet/ip.h references */
  103. #include <netinet/ip.h>        /* IPOPT_LSRR, header stuff */
  104. #include <netdb.h>        /* hostent, gethostby*, getservby* */
  105. #include <arpa/inet.h>        /* inet_ntoa */
  106. #else
  107. #include <fcntl.h>
  108. #include <io.h>
  109. #include <conio.h>
  110. #include <winsock.h>
  111. #endif
  112.  
  113. #include <stdio.h>
  114. #include <string.h>        /* strcpy, strchr, yadda yadda */
  115. #include <errno.h>
  116. #include <signal.h>
  117.  
  118. /* handy stuff: */
  119. #define SA struct sockaddr    /* socket overgeneralization braindeath */
  120. #define SAI struct sockaddr_in    /* ... whoever came up with this model */
  121. #define IA struct in_addr    /* ... should be taken out and shot, */
  122.                 /* ... not that TLI is any better.  sigh.. */
  123. #define SLEAZE_PORT 31337    /* for UDP-scan RTT trick, change if ya want */
  124. #define USHORT unsigned short    /* use these for options an' stuff */
  125. #define BIGSIZ 8192        /* big buffers */
  126. #define SMALLSIZ 256        /* small buffers, hostnames, etc */
  127.  
  128. #ifndef INADDR_NONE
  129. #define INADDR_NONE 0xffffffff
  130. #endif
  131. #ifdef MAXHOSTNAMELEN
  132. #undef MAXHOSTNAMELEN        /* might be too small on aix, so fix it */
  133. #endif
  134. #define MAXHOSTNAMELEN 256
  135. struct host_poop {
  136.   char name[MAXHOSTNAMELEN];    /* dns name */
  137.   char addrs[8][24];        /* ascii-format IP addresses */
  138.   struct in_addr iaddrs[8];    /* real addresses: in_addr.s_addr: ulong */
  139. };
  140. #define HINF struct host_poop
  141. struct port_poop {
  142.   char name [64];        /* name in /etc/services */
  143.   char anum [8];        /* ascii-format number */
  144.   USHORT num;            /* real host-order number */
  145. };
  146. #define PINF struct port_poop
  147.  
  148. /* globals: */
  149. jmp_buf jbuf;            /* timer crud */
  150. int jval = 0;            /* timer crud */
  151. int netfd = -1;
  152. static char unknown[] = "(UNKNOWN)";
  153. static char p_tcp[] = "tcp";    /* for getservby* */
  154. static char p_udp[] = "udp";
  155.  
  156. #ifndef WIN32
  157. #ifdef HAVE_BIND
  158. extern int h_errno;
  159. #endif
  160. #endif
  161. int gatesidx = 0;        /* LSRR hop count */
  162. int gatesptr = 4;        /* initial LSRR pointer, settable */
  163. USHORT Single = 1;        /* zero if scanning */
  164. unsigned int insaved = 0;    /* stdin-buffer size for multi-mode */
  165. unsigned int wrote_out = 0;    /* total stdout bytes */
  166. unsigned int wrote_net = 0;    /* total net bytes */
  167.  
  168. /* will malloc up the following globals: */
  169. struct timeval * timer1 = NULL;
  170. struct timeval * timer2 = NULL;
  171. SAI * lclend = NULL;        /* sockaddr_in structs */
  172. SAI * remend = NULL;
  173. HINF ** gates = NULL;        /* LSRR hop hostpoop */
  174. char * optbuf = NULL;        /* LSRR or sockopts */
  175. char * bigbuf_in;        /* data buffers */
  176. char * bigbuf_net;
  177. fd_set * ding1;            /* for select loop */
  178. fd_set * ding2;
  179. PINF * portpoop = NULL;        /* for getportpoop / getservby* */
  180.  
  181. #ifdef WIN32
  182.   char * setsockopt_c;
  183. #endif
  184.  
  185. /* global cmd flags: */
  186. USHORT o_alla = 0;
  187. unsigned int o_interval = 0;
  188. USHORT o_listen = 0;
  189. USHORT o_nflag = 0;
  190. USHORT o_random = 0;
  191. USHORT o_udpmode = 0;
  192. USHORT o_verbose = 0;
  193. unsigned int o_wait = 0;
  194. USHORT o_zero = 0;
  195.  
  196. /* Debug macro: squirt whatever to stderr and sleep a bit so we can see it go
  197.    by.  need to call like Debug ((stuff)) [with no ; ] so macro args match!
  198.    Beware: writes to stdOUT... */
  199. #ifdef DEBUG
  200. #define Debug(x) printf x; printf ("\n"); fflush (stdout); sleep (1);
  201. #else
  202. #define Debug(x)    /* nil... */
  203. #endif
  204.  
  205. /* support routines -- the bulk of this thing.  Placed in such an order that
  206.    we don't have to forward-declare anything: */
  207.  
  208.  
  209. #ifdef WIN32
  210.  
  211. /* res_init
  212.    winsock needs to be initialized. Might as well do it as the res_init
  213.    call for Win32 */
  214.  
  215. void res_init()
  216. {
  217. WORD wVersionRequested; 
  218. WSADATA wsaData; 
  219. int err; 
  220. wVersionRequested = MAKEWORD(1, 1); 
  221.  
  222. err = WSAStartup(wVersionRequested, &wsaData); 
  223.  
  224. if (err != 0) 
  225.     /* Tell the user that we couldn't find a useable */ 
  226.     /* winsock.dll.     */ 
  227.     return; 
  228.  
  229. /* Confirm that the Windows Sockets DLL supports 1.1.*/ 
  230. /* Note that if the DLL supports versions greater */ 
  231. /* than 1.1 in addition to 1.1, it will still return */ 
  232. /* 1.1 in wVersion since that is the version we */ 
  233. /* requested. */ 
  234.  
  235. if ( LOBYTE( wsaData.wVersion ) != 1 || 
  236.         HIBYTE( wsaData.wVersion ) != 1 ) { 
  237.     /* Tell the user that we couldn't find a useable */ 
  238.     /* winsock.dll. */ 
  239.     WSACleanup(); 
  240.     return; 
  241.     }
  242.  
  243. }
  244.  
  245.  
  246.  
  247.  
  248. /* winsockstr
  249.    Windows Sockets cannot report errors through perror() so we need to define
  250.    our own error strings to print. Someday all the string should be prettied up.
  251.    Prettied the errors I usually get */
  252. char * winsockstr(error)
  253. int error;
  254. {
  255.     switch (error)
  256.     {
  257.     case WSAEINTR          : return("INTR          ");
  258.     case WSAEBADF          : return("BADF          ");
  259.     case WSAEACCES         : return("ACCES         ");
  260.     case WSAEFAULT         : return("FAULT         ");
  261.     case WSAEINVAL         : return("INVAL         ");
  262.     case WSAEMFILE         : return("MFILE         ");
  263.     case WSAEWOULDBLOCK    : return("WOULDBLOCK    ");
  264.     case WSAEINPROGRESS    : return("INPROGRESS    ");
  265.     case WSAEALREADY       : return("ALREADY       ");
  266.     case WSAENOTSOCK       : return("NOTSOCK       ");
  267.     case WSAEDESTADDRREQ   : return("DESTADDRREQ   ");
  268.     case WSAEMSGSIZE       : return("MSGSIZE       ");
  269.     case WSAEPROTOTYPE     : return("PROTOTYPE     ");
  270.     case WSAENOPROTOOPT    : return("NOPROTOOPT    ");
  271.     case WSAEPROTONOSUPPORT: return("PROTONOSUPPORT");
  272.     case WSAESOCKTNOSUPPORT: return("SOCKTNOSUPPORT");
  273.     case WSAEOPNOTSUPP     : return("OPNOTSUPP     ");
  274.     case WSAEPFNOSUPPORT   : return("PFNOSUPPORT   ");
  275.     case WSAEAFNOSUPPORT   : return("AFNOSUPPORT   ");
  276.     case WSAEADDRINUSE     : return("ADDRINUSE     ");
  277.     case WSAEADDRNOTAVAIL  : return("ADDRNOTAVAIL  ");
  278.     case WSAENETDOWN       : return("NETDOWN       ");
  279.     case WSAENETUNREACH    : return("NETUNREACH    ");
  280.     case WSAENETRESET      : return("NETRESET      ");
  281.     case WSAECONNABORTED   : return("CONNABORTED   ");
  282.     case WSAECONNRESET     : return("CONNRESET     ");
  283.     case WSAENOBUFS        : return("NOBUFS        ");
  284.     case WSAEISCONN        : return("ISCONN        ");
  285.     case WSAENOTCONN       : return("NOTCONN       ");
  286.     case WSAESHUTDOWN      : return("SHUTDOWN      ");
  287.     case WSAETOOMANYREFS   : return("TOOMANYREFS   ");
  288.     case WSAETIMEDOUT      : return("TIMEDOUT      ");
  289.     case WSAECONNREFUSED   : return("connection refused");
  290.     case WSAELOOP          : return("LOOP          ");
  291.     case WSAENAMETOOLONG   : return("NAMETOOLONG   ");
  292.     case WSAEHOSTDOWN      : return("HOSTDOWN      ");
  293.     case WSAEHOSTUNREACH   : return("HOSTUNREACH   ");
  294.     case WSAENOTEMPTY      : return("NOTEMPTY      ");
  295.     case WSAEPROCLIM       : return("PROCLIM       ");
  296.     case WSAEUSERS         : return("USERS         ");
  297.     case WSAEDQUOT         : return("DQUOT         ");
  298.     case WSAESTALE         : return("STALE         ");
  299.     case WSAEREMOTE        : return("REMOTE        ");
  300.     case WSAEDISCON        : return("DISCON        ");
  301.     case WSASYSNOTREADY    : return("SYSNOTREADY    ");
  302.     case WSAVERNOTSUPPORTED: return("VERNOTSUPPORTED");
  303.     case WSANOTINITIALISED : return("NOTINITIALISED ");
  304.     case WSAHOST_NOT_FOUND : return("HOST_NOT_FOUND ");
  305.     case WSATRY_AGAIN      : return("TRY_AGAIN      ");
  306.     case WSANO_RECOVERY    : return("NO_RECOVERY    ");
  307.     case WSANO_DATA        : return("NO_DATA        ");
  308.     default : return("unknown socket error");
  309.     }
  310. }
  311. #endif
  312.  
  313.  
  314.  
  315.  
  316.  
  317. /* holler :
  318.    fake varargs -- need to do this way because we wind up calling through
  319.    more levels of indirection than vanilla varargs can handle, and not all
  320.    machines have vfprintf/vsyslog/whatever!  6 params oughta be enough. */
  321. void holler (str, p1, p2, p3, p4, p5, p6)
  322.   char * str;
  323.   char * p1, * p2, * p3, * p4, * p5, * p6;
  324. {
  325.   if (o_verbose) {
  326.     fprintf (stderr, str, p1, p2, p3, p4, p5, p6);
  327. #ifdef WIN32
  328.     if (h_errno)
  329.         fprintf (stderr, ": %s\n",winsockstr(h_errno));
  330. #else
  331.     if (errno) {        /* this gives funny-looking messages, but */
  332.       perror (" ");        /* it's more portable than sys_errlist[]... */
  333.     }                /* xxx: do something better.  */
  334. #endif
  335.     else
  336.       fprintf (stderr, "\n");
  337.     fflush (stderr);
  338.   }
  339. } /* holler */
  340.  
  341. /* bail :
  342.    error-exit handler, callable from anywhere */
  343. void bail (str, p1, p2, p3, p4, p5, p6)
  344.   char * str;
  345.   char * p1, * p2, * p3, * p4, * p5, * p6;
  346. {
  347.   o_verbose = 1;
  348.   holler (str, p1, p2, p3, p4, p5, p6);
  349. #ifdef WIN32
  350.   closesocket (netfd);
  351. #else
  352.   close (netfd);
  353. #endif
  354.   sleep (1);
  355.   exit (1);
  356. } /* bail */
  357.  
  358. /* catch :
  359.    no-brainer interrupt handler */
  360. void catch ()
  361. {
  362.   errno = 0;
  363.   bail (" punt!");
  364. }
  365.  
  366. /* timeout and other signal handling cruft */
  367. void tmtravel ()
  368. {
  369. #ifdef NTFIXTHIS
  370.   signal (SIGALRM, SIG_IGN);
  371.   alarm (0);
  372. #endif
  373.   if (jval == 0)
  374.     bail ("spurious timer interrupt!");
  375.   longjmp (jbuf, jval);
  376. }
  377.  
  378. /* arm :
  379.    set the timer.  Zero secs arg means unarm */
  380. void arm (num, secs)
  381.   unsigned int num;
  382.   unsigned int secs;
  383. {
  384.   if (secs == 0) {            /* reset */
  385. #ifdef NTFIXTHIS
  386.     signal (SIGALRM, SIG_IGN);
  387.     alarm (0);
  388. #endif
  389.     jval = 0;
  390.   } else {                /* set */
  391. #ifdef NTFIXTHIS
  392.     signal (SIGALRM, tmtravel);
  393.     alarm (secs);
  394. #endif
  395.     jval = num;
  396.   } /* if secs */
  397. } /* arm */
  398.  
  399. /* Hmalloc :
  400.    malloc up what I want, rounded up to *4, and pre-zeroed.  Either succeeds
  401.    or bails out on its own, so that callers don't have to worry about it. */
  402. char * Hmalloc (size)
  403.   unsigned int size;
  404. {
  405.   unsigned int s = (size + 4) & 0xfffffffc;    /* 4GB?! */
  406.   char * p = malloc (s);
  407.   if (p != NULL)
  408.     memset (p, 0, s);
  409.   else
  410.     bail ("Hmalloc %d failed", s);
  411.   return (p);
  412. } /* Hmalloc */
  413.  
  414. /* findline :
  415.    find the next newline in a buffer; return inclusive size of that "line",
  416.    or the entire buffer size, so the caller knows how much to then write().
  417.    Not distinguishing \n vs \r\n for the nonce; it just works as is... */
  418. unsigned int findline (buf, siz)
  419.   char * buf;
  420.   unsigned int siz;
  421. {
  422.   register char * p;
  423.   register int x;
  424.   if (! buf)            /* various sanity checks... */
  425.     return (0);
  426.   if (siz > BIGSIZ)
  427.     return (0);
  428.   x = siz;
  429.   for (p = buf; x > 0; x--) {
  430.     if (*p == '\n') {
  431.       x = (int) (p - buf);
  432.       x++;            /* 'sokay if it points just past the end! */
  433. Debug (("findline returning %d", x))
  434.       return (x);
  435.     }
  436.     p++;
  437.   } /* for */
  438. Debug (("findline returning whole thing: %d", siz))
  439.   return (siz);
  440. } /* findline */
  441.  
  442. /* comparehosts :
  443.    cross-check the host_poop we have so far against new gethostby*() info,
  444.    and holler about mismatches.  Perhaps gratuitous, but it can't hurt to
  445.    point out when someone's DNS is fukt.  Returns 1 if mismatch, in case
  446.    someone else wants to do something about it. */
  447. int comparehosts (poop, hp)
  448.   HINF * poop;
  449.   struct hostent * hp;
  450. {
  451.   errno = 0;
  452. /* The DNS spec is officially case-insensitive, but for those times when you
  453.    *really* wanna see any and all discrepancies, by all means define this. */
  454. #ifdef ANAL            
  455.   if (strcmp (poop->name, hp->h_name) != 0) {        /* case-sensitive */
  456. #else
  457.   if (strcasecmp (poop->name, hp->h_name) != 0) {    /* normal */
  458. #endif
  459.     holler ("DNS fwd/rev mismatch: %s != %s", poop->name, hp->h_name);
  460.     return (1);
  461.   }
  462.   return (0);
  463. /* ... do we need to do anything over and above that?? */
  464. } /* comparehosts */
  465.  
  466. /* gethostpoop :
  467.    resolve a host 8 ways from sunday; return a new host_poop struct with its
  468.    info.  The argument can be a name or [ascii] IP address; it will try its
  469.    damndest to deal with it.  "numeric" governs whether we do any DNS at all,
  470.    and we also check o_verbose for what's appropriate work to do. */
  471. HINF * gethostpoop (name, numeric)
  472.   char * name;
  473.   USHORT numeric;
  474. {
  475.   struct hostent * hostent;
  476.   struct in_addr iaddr;
  477.   register HINF * poop = NULL;
  478.   register int x;
  479.  
  480. /* I really want to strangle the twit who dreamed up all these sockaddr and
  481.    hostent abstractions, and then forced them all to be incompatible with
  482.    each other so you *HAVE* to do all this ridiculous casting back and forth.
  483.    If that wasn't bad enough, all the doc insists on referring to local ports
  484.    and addresses as "names", which makes NO sense down at the bare metal.
  485.  
  486.    What an absolutely horrid paradigm, and to think of all the people who
  487.    have been wasting significant amounts of time fighting with this stupid
  488.    deliberate obfuscation over the last 10 years... then again, I like
  489.    languages wherein a pointer is a pointer, what you put there is your own
  490.    business, the compiler stays out of your face, and sheep are nervous.
  491.    Maybe that's why my C code reads like assembler half the time... */
  492.  
  493. /* If we want to see all the DNS stuff, do the following hair --
  494.    if inet_addr, do reverse and forward with any warnings; otherwise try
  495.    to do forward and reverse with any warnings.  In other words, as long
  496.    as we're here, do a complete DNS check on these clowns.  Yes, it slows
  497.    things down a bit for a first run, but once it's cached, who cares? */
  498.  
  499.   errno = 0;
  500.   if (name)
  501.     poop = (HINF *) Hmalloc (sizeof (HINF));
  502.   if (! poop)
  503.     bail ("gethostpoop fuxored");
  504.   strcpy (poop->name, unknown);        /* preload it */
  505. /* see wzv:workarounds.c for dg/ux return-a-struct inet_addr lossage */
  506.   iaddr.s_addr = inet_addr (name);
  507.  
  508.   if (iaddr.s_addr == INADDR_NONE) {    /* here's the great split: names... */
  509.     if (numeric)
  510.       bail ("Can't parse %s as an IP address", name);
  511.     hostent = gethostbyname (name);
  512.     if (! hostent)
  513. /* failure to look up a name is fatal, since we can't do anything with it */
  514. /* XXX: h_errno only if BIND?  look up how telnet deals with this */
  515.       bail ("%s: forward host lookup failed: h_errno %d", name, h_errno);
  516.     strncpy (poop->name, hostent->h_name, sizeof (poop->name));
  517.     for (x = 0; hostent->h_addr_list[x] && (x < 8); x++) {
  518.       memcpy (&poop->iaddrs[x], hostent->h_addr_list[x], sizeof (IA));
  519.       strncpy (poop->addrs[x], inet_ntoa (poop->iaddrs[x]),
  520.     sizeof (poop->addrs[0]));
  521.     } /* for x -> addrs, part A */
  522.     if (! o_verbose)            /* if we didn't want to see the */
  523.       return (poop);            /* inverse stuff, we're done. */
  524. /* do inverse lookups in separate loop based on our collected forward addrs,
  525.    since gethostby* tends to crap into the same buffer over and over */
  526.     for (x = 0; poop->iaddrs[x].s_addr && (x < 8); x++) {
  527.       hostent = gethostbyaddr ((char *)&poop->iaddrs[x],
  528.                 sizeof (IA), AF_INET);
  529.       if ((! hostent) || (! hostent-> h_name))
  530.     holler ("Warning: inverse host lookup failed for %s: h_errno %d",
  531.       poop->addrs[x], h_errno);
  532.       else
  533.     (void) comparehosts (poop, hostent);
  534.     } /* for x -> addrs, part B */
  535.  
  536.   } else {            /* not INADDR_NONE: numeric addresses... */
  537.     memcpy (poop->iaddrs, &iaddr, sizeof (IA));
  538.     strncpy (poop->addrs[0], inet_ntoa (iaddr), sizeof (poop->addrs));
  539.     if (numeric)            /* if numeric-only, we're done */
  540.       return (poop);
  541.     if (! o_verbose)            /* likewise if we don't want */
  542.       return (poop);            /* the full DNS hair */
  543.     hostent = gethostbyaddr ((char *) &iaddr, sizeof (IA), AF_INET);
  544. /* numeric or not, failure to look up a PTR is *not* considered fatal */
  545.     if (! hostent)
  546.     holler ("%s: inverse host lookup failed: h_errno %d", name, h_errno);
  547.     else {
  548.     strncpy (poop->name, hostent->h_name, MAXHOSTNAMELEN - 2);
  549.     hostent = gethostbyname (poop->name);
  550.     if ((! hostent) || (! hostent->h_addr_list[0]))
  551.       holler ("Warning: forward host lookup failed for %s: h_errno %d",
  552.         poop->name, h_errno);
  553.     else
  554.       (void) comparehosts (poop, hostent);
  555.     } /* if hostent */
  556.   } /* INADDR_NONE Great Split */
  557.  
  558. /* whatever-all went down previously, we should now have a host_poop struct
  559.    with at least one IP address in it. */
  560.   return (poop);
  561. } /* gethostpoop */
  562.  
  563. /* getportpoop :
  564.    Same general idea as gethostpoop -- look up a port in /etc/services, fill
  565.    in global port_poop, but return the actual port *number*.  Pass ONE of:
  566.     pstring to resolve stuff like "23" or "exec";
  567.     pnum to reverse-resolve something that's already a number.
  568.    If o_nflag is on, fill in what we can but skip the getservby??? stuff.
  569.    Might as well have consistent behavior here... */
  570. USHORT getportpoop (pstring, pnum)
  571.   char * pstring;
  572.   unsigned int pnum;
  573. {
  574.   struct servent * servent;
  575.   register int x;
  576.   register int y;
  577.   char * whichp = p_tcp;
  578.   if (o_udpmode)
  579.     whichp = p_udp;
  580.   portpoop->name[0] = '?';        /* fast preload */
  581.   portpoop->name[1] = '\0';
  582.  
  583. /* case 1: reverse-lookup of a number; placed first since this case is much
  584.    more frequent if we're scanning */
  585.   if (pnum) {
  586.     if (pstring)            /* one or the other, pleeze */
  587.       return (0);
  588.     x = pnum;
  589.     if (o_nflag)            /* go faster, skip getservbyblah */
  590.       goto gp_finish;
  591.     y = htons (x);            /* gotta do this -- see Fig.1 below */
  592.     servent = getservbyport (y, whichp);
  593.     if (servent) {
  594.       y = ntohs (servent->s_port);
  595.       if (x != y)            /* "never happen" */
  596.     holler ("Warning: port-bynum mismatch, %d != %d", x, y);
  597.       strncpy (portpoop->name, servent->s_name, sizeof (portpoop->name));
  598.     } /* if servent */
  599.     goto gp_finish;
  600.   } /* if pnum */
  601.  
  602. /* case 2: resolve a string, but we still give preference to numbers instead
  603.    of trying to resolve conflicts.  None of the entries in *my* extensive
  604.    /etc/services begins with a digit, so this should "always work" unless
  605.    you're at 3com and have some company-internal services defined... */
  606.   if (pstring) {
  607.     if (pnum)                /* one or the other, pleeze */
  608.       return (0);
  609.     x = atoi (pstring);
  610.     if (x)
  611.       return (getportpoop (NULL, x));    /* recurse for numeric-string-arg */
  612.     if (o_nflag)            /* can't use names! */
  613.       return (0);
  614.     servent = getservbyname (pstring, whichp);
  615.     if (servent) {
  616.       strncpy (portpoop->name, servent->s_name, sizeof (portpoop->name));
  617.       x = ntohs (servent->s_port);
  618.       goto gp_finish;
  619.     } /* if servent */
  620.   } /* if pstring */
  621.  
  622.   return (0);                /* catches any problems so far */
  623.  
  624. /* Obligatory netdb.h-inspired rant: servent.s_port is supposed to be an int.
  625.    Despite this, we still have to treat it as a short when copying it around.
  626.    Not only that, but we have to convert it *back* into net order for
  627.    getservbyport to work.  Manpages generally aren't clear on all this, but
  628.    there are plenty of examples in which it is just quietly done.  More BSD
  629.    lossage... since everything getserv* ever deals with is local to our own
  630.    host, why bother with all this network-order/host-order crap at all?!
  631.    That should be saved for when we want to actually plug the port[s] into
  632.    some real network calls -- and guess what, we have to *re*-convert at that
  633.    point as well.  Fuckheads. */
  634.  
  635. gp_finish:
  636. /* Fall here whether or not we have a valid servent at this point, with
  637.    x containing our [host-order and therefore useful, dammit] port number */
  638.   sprintf (portpoop->anum, "%d", x);    /* always load any numeric specs! */
  639.   portpoop->num = (x & 0xffff);        /* ushort, remember... */
  640.   return (portpoop->num);
  641. } /* getportpoop */
  642.  
  643. /* nextport :
  644.    Come up with the next port to try, be it random or whatever.  "block" is
  645.    a ptr to randports array, whose bytes [so far] carry these meanings:
  646.     0    ignore
  647.     1    to be tested
  648.     2    tested [which is set as we find them here]
  649.    returns a USHORT random port, or 0 if all the t-b-t ones are used up. */
  650. USHORT nextport (block)
  651.   char * block;
  652. {
  653.   register unsigned int x;
  654.   register unsigned int y;
  655.  
  656.   y = 70000;            /* high safety count for rnd-tries */
  657.   while (y > 0) {
  658.     x = (RAND() & 0xffff);
  659.     if (block[x] == 1) {    /* try to find a not-done one... */
  660.       block[x] = 2;
  661.       break;
  662.     }
  663.     x = 0;            /* bummer. */
  664.     y--;
  665.   } /* while y */
  666.   if (x)
  667.     return (x);
  668.  
  669.   y = 65535;            /* no random one, try linear downsearch */
  670.   while (y > 0) {        /* if they're all used, we *must* be sure! */
  671.     if (block[y] == 1) {
  672.       block[y] = 2;
  673.       break;
  674.     }
  675.     y--;
  676.   } /* while y */
  677.   if (y)
  678.     return (y);            /* at least one left */
  679.  
  680.   return (0);            /* no more left! */
  681. } /* nextport */
  682.  
  683. /* loadports :
  684.    set "to be tested" indications in BLOCK, from LO to HI.  Almost too small
  685.    to be a separate routine, but makes main() a little cleaner... */
  686. void loadports (block, lo, hi)
  687.   char * block;
  688.   USHORT lo;
  689.   USHORT hi;
  690. {
  691.   USHORT x;
  692.  
  693.   if (! block)
  694.     bail ("loadports: no block?!");
  695.   if ((! lo) || (! hi))
  696.     bail ("loadports: bogus values %d, %d", lo, hi);
  697.   x = hi;
  698.   while (lo <= x) {
  699.     block[x] = 1;
  700.     x--;
  701.   }
  702. } /* loadports */
  703.  
  704. #ifdef GAPING_SECURITY_HOLE
  705. char * pr00gie = NULL;            /* global ptr to -e arg */
  706.  
  707. /* doexec :
  708.    fiddle all the file descriptors around, and hand off to another prog.  Sort
  709.    of like a one-off "poor man's inetd".  This is the only section of code
  710.    that would be security-critical, which is why it's ifdefed out by default.
  711.    Use at your own hairy risk; if you leave shells lying around behind open
  712.    listening ports you deserve to lose!! */
  713. doexec (fd)
  714.   int fd;
  715. {
  716.   register char * p;
  717.  
  718.   dup2 (fd, 0);                /* the precise order of fiddlage */
  719. #ifdef WIN32
  720.   closesocket (fd);
  721. #else
  722.   close (fd);                /* is apparently crucial; this is */
  723. #endif
  724.   dup2 (0, 1);                /* swiped directly out of "inetd". */
  725.   dup2 (0, 2);
  726.   p = strrchr (pr00gie, '/');        /* shorter argv[0] */
  727.   if (p)
  728.     p++;
  729.   else
  730.     p = pr00gie;
  731. Debug (("gonna exec %s as %s...", pr00gie, p))
  732.   execl (pr00gie, p, NULL);
  733.   bail ("exec %s failed", pr00gie);    /* this gets sent out.  Hmm... */
  734. } /* doexec */
  735. #endif /* GAPING_SECURITY_HOLE */
  736.  
  737. /* doconnect :
  738.    do all the socket stuff, and return an fd for one of
  739.     an open outbound TCP connection
  740.     a UDP stub-socket thingie
  741.    with appropriate socket options set up if we wanted source-routing, or
  742.     an unconnected TCP or UDP socket to listen on.
  743.    Examines various global o_blah flags to figure out what-all to do. */
  744. int doconnect (rad, rp, lad, lp)
  745.   IA * rad;
  746.   USHORT rp;
  747.   IA * lad;
  748.   USHORT lp;
  749. {
  750.   register int nnetfd;
  751.   register int rr;
  752.   int x, y;
  753.  
  754.   errno = 0;
  755.  
  756. /* grab a socket; set opts */
  757.   if (o_udpmode)
  758.     nnetfd = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  759.   else
  760.     nnetfd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
  761.   if (nnetfd < 0)
  762.     bail ("Can't get socket");
  763.   if (nnetfd == 0)        /* might *be* zero if stdin was closed! */
  764.     nnetfd = dup (nnetfd);    /* so fix it.  Leave the old 0 hanging. */
  765. #ifdef WIN32
  766.   rr = setsockopt (nnetfd, SOL_SOCKET, SO_REUSEADDR, (const char FAR *)setsockopt_c, sizeof (setsockopt_c));
  767. #else
  768.   x = 1;
  769.   rr = setsockopt (nnetfd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof (x));
  770. #endif
  771.   if (rr == -1)
  772.     holler ("nnetfd reuseaddr failed");        /* ??? */
  773. #ifdef SO_REUSEPORT    /* doesnt exist everywhere... */
  774. #ifdef WIN32
  775.   rr = setsockopt (nnetfd, SOL_SOCKET, SO_REUSEPORT, &c, sizeof (c));
  776. #else
  777.   rr = setsockopt (nnetfd, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
  778. #endif
  779.   if (rr == -1)
  780.     holler ("nnetfd reuseport failed");        /* ??? */
  781. #endif
  782.  
  783. /* fill in all the right sockaddr crud */
  784.   lclend->sin_family = AF_INET;
  785.   remend->sin_family = AF_INET;
  786.  
  787. /* if lad/lp, do appropriate binding */
  788.   if (lad)
  789.     memcpy (&lclend->sin_addr.s_addr, lad, sizeof (IA));
  790.   if (lp)
  791.     lclend->sin_port = htons (lp);
  792.   rr = 0;
  793.   if (lad || lp) {
  794.     x = (int) lp;
  795. /* try a few times for the local bind, a la ftp-data-port... */
  796.     for (y = 4; y > 0; y--) {
  797.       rr = bind (nnetfd, (SA *)lclend, sizeof (SA));
  798.       if (rr == 0)
  799.     break;
  800.       if (errno != EADDRINUSE)
  801.     break;
  802.       else {
  803.     holler ("retrying local %s:%d", inet_ntoa (lclend->sin_addr), lp);
  804.     sleep (1);
  805.     errno = 0;            /* clear from sleep */
  806.       } /* if EADDRINUSE */
  807.     } /* for y counter */
  808.   } /* if lad or lp */
  809.   if (rr)
  810.     bail ("Can't grab %s:%d with bind",
  811.     inet_ntoa(lclend->sin_addr), lp);
  812.  
  813.   if (o_listen)
  814.     return (nnetfd);            /* thanks, that's all for today */
  815.  
  816.   memcpy (&remend->sin_addr.s_addr, rad, sizeof (IA));
  817.   remend->sin_port = htons (rp);
  818.  
  819. /* if any -g arguments were given, set up source-routing.  We hit this after
  820.    the gates are all looked up and ready to rock, any -G pointer is set,
  821.    and gatesidx is now the *number* of hops */
  822.   if (gatesidx) {        /* if we wanted any srcrt hops ... */
  823. /* don't even bother compiling if we can't do IP options here! */
  824. //#ifdef IP_OPTIONS 
  825. #ifndef WIN32 
  826.     if (! optbuf) {        /* and don't already *have* a srcrt set */
  827.       char * opp;        /* then do all this setup hair */
  828.       optbuf = Hmalloc (48);
  829.       opp = optbuf;
  830.       *opp++ = IPOPT_LSRR;                    /* option */
  831.       *opp++ = (char)
  832.     (((gatesidx + 1) * sizeof (IA)) + 3) & 0xff;        /* length */
  833.       *opp++ = gatesptr;                    /* pointer */
  834. /* opp now points at first hop addr -- insert the intermediate gateways */
  835.       for ( x = 0; x < gatesidx; x++) {
  836.     memcpy (opp, gates[x]->iaddrs, sizeof (IA));
  837.     opp += sizeof (IA);
  838.       }
  839. /* and tack the final destination on the end [needed!] */
  840.       memcpy (opp, rad, sizeof (IA));
  841.       opp += sizeof (IA);
  842.       *opp = IPOPT_NOP;            /* alignment filler */
  843.     } /* if empty optbuf */
  844. /* calculate length of whole option mess, which is (3 + [hops] + [final] + 1),
  845.    and apply it [have to do this every time through, of course] */
  846.     x = ((gatesidx + 1) * sizeof (IA)) + 4;
  847.     rr = setsockopt (nnetfd, IPPROTO_IP, IP_OPTIONS, optbuf, x);
  848.     if (rr == -1)
  849.       bail ("srcrt setsockopt fuxored");
  850. #else /* IP_OPTIONS */
  851.     holler ("Warning: source routing unavailable on this machine, ignoring");
  852. #endif /* IP_OPTIONS*/
  853.   } /* if gatesidx */
  854.  
  855. /* wrap connect inside a timer, and hit it */
  856.   arm (1, o_wait);
  857.   if (setjmp (jbuf) == 0) {
  858.     rr = connect (nnetfd, (SA *)remend, sizeof (SA));
  859.   } else {                /* setjmp: connect failed... */
  860.     rr = -1;
  861. #ifdef WIN32
  862.     WSASetLastError(WSAETIMEDOUT);            /* fake it */
  863. #else
  864.     errno = ETIMEDOUT;            /* fake it */
  865. #endif
  866.   }
  867.   arm (0, 0);
  868.   if (rr == 0)
  869.     return (nnetfd);
  870. #ifdef WIN32
  871.   errno = h_errno;
  872.   closesocket (netfd);
  873.   WSASetLastError(errno); // don't want to lose connect error
  874. #else
  875.   close (nnetfd);            /* clean up junked socket FD!! */
  876. #endif
  877.   return (-1);
  878. } /* doconnect */
  879.  
  880. /* dolisten :
  881.    just like doconnect, and in fact calls a hunk of doconnect, but listens for
  882.    incoming and returns an open connection *from* someplace.  If we were
  883.    given host/port args, any connections from elsewhere are rejected.  This
  884.    in conjunction with local-address binding should limit things nicely... */
  885. int dolisten (rad, rp, lad, lp)
  886.   IA * rad;
  887.   USHORT rp;
  888.   IA * lad;
  889.   USHORT lp;
  890. {
  891.   register int nnetfd;
  892.   register int rr;
  893.   HINF * whozis = NULL;
  894.   int x;
  895.   USHORT z;
  896.   errno = 0;
  897.  
  898. /* Pass everything off to doconnect, who in o_listen mode just gets a socket */
  899.   nnetfd = doconnect (rad, rp, lad, lp);
  900.   if (nnetfd <= 0)
  901.     return (-1);
  902.   if (o_udpmode) {            /* apparently UDP can listen ON */
  903.     if (! lp)                /* "port 0",  but that's not useful */
  904.       bail ("UDP listen needs -p arg");
  905.   } else {
  906.     rr = listen (nnetfd, 1);        /* gotta listen() before we can get */
  907.     if (rr < 0)                /* our local random port.  sheesh. */
  908.       bail ("local listen fuxored");
  909.   }
  910.  
  911. /* I can't believe I have to do all this to get my own goddamn bound address
  912.    and port number.  It should just get filled in during bind() or something.
  913.    All this is only useful if we didn't say -p for listening, since if we
  914.    said -p we *know* what port we're listening on.  At any rate we won't bother
  915.    with it all unless we wanted to see it, although listening quietly on a
  916.    random unknown port is probably not very useful without "netstat". */
  917.   if (o_verbose) {
  918.     x = sizeof (SA);        /* how 'bout getsockNUM instead, pinheads?! */
  919.     rr = getsockname (nnetfd, (SA *) lclend, &x);
  920.       if (rr < 0)
  921.     holler ("local getsockname failed");
  922.     strcpy (bigbuf_net, "listening on [");    /* buffer reuse... */
  923.       if (lclend->sin_addr.s_addr)
  924.     strcat (bigbuf_net, inet_ntoa (lclend->sin_addr));
  925.       else
  926.     strcat (bigbuf_net, "any");
  927.     strcat (bigbuf_net, "] %d ...");
  928.     z = ntohs (lclend->sin_port);
  929.     holler (bigbuf_net, z);
  930.   } /* verbose -- whew!! */
  931.  
  932. /* UDP is a speeeeecial case -- we have to do I/O *and* get the calling
  933.    party's particulars all at once, listen() and accept() don't apply.
  934.    At least in the BSD universe, however, recvfrom/PEEK is enough to tell
  935.    us something came in, and we can set things up so straight read/write
  936.    actually does work after all.  Yow.  YMMV on strange platforms!  */
  937.   if (o_udpmode) {
  938.     x = sizeof (SA);        /* retval for recvfrom */
  939.     arm (2, o_wait);        /* might as well timeout this, too */
  940.     if (setjmp (jbuf) == 0) {    /* do timeout for initial connect */
  941.       rr = recvfrom        /* and here we block... */
  942.     (nnetfd, bigbuf_net, BIGSIZ, MSG_PEEK, (SA *) remend, &x);
  943. Debug (("dolisten/recvfrom ding, rr = %d, netbuf %s ", rr, bigbuf_net))
  944.     } else
  945.       goto dol_tmo;        /* timeout */
  946.     arm (0, 0);
  947. /* I'm not completely clear on how this works -- BSD seems to make UDP
  948.    just magically work in a connect()ed context, but we'll undoubtedly run
  949.    into systems this deal doesn't work on.  For now, we apparently have to
  950.    issue a connect() on our just-tickled socket so we can write() back.
  951.    Again, why the fuck doesn't it just get filled in and taken care of?!
  952.    This hack is anything but optimal.  Basically, if you want your listener
  953.    to also be able to send data back, you need this connect() line, which
  954.    also has the side effect that now anything from a different source or even a
  955.    different port on the other end won't show up and will cause ICMP errors.
  956.    I guess that's what they meant by "connect".
  957.    Let's try to remember what the "U" is *really* for, eh?
  958. */
  959.     rr = connect (nnetfd, (SA *)remend, sizeof (SA));
  960.     goto whoisit;
  961.   } /* o_udpmode */
  962.  
  963. /* fall here for TCP */
  964.   x = sizeof (SA);        /* retval for accept */
  965.   arm (2, o_wait);        /* wrap this in a timer, too; 0 = forever */
  966.   if (setjmp (jbuf) == 0) {
  967.     rr = accept (nnetfd, (SA *)remend, &x);
  968.   } else
  969.     goto dol_tmo;        /* timeout */
  970.   arm (0, 0);
  971. #ifdef WIN32
  972.   closesocket (netfd);
  973. #else
  974.   close (nnetfd);        /* dump the old socket */
  975. #endif
  976.   nnetfd = rr;            /* here's our new one */
  977.  
  978. whoisit:
  979.   if (rr < 0)
  980.     goto dol_err;        /* bail out if any errors so far */
  981.  
  982. /* Various things that follow temporarily trash bigbuf_net, which might contain
  983.    a copy of any recvfrom()ed packet, but we'll read() another copy later. */
  984.  
  985. /* If we can, look for any IP options.  Useful for testing the receiving end of
  986.    such things, and is a good exercise in dealing with it.  We do this before
  987.    the connect message, to ensure that the connect msg is uniformly the LAST
  988.    thing to emerge after all the intervening crud.  Doesn't work for UDP on
  989.    any machines I've tested, but feel free to surprise me. */
  990. //#ifdef IP_OPTIONS
  991. #ifndef WIN32
  992.   if (! o_verbose)            /* if we wont see it, we dont care */
  993.     goto dol_noop;
  994.   optbuf = Hmalloc (40);
  995.   x = 40;
  996.   rr = getsockopt (nnetfd, IPPROTO_IP, IP_OPTIONS, optbuf, &x);
  997.   if (rr < 0)
  998.     holler ("getsockopt failed");
  999. Debug (("ipoptions ret len %d", x))
  1000.   if (x) {                /* we've got options, lessee em... */
  1001.     unsigned char * q = (unsigned char *) optbuf;
  1002.     char * p = bigbuf_net;        /* local variables, yuk! */
  1003.     char * pp = &bigbuf_net[128];    /* get random space farther out... */
  1004.     memset (bigbuf_net, 0, 256);    /* clear it all first */
  1005.     while (x > 0) {
  1006.     sprintf (pp, "%2.2x ", *q);    /* clumsy, but works: turn into hex */
  1007.     strcat (p, pp);            /* and build the final string */
  1008.     q++; p++;
  1009.     x--;
  1010.     }
  1011.     holler ("IP options: %s", bigbuf_net);
  1012.   } /* if x, i.e. any options */
  1013. dol_noop:
  1014. #endif /* IP_OPTIONS */
  1015.  
  1016. /* now check out who it is.  We don't care about mismatched DNS names here,
  1017.    but any ADDR and PORT we specified had better fucking well match the caller.
  1018.    Converting from addr to inet_ntoa and back again is a bit of a kludge, but
  1019.    gethostpoop wants a string and there's much gnarlier code out there already,
  1020.    so I don't feel bad.
  1021.    The *real* question is why BFD sockets wasn't designed to allow listens for
  1022.    connections *from* specific hosts/ports, instead of requiring the caller to
  1023.    accept the connection and then reject undesireable ones by closing. */
  1024.   z = ntohs (remend->sin_port);
  1025.   strcpy (bigbuf_net, inet_ntoa (remend->sin_addr));
  1026.   whozis = gethostpoop (bigbuf_net, o_nflag);
  1027.   x = 0;                /* use as a flag... */
  1028.   if (rad)
  1029.     if (memcmp (rad, whozis->iaddrs, sizeof (SA)))
  1030.       x = 1;
  1031.   if (rp)
  1032.     if (z != rp)
  1033.       x = 1;
  1034.   if (x)                    /* guilty! */
  1035.     bail ("invalid connection from %s [%s] %d",
  1036.     whozis->name, whozis->addrs[0], z);
  1037.   holler ("connect from %s [%s] %d",        /* oh, you're okay.. */
  1038.     whozis->name, whozis->addrs[0], z);
  1039.   return (nnetfd);                /* open! */
  1040.  
  1041. dol_tmo:
  1042.   errno = ETIMEDOUT;            /* fake it */
  1043. dol_err:
  1044. #ifdef WIN32
  1045.   closesocket (netfd);
  1046. #else
  1047.   close (nnetfd);
  1048. #endif
  1049.   return (-1);
  1050. } /* dolisten */
  1051.  
  1052. /* udptest :
  1053.    fire a couple of packets at a UDP target port, just to see if it's really
  1054.    there.  On BSD kernels, ICMP host/port-unreachable errors get delivered to
  1055.    our socket as ECONNREFUSED write errors.  On SV kernels, we lose; we'll have
  1056.    to collect and analyze raw ICMP ourselves a la satan's probe_udp_ports
  1057.    backend.  Guess where could swipe the appropriate code from...
  1058.  
  1059.    Use the time delay between writes if given, otherwise use the "tcp ping"
  1060.    trick for getting the RTT.  [I got that idea from pluvius, and warped it.]
  1061.    Return either the original fd, or clean up and return -1. */
  1062. udptest (fd, where)
  1063.   int fd;
  1064.   IA * where;
  1065. {
  1066.   register int rr;
  1067.  
  1068. #ifdef WIN32
  1069.   rr = send (fd, bigbuf_in, 1, 0);
  1070. #else
  1071.   rr = write (fd, bigbuf_in, 1);
  1072. #endif
  1073.   if (rr != 1)
  1074.     holler ("udptest first write failed?! errno %d", errno);
  1075.   if (o_wait)
  1076.     sleep (o_wait);
  1077.   else {
  1078. /* use the tcp-ping trick: try connecting to a normally refused port, which
  1079.    causes us to block for the time that SYN gets there and RST gets back.
  1080.    Not completely reliable, but it *does* mostly work. */
  1081.     o_udpmode = 0;            /* so doconnect does TCP this time */
  1082. /* Set a temporary connect timeout, so packet filtration doesnt cause
  1083.    us to hang forever, and hit it */
  1084.     o_wait = 5;                /* XXX: enough to notice?? */
  1085.     rr = doconnect (where, SLEAZE_PORT, 0, 0);
  1086.     if (rr > 0)
  1087. #ifdef WIN32
  1088.       closesocket (rr);
  1089. #else
  1090.       close (rr);            /* in case it *did* open */
  1091. #endif
  1092.     o_wait = 0;                /* reset it */
  1093.     o_udpmode++;            /* we *are* still doing UDP, right? */
  1094.   } /* if o_wait */
  1095.   errno = 0;                /* clear from sleep */
  1096. #ifdef WIN32
  1097.   rr = send (fd, bigbuf_in, 1, 0);
  1098. #else
  1099.   rr = write (fd, bigbuf_in, 1);
  1100. #endif
  1101.   if (rr == 1)                /* if write error, no UDP listener */
  1102.     return (fd);
  1103. #ifdef WIN32
  1104.   closesocket (fd);
  1105. #else
  1106.   close (fd);                /* use it or lose it! */
  1107. #endif
  1108.   return (-1);
  1109. } /* udptest */
  1110.  
  1111. /* readwrite :
  1112.    handle stdin/stdout/network I/O.  Bwahaha!! -- the select loop from hell.
  1113.    In this instance, return what might become our exit status. */
  1114. int readwrite (fd)
  1115. #ifdef WIN32
  1116.   unsigned int fd;
  1117. #else
  1118.   int fd;
  1119. #endif
  1120. {
  1121.   register int rr;
  1122.   register char * zp;        /* stdin buf ptr */
  1123.   register char * np;        /* net-in buf ptr */
  1124.   unsigned int rzleft;
  1125.   unsigned int rnleft;
  1126.   USHORT netretry;        /* net-read retry counter */
  1127.   USHORT wretry;        /* net-write sanity counter */
  1128.  
  1129. #ifdef WIN32 /* (weld) WIN32 must poll because of weak stdin handling so we need a
  1130.                 short timer */
  1131.   struct timeval timer3;
  1132.   int istty;
  1133.  
  1134.   timer3.tv_sec = 0;
  1135.   timer3.tv_usec = 100;
  1136.  
  1137.   _setmode( 0, _O_BINARY ); /* (weld) I think we want to do this */
  1138.   _setmode( 1, _O_BINARY ); /* sets stdin and stdout to binary so no crlf translation */
  1139.  
  1140.   istty = _isatty( 0 );    /* WIN32 treats tty stdin differently, what'd ya expect? */
  1141.  
  1142. #endif
  1143.  
  1144. /* if you don't have all this FD_* macro hair in sys/types.h, you'll have to
  1145.    either find it or do your own bit-bashing: *ding1 |= (1 << fd), etc... */
  1146. #ifndef WIN32  // fd is not implemented as a real file handle in WIN32
  1147.   if (fd > FD_SETSIZE) {
  1148.     holler ("Preposterous fd value %d", fd);
  1149.     return (1);
  1150.   }
  1151. #endif
  1152.   FD_SET (fd, ding1);        /* global: the net is open */
  1153.   netretry = 2;
  1154.   rzleft = rnleft = 0;
  1155.   if (insaved) {
  1156.     rzleft = insaved;        /* preload multi-mode fakeouts */
  1157.     zp = bigbuf_in;
  1158.   }
  1159.   if (o_interval)
  1160.     sleep (o_interval);        /* pause *before* sending stuff, too */
  1161.   errno = 0;            /* clear from sleep */
  1162.  
  1163. /* and now the big ol' select shoveling loop ... */
  1164.   while (FD_ISSET (fd, ding1)) {    /* i.e. till the *net* closes! */
  1165.     *ding2 = *ding1;            /* FD_COPY ain't portable... */
  1166.     wretry = 8200;            /* more than we'll ever hafta write */
  1167. /* some systems, notably linux, crap into their select timers on return, so
  1168.    we create a expendable copy and give *that* to select.  *Fuck* me ... */
  1169.     if (timer1)
  1170.       memcpy (timer2, timer1, sizeof (struct timeval));
  1171. #ifdef WIN32 /* (weld)we must use our own small timeval to poll */
  1172.     rr = select (16, ding2, 0, 0, &timer3);    /* here it is, kiddies */
  1173. #else
  1174.     rr = select (16, ding2, 0, 0, timer2);    /* here it is, kiddies */
  1175. #endif
  1176.     if (rr < 0) {
  1177. #ifdef WIN32
  1178.     if (h_errno != WSAEINTR) {        /* might have gotten ^Zed, etc ?*/
  1179. #else
  1180.     if (errno != EINTR) {        /* might have gotten ^Zed, etc ?*/
  1181. #endif
  1182.       holler ("select fuxored");
  1183. #ifdef WIN32
  1184.       closesocket (fd);
  1185. #else
  1186.       close (fd);
  1187. #endif
  1188.       return (1);
  1189.     }
  1190.     } /* select fuckup */
  1191. /* if we have a timeout AND stdin is closed AND we haven't heard anything
  1192.    from the net during that time, assume it's dead and close it too. */
  1193. #ifndef WIN32  /* (weld) need to write some code here */
  1194.     if (rr == 0) {
  1195.     if (! FD_ISSET (0, ding1))
  1196.       netretry--;            /* we actually try a coupla times. */
  1197.     if (! netretry) {
  1198.       if (o_verbose > 1)        /* normally we don't care */
  1199.         holler ("net timeout");
  1200. #ifdef WIN32
  1201.       closesocket (fd);
  1202. #else
  1203.       close (fd);
  1204. #endif
  1205.       return (0);            /* not an error! */
  1206.     }
  1207.     } /* select timeout */
  1208. #endif
  1209. /* xxx: should we check the exception fds too?  The read fds seem to give
  1210.    us the right info, and none of the examples I found bothered. */
  1211.  
  1212. /* Ding!!  Something arrived, go check all the incoming hoppers, net first */
  1213.     if (FD_ISSET (fd, ding2)) {        /* net: ding! */
  1214. #ifdef WIN32
  1215.     rr = recv (fd, bigbuf_net, BIGSIZ, 0);
  1216. #else
  1217.     rr = read (fd, bigbuf_net, BIGSIZ);
  1218. #endif
  1219.     if (rr <= 0) {
  1220.       FD_CLR (fd, ding1);        /* net closed, we'll finish up... */
  1221.       rzleft = 0;            /* can't write anymore: broken pipe */
  1222.     } else {
  1223.       rnleft = rr;
  1224.       np = bigbuf_net;
  1225.     }
  1226. Debug (("got %d from the net, errno %d", rr, errno))
  1227.     } /* net:ding */
  1228.  
  1229. /* if we're in "slowly" mode there's probably still stuff in the stdin
  1230.    buffer, so don't read unless we really need MORE INPUT!  MORE INPUT! */
  1231.     if (rzleft)
  1232.     goto shovel;
  1233.  
  1234. /* okay, suck more stdin */
  1235. #ifndef WIN32
  1236.     if (FD_ISSET (0, ding2)) {        /* stdin: ding! */
  1237.     rr = read (0, bigbuf_in, BIGSIZ);
  1238.  
  1239. /* xxx: maybe make reads here smaller for UDP mode, so that the subsequent
  1240.    writes are smaller -- 1024 or something?  "oh, frag it", etc, although
  1241.    mobygrams are kinda fun and exercise the reassembler. */
  1242.     if (rr <= 0) {            /* at end, or fukt, or ... */
  1243.       FD_CLR (0, ding1);        /* disable and close stdin */
  1244.       close (0);
  1245.     } else {
  1246.       rzleft = rr;
  1247.       zp = bigbuf_in;
  1248. /* special case for multi-mode -- we'll want to send this one buffer to every
  1249.    open TCP port or every UDP attempt, so save its size and clean up stdin */
  1250.       if (! Single) {        /* we might be scanning... */
  1251.         insaved = rr;        /* save len */
  1252.         FD_CLR (0, ding1);        /* disable further junk from stdin */
  1253.         close (0);            /* really, I mean it */
  1254.       } /* Single */
  1255.     } /* if rr/read */
  1256.     } /* stdin:ding */
  1257. #else
  1258.     if (istty) {
  1259.         /* (weld) cool, we can actually peek a tty and not have to block */
  1260.         /* needs to be cleaned up */
  1261.         if (kbhit()) {
  1262. //            bigbuf_in[0] = getche();
  1263.             gets(bigbuf_in);
  1264.             strcat(bigbuf_in, "\r\n");
  1265.             rr = strlen(bigbuf_in);
  1266.             rzleft = rr;
  1267.             zp = bigbuf_in;
  1268. /* special case for multi-mode -- we'll want to send this one buffer to every
  1269.    open TCP port or every UDP attempt, so save its size and clean up stdin */
  1270.             if (! Single) {        /* we might be scanning... */
  1271.                 insaved = rr;        /* save len */
  1272.                 close (0);            /* really, I mean it */
  1273.             }
  1274.         }
  1275.     } else {
  1276.         /* (weld) this is gonna block until a <cr> so it kinda sucks */
  1277.         rr = read (0, bigbuf_in, BIGSIZ);
  1278.         if (rr <= 0) {            /* at end, or fukt, or ... */
  1279.             close (0);
  1280.         } else {
  1281.             rzleft = rr;
  1282.             zp = bigbuf_in;
  1283. /* special case for multi-mode -- we'll want to send this one buffer to every
  1284.    open TCP port or every UDP attempt, so save its size and clean up stdin */
  1285.             if (! Single) {        /* we might be scanning... */
  1286.                 insaved = rr;        /* save len */
  1287.                 close (0);            /* really, I mean it */
  1288.             } /* Single */
  1289.         } /* if rr/read */
  1290.     }
  1291.  
  1292. #endif
  1293. shovel:
  1294. /* now that we've dingdonged all our thingdings, send off the results.
  1295.    Geez, why does this look an awful lot like the big loop in "rsh"? ...
  1296.    not sure if the order of this matters, but write net -> stdout first. */
  1297.  
  1298. /* sanity check.  Works because they're both unsigned... */
  1299.     if ((rzleft > 8200) || (rnleft > 8200)) {
  1300.     holler ("Preposterous Pointers: %d, %d", rzleft, rnleft);
  1301.     rzleft = rnleft = 0;
  1302.     }
  1303. /* net write retries sometimes happen on UDP connections */
  1304.     if (! wretry) {            /* is something hung? */
  1305.     holler ("too many output retries");
  1306.     return (1);
  1307.     }
  1308.     if (rnleft) {
  1309.     rr = write (1, np, rnleft);
  1310.     if (rr > 0) {
  1311.       np += rr;            /* fix up ptrs and whatnot */
  1312.       rnleft -= rr;            /* will get sanity-checked above */
  1313.       wrote_out += rr;        /* global count */
  1314.     }
  1315. Debug (("wrote %d to stdout, errno %d", rr, errno))
  1316.     } /* rnleft */
  1317.     if (rzleft) {
  1318.     if (o_interval)            /* in "slowly" mode ?? */
  1319.       rr = findline (zp, rzleft);
  1320.     else
  1321.       rr = rzleft;
  1322. #ifdef WIN32
  1323.     rr = send (fd, zp, rr, 0);    /* one line, or the whole buffer */
  1324. #else
  1325.     rr = write (fd, zp, rr);    /* one line, or the whole buffer */
  1326. #endif
  1327.     if (rr > 0) {
  1328.       zp += rr;
  1329.       rzleft -= rr;
  1330.       wrote_net += rr;        /* global count */
  1331.     }
  1332. Debug (("wrote %d to net, errno %d", rr, errno))
  1333.     } /* rzleft */
  1334.     if (o_interval) {            /* cycle between slow lines, or ... */
  1335.     sleep (o_interval);
  1336.     errno = 0;            /* clear from sleep */
  1337.     continue;            /* ...with hairy select loop... */
  1338.     }
  1339.     if ((rzleft) || (rnleft)) {        /* shovel that shit till they ain't */
  1340.     wretry--;            /* none left, and get another load */
  1341.     goto shovel;
  1342.     }
  1343.   } /* while ding1:netfd is open */
  1344.  
  1345. /* XXX: maybe want a more graceful shutdown() here, or screw around with
  1346.    linger times??  I suspect that I don't need to since I'm always doing
  1347.    blocking reads and writes and my own manual "last ditch" efforts to read
  1348.    the net again after a timeout.  I haven't seen any screwups yet, but it's
  1349.    not like my test network is particularly busy... */
  1350. #ifdef WIN32
  1351.   closesocket (fd);
  1352. #else
  1353.   close (fd);
  1354. #endif
  1355.   return (0);
  1356. } /* readwrite */
  1357.  
  1358. /* main :
  1359.    now we pull it all together... */
  1360. main (argc, argv)
  1361.   int argc;
  1362.   char ** argv;
  1363. {
  1364. #ifndef HAVE_GETOPT
  1365.   extern char * optarg;
  1366.   extern int optind, optopt;
  1367. #endif
  1368.   register int x;
  1369.   register char *cp;
  1370.   HINF * gp;
  1371.   HINF * whereto = NULL;
  1372.   HINF * wherefrom = NULL;
  1373.   IA * ouraddr = NULL;
  1374.   IA * themaddr = NULL;
  1375.   USHORT o_lport = 0;
  1376.   USHORT ourport = 0;
  1377.   USHORT loport = 0;        /* for scanning stuff */
  1378.   USHORT hiport = 0;
  1379.   USHORT curport = 0;
  1380.   char * randports = NULL;
  1381.  
  1382. #ifdef HAVE_BIND
  1383. /* can *you* say "cc -yaddayadda netcat.c -lresolv -l44bsd" on SunLOSs? */
  1384.   res_init();
  1385. #endif
  1386. /* I was in this barbershop quartet in Skokie IL ... */
  1387. /* round up the usual suspects, i.e. malloc up all the stuff we need */
  1388.   lclend = (SAI *) Hmalloc (sizeof (SA));
  1389.   remend = (SAI *) Hmalloc (sizeof (SA));
  1390.   bigbuf_in = Hmalloc (BIGSIZ);
  1391.   bigbuf_net = Hmalloc (BIGSIZ);
  1392.   ding1 = (fd_set *) Hmalloc (sizeof (fd_set));
  1393.   ding2 = (fd_set *) Hmalloc (sizeof (fd_set));
  1394.   portpoop = (PINF *) Hmalloc (sizeof (PINF));
  1395.  
  1396. #ifdef WIN32
  1397.   setsockopt_c = (char *)malloc(sizeof(char));
  1398.   *setsockopt_c    = 1;
  1399. #endif
  1400.  
  1401.   errno = 0;
  1402.   gatesptr = 4;
  1403.  
  1404. /* catch a signal or two for cleanup */
  1405.   signal (SIGINT, catch);
  1406. #ifdef NTFIXTHIS
  1407.   signal (SIGQUIT, catch);
  1408. #endif
  1409.   signal (SIGTERM, catch);
  1410. #ifdef NTFIXTHIS
  1411.   signal (SIGURG, SIG_IGN);
  1412. #endif
  1413.  
  1414. /* if no args given at all, get 'em from stdin and construct an argv. */
  1415.   if (argc == 1) {
  1416.     cp = argv[0];
  1417.     argv = (char **) Hmalloc (128 * sizeof (char *));    /* XXX: 128? */
  1418.     argv[0] = cp;            /* leave old prog name intact */
  1419.     cp = Hmalloc (BIGSIZ);
  1420.     argv[1] = cp;            /* head of new arg block */
  1421.     fprintf (stderr, "Cmd line: ");
  1422.     fflush (stderr);        /* I dont care if it's unbuffered or not! */
  1423.     cp = fgets (cp, BIGSIZ - 8, stdin);    /* xxx: causes a big stdin read() */
  1424.     if (! cp)
  1425.     bail ("wrong");
  1426.     cp = strchr (argv[1], '\n');
  1427.     if (cp)
  1428.       *cp = '\0';
  1429.     cp = strchr (argv[1], '\r');    /* look for ^M too */
  1430.     if (cp)
  1431.       *cp = '\0';
  1432.  
  1433. /* find and stash pointers to remaining new "args" */
  1434.     cp = argv[1];
  1435.     cp++;                /* skip past first char */
  1436.     x = 2;                /* we know argv 0 and 1 already */
  1437.     for (; *cp != '\0'; cp++) {
  1438.       if (*cp == ' ') {
  1439.     *cp = '\0';            /* smash all spaces */
  1440.     continue;
  1441.       } else {
  1442.     if (*(cp-1) == '\0') {
  1443.       argv[x] = cp;
  1444.       x++;
  1445.     }
  1446.       } /* if space */
  1447.     } /* for cp */
  1448.     argc = x;
  1449.   } /* if no args given */
  1450.  
  1451. /* If your shitbox doesn't have getopt, step into the nineties already. */
  1452. /* optarg, optind = next-argv-component [i.e. flag arg]; optopt = last-char */
  1453.   while ((x = getopt (argc, argv, "ae:g:G:hi:lnp:rs:uvw:z")) != EOF) {
  1454. /* Debug (("in go: x now %c, optarg %x optind %d", x, optarg, optind)) */
  1455.     switch (x) {
  1456.       case 'a':
  1457.     bail ("all-A-records NIY");
  1458.     o_alla++; break;
  1459. #ifdef GAPING_SECURITY_HOLE
  1460.       case 'e':                /* prog to exec */
  1461.     pr00gie = optarg;
  1462.     close (0);            /* won't need stdin... */
  1463.     break;
  1464. #endif
  1465.       case 'G':                /* srcrt gateways pointer val */
  1466.     x = atoi (optarg);
  1467.     if ((x) && (x == (x & 0x1c)))    /* mask off bits of fukt values */
  1468.       gatesptr = x;
  1469.     else
  1470.       bail ("invalid hop pointer %d, must be multiple of 4 <= 28", x);
  1471.     break;
  1472.       case 'g':                /* srcroute hop[s] */
  1473.     if (gatesidx > 8)
  1474.       bail ("too many -g hops");
  1475.     if (gates == NULL)        /* eat this, Billy-boy */
  1476.       gates = (HINF **) Hmalloc (sizeof (HINF *) * 10);
  1477.     gp = gethostpoop (optarg, o_nflag);
  1478.     if (gp)
  1479.       gates[gatesidx] = gp;
  1480.     gatesidx++;
  1481.     break;
  1482.       case 'h':
  1483.     errno = 0;
  1484. #ifdef HAVE_HELP
  1485.     helpme();            /* exits by itself */
  1486. #else
  1487.     bail ("no help available, dork -- RTFS");
  1488. #endif
  1489.       case 'i':                /* line-interval time */
  1490. /* xxx: maybe want to parse xxx.yyy, create a timeval, and do microtiming via
  1491.    setitimer instead of alarm/sleep.  [is setitimer at all portable?!]
  1492.    also, the interval time only applies to stdin so far -- do we want it to
  1493.    work for the net, too???  probably not */
  1494.     o_interval = atoi (optarg) & 0xffff;
  1495.     if (! o_interval)
  1496.       bail ("invalid interval time %s", optarg);
  1497.     break;
  1498.       case 'l':                /* listen mode */
  1499.     o_listen++; break;
  1500.       case 'n':                /* numeric-only, no DNS lookups */
  1501.     o_nflag++; break;
  1502.       case 'p':                /* local source port */
  1503.     o_lport = getportpoop (optarg, 0);
  1504.     if (o_lport == 0)
  1505.       bail ("invalid local port %s", optarg);
  1506.     break;
  1507.       case 'r':                /* randomize various things */
  1508.     o_random++; break;
  1509.       case 's':                /* local source address */
  1510. /* do a full lookup [since everything else goes through the same mill],
  1511.    unless -n was previously specified.  In fact, careful placement of -n can
  1512.    be useful, so we'll still pass o_nflag here instead of forcing numeric.  */
  1513.     wherefrom = gethostpoop (optarg, o_nflag);
  1514.     ouraddr = &wherefrom->iaddrs[0];
  1515.     break;
  1516.       case 'u':                /* use UDP */
  1517.     o_udpmode++; break;
  1518.       case 'v':                /* verbose */
  1519.     o_verbose++; break;
  1520.       case 'w':                /* wait time */
  1521.     o_wait = atoi (optarg);
  1522.     if (o_wait <= 0)
  1523.       bail ("invalid wait-time %s", optarg);
  1524.     timer1 = (struct timeval *) Hmalloc (sizeof (struct timeval));
  1525.     timer2 = (struct timeval *) Hmalloc (sizeof (struct timeval));
  1526.     timer1->tv_sec = o_wait;    /* we need two.  see readwrite()... */
  1527.     break;
  1528.       case 'z':                /* little or no data xfer */
  1529.     o_zero++;
  1530.     close (0);            /* won't need stdin */
  1531.     break;
  1532.       default:
  1533.     errno = 0;
  1534.     bail ("nc -h for help");
  1535.     } /* switch x */
  1536.   } /* while getopt */
  1537.  
  1538. /* other misc initialization */
  1539. #ifndef WIN32  // Win32 doesn't like to mix file handles and sockets
  1540.   FD_SET (0, ding1);            /* stdin *is* initially open */
  1541. #endif
  1542.   if (o_random) {
  1543.     SRAND (time (0));
  1544.     randports = Hmalloc (65536);    /* big flag array for ports */
  1545.   }
  1546.   errno = 0;
  1547.  
  1548. /* optind is now index of first non -x arg */
  1549. Debug (("after go: x now %c, optarg %x optind %d", x, optarg, optind))
  1550. /* Debug (("optind up to %d at host-arg %s", optind, argv[optind])) */
  1551. /* gonna only use first addr of host-list, like our IQ was normal; if you wanna
  1552.    get fancy with addresses, look up the list yourself and plug 'em in for now.
  1553.    unless we finally implement -a, that is. */
  1554.   if (argv[optind])
  1555.     whereto = gethostpoop (argv[optind], o_nflag);
  1556.   if (whereto && whereto->iaddrs)
  1557.     themaddr = &whereto->iaddrs[0];
  1558.   if (themaddr)
  1559.     optind++;                /* skip past valid host lookup */
  1560.  
  1561. /* Handle listen mode here, and exit afterward.  Only does one connect;
  1562.    this is arguably the right thing to do.  A "persistent listen-and-fork"
  1563.    mode a la inetd has been thought about, but not implemented.  A tiny
  1564.    wrapper script can handle such things... */
  1565.   if (o_listen) {
  1566.     curport = 0;            /* rem port *can* be zero here... */
  1567.     if (argv[optind]) {            /* any rem-port-args? */
  1568.       curport = getportpoop (argv[optind], 0);
  1569.       if (curport == 0)            /* if given, demand correctness */
  1570.     bail ("invalid port %s", argv[optind]);
  1571.     } /* if port-arg */
  1572.     netfd = dolisten (themaddr, curport, ouraddr, o_lport);
  1573. /* dolisten does its own connect reporting, so we don't holler anything here */
  1574.     if (netfd > 0) {
  1575. #ifdef GAPING_SECURITY_HOLE
  1576.       if (pr00gie)            /* -e given? */
  1577.     doexec (netfd);
  1578. #endif /* GAPING_SECURITY_HOLE */
  1579.       x = readwrite (netfd);        /* it even works with UDP! */
  1580.       exit (x);                /* "pack out yer trash" */
  1581.     } else
  1582.       bail ("no connection");
  1583.   } /* o_listen */
  1584.  
  1585. /* fall thru to outbound connects.  Now we're more picky about args... */
  1586.   if (! themaddr)
  1587.     bail ("no destination");
  1588.   if (argv[optind] == NULL)
  1589.     bail ("no port[s] to connect to");
  1590.   if (argv[optind + 1])        /* look ahead: any more port args given? */
  1591.     Single = 0;                /* multi-mode, case A */
  1592.   ourport = o_lport;            /* which can be 0 */
  1593.  
  1594. /* everything from here down is treated as as ports and/or ranges thereof, so
  1595.    it's all enclosed in this big ol' argv-parsin' loop.  Any randomization is
  1596.    done within each given *range*, but in separate chunks per each succeeding
  1597.    argument, so we can control the pattern somewhat. */
  1598.   while (argv[optind]) {
  1599.     hiport = loport = 0;
  1600.     cp = strchr (argv[optind], '-');    /* nn-mm range? */
  1601.     if (cp) {
  1602.       *cp = '\0';
  1603.       cp++;
  1604.       hiport = getportpoop (cp, 0);
  1605.       if (hiport == 0)
  1606.     bail ("invalid port %s", cp);
  1607.     } /* if found a dash */
  1608.     loport = getportpoop (argv[optind], 0);
  1609.     if (loport == 0)
  1610.       bail ("invalid port %s", argv[optind]);
  1611.     if (hiport > loport) {        /* was it genuinely a range? */
  1612.       Single = 0;            /* multi-mode, case B */
  1613.       curport = hiport;            /* start high by default */
  1614.       if (o_random) {            /* maybe populate the random array */
  1615.     loadports (randports, loport, hiport);
  1616.     curport = nextport (randports);
  1617.       }
  1618.     } else            /* not a range, including args like "25-25" */
  1619.       curport = loport;
  1620. Debug (("Single %d, curport %d", Single, curport))
  1621.  
  1622. /* Now start connecting to these things.  curport is already preloaded. */
  1623.     while (loport <= curport) {
  1624.       if ((! o_lport) && (o_random)) {    /* -p overrides random local-port */
  1625.     ourport = (RAND() & 0xffff);    /* random local-bind -- well above */
  1626.     if (ourport < 8192)        /* resv and any likely listeners??? */
  1627.       ourport += 8192;        /* xxx: may still conflict; use -s? */
  1628.       }
  1629.       curport = getportpoop (NULL, curport);
  1630.       netfd = doconnect (themaddr, curport, ouraddr, ourport);
  1631. Debug (("netfd %d from port %d to port %d", netfd, ourport, curport))
  1632.       if (netfd > 0)
  1633.     if (o_zero && o_udpmode)    /* if UDP scanning... */
  1634.       netfd = udptest (netfd, themaddr);
  1635.       if (netfd > 0) {            /* Yow, are we OPEN YET?! */
  1636.     holler ("%s [%s] %d (%s) open",
  1637.       whereto->name, whereto->addrs[0], curport, portpoop->name);
  1638.     if (! o_zero)
  1639.       x = readwrite (netfd);    /* go shovel shit */
  1640.       } else { /* no netfd... */
  1641.     x = 1;                /* preload exit status for later */
  1642. /* if we're scanning at a "one -v" verbosity level, don't print refusals.
  1643.    Give it another -v if you want to see everything. */
  1644. #ifdef WIN32
  1645.     if ((Single || (o_verbose > 1)) || (h_errno != WSAECONNREFUSED))
  1646. #else
  1647.     if ((Single || (o_verbose > 1)) || (errno != ECONNREFUSED))
  1648. #endif
  1649.       holler ("%s [%s] %d (%s)",
  1650.         whereto->name, whereto->addrs[0], curport, portpoop->name);
  1651.       } /* if netfd */
  1652. #ifdef WIN32
  1653.       closesocket (netfd);            /* just in case we didn't already */
  1654. #else
  1655.       close (netfd);            /* just in case we didn't already */
  1656. #endif
  1657.       if (o_interval)
  1658.     sleep (o_interval);        /* if -i, delay between ports too */
  1659.       if (o_random)
  1660.     curport = nextport (randports);
  1661.       else
  1662.     curport--;            /* just decrement... */
  1663.     } /* while curport within current range */
  1664.     optind++;
  1665.   } /* while remaining port-args -- end of big argv-ports loop*/
  1666.  
  1667.   errno = 0;
  1668.   if (o_verbose > 1)        /* normally we don't care */
  1669.     holler ("sent %d, rcvd %d", wrote_net, wrote_out);
  1670.  
  1671.   if (Single)
  1672.     exit (x);            /* give us status on one connection */
  1673.   exit (0);            /* otherwise, we're just done */
  1674. } /* main */
  1675.  
  1676. #ifdef HAVE_HELP        /* unless we wanna be *really* cryptic */
  1677. /* helpme :
  1678.    the obvious */
  1679. helpme()
  1680. {
  1681.   o_verbose = 1;
  1682.   holler ("\
  1683. connect to somewhere:    nc [-options] hostname port[s] [ports] ... \n\
  1684. listen for inbound:    nc -l -p port [options] [hostname] [port]\n\
  1685. options:");
  1686. #ifdef GAPING_SECURITY_HOLE    /* needs to be separate holler() */
  1687.   holler ("\
  1688.     -e prog            inbound program to exec [dangerous!!]");
  1689. #endif
  1690.   holler ("\
  1691.     -g gateway        source-routing hop point[s], up to 8\n\
  1692.     -G num            source-routing pointer: 4, 8, 12, ...\n\
  1693.     -h            this cruft\n\
  1694.     -i secs            delay interval for lines sent, ports scanned\n\
  1695.     -l            listen mode, for inbound connects\n\
  1696.     -n            numeric-only IP addresses, no DNS\n\
  1697.     -p port            local port number\n\
  1698.     -r            randomize local and remote ports\n\
  1699.     -s addr            local source address\n\
  1700.     -u            UDP mode\n\
  1701.     -v            verbose [use twice to be more verbose]\n\
  1702.     -w secs            timeout for connects and final net reads\n\
  1703.     -z            zero-I/O mode [used for scanning]");
  1704.   bail ("port numbers can be individual or ranges: m-n");
  1705. } /* helpme */
  1706. #endif /* HAVE_HELP */
  1707.  
  1708.  
  1709. /* None genuine without this seal!  _H*/
  1710.