Solved

How to get multiple local IP addresses without gethostbyname()

Posted on 2004-09-11
6
3,188 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
Comment Utility
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
Comment Utility
>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
Comment Utility
I probably should point out that the process of gathering all IPs should not require any help or involvement from the user.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
>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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

772 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

10 Experts available now in Live!

Get 1:1 Help Now