diff --git a/src/local.c b/src/local.c index 484a3473..2881c6c8 100644 --- a/src/local.c +++ b/src/local.c @@ -170,6 +170,10 @@ int create_and_bind(const char *addr, const char *port) #ifdef SO_NOSIGPIPE setsockopt(listen_sock, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt)); #endif + int err = set_reuseport(listen_sock); + if (err == 0) { + LOGI("tcp port reuse enabled"); + } s = bind(listen_sock, rp->ai_addr, rp->ai_addrlen); if (s == 0) { diff --git a/src/netutils.c b/src/netutils.c index b4227a6c..e3b192e6 100644 --- a/src/netutils.c +++ b/src/netutils.c @@ -42,6 +42,16 @@ #include "netutils.h" #include "utils.h" +#ifndef SO_REUSEPORT +#define SO_REUSEPORT 15 +#endif + +int set_reuseport(int socket) +{ + int opt = 1; + return setsockopt(socket, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt)); +} + size_t get_sockaddr_len(struct sockaddr *addr) { if (addr->sa_family == AF_INET) { diff --git a/src/netutils.h b/src/netutils.h index 81a52856..e9717986 100644 --- a/src/netutils.h +++ b/src/netutils.h @@ -20,5 +20,11 @@ * . */ +#ifndef _NETUTILS_H +#define _NETUTILS_H + size_t get_sockaddr_len(struct sockaddr *addr); size_t get_sockaddr(char *host, char *port, struct sockaddr_storage *storage, int block); +int set_reuseport(int socket); + +#endif diff --git a/src/redir.c b/src/redir.c index 05d6b7dc..b9d24318 100644 --- a/src/redir.c +++ b/src/redir.c @@ -139,6 +139,10 @@ int create_and_bind(const char *addr, const char *port) #ifdef SO_NOSIGPIPE setsockopt(listen_sock, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt)); #endif + int err = set_reuseport(listen_sock); + if (err == 0) { + LOGI("tcp port reuse enabled"); + } s = bind(listen_sock, rp->ai_addr, rp->ai_addrlen); if (s == 0) { diff --git a/src/server.c b/src/server.c index a3424d83..600b516e 100644 --- a/src/server.c +++ b/src/server.c @@ -356,6 +356,11 @@ int create_and_bind(const char *host, const char *port) #ifdef SO_NOSIGPIPE setsockopt(listen_sock, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt)); #endif + int err = set_reuseport(listen_sock); + if (err == 0) { + LOGI("port reuse enabled"); + } + s = bind(listen_sock, rp->ai_addr, rp->ai_addrlen); if (s == 0) { /* We managed to bind successfully! */ diff --git a/src/tunnel.c b/src/tunnel.c index 76d8ab92..19900ddf 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -148,6 +148,10 @@ int create_and_bind(const char *addr, const char *port) #ifdef SO_NOSIGPIPE setsockopt(listen_sock, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt)); #endif + int err = set_reuseport(listen_sock); + if (err == 0) { + LOGI("tcp port reuse enabled"); + } s = bind(listen_sock, rp->ai_addr, rp->ai_addrlen); if (s == 0) { diff --git a/src/udprelay.c b/src/udprelay.c index 11ec652c..d771b223 100644 --- a/src/udprelay.c +++ b/src/udprelay.c @@ -438,6 +438,10 @@ int create_server_socket(const char *host, const char *port) #ifdef SO_NOSIGPIPE set_nosigpipe(server_sock); #endif + int err = set_reuseport(server_sock); + if (err == 0) { + LOGI("udp port reuse enabled"); + } #ifdef MODULE_REDIR if (setsockopt(server_sock, SOL_IP, IP_TRANSPARENT, &opt, sizeof(opt))) {