aboutgitcodebugslistschat
path: root/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'util.c')
-rw-r--r--util.c15
1 files changed, 10 insertions, 5 deletions
diff --git a/util.c b/util.c
index ff1f19f..053dd80 100644
--- a/util.c
+++ b/util.c
@@ -47,12 +47,14 @@ uint8_t eth_pad[ETH_ZLEN] = { 0 };
* @sa: Socket address to bind to
* @ifname: Interface for binding, NULL for any
* @v6only: If >= 0, set IPV6_V6ONLY socket option to this value
+ * @exclusive: Don't set SO_REUSEADDR, so that binding a port already bound
+ * by any other socket fails with EADDRINUSE
*
* Return: newly created socket, negative error code on failure
*/
static int sock_l4_(const struct ctx *c, enum epoll_type type,
const union sockaddr_inany *sa, const char *ifname,
- int v6only)
+ int v6only, bool exclusive)
{
sa_family_t af = sa->sa_family;
bool freebind = false;
@@ -104,7 +106,8 @@ static int sock_l4_(const struct ctx *c, enum epoll_type type,
}
}
- if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(y)))
+ if (!exclusive &&
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(y)))
debug("Failed to set SO_REUSEADDR on socket %i", fd);
if (proto == IPPROTO_UDP) {
@@ -173,11 +176,13 @@ static int sock_l4_(const struct ctx *c, enum epoll_type type,
* @type: epoll type
* @sa: Socket address to bind to
* @ifname: Interface for binding, NULL for any
+ * @exclusive: Don't set SO_REUSEADDR (see sock_l4_())
*
* Return: newly created socket, negative error code on failure
*/
int sock_l4(const struct ctx *c, enum epoll_type type,
- const union sockaddr_inany *sa, const char *ifname)
+ const union sockaddr_inany *sa, const char *ifname,
+ bool exclusive)
{
int v6only = -1;
@@ -188,7 +193,7 @@ int sock_l4(const struct ctx *c, enum epoll_type type,
IN6_IS_ADDR_UNSPECIFIED(&sa->sa6.sin6_addr))
v6only = 1;
- return sock_l4_(c, type, sa, ifname, v6only);
+ return sock_l4_(c, type, sa, ifname, v6only, exclusive);
}
/**
@@ -215,7 +220,7 @@ int sock_l4_dualstack_any(const struct ctx *c, enum epoll_type type,
* default, but sysctl net.ipv6.bindv6only can change that, so set the
* sockopt explicitly.
*/
- return sock_l4_(c, type, &sa, ifname, 0);
+ return sock_l4_(c, type, &sa, ifname, 0, false);
}
/**