X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Fcommon.c;h=ac3e27fab3439bac5bfe889dc948c882607686a7;hb=99a496b3ea09af27e7cdb30f05309d57787c1e1e;hp=a9aa2d9d94b336d72305c211e13f6e530c8d0b3f;hpb=50bac8400624db1ca8dc96e8ae5489c9275f1f4f;p=monky diff --git a/src/common.c b/src/common.c index a9aa2d9..ac3e27f 100644 --- a/src/common.c +++ b/src/common.c @@ -32,6 +32,9 @@ #include #include #include +#include +#include +#include #include #include "diskio.h" @@ -186,6 +189,49 @@ void clear_net_stats(void) memset(netstats, 0, sizeof(netstats)); } +/* We should check if this is ok with OpenBSD and NetBSD as well. */ +int interface_up(const char *dev) +{ + int fd; + struct ifreq ifr; + + if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { + CRIT_ERR("could not create sockfd"); + return 0; + } + strncpy(ifr.ifr_name, dev, IFNAMSIZ); + if (ioctl(fd, SIOCGIFFLAGS, &ifr)) { + /* if device does not exist, treat like not up */ + if (errno != ENODEV && errno != ENXIO) + perror("SIOCGIFFLAGS"); + goto END_FALSE; + } + + if (!(ifr.ifr_flags & IFF_UP)) /* iface is not up */ + goto END_FALSE; + if (ifup_strictness == IFUP_UP) + goto END_TRUE; + + if (!(ifr.ifr_flags & IFF_RUNNING)) + goto END_FALSE; + if (ifup_strictness == IFUP_LINK) + goto END_TRUE; + + if (ioctl(fd, SIOCGIFADDR, &ifr)) { + perror("SIOCGIFADDR"); + goto END_FALSE; + } + if (((struct sockaddr_in *)&(ifr.ifr_ifru.ifru_addr))->sin_addr.s_addr) + goto END_TRUE; + +END_FALSE: + close(fd); + return 0; +END_TRUE: + close(fd); + return 1; +} + void free_dns_data(void) { int i;