views:

561

answers:

3

Hi

I like to know how to 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  A: 
#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;

    strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);

    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.

Michael Foukarakis
A: 

get known all interfaces from "/proc/net/dev", note 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 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
}
EffoStaff Effo
see other posts for how to open the "fd"
EffoStaff Effo
+1  A: 

With the inputs from Michael Foukarakis I am able to show the ipaddress 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;
}
codingfreak