Use timeout in connect() too

This commit is contained in:
angt
2016-01-21 16:22:31 +01:00
parent 17547f555d
commit eefa7722c5

View File

@@ -40,12 +40,15 @@
#define O_CLOEXEC 0 #define O_CLOEXEC 0
#endif #endif
#define GT_TIMEOUT (5000)
#define GT_MTU_MAX (1500) #define GT_MTU_MAX (1500)
#define GT_PKT_MAX (32*1024) #define GT_PKT_MAX (32*1024)
#define GT_TUNR_SIZE (GT_PKT_MAX-16-2) #define GT_TUNR_SIZE (GT_PKT_MAX-16-2)
#define GT_TUNW_SIZE (GT_PKT_MAX) #define GT_TUNW_SIZE (GT_PKT_MAX)
static struct {
int timeout;
} gt;
struct fdbuf { struct fdbuf {
int fd; int fd;
buffer_t read; buffer_t read;
@@ -188,7 +191,7 @@ static int sk_listen (int fd, struct addrinfo *ai)
} }
#ifdef __linux__ #ifdef __linux__
sk_set_int(fd, sk_defer_accept, GT_TIMEOUT/1000); sk_set_int(fd, sk_defer_accept, gt.timeout/1000);
#else #else
char data[256] = "dataready"; char data[256] = "dataready";
sk_set(fd, sk_acceptfilter, &data, sizeof(data)); sk_set(fd, sk_acceptfilter, &data, sizeof(data));
@@ -199,10 +202,34 @@ static int sk_listen (int fd, struct addrinfo *ai)
static int sk_connect (int fd, struct addrinfo *ai) static int sk_connect (int fd, struct addrinfo *ai)
{ {
fd_set_nonblock(fd);
int ret = connect(fd, ai->ai_addr, ai->ai_addrlen); int ret = connect(fd, ai->ai_addr, ai->ai_addrlen);
if (ret==-1 && errno==EINTR) if (ret==-1) {
return 0; if (errno==EINTR)
return 0;
if (errno==EINPROGRESS) {
struct pollfd pollfd = {
.fd = fd,
.events = POLLOUT,
};
if (!poll(&pollfd, 1, gt.timeout))
return -1;
int opt = 0;
socklen_t optlen = sizeof(opt);
getsockopt(fd, SOL_SOCKET, SO_ERROR, &opt, &optlen);
if (!opt)
return 0;
errno = opt;
}
}
return ret; return ret;
} }
@@ -234,6 +261,8 @@ static int sk_accept (int fd)
if (ret==-1 && errno!=EINTR) if (ret==-1 && errno!=EINTR)
perror("accept"); perror("accept");
fd_set_nonblock(ret);
return ret; return ret;
} }
@@ -403,7 +432,7 @@ static size_t fd_read_all (int fd, void *data, size_t size)
.events = POLLIN, .events = POLLIN,
}; };
if (!poll(&pollfd, 1, GT_TIMEOUT)) if (!poll(&pollfd, 1, gt.timeout))
break; break;
continue; continue;
@@ -431,7 +460,7 @@ static size_t fd_write_all (int fd, const void *data, size_t size)
.events = POLLOUT, .events = POLLOUT,
}; };
if (!poll(&pollfd, 1, GT_TIMEOUT)) if (!poll(&pollfd, 1, gt.timeout))
break; break;
continue; continue;
@@ -1069,7 +1098,7 @@ int main (int argc, char **argv)
long retry_const = 0; long retry_const = 0;
long retry_limit = 1000000; long retry_limit = 1000000;
long user_timeout = 0; gt.timeout = 5000;
struct option ka_opts[] = { struct option ka_opts[] = {
{ "count", &ka_count, option_long }, { "count", &ka_count, option_long },
@@ -1100,7 +1129,7 @@ int main (int argc, char **argv)
{ "noquickack", NULL, option_option }, { "noquickack", NULL, option_option },
{ "retry", &retry_opts, option_option }, { "retry", &retry_opts, option_option },
{ "statefile", &statefile, option_str }, { "statefile", &statefile, option_str },
{ "timeout", &user_timeout, option_long }, { "timeout", &gt.timeout, option_long },
{ "debug", NULL, option_option }, { "debug", NULL, option_option },
{ "version", NULL, option_option }, { "version", NULL, option_option },
{ NULL }, { NULL },
@@ -1135,6 +1164,11 @@ int main (int argc, char **argv)
retry_count = 0; retry_count = 0;
} }
if (gt.timeout<=0 || gt.timeout>INT_MAX) {
gt_log("bad timeout\n");
return 1;
}
if (sodium_init()==-1) { if (sodium_init()==-1) {
gt_log("libsodium initialization has failed\n"); gt_log("libsodium initialization has failed\n");
return 1; return 1;
@@ -1226,8 +1260,6 @@ int main (int argc, char **argv)
gt_log("%s: connected\n", sockname); gt_log("%s: connected\n", sockname);
fd_set_nonblock(sock.fd);
sk_set_int(sock.fd, sk_nodelay, !delay); sk_set_int(sock.fd, sk_nodelay, !delay);
sk_set_int(sock.fd, sk_keepalive, keepalive); sk_set_int(sock.fd, sk_keepalive, keepalive);
@@ -1242,9 +1274,7 @@ int main (int argc, char **argv)
sk_set_int(sock.fd, sk_keepintvl, ka_interval); sk_set_int(sock.fd, sk_keepintvl, ka_interval);
} }
if (user_timeout>0 && user_timeout<=INT_MAX) sk_set_int(sock.fd, sk_user_timeout, gt.timeout);
sk_set_int(sock.fd, sk_user_timeout, user_timeout);
sk_set(sock.fd, sk_congestion, congestion, str_len(congestion)); sk_set(sock.fd, sk_congestion, congestion, str_len(congestion));
switch (gt_setup_crypto(&ctx, sock.fd, listener)) { switch (gt_setup_crypto(&ctx, sock.fd, listener)) {