diff options
author | ericj <> | 2000-09-26 05:03:31 +0000 |
---|---|---|
committer | ericj <> | 2000-09-26 05:03:31 +0000 |
commit | 084d07893bb039e0d5a78e6d8b5140017e4a68de (patch) | |
tree | 6f84eabd134a73ae6a916a6028947e141f41044d /src | |
parent | 72f9493edc2b3c0e1077f33e79ba8f7c74bc9894 (diff) | |
download | openbsd-084d07893bb039e0d5a78e6d8b5140017e4a68de.tar.gz openbsd-084d07893bb039e0d5a78e6d8b5140017e4a68de.tar.bz2 openbsd-084d07893bb039e0d5a78e6d8b5140017e4a68de.zip |
Implement a function for errors
it pays attention to h_error so we now have no need to handle
this ourselves.
Diffstat (limited to 'src')
-rw-r--r-- | src/usr.bin/nc/netcat.c | 141 |
1 files changed, 79 insertions, 62 deletions
diff --git a/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c index ad0b5bf149..fb7ac6c69c 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.12 2000/09/26 02:51:22 ericj Exp $ */ | 1 | /* $OpenBSD: netcat.c,v 1.13 2000/09/26 05:03:31 ericj Exp $ */ |
2 | 2 | ||
3 | /* Netcat 1.10 RELEASE 960320 | 3 | /* Netcat 1.10 RELEASE 960320 |
4 | * | 4 | * |
@@ -47,6 +47,7 @@ | |||
47 | #include <setjmp.h> | 47 | #include <setjmp.h> |
48 | #include <signal.h> | 48 | #include <signal.h> |
49 | #include <fcntl.h> | 49 | #include <fcntl.h> |
50 | #include <stdarg.h> | ||
50 | #include <stdlib.h> | 51 | #include <stdlib.h> |
51 | #include <unistd.h> | 52 | #include <unistd.h> |
52 | 53 | ||
@@ -75,15 +76,7 @@ int jval = 0; /* timer crud */ | |||
75 | int netfd = -1; | 76 | int netfd = -1; |
76 | int ofd = 0; /* hexdump output fd */ | 77 | int ofd = 0; /* hexdump output fd */ |
77 | 78 | ||
78 | extern int h_errno; | 79 | /* extern int h_errno; */ |
79 | /* stolen almost wholesale from bsd herror.c */ | ||
80 | static char *h_errs[] = { | ||
81 | "Error 0", /* but we *don't* use this */ | ||
82 | "Unknown host", /* 1 HOST_NOT_FOUND */ | ||
83 | "Host name lookup failure", /* 2 TRY_AGAIN */ | ||
84 | "Unknown server error", /* 3 NO_RECOVERY */ | ||
85 | "No address associated with name", /* 4 NO_ADDRESS */ | ||
86 | }; | ||
87 | 80 | ||
88 | int gatesidx = 0; /* LSRR hop count */ | 81 | int gatesidx = 0; /* LSRR hop count */ |
89 | int gatesptr = 4; /* initial LSRR pointer, settable */ | 82 | int gatesptr = 4; /* initial LSRR pointer, settable */ |
@@ -118,8 +111,9 @@ unsigned int o_wait = 0; | |||
118 | u_short o_zero = 0; | 111 | u_short o_zero = 0; |
119 | 112 | ||
120 | /* Function Prototype's */ | 113 | /* Function Prototype's */ |
121 | void usage __P((int)); | ||
122 | void help __P(()); | 114 | void help __P(()); |
115 | void usage __P((int)); | ||
116 | void nlog __P((int, char *, ...)); | ||
123 | 117 | ||
124 | /* | 118 | /* |
125 | * support routines -- the bulk of this thing. Placed in such an order that | 119 | * support routines -- the bulk of this thing. Placed in such an order that |
@@ -135,8 +129,8 @@ catch() | |||
135 | { | 129 | { |
136 | errno = 0; | 130 | errno = 0; |
137 | if (o_verbose > 1) /* normally we don't care */ | 131 | if (o_verbose > 1) /* normally we don't care */ |
138 | errx(1, "Sent %i Rcvd %i", wrote_net, wrote_out); | 132 | nlog(1, "Sent %i Rcvd %i", wrote_net, wrote_out); |
139 | errx(1, " punt!"); | 133 | nlog(1, " punt!"); |
140 | } | 134 | } |
141 | 135 | ||
142 | /* timeout and other signal handling cruft */ | 136 | /* timeout and other signal handling cruft */ |
@@ -146,7 +140,7 @@ tmtravel() | |||
146 | signal(SIGALRM, SIG_IGN); | 140 | signal(SIGALRM, SIG_IGN); |
147 | alarm(0); | 141 | alarm(0); |
148 | if (jval == 0) | 142 | if (jval == 0) |
149 | errx(1, "spurious timer interrupt!"); | 143 | nlog(1, "spurious timer interrupt!"); |
150 | longjmp(jbuf, jval); | 144 | longjmp(jbuf, jval); |
151 | } | 145 | } |
152 | 146 | ||
@@ -214,7 +208,7 @@ comparehosts(hinfo, hp) | |||
214 | errno = 0; | 208 | errno = 0; |
215 | h_errno = 0; | 209 | h_errno = 0; |
216 | if (strcasecmp(hinfo->name, hp->h_name) != 0) { | 210 | if (strcasecmp(hinfo->name, hp->h_name) != 0) { |
217 | warn("DNS fwd/rev mismatch: %s != %s", hinfo->name, hp->h_name); | 211 | nlog(0, "DNS fwd/rev mismatch: %s != %s", hinfo->name, hp->h_name); |
218 | return (1); | 212 | return (1); |
219 | } | 213 | } |
220 | return (0); | 214 | return (0); |
@@ -243,12 +237,12 @@ gethinfo(name, numeric) | |||
243 | hinfo = (struct host_info *) calloc(1, sizeof(struct host_info)); | 237 | hinfo = (struct host_info *) calloc(1, sizeof(struct host_info)); |
244 | 238 | ||
245 | if (!hinfo) | 239 | if (!hinfo) |
246 | errx(1, "error obtaining host information"); | 240 | nlog(1, "error obtaining host information"); |
247 | 241 | ||
248 | strlcpy(hinfo->name, "(UNKNOWN)", sizeof(hinfo->name)); | 242 | strlcpy(hinfo->name, "(UNKNOWN)", sizeof(hinfo->name)); |
249 | if (inet_aton(name, &iaddr) == 0) { | 243 | if (inet_aton(name, &iaddr) == 0) { |
250 | if (numeric) | 244 | if (numeric) |
251 | errx(1, "Can't parse %s as an IP address", name); | 245 | nlog(1, "Can't parse %s as an IP address", name); |
252 | 246 | ||
253 | /* | 247 | /* |
254 | * failure to look up a name is fatal, | 248 | * failure to look up a name is fatal, |
@@ -256,7 +250,7 @@ gethinfo(name, numeric) | |||
256 | */ | 250 | */ |
257 | hostent = gethostbyname(name); | 251 | hostent = gethostbyname(name); |
258 | if (!hostent) | 252 | if (!hostent) |
259 | errx(1, "%s: forward host lookup failed: ", name); | 253 | nlog(1, "%s: forward host lookup failed: ", name); |
260 | 254 | ||
261 | strlcpy(hinfo->name, hostent->h_name, MAXHOSTNAMELEN); | 255 | strlcpy(hinfo->name, hostent->h_name, MAXHOSTNAMELEN); |
262 | for (x = 0; hostent->h_addr_list[x] && (x < 8); x++) { | 256 | for (x = 0; hostent->h_addr_list[x] && (x < 8); x++) { |
@@ -278,7 +272,7 @@ gethinfo(name, numeric) | |||
278 | hostent = gethostbyaddr((char *) &hinfo->iaddrs[x], | 272 | hostent = gethostbyaddr((char *) &hinfo->iaddrs[x], |
279 | sizeof(struct in_addr), AF_INET); | 273 | sizeof(struct in_addr), AF_INET); |
280 | if ((!hostent) || (!hostent->h_name)) | 274 | if ((!hostent) || (!hostent->h_name)) |
281 | warn("Warning: inverse host lookup failed for %s: ", | 275 | nlog(0, "Warning: inverse host lookup failed for %s: ", |
282 | hinfo->addrs[x]); | 276 | hinfo->addrs[x]); |
283 | else | 277 | else |
284 | (void) comparehosts(hinfo, hostent); | 278 | (void) comparehosts(hinfo, hostent); |
@@ -303,12 +297,12 @@ gethinfo(name, numeric) | |||
303 | * *not* considered fatal | 297 | * *not* considered fatal |
304 | */ | 298 | */ |
305 | if (!hostent) | 299 | if (!hostent) |
306 | warn("%s: inverse host lookup failed: ", name); | 300 | nlog(0, "%s: inverse host lookup failed: ", name); |
307 | else { | 301 | else { |
308 | strlcpy(hinfo->name, hostent->h_name, MAXHOSTNAMELEN); | 302 | strlcpy(hinfo->name, hostent->h_name, MAXHOSTNAMELEN); |
309 | hostent = gethostbyname(hinfo->name); | 303 | hostent = gethostbyname(hinfo->name); |
310 | if ((!hostent) || (!hostent->h_addr_list[0])) | 304 | if ((!hostent) || (!hostent->h_addr_list[0])) |
311 | warn("Warning: forward host lookup failed for %s: ", | 305 | nlog(0, "Warning: forward host lookup failed for %s: ", |
312 | hinfo->name); | 306 | hinfo->name); |
313 | else | 307 | else |
314 | (void) comparehosts(hinfo, hostent); | 308 | (void) comparehosts(hinfo, hostent); |
@@ -364,7 +358,7 @@ getpinfo(pstring, pnum) | |||
364 | if (servent) { | 358 | if (servent) { |
365 | y = ntohs(servent->s_port); | 359 | y = ntohs(servent->s_port); |
366 | if (x != y) /* "never happen" */ | 360 | if (x != y) /* "never happen" */ |
367 | warn("Warning: port-bynum mismatch, %d != %d", x, y); | 361 | nlog(0, "Warning: port-bynum mismatch, %d != %d", x, y); |
368 | strlcpy(pinfo->name, servent->s_name, | 362 | strlcpy(pinfo->name, servent->s_name, |
369 | sizeof(pinfo->name)); | 363 | sizeof(pinfo->name)); |
370 | } | 364 | } |
@@ -466,9 +460,9 @@ loadports(block, lo, hi) | |||
466 | u_short x; | 460 | u_short x; |
467 | 461 | ||
468 | if (!block) | 462 | if (!block) |
469 | errx(1, "loadports: no block?!"); | 463 | nlog(1, "loadports: no block?!"); |
470 | if ((!lo) || (!hi)) | 464 | if ((!lo) || (!hi)) |
471 | errx(1, "loadports: bogus values %d, %d", lo, hi); | 465 | nlog(1, "loadports: bogus values %d, %d", lo, hi); |
472 | x = hi; | 466 | x = hi; |
473 | while (lo <= x) { | 467 | while (lo <= x) { |
474 | block[x] = 1; | 468 | block[x] = 1; |
@@ -505,13 +499,13 @@ newskt: | |||
505 | else | 499 | else |
506 | nnetfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | 500 | nnetfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
507 | if (nnetfd < 0) | 501 | if (nnetfd < 0) |
508 | errx(1, "Can't get socket"); | 502 | nlog(1, "Can't get socket"); |
509 | if (nnetfd == 0) /* if stdin was closed this might *be* 0, */ | 503 | if (nnetfd == 0) /* if stdin was closed this might *be* 0, */ |
510 | goto newskt; /* so grab another. See text for why... */ | 504 | goto newskt; /* so grab another. See text for why... */ |
511 | x = 1; | 505 | x = 1; |
512 | rr = setsockopt(nnetfd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x)); | 506 | rr = setsockopt(nnetfd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x)); |
513 | if (rr == -1) | 507 | if (rr == -1) |
514 | errx(1, NULL); | 508 | nlog(1, NULL); |
515 | 509 | ||
516 | /* fill in all the right sockaddr crud */ | 510 | /* fill in all the right sockaddr crud */ |
517 | lclend->sin_family = AF_INET; | 511 | lclend->sin_family = AF_INET; |
@@ -538,14 +532,14 @@ newskt: | |||
538 | if (errno != EADDRINUSE) | 532 | if (errno != EADDRINUSE) |
539 | break; | 533 | break; |
540 | else { | 534 | else { |
541 | warn("retrying local %s:%d", inet_ntoa(lclend->sin_addr), lp); | 535 | nlog(0, "retrying local %s:%d", inet_ntoa(lclend->sin_addr), lp); |
542 | sleep(2); | 536 | sleep(2); |
543 | errno = 0; /* clear from sleep */ | 537 | errno = 0; /* clear from sleep */ |
544 | } | 538 | } |
545 | } | 539 | } |
546 | } | 540 | } |
547 | if (rr) | 541 | if (rr) |
548 | errx(1, "Can't grab %s:%d with bind", | 542 | nlog(1, "Can't grab %s:%d with bind", |
549 | inet_ntoa(lclend->sin_addr), lp); | 543 | inet_ntoa(lclend->sin_addr), lp); |
550 | 544 | ||
551 | if (o_listen) | 545 | if (o_listen) |
@@ -601,18 +595,18 @@ dolisten(rad, rp, lad, lp) | |||
601 | return (-1); | 595 | return (-1); |
602 | if (o_udpmode) { | 596 | if (o_udpmode) { |
603 | if (!lp) | 597 | if (!lp) |
604 | errx(1, "UDP listen needs -p arg"); | 598 | nlog(1, "UDP listen needs -p arg"); |
605 | } else { | 599 | } else { |
606 | rr = listen(nnetfd, 1); | 600 | rr = listen(nnetfd, 1); |
607 | if (rr < 0) | 601 | if (rr < 0) |
608 | errx(1, "error listening"); | 602 | nlog(1, "error listening"); |
609 | } | 603 | } |
610 | 604 | ||
611 | if (o_verbose) { | 605 | if (o_verbose) { |
612 | x = sizeof(struct sockaddr); | 606 | x = sizeof(struct sockaddr); |
613 | rr = getsockname(nnetfd, (struct sockaddr *) lclend, &x); | 607 | rr = getsockname(nnetfd, (struct sockaddr *) lclend, &x); |
614 | if (rr < 0) | 608 | if (rr < 0) |
615 | warn("local getsockname failed"); | 609 | nlog(0, "local getsockname failed"); |
616 | strcpy(bigbuf_net, "listening on ["); /* buffer reuse... */ | 610 | strcpy(bigbuf_net, "listening on ["); /* buffer reuse... */ |
617 | if (lclend->sin_addr.s_addr) | 611 | if (lclend->sin_addr.s_addr) |
618 | strcat(bigbuf_net, inet_ntoa(lclend->sin_addr)); | 612 | strcat(bigbuf_net, inet_ntoa(lclend->sin_addr)); |
@@ -620,7 +614,7 @@ dolisten(rad, rp, lad, lp) | |||
620 | strcat(bigbuf_net, "any"); | 614 | strcat(bigbuf_net, "any"); |
621 | strcat(bigbuf_net, "] %d ..."); | 615 | strcat(bigbuf_net, "] %d ..."); |
622 | z = ntohs(lclend->sin_port); | 616 | z = ntohs(lclend->sin_port); |
623 | warn("%s %d", bigbuf_net, z); | 617 | nlog(0, "%s %d", bigbuf_net, z); |
624 | } /* verbose -- whew!! */ | 618 | } /* verbose -- whew!! */ |
625 | /* | 619 | /* |
626 | * UDP is a speeeeecial case -- we have to do I/O *and* get the | 620 | * UDP is a speeeeecial case -- we have to do I/O *and* get the |
@@ -671,7 +665,7 @@ whoisit: | |||
671 | x = sizeof(struct sockaddr); | 665 | x = sizeof(struct sockaddr); |
672 | rr = getsockname(nnetfd, (struct sockaddr *) lclend, &x); | 666 | rr = getsockname(nnetfd, (struct sockaddr *) lclend, &x); |
673 | if (rr < 0 && o_verbose) | 667 | if (rr < 0 && o_verbose) |
674 | warn("post-rcv getsockname failed"); | 668 | nlog(0, "post-rcv getsockname failed"); |
675 | strcpy(cp, inet_ntoa(lclend->sin_addr)); | 669 | strcpy(cp, inet_ntoa(lclend->sin_addr)); |
676 | 670 | ||
677 | z = ntohs(remend->sin_port); | 671 | z = ntohs(remend->sin_port); |
@@ -688,11 +682,11 @@ whoisit: | |||
688 | x = 1; | 682 | x = 1; |
689 | } | 683 | } |
690 | if (x) { | 684 | if (x) { |
691 | errx(1, "invalid connection to [%s] from %s [%s] %d", | 685 | nlog(1, "invalid connection to [%s] from %s [%s] %d", |
692 | cp, whozis->name, whozis->addrs[0], z); | 686 | cp, whozis->name, whozis->addrs[0], z); |
693 | } | 687 | } |
694 | if (o_verbose) { | 688 | if (o_verbose) { |
695 | warn("connect to [%s] from %s [%s] %d", | 689 | nlog(0, "connect to [%s] from %s [%s] %d", |
696 | cp, whozis->name, whozis->addrs[0], z); | 690 | cp, whozis->name, whozis->addrs[0], z); |
697 | } | 691 | } |
698 | return (nnetfd); | 692 | return (nnetfd); |
@@ -724,7 +718,7 @@ udptest(fd, where) | |||
724 | 718 | ||
725 | rr = write(fd, bigbuf_in, 1); | 719 | rr = write(fd, bigbuf_in, 1); |
726 | if (rr != 1 && o_verbose) | 720 | if (rr != 1 && o_verbose) |
727 | warn("udptest first write failed?! errno %d", errno); | 721 | nlog(0, "udptest first write failed?! errno %d", errno); |
728 | if (o_wait) | 722 | if (o_wait) |
729 | sleep(o_wait); | 723 | sleep(o_wait); |
730 | else { | 724 | else { |
@@ -772,7 +766,7 @@ oprint(which, buf, n) | |||
772 | unsigned int y; | 766 | unsigned int y; |
773 | 767 | ||
774 | if (!ofd) | 768 | if (!ofd) |
775 | errx(1, "oprint called with no open fd?!"); | 769 | nlog(1, "oprint called with no open fd?!"); |
776 | if (n == 0) | 770 | if (n == 0) |
777 | return; | 771 | return; |
778 | 772 | ||
@@ -835,7 +829,7 @@ oprint(which, buf, n) | |||
835 | *a = '\n'; /* finish the line */ | 829 | *a = '\n'; /* finish the line */ |
836 | x = write(ofd, stage, soc); | 830 | x = write(ofd, stage, soc); |
837 | if (x < 0) | 831 | if (x < 0) |
838 | errx(1, "ofd write err"); | 832 | nlog(1, "ofd write err"); |
839 | } | 833 | } |
840 | } | 834 | } |
841 | 835 | ||
@@ -915,7 +909,7 @@ readwrite(fd) | |||
915 | * *ds1 |= (1 << fd), etc. | 909 | * *ds1 |= (1 << fd), etc. |
916 | */ | 910 | */ |
917 | if (fd > FD_SETSIZE) { | 911 | if (fd > FD_SETSIZE) { |
918 | warn("Preposterous fd value %d", fd); | 912 | nlog(0, "Preposterous fd value %d", fd); |
919 | return (1); | 913 | return (1); |
920 | } | 914 | } |
921 | FD_SET(fd, &fds1); | 915 | FD_SET(fd, &fds1); |
@@ -955,7 +949,7 @@ readwrite(fd) | |||
955 | rr = select(getdtablesize(), &fds2, 0, 0, tv); | 949 | rr = select(getdtablesize(), &fds2, 0, 0, tv); |
956 | if (rr < 0) { | 950 | if (rr < 0) { |
957 | if (errno != EINTR) { | 951 | if (errno != EINTR) { |
958 | warn("Select Failure"); | 952 | nlog(0, "Select Failure"); |
959 | close(fd); | 953 | close(fd); |
960 | return (1); | 954 | return (1); |
961 | } | 955 | } |
@@ -970,7 +964,7 @@ readwrite(fd) | |||
970 | if (!netretry) { | 964 | if (!netretry) { |
971 | if (o_verbose) /* normally we don't | 965 | if (o_verbose) /* normally we don't |
972 | * care */ | 966 | * care */ |
973 | warn("net timeout"); | 967 | nlog(0, "net timeout"); |
974 | close(fd); | 968 | close(fd); |
975 | return (0); /* not an error! */ | 969 | return (0); /* not an error! */ |
976 | } | 970 | } |
@@ -1024,7 +1018,7 @@ shovel: | |||
1024 | } | 1018 | } |
1025 | /* net write retries sometimes happen on UDP connections */ | 1019 | /* net write retries sometimes happen on UDP connections */ |
1026 | if (!wretry) { /* is something hung? */ | 1020 | if (!wretry) { /* is something hung? */ |
1027 | warn("too many output retries"); | 1021 | nlog(0, "too many output retries"); |
1028 | return (1); | 1022 | return (1); |
1029 | } | 1023 | } |
1030 | if (rnleft) { | 1024 | if (rnleft) { |
@@ -1126,7 +1120,7 @@ main(argc, argv) | |||
1126 | * here */ | 1120 | * here */ |
1127 | cp[BIGSIZ-1] = '\0'; | 1121 | cp[BIGSIZ-1] = '\0'; |
1128 | if (insaved <= 0) | 1122 | if (insaved <= 0) |
1129 | errx(1, "wrong"); | 1123 | nlog(1, "wrong"); |
1130 | x = findline(cp, insaved); | 1124 | x = findline(cp, insaved); |
1131 | if (x) | 1125 | if (x) |
1132 | insaved -= x; /* remaining chunk size to be sent */ | 1126 | insaved -= x; /* remaining chunk size to be sent */ |
@@ -1167,11 +1161,11 @@ main(argc, argv) | |||
1167 | if ((x) && (x == (x & 0x1c))) | 1161 | if ((x) && (x == (x & 0x1c))) |
1168 | gatesptr = x; | 1162 | gatesptr = x; |
1169 | else | 1163 | else |
1170 | errx(1, "invalid hop pointer %d, must be multiple of 4 <= 28", x); | 1164 | nlog(1, "invalid hop pointer %d, must be multiple of 4 <= 28", x); |
1171 | break; | 1165 | break; |
1172 | case 'g': /* srcroute hop[s] */ | 1166 | case 'g': /* srcroute hop[s] */ |
1173 | if (gatesidx > 8) | 1167 | if (gatesidx > 8) |
1174 | errx(1, "Too many -g hops!"); | 1168 | nlog(1, "Too many -g hops!"); |
1175 | if (gates == NULL) | 1169 | if (gates == NULL) |
1176 | gates = (struct host_info **) calloc(1, | 1170 | gates = (struct host_info **) calloc(1, |
1177 | sizeof(struct host_info *) * 10); | 1171 | sizeof(struct host_info *) * 10); |
@@ -1186,7 +1180,7 @@ main(argc, argv) | |||
1186 | case 'i': /* line-interval time */ | 1180 | case 'i': /* line-interval time */ |
1187 | o_interval = atoi(optarg) & 0xffff; | 1181 | o_interval = atoi(optarg) & 0xffff; |
1188 | if (!o_interval) | 1182 | if (!o_interval) |
1189 | errx(1, "invalid interval time %s", optarg); | 1183 | nlog(1, "invalid interval time %s", optarg); |
1190 | break; | 1184 | break; |
1191 | case 'l': /* listen mode */ | 1185 | case 'l': /* listen mode */ |
1192 | o_listen++; | 1186 | o_listen++; |
@@ -1201,7 +1195,7 @@ main(argc, argv) | |||
1201 | case 'p': /* local source port */ | 1195 | case 'p': /* local source port */ |
1202 | o_lport = getpinfo(optarg, 0); | 1196 | o_lport = getpinfo(optarg, 0); |
1203 | if (o_lport == 0) | 1197 | if (o_lport == 0) |
1204 | errx(1, "invalid local port %s", optarg); | 1198 | nlog(1, "invalid local port %s", optarg); |
1205 | break; | 1199 | break; |
1206 | case 'r': /* randomize various things */ | 1200 | case 'r': /* randomize various things */ |
1207 | o_random++; | 1201 | o_random++; |
@@ -1230,7 +1224,7 @@ main(argc, argv) | |||
1230 | case 'w': /* wait time */ | 1224 | case 'w': /* wait time */ |
1231 | o_wait = atoi(optarg); | 1225 | o_wait = atoi(optarg); |
1232 | if (o_wait <= 0) | 1226 | if (o_wait <= 0) |
1233 | errx(1, "invalid wait-time %s", optarg); | 1227 | nlog(1, "invalid wait-time %s", optarg); |
1234 | timer1.tv_sec = o_wait; | 1228 | timer1.tv_sec = o_wait; |
1235 | timer1.tv_usec = 0; | 1229 | timer1.tv_usec = 0; |
1236 | break; | 1230 | break; |
@@ -1251,7 +1245,7 @@ main(argc, argv) | |||
1251 | if (o_wfile) { | 1245 | if (o_wfile) { |
1252 | ofd = open(stage, O_WRONLY | O_CREAT | O_TRUNC, 0664); | 1246 | ofd = open(stage, O_WRONLY | O_CREAT | O_TRUNC, 0664); |
1253 | if (ofd <= 0) /* must be > extant 0/1/2 */ | 1247 | if (ofd <= 0) /* must be > extant 0/1/2 */ |
1254 | errx(1, "Can't open %s", stage); | 1248 | nlog(1, "Can't open %s", stage); |
1255 | stage = (unsigned char *) calloc(1, 100); | 1249 | stage = (unsigned char *) calloc(1, 100); |
1256 | } | 1250 | } |
1257 | /* optind is now index of first non -x arg */ | 1251 | /* optind is now index of first non -x arg */ |
@@ -1275,22 +1269,22 @@ main(argc, argv) | |||
1275 | if (argv[optind]) { | 1269 | if (argv[optind]) { |
1276 | curport = getpinfo(argv[optind], 0); | 1270 | curport = getpinfo(argv[optind], 0); |
1277 | if (curport == 0) | 1271 | if (curport == 0) |
1278 | errx(1, "invalid port %s", argv[optind]); | 1272 | nlog(1, "invalid port %s", argv[optind]); |
1279 | } | 1273 | } |
1280 | netfd = dolisten(themaddr, curport, ouraddr, o_lport); | 1274 | netfd = dolisten(themaddr, curport, ouraddr, o_lport); |
1281 | if (netfd > 0) { | 1275 | if (netfd > 0) { |
1282 | x = readwrite(netfd); | 1276 | x = readwrite(netfd); |
1283 | if (o_verbose) | 1277 | if (o_verbose) |
1284 | warn("Sent %i Rcvd %i", wrote_net, wrote_out); | 1278 | nlog(0, "Sent %i Rcvd %i", wrote_net, wrote_out); |
1285 | exit(x); | 1279 | exit(x); |
1286 | } else | 1280 | } else |
1287 | errx(1, "no connection"); | 1281 | nlog(1, "no connection"); |
1288 | } | 1282 | } |
1289 | /* fall thru to outbound connects. Now we're more picky about args... */ | 1283 | /* fall thru to outbound connects. Now we're more picky about args... */ |
1290 | if (!themaddr) | 1284 | if (!themaddr) |
1291 | errx(1, "no destination"); | 1285 | nlog(1, "no destination"); |
1292 | if (argv[optind] == NULL) | 1286 | if (argv[optind] == NULL) |
1293 | errx(1, "no port[s] to connect to"); | 1287 | nlog(1, "no port[s] to connect to"); |
1294 | if (argv[optind + 1]) | 1288 | if (argv[optind + 1]) |
1295 | Single = 0; | 1289 | Single = 0; |
1296 | ourport = o_lport; | 1290 | ourport = o_lport; |
@@ -1303,11 +1297,11 @@ main(argc, argv) | |||
1303 | cp++; | 1297 | cp++; |
1304 | hiport = getpinfo(cp, 0); | 1298 | hiport = getpinfo(cp, 0); |
1305 | if (hiport == 0) | 1299 | if (hiport == 0) |
1306 | errx(1, "invalid port %s", cp); | 1300 | nlog(1, "invalid port %s", cp); |
1307 | } /* if found a dash */ | 1301 | } /* if found a dash */ |
1308 | loport = getpinfo(argv[optind], 0); | 1302 | loport = getpinfo(argv[optind], 0); |
1309 | if (loport == 0) | 1303 | if (loport == 0) |
1310 | errx(1, "invalid port %s", argv[optind]); | 1304 | nlog(1, "invalid port %s", argv[optind]); |
1311 | if (hiport > loport) { /* was it genuinely a range? */ | 1305 | if (hiport > loport) { /* was it genuinely a range? */ |
1312 | Single = 0; /* multi-mode, case B */ | 1306 | Single = 0; /* multi-mode, case B */ |
1313 | if (o_random) { /* maybe populate the random array */ | 1307 | if (o_random) { /* maybe populate the random array */ |
@@ -1330,7 +1324,7 @@ main(argc, argv) | |||
1330 | if (netfd > 0) { | 1324 | if (netfd > 0) { |
1331 | x = 0; | 1325 | x = 0; |
1332 | if (o_verbose) { | 1326 | if (o_verbose) { |
1333 | warn("%s [%s] %d (%s) open", | 1327 | nlog(0, "%s [%s] %d (%s) open", |
1334 | whereto->name, | 1328 | whereto->name, |
1335 | whereto->addrs[0], curport, | 1329 | whereto->addrs[0], curport, |
1336 | pinfo->name); | 1330 | pinfo->name); |
@@ -1341,9 +1335,9 @@ main(argc, argv) | |||
1341 | x = 1; | 1335 | x = 1; |
1342 | if ((Single || (o_verbose > 1)) | 1336 | if ((Single || (o_verbose > 1)) |
1343 | || (errno != ECONNREFUSED)) { | 1337 | || (errno != ECONNREFUSED)) { |
1344 | warn("%s [%s] %d (%s)", | 1338 | nlog(0, "%s [%s] %d (%s)", |
1345 | whereto->name, whereto->addrs[0], | 1339 | whereto->name, whereto->addrs[0], |
1346 | curport, pinfo->name); | 1340 | curport, pinfo->name); |
1347 | } | 1341 | } |
1348 | } | 1342 | } |
1349 | close(netfd); | 1343 | close(netfd); |
@@ -1358,13 +1352,36 @@ main(argc, argv) | |||
1358 | } | 1352 | } |
1359 | 1353 | ||
1360 | errno = 0; | 1354 | errno = 0; |
1361 | if (o_verbose > 1) | 1355 | nlog(0, "Sent %i Rcvd %i", wrote_net, wrote_out); |
1362 | warn("Sent %i Rcvd %i", wrote_net, wrote_out); | ||
1363 | if (Single) | 1356 | if (Single) |
1364 | exit(x); | 1357 | exit(x); |
1365 | exit(0); | 1358 | exit(0); |
1366 | } | 1359 | } |
1367 | 1360 | ||
1361 | /* | ||
1362 | * nlog: | ||
1363 | * dual purpose function, does both warn() and err() | ||
1364 | * and pays attention to o_verbose. | ||
1365 | */ | ||
1366 | void | ||
1367 | nlog(doexit, fmt) | ||
1368 | char *fmt; | ||
1369 | { | ||
1370 | va_list args; | ||
1371 | |||
1372 | if (o_verbose || doexit) { | ||
1373 | va_start(args, fmt); | ||
1374 | vfprintf(stderr, fmt, args); | ||
1375 | if (h_errno) | ||
1376 | herror(NULL); | ||
1377 | else | ||
1378 | putc('\n', stderr); | ||
1379 | } | ||
1380 | |||
1381 | if (doexit) | ||
1382 | exit(1); | ||
1383 | } | ||
1384 | |||
1368 | void | 1385 | void |
1369 | usage(doexit) | 1386 | usage(doexit) |
1370 | { | 1387 | { |