TR request and response message handlers.
[trust_router.git] / tr / tr_main.c
index 4f330ae..36f5a89 100644 (file)
 #include <tr.h>
 #include <trust_router/tid.h>
 #include <tr_config.h>
+#include <tr_idp.h>
 
+/* Structure to hold TR instance and original request in one cookie */
+typedef struct tr_resp_cookie {
+  TR_INSTANCE *tr;
+  TID_REQ *orig_req;
+} TR_RESP_COOKIE;
 
-static int tids_req_handler (TIDS_INSTANCE * tids,
+static void tr_tidc_resp_handler (TIDC_INSTANCE *tidc, 
+                       TID_REQ *req,
+                       TID_RESP *resp, 
+                       void *resp_cookie) 
+{
+  fprintf(stderr, "tr_tidc_resp_handler: Response received! Realm = %s, Community = %s.\n", resp->realm->buf, resp->comm->buf);
+  req->resp_rcvd = 1;
+
+  /* TBD -- handle concatentation of multiple responses to single req */
+  tids_send_response(((TR_RESP_COOKIE *)resp_cookie)->tr->tids, ((TR_RESP_COOKIE *)resp_cookie)->orig_req->conn, ((TR_RESP_COOKIE *)resp_cookie)->orig_req->gssctx, resp);
+  
+  return;
+}
+
+static int tr_tids_req_handler (TIDS_INSTANCE * tids,
                      TID_REQ *req, 
                      TID_RESP **resp,
-                     void *cookie)
+                     void *tr)
 {
+  gss_ctx_id_t gssctx;
+  TIDC_INSTANCE *tidc = NULL;
+  TR_RESP_COOKIE resp_cookie;
+  TR_AAA_SERVER *aaa_servers = NULL;
+  int conn = 0;
+  int rc;
+
+  if ((!tids) || (!req) || (!resp) || (!(*resp))) {
+    printf("tids_req_handler: Bad parameters\n");
+    return -1;
+  }
+
   printf("Request received! Realm = %s, Comm = %s\n", req->realm->buf, req->comm->buf);
   if (tids)
     tids->req_count++;
 
+  /* find the AAA server(s) for this request */
+  aaa_servers = tr_idp_aaa_server_lookup((TR_INSTANCE *)tids->cookie, req->realm, req->comm);
+  /* send a TID request to the AAA server(s), and get the answer(s) */
+  /* TBD -- Handle multiple servers */
+
+  /* Create a TID client instance */
+  if (NULL == (tidc = tidc_create())) {
+    fprintf(stderr, "tr_tids_req_hander: Unable to allocate TIDC instance.\n");
+    return -1;
+  }
+
+  /* Set-up TID connection */
+  /* TBD -- version of open_connection that takes an inaddr */
+  if (-1 == (conn = tidc_open_connection(tidc, inet_ntoa(aaa_servers->aaa_server_addr), &gssctx))) {
+    printf("tr_tids_req_handler: Error in tidc_open_connection.\n");
+    return -1;
+  };
+
+  /* Send a TID request */
+  resp_cookie.tr = tr;
+  resp_cookie.orig_req = req;
+
+  /* TBD -- version of send request that takes TR_NAMES */
+  if (0 > (rc = tidc_send_request(tidc, conn, gssctx, req->rp_realm->buf, req->realm->buf, req->comm->buf, &tr_tidc_resp_handler, (void *)&resp_cookie))) {
+    printf("Error in tidc_send_request, rc = %d.\n", rc);
+    return -1;
+  }
+    
   return 0;
 }
 
 int main (int argc, const char *argv[])
 {
   TR_INSTANCE *tr = NULL;
-  TIDS_INSTANCE *tids = NULL;
   struct dirent **cfg_files = NULL;
   json_t *jcfg = NULL;
   TR_CFG_RC rc = TR_CFG_SUCCESS;       /* presume success */
@@ -91,20 +150,19 @@ int main (int argc, const char *argv[])
     exit(1);
   }
 
-  //  printf("Trust Router Configured, max_tree_depth = %d.\n", tr->active_cfg->internal->max_tree_depth);
-
   /* initialize the trust path query server instance */
-  if (0 == (tids = tids_create ())) {
+  if (0 == (tr->tids = tids_create ())) {
     printf ("Error initializing Trust Path Query Server instance.\n");
-    return 1;
+    exit(1);
   }
 
   /* start the trust path query server, won't return unless error. */
-  if (0 != (err = tids_start(tids, &tids_req_handler, NULL))) {
+  if (0 != (err = tids_start(tr->tids, &tr_tids_req_handler, (void *)tr))) {
     printf ("Error starting Trust Path Query Server, err = %d.\n", err);
-    return err;
+    exit(err);
   }
 
-  tids_destroy(tids);
-  return 0;
+  tids_destroy(tr->tids);
+  tr_destroy(tr);
+  exit(0);
 }