12

Does anyone know a C/C++ code for finding the network interfaces available?

I've been looking for some code, but most times they are quite complex. Is there a simple way to do this?

jww
  • 97,681
  • 90
  • 411
  • 885
Filipe
  • 3,398
  • 5
  • 21
  • 35
  • 1
    look at the ifconfig source for linux? Or, the opensource versions of ethereal? – KevinDTimm Dec 08 '11 at 17:24
  • Unbutu/Linux. I'm new on this, and I've been looking for some codes, on the internet, but some of then are bit hard to understand. – Filipe Dec 08 '11 at 17:25
  • 2
    to give you an idea of the system calls needed, run `netstat` under `strace` as suggested in [this answer](http://stackoverflow.com/a/2989470/283302). – Sam Miller Dec 08 '11 at 17:30

2 Answers2

21

See the getifaddrs man page. There is an example program towards the end.

Duck
  • 26,924
  • 5
  • 64
  • 92
2

If you're looking for this in context of a desktop application, and you want to be notified of changes (e.g. interfaces connecting/disconnecting), consider using DBus to monitor NetworkManager.

http://projects.gnome.org/NetworkManager/developers/api/09/spec.html

You can enumerate interfaces, as well as interface-specific things (like available and connected WiFi access points, configured-but-not-dialed PPP links, and so forth), and if anything changes, you'll receive a notification over the DBus.

(If this is for something more like a server program, where you expect the network configuration to remain more stable, then things like getifaddrs are possibly more appropriate.)

BRPocock
  • 13,638
  • 3
  • 31
  • 50