From 7b4b46f97c2d37da38eba4637dcf040105269772 Mon Sep 17 00:00:00 2001 From: Stefan Becker Date: Thu, 27 Jun 2019 12:17:43 +0300 Subject: [PATCH] interfaces: allow list of ignored prefixes Improve on commit b4abda09c79e4ce372a3771300abf568c85c7ff5 Instead of checking one prefix, check against a list of prefixes. This allows libnice to be configured to ignore interfaces from virtual machines and containers. --- agent/interfaces.c | 72 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 19 deletions(-) diff --git a/agent/interfaces.c b/agent/interfaces.c index 3bfae3e..3a3b39c 100644 --- a/agent/interfaces.c +++ b/agent/interfaces.c @@ -73,6 +73,13 @@ #endif /* G_OS_UNIX */ +#ifdef IGNORED_IFACE_PREFIX +static const gchar *ignored_iface_prefix_list[] = { + IGNORED_IFACE_PREFIX, + NULL +}; +#endif + #if (defined(G_OS_UNIX) && defined(HAVE_GETIFADDRS)) || defined(G_OS_WIN32) /* Works on both UNIX and Windows. Magic! */ static gchar * @@ -253,7 +260,10 @@ nice_interfaces_get_local_ips (gboolean include_loopback) GList *ips = NULL; struct ifaddrs *ifa, *results; GList *loopbacks = NULL; - +#ifdef IGNORED_IFACE_PREFIX + const gchar **prefix; + gboolean ignored = FALSE; +#endif if (getifaddrs (&results) < 0) return NULL; @@ -290,18 +300,28 @@ nice_interfaces_get_local_ips (gboolean include_loopback) nice_debug ("Ignoring loopback interface"); g_free (addr_string); } + continue; + } + #ifdef IGNORED_IFACE_PREFIX - } else if (g_str_has_prefix (ifa->ifa_name, IGNORED_IFACE_PREFIX)) { - nice_debug ("Ignoring interface %s as it matches prefix %s", - ifa->ifa_name, IGNORED_IFACE_PREFIX); - g_free (addr_string); -#endif - } else { - if (nice_interfaces_is_private_ip (ifa->ifa_addr)) - ips = add_ip_to_list (ips, addr_string, TRUE); - else - ips = add_ip_to_list (ips, addr_string, FALSE); + for (prefix = ignored_iface_prefix_list; *prefix; prefix++) { + if (g_str_has_prefix (ifa->ifa_name, *prefix)) { + nice_debug ("Ignoring interface %s as it matches prefix %s", + ifa->ifa_name, *prefix); + g_free (addr_string); + ignored = true; + break; + } } + + if (ignored) + continue; +#endif + + if (nice_interfaces_is_private_ip (ifa->ifa_addr)) + ips = add_ip_to_list (ips, addr_string, TRUE); + else + ips = add_ip_to_list (ips, addr_string, FALSE); } freeifaddrs (results); @@ -324,6 +344,10 @@ nice_interfaces_get_local_ips (gboolean include_loopback) struct ifconf ifc; struct sockaddr_in *sa; GList *loopbacks = NULL; +#ifdef IGNORED_IFACE_PREFIX + const gchar **prefix; + gboolean ignored = FALSE; +#endif if ((sockfd = socket (AF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) { nice_debug ("Error : Cannot open socket to retrieve interface list"); @@ -381,17 +405,27 @@ nice_interfaces_get_local_ips (gboolean include_loopback) loopbacks = add_ip_to_list (loopbacks, g_strdup (inet_ntoa (sa->sin_addr)), TRUE); else nice_debug ("Ignoring loopback interface"); + continue; + } + #ifdef IGNORED_IFACE_PREFIX - } else if (g_str_has_prefix (ifr->ifr_name, IGNORED_IFACE_PREFIX)) { - nice_debug ("Ignoring interface %s as it matches prefix %s", - ifr->ifr_name, IGNORED_IFACE_PREFIX); + for (prefix = ignored_iface_prefix_list; *prefix; prefix++) { + if (g_str_has_prefix (ifr->ifr_name, *prefix)) { + nice_debug ("Ignoring interface %s as it matches prefix %s", + ifr->ifr_name, *prefix); + ignored = true; + break; + } + } + + if (ignored) + continue; #endif + + if (nice_interfaces_is_private_ip ((struct sockaddr *) sa)) { + ips = add_ip_to_list (ips, g_strdup (inet_ntoa (sa->sin_addr)), TRUE); } else { - if (nice_interfaces_is_private_ip ((struct sockaddr *) sa)) { - ips = add_ip_to_list (ips, g_strdup (inet_ntoa (sa->sin_addr)), TRUE); - } else { - ips = add_ip_to_list (ips, g_strdup (inet_ntoa (sa->sin_addr)), FALSE); - } + ips = add_ip_to_list (ips, g_strdup (inet_ntoa (sa->sin_addr)), FALSE); } } -- 2.7.4