Move community/realm out of inforec and into update message.
[trust_router.git] / trp / trps.c
index 1997c54..54a43a7 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 <fcntl.h>
 #include <talloc.h>
 #include <errno.h>
@@ -8,6 +42,7 @@
 #include <tr_rp.h>
 #include <trust_router/tr_name.h>
 #include <trp_internal.h>
+#include <tr_gss.h>
 #include <trp_ptable.h>
 #include <trp_rtable.h>
 #include <tr_debug.h>
@@ -32,6 +67,7 @@ TRPS_INSTANCE *trps_new (TALLOC_CTX *mem_ctx)
     trps->trpc=NULL;
     trps->update_interval=(struct timeval){0,0};
     trps->sweep_interval=(struct timeval){0,0};
+    trps->ptable=NULL;
 
     trps->mq=tr_mq_new(trps);
     if (trps->mq==NULL) {
@@ -40,13 +76,6 @@ TRPS_INSTANCE *trps_new (TALLOC_CTX *mem_ctx)
       return NULL;
     }
 
-    trps->ptable=trp_ptable_new(trps);
-    if (trps->ptable==NULL) {
-      /* failed to allocate ptable */
-      talloc_free(trps);
-      return NULL;
-    }
-
     trps->rtable=NULL;
     if (trps_init_rtable(trps) != TRP_SUCCESS) {
       /* failed to allocate rtable */
@@ -128,15 +157,35 @@ void trps_set_sweep_interval(TRPS_INSTANCE *trps, unsigned int interval)
   trps->sweep_interval.tv_usec=0;
 }
 
