简体   繁体   中英

How to get IP address (IPv4 is NULL, but IPv6 is exist), detail for content

Sorry, the title maybe be confuse. For example, eth1 substitute for br100. so ipv4 address of the eth1 is NULL.

/etc/networks/interfaces

br100 Link encap:Ethernet HWaddr 00:0c:29:75:e2:0a
inet addr:192.168.11.249 Bcast:192.168.11.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe75:e20a/64 Scope:Link

eth1 Link encap:Ethernet HWaddr 00:0c:29:75:e2:0a
inet6 addr: fe80::20c:29ff:fe75:e20a/64 Scope:Link

How can I do to read NULL value when I getting ipv4 of the eth1?

Here is my getipv4 function:

sIPv4 = new string[giIfaceCount];
struct ifaddrs *ifAddrStruct;
struct ifaddrs *ifa;
void *tmpAddrPtr;
if (getifaddrs(&ifAddrStruct) == -1)
{
    perror("getifaddrs");
    exit(1);
}
int iFlag4 = 0;
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next)
{
    if (ifa->ifa_addr->sa_family == AF_INET)
    {
        tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
        char addressBuffer[1024];
        if (strcmp(inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, 1024),"NULL") == 0)
        {
            cout << "addressBuffer = NULL" << endl;
        }
        else
        {
            inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, 1024);
            if (strcmp(ifa->ifa_name,"lo") == 0)
            {
                continue;
            }
            else
            {
                *(sIPv4+iFlag4) = addressBuffer;
                testIPv4.append(addressBuffer);
                testIPv4.append(" ");
                iFlag4 = iFlag4+1;
            }
        }
    }
}

Thanks to guys, I have already solved my problem.

In brief, I had combined all functions(about getIPv4 , getMAC , getIPv6 ...) into getHostinfo function. Then, It one by one got host information through a for loop (Interface Count).

Finally, I greatly appreciated all guys. ^__^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM