Attempt to detect if interface is actually up when doing autodetection.

This commit is contained in:
pdw
2011-10-04 18:45:54 +00:00
parent 093c15f72f
commit 8f1b75eda0

View File

@@ -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,11 +98,16 @@ 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)) {
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++;
}
if_freenameindex(nameindex);