Basic peer table, hard coded for testing.
authorJennifer Richards <jennifer@painless-security.com>
Fri, 15 Jul 2016 19:00:53 +0000 (15:00 -0400)
committerJennifer Richards <jennifer@painless-security.com>
Fri, 15 Jul 2016 19:00:53 +0000 (15:00 -0400)
Makefile.am
include/trp_internal.h
include/trp_ptable.h
tr/tr_main.c
trp/trp_ptable.c
trp/trps.c

index 38bc014..89d5976 100644 (file)
@@ -22,6 +22,7 @@ trp_srcs = trp/trp_upd.c \
 trp/trp_req.c \
 trp/trp_conn.c \
 trp/trps.c \
+trp/trp_ptable.c \
 trp/trp_rtable.c
 
 check_PROGRAMS = common/t_constraint
index fb2eaef..4d16124 100644 (file)
@@ -8,6 +8,7 @@
 #include <trust_router/tr_dh.h>
 #include <tr_mq.h>
 #include <tr_msg.h>
+#include <trp_ptable.h>
 #include <trp_rtable.h>
 #include <trust_router/trp.h>
 
@@ -91,7 +92,8 @@ struct trps_instance {
   void *cookie;
   TRP_CONNECTION *conn; /* connections from peers */
   TRPC_INSTANCE *trpc; /* connections to peers */
-  TR_MQ *mq;
+  TR_MQ *mq; /* incoming message queue */
+  TRP_PTABLE *ptable; /* peer table */
   TRP_RTABLE *rtable; /* route table */
 };
 
@@ -163,5 +165,6 @@ TRP_RENTRY *trps_get_route(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, T
 TRP_RENTRY *trps_get_selected_route(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm);
 TR_NAME *trps_get_next_hop(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm);
 TRP_RC trps_sweep_routes(TRPS_INSTANCE *trps);
+TRP_RC trps_add_peer(TRPS_INSTANCE *trps, TRP_PEER *peer);
 
 #endif /* TRP_INTERNAL_H */
index 774e0c4..762ba02 100644 (file)
@@ -8,7 +8,8 @@
 
 typedef struct trp_peer TRP_PEER;
 struct trp_peer {
-  TR_NAME *gssname;
+  char *server;
+  unsigned int port;
   unsigned int linkcost;
   TRP_PEER *next; /* for making a linked list */
 };
@@ -21,8 +22,17 @@ TRP_PTABLE *trp_ptable_new(TALLOC_CTX *memctx);
 void trp_ptable_free(TRP_PTABLE *ptbl);
 TRP_RC trp_ptable_add(TRP_PTABLE *ptbl, TRP_PEER *newpeer);
 TRP_RC trp_ptable_remove(TRP_PTABLE *ptbl, TRP_PEER *peer);
+char *trp_ptable_to_str(TALLOC_CTX *memctx, TRP_PTABLE *ptbl, const char *sep, const char *lineterm);
 
 TRP_PEER *trp_peer_new(TALLOC_CTX *memctx);
 void trp_peer_free(TRP_PEER *peer);
+char *trp_peer_get_server(TRP_PEER *peer);
+void trp_peer_set_server(TRP_PEER *peer, char *server);
+TR_NAME *trp_peer_get_gssname(TRP_PEER *peer);
+unsigned int trp_peer_get_port(TRP_PEER *peer);
+void trp_peer_set_port(TRP_PEER *peer, unsigned int port);
+unsigned int trp_peer_get_linkcost(TRP_PEER *peer);
+void trp_peer_set_linkcost(TRP_PEER *peer, unsigned int linkcost);
+char *trp_peer_to_str(TALLOC_CTX *memctx, TRP_PEER *peer, const char *sep);
 
 #endif /* _TRP_PTABLE_H_ */
index 91a494d..b4669ed 100644 (file)
@@ -50,6 +50,8 @@
 #include <tr_debug.h>
 
 #define TALLOC_DEBUG_ENABLE 1
+#define DEBUG_HARDCODED_PEER_TABLE 1
+#define DEBUG_PING_SELF 0
 
 /***** command-line option handling / setup *****/
 
@@ -273,6 +275,39 @@ int main(int argc, char *argv[])
     return 1;
   }
 
