Support non-default TRP and TID ports
[trust_router.git] / trp / trp_upd.c
index 1623b84..26b976e 100644 (file)
  *
  */
 
+#include <strings.h>
 #include <jansson.h>
 #include <talloc.h>
 
-#include <trust_router/tr_name.h>
+#include <tr_name_internal.h>
 #include <trp_internal.h>
 #include <tr_comm.h>
 #include <tr_apc.h>
@@ -80,7 +81,7 @@ TRP_INFOREC_TYPE trp_inforec_type_from_string(const char *s)
   struct trp_inforec_type_entry *entry=trp_inforec_type_table;
 
   while ((entry->type != TRP_INFOREC_TYPE_UNKNOWN)
-        && (strcmp(s, entry->name)!=0)) {
+        && (strcasecmp(s, entry->name)!=0)) {
     entry++;
   }
   return entry->type;
@@ -162,6 +163,7 @@ static TRP_INFOREC_DATA *trp_inforec_comm_new(TALLOC_CTX *mem_ctx)
     new_rec->apcs=NULL;
     new_rec->owner_realm=NULL;
     new_rec->owner_contact=NULL;
+    new_rec->expiration_interval=0;
     new_rec->provenance=NULL;
     new_rec->interval=0;
     talloc_set_destructor((void *)new_rec, trp_inforec_comm_destructor);
@@ -209,22 +211,34 @@ void trp_inforec_set_type(TRP_INFOREC *rec, TRP_INFOREC_TYPE type)
 TR_NAME *trp_inforec_get_trust_router(TRP_INFOREC *rec)
 {
   switch (rec->type) {
-  case TRP_INFOREC_TYPE_ROUTE:
-    if (rec->data->route!=NULL)
-      return rec->data->route->trust_router;
-    break;
-  default:
-    break;
+    case TRP_INFOREC_TYPE_ROUTE:
+      if (rec->data->route!=NULL)
+        return rec->data->route->trust_router;
+      break;
+    default:
+      break;
   }
   return NULL;
 }
 
+int trp_inforec_get_trust_router_port(TRP_INFOREC *rec)
+{
+  switch (rec->type) {
+    case TRP_INFOREC_TYPE_ROUTE:
+      if (rec->data->route!=NULL)
+        return rec->data->route->trust_router_port;
+      /* fall through */
+    default:
+      return -1;
+  }
+}
+
 TR_NAME *trp_inforec_dup_trust_router(TRP_INFOREC *rec)
 {
   return tr_dup_name(trp_inforec_get_trust_router(rec));
 }
 
-TRP_RC trp_inforec_set_trust_router(TRP_INFOREC *rec, TR_NAME *trust_router)
+TRP_RC trp_inforec_set_trust_router(TRP_INFOREC *rec, TR_NAME *trust_router, int port)
 {
   switch (rec->type) {
   case TRP_INFOREC_TYPE_ROUTE:
@@ -239,16 +253,15 @@ TRP_RC trp_inforec_set_trust_router(TRP_INFOREC *rec, TR_NAME *trust_router)
   return TRP_ERROR;
 }
 
-/* TODO: need to return hostname/port --jlr */
 TR_NAME *trp_inforec_get_next_hop(TRP_INFOREC *rec)
 {
   switch (rec->type) {
-  case TRP_INFOREC_TYPE_ROUTE:
-    if (rec->data->route!=NULL)
-      return rec->data->route->next_hop;
-    break;
-  default:
-    break;
+    case TRP_INFOREC_TYPE_ROUTE:
+      if (rec->data->route!=NULL)
+        return rec->data->route->next_hop;
+      break;
+    default:
+      break;
   }
   return NULL;
 }
@@ -258,23 +271,48 @@ TR_NAME *trp_inforec_dup_next_hop(TRP_INFOREC *rec)
   return tr_dup_name(trp_inforec_get_next_hop(rec));
 }
 
-TRP_RC trp_inforec_set_next_hop(TRP_INFOREC *rec, TR_NAME *next_hop)
+/**
+ * Set the next hop for the inforec
+ *
+ * Returns TRP_SUCCESS if it set the next hop value for the inforec.
+ * Returns TRP_UNSUPPORTED if the inforec does not have a next hop record but
+ * otherwise nothing went wrong.
+ * Returns TRP_ERROR or another error if there was a failure.
+ *
+ * @param rec
+ * @param next_hop
+ * @return TRP_SUCCESS if the value was set, TRP_UNSUPPORTED if the inforec does not support next hop, or an error code on failure
+ */
+TRP_RC trp_inforec_set_next_hop(TRP_INFOREC *rec, TR_NAME *next_hop, int port)
 {
+  /* Any inforec types that support next_hop should set it here. */
   switch (rec->type) {
   case TRP_INFOREC_TYPE_ROUTE:
     if (rec->data->route==NULL)
       return TRP_ERROR;
-    rec->data->route->next_hop=next_hop;
-    break;
-  case TRP_INFOREC_TYPE_COMMUNITY:
-    /* next hop not used for community records */
+    rec->data->route->next_hop = next_hop;
+    rec->data->route->next_hop_port = port;
     break;
+
   default:
-    break;
+    /* next hop not used for other records */
+    return TRP_UNSUPPORTED;
   }
   return TRP_SUCCESS;
 }
 
