Linux – Get the network interface type in Linux via C program

clinuxnetworking

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.

Edit

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.

Best Answer

You might try to use RTNETLINK from Netlink(1,2). See Manipulating the Networking Environment Using RTNETLINK for an exemple.

Related Topic