Checkpoint commit: refactoring the request code in TIDS for better reuse
[trust_router.git] / tid / tidc.c
index 62968fb..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);
 }
 
 int tidc_open_connection (TIDC_INSTANCE *tidc, 
-                         char *server,
+                         const char *server,
                          unsigned int port,
                          gss_ctx_id_t *gssctx)
 {
@@ -71,9 +82,10 @@ int tidc_open_connection (TIDC_INSTANCE *tidc,
 
   if (0 == port)
     use_port = TID_PORT;
-  else 
+  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)
@@ -82,12 +94,12 @@ 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)
 {
@@ -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;
@@ -142,10 +154,10 @@ int tidc_fwd_request (TIDC_INSTANCE *tidc,
   /* 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))) {
+  if (!(req_buf = tr_msg_encode(NULL, msg))) {
     tr_err("tidc_fwd_request: Error encoding TID request.\n");
     goto error;
   }
@@ -154,7 +166,7 @@ int tidc_fwd_request (TIDC_INSTANCE *tidc,
   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))) {
     tr_err( "tidc_fwd_request: Error sending request over connection.\n");
     goto error;
@@ -183,10 +195,13 @@ int tidc_fwd_request (TIDC_INSTANCE *tidc,
     tr_err( "tidc_fwd_request: Error, no response in the response!\n");
     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;
 }