16

I want to determine if a network card is enabled, up, and plugged in. Basically, I want to know if the network card will work. I need this information from with a C++ program, and would like to display an error message when the network isn't working properly. If possible I would like to avoid using shell commands to determine this information.

Serge Wautier
  • 21,494
  • 13
  • 69
  • 110
Dylan Klomparens
  • 2,853
  • 7
  • 35
  • 52
  • 1
    if you just want to determine if the NIC is "up" use `ioctl()` with `SIOCGIFFLAGS` to directly get the boolean status see http://stackoverflow.com/questions/15723061/how-to-check-if-interface-is-up – Trevor Boyd Smith Jun 23 '15 at 17:35

5 Answers5

16

You can look at /sys/class/net/eth0/operstate where eth0 is your interface to see if it's up.

Look at /sys/class/net/eth0/carrier to see if there is a carrier.

Though I guess executing ifconfig and friends will give you more compatibility to *BSDs.

EFraim
  • 12,811
  • 4
  • 46
  • 62
  • Is there something similarly elegant for older kernels? On my old CentOS 4 box, with kernel 2.6.9, I have no "operstate" file under /sys/class/net/eth?. – Matt Jul 15 '11 at 21:25
  • Do you know how to check the link status? Is there a file for that? As in when you do `ethtool eth0`, output will show you link detected: YES – user1527227 Jul 25 '14 at 20:22
  • @EFraim I've just verified that this is not working correctly on my 2.6.31. It seems that for an access point it will not be up until someone is not connected, while ifconfig shows it's up. That is a very big distinction. I can't easily check this right now with new kernel though. In other words flag shows up, carrier 0, operstate down. – tothphu Oct 18 '16 at 00:45
6
  1. open AF_NETLINK socket
  2. bind it to sockaddr_nl with nl_groups = RTMGRP_LINK
  3. send message RTM_GETLINK to kernel
  4. make poll/epoll on socket to read RTM_NEWLINK and RTM_DELLINK messages
  5. you will receive initial interfaces list and its changes in future
puchu
  • 3,294
  • 6
  • 38
  • 62
4

Remember, on Linux "everything" is a file.

The best way would be to use the approved kernel<->userspace communication, namely sysfs, mounted at /sys. Network devices are linked at /sys/class/net

If you wish to use the ioctl interface, look at man netdevice

Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
1

How do you want to identify the network card? You might try taking a look at /etc/udev/rules.d/70-persistent-net.rules which maps hardware MAC addresses into nice names (like eth0).

Then, when you have the nicer name, you can run things like ethtool eth0 to determine if it is [physically] connected (last line), ifconfig eth0 to determine if it is up (look for "UP BROADCAST..."), and if it has an IP address.

I'm willing to guess there are automatic libraries for this though; have you looked around? I'm not sure if there's easily accessible code in NetworkManager, but that should be a good first place to look.

gatoatigrado
  • 16,580
  • 18
  • 81
  • 143
1

Run through the output of getifaddrs, you can use the link layer for the MAC address to identify an adapter and check the ifa_flags for IFF_UP. Use AF_NETLINK for notifications about interface changes.

Steve-o
  • 12,678
  • 2
  • 41
  • 60