From 3f3b9d158788760a1a5d9f57e1aef6037894cdd9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Olivier=20Cr=C3=AAte?= Date: Fri, 22 Jul 2022 16:55:14 -0400 Subject: [PATCH] udp-bsd: Explicitly bind to the interface when creating a socket --- socket/udp-bsd.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/socket/udp-bsd.c b/socket/udp-bsd.c index 7064408..481b372 100644 --- a/socket/udp-bsd.c +++ b/socket/udp-bsd.c @@ -51,6 +51,7 @@ #include "udp-bsd.h" #include "agent-priv.h" +#include "interfaces.h" #ifndef G_OS_WIN32 #include @@ -119,6 +120,33 @@ nice_udp_bsd_socket_new (NiceAddress *addr, GError **error) return NULL; } +#if defined(IP_UNICAST_IF) && defined(IPV6_UNICAST_IF) + if (addr) { + guint if_index = nice_interfaces_get_if_index_by_addr (addr); + + if (if_index) { + guint level, optname; + GError *gerr = NULL; + + if (nice_address_ip_version (addr) == 6) { + level = IPPROTO_IPV6; + optname = IPV6_UNICAST_IF; + } else { + level = IPPROTO_IP; + optname = IP_UNICAST_IF; + } + + if_index = htonl (if_index); + if (!g_socket_set_option (gsock, level, optname, if_index, &gerr)) { + nice_debug ("Could not bind socket to specific interface: %s", + gerr->message); + g_clear_error (&gerr); + } + } + } +#endif + + /* GSocket: All socket file descriptors are set to be close-on-exec. */ g_socket_set_blocking (gsock, false); gaddr = g_socket_address_new_from_native (&name.addr, sizeof (name)); -- 2.7.4