[Olsr-dev] [olsrd] [PATCH v1 17/29] gateway: reorder functions

Ferry Huberts (spam-protected)
Tue Jul 24 18:09:06 CEST 2012


From: Ferry Huberts <(spam-protected)>

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 src/gateway.c |  349 +++++++++++++++++++++++++++++----------------------------
 1 file changed, 179 insertions(+), 170 deletions(-)

diff --git a/src/gateway.c b/src/gateway.c
index bf9d1ba..6538891 100644
--- a/src/gateway.c
+++ b/src/gateway.c
@@ -35,6 +35,10 @@ static struct olsr_gw_handler *gw_handler;
 static struct olsr_iptunnel_entry *v4gw_tunnel, *v6gw_tunnel;
 static bool v4gw_choosen_external, v6gw_choosen_external;
 
+/*
+ * Helper Functions
+ */
+
 /**
  * Reconstructs an uplink/downlink speed value from the encoded
  * 1 byte transport value (3 bit mantissa, 5 bit exponent)
@@ -116,6 +120,45 @@ static void refresh_smartgw_netmask(void) {
 }
 
 /**
+ * Triggers an instant gateway selection based on the current data
+ * @param ipv4 trigger a ipv4 gateway lookup
+ * @param ipv6 trigger a ipv6 gateway lookup
+ * @return 0 if successful, -1 otherwise
+ */
+static int
+olsr_trigger_inetgw_selection(bool ipv4, bool ipv6) {
+  gw_handler->select_gateway(ipv4, ipv6);
+  return ((ipv4 && current_ipv4_gw == NULL) || (ipv6 && current_ipv6_gw == NULL)) ? -1 : 0;
+}
+
+/**
+ * @param originator
+ * @return gateway_entry for corresponding router
+ */
+static struct gateway_entry *
+olsr_find_gateway_entry(union olsr_ip_addr *originator) {
+  struct avl_node *node = avl_find(&gateway_tree, originator);
+
+  return node == NULL ? NULL : node2gateway(node);
+}
+
+static void cleanup_gateway_handler(void *ptr) {
+  struct gateway_entry *gw = ptr;
+
+  if (gw->ipv4 || gw->ipv6) {
+    return;
+  }
+
+  /* remove gateway entry */
+  avl_delete(&gateway_tree, &gw->node);
+  olsr_cookie_free(gw_mem_cookie, gw);
+}
+
+/*
+ * Exported Functions
+ */
+
+/**
  * Initialize gateway system
  */
 int
@@ -171,18 +214,6 @@ olsr_trigger_inetgw_startup(void) {
 }
 
 /**
- * Triggers an instant gateway selection based on the current data
- * @param ipv4 trigger a ipv4 gateway lookup
- * @param ipv6 trigger a ipv6 gateway lookup
- * @return 0 if successful, -1 otherwise
- */
-static int
-olsr_trigger_inetgw_selection(bool ipv4, bool ipv6) {
-  gw_handler->select_gateway(ipv4, ipv6);
-  return ((ipv4 && current_ipv4_gw == NULL) || (ipv6 && current_ipv6_gw == NULL)) ? -1 : 0;
-}
-
-/**
  * Triggers a check if the one of the gateways have been lost
  * through ETX = infinity
  */
@@ -205,124 +236,6 @@ void olsr_trigger_gatewayloss_check(void) {
     olsr_trigger_inetgw_selection(ipv4, ipv6);
   }
 }
