]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Use LINK_UP/DOWN instead of magic numbers
authorRoy Marples <roy@marples.name>
Thu, 30 May 2013 07:51:02 +0000 (07:51 +0000)
committerRoy Marples <roy@marples.name>
Thu, 30 May 2013 07:51:02 +0000 (07:51 +0000)
dhcpcd.c
if-bsd.c
if-linux.c
net.c

index 3996d9a15756933df3356faa9a147887be8f3b5a..11c22e7f280a6d2ea9b1d20a43e8344a5291161f 100644 (file)
--- 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;
index 7f313423e7c0bef4e7932d5f0f4cf58d5138b9a7..39a1038356b0572e7e6f82ec1aebd32bf55b5c1c 100644 (file)
--- 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);
index 76cfbcbb4f7fc13feecb4688c2af24574fa7e215..449742abbaaa4afcc75ef2d70774e037f101c4bd 100644 (file)
@@ -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 9aa5efb6e2a1d594023a5fe8002ee214b9463276..231482bf9e4c6558613e36b9606f47c457f6910d 100644 (file)
--- 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;
 }