+void trps_set_ptable(TRPS_INSTANCE *trps, TRP_PTABLE *ptable)
+{
+  if (trps->ptable!=NULL)
+    trp_ptable_free(trps->ptable);
+  trps->ptable=ptable;
+}
+
+void trps_set_peer_status_callback(TRPS_INSTANCE *trps, void (*cb)(TRP_PEER *, void *), void *cookie)
+{
+  TRP_PTABLE_ITER *iter=NULL;
+  TRP_PEER *peer=NULL;
+  if (trps->ptable==NULL)
+    return;
+
+  iter=trp_ptable_iter_new(NULL);
+  for (peer=trp_ptable_iter_first(iter, trps->ptable); peer!=NULL; peer=trp_ptable_iter_next(iter))
+    trp_peer_set_conn_status_cb(peer, cb, cookie);
+  trp_ptable_iter_free(iter);
+}
+
 TRPC_INSTANCE *trps_find_trpc(TRPS_INSTANCE *trps, TRP_PEER *peer)
 {
   TRPC_INSTANCE *cur=NULL;
   TR_NAME *name=NULL;
-  TR_NAME *peer_gssname=trp_peer_get_gssname(peer);
+  TR_NAME *peer_servicename=trp_peer_get_servicename(peer);
 
   for (cur=trps->trpc; cur!=NULL; cur=trpc_get_next(cur)) {
     name=trpc_get_gssname(cur);
-    if ((name!=NULL) && (0==tr_name_cmp(peer_gssname, name))) {
+    if ((name!=NULL) && (0==tr_name_cmp(peer_servicename, name))) {
       break;
     }
   }
@@ -242,6 +291,7 @@ TRP_ROUTE *trps_get_route(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR
 
 TRP_ROUTE *trps_get_selected_route(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm)
 {
+  tr_debug("trps_get_selected_route: entered. trps=%p, comm=%p, realm=%p", trps, comm, realm);
   return trp_rtable_get_selected_entry(trps->rtable, comm, realm);
 }
 
@@ -274,7 +324,8 @@ static TRP_RC trps_read_message(TRPS_INSTANCE *trps, TRP_CONNECTION *conn, TR_MS
   int err=0;
   char *buf=NULL;
   size_t buflen = 0;
-  TR_NAME *peer=NULL;
+  TRP_PEER *peer=NULL; /* entry in the peer table */
+  TR_NAME *conn_peer=NULL; /* name from the TRP_CONN, which comes from the gss context */
 
   tr_debug("trps_read_message: started");
   if (err = gsscon_read_encrypted_token(trp_connection_get_fd(conn),
@@ -287,27 +338,39 @@ static TRP_RC trps_read_message(TRPS_INSTANCE *trps, TRP_CONNECTION *conn, TR_MS
     return TRP_ERROR;
   }
 
-  tr_debug("trps_read_message(): message received, %u bytes.", (unsigned) buflen);
-  tr_debug("trps_read_message(): %.*s", buflen, buf);
+  tr_debug("trps_read_message: message received, %u bytes.", (unsigned) buflen);
+  tr_debug("trps_read_message: %.*s", buflen, buf);
 
   *msg=tr_msg_decode(buf, buflen);
   free(buf);
   if (*msg==NULL)
     return TRP_NOPARSE;
 
-  peer=trp_connection_get_peer(conn);
+  conn_peer=trp_connection_get_peer(conn);
+  if (conn_peer==NULL) {
+    tr_err("trps_read_message: connection has no peer name");
+    return TRP_ERROR;
+  }
+
+  peer=trps_get_peer_by_gssname(trps, conn_peer);
+  if (peer==NULL) {
+    tr_err("trps_read_message: could not find peer with gssname=%s", trp_connection_get_gssname(conn));
+    return TRP_ERROR;
+  }
+
   /* verify we received a message we support, otherwise drop it now */
   switch (tr_msg_get_msg_type(*msg)) {
   case TRP_UPDATE:
-    trp_upd_set_peer(tr_msg_get_trp_upd(*msg), tr_dup_name(peer));
+    trp_upd_set_peer(tr_msg_get_trp_upd(*msg), tr_dup_name(conn_peer));
+    trp_upd_set_next_hop(tr_msg_get_trp_upd(*msg), trp_peer_get_server(peer), 0); /* TODO: 0 should be the configured TID port */
     break;
 
   case TRP_REQUEST:
-    trp_req_set_peer(tr_msg_get_trp_req(*msg), tr_dup_name(peer));
+    trp_req_set_peer(tr_msg_get_trp_req(*msg), tr_dup_name(conn_peer));
     break;
 
   default:
-    tr_debug("trps_read_message: received unsupported message from %.*s", peer->len, peer->buf);
+    tr_debug("trps_read_message: received unsupported message from %.*s", conn_peer->len, conn_peer->buf);
     tr_msg_free_decoded(*msg);
     *msg=NULL;
     return TRP_UNSUPPORTED;
@@ -395,6 +458,7 @@ void trps_handle_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
   tr_debug("trps_handle_connection: connection closed.");
 }
 
+/* TODO: check realm/comm, now part of the update instead of inforec */
 static TRP_RC trps_validate_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
 {
   if (upd==NULL) {
@@ -402,6 +466,16 @@ static TRP_RC trps_validate_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
     return TRP_BADARG;
   }
 
+  if (trp_upd_get_realm(upd)==NULL) {
+    tr_notice("trps_validate_update: received TRP update without realm.");
+    return TRP_ERROR;
+  }
+
+  if (trp_upd_get_comm(upd)==NULL) {
+    tr_notice("trps_validate_update: received TRP update without community.");
+    return TRP_ERROR;
+  }
+
   if (trp_upd_get_inforec(upd)==NULL) {
     tr_notice("trps_validate_update: received TRP update with no info records.");
     return TRP_ERROR;
@@ -411,6 +485,7 @@ static TRP_RC trps_validate_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
     tr_notice("trps_validate_update: received TRP update without origin peer information.");
     return TRP_ERROR;
   }
+
   
   return TRP_SUCCESS;
 }
@@ -420,9 +495,7 @@ static TRP_RC trps_validate_inforec(TRPS_INSTANCE *trps, TRP_INFOREC *rec)
 {
   switch(trp_inforec_get_type(rec)) {
   case TRP_INFOREC_TYPE_ROUTE:
-    if ((trp_inforec_get_comm(rec)==NULL)
-       || (trp_inforec_get_realm(rec)==NULL)
-       || (trp_inforec_get_trust_router(rec)==NULL)
+    if ((trp_inforec_get_trust_router(rec)==NULL)
        || (trp_inforec_get_next_hop(rec)==NULL)) {
       tr_debug("trps_validate_inforec: missing record info.");
       return TRP_ERROR;
@@ -463,7 +536,7 @@ static unsigned int trps_advertised_metric(TRPS_INSTANCE *trps, TR_NAME *comm, T
   return trp_route_get_metric(entry) + trps_cost(trps, peer);
 }
 
-static int trps_check_feasibility(TRPS_INSTANCE *trps, TRP_INFOREC *rec)
+static int trps_check_feasibility(TRPS_INSTANCE *trps, TR_NAME *realm, TR_NAME *comm, TRP_INFOREC *rec)
 {
   unsigned int rec_metric=trp_inforec_get_metric(rec);
   unsigned int new_metric=0;
@@ -479,9 +552,7 @@ static int trps_check_feasibility(TRPS_INSTANCE *trps, TRP_INFOREC *rec)
     return 1;
 
   /* updates from our current next hop are always feasible*/
-  next_hop=trps_get_next_hop(trps,
-                             trp_inforec_get_comm(rec),
-                             trp_inforec_get_realm(rec));;
+  next_hop=trps_get_next_hop(trps, comm, realm);
   if ((next_hop!=NULL)
      && (0==tr_name_cmp(next_hop,trp_inforec_get_next_hop(rec)))) {
     return 1;
@@ -490,10 +561,7 @@ static int trps_check_feasibility(TRPS_INSTANCE *trps, TRP_INFOREC *rec)
 
   /* compare the existing metric we advertise to what we would advertise
    * if we accept this update */
-  current_metric=trps_advertised_metric(trps,
-                                        trp_inforec_get_comm(rec),
-                                        trp_inforec_get_realm(rec),
-                                        trp_inforec_get_next_hop(rec));
+  current_metric=trps_advertised_metric(trps, comm, realm, trp_inforec_get_next_hop(rec));
   new_metric=rec_metric + trps_cost(trps, trp_inforec_get_next_hop(rec));
   if (new_metric <= current_metric)
     return 1;
@@ -519,8 +587,8 @@ static TRP_RC trps_accept_update(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC
   TRP_ROUTE *entry=NULL;
 
   entry=trp_rtable_get_entry(trps->rtable,
-                             trp_inforec_get_comm(rec),
-                             trp_inforec_get_realm(rec),
+                             trp_upd_get_comm(upd),
+                             trp_upd_get_realm(upd),
                              trp_inforec_get_next_hop(rec));
   if (entry==NULL) {
     entry=trp_route_new(NULL);
@@ -529,11 +597,12 @@ static TRP_RC trps_accept_update(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC
       return TRP_NOMEM;
     }
 
-    trp_route_set_comm(entry, trp_inforec_dup_comm(rec));
-    trp_route_set_realm(entry, trp_inforec_dup_realm(rec));
+    trp_route_set_comm(entry, trp_upd_dup_comm(upd));
+    trp_route_set_realm(entry, trp_upd_dup_realm(upd));
     trp_route_set_peer(entry, trp_upd_dup_peer(upd));
     trp_route_set_trust_router(entry, trp_inforec_dup_trust_router(rec));
     trp_route_set_next_hop(entry, trp_inforec_dup_next_hop(rec));
+    /* TODO: pass next hop port (now defaults to TID_PORT) --jlr */
     if ((trp_route_get_comm(entry)==NULL)
        ||(trp_route_get_realm(entry)==NULL)
        ||(trp_route_get_peer(entry)==NULL)
@@ -593,13 +662,13 @@ static TRP_RC trps_handle_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
 
   for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
     /* determine feasibility */
-    feas=trps_check_feasibility(trps, rec);
+    feas=trps_check_feasibility(trps, trp_upd_get_realm(upd), trp_upd_get_comm(upd), rec);
     tr_debug("trps_handle_update: record feasibility=%d", feas);
 
     /* do we have an existing route? */
     route=trps_get_route(trps,
-                         trp_inforec_get_comm(rec),
-                         trp_inforec_get_realm(rec),
+                         trp_upd_get_comm(upd),
+                         trp_upd_get_realm(upd),
                          trp_upd_get_peer(upd));
     if (route!=NULL) {
       /* there was a route table entry already */
@@ -881,9 +950,7 @@ static TRP_INFOREC *trps_route_to_inforec(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *tr
 
     /* Note that we leave the next hop empty since the recipient fills that in.
      * This is where we add the link cost (currently always 1) to the next peer. */
-    if ((trp_inforec_set_comm(rec, trp_route_dup_comm(route)) != TRP_SUCCESS)
-       ||(trp_inforec_set_realm(rec, trp_route_dup_realm(route)) != TRP_SUCCESS)
-       ||(trp_inforec_set_trust_router(rec, trp_route_dup_trust_router(route)) != TRP_SUCCESS)
+    if ((trp_inforec_set_trust_router(rec, trp_route_dup_trust_router(route)) != TRP_SUCCESS)
        ||(trp_inforec_set_metric(rec,
                                  trps_metric_add(trp_route_get_metric(route),
                                                  linkcost)) != TRP_SUCCESS)
@@ -898,7 +965,7 @@ static TRP_INFOREC *trps_route_to_inforec(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *tr
 
 /* all routes to a single peer, unless comm/realm are specified (both or neither must be NULL) */
 static TRP_RC trps_update_one_peer(TRPS_INSTANCE *trps,
-                                   TR_NAME *peer_gssname,
+                                   TRP_PEER *peer,
                                    TRP_UPDATE_TYPE update_type,
                                    TR_NAME *comm,
                                    TR_NAME *realm)
@@ -911,20 +978,20 @@ static TRP_RC trps_update_one_peer(TRPS_INSTANCE *trps,
   size_t n_updates=0, ii=0;
   char *encoded=NULL;
   TRP_RC rc=TRP_ERROR;
-  TRP_PEER *peer=trps_get_peer_by_gssname(trps, peer_gssname);
+  TR_NAME *peer_label=trp_peer_get_label(peer);
 
   switch (update_type) {
   case TRP_UPDATE_TRIGGERED:
     tr_debug("trps_update_one_peer: preparing triggered route update for %.*s",
-             peer_gssname->len, peer_gssname->buf);
+             peer_label->len, peer_label->buf);
     break;
   case TRP_UPDATE_SCHEDULED:
     tr_debug("trps_update_one_peer: preparing scheduled route update for %.*s",
-             peer_gssname->len, peer_gssname->buf);
+             peer_label->len, peer_label->buf);
     break;
   case TRP_UPDATE_REQUESTED:
     tr_debug("trps_update_one_peer: preparing requested route update for %.*s",
-             peer_gssname->len, peer_gssname->buf);
+             peer_label->len, peer_label->buf);
   }
 
   /* do not fill in peer, recipient does that */
@@ -932,7 +999,7 @@ static TRP_RC trps_update_one_peer(TRPS_INSTANCE *trps,
     /* do all realms */
     update_list=trps_select_updates_for_peer(tmp_ctx,
                                              trps,
-                                             peer_gssname,
+                                             peer_label,
                                              update_type==TRP_UPDATE_TRIGGERED,
                                             &n_updates);
   } else if ((comm!=NULL) && (realm!=NULL)) {
@@ -943,7 +1010,7 @@ static TRP_RC trps_update_one_peer(TRPS_INSTANCE *trps,
       rc=TRP_NOMEM;
       goto cleanup;
     }
-    *update_list=trps_select_realm_update(trps, comm, realm, peer_gssname);
+    *update_list=trps_select_realm_update(trps, comm, realm, peer_label);
     if (*update_list==NULL) {
       /* we have no actual update to send back, MUST send a retraction */
       tr_debug("trps_update_one_peer: community/realm without route requested, sending mandatory retraction.");
@@ -965,6 +1032,7 @@ static TRP_RC trps_update_one_peer(TRPS_INSTANCE *trps,
     tr_debug("trps_update_one_peer: sending %u update records.", (unsigned int)n_updates);
     upd=trp_upd_new(tmp_ctx);
 
+    /* TODO: set realm/comm in update; used to be in the inforec */
     for (ii=0; ii<n_updates; ii++) {
       rec=trps_route_to_inforec(tmp_ctx, trps, update_list[ii]);
       if (rec==NULL) {
@@ -996,7 +1064,10 @@ static TRP_RC trps_update_one_peer(TRPS_INSTANCE *trps,
     encoded=NULL;
     trp_upd_free(upd);
     upd=NULL;
-  }
+  } else if (n_updates==0)
+    tr_debug("trps_update_one_peer: no updates for %.*s", peer_label->len, peer_label->buf);
+
+  rc=TRP_SUCCESS;
 
 cleanup:
   talloc_free(tmp_ctx);
@@ -1011,6 +1082,9 @@ TRP_RC trps_update(TRPS_INSTANCE *trps, TRP_UPDATE_TYPE update_type)
   TRP_PEER *peer=NULL;
   TRP_RC rc=TRP_SUCCESS;
 
+  if (trps->ptable==NULL)
+    return TRP_SUCCESS; /* no peers, nothing to do */
+
   if (iter==NULL) {
     tr_err("trps_update: failed to allocate peer table iterator.");
     talloc_free(tmp_ctx);
@@ -1022,14 +1096,15 @@ TRP_RC trps_update(TRPS_INSTANCE *trps, TRP_UPDATE_TYPE update_type)
        peer=trp_ptable_iter_next(iter))
   {
     if (!trps_peer_connected(trps, peer)) {
-      TR_NAME *peer_gssname=trp_peer_get_gssname(peer);
+      TR_NAME *peer_label=trp_peer_get_label(peer);
       tr_debug("trps_update: no TRP connection to %.*s, skipping.",
-               peer_gssname->len, peer_gssname->buf);
+               peer_label->len, peer_label->buf);
       continue;
     }
-    rc=trps_update_one_peer(trps, trp_peer_get_gssname(peer), update_type, NULL, NULL);
+    rc=trps_update_one_peer(trps, peer, update_type, NULL, NULL);
   }
-  
+
+  tr_debug("trps_update: rc=%u after attempting update.", rc);
   trp_ptable_iter_free(iter);
   trp_rtable_clear_triggered(trps->rtable); /* don't re-send triggered updates */
   talloc_free(tmp_ctx);
@@ -1045,16 +1120,27 @@ TRP_RC trps_add_route(TRPS_INSTANCE *trps, TRP_ROUTE *route)
 /* steals the peer object */
 TRP_RC trps_add_peer(TRPS_INSTANCE *trps, TRP_PEER *peer)
 {
+  if (trps->ptable==NULL) {
+    trps->ptable=trp_ptable_new(trps);
+    if (trps->ptable==NULL)
+      return TRP_NOMEM;
+  }
   return trp_ptable_add(trps->ptable, peer);
 }
 
 TRP_PEER *trps_get_peer_by_gssname(TRPS_INSTANCE *trps, TR_NAME *gssname)
 {
-  return trp_ptable_find_gssname(trps->ptable, gssname);
+  if (trps->ptable==NULL)
+    return NULL;
+
+  return trp_ptable_find_gss_name(trps->ptable, gssname);
 }
 
 TRP_PEER *trps_get_peer_by_servicename(TRPS_INSTANCE *trps, TR_NAME *servicename)
 {
+  if (trps->ptable==NULL)
+    return NULL;
+
   return trp_ptable_find_servicename(trps->ptable, servicename);
 }
 
@@ -1092,7 +1178,11 @@ static TRP_RC trps_handle_request(TRPS_INSTANCE *trps, TRP_REQ *req)
     tr_debug("trps_handle_request: all routes requested.");
     /* leave comm/realm NULL */
   }
-  return trps_update_one_peer(trps, trp_req_get_peer(req), TRP_UPDATE_REQUESTED, comm, realm);
+  return trps_update_one_peer(trps,
+                              trps_get_peer_by_gssname(trps, trp_req_get_peer(req)),
+                              TRP_UPDATE_REQUESTED,
+                              comm,
+                              realm);
 }