From 67c34a74607248dfa80b3678d1e041b8499ab51f Mon Sep 17 00:00:00 2001 From: Yves Rutschle Date: Sun, 9 Feb 2014 13:29:49 +0100 Subject: [PATCH] set IP_FREEBIND if available to bind to non-existent interfaces --- ChangeLog | 3 +++ common.c | 13 +++++++++---- common.h | 4 ++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3c1ff68..5f86b6a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -10,6 +10,9 @@ vNEXT: actual errors if connections are dropped before getting to getpeername). + Set IP_FREEDBIND if available to bind to addresses + that don't yet exist. + v1.15: 27JUL2013 Added --transparent option for transparent proxying. See README for iptables magic and capability diff --git a/common.c b/common.c index 2214d7e..7703189 100644 --- a/common.c +++ b/common.c @@ -62,7 +62,7 @@ int start_listen_sockets(int *sockfd[], struct addrinfo *addr_list) { struct sockaddr_storage *saddr; struct addrinfo *addr; - int i, res, reuse; + int i, res, one; int num_addr = 0; for (addr = addr_list; addr; addr = addr->ai_next) @@ -83,9 +83,14 @@ int start_listen_sockets(int *sockfd[], struct addrinfo *addr_list) (*sockfd)[i] = socket(saddr->ss_family, SOCK_STREAM, 0); check_res_dumpdie((*sockfd)[i], addr, "socket"); - reuse = 1; - res = setsockopt((*sockfd)[i], SOL_SOCKET, SO_REUSEADDR, (char*)&reuse, sizeof(reuse)); - check_res_dumpdie(res, addr, "setsockopt"); + one = 1; + res = setsockopt((*sockfd)[i], SOL_SOCKET, SO_REUSEADDR, (char*)&one, sizeof(one)); + check_res_dumpdie(res, addr, "setsockopt(SO_REUSEADDR)"); + + if (IP_FREEBIND) { + res = setsockopt((*sockfd)[i], IPPROTO_IP, IP_FREEBIND, (char*)&one, sizeof(one)); + check_res_dumpdie(res, addr, "setsockopt(IP_FREEBIND)"); + } res = bind((*sockfd)[i], addr->ai_addr, addr->ai_addrlen); check_res_dumpdie(res, addr, "bind"); diff --git a/common.h b/common.h index a88440a..3d09d71 100644 --- a/common.h +++ b/common.h @@ -49,6 +49,10 @@ #define TRACE #endif +#ifndef IP_FREEBIND +#define IP_FREEBIND 0 +#endif + enum connection_state { ST_PROBING=1, /* Waiting for timeout to find where to forward */ ST_SHOVELING /* Connexion is established */