Do not allocate return array if there are no return values
[trust_router.git] / trp / trp_rtable.c
index 64396bf..9b915a2 100644 (file)
+/*
+ * 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>
 #include <talloc.h>
+#include <time.h>
 
-#include <trust_router/tr_name.h>
+#include <tr_name_internal.h>
+#include <trp_route.h>
 #include <trp_internal.h>
 #include <trp_rtable.h>
 #include <tr_debug.h>
-
-/* Note: be careful mixing talloc with glib. */
-
-static int trp_rentry_destructor(void *obj)
-{
-  TRP_RENTRY *entry=talloc_get_type_abort(obj, TRP_RENTRY);
-  if (entry->apc!=NULL)
-    tr_free_name(entry->apc);
-  if (entry->realm!=NULL)
-    tr_free_name(entry->realm);
-  if (entry->trust_router!=NULL)
-    tr_free_name(entry->trust_router);
-  if (entry->peer!=NULL)
-    tr_free_name(entry->peer);
-  if (entry->next_hop!=NULL)
-    tr_free_name(entry->next_hop);
-  return 0;
-}
-
-TRP_RENTRY *trp_rentry_new(TALLOC_CTX *mem_ctx)
-{
-  TRP_RENTRY *entry=talloc(mem_ctx, TRP_RENTRY);
-  if (entry!=NULL) {
-    entry->apc=NULL;
-    entry->realm=NULL;
-    entry->trust_router=NULL;
-    entry->peer=NULL;
-    entry->next_hop=NULL;
-    entry->selected=0;
-    entry->expiry=talloc(entry, struct timespec);
-    if (entry->expiry==NULL) {
-      talloc_free(entry);
-      return NULL;
-    }
-    talloc_set_destructor((void *)entry, trp_rentry_destructor);
-  }
-  tr_debug("trp_rentry_new: %p", entry);
-  return entry;
-}
-
-void trp_rentry_free(TRP_RENTRY *entry)
-{
-  if (entry!=NULL)
-    talloc_free(entry);
-}
-
-void trp_rentry_set_apc(TRP_RENTRY *entry, TR_NAME *apc)
-{
-  entry->apc=apc;
-}
-
-TR_NAME *trp_rentry_get_apc(TRP_RENTRY *entry)
-{
-  return entry->apc;
-}
-
-void trp_rentry_set_realm(TRP_RENTRY *entry, TR_NAME *realm)
-{
-  entry->realm=realm;
-}
-
-TR_NAME *trp_rentry_get_realm(TRP_RENTRY *entry)
-{
-  return entry->realm;
-}
-
-void trp_rentry_set_trust_router(TRP_RENTRY *entry, TR_NAME *tr)
-{
-  entry->trust_router=tr;
-}
-
-TR_NAME *trp_rentry_get_trust_router(TRP_RENTRY *entry)
-{
-  return entry->trust_router;
-}
-
-void trp_rentry_set_peer(TRP_RENTRY *entry, TR_NAME *peer)
-{
-  entry->peer=peer;
-}
-
-TR_NAME *trp_rentry_get_peer(TRP_RENTRY *entry)
-{
-  return entry->peer;
-}
-
-void trp_rentry_set_metric(TRP_RENTRY *entry, unsigned int metric)
-{
-  entry->metric=metric;
-}
-
-unsigned int trp_rentry_get_metric(TRP_RENTRY *entry)
-{
-  return entry->metric;
-}
-
-void trp_rentry_set_next_hop(TRP_RENTRY *entry, TR_NAME *next_hop)
-{
-  entry->next_hop=next_hop;
-}
-
-TR_NAME *trp_rentry_get_next_hop(TRP_RENTRY *entry)
-{
-  return entry->next_hop;
-}
-
-void trp_rentry_set_selected(TRP_RENTRY *entry, int sel)
-{
-  entry->selected=sel;
-}
-
-int trp_rentry_get_selected(TRP_RENTRY *entry)
-{
-  return entry->selected;
-}
-
-/* copies incoming value, does not assume responsibility for freeing */
-void trp_rentry_set_expiry(TRP_RENTRY *entry, struct timespec *exp)
-{
-  entry->expiry->tv_sec=exp->tv_sec;
-  entry->expiry->tv_nsec=exp->tv_nsec;
-}
-
-struct timespec *trp_rentry_get_expiry(TRP_RENTRY *entry)
-{
-  return entry->expiry;
-}
+#include <trust_router/trp.h>
+#include <trust_router/tid.h>
 
 
 /* result must be freed with g_free */
 static gchar *tr_name_to_g_str(const TR_NAME *n)
 {
   gchar *s=g_strndup(n->buf, n->len);
+  if (s==NULL)
+    tr_debug("tr_name_to_g_str: allocation failure.");
   return s;
 }
 
