From 6c695c52c4163fa8d3ea62ce3538c2a4fa2372f2 Mon Sep 17 00:00:00 2001 From: Divya Sampath Kumar Date: Wed, 28 Feb 2024 10:05:18 -0800 Subject: [PATCH] Filter out non-operational and loopback interfaces --- src/source/Ice/Network.c | 79 +++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/src/source/Ice/Network.c b/src/source/Ice/Network.c index a695838da8..b5568bb97b 100644 --- a/src/source/Ice/Network.c +++ b/src/source/Ice/Network.c @@ -35,48 +35,51 @@ STATUS getLocalhostIpAddresses(PKvsIpAddress destIpList, PUINT32 pDestIpListLen, CHK(retWinStatus == ERROR_SUCCESS, STATUS_GET_LOCAL_IP_ADDRESSES_FAILED); for (aa = adapterAddresses; aa != NULL && ipCount < destIpListLen; aa = aa->Next) { - char ifa_name[BUFSIZ]; - memset(ifa_name, 0, BUFSIZ); - WideCharToMultiByte(CP_ACP, 0, aa->FriendlyName, wcslen(aa->FriendlyName), ifa_name, BUFSIZ, NULL, NULL); - - for (ua = aa->FirstUnicastAddress; ua != NULL; ua = ua->Next) { - if (filter != NULL) { - DLOGI("Callback set to allow network interface filtering"); - // The callback evaluates to a FALSE if the application is interested in black listing an interface - if (filter(customData, ifa_name) == FALSE) { - filterSet = FALSE; - } else { - filterSet = TRUE; + // Skip inactive interfaces and loop back interfaces + if (aa->OperStatus == IfOperStatusUp && aa->IfType != IF_TYPE_SOFTWARE_LOOPBACK) { + char ifa_name[BUFSIZ]; + memset(ifa_name, 0, BUFSIZ); + WideCharToMultiByte(CP_ACP, 0, aa->FriendlyName, wcslen(aa->FriendlyName), ifa_name, BUFSIZ, NULL, NULL); + + for (ua = aa->FirstUnicastAddress; ua != NULL; ua = ua->Next) { + if (filter != NULL) { + DLOGI("Callback set to allow network interface filtering"); + // The callback evaluates to a FALSE if the application is interested in black listing an interface + if (filter(customData, ifa_name) == FALSE) { + filterSet = FALSE; + } else { + filterSet = TRUE; + } } - } - - // If filter is set, ensure the details are collected for the interface - if (filterSet == TRUE) { - int family = ua->Address.lpSockaddr->sa_family; - - if (family == AF_INET) { - destIpList[ipCount].family = KVS_IP_FAMILY_TYPE_IPV4; - destIpList[ipCount].port = 0; - - pIpv4Addr = (struct sockaddr_in*) (ua->Address.lpSockaddr); - MEMCPY(destIpList[ipCount].address, &pIpv4Addr->sin_addr, IPV4_ADDRESS_LENGTH); - } else { - destIpList[ipCount].family = KVS_IP_FAMILY_TYPE_IPV6; - destIpList[ipCount].port = 0; - pIpv6Addr = (struct sockaddr_in6*) (ua->Address.lpSockaddr); - // Ignore unspecified addres: the other peer can't use this address - // Ignore link local: not very useful and will add work unnecessarily - // Ignore site local: https://tools.ietf.org/html/rfc8445#section-5.1.1.1 - if (IN6_IS_ADDR_UNSPECIFIED(&pIpv6Addr->sin6_addr) || IN6_IS_ADDR_LINKLOCAL(&pIpv6Addr->sin6_addr) || - IN6_IS_ADDR_SITELOCAL(&pIpv6Addr->sin6_addr)) { - continue; + // If filter is set, ensure the details are collected for the interface + if (filterSet == TRUE) { + int family = ua->Address.lpSockaddr->sa_family; + + if (family == AF_INET) { + destIpList[ipCount].family = KVS_IP_FAMILY_TYPE_IPV4; + destIpList[ipCount].port = 0; + + pIpv4Addr = (struct sockaddr_in*) (ua->Address.lpSockaddr); + MEMCPY(destIpList[ipCount].address, &pIpv4Addr->sin_addr, IPV4_ADDRESS_LENGTH); + } else { + destIpList[ipCount].family = KVS_IP_FAMILY_TYPE_IPV6; + destIpList[ipCount].port = 0; + + pIpv6Addr = (struct sockaddr_in6*) (ua->Address.lpSockaddr); + // Ignore unspecified addres: the other peer can't use this address + // Ignore link local: not very useful and will add work unnecessarily + // Ignore site local: https://tools.ietf.org/html/rfc8445#section-5.1.1.1 + if (IN6_IS_ADDR_UNSPECIFIED(&pIpv6Addr->sin6_addr) || IN6_IS_ADDR_LINKLOCAL(&pIpv6Addr->sin6_addr) || + IN6_IS_ADDR_SITELOCAL(&pIpv6Addr->sin6_addr)) { + continue; + } + MEMCPY(destIpList[ipCount].address, &pIpv6Addr->sin6_addr, IPV6_ADDRESS_LENGTH); } - MEMCPY(destIpList[ipCount].address, &pIpv6Addr->sin6_addr, IPV6_ADDRESS_LENGTH); - } - // in case of overfilling destIpList - ipCount++; + // in case of overfilling destIpList + ipCount++; + } } } }