summaryrefslogtreecommitdiff
path: root/src/usr.bin
diff options
context:
space:
mode:
authorericj <>2000-09-26 18:53:13 +0000
committerericj <>2000-09-26 18:53:13 +0000
commitbac60cdd71e32a92a54762886ece2fab5e012b00 (patch)
tree577ce1f3fd5ae352f9d60dabd56d540d746020a2 /src/usr.bin
parentafba3ddf77a34cc5915c1fd95dff2d4babc98a81 (diff)
downloadopenbsd-bac60cdd71e32a92a54762886ece2fab5e012b00.tar.gz
openbsd-bac60cdd71e32a92a54762886ece2fab5e012b00.tar.bz2
openbsd-bac60cdd71e32a92a54762886ece2fab5e012b00.zip
more cleanup and have nlog() deal with errno now as well.
Diffstat (limited to 'src/usr.bin')
-rw-r--r--src/usr.bin/nc/netcat.c64
1 files changed, 24 insertions, 40 deletions
diff --git a/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c
index cc62f4a683..6a7b1bc0d6 100644
--- a/src/usr.bin/nc/netcat.c
+++ b/src/usr.bin/nc/netcat.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: netcat.c,v 1.16 2000/09/26 17:46:40 ericj Exp $ */ 1/* $OpenBSD: netcat.c,v 1.17 2000/09/26 18:53:13 ericj Exp $ */
2 2
3/* Netcat 1.10 RELEASE 960320 3/* Netcat 1.10 RELEASE 960320
4 * 4 *
@@ -29,8 +29,6 @@
29*/ 29*/
30 30
31 31
32#define HAVE_BIND /* ASSUMPTION -- seems to work everywhere! */
33
34#include <sys/types.h> 32#include <sys/types.h>
35#include <sys/time.h> 33#include <sys/time.h>
36#include <sys/select.h> 34#include <sys/select.h>
@@ -105,9 +103,9 @@ unsigned int o_wait = 0;
105u_short o_zero = 0; 103u_short o_zero = 0;
106 104
107/* Function Prototype's */ 105/* Function Prototype's */
108void help __P(()); 106void help __P(());
109void usage __P((int));
110void nlog __P((int, char *, ...)); 107void nlog __P((int, char *, ...));
108void usage __P((int));
111 109
112/* 110/*
113 * support routines -- the bulk of this thing. Placed in such an order that 111 * support routines -- the bulk of this thing. Placed in such an order that
@@ -121,8 +119,7 @@ void nlog __P((int, char *, ...));
121void 119void
122catch() 120catch()
123{ 121{
124 errno = 0; 122 if (o_verbose) /* normally we don't care */
125 if (o_verbose > 1) /* normally we don't care */
126 nlog(1, "Sent %i Rcvd %i", wrote_net, wrote_out); 123 nlog(1, "Sent %i Rcvd %i", wrote_net, wrote_out);
127 nlog(1, " punt!"); 124 nlog(1, " punt!");
128} 125}
@@ -199,7 +196,6 @@ comparehosts(hinfo, hp)
199 struct host_info *hinfo; 196 struct host_info *hinfo;
200 struct hostent *hp; 197 struct hostent *hp;
201{ 198{
202 errno = 0;
203 if (strcasecmp(hinfo->name, hp->h_name) != 0) { 199 if (strcasecmp(hinfo->name, hp->h_name) != 0) {
204 nlog(0, "DNS fwd/rev mismatch: %s != %s", hinfo->name, hp->h_name); 200 nlog(0, "DNS fwd/rev mismatch: %s != %s", hinfo->name, hp->h_name);
205 return (1); 201 return (1);
@@ -224,7 +220,6 @@ gethinfo(name, numeric)
224 struct host_info *hinfo = NULL; 220 struct host_info *hinfo = NULL;
225 int x; 221 int x;
226 222
227 errno = 0;
228 if (name) 223 if (name)
229 hinfo = (struct host_info *) calloc(1, sizeof(struct host_info)); 224 hinfo = (struct host_info *) calloc(1, sizeof(struct host_info));
230 225
@@ -325,9 +320,6 @@ getpinfo(pstring, pnum)
325 struct servent *servent; 320 struct servent *servent;
326 int x; 321 int x;
327 int y; 322 int y;
328 char *whichp = "tcp";
329 if (o_udpmode)
330 whichp = "udp";
331 323
332 pinfo->name[0] = '?';/* fast preload */ 324 pinfo->name[0] = '?';/* fast preload */
333 pinfo->name[1] = '\0'; 325 pinfo->name[1] = '\0';
@@ -345,10 +337,10 @@ getpinfo(pstring, pnum)
345 if (o_nflag) /* go faster, skip getservbyblah */ 337 if (o_nflag) /* go faster, skip getservbyblah */
346 goto gp_finish; 338 goto gp_finish;
347 y = htons(x); /* gotta do this -- see Fig.1 below */ 339 y = htons(x); /* gotta do this -- see Fig.1 below */
348 servent = getservbyport(y, whichp); 340 servent = getservbyport(y, o_udpmode ? "udp" : "tcp");
349 if (servent) { 341 if (servent) {
350 y = ntohs(servent->s_port); 342 y = ntohs(servent->s_port);
351 if (x != y) /* "never happen" */ 343 if (x != y)
352 nlog(0, "Warning: port-bynum mismatch, %d != %d", x, y); 344 nlog(0, "Warning: port-bynum mismatch, %d != %d", x, y);
353 strlcpy(pinfo->name, servent->s_name, 345 strlcpy(pinfo->name, servent->s_name,
354 sizeof(pinfo->name)); 346 sizeof(pinfo->name));
@@ -371,9 +363,9 @@ getpinfo(pstring, pnum)
371 if (x) 363 if (x)
372 return (getpinfo(NULL, x)); /* recurse for 364 return (getpinfo(NULL, x)); /* recurse for
373 * numeric-string-arg */ 365 * numeric-string-arg */
374 if (o_nflag) /* can't use names! */ 366 if (o_nflag)
375 return (0); 367 return (0);
376 servent = getservbyname(pstring, whichp); 368 servent = getservbyname(pstring, o_udpmode ? "udp" : "tcp");
377 if (servent) { 369 if (servent) {
378 strlcpy(pinfo->name, servent->s_name, 370 strlcpy(pinfo->name, servent->s_name,
379 sizeof(pinfo->name)); 371 sizeof(pinfo->name));
@@ -481,7 +473,6 @@ doconnect(rad, rp, lad, lp)
481 int nnetfd = 0; 473 int nnetfd = 0;
482 int rr; 474 int rr;
483 int x, y; 475 int x, y;
484 errno = 0;
485 476
486 /* grab a socket; set opts */ 477 /* grab a socket; set opts */
487 while (nnetfd == 0) { 478 while (nnetfd == 0) {
@@ -522,7 +513,6 @@ doconnect(rad, rp, lad, lp)
522 else { 513 else {
523 nlog(0, "retrying local %s:%d", inet_ntoa(lclend->sin_addr), lp); 514 nlog(0, "retrying local %s:%d", inet_ntoa(lclend->sin_addr), lp);
524 sleep(2); 515 sleep(2);
525 errno = 0; /* clear from sleep */
526 } 516 }
527 } 517 }
528 } 518 }
@@ -572,7 +562,6 @@ dolisten(rad, rp, lad, lp)
572 int x; 562 int x;
573 char *cp; 563 char *cp;
574 u_short z; 564 u_short z;
575 errno = 0;
576 565
577 /* 566 /*
578 * Pass everything off to doconnect, 567 * Pass everything off to doconnect,
@@ -652,14 +641,13 @@ whoisit:
652 cp = &bigbuf_net[32]; 641 cp = &bigbuf_net[32];
653 x = sizeof(struct sockaddr); 642 x = sizeof(struct sockaddr);
654 rr = getsockname(nnetfd, (struct sockaddr *) lclend, &x); 643 rr = getsockname(nnetfd, (struct sockaddr *) lclend, &x);
655 if (rr < 0 && o_verbose) 644 if (rr < 0)
656 nlog(0, "post-rcv getsockname failed"); 645 nlog(0, "post-rcv getsockname failed");
657 strcpy(cp, inet_ntoa(lclend->sin_addr)); 646 strcpy(cp, inet_ntoa(lclend->sin_addr));
658 647
659 z = ntohs(remend->sin_port); 648 z = ntohs(remend->sin_port);
660 strcpy(bigbuf_net, inet_ntoa(remend->sin_addr)); 649 strcpy(bigbuf_net, inet_ntoa(remend->sin_addr));
661 whozis = gethinfo(bigbuf_net, o_nflag); 650 whozis = gethinfo(bigbuf_net, o_nflag);
662 errno = 0;
663 x = 0; 651 x = 0;
664 if (rad) /* xxx: fix to go down the *list* if we have 652 if (rad) /* xxx: fix to go down the *list* if we have
665 * one? */ 653 * one? */
@@ -705,8 +693,8 @@ udptest(fd, where)
705 int rr; 693 int rr;
706 694
707 rr = write(fd, bigbuf_in, 1); 695 rr = write(fd, bigbuf_in, 1);
708 if (rr != 1 && o_verbose) 696 if (rr != 1)
709 nlog(0, "udptest first write failed?! errno %d", errno); 697 nlog(0, "udptest first write failed: ");
710 if (o_wait) 698 if (o_wait)
711 sleep(o_wait); 699 sleep(o_wait);
712 else { 700 else {
@@ -718,7 +706,6 @@ udptest(fd, where)
718 o_wait = 0; 706 o_wait = 0;
719 o_udpmode++; 707 o_udpmode++;
720 } 708 }
721 errno = 0;
722 rr = write(fd, bigbuf_in, 1); 709 rr = write(fd, bigbuf_in, 1);
723 if (rr == 1) 710 if (rr == 1)
724 return (fd); 711 return (fd);
@@ -727,7 +714,7 @@ udptest(fd, where)
727} 714}
728 715
729/* 716/*
730 * oprint : 717 * oprint :
731 * Hexdump bytes shoveled either way to a running logfile, in the format: 718 * Hexdump bytes shoveled either way to a running logfile, in the format:
732 * D offset - - - - --- 16 bytes --- - - - - # .... ascii ..... 719 * D offset - - - - --- 16 bytes --- - - - - # .... ascii .....
733 * where "which" sets the direction indicator, D: 720 * where "which" sets the direction indicator, D:
@@ -918,7 +905,6 @@ readwrite(fd)
918 } 905 }
919 if (o_interval) 906 if (o_interval)
920 sleep(o_interval); 907 sleep(o_interval);
921 errno = 0;
922 908
923 while (FD_ISSET(fd, &fds1)) { /* i.e. till the *net* closes! */ 909 while (FD_ISSET(fd, &fds1)) { /* i.e. till the *net* closes! */
924 struct timeval *tv; 910 struct timeval *tv;
@@ -1035,7 +1021,6 @@ shovel:
1035 } 1021 }
1036 if (o_interval) { 1022 if (o_interval) {
1037 sleep(o_interval); 1023 sleep(o_interval);
1038 errno = 0;
1039 continue; 1024 continue;
1040 } 1025 }
1041 if ((rzleft) || (rnleft)) { 1026 if ((rzleft) || (rnleft)) {
@@ -1069,16 +1054,14 @@ main(argc, argv)
1069 u_short curport = 0; 1054 u_short curport = 0;
1070 char *randports = NULL; 1055 char *randports = NULL;
1071 1056
1072#ifdef HAVE_BIND
1073 res_init(); 1057 res_init();
1074#endif 1058
1075 lclend = (struct sockaddr_in *) calloc(1, sizeof(struct sockaddr)); 1059 lclend = (struct sockaddr_in *) calloc(1, sizeof(struct sockaddr));
1076 remend = (struct sockaddr_in *) calloc(1, sizeof(struct sockaddr)); 1060 remend = (struct sockaddr_in *) calloc(1, sizeof(struct sockaddr));
1077 bigbuf_in = calloc(1, BIGSIZ); 1061 bigbuf_in = calloc(1, BIGSIZ);
1078 bigbuf_net = calloc(1, BIGSIZ); 1062 bigbuf_net = calloc(1, BIGSIZ);
1079 pinfo= (struct port_info *) calloc(1, sizeof(struct port_info)); 1063 pinfo= (struct port_info *) calloc(1, sizeof(struct port_info));
1080 1064
1081 errno = 0;
1082 gatesptr = 4; 1065 gatesptr = 4;
1083 1066
1084 /* 1067 /*
@@ -1229,14 +1212,14 @@ main(argc, argv)
1229 } 1212 }
1230 1213
1231 /* other misc initialization */ 1214 /* other misc initialization */
1232 FD_SET(0, &fds1); /* stdin *is* initially open */ 1215 FD_SET(0, &fds1); /* stdin *is* initially open */
1233 if (o_random) { 1216 if (o_random) {
1234 randports = calloc(1, 65536); /* big flag array for ports */ 1217 randports = calloc(1, 65536); /* big flag array for ports */
1235 } 1218 }
1236 if (o_wfile) { 1219 if (o_wfile) {
1237 ofd = open(stage, O_WRONLY | O_CREAT | O_TRUNC, 0664); 1220 ofd = open(stage, O_WRONLY | O_CREAT | O_TRUNC, 0664);
1238 if (ofd <= 0) /* must be > extant 0/1/2 */ 1221 if (ofd <= 0) /* must be > extant 0/1/2 */
1239 nlog(1, "Can't open %s", stage); 1222 nlog(1, "%s: ", stage);
1240 stage = (unsigned char *) calloc(1, 100); 1223 stage = (unsigned char *) calloc(1, 100);
1241 } 1224 }
1242 /* optind is now index of first non -x arg */ 1225 /* optind is now index of first non -x arg */
@@ -1246,7 +1229,6 @@ main(argc, argv)
1246 themaddr = &whereto->iaddrs[0]; 1229 themaddr = &whereto->iaddrs[0];
1247 if (themaddr) 1230 if (themaddr)
1248 optind++; /* skip past valid host lookup */ 1231 optind++; /* skip past valid host lookup */
1249 errno = 0;
1250 1232
1251 /* 1233 /*
1252 * Handle listen mode here, and exit afterward. Only does one connect; 1234 * Handle listen mode here, and exit afterward. Only does one connect;
@@ -1281,14 +1263,13 @@ main(argc, argv)
1281 1263
1282 while (argv[optind]) { 1264 while (argv[optind]) {
1283 hiport = loport = 0; 1265 hiport = loport = 0;
1284 cp = strchr(argv[optind], '-'); 1266 if (cp = strchr(argv[optind], '-')) {
1285 if (cp) {
1286 *cp = '\0'; 1267 *cp = '\0';
1287 cp++; 1268 cp++;
1288 hiport = getpinfo(cp, 0); 1269 hiport = getpinfo(cp, 0);
1289 if (hiport == 0) 1270 if (hiport == 0)
1290 nlog(1, "invalid port %s", cp); 1271 nlog(1, "invalid port %s", cp);
1291 } /* if found a dash */ 1272 }
1292 loport = getpinfo(argv[optind], 0); 1273 loport = getpinfo(argv[optind], 0);
1293 if (loport == 0) 1274 if (loport == 0)
1294 nlog(1, "invalid port %s", argv[optind]); 1275 nlog(1, "invalid port %s", argv[optind]);
@@ -1299,7 +1280,7 @@ main(argc, argv)
1299 curport = nextport(randports); 1280 curport = nextport(randports);
1300 } else 1281 } else
1301 curport = hiport; 1282 curport = hiport;
1302 } else /* not a range, including args like "25-25" */ 1283 } else
1303 curport = loport; 1284 curport = loport;
1304 /* 1285 /*
1305 * Now start connecting to these things. 1286 * Now start connecting to these things.
@@ -1323,7 +1304,7 @@ main(argc, argv)
1323 x = readwrite(netfd); 1304 x = readwrite(netfd);
1324 } else { 1305 } else {
1325 x = 1; 1306 x = 1;
1326 if ((Single || (o_verbose > 1)) 1307 if ((Single || (o_verbose))
1327 || (errno != ECONNREFUSED)) { 1308 || (errno != ECONNREFUSED)) {
1328 nlog(0, "%s [%s] %d (%s)", 1309 nlog(0, "%s [%s] %d (%s)",
1329 whereto->name, whereto->addrs[0], 1310 whereto->name, whereto->addrs[0],
@@ -1337,11 +1318,10 @@ main(argc, argv)
1337 curport = nextport(randports); 1318 curport = nextport(randports);
1338 else 1319 else
1339 curport--; 1320 curport--;
1340 } 1321 }
1341 optind++; 1322 optind++;
1342 } 1323 }
1343 1324
1344 errno = 0;
1345 nlog(0, "Sent %i Rcvd %i", wrote_net, wrote_out); 1325 nlog(0, "Sent %i Rcvd %i", wrote_net, wrote_out);
1346 if (Single) 1326 if (Single)
1347 exit(x); 1327 exit(x);
@@ -1364,6 +1344,8 @@ nlog(doexit, fmt)
1364 vfprintf(stderr, fmt, args); 1344 vfprintf(stderr, fmt, args);
1365 if (h_errno) 1345 if (h_errno)
1366 herror(NULL); 1346 herror(NULL);
1347 else if (errno)
1348 fprintf(stderr, "%s\n", strerror(errno));
1367 else 1349 else
1368 putc('\n', stderr); 1350 putc('\n', stderr);
1369 va_end(args); 1351 va_end(args);
@@ -1371,6 +1353,8 @@ nlog(doexit, fmt)
1371 1353
1372 if (doexit) 1354 if (doexit)
1373 exit(1); 1355 exit(1);
1356
1357 h_errno = errno = 0;
1374} 1358}
1375 1359
1376void 1360void