+#if DEBUG_HARDCODED_PEER_TABLE
+  {
+    TRP_PEER *hc_peer=NULL;
+    char *s=NULL;
+
+    hc_peer=trp_peer_new(main_ctx); /* will later be stolen by ptable context */
+    if (hc_peer==NULL) {
+      tr_crit("Unable to allocate new peer. Aborting.");
+      return 1;
+    }
+    trp_peer_set_server(hc_peer, "epsilon.vmnet");
+    switch (tr->trps->port) {
+    case 10000:
+      trp_peer_set_port(hc_peer, 10001);
+      break;
+    case 10001:
+      trp_peer_set_port(hc_peer, 10000);
+      break;
+    default:
+      tr_crit("Cannot use hardcoded peer table with port other than 10000 or 10001.");
+      return 1;
+    }
+    if (TRP_SUCCESS != trps_add_peer(tr->trps, hc_peer)) {
+      tr_crit("Unable to add peer.");
+      return 1;
+    }
+
+    s=trp_ptable_to_str(main_ctx, tr->trps->ptable, NULL, NULL);
+    tr_debug("Peer Table:\n%s\n", s);
+    talloc_free(s);
+  }
+#endif /* DEBUG_HARDCODED_PEER_TABLE */
+
 #if DEBUG_PING_SELF
   /* for debugging, send a message to peers on a timer */
   debug_ping_ev=evtimer_new(ev_base, debug_ping, (void *)&thingy);
index 6ff4549..0056049 100644 (file)
@@ -1,27 +1,18 @@
 #include <talloc.h>
 
-#include <tr_name.h>
+#include <trust_router/tr_name.h>
 #include <trp_internal.h>
 #include <trp_ptable.h>
+#include <tr_debug.h>
 
-static int trp_peer_destructor(void *object)
-{
-  TRP_PEER *peer=talloc_get_type_abort(object, TRP_PEER);
-
-  if (peer->gssname != NULL) {
-    tr_free_name(peer->gssname);
-  }
-  return 0;
-}
-
-TRP_PTABLE *trp_peer_new(TALLOC_CTX *memctx)
+TRP_PEER *trp_peer_new(TALLOC_CTX *memctx)
 {
   TRP_PEER *peer=talloc(memctx, TRP_PEER);
   if (peer!=NULL) {
-    peer->gssname=NULL;
+    peer->server=NULL;
+    peer->port=0;
     peer->linkcost=TRP_METRIC_INFINITY;
     peer->next=NULL;
-    talloc_set_destructor((void *)peer, trp_peer_destructor);
   }
   return peer;
 }
@@ -31,7 +22,7 @@ void trp_peer_free(TRP_PEER *peer)
   talloc_free(peer);
 }
 
