Checkpoint commit: refactoring the request code in TIDS for better reuse
[trust_router.git] / tid / tidc.c
index e7d7caf..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 <gsscon.h>
+#include <talloc.h>
 
 #include <trust_router/tr_dh.h>
-#include <trust_router/tid.h>
+#include <tid_internal.h>
 #include <tr_msg.h>
 #include <gsscon.h>
+#include <tr_debug.h>
+
 
 int tmp_len = 32;
 
-TIDC_INSTANCE *tidc_create ()
+static int tidc_destructor(void *obj)
 {
-  TIDC_INSTANCE *tidc = NULL;
-
-  if (tidc = malloc(sizeof(TIDC_INSTANCE))) 
-    memset(tidc, 0, sizeof(TIDC_INSTANCE));
-  else
-    return NULL;
-
-  return tidc;
+  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;
 }
 
-void tidc_destroy (TIDC_INSTANCE *tidc)
+/* creates struct in talloc null context */
+TIDC_INSTANCE *tidc_create(void)
 {
-  if (tidc)
-    free(tidc);
+  TIDC_INSTANCE *tidc=talloc(NULL, TIDC_INSTANCE);
+  if (tidc!=NULL) {
+    tidc->client_dh=NULL;
+    talloc_set_destructor((void *)tidc, tidc_destructor);
+  }
+  return tidc;
 }
 
-TID_REQ *tid_dup_req (TID_REQ *orig_req) 
+void tidc_destroy(TIDC_INSTANCE *tidc)
 {
-  TID_REQ *new_req = NULL;
-
-  if (NULL == (new_req = malloc(sizeof(TID_REQ)))) {
-    fprintf(stderr, "tid_dup_req: Can't allocated duplicate request.\n");
-    return NULL;
-  }
-
-  /* Memcpy for flat fields, not valid until names are duped. */
-  memcpy(new_req, orig_req, sizeof(TID_REQ));
-  
-  if ((NULL == (new_req->rp_realm = tr_dup_name(orig_req->rp_realm))) ||
-      (NULL == (new_req->realm = tr_dup_name(orig_req->realm))) ||
-      (NULL == (new_req->comm = tr_dup_name(orig_req->comm)))) {
-       fprintf(stderr, "tid_dup_req: Can't duplicate request (names).\n");
-  }
-
-  if (orig_req->orig_coi) {
-    if (NULL == (new_req->orig_coi = tr_dup_name(orig_req->orig_coi))) {
-      fprintf(stderr, "tid_dup_req: Can't duplicate request (orig_coi).\n");
-    }
-  }
-  
-  return new_req;
+  talloc_free(tidc);
 }
 
 int tidc_open_connection (TIDC_INSTANCE *tidc, 
-                         char *server,
+                         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, TID_PORT, "trustidentity", &conn, gssctx);
+  if (0 == port)
+    use_port = TID_PORT;
+  else
+    use_port = port;
+
+  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;
@@ -104,44 +94,47 @@ int tidc_open_connection (TIDC_INSTANCE *tidc,
     return -1;
 }
 
-int tidc_send_request (TIDC_INSTANCE *tidc, 
-                      int conn, 
+int tidc_send_request (TIDC_INSTANCE *tidc,
+                      int conn,
                       gss_ctx_id_t gssctx,
-                      char *rp_realm,
-                      char *realm, 
-                      char *comm,
+                      const char *rp_realm,
+                      const char *realm, 
+                      const char *comm,
                       TIDC_RESP_FUNC *resp_handler,
                       void *cookie)
-
 {
   TID_REQ *tid_req = NULL;
+  int rc;
 
   /* Create and populate a TID req structure */
-  if (!(tid_req = malloc(sizeof(TID_REQ))))
+  if (!(tid_req = tid_req_new()))
     return -1;
 
-  memset(tid_req, 0, sizeof(TID_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)))) {
-    fprintf (stderr, "tidc_send_request: Error duplicating names.\n");
-    return -1;
+    tr_err ( "tidc_send_request: Error duplicating names.\n");
+    goto error;
   }
 
