2017-07-17 15:26:12 +02:00
|
|
|
From: Aron Xu <aron@debian.org>
|
|
|
|
Date: Mon, 13 Feb 2012 14:43:56 +0800
|
|
|
|
Subject: connect timeout
|
|
|
|
|
|
|
|
---
|
|
|
|
netcat.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
|
|
|
|
1 file changed, 76 insertions(+), 2 deletions(-)
|
|
|
|
|
|
|
|
--- a/netcat.c
|
|
|
|
+++ b/netcat.c
|
2019-09-09 20:51:34 +02:00
|
|
|
@@ -90,6 +90,7 @@
|
|
|
|
#include <ctype.h>
|
2017-07-17 15:26:12 +02:00
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
+#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <poll.h>
|
|
|
|
@@ -124,6 +125,10 @@
|
2018-11-14 14:18:05 +01:00
|
|
|
# define TLS_MUSTSTAPLE (1 << 4)
|
2017-07-17 15:26:12 +02:00
|
|
|
#endif
|
2013-09-02 21:53:10 +02:00
|
|
|
|
|
|
|
+#define CONNECTION_SUCCESS 0
|
|
|
|
+#define CONNECTION_FAILED 1
|
|
|
|
+#define CONNECTION_TIMEOUT 2
|
|
|
|
+
|
|
|
|
/* Command Line Options */
|
|
|
|
int dflag; /* detached, no stdin */
|
2017-07-17 15:26:12 +02:00
|
|
|
int Fflag; /* fdpass sock to stdout */
|
2018-11-14 14:18:05 +01:00
|
|
|
@@ -214,6 +219,9 @@ ssize_t drainbuf(int, unsigned char *, s
|
2017-07-17 15:26:12 +02:00
|
|
|
ssize_t fillbuf(int, unsigned char *, size_t *);
|
|
|
|
# endif
|
2013-09-02 21:53:10 +02:00
|
|
|
|
2017-07-17 15:26:12 +02:00
|
|
|
+static int connect_with_timeout(int fd, const struct sockaddr *sa,
|
|
|
|
+ socklen_t salen, int ctimeout);
|
2013-09-02 21:53:10 +02:00
|
|
|
+
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2019-09-09 20:51:34 +02:00
|
|
|
@@ -1078,11 +1086,14 @@ remote_connect(const char *host, const c
|
2017-07-17 15:26:12 +02:00
|
|
|
|
|
|
|
set_common_sockopts(s, res->ai_family);
|
2013-09-02 21:53:10 +02:00
|
|
|
|
2017-07-17 15:26:12 +02:00
|
|
|
- if (timeout_connect(s, res->ai_addr, res->ai_addrlen) == 0)
|
|
|
|
+ if ((error = connect_with_timeout(s, res->ai_addr, res->ai_addrlen, timeout)) == CONNECTION_SUCCESS)
|
2013-09-02 21:53:10 +02:00
|
|
|
break;
|
2017-07-17 15:26:12 +02:00
|
|
|
- if (vflag)
|
|
|
|
+ if (vflag && error == CONNECTION_FAILED)
|
2013-09-02 21:53:10 +02:00
|
|
|
warn("connect to %s port %s (%s) failed", host, port,
|
|
|
|
uflag ? "udp" : "tcp");
|
2017-07-17 15:26:12 +02:00
|
|
|
+ else if (vflag && error == CONNECTION_TIMEOUT)
|
|
|
|
+ warn("connect to %s port %s (%s) timed out", host, port,
|
|
|
|
+ uflag ? "udp" : "tcp");
|
|
|
|
|
|
|
|
save_errno = errno;
|
2013-09-02 21:53:10 +02:00
|
|
|
close(s);
|
2019-09-09 20:51:34 +02:00
|
|
|
@@ -1123,6 +1134,69 @@ timeout_connect(int s, const struct sock
|
2018-11-14 14:18:05 +01:00
|
|
|
return ret;
|
2013-09-02 21:53:10 +02:00
|
|
|
}
|
|
|
|
|
2017-07-17 15:26:12 +02:00
|
|
|
+static int connect_with_timeout(int fd, const struct sockaddr *sa,
|
2013-09-02 21:53:10 +02:00
|
|
|
+ socklen_t salen, int ctimeout)
|
|
|
|
+{
|
|
|
|
+ int err;
|
|
|
|
+ struct timeval tv, *tvp = NULL;
|
|
|
|
+ fd_set connect_fdset;
|
|
|
|
+ socklen_t len;
|
2017-07-17 15:26:12 +02:00
|
|
|
+ int orig_flags;
|
2013-09-02 21:53:10 +02:00
|
|
|
+
|
|
|
|
+ orig_flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
+ if (fcntl(fd, F_SETFL, orig_flags | O_NONBLOCK) < 0 ) {
|
2017-07-17 15:26:12 +02:00
|
|
|
+ warn("can't set O_NONBLOCK - timeout not available");
|
2013-09-02 21:53:10 +02:00
|
|
|
+ if (connect(fd, sa, salen) == 0)
|
|
|
|
+ return CONNECTION_SUCCESS;
|
|
|
|
+ else
|
|
|
|
+ return CONNECTION_FAILED;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /* set connect timeout */
|
|
|
|
+ if (ctimeout > 0) {
|
|
|
|
+ tv.tv_sec = (time_t)ctimeout/1000;
|
|
|
|
+ tv.tv_usec = 0;
|
|
|
|
+ tvp = &tv;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /* attempt the connection */
|
|
|
|
+ err = connect(fd, sa, salen);
|
|
|
|
+ if (err != 0 && errno == EINPROGRESS) {
|
|
|
|
+ /* connection is proceeding
|
|
|
|
+ * it is complete (or failed) when select returns */
|
|
|
|
+
|
|
|
|
+ /* initialize connect_fdset */
|
|
|
|
+ FD_ZERO(&connect_fdset);
|
|
|
|
+ FD_SET(fd, &connect_fdset);
|
|
|
|
+
|
|
|
|
+ /* call select */
|
|
|
|
+ do {
|
2017-07-17 15:26:12 +02:00
|
|
|
+ err = select(fd + 1, NULL, &connect_fdset,
|
2013-09-02 21:53:10 +02:00
|
|
|
+ NULL, tvp);
|
|
|
|
+ } while (err < 0 && errno == EINTR);
|
|
|
|
+
|
|
|
|
+ /* select error */
|
|
|
|
+ if (err < 0)
|
|
|
|
+ errx(1,"select error: %s", strerror(errno));
|
|
|
|
+ /* we have reached a timeout */
|
2017-07-17 15:26:12 +02:00
|
|
|
+ if (err == 0)
|
2013-09-02 21:53:10 +02:00
|
|
|
+ return CONNECTION_TIMEOUT;
|
2017-07-17 15:26:12 +02:00
|
|
|
+ /* select returned successfully, but we must test socket
|
2013-09-02 21:53:10 +02:00
|
|
|
+ * error for result */
|
|
|
|
+ len = sizeof(err);
|
|
|
|
+ if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0)
|
|
|
|
+ errx(1, "getsockopt error: %s", strerror(errno));
|
2017-07-17 15:26:12 +02:00
|
|
|
+ /* setup errno according to the result returned by
|
2013-09-02 21:53:10 +02:00
|
|
|
+ * getsockopt */
|
|
|
|
+ if (err != 0)
|
|
|
|
+ errno = err;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /* return aborted if an error occured, and valid otherwise */
|
|
|
|
+ fcntl(fd, F_SETFL, orig_flags);
|
|
|
|
+ return (err != 0)? CONNECTION_FAILED : CONNECTION_SUCCESS;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
/*
|
|
|
|
* local_listen()
|
|
|
|
* Returns a socket listening on a local port, binds to specified source
|