[Babel-users] [PATCH 2/2] Use FOR_ALL_INTERFACES_UP macro

Dave Taht dave.taht at gmail.com
Sun Nov 25 22:47:36 GMT 2018


From: Dave Taht <dave at taht.net>

Checking for whether an interface is up or down is a common
pattern, and easy to forget to do explicitly. This new
macro does it for you while walking the list.
---
 babeld.c    | 36 +++++++++---------------------------
 interface.h |  9 +++++++++
 message.c   | 22 +++++++---------------
 3 files changed, 25 insertions(+), 42 deletions(-)

diff --git a/babeld.c b/babeld.c
index 37f9a76..d28986f 100644
--- a/babeld.c
+++ b/babeld.c
@@ -564,9 +564,7 @@ main(int argc, char **argv)
 
     /* Make some noise so that others notice us, and send retractions in
        case we were restarted recently */
-    FOR_ALL_INTERFACES(ifp) {
-        if(!if_up(ifp))
-            continue;
+    FOR_ALL_INTERFACES_UP(ifp) {
         /* Apply jitter before we send the first message. */
         usleep(roughly(10000));
         gettime(&now);
@@ -574,9 +572,7 @@ main(int argc, char **argv)
         send_wildcard_retraction(ifp);
     }
 
-    FOR_ALL_INTERFACES(ifp) {
-        if(!if_up(ifp))
-            continue;
+    FOR_ALL_INTERFACES_UP(ifp) {
         usleep(roughly(10000));
         gettime(&now);
         send_hello(ifp);
@@ -602,9 +598,7 @@ main(int argc, char **argv)
         timeval_min_sec(&tv, source_expiry_time);
         timeval_min_sec(&tv, kernel_dump_time);
         timeval_min(&tv, &resend_time);
-        FOR_ALL_INTERFACES(ifp) {
-            if(!if_up(ifp))
-                continue;
+        FOR_ALL_INTERFACES_UP(ifp) {
             timeval_min(&tv, &ifp->buf.timeout);
             timeval_min(&tv, &ifp->hello_timeout);
             timeval_min(&tv, &ifp->update_timeout);
@@ -668,9 +662,7 @@ main(int argc, char **argv)
                     sleep(1);
                 }
             } else {
-                FOR_ALL_INTERFACES(ifp) {
-                    if(!if_up(ifp))
-                        continue;
+                FOR_ALL_INTERFACES_UP(ifp) {
                     if(ifp->ifindex == sin6.sin6_scope_id) {
                         parse_packet((unsigned char*)&sin6.sin6_addr, ifp,
                                      receive_buffer, rc);
@@ -758,9 +750,7 @@ main(int argc, char **argv)
             source_expiry_time = now.tv_sec + roughly(300);
         }
 
-        FOR_ALL_INTERFACES(ifp) {
-            if(!if_up(ifp))
-                continue;
+        FOR_ALL_INTERFACES_UP(ifp) {
             if(timeval_compare(&now, &ifp->hello_timeout) >= 0)
                 send_hello(ifp);
             if(timeval_compare(&now, &ifp->update_timeout) >= 0)
@@ -774,9 +764,7 @@ main(int argc, char **argv)
                 do_resend();
         }
 
-        FOR_ALL_INTERFACES(ifp) {
-            if(!if_up(ifp))
-                continue;
+        FOR_ALL_INTERFACES_UP(ifp) {
             if(ifp->buf.timeout.tv_sec != 0) {
                 if(timeval_compare(&now, &ifp->buf.timeout) >= 0) {
                     flushupdates(ifp);
@@ -806,9 +794,7 @@ main(int argc, char **argv)
     /* We need to flush so interface_up won't try to reinstall. */
     flush_all_routes();
 
-    FOR_ALL_INTERFACES(ifp) {
-        if(!if_up(ifp))
-            continue;
+    FOR_ALL_INTERFACES_UP(ifp) {
         send_wildcard_retraction(ifp);
         /* Make sure that we expire quickly from our neighbours'
            association caches. */
@@ -817,9 +803,7 @@ main(int argc, char **argv)
         usleep(roughly(1000));
         gettime(&now);
     }
-    FOR_ALL_INTERFACES(ifp) {
-        if(!if_up(ifp))
-            continue;
+    FOR_ALL_INTERFACES_UP(ifp) {
         /* Make sure they got it. */
         send_wildcard_retraction(ifp);
         send_hello_noihu(ifp, 1);
@@ -880,9 +864,7 @@ main(int argc, char **argv)
     exit(1);
 
  fail:
-    FOR_ALL_INTERFACES(ifp) {
-        if(!if_up(ifp))
-            continue;
+    FOR_ALL_INTERFACES_UP(ifp) {
         interface_up(ifp, 0);
     }
     kernel_setup_socket(0);
diff --git a/interface.h b/interface.h
index 782979a..d265a6c 100644
--- a/interface.h
+++ b/interface.h
@@ -142,6 +142,15 @@ if_up(struct interface *ifp)
     return !!(ifp->flags & IF_UP);
 }
 
+static inline struct interface *
+get_up(struct interface *ifp) {
+    while (ifp && !if_up(ifp))
+	ifp = ifp->next;
+    return ifp;
+}
+
+#define FOR_ALL_INTERFACES_UP(_ifp) for(_ifp = get_up(interfaces); _ifp; _ifp = get_up(_ifp->next))
+
 struct interface *add_interface(char *ifname, struct interface_conf *if_conf);
 int flush_interface(char *ifname);
 unsigned jitter(struct buffered *buf, int urgent);
diff --git a/message.c b/message.c
index 930fc69..aae0cdd 100644
--- a/message.c
+++ b/message.c
@@ -1534,7 +1534,7 @@ send_wildcard_retraction(struct interface *ifp)
 {
     if(ifp == NULL) {
         struct interface *ifp_aux;
-        FOR_ALL_INTERFACES(ifp_aux)
+        FOR_ALL_INTERFACES_UP(ifp_aux)
             send_wildcard_retraction(ifp_aux);
         return;
     }
@@ -1567,9 +1567,7 @@ send_self_update(struct interface *ifp)
     struct xroute_stream *xroutes;
     if(ifp == NULL) {
         struct interface *ifp_aux;
-        FOR_ALL_INTERFACES(ifp_aux) {
-            if(!if_up(ifp_aux))
-                continue;
+        FOR_ALL_INTERFACES_UP(ifp_aux) {
             send_self_update(ifp_aux);
         }
         return;
@@ -1627,9 +1625,8 @@ send_ihu(struct neighbour *neigh, struct interface *ifp)
 
     if(neigh == NULL && ifp == NULL) {
         struct interface *ifp_aux;
-        FOR_ALL_INTERFACES(ifp_aux) {
-            if(if_up(ifp_aux))
-                send_ihu(NULL, ifp_aux);
+        FOR_ALL_INTERFACES_UP(ifp_aux) {
+            send_ihu(NULL, ifp_aux);
         }
         return;
     }
@@ -1745,9 +1742,7 @@ send_multicast_request(struct interface *ifp,
 {
     if(ifp == NULL) {
         struct interface *ifp_auxn;
-        FOR_ALL_INTERFACES(ifp_auxn) {
-            if(!if_up(ifp_auxn))
-                continue;
+        FOR_ALL_INTERFACES_UP(ifp_auxn) {
             send_multicast_request(ifp_auxn, prefix, plen, src_prefix, src_plen);
         }
         return;
@@ -1840,9 +1835,7 @@ send_multicast_multihop_request(struct interface *ifp,
 {
     if(ifp == NULL) {
         struct interface *ifp_aux;
-        FOR_ALL_INTERFACES(ifp_aux) {
-            if(!if_up(ifp_aux))
-                continue;
+        FOR_ALL_INTERFACES_UP(ifp_aux) {
             send_multicast_multihop_request(ifp_aux,
                                             prefix, plen, src_prefix, src_plen,
                                             seqno, id, hop_count);
@@ -1903,8 +1896,7 @@ send_request_resend(const unsigned char *prefix, unsigned char plen,
                       id, neigh->ifp, resend_delay);
     } else {
         struct interface *ifp;
-        FOR_ALL_INTERFACES(ifp) {
-	    if(!if_up(ifp)) continue;
+        FOR_ALL_INTERFACES_UP(ifp) {
             send_multihop_request(&ifp->buf, prefix, plen, src_prefix, src_plen,
                                   seqno, id, 127);
 	}
-- 
2.17.1




More information about the Babel-users mailing list