Solved

How to get multiple local IP addresses without gethostbyname()

Posted on 2004-09-11
6
3,197 Views
Last Modified: 2012-08-14
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
Comment
Question by:Aidman
  • 3
  • 2
6 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12034038
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
 

Author Comment

by:Aidman
ID: 12034096
>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
 

Author Comment

by:Aidman
ID: 12034118
I probably should point out that the process of gathering all IPs should not require any help or involvement from the user.
0
Network it in WD Red

There's an industry-leading WD Red drive for every compatible NAS system to help fulfill your data storage needs. With drives up to 8TB, WD Red offers a wide array of solutions for customers looking to build the biggest, best-performing NAS storage solution.  

 
LVL 45

Expert Comment

by:sunnycoder
ID: 12034405
>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
 

Author Comment

by:Aidman
ID: 12034603
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
 
LVL 1

Accepted Solution

by:
bsnh99 earned 250 total points
ID: 12036707
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now