Checkpoint commit: refactoring the request code in TIDS for better reuse
[trust_router.git] / tid / tidc.c
index e7ab977..648107a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, JANET(UK)
+ * Copyright (c) 2012, 2014-2015, JANET(UK)
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  */
 
 #include <stdio.h>
-#include <stdlib.h>
 #include <jansson.h>
+#include <talloc.h>
 
-#include <gsscon.h>
-#include <tr_dh.h>
-#include <tpq.h>
+#include <trust_router/tr_dh.h>
+#include <tid_internal.h>
 #include <tr_msg.h>
+#include <gsscon.h>
+#include <tr_debug.h>
 
-/* char tmp_key[32] = 
-  {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 
-   0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
-   0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
-   0x19, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
-*/
 
 int tmp_len = 32;
 
-TPQC_INSTANCE *tpqc_create ()
+static int tidc_destructor(void *obj)
 {
-  TPQC_INSTANCE *tpqc = NULL;
-
-  if (tpqc = malloc(sizeof(TPQC_INSTANCE))) 
-    memset(tpqc, 0, sizeof(TPQC_INSTANCE));
-  else
-    return NULL;
-
-  if (NULL == (tpqc->priv_dh = tr_create_dh_params(NULL, 0))) {
-    free (tpqc);
-    return NULL;
+  TIDC_INSTANCE *tidc=talloc_get_type_abort(obj, TIDC_INSTANCE);
+  if (NULL!=tidc) {
+    if (NULL!=tidc->client_dh)
+      tr_destroy_dh_params(tidc->client_dh);
   }
+  return 0;
+}
 
-  fprintf(stderr, "TPQC DH Parameters:\n");
-  DHparams_print_fp(stdout, tpqc->priv_dh);
-  fprintf(stderr, "\n");
-
-  return tpqc;
+/* creates struct in talloc null context */
+TIDC_INSTANCE *tidc_create(void)
+{
+  TIDC_INSTANCE *tidc=talloc(NULL, TIDC_INSTANCE);
+  if (tidc!=NULL) {
+    tidc->client_dh=NULL;
+    talloc_set_destructor((void *)tidc, tidc_destructor);
+  }
+  return tidc;
 }
 
-void tpqc_destroy (TPQC_INSTANCE *tpqc)
+void tidc_destroy(TIDC_INSTANCE *tidc)
 {
-  if (tpqc)
-    free(tpqc);
+  talloc_free(tidc);
 }
 
-int tpqc_open_connection (TPQC_INSTANCE *tpqc, 
-                         char *server,
+int tidc_open_connection (TIDC_INSTANCE *tidc, 
+                         const char *server,
+                         unsigned int port,
                          gss_ctx_id_t *gssctx)
 {
   int err = 0;
   int conn = -1;
+  unsigned int use_port = 0;
 
-  err = gsscon_connect(server, TPQ_PORT, &conn);
+  if (0 == port)
+    use_port = TID_PORT;
+  else
+    use_port = port;
 
-  if (!err)
-    err = gsscon_active_authenticate(conn, NULL, "trustquery", gssctx);
+  tr_debug("tidc_open_connection: opening tidc connection to %s:%d", server, port);
+  err = gsscon_connect(server, use_port, "trustidentity", &conn, gssctx);
 
   if (!err)
     return conn;
@@ -95,92 +94,138 @@ int tpqc_open_connection (TPQC_INSTANCE *tpqc,
     return -1;
 }
 
-int tpqc_send_request (TPQC_INSTANCE *tpqc, 
-                      int conn, 
+int tidc_send_request (TIDC_INSTANCE *tidc,
+                      int conn,
                       gss_ctx_id_t gssctx,
-                      char *rp_realm,
-                      char *realm, 
-                      char *coi,
-                      TPQC_RESP_FUNC *resp_handler,
+                      const char *rp_realm,
+                      const char *realm, 
+                      const char *comm,
+                      TIDC_RESP_FUNC *resp_handler,
                       void *cookie)
-
 {
-  json_t *jreq;
-  int err;
-  char *req_buf;
-  char *resp_buf;
-  size_t resp_buflen = 0;
-  TR_MSG *msg;
-  TPQ_REQ *tpq_req;
+  TID_REQ *tid_req = NULL;
+  int rc;
 
-  /* Create and populate a TPQ msg structure */
-  if ((!(msg = malloc(sizeof(TR_MSG)))) ||
-      (!(tpq_req = malloc(sizeof(TPQ_REQ)))))
+  /* Create and populate a TID req structure */
+  if (!(tid_req = tid_req_new()))
     return -1;
 
-  memset(tpq_req, 0, sizeof(tpq_req));
+  tid_req->conn = conn;
+  tid_req->gssctx = gssctx;
+
+  if ((NULL == (tid_req->rp_realm = tr_new_name(rp_realm))) ||
+      (NULL == (tid_req->realm = tr_new_name(realm))) ||
+      (NULL == (tid_req->comm = tr_new_name(comm)))) {
+    tr_err ( "tidc_send_request: Error duplicating names.\n");
+    goto error;
+  }
 
-  msg->msg_type = TPQ_REQUEST;
+  tid_req->tidc_dh = tr_dh_dup(tidc->client_dh);
 
-  msg->tpq_req = tpq_req;
+  rc = tidc_fwd_request(tidc, tid_req, resp_handler, cookie);
+  goto cleanup;
+ error:
+  rc = -1;
+ cleanup:
+  tid_req_free(tid_req);
+  return rc;
+}
 
-  tpq_req->conn = conn;
+int tidc_fwd_request(TIDC_INSTANCE *tidc,
+                     TID_REQ *tid_req,
+                    TIDC_RESP_FUNC *resp_handler,
+                     void *cookie)
+{
+  char *req_buf = NULL;
+  char *resp_buf = NULL;
+  size_t resp_buflen = 0;
+  TR_MSG *msg = NULL;
+  TR_MSG *resp_msg = NULL;
+  int err;
+  int rc = 0;
 
-  /* TBD -- error handling */
-  tpq_req->rp_realm = tr_new_name(rp_realm);
-  tpq_req->realm = tr_new_name(realm);
-  tpq_req->coi = tr_new_name(coi);
+  /* Create and populate a TID msg structure */
+  if (!(msg = talloc_zero(tid_req, TR_MSG)))
+    goto error;
+
+  msg->msg_type = TID_REQUEST;
+  tr_msg_set_req(msg, tid_req);
+
+  /* store the response function and cookie */
+  // tid_req->resp_func = resp_handler;
+  // tid_req->cookie = cookie;
 
-  tpq_req->tpqc_dh = tpqc->priv_dh;
-  
-  tpq_req->resp_func = resp_handler;
-  tpq_req->cookie = cookie;
 
   /* Encode the request into a json string */
-  if (!(req_buf = tr_msg_encode(msg))) {
-    printf("Error encoding TPQ request.\n");
-    return -1;
+  if (!(req_buf = tr_msg_encode(NULL, msg))) {
+    tr_err("tidc_fwd_request: Error encoding TID request.\n");
+    goto error;
   }
 
-  printf ("Sending TPQ request:\n");
-  printf ("%s\n", req_buf);
+  tr_debug( "tidc_fwd_request: Sending TID request:\n");
+  tr_debug( "%s\n", req_buf);
 
   /* Send the request over the connection */
-  if (err = gsscon_write_encrypted_token (conn, gssctx, req_buf, 
+  if (err = gsscon_write_encrypted_token (tid_req->conn, tid_req->gssctx, req_buf,
                                          strlen(req_buf))) {
-    fprintf(stderr, "Error sending request over connection.\n");
-    return -1;
+    tr_err( "tidc_fwd_request: Error sending request over connection.\n");
+    goto error;
   }
 
-  /* TBD -- should queue request on instance, resps read in separate thread */
-  /* Read the response from the connection */
+  /* TBD -- queue request on instance, read resps in separate thread */
 
-  if (err = gsscon_read_encrypted_token(conn, gssctx, &resp_buf, &resp_buflen)) {
+  /* Read the response from the connection */
+  /* TBD -- timeout? */
+  if (err = gsscon_read_encrypted_token(tid_req->conn, tid_req->gssctx, &resp_buf, &resp_buflen)) {
     if (resp_buf)
       free(resp_buf);
-    return -1;
+    goto error;
+  }
+
+  tr_debug( "tidc_fwd_request: Response Received (%u bytes).\n", (unsigned) resp_buflen);
+  tr_debug( "%s\n", resp_buf);
+
+  if (NULL == (resp_msg = tr_msg_decode(resp_buf, resp_buflen))) {
+    tr_err( "tidc_fwd_request: Error decoding response.\n");
+    goto error;
   }
 
-  fprintf(stdout, "Response Received, %d bytes.\n", resp_buflen);
+  /* TBD -- Check if this is actually a valid response */
+  if (TID_RESPONSE != tr_msg_get_msg_type(resp_msg)) {
+    tr_err( "tidc_fwd_request: Error, no response in the response!\n");
+    goto error;
+  }
 
-  /* Parse response -- TBD */
+  if (resp_handler) {
+    /* Call the caller's response function. It must copy any data it needs before returning. */
+    tr_debug("tidc_fwd_request: calling response callback function.");
+    (*resp_handler)(tidc, tid_req, tr_msg_get_resp(resp_msg), cookie);
+  }
 
-  /* Call the caller's response function */
-  (*resp_handler)(tpqc, NULL, cookie);
+  goto cleanup;
 
+ error:
+  rc = -1;
+ cleanup:
   if (msg)
-    free(msg);
-  if (tpq_req)
-    free(tpq_req);
+    talloc_free(msg);
   if (req_buf)
     free(req_buf);
   if (resp_buf)
     free(resp_buf);
-
-  return 0;
+  if (resp_msg)
+    tr_msg_free_decoded(resp_msg);
+  return rc;
 }
 
 
+DH * tidc_get_dh(TIDC_INSTANCE *inst)
+{
+  return inst->client_dh;
+}
 
-
-
+DH *tidc_set_dh(TIDC_INSTANCE *inst, DH *dh)
+{
+  inst->client_dh = dh;
+  return dh;
+}