[Babel-users] [PATCH 2/2] interface.c: interface_updown() minor refactoring

Christof Schulze christof.schulze at gmx.net
Wed Aug 7 19:24:28 BST 2019


* clarify code flow, only checking for future state once
* clarify error messages when MTU is guessed by including the MTU value
* remove useless comment about the need to run an IP check that already
   is run
---
  interface.c | 22 ++++++++++------------
  1 file changed, 10 insertions(+), 12 deletions(-)

diff --git a/interface.c b/interface.c
index e74bd14..f04be5f 100644
--- a/interface.c
+++ b/interface.c
@@ -43,6 +43,8 @@ THE SOFTWARE.
  #include "local.h"
  #include "xroute.h"

+#define MIN_MTU 512
+
  struct interface *interfaces = NULL;

  static struct interface *
@@ -279,12 +281,8 @@ interface_updown(struct interface *ifp, int up)
      if((!!up) == if_up(ifp))
          return 0;

-    if(up)
-        ifp->flags |= IF_UP;
-    else
-        ifp->flags &= ~IF_UP;
-
      if(up) {
+        ifp->flags |= IF_UP;
          if(ifp->ifindex <= 0) {
              fprintf(stderr,
                      "Upping unknown interface %s.\n", ifp->name);
@@ -306,7 +304,7 @@ interface_updown(struct interface *ifp, int up)

          mtu = kernel_interface_mtu(ifp->name, ifp->ifindex);
          if(mtu < 0) {
-            fprintf(stderr, "Warning: couldn't get MTU of interface %s (%u).\n",
+            fprintf(stderr, "Warning: couldn't get MTU of interface %s (%u), using 1280.\n",
                      ifp->name, ifp->ifindex);
              mtu = 1280;
          }
@@ -315,10 +313,10 @@ interface_updown(struct interface *ifp, int up)
             so MTUs below 116 require lower layer fragmentation. */
          /* In IPv6, the minimum MTU is 1280, and every host must be able
             to reassemble up to 1500 bytes, but I'd rather not rely on this. */
-        if(mtu < 128) {
-            fprintf(stderr, "Suspiciously low MTU %d on interface %s (%u).\n",
-                    mtu, ifp->name, ifp->ifindex);
-            mtu = 128;
+        if(mtu < MIN_MTU) {
+            fprintf(stderr, "Suspiciously low MTU %d on interface %s (%u), using %d.\n",
+                    mtu, ifp->name, ifp->ifindex, MIN_MTU);
+            mtu = MIN_MTU;
          }

          if(ifp->buf.buf)
@@ -326,6 +324,7 @@ interface_updown(struct interface *ifp, int up)

          /* 40 for IPv6 header, 8 for UDP header, 12 for good luck. */
          ifp->buf.size = mtu - sizeof(packet_header) - 60;
+
          ifp->buf.buf = malloc(ifp->buf.size);
          if(ifp->buf.buf == NULL) {
              fprintf(stderr, "Couldn't allocate sendbuf.\n");
@@ -483,6 +482,7 @@ interface_updown(struct interface *ifp, int up)
              send_update(ifp, 0, NULL, 0, NULL, 0);
          send_multicast_request(ifp, NULL, 0, NULL, 0);
      } else {
+        ifp->flags &= ~IF_UP;
          flush_interface_routes(ifp, 0);
          ifp->buf.len = 0;
          ifp->buf.size = 0;
@@ -559,8 +559,6 @@ check_interfaces(void)
          }

          if(if_up(ifp)) {
-            /* Bother, said Pooh.  We should probably check for a change
-               in IPv4 addresses at this point. */
              check_link_local_addresses(ifp);
              check_interface_channel(ifp);
              rc = check_interface_ipv4(ifp);
--
2.20.1




More information about the Babel-users mailing list