Reputation: 41
How can I get the type name for a network interface from C program in Linux?
I tried using getifaddr
to access network interfaces, and I got everything I needed, except its type name. Any ideas how it could be done?
I'm still new to Linux, so any comments on any workarounds or example codes would be appreciated.
To elaborate the question. Basically, while doing ifconfig
I can get something like this:
eth0 Link encap:Ethernet HWaddr 00:0C:29:40:93:9C inet addr:192.168.154.102 Bcast:192.168.154.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe40:939c/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:14785 errors:0 dropped:0 overruns:0 frame:0 TX packets:429 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:961439 (938.9 KiB) TX bytes:71866 (70.1 KiB) Interrupt:67 Base address:0x2000
Here is what i understood that eth0 is interface name. While there can be several network interfaces (eth0, eth1, eth2) and several virtual interfaces (tap0, tap1, etc) and wireless ones, all of them are Ethernet interfaces.
The same for loopbacks, being lo as interface name, its interface is Local Loopback.
So the question is, how can i, using C program, print that eth0,tap0,wlan0 belong to type of Ethernet, and lo -> to Local Loopback type, when i don`t know beforehand what interfaces are used and how many of them are in the machine.
Hope I could explain this correctly.
Upvotes: 2
Views: 10128
Reputation: 21
Unrelated to the question but, in case someone wants to find it on the shell here one way to find the wifi interfaces:
for iface in ` ifconfig | cut -f 1 -d ' ' | sort | grep -v '^$'`
do
if [ -d /sys/class/net/$iface/wireless ];then echo $iface ; fi
done
Upvotes: 0
Reputation: 364
A simple way would be:
cat /sys/class/net/X/type
(where X is your netdev name, e.g. eth0)
Then compare the number to: http://lxr.linux.no/linux+v3.0/include/linux/if_arp.h#L30
for example:
cat /sys/class/net/eth0/type
output:
1
=> where 1=ARPHRD_ETHER accoridng to if_arp.h header file.
Upvotes: 1
Reputation: 17312
There's no ioctl
that returns the link encapsulation, if you strace
ifconfig or have a look at its source code you will see that it uses a table look-up to find the name of the link encapsulation. This is from net-tools source code, as you can see the names are statically initialized:
//lib/hw.c
void hwinit()
{
loop_hwtype.title = _("Local Loopback");
unspec_hwtype.title = _("UNSPEC");
#if HAVE_HWSLIP
slip_hwtype.title = _("Serial Line IP");
cslip_hwtype.title = _("VJ Serial Line IP");
slip6_hwtype.title = _("6-bit Serial Line IP");
cslip6_hwtype.title = _("VJ 6-bit Serial Line IP");
adaptive_hwtype.title = _("Adaptive Serial Line IP");
#endif
#if HAVE_HWETHER
ether_hwtype.title = _("Ethernet");
#endif
....
Upvotes: 1
Reputation: 5453
You might try to use RTNETLINK from Netlink(1,2). See Manipulating the Networking Environment Using RTNETLINK for an exemple.
Upvotes: 4
Reputation: 4559
Really broad question.
Under linux the OS itself can be considered a network interface, the so called "loopback interfaces" admit this kind of options and they are "network interfaces" even if they are virtual and not a real piece of hardware they look the same as your PCI network card or your USB dongle. if you are looking for the networking hardware is suggest to take a look at lshw
or to spend time studying how the kernel interfaces itself with the networking resources.
Upvotes: 0