-static TRP_PEER trp_peer_tail(TRP_PEER *peer)
+static TRP_PEER *trp_peer_tail(TRP_PEER *peer)
 {
   while (peer->next!=NULL) {
     peer=peer->next;
@@ -39,6 +30,58 @@ static TRP_PEER trp_peer_tail(TRP_PEER *peer)
   return peer;
 }
 
+char *trp_peer_get_server(TRP_PEER *peer)
+{
+  return peer->server;
+}
+
+/* copies input; on error, peer->gssname will be null */
+void trp_peer_set_server(TRP_PEER *peer, char *server)
+{
+  peer->server=talloc_strdup(peer, server); /* will be null on error */
+}
+
+/* get the peer name based on the server name; caller is responsible for freeing the TR_NAME */
+TR_NAME *trp_peer_get_gssname(TRP_PEER *peer)
+{
+  TR_NAME *gssname=NULL;
+  char *s=NULL;
+
+  if (0<asprintf(&s, "trustrouter@%s", peer->server)) {
+    if (tr_new_name(s)!=NULL) {
+      gssname=tr_new_name(s);
+    }
+    free(s);
+  }
+  return gssname;
+}
+
+unsigned int trp_peer_get_port(TRP_PEER *peer)
+{
+  return peer->port;
+}
+
+void trp_peer_set_port(TRP_PEER *peer, unsigned int port)
+{
+  peer->port=port;
+}
+
+unsigned int trp_peer_get_linkcost(TRP_PEER *peer)
+{
+  return peer->linkcost;
+}
+
+void trp_peer_set_linkcost(TRP_PEER *peer, unsigned int linkcost)
+{
+  if ((linkcost>TRP_METRIC_INFINITY) && (linkcost!=TRP_METRIC_INVALID)) {
+    /* This indicates a programming error, but probably means an already infinite metric
+     * was (incorrectly) incremented. Issue a warning and proceed with an infinite metric. */
+    tr_warning("trp_peer_set_linkcost: link cost > infinity encountered, setting to infinity");
+    linkcost=TRP_METRIC_INFINITY;
+  }
+  peer->linkcost=linkcost;
+}
+
 
 TRP_PTABLE *trp_ptable_new(TALLOC_CTX *memctx)
 {
@@ -90,3 +133,32 @@ TRP_RC trp_ptable_remove(TRP_PTABLE *ptbl, TRP_PEER *peer)
   return TRP_ERROR;
 }
 
+char *trp_peer_to_str(TALLOC_CTX *memctx, TRP_PEER *peer, const char *sep)
+{
+  if (sep==NULL)
+    sep=", ";
+  return talloc_asprintf(memctx,
+                         "%s:%u%s0x%04X",
+                         peer->server, peer->port, sep,
+                         peer->linkcost);
+}
+
+/* this is horribly inefficient but should be ok for small peer tables */
+char *trp_ptable_to_str(TALLOC_CTX *memctx, TRP_PTABLE *ptbl, const char *sep, const char *lineterm)
+{
+  TALLOC_CTX *tmpctx=talloc_new(NULL);
+  TRP_PEER *peer=NULL;
+  char *result=talloc_strdup(tmpctx, "");
+
+  if (lineterm==NULL)
+    lineterm="\n";
+
+  /* this leaves intermediate result strings in the tmpctx context, we'll free these when
+   * we're done */
+  for (peer=ptbl->head; peer!=NULL; peer=peer->next)
+    result=talloc_asprintf(tmpctx, "%s%s%s", result, lineterm, trp_peer_to_str(tmpctx, peer, sep));
+
+  talloc_steal(memctx, result); /* hand result over to caller */
+  talloc_free(tmpctx); /* free detritus */
+  return result;
+}
index 5134645..30080bb 100644 (file)
@@ -7,6 +7,7 @@
 #include <tr_rp.h>
 #include <trust_router/tr_name.h>
 #include <trp_internal.h>
+#include <trp_ptable.h>
 #include <trp_rtable.h>
 #include <tr_debug.h>
 
@@ -28,20 +29,29 @@ TRPS_INSTANCE *trps_new (TALLOC_CTX *mem_ctx)
     trps->cookie=NULL;
     trps->conn=NULL;
     trps->trpc=NULL;
+
     trps->mq=tr_mq_new(trps);
     if (trps->mq==NULL) {
       /* failed to allocate mq */
       talloc_free(trps);
-      trps=NULL;
-    } else {
-      trps->rtable=trp_rtable_new();
-      if (trps->rtable==NULL) {
-        /* failed to allocate rtable */
-        talloc_free(trps);
-        trps=NULL;
-      } else
-        talloc_set_destructor((void *)trps, trps_destructor);
+      return NULL;
+    }
+
+    trps->ptable=trp_ptable_new(trps);
+    if (trps->ptable==NULL) {
+      /* failed to allocate ptable */
+      talloc_free(trps);
+      return NULL;
+    }
+
+    trps->rtable=trp_rtable_new();
+    if (trps->rtable==NULL) {
+      /* failed to allocate rtable */
+      talloc_free(trps);
+      return NULL;
     }
+
+    talloc_set_destructor((void *)trps, trps_destructor);
   }
   return trps;
 }
@@ -62,11 +72,15 @@ void trps_mq_append(TRPS_INSTANCE *trps, TR_MQ_MSG *msg)
   tr_mq_append(trps->mq, msg);
 }
 
-/* stand-in for a function that finds the connection for a particular peer */
 #if 0
-static TRP_CONNECTION *trps_find_connection(TRPS_INSTANCE *trps)
+static TRP_CONNECTION *trps_find_conn(TRPS_INSTANCE *trps, TR_NAME *peer_gssname)
 {
-  return trps->conn;
+  TRP_CONNECTION *cur=NULL;
+  for (cur=trps->conn; cur!=NULL; cur=trp_connection_get_next(cur)) {
+    if (0==tr_name_cmp(peer_gssname, trp_connection_get_gssname(cur)))
+      break;
+  }
+  return cur;
 }
 #endif
 
@@ -665,3 +679,8 @@ TRP_RC trps_sweep_routes(TRPS_INSTANCE *trps)
   talloc_free(entry);
   return TRP_SUCCESS;
 }
+
+TRP_RC trps_add_peer(TRPS_INSTANCE *trps, TRP_PEER *peer)
+{
+  return trp_ptable_add(trps->ptable, peer);
+}