From 4e725e1520b39ea79809c2947b0a3560c459e14a Mon Sep 17 00:00:00 2001 From: yrutschle Date: Sun, 10 Mar 2019 10:11:06 +0100 Subject: [PATCH] added TFO for listening socket --- common.c | 21 +++++++++++++++++++++ common.h | 4 ++++ 2 files changed, 25 insertions(+) diff --git a/common.c b/common.c index 79ad8b4..10dca14 100644 --- a/common.c +++ b/common.c @@ -86,6 +86,24 @@ int get_fd_sockets(int *sockfd[]) return sd; } +/* Set TCP_FASTOPEN on listening socket if all client protocols support it */ +int make_listen_tfo(int s) +{ + int i, qlen = 5; + + /* Don't do it if not supported */ + if (!TCP_FASTOPEN) + return; + + /* Don't do it if any protocol does not specify it */ + for (i = 0; i < cfg.protocols_len; i++) { + if (! cfg.protocols[i].tfo_ok) + return; + } + + return setsockopt(s, SOL_SOCKET, TCP_FASTOPEN, (char*)&qlen, sizeof(qlen)); +} + /* Starts listening sockets on specified addresses. * IN: addr[], num_addr * OUT: *sockfd[] pointer to newly-allocated array of file descriptors @@ -134,6 +152,9 @@ int start_listen_sockets(int *sockfd[], struct addrinfo *addr_list) res = setsockopt((*sockfd)[i], SOL_SOCKET, SO_REUSEADDR, (char*)&one, sizeof(one)); check_res_dump(CR_DIE, res, addr, "setsockopt(SO_REUSEADDR)"); + res = make_listen_tfo((*sockfd)[i]); + check_res_dump(CR_WARN, res, addr, "setsockopt(TCP_FASTOPEN)"); + if (addr->ai_flags & SO_KEEPALIVE) { res = setsockopt((*sockfd)[i], SOL_SOCKET, SO_KEEPALIVE, (char*)&one, sizeof(one)); check_res_dump(CR_DIE, res, addr, "setsockopt(SO_KEEPALIVE)"); diff --git a/common.h b/common.h index ae00fdf..cf944a9 100644 --- a/common.h +++ b/common.h @@ -67,6 +67,10 @@ #define IP_FREEBIND 0 #endif +#ifndef TCP_FASTOPEN +#define TCP_FASTOPEN 0 +#endif + #ifndef TCP_FASTOPEN_CONNECT #define TCP_FASTOPEN_CONNECT 30 /* Attempt FastOpen with connect. */ #endif