James wrote: > if i know a device's name (eth0 etc), how can i find out it's IP address? > i've looked in the ifconfig source but there's so much extra stuff in there > it's hard finding the relevant bits. Try the attached program. -- Glynn Clements <[EMAIL PROTECTED]>
#include <sys/ioctl.h> #include <sys/socket.h> #include <netinet/in.h> #include <net/if.h> #include <arpa/inet.h> #include <unistd.h> #include <stdio.h> static struct in_addr device_address(const char *if_name) { struct ifreq ifr; struct sockaddr_in *addr; int s; s = socket(PF_INET, SOCK_DGRAM, 0); if (s == -1) { perror("socket"); exit(1); } strcpy(ifr.ifr_name, if_name); ifr.ifr_name.sa_family = AF_INET; if (ioctl(s, SIOCGIFADDR, &ifr) < 0) { perror("ioctl(SIOCGIFADDR)"); exit(1); } addr = (struct sockaddr_in *) &ifr.ifr_addr; close(s); return addr->sin_addr; } int main(int argc, char **argv) { struct in_addr addr; if (argc < 2) { fprintf(stderr, "Usage: %s <interface>\n", argv[0]); exit(1); } addr = device_address(argv[1]); printf("IP Address: %s\n", inet_ntoa(addr)); return 0; }