Use GArray for route update gathering.
[trust_router.git] / trp / trp_rtable.c
index 3e4434e..ab693c5 100644 (file)
@@ -1,3 +1,37 @@
+/*
+ * Copyright (c) 2016, JANET(UK)
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * 3. Neither the name of JANET(UK) nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+ * OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
 #include <stdlib.h>
 
 #include <glib.h>
@@ -8,6 +42,8 @@
 #include <trp_internal.h>
 #include <trp_rtable.h>
 #include <tr_debug.h>
+#include <trust_router/trp.h>
+#include <trust_router/tid.h>
 
 /* Note: be careful mixing talloc with glib. */
 
@@ -34,6 +70,8 @@ TRP_ROUTE *trp_route_new(TALLOC_CTX *mem_ctx)
     entry->comm=NULL;
     entry->realm=NULL;
     entry->trust_router=NULL;
+    entry->trp_port=TRP_PORT;
+    entry->tid_port=TID_PORT;
     entry->peer=NULL;
     entry->next_hop=NULL;
     entry->selected=0;
@@ -135,6 +173,7 @@ unsigned int trp_route_get_metric(TRP_ROUTE *entry)
   return entry->metric;
 }
 
+/* TODO: set the hostname and port for the next hop. Currently assume default TID port. --jlr */
 void trp_route_set_next_hop(TRP_ROUTE *entry, TR_NAME *next_hop)
 {
   if (entry->next_hop!=NULL)
@@ -196,6 +235,11 @@ int trp_route_is_local(TRP_ROUTE *entry)
 
 void trp_route_set_triggered(TRP_ROUTE *entry, int trig)
 {
+  tr_debug("trp_route_set_triggered: setting route to %.*s/%.*s through %.*s to %s",
+           entry->comm->len, entry->comm->buf,
+           entry->realm->len, entry->realm->buf,
+           entry->peer->len, entry->peer->buf,
+           trig ? "triggered" : "not triggered");
   entry->triggered=trig;
 }
 
@@ -610,6 +654,7 @@ static char *timespec_to_str(struct timespec *ts)
 TRP_ROUTE *trp_rtable_get_selected_entry(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
 {
   size_t n=0;
+  int ii=0;
   TRP_ROUTE **entry=trp_rtable_get_realm_entries(rtbl, comm, realm, &n);
   TRP_ROUTE *selected=NULL;
 
@@ -618,11 +663,13 @@ TRP_ROUTE *trp_rtable_get_selected_entry(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAM
 
   tr_debug("trp_rtable_get_selected_entry: looking through route table entries for realm %.*s.",
            realm->len, realm->buf);
-  while(n-- && !trp_route_is_selected(entry[n])) { }
-  tr_debug("trp_rtable_get_selected_entry: n=%d.", n);
-
-  if (n>=0)
-    selected=entry[n];
+  for(ii=0; ii<n; ii++) {
+    if (trp_route_is_selected(entry[ii])) {
+      selected=entry[ii];
+      break;
+    }
+  }
+  tr_debug("trp_rtable_get_selected_entry: ii=%d.", ii);
 
   talloc_free(entry);
   return selected;