@@ -164,7 +79,7 @@ static gboolean trp_tr_name_equal(gconstpointer key1, gconstpointer key2)
   return equal;
 }
 
-/* free a value to the top level rtable (a hash of all entries in the apc) */
+/* free a value to the top level rtable (a hash of all entries in the comm) */
 static void trp_rtable_destroy_table(gpointer data)
 {
   g_hash_table_destroy(data);
@@ -172,16 +87,20 @@ static void trp_rtable_destroy_table(gpointer data)
 
 static void trp_rtable_destroy_rentry(gpointer data)
 {
-  trp_rentry_free(data);
+  trp_route_free(data);
+}
+
+static void trp_rtable_destroy_tr_name(gpointer data)
+{
+  tr_free_name(data);
 }
 
 TRP_RTABLE *trp_rtable_new(void)
 {
   GHashTable *new=g_hash_table_new_full(trp_tr_name_hash,
                                         trp_tr_name_equal,
-                                        NULL, /* no need to free the key, it is part of the TRP_RENTRY */
+                                        trp_rtable_destroy_tr_name,
                                         trp_rtable_destroy_table);
-  tr_debug("trp_rtable_new: %p", new);
   return new;
 }
 
@@ -198,124 +117,361 @@ static GHashTable *trp_rtbl_get_or_add_table(GHashTable *tbl, TR_NAME *key, GDes
   if (val_tbl==NULL) {
     val_tbl=g_hash_table_new_full(trp_tr_name_hash,
                                   trp_tr_name_equal,
-                                  NULL, /* no need to free the key */
+                                  trp_rtable_destroy_tr_name,
                                   destroy);
-    tr_debug("tr_rtbl_get_or_add_table: %p", val_tbl, trp_rtable_destroy_table);
-    g_hash_table_insert(tbl, key, val_tbl);
+    g_hash_table_insert(tbl, tr_dup_name(key), val_tbl);
   }
   return val_tbl;
 }
 
-void trp_rtable_add(TRP_RTABLE *rtbl, TRP_RENTRY *entry)
+void trp_rtable_add(TRP_RTABLE *rtbl, TRP_ROUTE *entry)
 {
-  GHashTable *apc_tbl=NULL;
+  GHashTable *comm_tbl=NULL;
   GHashTable *realm_tbl=NULL;
 
-  apc_tbl=trp_rtbl_get_or_add_table(rtbl, entry->apc, trp_rtable_destroy_table);
-  realm_tbl=trp_rtbl_get_or_add_table(apc_tbl, entry->realm, trp_rtable_destroy_rentry);
-  g_hash_table_insert(realm_tbl, entry->peer, entry); /* destroys and replaces a duplicate */
+  comm_tbl=trp_rtbl_get_or_add_table(rtbl, entry->comm, trp_rtable_destroy_table);
+  realm_tbl=trp_rtbl_get_or_add_table(comm_tbl, entry->realm, trp_rtable_destroy_rentry);
+  g_hash_table_insert(realm_tbl, tr_dup_name(entry->peer), entry); /* destroys and replaces a duplicate */
+  /* the route entry should not belong to any context, we will manage it ourselves */
+  talloc_steal(NULL, entry);
 }
 
-void trp_rtable_remove(TRP_RTABLE *rtbl, TRP_RENTRY *entry)
+/* note: the entry pointer passed in is invalid after calling this because the entry is freed */
+void trp_rtable_remove(TRP_RTABLE *rtbl, TRP_ROUTE *entry)
 {
-  GHashTable *apc_tbl=NULL;
+  GHashTable *comm_tbl=NULL;
   GHashTable *realm_tbl=NULL;
 
-  apc_tbl=g_hash_table_lookup(rtbl, entry->apc);
-  if (apc_tbl==NULL)
+  comm_tbl=g_hash_table_lookup(rtbl, entry->comm);
+  if (comm_tbl==NULL)
     return;
-  realm_tbl=g_hash_table_lookup(apc_tbl, entry->realm);
+
+  realm_tbl=g_hash_table_lookup(comm_tbl, entry->realm);
   if (realm_tbl==NULL)
     return;
+
+  /* remove the element */
   g_hash_table_remove(realm_tbl, entry->peer);
+  /* if that was the last entry in the realm, remove the realm table */
+  if (g_hash_table_size(realm_tbl)==0)
+    g_hash_table_remove(comm_tbl, entry->realm);
+  /* if that was the last realm in the comm, remove the comm table */
+  if (g_hash_table_size(comm_tbl)==0)
+    g_hash_table_remove(rtbl, entry->comm);
 }
 
-/* Get all entries in an apc. Returned as a talloc'ed array in the NULL
- * context. Caller should free these. */
-size_t trp_rtable_get_apc(TRP_RTABLE *rtbl, TR_NAME *apc, TRP_RENTRY **ret)
+void trp_rtable_clear(TRP_RTABLE *rtbl)
 {
-  GHashTable *apc_tbl=NULL;
-  size_t len=0; /* length of return array */
-  size_t ii=0;
-  GList *realms=NULL;
-  GList *realm_entries=NULL;
-  GList *p1=NULL, *p2=NULL;
+  g_hash_table_remove_all(rtbl); /* destructors should do all the cleanup */
+}
 
-  apc_tbl=g_hash_table_lookup(rtbl, apc);
-  if (apc_tbl==NULL)
-    return 0;
+/* gets the actual hash table, for internal use only */
+static GHashTable *trp_rtable_get_comm_table(TRP_RTABLE *rtbl, TR_NAME *comm)
+{
+  return g_hash_table_lookup(rtbl, comm);
+}
 
-  realms=g_hash_table_get_values(apc_tbl);
-  /* make two passes: first count the entries, then allocate and populate the output array */
-  for (p1=realms; p1!=NULL; p1=g_list_next(p1))
-    len+=g_hash_table_size(p1->data);
-  if (len==0) {
-    g_list_free(realms);
+/* gets the actual hash table, for internal use only */
+static GHashTable *trp_rtable_get_realm_table(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
+{
+  GHashTable *comm_tbl=trp_rtable_get_comm_table(rtbl, comm);
+  if (comm_tbl==NULL)
+    return NULL;
+  else
+    return g_hash_table_lookup(comm_tbl, realm);
+}
+
+struct table_size_cookie {
+  TRP_RTABLE *rtbl;
+  size_t size;
+};
+static void trp_rtable_size_helper(gpointer key, gpointer value, gpointer user_data)
+{
+  struct table_size_cookie *data=(struct table_size_cookie *)user_data;
+  data->size += trp_rtable_comm_size(data->rtbl, (TR_NAME *)key);
+};
+size_t trp_rtable_size(TRP_RTABLE *rtbl)
+{
+  struct table_size_cookie data={rtbl, 0};
+  g_hash_table_foreach(rtbl, trp_rtable_size_helper, &data);
+  return data.size;
+}
+
+struct table_comm_size_cookie {
+  TR_NAME *comm;
+  TRP_RTABLE *rtbl;
+  size_t size;
+};
+static void table_comm_size_helper(gpointer key, gpointer value, gpointer user_data)
+{
+  struct table_comm_size_cookie *data=(struct table_comm_size_cookie *)user_data;
+  data->size += trp_rtable_realm_size(data->rtbl, data->comm, (TR_NAME *)key);
+}
+size_t trp_rtable_comm_size(TRP_RTABLE *rtbl, TR_NAME *comm)
+{
+  struct table_comm_size_cookie data={comm, rtbl, 0};
+  GHashTable *comm_tbl=trp_rtable_get_comm_table(rtbl, comm);
+  if (comm_tbl==NULL)
+    return 0;;
+  g_hash_table_foreach(comm_tbl, table_comm_size_helper, &data);
+  return data.size;
+}
+
+size_t trp_rtable_realm_size(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
+{
+  GHashTable *realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
+  if (realm_tbl==NULL)
     return 0;
+  else
+    return g_hash_table_size(g_hash_table_lookup(
+                               g_hash_table_lookup(rtbl, comm),
+                               realm));
+}
+
+/* Returns an array of pointers to TRP_ROUTE, length of array in n_out.
+ * Caller must free the array (in the mem_ctx context), but must
+ * not free its contents. */
+TRP_ROUTE **trp_rtable_get_entries(TALLOC_CTX *mem_ctx, TRP_RTABLE *rtbl, size_t *n_out)
+{
+  TRP_ROUTE **ret=NULL;
+  TR_NAME **comm=NULL;
+  size_t n_comm=0;
+  TRP_ROUTE **comm_entries=NULL;
+  size_t n_entries=0;
+  size_t ii_ret=0;
+
+  *n_out=trp_rtable_size(rtbl);
+  if (*n_out==0)
+    return NULL;
+
+  ret=talloc_array(mem_ctx, TRP_ROUTE *, *n_out);
+  if (ret==NULL) {
+    tr_crit("trp_rtable_get_entries: unable to allocate return array.");
+    *n_out=0;
+    return NULL;
   }
 
-  *ret=talloc_array(NULL, TRP_RENTRY, len);
-  if (*ret==NULL) {
-    tr_crit("trp_rtable_get_apc: could not allocate return array.");
-    g_list_free(realms);
-    return 0;
+  ii_ret=0; /* counts output entries */
+  comm=trp_rtable_get_comms(rtbl, &n_comm);
+  while(n_comm--) {
+    comm_entries=trp_rtable_get_comm_entries(rtbl, comm[n_comm], &n_entries);
+    while (n_entries--)
+      ret[ii_ret++]=comm_entries[n_entries];
+    talloc_free(comm_entries);
   }
+  talloc_free(comm);
 
-  ii=0;
-  for (p1=realms; p1!=NULL; p1=g_list_next(p1)) {
-    realm_entries=g_hash_table_get_values(p1->data);
-    for (p2=realm_entries; p2!=NULL; p2=g_list_next(p2)) {
-      memcpy(*ret+ii, p2->data, sizeof(TRP_RENTRY));
-      ii++;
-    }
-    g_list_free(realm_entries);
+  if (ii_ret!=*n_out) {
+    tr_crit("trp_rtable_get_entries: found incorrect number of entries.");
+    talloc_free(ret);
+    *n_out=0;
+    return NULL;
+  }
+  return ret;
+}
+
+/* Returns an array of pointers to TR_NAME, length of array in n_out.
+ * Caller must free the array (in the talloc NULL context). */
+TR_NAME **trp_rtable_get_comms(TRP_RTABLE *rtbl, size_t *n_out)
+{
+  size_t len=g_hash_table_size(rtbl); /* known comms are keys in top level hash table */
+  size_t ii=0;
+  GList *comms=NULL;;
+  GList *p=NULL;
+  TR_NAME **ret=NULL;
+
+  if (len==0) {
+    *n_out=0;
+    return NULL;
+  }
+    
+  ret=talloc_array(NULL, TR_NAME *, len);
+  if (ret==NULL) {
+    tr_crit("trp_rtable_get_comms: unable to allocate return array.");
+    *n_out=0;
+    return NULL;
   }
+  comms=g_hash_table_get_keys(rtbl);
+  for (ii=0,p=comms; p!=NULL; ii++,p=g_list_next(p))
+    ret[ii]=(TR_NAME *)p->data;
 
-  g_list_free(realms);
-  return len;
+  g_list_free(comms);
+
+  *n_out=len;
+  return ret;
 }
 
-/* Get all entries in an apc/realm. Returns as a talloc'ed array in
- * the NULL context via .  Caller must free these. */
-size_t trp_rtable_get_realm(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, TRP_RENTRY **ret)
+/* Returns an array of pointers to TR_NAME, length of array in n_out.
+ * Caller must free the array (in the talloc NULL context). */
+TR_NAME **trp_rtable_get_comm_realms(TRP_RTABLE *rtbl, TR_NAME *comm, size_t *n_out)
 {
-  GHashTable *apc_tbl=NULL;
-  GHashTable *realm_tbl=NULL;
-  size_t len=0;
   size_t ii=0;
+  TRP_RTABLE *comm_tbl=g_hash_table_lookup(rtbl, comm);;
   GList *entries=NULL;
   GList *p=NULL;
+  TR_NAME **ret=NULL;
 
-  apc_tbl=g_hash_table_lookup(rtbl, apc);
-  if (apc_tbl==NULL)
-    return 0;
-  realm_tbl=g_hash_table_lookup(apc_tbl, realm);
-  if (realm_tbl==NULL)
-    return 0;
-  entries=g_hash_table_get_values(realm_tbl);
-  len=g_hash_table_size(realm_tbl);
-  *ret=talloc_array(NULL, TRP_RENTRY, len);
-  if (*ret==NULL) {
-    tr_crit("trp_rtable_get_realm: could not allocate return array.");
-    return 0;
+  if (comm_tbl==NULL) {
+    *n_out=0;
+    return NULL;
   }
+  *n_out=g_hash_table_size(comm_tbl); /* set output length */
+  ret=talloc_array(NULL, TR_NAME *, *n_out);
+  entries=g_hash_table_get_keys(comm_tbl);
   for (ii=0,p=entries; p!=NULL; ii++,p=g_list_next(p))
-    memcpy(*ret+ii, p->data, sizeof(TRP_RENTRY));
+    ret[ii]=(TR_NAME *)p->data;
+
   g_list_free(entries);
-  return len;
+  return ret;
 }
 
-/* Gets a single entry, in the NULL talloc context. Caller must free. */
-TRP_RENTRY *trp_rtable_get_entry(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, TR_NAME *peer)
+/* Get all entries in an comm. Returns an array of pointers in NULL talloc context.
+ * Caller must free this list with talloc_free, but must not free the entries in the
+ * list.. */
+TRP_ROUTE **trp_rtable_get_comm_entries(TRP_RTABLE *rtbl, TR_NAME *comm, size_t *n_out)
 {
-  GHashTable *apc_tbl=NULL;
-  GHashTable *realm_tbl=NULL;
+  size_t ii=0, jj=0;
+  TR_NAME **realm=NULL;
+  size_t n_realms=0;
+  TRP_ROUTE **realm_entries=NULL;
+  size_t n_entries=0;
+  TRP_ROUTE **ret=NULL;
+  size_t ii_ret=0;
+
+  *n_out=trp_rtable_comm_size(rtbl, comm);
+  if (*n_out==0)
+    return NULL;
+
+  ret=talloc_array(NULL, TRP_ROUTE *, *n_out);
+  if (ret==NULL) {
+    tr_crit("trp_rtable_get_comm_entries: could not allocate return array.");
+    *n_out=0;
+    return NULL;
+  }
   
-  apc_tbl=g_hash_table_lookup(rtbl, apc);
-  if (apc_tbl==NULL)
+  ii_ret=0; /* counts entries in the output array */
+  realm=trp_rtable_get_comm_realms(rtbl, comm, &n_realms);
+  for (ii=0; ii<n_realms; ii++) {
+    realm_entries=trp_rtable_get_realm_entries(rtbl, comm, realm[ii], &n_entries);
+    for (jj=0; jj<n_entries; jj++)
+      ret[ii_ret++]=realm_entries[jj];
+    talloc_free(realm_entries);
+  }
+  talloc_free(realm);
+
+  if (ii_ret!=*n_out) {
+    tr_crit("trp_rtable_get_comm_entries: found incorrect number of entries.");
+    talloc_free(ret);
+    *n_out=0;
+    return NULL;
+  }
+
+  return ret;
+}
+
+/* Get all entries in an comm/realm. Returns an array of pointers in NULL talloc context.
+ * Caller must free this list with talloc_free, but must not free the entries in the
+ * list.
+ *
+ * If *n_out is 0, then no memory is allocated and NULL is returned. */
+TRP_ROUTE **trp_rtable_get_realm_entries(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, size_t *n_out)
+{
+  size_t ii=0;
+  TRP_ROUTE **ret=NULL;
+  TR_NAME **peer=NULL;
+
+  tr_debug("trp_rtable_get_realm_entries: entered.");
+  peer=trp_rtable_get_comm_realm_peers(rtbl, comm, realm, n_out);
+  if ((peer == NULL) || (*n_out == 0)) {
+    *n_out = 0; /* May be redundant. That's ok, compilers are smart. */
+    goto cleanup;
+  }
+
+  ret=talloc_array(NULL, TRP_ROUTE *, *n_out);
+  if (ret==NULL) {
+    tr_crit("trp_rtable_get_realm_entries: could not allocate return array.");
+    n_out=0;
+    goto cleanup;
+  }
+  for (ii=0; ii<*n_out; ii++)
+    ret[ii]=trp_rtable_get_entry(rtbl, comm, realm, peer[ii]);
+
+cleanup:
+  if (peer)
+    talloc_free(peer);
+  return ret;
+}
+
+TR_NAME **trp_rtable_get_comm_realm_peers(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, size_t *n_out)
+{
+  TR_NAME **ret=NULL;
+  GHashTable *realm_tbl=NULL;
+  GList *keys=NULL;
+  GList *p=NULL;
+  size_t ii=0;
+
+  *n_out=trp_rtable_realm_size(rtbl, comm, realm);
+  if (*n_out==0)
+    return NULL;
+  realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
+  ret=talloc_array(NULL, TR_NAME *, *n_out);
+  if (ret==NULL) {
+    tr_crit("trp_rtable_get_comm_realm_peers: could not allocate return array.");
+    *n_out=0;
     return NULL;
-  realm_tbl=g_hash_table_lookup(apc_tbl, realm);
+  }
+  keys=g_hash_table_get_keys(realm_tbl);
+  for (ii=0,p=keys; p!=NULL; ii++,p=g_list_next(p))
+    ret[ii]=(TR_NAME *)p->data;
+  g_list_free(keys);
+  return ret;
+}
+
+/* Gets a single entry. Do not free it. */
+TRP_ROUTE *trp_rtable_get_entry(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
+{
+  GHashTable *realm_tbl=NULL;
+
+  realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
   if (realm_tbl==NULL)
     return NULL;
-  return (TRP_RENTRY *)g_hash_table_lookup(realm_tbl, peer);
+
+  return g_hash_table_lookup(realm_tbl, peer); /* does not copy or increment ref count */
+}
+
+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;
+
+  if (n==0)
+    return NULL;
+
+  tr_debug("trp_rtable_get_selected_entry: looking through route table entries for realm %.*s.",
+           realm->len, realm->buf);
+  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;
+}
+
+void trp_rtable_clear_triggered(TRP_RTABLE *rtbl)
+{
+  size_t n_entries=0;
+  TRP_ROUTE **entries= trp_rtable_get_entries(NULL, rtbl, &n_entries);
+  size_t ii=0;
+
+  if (entries!=NULL) {
+    for (ii=0; ii<n_entries; ii++)
+      trp_route_set_triggered(entries[ii], 0);
+    talloc_free(entries);
+  }
 }