summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier CrĂȘte <olivier.crete@collabora.com>2022-07-22 16:55:14 -0400
committerOlivier CrĂȘte <olivier.crete@collabora.com>2022-07-29 12:35:37 -0400
commit3f3b9d158788760a1a5d9f57e1aef6037894cdd9 (patch)
tree6e215df3e216a73cde1d0e83b5f08cb12499bac1
parenta667957f7b23502da111dc2b314a6ad4120c590d (diff)
downloadlibnice-3f3b9d158788760a1a5d9f57e1aef6037894cdd9.tar.gz
udp-bsd: Explicitly bind to the interface when creating a socket
-rw-r--r--socket/udp-bsd.c28
1 files changed, 28 insertions, 0 deletions
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 <unistd.h>
@@ -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));