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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
11-07-2002, 11:55 PM
|
#1
|
Member
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114
Rep:
|
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..
|
|
|
11-08-2002, 12:10 AM
|
#2
|
Member
Registered: Jul 2002
Location: san francisco
Distribution: freebsd
Posts: 102
Rep:
|
ifconfig -a
|
|
|
11-08-2002, 03:23 AM
|
#3
|
Member
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114
Original Poster
Rep:
|
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.
|
|
|
11-08-2002, 04:16 AM
|
#4
|
Senior Member
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316
Rep:
|
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'
|
|
|
11-08-2002, 06:02 AM
|
#5
|
Senior Member
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316
Rep:
|
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 06:17 AM.
|
|
|
11-11-2002, 12:46 AM
|
#6
|
Member
Registered: Sep 2002
Distribution: RH, FC, Ubuntu, Solaris, AIX
Posts: 114
Original Poster
Rep:
|
Thanx for the code Nik. It was great help.
|
|
|
All times are GMT -5. The time now is 10:04 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|