Look up TRPC with service name, not gss name.
[trust_router.git] / trp / trps.c
index ec4ada5..7f58644 100644 (file)
@@ -8,6 +8,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 +33,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 +42,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 */
@@ -90,9 +85,9 @@ TR_MQ_MSG *trps_mq_pop(TRPS_INSTANCE *trps)
   return tr_mq_pop(trps->mq);
 }
 
-void trps_mq_append(TRPS_INSTANCE *trps, TR_MQ_MSG *msg)
+void trps_mq_add(TRPS_INSTANCE *trps, TR_MQ_MSG *msg)
 {
-  tr_mq_append(trps->mq, msg);
+  tr_mq_add(trps->mq, msg);
 }
 
 unsigned int trps_get_connect_interval(TRPS_INSTANCE *trps)
@@ -128,15 +123,22 @@ 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;
+}
+
 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;
     }
   }
@@ -187,16 +189,15 @@ TRP_RC trps_send_msg(TRPS_INSTANCE *trps, TRP_PEER *peer, const char *msg)
 
   /* get the connection for this peer */
   trpc=trps_find_trpc(trps, peer);
-  if ((trpc==NULL) || (trpc_get_status(trps->trpc)!=TRP_CONNECTION_UP)) {
-    /* We could just let these sit on the queue in the hopes that a connection
-     * is eventually established. However, we'd then have to ensure the queue
-     * didn't keep growing, etc. */
-    tr_warning("trps_send_msg: skipping message queued while TRPC connection not up.");
+  /* instead, let's let that happen and then clear the queue when an attempt to
+   * connect fails */
+  if (trpc==NULL) {
+    tr_warning("trps_send_msg: skipping message queued for missing TRP client entry.");
   } else {
-    mq_msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPC_SEND);
+    mq_msg=tr_mq_msg_new(tmp_ctx, TR_MQMSG_TRPC_SEND, TR_MQ_PRIO_NORMAL);
     msg_dup=talloc_strdup(mq_msg, msg); /* get local copy in mq_msg context */
     tr_mq_msg_set_payload(mq_msg, msg_dup, NULL); /* no need for a free() func */
-    trpc_mq_append(trpc, mq_msg);
+    trpc_mq_add(trpc, mq_msg);
     rc=TRP_SUCCESS;
   }
   talloc_free(tmp_ctx);
@@ -235,22 +236,6 @@ static int trps_listen (TRPS_INSTANCE *trps, int port)
   return conn;
 }
 
