Checkpoint commit: refactoring the request code in TIDS for better reuse
[trust_router.git] / tid / tidc.c
index 895fce6..648107a 100644 (file)
 
 int tmp_len = 32;
 
-TIDC_INSTANCE *tidc_create ()
+static int tidc_destructor(void *obj)
 {
-  TIDC_INSTANCE *tidc = NULL;
-
-  if (NULL == (tidc = talloc_zero(NULL, TIDC_INSTANCE)))
-    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;
+}
 
+/* 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 tidc_destroy (TIDC_INSTANCE *tidc)
+void tidc_destroy(TIDC_INSTANCE *tidc)
 {
   talloc_free(tidc);
 }
@@ -74,6 +85,7 @@ int tidc_open_connection (TIDC_INSTANCE *tidc,
   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)
@@ -108,7 +120,7 @@ int tidc_send_request (TIDC_INSTANCE *tidc,
     goto error;
   }
 
-  tid_req->tidc_dh = tidc->client_dh;
+  tid_req->tidc_dh = tr_dh_dup(tidc->client_dh);
 
   rc = tidc_fwd_request(tidc, tid_req, resp_handler, cookie);
   goto cleanup;
@@ -119,10 +131,10 @@ int tidc_send_request (TIDC_INSTANCE *tidc,
   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;
@@ -145,7 +157,7 @@ int tidc_fwd_request (TIDC_INSTANCE *tidc,
 
 
   /* Encode the request into a json string */
-  if (!(req_buf = tr_msg_encode(msg))) {
+  if (!(req_buf = tr_msg_encode(NULL, msg))) {
     tr_err("tidc_fwd_request: Error encoding TID request.\n");
     goto error;
   }
@@ -184,9 +196,12 @@ int tidc_fwd_request (TIDC_INSTANCE *tidc,
     goto error;
   }
 
-  if (resp_handler)
-    /* Call the caller's response function */
+  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);
+  }
+
   goto cleanup;
 
  error:
@@ -198,9 +213,8 @@ int tidc_fwd_request (TIDC_INSTANCE *tidc,
     free(req_buf);
   if (resp_buf)
     free(resp_buf);
-
-  /* TBD -- free the decoded response */
-
+  if (resp_msg)
+    tr_msg_free_decoded(resp_msg);
   return rc;
 }