Linux C: Get default interface's IP address

13,078

Solution 1

This code will do the thing:

#include <stdio.h>
#include <unistd.h>
#include <string.h> /* For strncpy */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>

int
main()
{
    int fd;
    struct ifreq ifr;

    fd = socket(AF_INET, SOCK_DGRAM, 0);

    /* I want to get an IPv4 IP address */
    ifr.ifr_addr.sa_family = AF_INET;

    /* I want an IP address attached to "eth0" */
    strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);

    ioctl(fd, SIOCGIFADDR, &ifr);

    close(fd);

    /* Display result */
    printf("%s\n", inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr));

    return 0;
}

Result:

192.168.5.27

RUN SUCCESSFUL (total time: 52ms)

Alternatively, you can also use an IP address mask. For example, this will print only if mask is different from 255.0.0.0 (loopback mask)

#include <stdio.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>

int main(int argc, const char * argv[]) {
    struct ifaddrs * ifAddrStruct = NULL, * ifa = NULL;
    void * tmpAddrPtr = NULL;

    getifaddrs(&ifAddrStruct);
    for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
        if (ifa ->ifa_addr->sa_family == AF_INET) { // Check it is IPv4
            char mask[INET_ADDRSTRLEN];
            void* mask_ptr = &((struct sockaddr_in*) ifa->ifa_netmask)->sin_addr;
            inet_ntop(AF_INET, mask_ptr, mask, INET_ADDRSTRLEN);
            if (strcmp(mask, "255.0.0.0") != 0) {
                printf("mask:%s\n", mask);
                // Is a valid IPv4 Address
                tmpAddrPtr = &((struct sockaddr_in *) ifa->ifa_addr)->sin_addr;
                char addressBuffer[INET_ADDRSTRLEN];
                inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
                printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
            }
            else if (ifa->ifa_addr->sa_family == AF_INET6) { // Check it is
                // a valid IPv6 Address.

                // Do something
            }
        }
    }
    if (ifAddrStruct != NULL)
        freeifaddrs(ifAddrStruct);
    return 0;
}

Result:

mask:255.255.255.0

eth0 IP Address 192.168.5.27

RUN SUCCESSFUL (total time: 53ms)

Solution 2

A method that covers more situations would involve something like this:

  • parse the /proc/net/route file to see which interface is the "default" one;
  • use your piece of code from the first post (using getifaddrs) to figure out the IP address for that interface.

The route file parsing might look like this (C++ code for brevity):

std::string defaultInterface;

std::ifstream routeFile(NET_ROUTE_FILEPATH, std::ios_base::in);
if (!routeFile.good())
{
    return;
}

std::string line;
std::vector<std::string> tokens;
while(std::getline(routeFile, line))
{
    std::istringstream stream(line);
    std::copy(std::istream_iterator<std::string>(stream),
              std::istream_iterator<std::string>(),
              std::back_inserter<std::vector<std::string> >(tokens));

    // the default interface is the one having the second 
    // field, Destination, set to "00000000"
    if ((tokens.size() >= 2) && (tokens[1] == std::string("00000000")))
    {
        defaultInterface = tokens[0];
        break;
    }

    tokens.clear();
}

routeFile.close();

Then, in your code, in the for loop iterating over the ifAddrStruct structs, you could add a test for ifa->ifa_name to be the defaultInterface determined above.

Share:
13,078
Musher
Author by

Musher

Updated on June 04, 2022

Comments

  • Musher
    Musher almost 2 years

    My question is about the following code (in this link):

    #include <stdio.h>
    #include <sys/types.h>
    #include <ifaddrs.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <arpa/inet.h>
    
    int main (int argc, const char * argv[]) {
        struct ifaddrs * ifAddrStruct = NULL;
        struct ifaddrs * ifa = NULL;
        void * tmpAddrPtr = NULL;
    
        getifaddrs(&ifAddrStruct);
    
        for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
            if (ifa ->ifa_addr->sa_family==AF_INET) { // Check it is
                // a valid IPv4 address
                tmpAddrPtr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
                char addressBuffer[INET_ADDRSTRLEN];
                inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
                printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
            }
            else if (ifa->ifa_addr->sa_family==AF_INET6) { // Check it is
                // a valid IPv6 address
                tmpAddrPtr = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
                char addressBuffer[INET6_ADDRSTRLEN];
                inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
                printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
            }
        }
        if (ifAddrStruct != NULL)
            freeifaddrs(ifAddrStruct);
        return 0;
    }
    

    This code's output is like this:

    lo IP Address 127.0.0.1
    wlan0 IP Address 172.28.1.89 (I want to only this)
    lo IP Address ::1
    wlan0 IP Address fe80::6e71:d9ff:fe1d:b0
    

    How can I get the default interface's IP address? (You can give an answer with another code piece.)