-  tid_req->tidc_dh = tidc->client_dh;
+  tid_req->tidc_dh = tr_dh_dup(tidc->client_dh);
 
-  return (tidc_fwd_request(tidc, tid_req, resp_handler, cookie));
+  rc = tidc_fwd_request(tidc, tid_req, resp_handler, cookie);
+  goto cleanup;
+ error:
+  rc = -1;
+ cleanup:
+  tid_req_free(tid_req);
+  return rc;
 }
 
-int tidc_fwd_request (TIDC_INSTANCE *tidc, 
-                     TID_REQ *tid_req, 
-                     TIDC_RESP_FUNC *resp_handler,
-                     void *cookie)
-
+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;
@@ -149,33 +142,34 @@ int tidc_fwd_request (TIDC_INSTANCE *tidc,
   TR_MSG *msg = NULL;
   TR_MSG *resp_msg = NULL;
   int err;
+  int rc = 0;
 
   /* Create and populate a TID msg structure */
-  if (!(msg = malloc(sizeof(TR_MSG))))
-    return -1;
+  if (!(msg = talloc_zero(tid_req, TR_MSG)))
+    goto error;
 
   msg->msg_type = TID_REQUEST;
-  msg->tid_req = tid_req;
+  tr_msg_set_req(msg, tid_req);
 
   /* store the response function and cookie */
   // tid_req->resp_func = resp_handler;
   // tid_req->cookie = cookie;
-  
+
 
   /* Encode the request into a json string */
-  if (!(req_buf = tr_msg_encode(msg))) {
-    fprintf(stderr, "tidc_fwd_request: Error encoding TID request.\n");
-    return -1;
+  if (!(req_buf = tr_msg_encode(NULL, msg))) {
+    tr_err("tidc_fwd_request: Error encoding TID request.\n");
+    goto error;
   }
 
-  fprintf (stderr, "tidc_fwd_request: Sending TID request:\n");
-  fprintf (stderr, "%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 (tid_req->conn, tid_req->gssctx, req_buf, 
+  if (err = gsscon_write_encrypted_token (tid_req->conn, tid_req->gssctx, req_buf,
                                          strlen(req_buf))) {
-    fprintf(stderr, "tidc_fwd_request: Error sending request over connection.\n");
-    return -1;
+    tr_err( "tidc_fwd_request: Error sending request over connection.\n");
+    goto error;
   }
 
   /* TBD -- queue request on instance, read resps in separate thread */
@@ -185,44 +179,53 @@ int tidc_fwd_request (TIDC_INSTANCE *tidc,
   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;
   }
 
-  fprintf(stdout, "tidc_fwd_request: Response Received (%u bytes).\n", (unsigned) resp_buflen);
-  fprintf(stdout, "%s\n", resp_buf);
+  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))) {
-    fprintf(stderr, "tidc_fwd_request: Error decoding response.\n");
-    return -1;
+    tr_err( "tidc_fwd_request: Error decoding response.\n");
+    goto error;
   }
 
   /* TBD -- Check if this is actually a valid response */
-  if (!resp_msg->tid_resp) {
-    fprintf(stderr, "tidc_fwd_request: Error, no response in the response!\n");
-    return -1;
+  if (TID_RESPONSE != tr_msg_get_msg_type(resp_msg)) {
+    tr_err( "tidc_fwd_request: Error, no response in the response!\n");
+    goto error;
+  }
+
+  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);
   }
-  
-  if (resp_handler)
-    /* Call the caller's response function */
-    (*resp_handler)(tidc, tid_req, resp_msg->tid_resp, cookie);
-  else
-    fprintf(stderr, "tidc_fwd_request: NULL response function.\n");
 
+  goto cleanup;
+
+ error:
+  rc = -1;
+ cleanup:
   if (msg)
-    free(msg);
-  if (tid_req)
-    free(tid_req);
+    talloc_free(msg);
   if (req_buf)
     free(req_buf);
   if (resp_buf)
     free(resp_buf);
-
-  /* TBD -- free the decoded response */
-
-  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;
+}