Solved

How to get multiple local IP addresses without gethostbyname()

Posted on 2004-09-11
6
3,216 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

832 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