Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3330
  • Last Modified:

How to get multiple local IP addresses without gethostbyname()

Hi,

I am looking for a way to resolve all IP addresses on a local Unix/Linux system without using gethostbyname, since it has proven unable. For me, gethostbyname returns only one IP while ifconfig -a displays two. I have read that gethostbyname can be fixed through config files, but that is not a solution to me since I am not interested in altering a system’s configurations just to be able to run my program on it. However I have read about the function ioctl, which can be called with SIOCGIFADDR and return an IP. But the problem here is that I have to input interface names which can vary from system to system. So is it possible to enumerate all interface names and thereby resolve all IPs?

Any ideas are highly appreciated.
0
Aidman
Asked:
Aidman
  • 3
  • 2
1 Solution
 
sunnycoderCommented:
Hi Aidman,

You can launch ifconfig from your rpgram to gather the names of all the interfaces and get their IP addresses using the ioctl


#include <stdio.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <netinet/in.h>

#define IFNAME "ppp0"
...
  int fd;
  struct ifreq ifr;
  u_char *addr;

  fd = socket (AF_INET, SOCK_DGRAM,IPPROTO_IP);
  memset (&ifr, 0, sizeof (struct ifreq));
  strcpy (ifr.ifr_name, IFNAME);
  ifr.ifr_addr.sa_family = AF_INET;
  ioctl(fd, SIOCGIFADDR, &ifr);
  addr=(u_char*)&(((struct sockaddr_in * )&ifr.ifr_addr)->sin_addr);
  printf("eth %s, addr %d.%d.%d.%d\n", ifr.ifr_name,addr[0],addr[1],addr[2],addr[3]);
  close(fd);

The code was written by scn here
http://www.experts-exchange.com/Programming/Programming_Platforms/Linux_Programming/Q_20919674.html

cheers
sunnycoder
0
 
AidmanAuthor Commented:
>You can launch ifconfig from your rpgram to gather the names of all the interfaces and get their IP addresses using the ioctl

Thats seems very difficult to program (since the user should not be aware of it) and also there is the problem that all users don't have access to ifconfig, and if they do it isn't a guaranty that the command is pathed for the terminal. I for instance have to write /sbin/ifconfig
0
 
AidmanAuthor Commented:
I probably should point out that the process of gathering all IPs should not require any help or involvement from the user.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
sunnycoderCommented:
>Thats seems very difficult to program
Its easy

p = popen ("/sbin/ifconfig", "r")

now read from p and parse the results ...

ofcourse you will need superuser permissions for this ... Are you planning to be able to extract all IP and interface information as an unpriviledged user? I do not think that will be possible, I will try to look though
0
 
AidmanAuthor Commented:
I found an very interesting function which seems to solve my problem, getifaddrs().
But when I try to compile gcc complains about not finding the function’s header ifaddrs.h
It could be because it’s written for FreeBSD, but if so then there should be an equivalent function for my system also.
0
 
bsnh99Commented:
Check this out:

    http://www.daimi.au.dk/~kasperd/comp.os.linux.development.faq.html#IP

Note use of SIOCGIFCONF to get the list of interfaces. This program will also list multiple IP addresses configured on a single interface.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now