From: Roy Marples Date: Thu, 30 May 2013 07:51:02 +0000 (+0000) Subject: Use LINK_UP/DOWN instead of magic numbers X-Git-Tag: v5.99.7~30 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fefc5353f3a9b3731316352de2b12ce88e88d9e4;p=thirdparty%2Fdhcpcd.git Use LINK_UP/DOWN instead of magic numbers --- diff --git a/dhcpcd.c b/dhcpcd.c index 3996d9a1..11c22e7f 100644 --- a/dhcpcd.c +++ b/dhcpcd.c @@ -294,7 +294,7 @@ configure_interface1(struct interface *ifp) ifo->options &= ~(DHCPCD_ARP | DHCPCD_IPV4LL); if (!(ifp->flags & (IFF_POINTOPOINT | IFF_LOOPBACK | IFF_MULTICAST))) ifo->options &= ~DHCPCD_IPV6RS; - if (ifo->options & DHCPCD_LINK && carrier_status(ifp) == -1) + if (ifo->options & DHCPCD_LINK && carrier_status(ifp) == LINK_UNKNOWN) ifo->options &= ~DHCPCD_LINK; if (ifo->metric != -1) @@ -360,10 +360,9 @@ configure_interface(struct interface *ifp, int argc, char **argv) } void -handle_carrier(int action, int flags, const char *ifname) +handle_carrier(int carrier, int flags, const char *ifname) { struct interface *ifp; - int carrier; if (!(options & DHCPCD_LINK)) return; @@ -375,17 +374,15 @@ handle_carrier(int action, int flags, const char *ifname) if (!(ifp->options->options & DHCPCD_LINK)) return; - if (action) { - carrier = action == 1 ? 1 : 0; + if (carrier == LINK_UNKNOWN) + carrier = carrier_status(ifp); /* will set ifp->flags */ + else ifp->flags = flags; - } else - carrier = carrier_status(ifp); - if (carrier == -1) + if (carrier == LINK_UNKNOWN) syslog(LOG_ERR, "%s: carrier_status: %m", ifname); - else if (carrier == 0 || - (ifp->flags & (IFF_UP | IFF_RUNNING)) != (IFF_UP | IFF_RUNNING)) - { + /* IFF_RUNNING is checked, if needed, earlier and is OS dependant */ + else if (carrier == LINK_DOWN || (ifp->flags & IFF_UP) == 0) { if (ifp->carrier != LINK_DOWN) { ifp->carrier = LINK_DOWN; syslog(LOG_INFO, "%s: carrier lost", ifp->name); @@ -395,9 +392,7 @@ handle_carrier(int action, int flags, const char *ifname) ipv6_free(ifp); dhcp_drop(ifp, "NOCARRIER"); } - } else if (carrier == 1 && - (ifp->flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)) - { + } else if (carrier == LINK_UP && ifp->flags & IFF_UP) { if (ifp->carrier != LINK_UP) { ifp->carrier = LINK_UP; syslog(LOG_INFO, "%s: carrier acquired", ifp->name); @@ -417,7 +412,7 @@ start_interface(void *arg) struct if_options *ifo = ifp->options; int nolease; - handle_carrier(0, 0, ifp->name); + handle_carrier(LINK_UNKNOWN, 0, ifp->name); if (ifp->carrier == LINK_DOWN) { syslog(LOG_INFO, "%s: waiting for carrier", ifp->name); return; @@ -1208,7 +1203,7 @@ main(int argc, char **argv) ts.tv_nsec = 0; nanosleep(&ts, NULL); TAILQ_FOREACH(ifp, ifaces, next) { - handle_carrier(0, 0, ifp->name); + handle_carrier(LINK_UNKNOWN, 0, ifp->name); if (ifp->carrier != LINK_DOWN) { opt = 1; break; diff --git a/if-bsd.c b/if-bsd.c index 7f313423..39a10383 100644 --- a/if-bsd.c +++ b/if-bsd.c @@ -570,10 +570,10 @@ manage_link(int fd) break; switch (ifm->ifm_data.ifi_link_state) { case LINK_STATE_DOWN: - len = -1; + len = LINK_DOWN; break; case LINK_STATE_UP: - len = 1; + len = LINK_UP; break; default: /* handle_carrier will re-load @@ -583,7 +583,7 @@ manage_link(int fd) * set IFF_RUNNING when this routing * message is generated. * As such, it is a race ...*/ - len = 0; + len = LINK_UNKNOWN; break; } handle_carrier(len, ifm->ifm_flags, ifname); diff --git a/if-linux.c b/if-linux.c index 76cfbcbb..449742ab 100644 --- a/if-linux.c +++ b/if-linux.c @@ -443,7 +443,7 @@ link_netlink(struct nlmsghdr *nlm) return 1; } - handle_carrier(ifi->ifi_flags & IFF_RUNNING ? 1 : -1, + handle_carrier(ifi->ifi_flags & IFF_RUNNING ? LINK_UP : LINK_DOWN, ifi->ifi_flags, ifn); return 1; } diff --git a/net.c b/net.c index 9aa5efb6..231482bf 100644 --- a/net.c +++ b/net.c @@ -169,16 +169,16 @@ carrier_status(struct interface *iface) return -1; iface->flags = ifr.ifr_flags; - ret = -1; + ret = LINK_UNKNOWN; #ifdef SIOCGIFMEDIA memset(&ifmr, 0, sizeof(ifmr)); strlcpy(ifmr.ifm_name, iface->name, sizeof(ifmr.ifm_name)); if (ioctl(socket_afnet, SIOCGIFMEDIA, &ifmr) != -1 && ifmr.ifm_status & IFM_AVALID) - ret = (ifmr.ifm_status & IFM_ACTIVE) ? 1 : 0; + ret = (ifmr.ifm_status & IFM_ACTIVE) ? LINK_UP : LINK_DOWN; #endif - if (ret == -1) - ret = (ifr.ifr_flags & IFF_RUNNING) ? 1 : 0; + if (ret == LINK_UNKNOWN) + ret = (ifr.ifr_flags & IFF_RUNNING) ? LINK_UP : LINK_DOWN; return ret; }