diff --git a/src/sockopt.c b/src/sockopt.c index a7a219d..69fabd2 100644 --- a/src/sockopt.c +++ b/src/sockopt.c @@ -1614,8 +1614,8 @@ int getlocaladdrs(char ip_addrs[][IP_ADDRESS_SIZE], \ int s; struct ifconf ifconf; struct ifreq ifr[32]; - int if_count; - int i; + struct ifreq *ifrp; + char *p_end; int result; *count = 0; @@ -1640,20 +1640,26 @@ int getlocaladdrs(char ip_addrs[][IP_ADDRESS_SIZE], \ return result; } - if_count = ifconf.ifc_len / sizeof(ifr[0]); - if (max_count < if_count) - { - logError("file: "__FILE__", line: %d, " \ - "max_count: %d < iterface count: %d", \ - __LINE__, max_count, if_count); - close(s); - return ENOSPC; - } - - for (i = 0; i < if_count; i++) + ifrp = ifconf.ifc_req; + p_end = (char *)ifr + ifconf.ifc_len; + while ((char *)ifrp < p_end) { + struct sockaddr *sa = &ifrp->ifr_addr; struct sockaddr_in *s_in; - s_in = (struct sockaddr_in *) &ifr[i].ifr_addr; + + if (*count >= max_count) + { + logError("file: "__FILE__", line: %d, " \ + "max_count: %d < iterface count: %d", \ + __LINE__, max_count, *count); + close(s); + return ENOSPC; + } + + + s_in = (struct sockaddr_in *) &ifrp->ifr_addr; + if (sa->sa_family == AF_INET) + { if (!inet_ntop(AF_INET, &s_in->sin_addr, \ ip_addrs[*count], IP_ADDRESS_SIZE)) { @@ -1666,6 +1672,10 @@ int getlocaladdrs(char ip_addrs[][IP_ADDRESS_SIZE], \ return result; } (*count)++; + } + + + ifrp = (struct ifreq*)((caddr_t)&ifrp->ifr_addr + sa->sa_len); } close(s); diff --git a/src/tests/test_mblock.c b/src/tests/test_mblock.c index 419ef10..3f06456 100644 --- a/src/tests/test_mblock.c +++ b/src/tests/test_mblock.c @@ -12,6 +12,7 @@ #include "fast_mblock.h" #include "sockopt.h" #include "system_info.h" +#include "local_ip_func.h" struct my_struct { struct fast_mblock_man *mblock; @@ -67,6 +68,9 @@ int main(int argc, char *argv[]) return result; } +load_local_host_ip_addrs(); +print_local_host_ip_addrs(); + getifconfigs(if_configs, sizeof(if_configs) / sizeof(if_configs[0]), &count); printf("ifconfig count: %d\n", count); for (i=0; i