Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using C code to get same info as ifconfig

Is there a way in Linux, using C code, to get the same information that "ifconfig eth0" would return? I'm interested in things like IP address, link status, and MAC address.

Here's sample output from ifconfig:

eth0      Link encap:Ethernet  HWaddr 00:0F:20:CF:8B:42           inet addr:217.149.127.10  Bcast:217.149.127.63  Mask:255.255.255.192           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1           RX packets:2472694671 errors:1 dropped:0 overruns:0 frame:0           TX packets:44641779 errors:0 dropped:0 overruns:0 carrier:0           collisions:0 txqueuelen:1000           RX bytes:1761467179 (1679.8 Mb)  TX bytes:2870928587 (2737.9 Mb)           Interrupt:28  
like image 651
Ravi Avatar asked Feb 09 '11 22:02

Ravi


People also ask

What is the replacement for ifconfig?

The ifconfig command has served us well, but now it's time to look to the future with its replacement, the ip command.

What is the difference between ifconfig and ip addr?

However there is one key difference, the “ifconfig” command only shows the interfaces that are currently enabled. Note the missing eth2 in the ifconfig output below. The “ip addr” command shows interfaces that are disabled also. To do the same with ifconfig, use the ifconfig with “-a” switch.

What information can you determine from the ifconfig command?

The “ifconfig” command is used for displaying current network configuration information, setting up an ip address, netmask, or broadcast address to a network interface, creating an alias for the network interface, setting up hardware address, and enable or disable network interfaces.


2 Answers

One way to get to the bottom of problems like this, particularly in cases when you don't have source, is strace.

It gives you a list of all the system calls made by any program you pass it, along with their arguments and return values. If your program just dumps some info and quits rather than running for an extended time it can be pretty straightforward to just do a man on all the system calls you see that look like they might provide the info you're looking for.

When I run

strace ifconfig 

Some of the interesting calls are:

open("/proc/net/dev", O_RDONLY)         = 6 

followed by a bunch of ioctls, corroborating @payne's answer:

ioctl(5, SIOCGIFFLAGS, {ifr_name="eth0",    ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0 ioctl(5, SIOCGIFHWADDR, {ifr_name="eth0", ifr_hwaddr=84:2b:2b:b7:9e:6d}) = 0 ioctl(5, SIOCGIFMETRIC, {ifr_name="eth0", ifr_metric=0}) = 0 ioctl(5, SIOCGIFMTU, {ifr_name="eth0", ifr_mtu=1500}) = 0 
like image 175
Paul Rubel Avatar answered Sep 21 '22 22:09

Paul Rubel


Yes, ifconfig itself is written in C. :) See: http://cvsweb.netbsd.org/bsdweb.cgi/src/sbin/ifconfig/ifconfig.c?rev=1.169&content-type=text/x-cvsweb-markup

Do man netdevice to see the details (on Linux). You use the ioctl() system call.

like image 29
payne Avatar answered Sep 23 '22 22:09

payne