LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-07-2002, 10:55 PM   #1
yrraja
Member
 
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114

Rep: Reputation: 15
Getting Network Interface Information


I am using Redhat Linux 7.2 and C programming language. I want to know two things:

1. How to get the list of Network interfaces the system has?

2. How to get the IP addresses (IPv4/IPv6) associated with each interface?

any comments on this will be appreciated..
 
Old 11-07-2002, 11:10 PM   #2
leed_25
Member
 
Registered: Jul 2002
Location: san francisco
Distribution: freebsd
Posts: 102

Rep: Reputation: 17
ifconfig -a
 
Old 11-08-2002, 02:23 AM   #3
yrraja
Member
 
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114

Original Poster
Rep: Reputation: 15
Thanx leed_25 but i want some function call which does this within my C program. I dont want to use the system call and parsing.
 
Old 11-08-2002, 03:16 AM   #4
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 46
Well you can read the information directly from the proc filesystem. But the proc filesystem isn't always present and you will have to write a parser for the information which isn't always convenient.

There are other ways to get information using ioctls. You should read the following manpage: 'man 7 netdevice'
 
Old 11-08-2002, 05:02 AM   #5
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 46
Well since I was curious on how it actually worked, I wrote a small program which gets a list of all the interfaces and their ip numbers. Well here is the source code:

Code:
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>

int main()
{
  struct ifconf conf;
  struct sockaddr_in *s_in;
  int sock, i, count;

  // Open dummy socket
  if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
  {
     perror("error opening socket");
     return -1;
  }

  // Get list of devices (only gets first 10)
  memset(&conf, 0, sizeof(conf));
  conf.ifc_len = sizeof(struct ifreq) * 10;
  conf.ifc_buf = (char*) malloc(conf.ifc_len);
        
  if (ioctl(sock, SIOCGIFCONF, &conf) == -1)
  {
     perror("failed to get device list");
     return -1;
  }

  count = conf.ifc_len / sizeof(struct ifreq);

  for (i = 0; i < count; i++)
  {
     s_in = (struct sockaddr_in*) &conf.ifc_req[i].ifr_addr;
     printf("%s %s\n", conf.ifc_req[i].ifr_name,
                       inet_ntoa(s_in->sin_addr));
  }

  free(conf.ifc_buf);

  return 0;
}

Last edited by Mik; 11-08-2002 at 05:17 AM.
 
Old 11-10-2002, 11:46 PM   #6
yrraja
Member
 
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114

Original Poster
Rep: Reputation: 15
Thanx for the code Nik. It was great help.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting Network Interface Information manikchopra Programming 2 08-17-2005 10:48 AM
How to get information about net interface adapters? ukrainet Linux - Newbie 2 12-24-2004 04:52 AM
CDROM interface COR334 SONY CDU33A-C3 Information cgtueno Linux - Hardware 0 08-18-2004 01:58 AM
Bringing up interface eth0: determining ip information for eth0... failed ralphethomas Linux - Networking 0 01-24-2004 05:33 PM
error fetching interface information jw.pi Linux - Networking 2 06-22-2001 06:37 PM


All times are GMT -5. The time now is 09:12 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration