From 2cad381036c7fe684b03b734e949cb2bd9d5f222 Mon Sep 17 00:00:00 2001 From: Max Lv Date: Sun, 26 Nov 2017 14:32:26 +0800 Subject: [PATCH] Refine TFO in redir and server mode --- configure.ac | 2 +- src/netutils.h | 9 ++++++--- src/redir.c | 2 +- src/server.c | 24 +++++++++++++++--------- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/configure.ac b/configure.ac index 1f61dae3..cea85f66 100755 --- a/configure.ac +++ b/configure.ac @@ -139,7 +139,7 @@ AM_CONDITIONAL(BUILD_REDIRECTOR, test "$os_support" = "linux") AM_CONDITIONAL(BUILD_WINCOMPAT, test "$os_support" = "mingw") dnl Checks for header files. -AC_CHECK_HEADERS([limits.h stdint.h inttypes.h arpa/inet.h fcntl.h langinfo.h locale.h netdb.h netinet/in.h stdlib.h string.h strings.h unistd.h sys/ioctl.h linux/random.h]) +AC_CHECK_HEADERS([limits.h stdint.h inttypes.h arpa/inet.h fcntl.h langinfo.h locale.h linux/tcp.h netinet/tcp.h netdb.h netinet/in.h stdlib.h string.h strings.h unistd.h sys/ioctl.h linux/random.h]) dnl A special check required for on Darwin. See dnl http://www.gnu.org/software/autoconf/manual/html_node/Header-Portability.html. diff --git a/src/netutils.h b/src/netutils.h index 4cfe748d..3845d251 100644 --- a/src/netutils.h +++ b/src/netutils.h @@ -25,13 +25,16 @@ #include -#if defined(__linux__) -#include +#if HAVE_LINUX_TCP_H #include -#else +#elif defined(HAVE_NETINET_TCP_H) #include #endif +#ifdef HAVE_NETDB_H +#include +#endif + /* MPTCP_ENABLED setsockopt values for kernel 4 & 3, best behaviour to be independant of kernel version is to test from newest to the latest values */ #ifndef MPTCP_ENABLED static const char mptcp_enabled_values[] = { 42, 26, 0 }; diff --git a/src/redir.c b/src/redir.c index 70915f06..99db0d80 100644 --- a/src/redir.c +++ b/src/redir.c @@ -558,7 +558,7 @@ remote_send_cb(EV_P_ ev_io *w, int revents) if(setsockopt(remote->fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, (void *)&optval, sizeof(optval)) < 0) FATAL("failed to set TCP_FASTOPEN_CONNECT"); - s = connect(remote->fd, (struct sockaddr *)&(remote->addr), remote->addr_len); + s = connect(remote->fd, remote->addr, get_sockaddr_len(remote->addr)); if (s == 0) s = send(remote->fd, remote->buf->data, remote->buf->len, 0); #elif defined(MSG_FASTOPEN) diff --git a/src/server.c b/src/server.c index bec12041..cd9ae9ad 100644 --- a/src/server.c +++ b/src/server.c @@ -506,9 +506,19 @@ connect_to_remote(EV_P_ struct addrinfo *res, remote_t *remote = new_remote(sockfd); -#ifdef TCP_FASTOPEN if (fast_open) { -#ifdef __APPLE__ +#if defined(TCP_FASTOPEN_CONNECT) + int optval = 1; + if(setsockopt(sockfd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, + (void *)&optval, sizeof(optval)) < 0) + FATAL("failed to set TCP_FASTOPEN_CONNECT"); + int s = connect(sockfd, res->ai_addr, res->ai_addrlen); + if (s == 0) + s = send(sockfd, server->buf->data, server->buf->len, 0); +#elif defined(MSG_FASTOPEN) + ssize_t s = sendto(sockfd, server->buf->data, server->buf->len, + MSG_FASTOPEN, res->ai_addr, res->ai_addrlen); +#elif defined(CONNECT_DATA_IDEMPOTENT) ((struct sockaddr_in *)(res->ai_addr))->sin_len = sizeof(struct sockaddr_in); sa_endpoints_t endpoints; memset((char *)&endpoints, 0, sizeof(endpoints)); @@ -521,16 +531,13 @@ connect_to_remote(EV_P_ struct addrinfo *res, size_t len; int s = connectx(sockfd, &endpoints, SAE_ASSOCID_ANY, CONNECT_DATA_IDEMPOTENT, &iov, 1, &len, NULL); - if (s == 0) { + if (s == 0) s = len; - } #else - ssize_t s = sendto(sockfd, server->buf->data, server->buf->len, - MSG_FASTOPEN, res->ai_addr, res->ai_addrlen); + FATAL("fast open is not enabled in this build"); #endif if (s == -1) { - if (errno == CONNECT_IN_PROGRESS || errno == EAGAIN - || errno == EWOULDBLOCK) { + if (errno == CONNECT_IN_PROGRESS) { // The remote server doesn't support tfo or it's the first connection to the server. // It will automatically fall back to conventional TCP. } else if (errno == EOPNOTSUPP || errno == EPROTONOSUPPORT || @@ -546,7 +553,6 @@ connect_to_remote(EV_P_ struct addrinfo *res, server->buf->len -= s; } } -#endif if (!fast_open) { int r = connect(sockfd, res->ai_addr, res->ai_addrlen);