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