summaryrefslogtreecommitdiff
path: root/src/usr.bin/nc/netcat.c
diff options
context:
space:
mode:
authorbluhm <>2017-05-10 21:56:53 +0000
committerbluhm <>2017-05-10 21:56:53 +0000
commit5f64f36094433c7ef3912c187243ab5b1c54b413 (patch)
tree56092c4c202a156a1fb718e82e9527a63355fab0 /src/usr.bin/nc/netcat.c
parent3fc4fca12d585e23e74c7b750445d31c96380ba0 (diff)
downloadopenbsd-5f64f36094433c7ef3912c187243ab5b1c54b413.tar.gz
openbsd-5f64f36094433c7ef3912c187243ab5b1c54b413.tar.bz2
openbsd-5f64f36094433c7ef3912c187243ab5b1c54b413.zip
Implement nc -W recvlimit to terminate netcat after receiving a
number of packets. This allows to send a UDP request, receive a reply and check the result on the command line. input jmc@; OK millert@
Diffstat (limited to 'src/usr.bin/nc/netcat.c')
-rw-r--r--src/usr.bin/nc/netcat.c27
1 files changed, 21 insertions, 6 deletions
diff --git a/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c
index bd9fb09a0f..54ebacf9ea 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.181 2017/04/16 15:11:01 deraadt Exp $ */ 1/* $OpenBSD: netcat.c,v 1.182 2017/05/10 21:56:53 bluhm Exp $ */
2/* 2/*
3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> 3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org>
4 * Copyright (c) 2015 Bob Beck. All rights reserved. 4 * Copyright (c) 2015 Bob Beck. All rights reserved.
@@ -108,6 +108,7 @@ char *tls_expectname; /* required name in peer cert */
108char *tls_expecthash; /* required hash of peer cert */ 108char *tls_expecthash; /* required hash of peer cert */
109FILE *Zflag; /* file to save peer cert */ 109FILE *Zflag; /* file to save peer cert */
110 110
111int recvcount, recvlimit;
111int timeout = -1; 112int timeout = -1;
112int family = AF_UNSPEC; 113int family = AF_UNSPEC;
113char *portlist[PORT_MAX+1]; 114char *portlist[PORT_MAX+1];
@@ -167,7 +168,8 @@ main(int argc, char *argv[])
167 signal(SIGPIPE, SIG_IGN); 168 signal(SIGPIPE, SIG_IGN);
168 169
169 while ((ch = getopt(argc, argv, 170 while ((ch = getopt(argc, argv,
170 "46C:cDde:FH:hI:i:K:klM:m:NnO:o:P:p:R:rSs:T:tUuV:vw:X:x:Z:z")) != -1) { 171 "46C:cDde:FH:hI:i:K:klM:m:NnO:o:P:p:R:rSs:T:tUuV:vW:w:X:x:Z:z"))
172 != -1) {
171 switch (ch) { 173 switch (ch) {
172 case '4': 174 case '4':
173 family = AF_INET; 175 family = AF_INET;
@@ -270,6 +272,11 @@ main(int argc, char *argv[])
270 case 'v': 272 case 'v':
271 vflag = 1; 273 vflag = 1;
272 break; 274 break;
275 case 'W':
276 recvlimit = strtonum(optarg, 1, INT_MAX, &errstr);
277 if (errstr)
278 errx(1, "receive limit %s: %s", errstr, optarg);
279 break;
273 case 'w': 280 case 'w':
274 timeout = strtonum(optarg, 0, INT_MAX / 1000, &errstr); 281 timeout = strtonum(optarg, 0, INT_MAX / 1000, &errstr);
275 if (errstr) 282 if (errstr)
@@ -1166,6 +1173,12 @@ readwrite(int net_fd, struct tls *tls_ctx)
1166 shutdown(pfd[POLL_NETIN].fd, SHUT_RD); 1173 shutdown(pfd[POLL_NETIN].fd, SHUT_RD);
1167 pfd[POLL_NETIN].fd = -1; 1174 pfd[POLL_NETIN].fd = -1;
1168 } 1175 }
1176 if (recvlimit > 0 && ++recvcount >= recvlimit) {
1177 if (pfd[POLL_NETIN].fd != -1)
1178 shutdown(pfd[POLL_NETIN].fd, SHUT_RD);
1179 pfd[POLL_NETIN].fd = -1;
1180 pfd[POLL_STDIN].fd = -1;
1181 }
1169 /* read something - poll stdout */ 1182 /* read something - poll stdout */
1170 if (netinbufpos > 0) 1183 if (netinbufpos > 0)
1171 pfd[POLL_STDOUT].events = POLLOUT; 1184 pfd[POLL_STDOUT].events = POLLOUT;
@@ -1706,6 +1719,7 @@ help(void)
1706 \t-u UDP mode\n\ 1719 \t-u UDP mode\n\
1707 \t-V rtable Specify alternate routing table\n\ 1720 \t-V rtable Specify alternate routing table\n\
1708 \t-v Verbose\n\ 1721 \t-v Verbose\n\
1722 \t-W recvlimit Terminate after receiving a number of packets\n\
1709 \t-w timeout Timeout for connects and final net reads\n\ 1723 \t-w timeout Timeout for connects and final net reads\n\
1710 \t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\ 1724 \t-X proto Proxy protocol: \"4\", \"5\" (SOCKS) or \"connect\"\n\
1711 \t-x addr[:port]\tSpecify proxy address and port\n\ 1725 \t-x addr[:port]\tSpecify proxy address and port\n\
@@ -1724,10 +1738,11 @@ usage(int ret)
1724 "\t [-i interval] [-K keyfile] [-M ttl] [-m minttl] [-O length]\n" 1738 "\t [-i interval] [-K keyfile] [-M ttl] [-m minttl] [-O length]\n"
1725 "\t [-o staplefile] [-P proxy_username] [-p source_port] " 1739 "\t [-o staplefile] [-P proxy_username] [-p source_port] "
1726 "[-R CAfile]\n" 1740 "[-R CAfile]\n"
1727 "\t [-s source] [-T keyword] [-V rtable] [-w timeout] " 1741 "\t [-s source] [-T keyword] [-V rtable] [-W recvlimit] "
1728 "[-X proxy_protocol]\n" 1742 "[-w timeout]\n"
1729 "\t [-x proxy_address[:port]] [-Z peercertfile] " 1743 "\t [-X proxy_protocol] [-x proxy_address[:port]] "
1730 "[destination] [port]\n"); 1744 "[-Z peercertfile]\n"
1745 "\t [destination] [port]\n");
1731 if (ret) 1746 if (ret)
1732 exit(1); 1747 exit(1);
1733} 1748}