?
Solved

How to get multiple local IP addresses without gethostbyname()

Posted on 2004-09-11
6
Medium Priority
?
3,295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 and use conditional statements in the C programming language.

764 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