summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfgsch <>2011-10-04 08:34:34 +0000
committerfgsch <>2011-10-04 08:34:34 +0000
commitd8df5f35946db56b630db60f4869ba5d9cb12a99 (patch)
treeafe30c6696bc88421be1c64cdbdcb5930a293c77
parentd0c429847ac53ca168183bfe6fe005f1cc960182 (diff)
downloadopenbsd-d8df5f35946db56b630db60f4869ba5d9cb12a99.tar.gz
openbsd-d8df5f35946db56b630db60f4869ba5d9cb12a99.tar.bz2
openbsd-d8df5f35946db56b630db60f4869ba5d9cb12a99.zip
change -w to apply to the connection as well. manpage bit from jmc@
nicm@ ok.
-rw-r--r--src/usr.bin/nc/nc.18
-rw-r--r--src/usr.bin/nc/netcat.c42
2 files changed, 44 insertions, 6 deletions
diff --git a/src/usr.bin/nc/nc.1 b/src/usr.bin/nc/nc.1
index 6a1538cb45..d249aa18b1 100644
--- a/src/usr.bin/nc/nc.1
+++ b/src/usr.bin/nc/nc.1
@@ -1,4 +1,4 @@
1.\" $OpenBSD: nc.1,v 1.58 2011/09/17 14:10:05 haesbaert Exp $ 1.\" $OpenBSD: nc.1,v 1.59 2011/10/04 08:34:34 fgsch Exp $
2.\" 2.\"
3.\" Copyright (c) 1996 David Sacerdote 3.\" Copyright (c) 1996 David Sacerdote
4.\" All rights reserved. 4.\" All rights reserved.
@@ -25,7 +25,7 @@
25.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27.\" 27.\"
28.Dd $Mdocdate: September 17 2011 $ 28.Dd $Mdocdate: October 4 2011 $
29.Dt NC 1 29.Dt NC 1
30.Os 30.Os
31.Sh NAME 31.Sh NAME
@@ -210,9 +210,9 @@ Have
210.Nm 210.Nm
211give more verbose output. 211give more verbose output.
212.It Fl w Ar timeout 212.It Fl w Ar timeout
213If a connection and stdin are idle for more than 213Connections which cannot be established or are idle timeout after
214.Ar timeout 214.Ar timeout
215seconds, then the connection is silently closed. 215seconds.
216The 216The
217.Fl w 217.Fl w
218flag has no effect on the 218flag has no effect on the
diff --git a/src/usr.bin/nc/netcat.c b/src/usr.bin/nc/netcat.c
index 952bfb7dda..7880c7f452 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.102 2011/09/17 14:10:05 haesbaert Exp $ */ 1/* $OpenBSD: netcat.c,v 1.103 2011/10/04 08:34:34 fgsch Exp $ */
2/* 2/*
3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org> 3 * Copyright (c) 2001 Eric Jackson <ericj@monkey.org>
4 * 4 *
@@ -98,6 +98,7 @@ void help(void);
98int local_listen(char *, char *, struct addrinfo); 98int local_listen(char *, char *, struct addrinfo);
99void readwrite(int); 99void readwrite(int);
100int remote_connect(const char *, const char *, struct addrinfo); 100int remote_connect(const char *, const char *, struct addrinfo);
101int timeout_connect(int, const struct sockaddr *, socklen_t);
101int socks_connect(const char *, const char *, struct addrinfo, 102int socks_connect(const char *, const char *, struct addrinfo,
102 const char *, const char *, struct addrinfo, int, const char *); 103 const char *, const char *, struct addrinfo, int, const char *);
103int udptest(int); 104int udptest(int);
@@ -590,7 +591,7 @@ remote_connect(const char *host, const char *port, struct addrinfo hints)
590 591
591 set_common_sockopts(s); 592 set_common_sockopts(s);
592 593
593 if (connect(s, res0->ai_addr, res0->ai_addrlen) == 0) 594 if (timeout_connect(s, res0->ai_addr, res0->ai_addrlen) == 0)
594 break; 595 break;
595 else if (vflag) 596 else if (vflag)
596 warn("connect to %s port %s (%s) failed", host, port, 597 warn("connect to %s port %s (%s) failed", host, port,
@@ -605,6 +606,43 @@ remote_connect(const char *host, const char *port, struct addrinfo hints)
605 return (s); 606 return (s);
606} 607}
607 608
609int
610timeout_connect(int s, const struct sockaddr *name, socklen_t namelen)
611{
612 struct pollfd pfd;
613 socklen_t optlen;
614 int flags, optval;
615 int ret;
616
617 if (timeout != -1) {
618 flags = fcntl(s, F_GETFL, 0);
619 if (fcntl(s, F_SETFL, flags | O_NONBLOCK) == -1)
620 err(1, "set non-blocking mode");
621 }
622
623 if ((ret = connect(s, name, namelen)) != 0 && errno == EINPROGRESS) {
624 pfd.fd = s;
625 pfd.events = POLLOUT;
626 if ((ret = poll(&pfd, 1, timeout)) == 1) {
627 optlen = sizeof(optval);
628 if ((ret = getsockopt(s, SOL_SOCKET, SO_ERROR,
629 &optval, &optlen)) == 0) {
630 errno = optval;
631 ret = optval == 0 ? 0 : -1;
632 }
633 } else if (ret == 0) {
634 errno = ETIMEDOUT;
635 ret = -1;
636 } else
637 err(1, "poll failed");
638 }
639
640 if (timeout != -1 && fcntl(s, F_SETFL, flags) == -1)
641 err(1, "restoring flags");
642
643 return (ret);
644}
645
608/* 646/*
609 * local_listen() 647 * local_listen()
610 * Returns a socket listening on a local port, binds to specified source 648 * Returns a socket listening on a local port, binds to specified source