[BACK]Return to netcat.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / nc

Diff for /src/usr.bin/nc/netcat.c between version 1.16 and 1.17

version 1.16, 2000/09/26 17:46:40 version 1.17, 2000/09/26 18:53:13
Line 29 
Line 29 
 */  */
   
   
 #define HAVE_BIND               /* ASSUMPTION -- seems to work everywhere! */  
   
 #include <sys/types.h>  #include <sys/types.h>
 #include <sys/time.h>  #include <sys/time.h>
 #include <sys/select.h>  #include <sys/select.h>
Line 105 
Line 103 
 u_short  o_zero = 0;  u_short  o_zero = 0;
   
 /* Function Prototype's */  /* Function Prototype's */
 void    help    __P(());  void    help __P(());
 void    usage   __P((int));  
 void    nlog __P((int, char *, ...));  void    nlog __P((int, char *, ...));
   void    usage __P((int));
   
 /*  /*
  * support routines -- the bulk of this thing.  Placed in such an order that   * support routines -- the bulk of this thing.  Placed in such an order that
Line 121 
Line 119 
 void  void
 catch()  catch()
 {  {
         errno = 0;          if (o_verbose)  /* normally we don't care */
         if (o_verbose > 1)      /* normally we don't care */  
                 nlog(1, "Sent %i Rcvd %i", wrote_net, wrote_out);                  nlog(1, "Sent %i Rcvd %i", wrote_net, wrote_out);
         nlog(1, " punt!");          nlog(1, " punt!");
 }  }
