diff options
| author | vincent <> | 2002-07-04 04:42:25 +0000 |
|---|---|---|
| committer | vincent <> | 2002-07-04 04:42:25 +0000 |
| commit | 46524c49952af72875b1e53f5bc8c3af16a4e682 (patch) | |
| tree | c549194b351ae8011e6eae77b94773bd58bc167a | |
| parent | 41544027d04c99d53ce3503fd4f4e5f5d377cf30 (diff) | |
| download | openbsd-46524c49952af72875b1e53f5bc8c3af16a4e682.tar.gz openbsd-46524c49952af72875b1e53f5bc8c3af16a4e682.tar.bz2 openbsd-46524c49952af72875b1e53f5bc8c3af16a4e682.zip | |
correct handling of EOF on both tty input and network. for example,
this allows stuff like nc -l 10101 < /dev/null to have nc exit
automatically when it gets EOF from the network.
ok ericj@
Diffstat (limited to '')
| -rw-r--r-- | src/usr.bin/nc/netcat.c | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c index c5df372210..9e6acc9f6d 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.51 2002/07/01 20:12:40 vincent Exp $ */ | 1 | /* $OpenBSD: netcat.c,v 1.52 2002/07/04 04:42:25 vincent Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> | 3 | * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> |
| 4 | * | 4 | * |
| @@ -533,13 +533,11 @@ local_listen(char *host, char *port, struct addrinfo hints) | |||
| 533 | void | 533 | void |
| 534 | readwrite(int nfd) | 534 | readwrite(int nfd) |
| 535 | { | 535 | { |
| 536 | struct pollfd *pfd; | 536 | struct pollfd pfd[2]; |
| 537 | char buf[BUFSIZ]; | 537 | char buf[BUFSIZ]; |
| 538 | int wfd = fileno(stdin), n, ret; | 538 | int wfd = fileno(stdin), n, ret; |
| 539 | int lfd = fileno(stdout); | 539 | int lfd = fileno(stdout); |
| 540 | 540 | ||
| 541 | pfd = malloc(2 * sizeof(struct pollfd)); | ||
| 542 | |||
| 543 | /* Setup Network FD */ | 541 | /* Setup Network FD */ |
| 544 | pfd[0].fd = nfd; | 542 | pfd[0].fd = nfd; |
| 545 | pfd[0].events = POLLIN; | 543 | pfd[0].events = POLLIN; |
| @@ -548,23 +546,25 @@ readwrite(int nfd) | |||
| 548 | pfd[1].fd = wfd; | 546 | pfd[1].fd = wfd; |
| 549 | pfd[1].events = POLLIN; | 547 | pfd[1].events = POLLIN; |
| 550 | 548 | ||
| 551 | for (;;) { | 549 | while (pfd[0].fd != -1 || pfd[1].fd != -1) { |
| 552 | if (iflag) | 550 | if (iflag) |
| 553 | sleep(iflag); | 551 | sleep(iflag); |
| 554 | 552 | ||
| 555 | if ((n = poll(pfd, 2, timeout)) < 0) { | 553 | if ((n = poll(pfd, 2, timeout)) < 0) { |
| 556 | close(nfd); | 554 | close(nfd); |
| 557 | close(wfd); | 555 | err(1, "Polling Error"); |
| 558 | free(pfd); | ||
| 559 | errx(1, "Polling Error"); | ||
| 560 | } | 556 | } |
| 561 | 557 | ||
| 562 | if (n == 0) | 558 | if (n == 0) |
| 563 | return; | 559 | return; |
| 564 | 560 | ||
| 565 | if (pfd[0].revents & POLLIN) { | 561 | if (pfd[0].revents & POLLIN) { |
| 566 | if ((n = read(nfd, buf, sizeof(buf))) <= 0) { | 562 | if ((n = read(nfd, buf, sizeof(buf))) < 0) |
| 567 | return; | 563 | return; |
| 564 | else if (n == 0) { | ||
| 565 | shutdown(nfd, SHUT_RD); | ||
| 566 | pfd[0].fd = -1; | ||
| 567 | pfd[0].events = 0; | ||
| 568 | } else { | 568 | } else { |
| 569 | if (tflag) | 569 | if (tflag) |
| 570 | atelnet(nfd, buf, n); | 570 | atelnet(nfd, buf, n); |
| @@ -576,7 +576,11 @@ readwrite(int nfd) | |||
| 576 | if (pfd[1].revents & POLLIN) { | 576 | if (pfd[1].revents & POLLIN) { |
| 577 | if ((n = read(wfd, buf, sizeof(buf))) < 0) | 577 | if ((n = read(wfd, buf, sizeof(buf))) < 0) |
| 578 | return; | 578 | return; |
| 579 | else { | 579 | else if (n == 0) { |
| 580 | shutdown(nfd, SHUT_WR); | ||
| 581 | pfd[1].fd = -1; | ||
| 582 | pfd[1].events = 0; | ||
| 583 | } else { | ||
| 580 | if((ret = atomicio(write, nfd, buf, n)) != n) | 584 | if((ret = atomicio(write, nfd, buf, n)) != n) |
| 581 | return; | 585 | return; |
| 582 | } | 586 | } |
| @@ -688,7 +692,7 @@ udptest(int s) | |||
| 688 | { | 692 | { |
| 689 | int i, rv, ret; | 693 | int i, rv, ret; |
| 690 | 694 | ||
| 691 | for (i=0; i <= 3; i++) { | 695 | for (i = 0; i <= 3; i++) { |
| 692 | if ((rv = write(s, "X", 1)) == 1) | 696 | if ((rv = write(s, "X", 1)) == 1) |
| 693 | ret = 1; | 697 | ret = 1; |
| 694 | else | 698 | else |
