From 0f342f7235713416ccae7c52d8de469f5bf76d03 Mon Sep 17 00:00:00 2001 From: Fabian Mauchle Date: Fri, 12 May 2017 17:55:48 +0200 Subject: [PATCH] move setsockopt to util function --- tcp.c | 26 +------------------------- tls.c | 2 ++ util.c | 23 +++++++++++++++++++++++ util.h | 1 + 4 files changed, 27 insertions(+), 25 deletions(-) diff --git a/tcp.c b/tcp.c index 9d79bd3..a194055 100644 --- a/tcp.c +++ b/tcp.c @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -84,9 +83,6 @@ int tcpconnect(struct server *server, struct timeval *when, int timeout, char *t struct timeval now; time_t elapsed; - int optval; - socklen_t optlen = sizeof(optval); - debug(DBG_DBG, "tcpconnect: called from %s", text); pthread_mutex_lock(&server->lock); if (when && memcmp(&server->lastconnecttry, when, sizeof(struct timeval))) { @@ -123,27 +119,7 @@ int tcpconnect(struct server *server, struct timeval *when, int timeout, char *t if (server->sock >= 0) close(server->sock); if ((server->sock = connecttcphostlist(server->conf->hostports, srcres)) >= 0) { - optval = 1; - if(setsockopt(server->sock, SOL_SOCKET, TCP_KEEPCNT, &optval, optlen) < 0) { - debug(DBG_ERR, "tcpconnect: setsockopt TCP_KEEPCNT failed"); - } - optval = 2000; - if(setsockopt(server->sock, SOL_SOCKET, TCP_KEEPIDLE, &optval, optlen) < 0) { - debug(DBG_ERR, "tcpconnect: setsockopt TCP_KEEPIDLE %d failed", optval); - } - if(getsockopt(server->sock, SOL_SOCKET, TCP_KEEPIDLE, &optval, &optlen) < 0) { - debug(DBG_ERR, "tcpconnect: getsockopt failed"); - }else{ - debug(DBG_ERR, "tcpconnect: TCP_KEEPIDLE is %d", optval); - } - optval = 1; - if(setsockopt(server->sock, SOL_SOCKET, TCP_KEEPINTVL, &optval, optlen) < 0) { - debug(DBG_ERR, "tcpconnect: setsockopt TCP_KEEPINTVL failed"); - } - optval = 1; - if(setsockopt(server->sock, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) { - debug(DBG_ERR, "tcpconnect: setsockopt SO_KEEPALIVE failed"); - } + enable_keepalive(server->sock); break; } } diff --git a/tls.c b/tls.c index bbf9da3..7b18232 100644 --- a/tls.c +++ b/tls.c @@ -130,6 +130,8 @@ int tlsconnect(struct server *server, struct timeval *when, int timeout, char *t if ((server->sock = connecttcphostlist(server->conf->hostports, srcres)) < 0) continue; + enable_keepalive(server->sock); + SSL_free(server->ssl); server->ssl = NULL; ctx = tlsgetctx(handle, server->conf->tlsconf); diff --git a/util.c b/util.c index c976175..41e299c 100644 --- a/util.c +++ b/util.c @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -123,6 +124,28 @@ void disable_DF_bit(int socket, struct addrinfo *res) { } } +void enable_keepalive(int socket) { + int optval; + socklen_t optlen = sizeof(optval); + + optval = 3; + if(setsockopt(socket, SOL_TCP, TCP_KEEPCNT, &optval, optlen) < 0) { + debug(DBG_ERR, "enable_keepalive: setsockopt TCP_KEEPCNT failed"); + } + optval = 10; + if(setsockopt(socket, SOL_TCP, TCP_KEEPIDLE, &optval, optlen) < 0) { + debug(DBG_ERR, "enable_keepalive: setsockopt TCP_KEEPIDLE %d failed", optval); + } + optval = 10; + if(setsockopt(socket, SOL_TCP, TCP_KEEPINTVL, &optval, optlen) < 0) { + debug(DBG_ERR, "enable_keepalive: setsockopt TCP_KEEPINTVL failed"); + } + optval = 1; + if(setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) { + debug(DBG_ERR, "enable_keepalive: setsockopt SO_KEEPALIVE failed"); + } +} + int bindtoaddr(struct addrinfo *addrinfo, int family, int reuse, int v6only) { int s, on = 1; struct addrinfo *res; diff --git a/util.h b/util.h index 36a5cdd..b5758f5 100644 --- a/util.h +++ b/util.h @@ -20,6 +20,7 @@ void port_set(struct sockaddr *sa, uint16_t port); void printfchars(char *prefixfmt, char *prefix, char *charfmt, uint8_t *chars, int len); void disable_DF_bit(int socket, struct addrinfo *res); +void enable_keepalive(int socket); int bindtoaddr(struct addrinfo *addrinfo, int family, int reuse, int v6only); int connecttcp(struct addrinfo *addrinfo, struct addrinfo *src, uint16_t timeout);