diff options
author | pdw <> | 2011-10-04 18:45:54 +0000 |
---|---|---|
committer | pdw <> | 2011-10-04 18:45:54 +0000 |
commit | 8f1b75eda094bad5c39750b3b04429708a55e7fa (patch) | |
tree | 6807c5ca2ce7ab7db8cfdfdd3777b2f921462ec3 | |
parent | 093c15f72f46913f0a5e168dcd4690aeee635d01 (diff) | |
download | iftop-8f1b75eda094bad5c39750b3b04429708a55e7fa.zip |
Attempt to detect if interface is actually up when doing autodetection.
-rw-r--r-- | options.c | 11 |
1 files changed, 9 insertions, 2 deletions
@@ -87,8 +87,10 @@ static int is_bad_interface_name(char *i) { * interface or one of the interface types listed in bad_interface_names. */ static char *get_first_interface(void) { struct if_nameindex * nameindex; + struct ifreq ifr; char *i = NULL; int j = 0; + int s; /* Use if_nameindex(3) instead? */ nameindex = if_nameindex(); @@ -96,10 +98,15 @@ static char *get_first_interface(void) { return NULL; } + s = socket(AF_INET, SOCK_DGRAM, 0); /* any sort of IP socket will do */ + while(nameindex[j].if_index != 0) { if (strcmp(nameindex[j].if_name, "lo") != 0 && !is_bad_interface_name(nameindex[j].if_name)) { - i = xstrdup(nameindex[j].if_name); - break; + strncpy(ifr.ifr_name, nameindex[j].if_name, sizeof(ifr.ifr_name)); + if ((s == -1) || (ioctl(s, SIOCGIFFLAGS, &ifr) != -1) || (ifr.ifr_flags & IFF_UP)) { + i = xstrdup(nameindex[j].if_name); + break; + } } j++; } |