C code to get the IP address

0 votes
asked Oct 15, 2009 by codingfreak

How do I get the IP address of the local machine using C code?

If there are multiple Interfaces then I should be able to display the IP address of each interface.

NOTE: Do not use any commands like ifconfig within C code to retrieve the IP address.

3 Answers

0 votes
answered Oct 15, 2009 by michael-foukarakis
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>

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

    fd = socket(AF_INET, SOCK_DGRAM, 0);

    ifr.ifr_addr.sa_family = AF_INET;

    snprintf(ifr.ifr_name, IFNAMSIZ, "eth0");

    ioctl(fd, SIOCGIFADDR, &ifr);

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

    close(fd);
}

If you want to enumerate all the interfaces, have a look at the getifaddrs() function - if you're on Linux.

0 votes
answered Oct 15, 2009 by test

Get known all interfaces from "/proc/net/dev". Note: it cannot get all interfaces using ioctl only.

#define PROC_NETDEV "/proc/net/dev"

fp = fopen(PROC_NETDEV, "r");
while (NULL != fgets(buf, sizeof buf, fp)) {
    s = strchr(buf, ':');
    *s = '\0';
    s = buf;

    // Filter all space ' ' here
    got one interface name here, continue for others
}
fclose(fp);

Then get the address using ioctl():

struct ifreq ifr;
struct ifreq ifr_copy;
struct sockaddr_in *sin;

for each interface name {
    strncpy(ifr.ifr_name, ifi->name, sizeof(ifr.ifr_name) - 1);
    ifr_copy = ifr;
    ioctl(fd, SIOCGIFFLAGS, &ifr_copy);
    ifi->flags = ifr_copy.ifr_flags;
    ioctl(fd, SIOCGIFADDR, &ifr_copy);
    sin = (struct sockaddr_in*)&ifr_copy.ifr_addr;
    ifi->addr = allocating address memory here
    bzero(ifi->addr, sizeof *ifi->addr);
    *(struct sockaddr_in*)ifi->addr = *sin;

    /* Here also you could get netmask and hwaddr. */
}
0 votes
answered Oct 21, 2009 by codingfreak

With the inputs from Michael Foukarakis I am able to show the IP address for various interfaces on the same machine:

#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
    struct ifaddrs *ifaddr, *ifa;
    int family, s;
    char host[NI_MAXHOST];

    if (getifaddrs(&ifaddr) == -1) {
        perror("getifaddrs");
        exit(EXIT_FAILURE);
    }

    for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
        family = ifa->ifa_addr->sa_family;

        if (family == AF_INET) {
            s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in),
                                           host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
            if (s != 0) {
                printf("getnameinfo() failed: %s\n", gai_strerror(s));
                exit(EXIT_FAILURE);
            }
            printf("<Interface>: %s \t <Address> %s\n", ifa->ifa_name, host);
        }
    }
    return 0;
}
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...