Browse Source

Refine TFO in redir and server mode

pull/1813/merge
Max Lv 7 years ago
parent
commit
2cad381036
4 changed files with 23 additions and 14 deletions
  1. 2
      configure.ac
  2. 9
      src/netutils.h
  3. 2
      src/redir.c
  4. 24
      src/server.c

2
configure.ac

@ -139,7 +139,7 @@ AM_CONDITIONAL(BUILD_REDIRECTOR, test "$os_support" = "linux")
AM_CONDITIONAL(BUILD_WINCOMPAT, test "$os_support" = "mingw") AM_CONDITIONAL(BUILD_WINCOMPAT, test "$os_support" = "mingw")
dnl Checks for header files. 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 <net/if.h> on Darwin. See dnl A special check required for <net/if.h> on Darwin. See
dnl http://www.gnu.org/software/autoconf/manual/html_node/Header-Portability.html. dnl http://www.gnu.org/software/autoconf/manual/html_node/Header-Portability.html.

9
src/netutils.h

@ -25,13 +25,16 @@
#include <sys/socket.h> #include <sys/socket.h>
#if defined(__linux__)
#include <netdb.h>
#if HAVE_LINUX_TCP_H
#include <linux/tcp.h> #include <linux/tcp.h>
#else
#elif defined(HAVE_NETINET_TCP_H)
#include <netinet/tcp.h> #include <netinet/tcp.h>
#endif #endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#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 */ /* 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 #ifndef MPTCP_ENABLED
static const char mptcp_enabled_values[] = { 42, 26, 0 }; static const char mptcp_enabled_values[] = { 42, 26, 0 };

2
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, if(setsockopt(remote->fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT,
(void *)&optval, sizeof(optval)) < 0) (void *)&optval, sizeof(optval)) < 0)
FATAL("failed to set TCP_FASTOPEN_CONNECT"); 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) if (s == 0)
s = send(remote->fd, remote->buf->data, remote->buf->len, 0); s = send(remote->fd, remote->buf->data, remote->buf->len, 0);
#elif defined(MSG_FASTOPEN) #elif defined(MSG_FASTOPEN)

24
src/server.c

@ -506,9 +506,19 @@ connect_to_remote(EV_P_ struct addrinfo *res,
remote_t *remote = new_remote(sockfd); remote_t *remote = new_remote(sockfd);
#ifdef TCP_FASTOPEN
if (fast_open) { 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); ((struct sockaddr_in *)(res->ai_addr))->sin_len = sizeof(struct sockaddr_in);
sa_endpoints_t endpoints; sa_endpoints_t endpoints;
memset((char *)&endpoints, 0, sizeof(endpoints)); memset((char *)&endpoints, 0, sizeof(endpoints));
@ -521,16 +531,13 @@ connect_to_remote(EV_P_ struct addrinfo *res,
size_t len; size_t len;
int s = connectx(sockfd, &endpoints, SAE_ASSOCID_ANY, CONNECT_DATA_IDEMPOTENT, int s = connectx(sockfd, &endpoints, SAE_ASSOCID_ANY, CONNECT_DATA_IDEMPOTENT,
&iov, 1, &len, NULL); &iov, 1, &len, NULL);
if (s == 0) {
if (s == 0)
s = len; s = len;
}
#else #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 #endif
if (s == -1) { 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. // The remote server doesn't support tfo or it's the first connection to the server.
// It will automatically fall back to conventional TCP. // It will automatically fall back to conventional TCP.
} else if (errno == EOPNOTSUPP || errno == EPROTONOSUPPORT || } else if (errno == EOPNOTSUPP || errno == EPROTONOSUPPORT ||
@ -546,7 +553,6 @@ connect_to_remote(EV_P_ struct addrinfo *res,
server->buf->len -= s; server->buf->len -= s;
} }
} }
#endif
if (!fast_open) { if (!fast_open) {
int r = connect(sockfd, res->ai_addr, res->ai_addrlen); int r = connect(sockfd, res->ai_addr, res->ai_addrlen);

Loading…
Cancel
Save