jsoninfo: move duplicated 'selectable' code into a function
authorFerry Huberts <ferry.huberts@pelagic.nl>
Thu, 3 Dec 2015 15:33:57 +0000 (16:33 +0100)
committerFerry Huberts <ferry.huberts@pelagic.nl>
Thu, 3 Dec 2015 15:43:34 +0000 (16:43 +0100)
Signed-off-by: Ferry Huberts <ferry.huberts@pelagic.nl>
lib/jsoninfo/src/olsrd_jsoninfo.c

index 0dd741c..3041f22 100644 (file)
 
 struct timeval start_time;
 
+#ifdef __linux__
+static bool isGwSelectable(struct gateway_entry * gw, bool ipv6) {
+  if (!ipv6) {
+    return gw->ipv4 //
+        && ((olsr_cnf->ip_version == AF_INET) //
+            || olsr_cnf->use_niit) //
+        && (olsr_cnf->smart_gw_allow_nat //
+            || !gw->ipv4nat);
+  }
+
+  return gw->ipv6 //
+      && (olsr_cnf->ip_version == AF_INET6);
+}
+#endif /* __linux__ */
+
 void plugin_init(const char *plugin_name) {
   /* Get start time */
   gettimeofday(&start_time, NULL);
@@ -362,20 +377,9 @@ static void ipc_print_gateways_ipvx(struct autobuf *abuf, bool ipv6) {
           abuf_json_mark_array_entry(true, abuf);
           {
             struct tc_entry* tc = olsr_lookup_tc_entry(&gw->originator);
-            bool selectable;
-            if (!ipv6) {
-              selectable = gw->ipv4 //
-                  && ((olsr_cnf->ip_version == AF_INET) //
-                      || olsr_cnf->use_niit) //
-                  && (olsr_cnf->smart_gw_allow_nat //
-                      || !gw->ipv4nat);
-            } else {
-              selectable = gw->ipv6 //
-                  && (olsr_cnf->ip_version == AF_INET6);
-            }
 
             abuf_json_boolean(abuf, "selected", current_gw && (current_gw == gw));
-            abuf_json_boolean(abuf, "selectable", selectable);
+            abuf_json_boolean(abuf, "selectable", isGwSelectable(gw, ipv6));
             abuf_json_ip_address(abuf, "originator", &gw->originator);
             abuf_json_ip_address(abuf, "prefix", &gw->external_prefix.prefix);
             abuf_json_int(abuf, "prefixLen", gw->external_prefix.prefix_len);
@@ -443,20 +447,9 @@ static void sgw_ipvx(struct autobuf *abuf, bool ipv6) {
       abuf_json_mark_array_entry(true, abuf);
       {
         struct tc_entry* tc = olsr_lookup_tc_entry(&gw->originator);
-        bool selectable;
-        if (!ipv6) {
-          selectable = gw->ipv4 //
-              && ((olsr_cnf->ip_version == AF_INET) //
-                  || olsr_cnf->use_niit) //
-              && (olsr_cnf->smart_gw_allow_nat //
-                  || !gw->ipv4nat);
-        } else {
-          selectable = gw->ipv6 //
-              && (olsr_cnf->ip_version == AF_INET6);
-        }
 
         abuf_json_boolean(abuf, "selected", current_gw && (current_gw == gw));
-        abuf_json_boolean(abuf, "selectable", selectable);
+        abuf_json_boolean(abuf, "selectable", isGwSelectable(gw, ipv6));
         abuf_json_ip_address(abuf, "originator", &gw->originator);
         abuf_json_ip_address(abuf, "prefix", &gw->external_prefix.prefix);
         abuf_json_int(abuf, "prefixLen", gw->external_prefix.prefix_len);