-/**
- * Set a new gateway handler. Do only call this once during startup from
- * a plugin to overwrite the default handler.
- * @param h pointer to gateway handler struct
- */
-void
-olsr_set_inetgw_handler(struct olsr_gw_handler *h) {
-  gw_handler = h;
-}
-
-/**
- * @param originator
- * @return gateway_entry for corresponding router
- */
-static struct gateway_entry *
-olsr_find_gateway_entry(union olsr_ip_addr *originator) {
-  struct avl_node *node = avl_find(&gateway_tree, originator);
-
-  return node == NULL ? NULL : node2gateway(node);
-}
-
-/**
- * Sets a new internet gateway.
- * An external set command might trigger an internal one if
- * address is not a legal gateway.
- *
- * @param originator ip address of the node with the new gateway
- * @param ipv4 set ipv4 gateway
- * @param ipv6 set ipv6 gateway
- * @param external true if change was triggered directly by an user,
- *   false if triggered by automatic lookup.
- * @return true if an error happened, false otherwise
- */
-bool
-olsr_set_inet_gateway(union olsr_ip_addr *originator, bool ipv4, bool ipv6, bool external) {
-  struct gateway_entry *entry, *oldV4, *oldV6;
-  struct olsr_iptunnel_entry *tunnelV4, *tunnelV6;
-
-  oldV4 = current_ipv4_gw;
-  oldV6 = current_ipv6_gw;
-  tunnelV4 = v4gw_tunnel;
-  tunnelV6 = v6gw_tunnel;
-
-  ipv4 = ipv4 && (olsr_cnf->ip_version == AF_INET || olsr_cnf->use_niit);
-  ipv6 = ipv6 && (olsr_cnf->ip_version == AF_INET6);
-
-  if (ipv4) {
-    current_ipv4_gw = NULL;
-  }
-  if (ipv6) {
-    current_ipv6_gw = NULL;
-  }
-
-  entry = olsr_find_gateway_entry(originator);
-  if (entry != NULL) {
-    if (ipv4 && entry != current_ipv4_gw && entry->ipv4
-        && (!entry->ipv4nat || olsr_cnf->smart_gw_allow_nat)) {
-      /* valid ipv4 gateway */
-      current_ipv4_gw = entry;
-    }
-    if (ipv6 && entry != current_ipv6_gw && entry->ipv6) {
-      /* valid ipv6 gateway */
-      current_ipv6_gw = entry;
-    }
-  }
-
-  /* handle IPv4 */
-  if (oldV4 != current_ipv4_gw) {
-    if ((v4gw_tunnel = olsr_os_add_ipip_tunnel(&current_ipv4_gw->originator, true)) != NULL) {
-      olsr_os_inetgw_tunnel_route(v4gw_tunnel->if_index, true, true);
-      v4gw_choosen_external = external;
-    }
-    else {
-      // TODO: what to do now ? Choose another one ? Fire up a timer ?
-      current_ipv4_gw = NULL;
-    }
-    if (oldV4 != NULL) {
-      olsr_os_del_ipip_tunnel(tunnelV4);
-    }
-  }
-  /* handle IPv6 */
-  if (oldV6 != current_ipv6_gw) {
-    if ((v6gw_tunnel = olsr_os_add_ipip_tunnel(&current_ipv6_gw->originator, false)) != NULL) {
-      olsr_os_inetgw_tunnel_route(v6gw_tunnel->if_index, false, true);
-      v6gw_choosen_external = external;
-    }
-    else {
-      // TODO: what to do now ? Choose another one ? Fire up a timer ?
-      current_ipv6_gw = NULL;
-    }
-    if (oldV6 != NULL) {
-      olsr_os_del_ipip_tunnel(tunnelV6);
-    }
-  }
-  return (ipv4 && current_ipv4_gw == NULL) || (ipv6 && current_ipv6_gw == NULL);
-}
-
-/**
- * returns the gateway_entry of the current ipv4 internet gw.
- * @return pointer to gateway_entry or NULL if not set
- */
-struct gateway_entry *olsr_get_ipv4_inet_gateway(bool *ext) {
-  if (ext) {
-    *ext = v4gw_choosen_external;
-  }
-  return current_ipv4_gw;
-}
-
-/**
- * returns the gateway_entry of the current ipv6 internet gw.
- * @return pointer to gateway_entry or NULL if not set
- */
-struct gateway_entry *olsr_get_ipv6_inet_gateway(bool *ext) {
-  if (ext) {
-    *ext = v6gw_choosen_external;
-  }
-  return current_ipv6_gw;
-}
 
 /**
  * update a gateway_entry based on data received from a HNA
@@ -393,18 +306,6 @@ olsr_update_gateway_entry(union olsr_ip_addr *originator, union olsr_ip_addr *ma
   gw_handler->handle_update_gw(gw);
 }
 
-static void cleanup_gateway_handler(void *ptr) {
-  struct gateway_entry *gw = ptr;
-
-  if (gw->ipv4 || gw->ipv6) {
-    return;
-  }
-
-  /* remove gateway entry */
-  avl_delete(&gateway_tree, &gw->node);
-  olsr_cookie_free(gw_mem_cookie, gw);
-}
-
 /**
  * Delete a gateway based on the originator IP and the prefixlength of a HNA.
  * Should only be called if prefix is a smart_gw prefix or if node is removed
@@ -468,6 +369,142 @@ olsr_delete_gateway_entry(union olsr_ip_addr *originator, uint8_t prefixlen) {
 }
 
 /**
+ * Print debug information about gateway entries
+ */
+void
+olsr_print_gateway_entries(void) {
+#ifndef NODEBUG
+  struct ipaddr_str buf;
+  struct gateway_entry *gw;
+  const int addrsize = olsr_cnf->ip_version == AF_INET ? 15 : 39;
+
+  OLSR_PRINTF(0, "\n--- %s ---------------------------------------------------- GATEWAYS\n\n",
+      olsr_wallclock_string());
+  OLSR_PRINTF(0, "%-*s %-6s %-9s %-9s %s\n", addrsize, "IP address", "Type", "Uplink", "Downlink",
+      olsr_cnf->ip_version == AF_INET ? "" : "External Prefix");
+
+  OLSR_FOR_ALL_GATEWAY_ENTRIES(gw) {
+    OLSR_PRINTF(0, "%-*s %s%c%s%c%c %-9u %-9u %s\n", addrsize, olsr_ip_to_string(&buf, &gw->originator),
+        gw->ipv4nat ? "" : "   ",
+        gw->ipv4 ? '4' : ' ',
+        gw->ipv4nat ? "(N)" : "",
+        (gw->ipv4 && gw->ipv6) ? ',' : ' ',
+        gw->ipv6 ? '6' : ' ',
+        gw->uplink, gw->downlink,
+        gw->external_prefix.prefix_len == 0 ? "" : olsr_ip_prefix_to_string(&gw->external_prefix));
+  } OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)
+#endif
+}
+
+/**
+ * Set a new gateway handler. Do only call this once during startup from
+ * a plugin to overwrite the default handler.
+ * @param h pointer to gateway handler struct
+ */
+void
+olsr_set_inetgw_handler(struct olsr_gw_handler *h) {
+  gw_handler = h;
+}
+
+/**
+ * Sets a new internet gateway.
+ * An external set command might trigger an internal one if
+ * address is not a legal gateway.
+ *
+ * @param originator ip address of the node with the new gateway
+ * @param ipv4 set ipv4 gateway
+ * @param ipv6 set ipv6 gateway
+ * @param external true if change was triggered directly by an user,
+ *   false if triggered by automatic lookup.
+ * @return true if an error happened, false otherwise
+ */
+bool
+olsr_set_inet_gateway(union olsr_ip_addr *originator, bool ipv4, bool ipv6, bool external) {
+  struct gateway_entry *entry, *oldV4, *oldV6;
+  struct olsr_iptunnel_entry *tunnelV4, *tunnelV6;
+
+  oldV4 = current_ipv4_gw;
+  oldV6 = current_ipv6_gw;
+  tunnelV4 = v4gw_tunnel;
+  tunnelV6 = v6gw_tunnel;
+
+  ipv4 = ipv4 && (olsr_cnf->ip_version == AF_INET || olsr_cnf->use_niit);
+  ipv6 = ipv6 && (olsr_cnf->ip_version == AF_INET6);
+
+  if (ipv4) {
+    current_ipv4_gw = NULL;
+  }
+  if (ipv6) {
+    current_ipv6_gw = NULL;
+  }
+
+  entry = olsr_find_gateway_entry(originator);
+  if (entry != NULL) {
+    if (ipv4 && entry != current_ipv4_gw && entry->ipv4
+        && (!entry->ipv4nat || olsr_cnf->smart_gw_allow_nat)) {
+      /* valid ipv4 gateway */
+      current_ipv4_gw = entry;
+    }
+    if (ipv6 && entry != current_ipv6_gw && entry->ipv6) {
+      /* valid ipv6 gateway */
+      current_ipv6_gw = entry;
+    }
+  }
+
+  /* handle IPv4 */
+  if (oldV4 != current_ipv4_gw) {
+    if ((v4gw_tunnel = olsr_os_add_ipip_tunnel(&current_ipv4_gw->originator, true)) != NULL) {
+      olsr_os_inetgw_tunnel_route(v4gw_tunnel->if_index, true, true);
+      v4gw_choosen_external = external;
+    }
+    else {
+      // TODO: what to do now ? Choose another one ? Fire up a timer ?
+      current_ipv4_gw = NULL;
+    }
+    if (oldV4 != NULL) {
+      olsr_os_del_ipip_tunnel(tunnelV4);
+    }
+  }
+  /* handle IPv6 */
+  if (oldV6 != current_ipv6_gw) {
+    if ((v6gw_tunnel = olsr_os_add_ipip_tunnel(&current_ipv6_gw->originator, false)) != NULL) {
+      olsr_os_inetgw_tunnel_route(v6gw_tunnel->if_index, false, true);
+      v6gw_choosen_external = external;
+    }
+    else {
+      // TODO: what to do now ? Choose another one ? Fire up a timer ?
+      current_ipv6_gw = NULL;
+    }
+    if (oldV6 != NULL) {
+      olsr_os_del_ipip_tunnel(tunnelV6);
+    }
+  }
+  return (ipv4 && current_ipv4_gw == NULL) || (ipv6 && current_ipv6_gw == NULL);
+}
+
+/**
+ * returns the gateway_entry of the current ipv4 internet gw.
+ * @return pointer to gateway_entry or NULL if not set
+ */
+struct gateway_entry *olsr_get_ipv4_inet_gateway(bool *ext) {
+  if (ext) {
+    *ext = v4gw_choosen_external;
+  }
+  return current_ipv4_gw;
+}
+
+/**
+ * returns the gateway_entry of the current ipv6 internet gw.
+ * @return pointer to gateway_entry or NULL if not set
+ */
+struct gateway_entry *olsr_get_ipv6_inet_gateway(bool *ext) {
+  if (ext) {
+    *ext = v6gw_choosen_external;
+  }
+  return current_ipv6_gw;
+}
+
+/**
  * Checks if a prefix/netmask combination is a smart gateway
  * @param prefix
  * @param mask
@@ -510,32 +547,4 @@ olsr_modifiy_inetgw_netmask(union olsr_ip_addr *mask, int prefixlen) {
   }
 }
 
-/**
- * Print debug information about gateway entries
- */
-void
-olsr_print_gateway_entries(void) {
-#ifndef NODEBUG
-  struct ipaddr_str buf;
-  struct gateway_entry *gw;
-  const int addrsize = olsr_cnf->ip_version == AF_INET ? 15 : 39;
-
-  OLSR_PRINTF(0, "\n--- %s ---------------------------------------------------- GATEWAYS\n\n",
-      olsr_wallclock_string());
-  OLSR_PRINTF(0, "%-*s %-6s %-9s %-9s %s\n", addrsize, "IP address", "Type", "Uplink", "Downlink",
-      olsr_cnf->ip_version == AF_INET ? "" : "External Prefix");
-
-  OLSR_FOR_ALL_GATEWAY_ENTRIES(gw) {
-    OLSR_PRINTF(0, "%-*s %s%c%s%c%c %-9u %-9u %s\n", addrsize, olsr_ip_to_string(&buf, &gw->originator),
-        gw->ipv4nat ? "" : "   ",
-        gw->ipv4 ? '4' : ' ',
-        gw->ipv4nat ? "(N)" : "",
-        (gw->ipv4 && gw->ipv6) ? ',' : ' ',
-        gw->ipv6 ? '6' : ' ',
-        gw->uplink, gw->downlink,
-        gw->external_prefix.prefix_len == 0 ? "" : olsr_ip_prefix_to_string(&gw->external_prefix));
-  } OLSR_FOR_ALL_GATEWAY_ENTRIES_END(gw)
-#endif
-}
-
 #endif /* !WIN32 */
-- 
1.7.10.4





More information about the Olsr-dev mailing list