Commit 8f1b75ed authored by pdw's avatar pdw

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

parent 093c15f7
...@@ -87,8 +87,10 @@ static int is_bad_interface_name(char *i) { ...@@ -87,8 +87,10 @@ static int is_bad_interface_name(char *i) {
* interface or one of the interface types listed in bad_interface_names. */ * interface or one of the interface types listed in bad_interface_names. */
static char *get_first_interface(void) { static char *get_first_interface(void) {
struct if_nameindex * nameindex; struct if_nameindex * nameindex;
struct ifreq ifr;
char *i = NULL; char *i = NULL;
int j = 0; int j = 0;
int s;
/* Use if_nameindex(3) instead? */ /* Use if_nameindex(3) instead? */
nameindex = if_nameindex(); nameindex = if_nameindex();
...@@ -96,10 +98,15 @@ static char *get_first_interface(void) { ...@@ -96,10 +98,15 @@ static char *get_first_interface(void) {
return NULL; return NULL;
} }
s = socket(AF_INET, SOCK_DGRAM, 0); /* any sort of IP socket will do */
while(nameindex[j].if_index != 0) { while(nameindex[j].if_index != 0) {
if (strcmp(nameindex[j].if_name, "lo") != 0 && !is_bad_interface_name(nameindex[j].if_name)) { if (strcmp(nameindex[j].if_name, "lo") != 0 && !is_bad_interface_name(nameindex[j].if_name)) {
i = xstrdup(nameindex[j].if_name); strncpy(ifr.ifr_name, nameindex[j].if_name, sizeof(ifr.ifr_name));
break; if ((s == -1) || (ioctl(s, SIOCGIFFLAGS, &ifr) != -1) || (ifr.ifr_flags & IFF_UP)) {
i = xstrdup(nameindex[j].if_name);
break;
}
} }
j++; j++;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment