Module: kamailio Branch: master Commit: 3e7d5ed34033067377ed9034e4ec49be444ca6fe URL: https://github.com/kamailio/kamailio/commit/3e7d5ed34033067377ed9034e4ec49be...
Author: Rhys Hanrahan rhys@nexusone.com.au Committer: Daniel-Constantin Mierla miconda@gmail.com Date: 2022-01-07T12:27:25+01:00
core: Updated check_local_addresses to use getifaddrs
Changed virtual socket code to use getifaddrs instead of gethostbyname as this avoids lookups against the hosts file, which causes incorrect results.
---
Modified: src/core/socket_info.c
---
Diff: https://github.com/kamailio/kamailio/commit/3e7d5ed34033067377ed9034e4ec49be... Patch: https://github.com/kamailio/kamailio/commit/3e7d5ed34033067377ed9034e4ec49be...
---
diff --git a/src/core/socket_info.c b/src/core/socket_info.c index 8ee6cd0b32..afb1dd5603 100644 --- a/src/core/socket_info.c +++ b/src/core/socket_info.c @@ -575,8 +575,8 @@ struct socket_info** get_sock_info_list(unsigned short proto) */ static int check_local_addresses(struct socket_info* si) { - struct hostent* he; - struct utsname myname; + int match = 0; + struct ifaddrs *ifap, *ifa;
if (si == NULL) { LM_ERR("Socket info is NULL. Returning no match.\n"); @@ -589,40 +589,41 @@ static int check_local_addresses(struct socket_info* si) return 1; }
- if (uname(&myname) <0){ - LM_ERR("Cannot determine hostname. Guessing a not local virtual IP.\n"); + if (getifaddrs(&ifap) != 0) { + LM_ERR("getifaddrs failed. Assuming no match.\n"); return 0; } - - //Should return a list of local IPs - he = _resolvehost(myname.nodename); - if (he == NULL) { - LM_ERR("Cannot get list of local IPs. Guessing not a local virtual IP.\n"); - return 0; - } - char** paddrlist = he->h_addr_list; - int i = 0; - while (*paddrlist != NULL) + + for (ifa = ifap; ifa; ifa = ifa->ifa_next) { + /* skip if no IP addr associated with the interface */ + if (ifa->ifa_addr==0) + continue; +#ifdef AF_PACKET + /* skip AF_PACKET addr family since it is of no use later on */ + if (ifa->ifa_addr->sa_family == AF_PACKET) + continue; +#endif struct ip_addr local_addr; - hostent2ip_addr(&local_addr, he, i); + sockaddr2ip_addr(&local_addr, (struct sockaddr*)ifa->ifa_addr);
LM_DBG("Checking local address: %s\n", ip_addr2a(&local_addr)); if (ip_addr_cmp(&si->address, &local_addr)) { - LM_DBG("Found matching local IP for virtual socket: %s\n", ip_addr2a(&local_addr)); - return 1; + match = 1; + LM_DBG("Found matching local IP %s for virtual socket %s\n", ip_addr2a(&local_addr), si->name.s); + break; } - - i++; - paddrlist++; } - + freeifaddrs(ifap); //Default to not local if no match is found - LM_DBG("No matching local IP found.\n"); - return 0; + if (!match) { + LM_DBG("No matching local IP found for socket %s.\n", si->name.s); + return 0; + } else { + return 1; + } }
- /* helper function for grep_sock_info * params: * host - hostname to compare with