Line 199 
Line 196 
         struct host_info   *hinfo;          struct host_info   *hinfo;
         struct hostent *hp;          struct hostent *hp;
 {  {
         errno = 0;  
         if (strcasecmp(hinfo->name, hp->h_name) != 0) {          if (strcasecmp(hinfo->name, hp->h_name) != 0) {
                 nlog(0, "DNS fwd/rev mismatch: %s != %s", hinfo->name, hp->h_name);                  nlog(0, "DNS fwd/rev mismatch: %s != %s", hinfo->name, hp->h_name);
                 return (1);                  return (1);
Line 224 
Line 220 
         struct host_info *hinfo = NULL;          struct host_info *hinfo = NULL;
         int x;          int x;
   
         errno = 0;  
         if (name)          if (name)
                 hinfo = (struct host_info *) calloc(1, sizeof(struct host_info));                  hinfo = (struct host_info *) calloc(1, sizeof(struct host_info));
   
Line 325 
Line 320 
         struct servent *servent;          struct servent *servent;
         int x;          int x;
         int y;          int y;
         char   *whichp = "tcp";  
         if (o_udpmode)  
                 whichp = "udp";  
   
         pinfo->name[0] = '?';/* fast preload */          pinfo->name[0] = '?';/* fast preload */
         pinfo->name[1] = '\0';          pinfo->name[1] = '\0';
Line 345 
Line 337 
                 if (o_nflag)    /* go faster, skip getservbyblah */                  if (o_nflag)    /* go faster, skip getservbyblah */
                         goto gp_finish;                          goto gp_finish;
                 y = htons(x);   /* gotta do this -- see Fig.1 below */                  y = htons(x);   /* gotta do this -- see Fig.1 below */
                 servent = getservbyport(y, whichp);                  servent = getservbyport(y, o_udpmode ? "udp" : "tcp");
                 if (servent) {                  if (servent) {
                         y = ntohs(servent->s_port);                          y = ntohs(servent->s_port);
                         if (x != y)     /* "never happen" */                          if (x != y)
                                 nlog(0, "Warning: port-bynum mismatch, %d != %d", x, y);                                  nlog(0, "Warning: port-bynum mismatch, %d != %d", x, y);
                         strlcpy(pinfo->name, servent->s_name,                          strlcpy(pinfo->name, servent->s_name,
                             sizeof(pinfo->name));                              sizeof(pinfo->name));
Line 371 
Line 363 
                 if (x)                  if (x)
                         return (getpinfo(NULL, x));     /* recurse for                          return (getpinfo(NULL, x));     /* recurse for
                                                          * numeric-string-arg */                                                           * numeric-string-arg */
                 if (o_nflag)    /* can't use names! */                  if (o_nflag)
                         return (0);                          return (0);
                 servent = getservbyname(pstring, whichp);                  servent = getservbyname(pstring, o_udpmode ? "udp" : "tcp");
                 if (servent) {                  if (servent) {
                         strlcpy(pinfo->name, servent->s_name,                          strlcpy(pinfo->name, servent->s_name,
                             sizeof(pinfo->name));                              sizeof(pinfo->name));
Line 481 
Line 473 
         int nnetfd = 0;          int nnetfd = 0;
         int rr;          int rr;
         int     x, y;          int     x, y;
         errno = 0;  
   
         /* grab a socket; set opts */          /* grab a socket; set opts */
         while (nnetfd == 0) {          while (nnetfd == 0) {
Line 522 
Line 513 
                         else {                          else {
                                 nlog(0, "retrying local %s:%d", inet_ntoa(lclend->sin_addr), lp);                                  nlog(0, "retrying local %s:%d", inet_ntoa(lclend->sin_addr), lp);
                                 sleep(2);                                  sleep(2);
                                 errno = 0;      /* clear from sleep */  
                         }                          }
                 }                  }
         }          }
Line 572 
Line 562 
         int     x;          int     x;
         char   *cp;          char   *cp;
         u_short  z;          u_short  z;
         errno = 0;  
   
         /*          /*
          * Pass everything off to doconnect,           * Pass everything off to doconnect,
Line 652 
Line 641 
         cp = &bigbuf_net[32];          cp = &bigbuf_net[32];
         x = sizeof(struct sockaddr);          x = sizeof(struct sockaddr);
         rr = getsockname(nnetfd, (struct sockaddr *) lclend, &x);          rr = getsockname(nnetfd, (struct sockaddr *) lclend, &x);
         if (rr < 0 && o_verbose)          if (rr < 0)
                 nlog(0, "post-rcv getsockname failed");                  nlog(0, "post-rcv getsockname failed");
         strcpy(cp, inet_ntoa(lclend->sin_addr));          strcpy(cp, inet_ntoa(lclend->sin_addr));
   
         z = ntohs(remend->sin_port);          z = ntohs(remend->sin_port);
         strcpy(bigbuf_net, inet_ntoa(remend->sin_addr));          strcpy(bigbuf_net, inet_ntoa(remend->sin_addr));
         whozis = gethinfo(bigbuf_net, o_nflag);          whozis = gethinfo(bigbuf_net, o_nflag);
         errno = 0;  
         x = 0;          x = 0;
         if (rad)                /* xxx: fix to go down the *list* if we have          if (rad)                /* xxx: fix to go down the *list* if we have
                                  * one? */                                   * one? */
Line 705 
Line 693 
         int rr;          int rr;
   
         rr = write(fd, bigbuf_in, 1);          rr = write(fd, bigbuf_in, 1);
         if (rr != 1 && o_verbose)          if (rr != 1)
                 nlog(0, "udptest first write failed?! errno %d", errno);                  nlog(0, "udptest first write failed: ");
         if (o_wait)          if (o_wait)
                 sleep(o_wait);                  sleep(o_wait);
         else {          else {
Line 718 
Line 706 
                 o_wait = 0;                  o_wait = 0;
                 o_udpmode++;                  o_udpmode++;
         }          }
         errno = 0;  
         rr = write(fd, bigbuf_in, 1);          rr = write(fd, bigbuf_in, 1);
         if (rr == 1)          if (rr == 1)
                 return (fd);                  return (fd);
Line 727 
Line 714 
 }  }
   
 /*  /*
  *  oprint :   * oprint :
  * Hexdump bytes shoveled either way to a running logfile, in the format:   * Hexdump bytes shoveled either way to a running logfile, in the format:
  * D offset       -  - - - --- 16 bytes --- - - -  -     # .... ascii .....   * D offset       -  - - - --- 16 bytes --- - - -  -     # .... ascii .....
  * where "which" sets the direction indicator, D:   * where "which" sets the direction indicator, D:
Line 918 
Line 905 
         }          }
         if (o_interval)          if (o_interval)
                 sleep(o_interval);                  sleep(o_interval);
         errno = 0;  
   
         while (FD_ISSET(fd, &fds1)) {   /* i.e. till the *net* closes! */          while (FD_ISSET(fd, &fds1)) {   /* i.e. till the *net* closes! */
                 struct timeval *tv;                  struct timeval *tv;
Line 1035 
Line 1021 
                 }                  }
                 if (o_interval) {                  if (o_interval) {
                         sleep(o_interval);                          sleep(o_interval);
                         errno = 0;  
                         continue;                          continue;
                 }                  }
                 if ((rzleft) || (rnleft)) {                  if ((rzleft) || (rnleft)) {
Line 1069 
Line 1054 
         u_short  curport = 0;          u_short  curport = 0;
         char   *randports = NULL;          char   *randports = NULL;
   
 #ifdef HAVE_BIND  
         res_init();          res_init();
 #endif  
         lclend = (struct sockaddr_in *) calloc(1, sizeof(struct sockaddr));          lclend = (struct sockaddr_in *) calloc(1, sizeof(struct sockaddr));
         remend = (struct sockaddr_in *) calloc(1, sizeof(struct sockaddr));          remend = (struct sockaddr_in *) calloc(1, sizeof(struct sockaddr));
         bigbuf_in = calloc(1, BIGSIZ);          bigbuf_in = calloc(1, BIGSIZ);
         bigbuf_net = calloc(1, BIGSIZ);          bigbuf_net = calloc(1, BIGSIZ);
         pinfo= (struct port_info *) calloc(1, sizeof(struct port_info));          pinfo= (struct port_info *) calloc(1, sizeof(struct port_info));
   
         errno = 0;  
         gatesptr = 4;          gatesptr = 4;
   
         /*          /*
Line 1229 
Line 1212 
         }          }
   
         /* other misc initialization */          /* other misc initialization */
             FD_SET(0, &fds1);   /* stdin *is* initially open */          FD_SET(0, &fds1);       /* stdin *is* initially open */
         if (o_random) {          if (o_random) {
                 randports = calloc(1, 65536);   /* big flag array for ports */                  randports = calloc(1, 65536);   /* big flag array for ports */
         }          }
         if (o_wfile) {          if (o_wfile) {
                 ofd = open(stage, O_WRONLY | O_CREAT | O_TRUNC, 0664);                  ofd = open(stage, O_WRONLY | O_CREAT | O_TRUNC, 0664);
                 if (ofd <= 0)   /* must be > extant 0/1/2 */                  if (ofd <= 0)   /* must be > extant 0/1/2 */
                         nlog(1, "Can't open %s", stage);                          nlog(1, "%s: ", stage);
                 stage = (unsigned char *) calloc(1, 100);                  stage = (unsigned char *) calloc(1, 100);
         }          }
         /* optind is now index of first non -x arg */          /* optind is now index of first non -x arg */
Line 1246 
Line 1229 
                 themaddr = &whereto->iaddrs[0];                  themaddr = &whereto->iaddrs[0];
         if (themaddr)          if (themaddr)
                 optind++;       /* skip past valid host lookup */                  optind++;       /* skip past valid host lookup */
         errno = 0;  
   
         /*          /*
          * Handle listen mode here, and exit afterward.  Only does one connect;           * Handle listen mode here, and exit afterward.  Only does one connect;
Line 1281 
Line 1263 
   
         while (argv[optind]) {          while (argv[optind]) {
                 hiport = loport = 0;                  hiport = loport = 0;
                 cp = strchr(argv[optind], '-');                  if (cp = strchr(argv[optind], '-')) {
                 if (cp) {  
                         *cp = '\0';                          *cp = '\0';
                         cp++;                          cp++;
                         hiport = getpinfo(cp, 0);                          hiport = getpinfo(cp, 0);
                         if (hiport == 0)                          if (hiport == 0)
                                 nlog(1, "invalid port %s", cp);                                  nlog(1, "invalid port %s", cp);
                 }               /* if found a dash */                  }
                 loport = getpinfo(argv[optind], 0);                  loport = getpinfo(argv[optind], 0);
                 if (loport == 0)                  if (loport == 0)
                         nlog(1, "invalid port %s", argv[optind]);                          nlog(1, "invalid port %s", argv[optind]);
Line 1299 
Line 1280 
                                 curport = nextport(randports);                                  curport = nextport(randports);
                         } else                          } else
                                 curport = hiport;                                  curport = hiport;
                 } else          /* not a range, including args like "25-25" */                  } else
                         curport = loport;                          curport = loport;
                     /*                      /*
                      * Now start connecting to these things.                       * Now start connecting to these things.
Line 1323 
Line 1304 
                                         x = readwrite(netfd);                                          x = readwrite(netfd);
                         } else {                          } else {
                                 x = 1;                                  x = 1;
                                 if ((Single || (o_verbose > 1))                                  if ((Single || (o_verbose))
                                    || (errno != ECONNREFUSED)) {                                     || (errno != ECONNREFUSED)) {
                                         nlog(0, "%s [%s] %d (%s)",                                          nlog(0, "%s [%s] %d (%s)",
                                              whereto->name, whereto->addrs[0],                                               whereto->name, whereto->addrs[0],
Line 1337 
Line 1318 
                                 curport = nextport(randports);                                  curport = nextport(randports);
                         else                          else
                                 curport--;                                  curport--;
                 }                      }
                 optind++;                  optind++;
         }          }
   
         errno = 0;  
         nlog(0, "Sent %i Rcvd %i", wrote_net, wrote_out);          nlog(0, "Sent %i Rcvd %i", wrote_net, wrote_out);
         if (Single)          if (Single)
                 exit(x);                  exit(x);
Line 1364 
Line 1344 
                 vfprintf(stderr, fmt, args);                  vfprintf(stderr, fmt, args);
                 if (h_errno)                  if (h_errno)
                         herror(NULL);                          herror(NULL);
                   else if (errno)
                           fprintf(stderr, "%s\n", strerror(errno));
                 else                  else
                         putc('\n', stderr);                          putc('\n', stderr);
                 va_end(args);                  va_end(args);
Line 1371 
Line 1353 
   
         if (doexit)          if (doexit)
                 exit(1);                  exit(1);
   
           h_errno = errno = 0;
 }  }
   
 void  void

Legend:
Removed from v.1.16  
changed lines
  Added in v.1.17