+int trp_inforec_get_next_hop_port(TRP_INFOREC *rec)
+{
+  switch (rec->type) {
+    case TRP_INFOREC_TYPE_ROUTE:
+      if (rec->data->route!=NULL)
+        return rec->data->route->next_hop_port;
+      /* fall through */
+    default:
+      return -1;
+  }
+}
+
 unsigned int trp_inforec_get_metric(TRP_INFOREC *rec)
 {
   switch (rec->type) {
@@ -340,6 +378,34 @@ TRP_RC trp_inforec_set_interval(TRP_INFOREC *rec, unsigned int interval)
   return TRP_ERROR;
 }
 
+time_t trp_inforec_get_exp_interval(TRP_INFOREC *rec)
+{
+  switch (rec->type) {
+  case TRP_INFOREC_TYPE_COMMUNITY:
+    if (rec->data->comm!=NULL)
+      return rec->data->comm->expiration_interval;
+    break;
+  default:
+    break;
+  }
+  return 0;
+}
+
+TRP_RC trp_inforec_set_exp_interval(TRP_INFOREC *rec, time_t expint)
+{
+  switch (rec->type) {
+  case TRP_INFOREC_TYPE_COMMUNITY:
+    if (rec->data->comm!=NULL) {
+      rec->data->comm->expiration_interval=expint;
+      return TRP_SUCCESS;
+    }
+    break;
+  default:
+    break;
+  }
+  return TRP_ERROR;
+}
+
 TR_COMM_TYPE trp_inforec_get_comm_type(TRP_INFOREC *rec)
 {
   switch (rec->type) {
@@ -515,27 +581,27 @@ TRP_RC trp_inforec_set_provenance(TRP_INFOREC *rec, json_t *prov)
   return TRP_ERROR;
 }
 
-static TRP_RC trp_inforec_add_to_provenance(TRP_INFOREC *rec, TR_NAME *peer)
+static TRP_RC trp_inforec_add_to_provenance(TRP_INFOREC *rec, TR_NAME *name)
 {
-  json_t *jpeer=NULL;
+  json_t *jname=NULL;
 
   switch (rec->type) {
   case TRP_INFOREC_TYPE_ROUTE:
     /* no provenance list */
     break;
   case TRP_INFOREC_TYPE_COMMUNITY:
-    jpeer=tr_name_to_json_string(peer);
-    if (jpeer==NULL)
+    jname=tr_name_to_json_string(name);
+    if (jname==NULL)
       return TRP_ERROR;
     if (rec->data->comm->provenance==NULL) {
       rec->data->comm->provenance=json_array();
       if (rec->data->comm->provenance==NULL) {
-        json_decref(jpeer);
+        json_decref(jname);
         return TRP_ERROR;
       }
     }
-    if (0!=json_array_append_new(rec->data->comm->provenance, jpeer)) {
-      json_decref(jpeer);
+    if (0!=json_array_append_new(rec->data->comm->provenance, jname)) {
+      json_decref(jname);
       return TRP_ERROR;
     }
     break;
@@ -545,6 +611,24 @@ static TRP_RC trp_inforec_add_to_provenance(TRP_INFOREC *rec, TR_NAME *peer)
   return TRP_SUCCESS;
 }
 
+TR_NAME *trp_inforec_dup_origin(TRP_INFOREC *rec)
+{
+  TR_NAME *origin=NULL;
+  json_t *prov=trp_inforec_get_provenance(rec);
+  const char *s=NULL;
+
+  if (prov==NULL)
+    return NULL;
+
+  s=json_string_value(json_array_get(prov, 0));
+  if (s==NULL) {
+    tr_debug("trp_inforec_dup_origin: empty origin in provenance list.");
+    return NULL;
+  }
+  origin=tr_new_name(s);
+  return origin;
+}
+
 /* generic record type */
 TRP_INFOREC *trp_inforec_new(TALLOC_CTX *mem_ctx, TRP_INFOREC_TYPE type)
 {
@@ -630,6 +714,45 @@ void trp_upd_add_inforec(TRP_UPD *upd, TRP_INFOREC *rec)
   talloc_steal(upd, rec);
 }
 
+/**
+ * Removes and frees the selected inforec.
+ *
+ * @param upd Update to remove from
+ * @param rec Inforec to remove
+ */
+void trp_upd_remove_inforec(TRP_UPD *upd, TRP_INFOREC *rec)
+{
+  TRP_INFOREC *this=upd->records;
+
+  /* special case for the first element */
+  if (this==rec) {
+    upd->records=upd->records->next;
+    trp_inforec_free(this);
+    return;
+  }
+
+  while (this->next!=NULL) {
+    if (this->next==rec) {
+      this->next=this->next->next; /* this->next is not null */
+      trp_inforec_free(rec);
+    }
+    this=this->next;
+  }
+}
+
+size_t trp_upd_num_inforecs(TRP_UPD *upd)
+{
+  size_t count=0;
+  TRP_INFOREC *this=upd->records;
+
+  while (this != NULL) {
+    count++;
+    this=this->next;
+  }
+  return count;
+}
+
+
 TR_NAME *trp_upd_get_realm(TRP_UPD *upd)
 {
   return upd->realm;
@@ -676,30 +799,41 @@ TR_NAME *trp_upd_dup_peer(TRP_UPD *upd)
 
 void trp_upd_set_peer(TRP_UPD *upd, TR_NAME *peer)
 {
+  upd->peer=peer;
+}
+
+void trp_upd_set_next_hop(TRP_UPD *upd, const char *hostname, unsigned int port)
+{
   TRP_INFOREC *rec=NULL;
   TR_NAME *cpy=NULL;
 
-  upd->peer=peer;
-
-  /* add to provenance list of any records that need it */
   for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
-    if (trp_inforec_add_to_provenance(rec, cpy=tr_dup_name(peer)) != TRP_SUCCESS) {
-      tr_err("trp_upd_set_peer: error adding peer to provenance list.");
-      tr_free_name(cpy);
+    switch (trp_inforec_set_next_hop(rec, cpy=tr_new_name(hostname), port)) {
+      case TRP_SUCCESS:
+        /* Success, the TR_NAME in cpy is now stored with the inforec */
+        break;
+
+      case TRP_UNSUPPORTED:
+        /* No error, but the inforec does not accept a next_hop. Free our copy. */
+        tr_free_name(cpy);
+        break;
+
+      default:
+        tr_err("trp_upd_set_next_hop: error setting next hop.");
+        tr_free_name(cpy);
+        break;
     }
   }
 }
 
-void trp_upd_set_next_hop(TRP_UPD *upd, const char *hostname, unsigned int port)
+void trp_upd_add_to_provenance(TRP_UPD *upd, TR_NAME *name)
 {
   TRP_INFOREC *rec=NULL;
-  TR_NAME *cpy=NULL;
-  
+
+  /* add it to all inforecs */
   for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
-    if (trp_inforec_set_next_hop(rec, cpy=tr_new_name(hostname)) != TRP_SUCCESS) {
-      tr_err("trp_upd_set_next_hop: error setting next hop.");
-      tr_free_name(cpy);
-    }
+    if (TRP_SUCCESS!=trp_inforec_add_to_provenance(rec, name))
+      tr_err("trp_upd_set_peer: error adding peer to provenance list.");
   }
 }
 
@@ -707,7 +841,7 @@ void trp_upd_set_next_hop(TRP_UPD *upd, const char *hostname, unsigned int port)
 static void trp_inforec_route_print(TRP_INFOREC_DATA *data)
 {
   if (data->route!=NULL) {
-    printf("     trust_router=%.*s\n     metric=%d\n     interval=%d]\n",
+    tr_info("     trust_router=%.*s\n     metric=%d\n     interval=%d]\n",
            data->route->trust_router->len, data->route->trust_router->buf,
            data->route->metric, data->route->interval);
   }
@@ -716,7 +850,7 @@ static void trp_inforec_route_print(TRP_INFOREC_DATA *data)
 static void trp_inforec_comm_print(TRP_INFOREC_DATA *data)
 {
   if (data->comm!=NULL) {
-    printf("     type=%s\n     role=%s\n     owner=%.*s\n     contact=%.*s]\n",
+    tr_info("     type=%s\n     role=%s\n     owner=%.*s\n     contact=%.*s]\n",
            tr_comm_type_to_str(data->comm->comm_type),
            tr_realm_role_to_str(data->comm->role),
            data->comm->owner_realm->len, data->comm->owner_realm->buf,