getlocaladdrs fixed

pull/5/head
yuqing 2016-01-17 22:48:30 +08:00
parent 481f57befa
commit 05e01ce113
2 changed files with 28 additions and 14 deletions

View File

@ -1614,8 +1614,8 @@ int getlocaladdrs(char ip_addrs[][IP_ADDRESS_SIZE], \
int s; int s;
struct ifconf ifconf; struct ifconf ifconf;
struct ifreq ifr[32]; struct ifreq ifr[32];
int if_count; struct ifreq *ifrp;
int i; char *p_end;
int result; int result;
*count = 0; *count = 0;
@ -1640,20 +1640,26 @@ int getlocaladdrs(char ip_addrs[][IP_ADDRESS_SIZE], \
return result; return result;
} }
if_count = ifconf.ifc_len / sizeof(ifr[0]); ifrp = ifconf.ifc_req;
if (max_count < if_count) p_end = (char *)ifr + ifconf.ifc_len;
{ while ((char *)ifrp < p_end)
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++)
{ {
struct sockaddr *sa = &ifrp->ifr_addr;
struct sockaddr_in *s_in; 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, \ if (!inet_ntop(AF_INET, &s_in->sin_addr, \
ip_addrs[*count], IP_ADDRESS_SIZE)) ip_addrs[*count], IP_ADDRESS_SIZE))
{ {
@ -1666,6 +1672,10 @@ int getlocaladdrs(char ip_addrs[][IP_ADDRESS_SIZE], \
return result; return result;
} }
(*count)++; (*count)++;
}
ifrp = (struct ifreq*)((caddr_t)&ifrp->ifr_addr + sa->sa_len);
} }
close(s); close(s);

View File

@ -12,6 +12,7 @@
#include "fast_mblock.h" #include "fast_mblock.h"
#include "sockopt.h" #include "sockopt.h"
#include "system_info.h" #include "system_info.h"
#include "local_ip_func.h"
struct my_struct { struct my_struct {
struct fast_mblock_man *mblock; struct fast_mblock_man *mblock;
@ -67,6 +68,9 @@ int main(int argc, char *argv[])
return result; return result;
} }
load_local_host_ip_addrs();
print_local_host_ip_addrs();
getifconfigs(if_configs, sizeof(if_configs) / sizeof(if_configs[0]), &count); getifconfigs(if_configs, sizeof(if_configs) / sizeof(if_configs[0]), &count);
printf("ifconfig count: %d\n", count); printf("ifconfig count: %d\n", count);
for (i=0; i<count; i++) { for (i=0; i<count; i++) {