-#if 0 /* remove this if I forget to do so */
-/* returns EACCES if authorization is denied */
-int trps_auth_cb(gss_name_t clientName, gss_buffer_t displayName, void *data)
-{
-  TRPS_INSTANCE *trps = (TRPS_INSTANCE *)data;
-  int result=0;
-
-  if (0!=trps->auth_handler(clientName, displayName, trps->cookie)) {
-    tr_debug("trps_auth_cb: client '%.*s' denied authorization.", displayName->length, displayName->value);
-    result=EACCES; /* denied */
-  }
-
-  return result;
-}
-#endif 
-
 /* get the currently selected route if available */
 TRP_ROUTE *trps_get_route(TRPS_INSTANCE *trps, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
 {
@@ -304,7 +289,7 @@ static TRP_RC trps_read_message(TRPS_INSTANCE *trps, TRP_CONNECTION *conn, TR_MS
     return TRP_ERROR;
   }
 
-  tr_debug("trps_read_message(): Request Received, %u bytes.", (unsigned) buflen);
+  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);
@@ -375,41 +360,50 @@ int trps_get_listener(TRPS_INSTANCE *trps,
   return listen;
 }
 
+TRP_RC trps_authorize_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
+{
+  /* try to establish a GSS context */
+  if (0!=trp_connection_auth(conn, trps->auth_handler, trps->cookie)) {
+    tr_notice("trps_authorize_connection: failed to authorize connection");
+    trp_connection_close(conn);
+    return TRP_ERROR;
+  }
+  tr_notice("trps_authorize_connection: authorized connection");
+  return TRP_SUCCESS;
+}
+
 void trps_handle_connection(TRPS_INSTANCE *trps, TRP_CONNECTION *conn)
 {
   TR_MSG *msg=NULL;
   TRP_RC rc=TRP_ERROR;
 
-  /* try to establish a GSS context */
-  if (0!=trp_connection_auth(conn, trps->auth_handler, trps->cookie)) {
-    tr_notice("tr_trps_conn_thread: failed to authorize connection");
-    trp_connection_close(conn);
-  } else {
-    tr_notice("trps_handle_connection: authorized connection");
-  
-    /* loop as long as the connection exists */
-    while (trp_connection_get_status(conn)==TRP_CONNECTION_UP) {
-      rc=trps_read_message(trps, conn, &msg);
-      switch(rc) {
-      case TRP_SUCCESS:
-        trps->msg_handler(trps, conn, msg); /* send the TR_MSG off to the callback */
-        break;
-
-      case TRP_ERROR:
-        trp_connection_close(conn);
-        break;
-
-      default:
-        tr_debug("trps_handle_connection: trps_read_message failed (%d)", rc);
-      }
-    }
+  /* loop as long as the connection exists */
+  while (trp_connection_get_status(conn)==TRP_CONNECTION_UP) {
+    rc=trps_read_message(trps, conn, &msg);
+    switch(rc) {
+    case TRP_SUCCESS:
+      trps->msg_handler(trps, conn, msg); /* send the TR_MSG off to the callback */
+      break;
+
+    case TRP_ERROR:
+      trp_connection_close(conn);
+      break;
 
-    tr_debug("trps_handle_connection: connection closed.");
+    default:
+      tr_debug("trps_handle_connection: trps_read_message failed (%d)", rc);
+    }
   }
+
+  tr_debug("trps_handle_connection: connection closed.");
 }
 
 static TRP_RC trps_validate_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
 {
+  if (upd==NULL) {
+    tr_notice("trps_validate_update: null TRP update.");
+    return TRP_BADARG;
+  }
+
   if (trp_upd_get_inforec(upd)==NULL) {
     tr_notice("trps_validate_update: received TRP update with no info records.");
     return TRP_ERROR;
@@ -522,7 +516,7 @@ static struct timespec *trps_compute_expiry(TRPS_INSTANCE *trps, unsigned int in
   return ts;
 }
 
-static TRP_RC trps_accept_update(TRPS_INSTANCE *trps, TRP_INFOREC *rec)
+static TRP_RC trps_accept_update(TRPS_INSTANCE *trps, TRP_UPD *upd, TRP_INFOREC *rec)
 {
   TRP_ROUTE *entry=NULL;
 
@@ -537,12 +531,12 @@ static TRP_RC trps_accept_update(TRPS_INSTANCE *trps, TRP_INFOREC *rec)
       return TRP_NOMEM;
     }
 
-    trp_route_set_apc(entry, tr_dup_name(trp_inforec_get_comm(rec)));
-    trp_route_set_realm(entry, tr_dup_name(trp_inforec_get_realm(rec)));
-    trp_route_set_peer(entry, tr_dup_name(trp_inforec_get_next_hop(rec)));
-    trp_route_set_trust_router(entry, tr_dup_name(trp_inforec_get_trust_router(rec)));
-    trp_route_set_next_hop(entry, tr_dup_name(trp_inforec_get_next_hop(rec)));
-    if ((trp_route_get_apc(entry)==NULL)
+    trp_route_set_comm(entry, trp_inforec_dup_comm(rec));
+    trp_route_set_realm(entry, trp_inforec_dup_realm(rec));
+    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));
+    if ((trp_route_get_comm(entry)==NULL)
        ||(trp_route_get_realm(entry)==NULL)
        ||(trp_route_get_peer(entry)==NULL)
        ||(trp_route_get_trust_router(entry)==NULL)
@@ -580,7 +574,6 @@ static TRP_RC trps_accept_update(TRPS_INSTANCE *trps, TRP_INFOREC *rec)
   return TRP_SUCCESS;
 }
 
-/* TODO: handle community updates */
 static TRP_RC trps_handle_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
 {
   unsigned int feas=0;
@@ -595,10 +588,12 @@ 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)) {
     /* validate/sanity check the record update */
     if (trps_validate_inforec(trps, rec) != TRP_SUCCESS) {
-      tr_notice("trps_handle_update: invalid record in TRP update.");
-      continue;
+      tr_notice("trps_handle_update: invalid record in TRP update, discarding entire update.");
+      return TRP_ERROR;
     }
+  }
 
+  for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
     /* determine feasibility */
     feas=trps_check_feasibility(trps, rec);
     tr_debug("trps_handle_update: record feasibility=%d", feas);
@@ -607,13 +602,13 @@ static TRP_RC trps_handle_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
     route=trps_get_route(trps,
                          trp_inforec_get_comm(rec),
                          trp_inforec_get_realm(rec),
-                         trp_inforec_get_next_hop(rec));
+                         trp_upd_get_peer(upd));
     if (route!=NULL) {
       /* there was a route table entry already */
       tr_debug("trps_handle_updates: route entry already exists.");
       if (feas) {
         /* Update is feasible. Accept it. */
-        trps_accept_update(trps, rec);
+        trps_accept_update(trps, upd, rec);
       } else {
         /* Update is infeasible. Ignore it unless the trust router has changed. */
         if (0!=tr_name_cmp(trp_route_get_trust_router(route),
@@ -626,12 +621,37 @@ static TRP_RC trps_handle_update(TRPS_INSTANCE *trps, TRP_UPD *upd)
       /* No existing route table entry. Ignore it unless it is feasible and not a retraction. */
       tr_debug("trps_handle_update: no route entry exists yet.");
       if (feas && trp_metric_is_finite(trp_inforec_get_metric(rec)))
-        trps_accept_update(trps, rec);
+        trps_accept_update(trps, upd, rec);
     }
   }
   return TRP_SUCCESS;
 }
 
+static TRP_RC trps_validate_request(TRPS_INSTANCE *trps, TRP_REQ *req)
+{
+  if (req==NULL) {
+    tr_notice("trps_validate_request: null TRP request.");
+    return TRP_BADARG;
+  }
+
+  if (trp_req_get_comm(req)==NULL) {
+    tr_notice("trps_validate_request: received TRP request with null community.");
+    return TRP_ERROR;
+  }
+  
+  if (trp_req_get_realm(req)==NULL) {
+    tr_notice("trps_validate_request: received TRP request with null realm.");
+    return TRP_ERROR;
+  }
+  
+  if (trp_req_get_peer(req)==NULL) {
+    tr_notice("trps_validate_request: received TRP request without origin peer information.");
+    return TRP_ERROR;
+  }
+  
+  return TRP_SUCCESS;
+}
+
 /* choose the best route to comm/realm, optionally excluding routes to a particular peer */
 static TRP_ROUTE *trps_find_best_route(TRPS_INSTANCE *trps,
                                         TR_NAME *comm,
@@ -666,23 +686,23 @@ static TRP_ROUTE *trps_find_best_route(TRPS_INSTANCE *trps,
  * to avoid flapping between routers or routes. */
 TRP_RC trps_update_active_routes(TRPS_INSTANCE *trps)
 {
-  size_t n_apc=0, ii=0;
-  TR_NAME **apc=trp_rtable_get_apcs(trps->rtable, &n_apc);
+  size_t n_comm=0, ii=0;
+  TR_NAME **comm=trp_rtable_get_comms(trps->rtable, &n_comm);
   size_t n_realm=0, jj=0;
   TR_NAME **realm=NULL;
   TRP_ROUTE *best_route=NULL, *cur_route=NULL;
   unsigned int best_metric=0, cur_metric=0;
 
-  for (ii=0; ii<n_apc; ii++) {
-    realm=trp_rtable_get_apc_realms(trps->rtable, apc[ii], &n_realm);
+  for (ii=0; ii<n_comm; ii++) {
+    realm=trp_rtable_get_comm_realms(trps->rtable, comm[ii], &n_realm);
     for (jj=0; jj<n_realm; jj++) {
-      best_route=trps_find_best_route(trps, apc[ii], realm[jj], NULL);
+      best_route=trps_find_best_route(trps, comm[ii], realm[jj], NULL);
       if (best_route==NULL)
         best_metric=TRP_METRIC_INFINITY;
       else
         best_metric=trp_route_get_metric(best_route);
 
-      cur_route=trps_get_selected_route(trps, apc[ii], realm[jj]);
+      cur_route=trps_get_selected_route(trps, comm[ii], realm[jj]);
       if (cur_route!=NULL) {
         cur_metric=trp_route_get_metric(cur_route);
         if ((best_metric < cur_metric) && (trp_metric_is_finite(best_metric))) {
@@ -699,35 +719,13 @@ TRP_RC trps_update_active_routes(TRPS_INSTANCE *trps)
       talloc_free(realm);
     realm=NULL; n_realm=0;
   }
-  if (apc!=NULL)
-    talloc_free(apc);
-  apc=NULL; n_apc=0;
+  if (comm!=NULL)
+    talloc_free(comm);
+  comm=NULL; n_comm=0;
 
   return TRP_SUCCESS;
 }
 
-TRP_RC trps_handle_tr_msg(TRPS_INSTANCE *trps, TR_MSG *tr_msg)
-{
-  TRP_RC rc=TRP_ERROR;
-
-  switch (tr_msg_get_msg_type(tr_msg)) {
-  case TRP_UPDATE:
-    rc=trps_handle_update(trps, tr_msg_get_trp_upd(tr_msg));
-    if (rc==TRP_SUCCESS) {
-      rc=trps_update_active_routes(trps);
-      trps_update(trps, TRP_UPDATE_TRIGGERED); /* send any triggered routes */
-    }
-    return rc;
-
-  case TRP_REQUEST:
-    return TRP_UNSUPPORTED;
-
-  default:
-    /* unknown error or one we don't care about (e.g., TID messages) */
-    return TRP_ERROR;
-  }
-}
-
 /* true if curtime >= expiry */
 static int trps_expired(struct timespec *expiry, struct timespec *curtime)
 {
@@ -813,8 +811,8 @@ static TRP_ROUTE **trps_select_updates_for_peer(TALLOC_CTX *memctx,
                                                  int triggered,
                                                  size_t *n_update)
 {
-  size_t n_apc=0;
-  TR_NAME **apc=trp_rtable_get_apcs(trps->rtable, &n_apc);
+  size_t n_comm=0;
+  TR_NAME **comm=trp_rtable_get_comms(trps->rtable, &n_comm);
   TR_NAME **realm=NULL;
   size_t n_realm=0;
   size_t ii=0, jj=0;
@@ -828,15 +826,15 @@ static TRP_ROUTE **trps_select_updates_for_peer(TALLOC_CTX *memctx,
    * unlikely to be significant in the near future. */
   result=talloc_array(memctx, TRP_ROUTE *, trp_rtable_size(trps->rtable));
   if (result==NULL) {
-    talloc_free(apc);
+    talloc_free(comm);
     *n_update=0;
     return NULL;
   }
   
-  for (ii=0; ii<n_apc; ii++) {
-    realm=trp_rtable_get_apc_realms(trps->rtable, apc[ii], &n_realm);
+  for (ii=0; ii<n_comm; ii++) {
+    realm=trp_rtable_get_comm_realms(trps->rtable, comm[ii], &n_realm);
     for (jj=0; jj<n_realm; jj++) {
-      best=trps_select_realm_update(trps, apc[ii], realm[jj], peer_gssname);
+      best=trps_select_realm_update(trps, comm[ii], realm[jj], peer_gssname);
       /* If we found a route, add it to the list. If triggered!=0, then only
        * add triggered routes. */
       if ((best!=NULL) && ((!triggered) || trp_route_is_triggered(best)))
@@ -847,8 +845,8 @@ static TRP_ROUTE **trps_select_updates_for_peer(TALLOC_CTX *memctx,
     realm=NULL;
     n_realm=0;
   }
-  if (apc!=NULL)
-    talloc_free(apc);
+  if (comm!=NULL)
+    talloc_free(comm);
 
   *n_update=n_used;
   return result;
@@ -879,13 +877,13 @@ static TRP_INFOREC *trps_route_to_inforec(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *tr
     if (trp_route_is_local(route))
       linkcost=0;
     else {
-      linkcost=trp_peer_get_linkcost(trps_get_peer(trps,
-                                                   trp_route_get_next_hop(route)));
+      linkcost=trp_peer_get_linkcost(trps_get_peer_by_gssname(trps,
+                                                              trp_route_get_peer(route)));
     }
 
     /* 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_apc(route)) != TRP_SUCCESS)
+    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)
        ||(trp_inforec_set_metric(rec,
@@ -900,11 +898,14 @@ static TRP_INFOREC *trps_route_to_inforec(TALLOC_CTX *mem_ctx, TRPS_INSTANCE *tr
   return rec;
 }
 
-TRP_RC trps_update(TRPS_INSTANCE *trps, TRP_UPDATE_TYPE triggered)
+/* 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,
+                                   TRP_PEER *peer,
+                                   TRP_UPDATE_TYPE update_type,
+                                   TR_NAME *comm,
+                                   TR_NAME *realm)
 {
   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
-  TRP_PTABLE_ITER *iter=trp_ptable_iter_new(tmp_ctx);
-  TRP_PEER *peer=NULL;
   TR_MSG msg; /* not a pointer! */
   TRP_UPD *upd=NULL;
   TRP_ROUTE **update_list=NULL;
@@ -912,7 +913,108 @@ TRP_RC trps_update(TRPS_INSTANCE *trps, TRP_UPDATE_TYPE triggered)
   size_t n_updates=0, ii=0;
   char *encoded=NULL;
   TRP_RC rc=TRP_ERROR;
-  TR_NAME *peer_gssname=NULL;
+  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_label->len, peer_label->buf);
+    break;
+  case TRP_UPDATE_SCHEDULED:
+    tr_debug("trps_update_one_peer: preparing scheduled route update for %.*s",
+             peer_label->len, peer_label->buf);
+    break;
+  case TRP_UPDATE_REQUESTED:
+    tr_debug("trps_update_one_peer: preparing requested route update for %.*s",
+             peer_label->len, peer_label->buf);
+  }
+
+  /* do not fill in peer, recipient does that */
+  if ((comm==NULL) && (realm==NULL)) {
+    /* do all realms */
+    update_list=trps_select_updates_for_peer(tmp_ctx,
+                                             trps,
+                                             peer_label,
+                                             update_type==TRP_UPDATE_TRIGGERED,
+                                            &n_updates);
+  } else if ((comm!=NULL) && (realm!=NULL)) {
+    /* a single community/realm was requested */
+    update_list=talloc(tmp_ctx, TRP_ROUTE *);
+    if (update_list==NULL) {
+      tr_err("trps_update_one_peer: could not allocate update_list.");
+      rc=TRP_NOMEM;
+      goto cleanup;
+    }
+    *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.");
+      *update_list=trp_route_new(update_list);
+      trp_route_set_comm(*update_list, tr_dup_name(comm));
+      trp_route_set_realm(*update_list, tr_dup_name(realm));
+      trp_route_set_peer(*update_list, tr_new_name(""));
+      trp_route_set_metric(*update_list, TRP_METRIC_INFINITY);
+      trp_route_set_trust_router(*update_list, tr_new_name(""));
+      trp_route_set_next_hop(*update_list, tr_new_name(""));
+    }
+    n_updates=1;
+  } else {
+    tr_err("trps_update_one_peer: error: only comm or realm was specified.");
+    rc=TRP_ERROR;
+    goto cleanup;
+  }
+  if ((n_updates>0) && (update_list!=NULL)) {
+    tr_debug("trps_update_one_peer: sending %u update records.", (unsigned int)n_updates);
+    upd=trp_upd_new(tmp_ctx);
+
+    for (ii=0; ii<n_updates; ii++) {
+      rec=trps_route_to_inforec(tmp_ctx, trps, update_list[ii]);
+      if (rec==NULL) {
+        tr_err("trps_update_one_peer: could not create all update records.");
+        rc=TRP_ERROR;
+        goto cleanup;
+      }
+      trp_upd_add_inforec(upd, rec);
+    }
+    talloc_free(update_list);
+    update_list=NULL;
+
+    /* now encode the update message */
+    tr_msg_set_trp_upd(&msg, upd);
+    encoded=tr_msg_encode(&msg);
+    if (encoded==NULL) {
+      tr_err("trps_update_one_peer: error encoding update.");
+      rc=TRP_ERROR;
+      goto cleanup;
+    }
+
+    tr_debug("trps_update_one_peer: adding message to queue.");
+    if (trps_send_msg(trps, peer, encoded) != TRP_SUCCESS)
+      tr_err("trps_update_one_peer: error queueing update.");
+    else
+      tr_debug("trps_update_one_peer: update queued successfully.");
+
+    tr_msg_free_encoded(encoded);
+    encoded=NULL;
+    trp_upd_free(upd);
+    upd=NULL;
+  }
+
+cleanup:
+  talloc_free(tmp_ctx);
+  return rc;
+}
+
+/* all routes to all peers */
+TRP_RC trps_update(TRPS_INSTANCE *trps, TRP_UPDATE_TYPE update_type)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TRP_PTABLE_ITER *iter=trp_ptable_iter_new(tmp_ctx);
+  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.");
@@ -921,67 +1023,18 @@ TRP_RC trps_update(TRPS_INSTANCE *trps, TRP_UPDATE_TYPE triggered)
   }
 
   for (peer=trp_ptable_iter_first(iter, trps->ptable);
-       peer!=NULL;
+       peer!=NULL && rc==TRP_SUCCESS;
        peer=trp_ptable_iter_next(iter))
   {
-    peer_gssname=trp_peer_get_gssname(peer);
     if (!trps_peer_connected(trps, 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;
     }
-    if (triggered==TRP_UPDATE_TRIGGERED) {
-      tr_debug("trps_update: preparing triggered route update for %.*s",
-               peer_gssname->len, peer_gssname->buf);
-    } else {
-      tr_debug("trps_update: preparing scheduled route update for %.*s",
-               peer_gssname->len, peer_gssname->buf);
-    }
-    /* do not fill in peer, recipient does that */
-    update_list=trps_select_updates_for_peer(tmp_ctx,
-                                             trps,
-                                             peer_gssname,
-                                             triggered==TRP_UPDATE_TRIGGERED,
-                                            &n_updates);
-    if ((n_updates>0) && (update_list!=NULL)) {
-      tr_debug("trps_update: sending %u update records.", (unsigned int)n_updates);
-      upd=trp_upd_new(tmp_ctx);
-
-      for (ii=0; ii<n_updates; ii++) {
-        rec=trps_route_to_inforec(tmp_ctx, trps, update_list[ii]);
-        if (rec==NULL) {
-          tr_err("trps_update: could not create all update records.");
-          rc=TRP_ERROR;
-          goto cleanup;
-        }
-        trp_upd_add_inforec(upd, rec);
-      }
-      talloc_free(update_list);
-      update_list=NULL;
-
-      /* now encode the update message */
-      tr_msg_set_trp_upd(&msg, upd);
-      encoded=tr_msg_encode(&msg);
-      if (encoded==NULL) {
-        tr_err("trps_update: error encoding update.");
-        rc=TRP_ERROR;
-        goto cleanup;
-      }
-
-      tr_debug("trps_update: adding message to queue.");
-      if (trps_send_msg(trps, peer, encoded) != TRP_SUCCESS)
-        tr_err("trps_update: error queueing update.");
-      else
-        tr_debug("trps_update: update queued successfully.");
-
-      encoded=NULL;
-      tr_msg_free_encoded(encoded);
-      trp_upd_free(upd);
-      upd=NULL;
-    }
+    rc=trps_update_one_peer(trps, peer, update_type, NULL, NULL);
   }
   
-cleanup:
   trp_ptable_iter_free(iter);
   trp_rtable_clear_triggered(trps->rtable); /* don't re-send triggered updates */
   talloc_free(tmp_ctx);
@@ -997,12 +1050,28 @@ 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(TRPS_INSTANCE *trps, TR_NAME *gssname)
+TRP_PEER *trps_get_peer_by_gssname(TRPS_INSTANCE *trps, TR_NAME *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)
 {
-  return trp_ptable_find(trps->ptable, gssname);
+  if (trps->ptable==NULL)
+    return NULL;
+
+  return trp_ptable_find_servicename(trps->ptable, servicename);
 }
 
 int trps_peer_connected(TRPS_INSTANCE *trps, TRP_PEER *peer)
@@ -1016,3 +1085,104 @@ int trps_peer_connected(TRPS_INSTANCE *trps, TRP_PEER *peer)
   else
     return 0;
 }
+
+
+static TRP_RC trps_handle_request(TRPS_INSTANCE *trps, TRP_REQ *req)
+{
+  TR_NAME *comm=NULL;
+  TR_NAME *realm=NULL;
+
+  tr_debug("trps_handle_request: handling TRP request.");
+
+  if (trps_validate_request(trps, req) != TRP_SUCCESS) {
+    tr_notice("trps_handle_request: received invalid TRP request.");
+    return TRP_ERROR;
+  }
+
+  if (!trp_req_is_wildcard(req)) {
+    comm=trp_req_get_comm(req);
+    realm=trp_req_get_realm(req);
+    tr_debug("trps_handle_request: route for %.*s/%.*s requested.",
+             comm->len, comm->buf, realm->len, realm->buf);
+  } else {
+    tr_debug("trps_handle_request: all routes requested.");
+    /* leave comm/realm NULL */
+  }
+  return trps_update_one_peer(trps,
+                              trps_get_peer_by_gssname(trps, trp_req_get_peer(req)),
+                              TRP_UPDATE_REQUESTED,
+                              comm,
+                              realm);
+}
+
+
+TRP_RC trps_handle_tr_msg(TRPS_INSTANCE *trps, TR_MSG *tr_msg)
+{
+  TRP_RC rc=TRP_ERROR;
+
+  switch (tr_msg_get_msg_type(tr_msg)) {
+  case TRP_UPDATE:
+    rc=trps_handle_update(trps, tr_msg_get_trp_upd(tr_msg));
+    if (rc==TRP_SUCCESS) {
+      rc=trps_update_active_routes(trps);
+      trps_update(trps, TRP_UPDATE_TRIGGERED); /* send any triggered routes */
+    }
+    return rc;
+
+  case TRP_REQUEST:
+    rc=trps_handle_request(trps, tr_msg_get_trp_req(tr_msg));
+    return rc;
+
+  default:
+    /* unknown error or one we don't care about (e.g., TID messages) */
+    return TRP_ERROR;
+  }
+}
+
+/* send wildcard route request to a peer */
+TRP_RC trps_wildcard_route_req(TRPS_INSTANCE *trps, TR_NAME *peer_servicename)
+{
+  TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+  TRP_PEER *peer=trps_get_peer_by_servicename(trps, peer_servicename);
+  TR_MSG msg; /* not a pointer */
+  TRP_REQ *req=trp_req_new(tmp_ctx);
+  char *encoded=NULL;
+  TRP_RC rc=TRP_ERROR;
+
+  if (peer==NULL) {
+    tr_err("trps_wildcard_route_req: unknown peer (%.*s).", peer_servicename->len, peer_servicename->buf);
+    rc=TRP_BADARG;
+    goto cleanup;
+  }
+  if ((req==NULL) || (trp_req_make_wildcard(req)!=TRP_SUCCESS)) {
+    tr_err("trps_wildcard_route_req: unable to create wildcard TRP request.");
+    rc=TRP_NOMEM;
+    goto cleanup;
+  }
+
+  tr_msg_set_trp_req(&msg, req);
+  encoded=tr_msg_encode(&msg);
+  if (encoded==NULL) {
+    tr_err("trps_wildcard_route_req: error encoding wildcard TRP request.");
+    rc=TRP_ERROR;
+    goto cleanup;
+  }
+
+  tr_debug("trps_wildcard_route_req: adding message to queue.");
+  if (trps_send_msg(trps, peer, encoded) != TRP_SUCCESS) {
+    tr_err("trps_wildcard_route_req: error queueing request.");
+    rc=TRP_ERROR;
+  } else {
+    tr_debug("trps_wildcard_route_req: request queued successfully.");
+    rc=TRP_SUCCESS;
+  }
+
+cleanup:
+  if (encoded!=NULL)
+    tr_msg_free_encoded(encoded);
+  if (req!=NULL)
+    trp_req_free(req);
+
+  talloc_free(tmp_ctx);
+  return rc;
+}