2

I want to get a list of the ethernet device on a system using a C program. In my system below, I would like to get a listing of eth0-9, and if possible some associated properties (e.g. MAC addr, max speed supported).

# ip link show | grep " eth"
3: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
4: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000
5: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
6: eth3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
7: eth4: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
8: eth5: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
14: eth6: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
15: eth7: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
16: eth8: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000
17: eth9: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 1000

Searching the net, I found code samples similar to below:

char          buf[1024];
struct ifconf ifc;
int           sck;

/* Get a socket handle. */
sck = socket(AF_INET, SOCK_DGRAM, 0);

/* Query available interfaces. */
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
ioctl(sck, SIOCGIFCONF, &ifc);

However, the above code only retrieves the list of eth devices for which and IP address is assigned. I want to get the full list of eth devices whether an IP addr is assigned to it or not.

I also came across the following function in glibc:

struct if_nameindex *if_nameindex(void);

However, the above function returns only the eth device index and name (as in ethX), which is pretty limited info.

Is there other/better options.

To be clear in my objective, I want to get the list of eth devices from the system, and only pick the eth devices that are 10G ports.

Thank you, Ahmed.

Ahmed A
  • 3,362
  • 7
  • 39
  • 57

1 Answers1

2

You are looking for getifaddrs. At the bottom of the man page is an example program demonstrating how to loop through all the interfaces. You can use if_nameindex,if_nametoindex, if_indextoname, and if_freenameindex in conjuction or associated functions.

Duck
  • 26,924
  • 5
  • 64
  • 92
  • Thank you. getifaddrs() got me partially what I needed. I noticed that for physical device, it returns a pointer to struct rtnl_link_stats which has low level device attr, but no max speed. – Ahmed A May 15 '14 at 21:03