Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
030087cb27 | ||
|
|
eefa7722c5 |
74
src/main.c
74
src/main.c
@@ -40,12 +40,18 @@
|
||||
#define O_CLOEXEC 0
|
||||
#endif
|
||||
|
||||
#define GT_TIMEOUT (5000)
|
||||
#define GT_MTU_MAX (1500)
|
||||
#define GT_PKT_MAX (32*1024)
|
||||
#define GT_TUNR_SIZE (GT_PKT_MAX-16-2)
|
||||
#define GT_TUNW_SIZE (GT_PKT_MAX)
|
||||
|
||||
#define MPTCP_ENABLED (26)
|
||||
|
||||
static struct {
|
||||
long timeout;
|
||||
int mptcp;
|
||||
} gt;
|
||||
|
||||
struct fdbuf {
|
||||
int fd;
|
||||
buffer_t read;
|
||||
@@ -101,6 +107,7 @@ enum sk_opt {
|
||||
sk_acceptfilter,
|
||||
sk_quickack,
|
||||
sk_user_timeout,
|
||||
sk_mptcp,
|
||||
};
|
||||
|
||||
static void sk_set (int fd, enum sk_opt opt, const void *val, socklen_t len)
|
||||
@@ -155,6 +162,11 @@ static void sk_set (int fd, enum sk_opt opt, const void *val, socklen_t len)
|
||||
[sk_user_timeout] = { "TCP_USER_TIMEOUT",
|
||||
#ifdef TCP_USER_TIMEOUT
|
||||
1, IPPROTO_TCP, TCP_USER_TIMEOUT,
|
||||
#endif
|
||||
},
|
||||
[sk_mptcp] = { "MPTCP_ENABLED",
|
||||
#ifdef MPTCP_ENABLED
|
||||
1, IPPROTO_TCP, MPTCP_ENABLED,
|
||||
#endif
|
||||
},
|
||||
};
|
||||
@@ -177,6 +189,9 @@ static int sk_listen (int fd, struct addrinfo *ai)
|
||||
{
|
||||
sk_set_int(fd, sk_reuseaddr, 1);
|
||||
|
||||
if (gt.mptcp)
|
||||
sk_set_int(fd, sk_mptcp, 1);
|
||||
|
||||
if (bind(fd, ai->ai_addr, ai->ai_addrlen)==-1) {
|
||||
perror("bind");
|
||||
return -1;
|
||||
@@ -188,7 +203,7 @@ static int sk_listen (int fd, struct addrinfo *ai)
|
||||
}
|
||||
|
||||
#ifdef __linux__
|
||||
sk_set_int(fd, sk_defer_accept, GT_TIMEOUT/1000);
|
||||
sk_set_int(fd, sk_defer_accept, gt.timeout/1000);
|
||||
#else
|
||||
char data[256] = "dataready";
|
||||
sk_set(fd, sk_acceptfilter, &data, sizeof(data));
|
||||
@@ -199,10 +214,37 @@ static int sk_listen (int fd, struct addrinfo *ai)
|
||||
|
||||
static int sk_connect (int fd, struct addrinfo *ai)
|
||||
{
|
||||
fd_set_nonblock(fd);
|
||||
|
||||
if (gt.mptcp)
|
||||
sk_set_int(fd, sk_mptcp, 1);
|
||||
|
||||
int ret = connect(fd, ai->ai_addr, ai->ai_addrlen);
|
||||
|
||||
if (ret==-1 && errno==EINTR)
|
||||
return 0;
|
||||
if (ret==-1) {
|
||||
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;
|
||||
}
|
||||
@@ -234,6 +276,8 @@ static int sk_accept (int fd)
|
||||
if (ret==-1 && errno!=EINTR)
|
||||
perror("accept");
|
||||
|
||||
fd_set_nonblock(ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -403,7 +447,7 @@ static size_t fd_read_all (int fd, void *data, size_t size)
|
||||
.events = POLLIN,
|
||||
};
|
||||
|
||||
if (!poll(&pollfd, 1, GT_TIMEOUT))
|
||||
if (!poll(&pollfd, 1, gt.timeout))
|
||||
break;
|
||||
|
||||
continue;
|
||||
@@ -431,7 +475,7 @@ static size_t fd_write_all (int fd, const void *data, size_t size)
|
||||
.events = POLLOUT,
|
||||
};
|
||||
|
||||
if (!poll(&pollfd, 1, GT_TIMEOUT))
|
||||
if (!poll(&pollfd, 1, gt.timeout))
|
||||
break;
|
||||
|
||||
continue;
|
||||
@@ -1069,7 +1113,7 @@ int main (int argc, char **argv)
|
||||
long retry_const = 0;
|
||||
long retry_limit = 1000000;
|
||||
|
||||
long user_timeout = 0;
|
||||
gt.timeout = 5000;
|
||||
|
||||
struct option ka_opts[] = {
|
||||
{ "count", &ka_count, option_long },
|
||||
@@ -1100,7 +1144,8 @@ int main (int argc, char **argv)
|
||||
{ "noquickack", NULL, option_option },
|
||||
{ "retry", &retry_opts, option_option },
|
||||
{ "statefile", &statefile, option_str },
|
||||
{ "timeout", &user_timeout, option_long },
|
||||
{ "timeout", >.timeout, option_long },
|
||||
{ "mptcp", NULL, option_option },
|
||||
{ "debug", NULL, option_option },
|
||||
{ "version", NULL, option_option },
|
||||
{ NULL },
|
||||
@@ -1120,6 +1165,8 @@ int main (int argc, char **argv)
|
||||
const int noquickack = option_is_set(opts, "noquickack");
|
||||
const int debug = option_is_set(opts, "debug");
|
||||
|
||||
gt.mptcp = option_is_set(opts, "mptcp");
|
||||
|
||||
if (buffer_size < GT_PKT_MAX) {
|
||||
buffer_size = GT_PKT_MAX;
|
||||
gt_log("buffer size must be greater than or equal to %li\n", buffer_size);
|
||||
@@ -1135,6 +1182,11 @@ int main (int argc, char **argv)
|
||||
retry_count = 0;
|
||||
}
|
||||
|
||||
if (gt.timeout<=0 || gt.timeout>INT_MAX) {
|
||||
gt_log("bad timeout\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (sodium_init()==-1) {
|
||||
gt_log("libsodium initialization has failed\n");
|
||||
return 1;
|
||||
@@ -1226,8 +1278,6 @@ int main (int argc, char **argv)
|
||||
|
||||
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_keepalive, keepalive);
|
||||
|
||||
@@ -1242,9 +1292,7 @@ int main (int argc, char **argv)
|
||||
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, user_timeout);
|
||||
|
||||
sk_set_int(sock.fd, sk_user_timeout, gt.timeout);
|
||||
sk_set(sock.fd, sk_congestion, congestion, str_len(congestion));
|
||||
|
||||
switch (gt_setup_crypto(&ctx, sock.fd, listener)) {
|
||||
|
||||
Reference in New Issue
Block a user