diff --git a/include/netutils/ifc.h b/include/netutils/ifc.h index e24526299..650d89d0c 100644 --- a/include/netutils/ifc.h +++ b/include/netutils/ifc.h @@ -36,6 +36,7 @@ extern int ifc_disable(const char *ifname); extern int ifc_reset_connections(const char *ifname); +extern int ifc_get_addr(const char *name, in_addr_t *addr); extern int ifc_set_addr(const char *name, in_addr_t addr); extern int ifc_set_mask(const char *name, in_addr_t mask); extern int ifc_set_hwaddr(const char *name, const void *ptr); diff --git a/libnetutils/ifc_utils.c b/libnetutils/ifc_utils.c index 807649690..6788391ab 100644 --- a/libnetutils/ifc_utils.c +++ b/libnetutils/ifc_utils.c @@ -189,6 +189,23 @@ int ifc_set_mask(const char *name, in_addr_t mask) return ioctl(ifc_ctl_sock, SIOCSIFNETMASK, &ifr); } +int ifc_get_addr(const char *name, in_addr_t *addr) +{ + struct ifreq ifr; + int ret = 0; + + ifc_init_ifr(name, &ifr); + if (addr != NULL) { + ret = ioctl(ifc_ctl_sock, SIOCGIFADDR, &ifr); + if (ret < 0) { + *addr = 0; + } else { + *addr = ((struct sockaddr_in*) &ifr.ifr_addr)->sin_addr.s_addr; + } + } + return ret; +} + int ifc_get_info(const char *name, in_addr_t *addr, in_addr_t *mask, unsigned *flags) { struct ifreq ifr; @@ -312,11 +329,20 @@ int ifc_enable(const char *ifname) int ifc_disable(const char *ifname) { + unsigned addr, count; int result; ifc_init(); result = ifc_down(ifname); + ifc_set_addr(ifname, 0); + for (count=0, addr=1;((addr != 0) && (count < 255)); count++) { + if (ifc_get_addr(ifname, &addr) < 0) + break; + if (addr) + ifc_set_addr(ifname, 0); + } + ifc_close(); return result; }