Real syslog support for trust router
[trust_router.git] / tid / tidc.c
index 5c3028f..bd88671 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 <trust_router/tr_dh.h>
-#include <trust_router/tid.h>
+#include <tid_internal.h>
 #include <tr_msg.h>
 #include <gsscon.h>
 
@@ -62,35 +60,21 @@ void tidc_destroy (TIDC_INSTANCE *tidc)
     free(tidc);
 }
 
-TID_REQ *tid_dup_req (TID_REQ *orig_req) 
-{
-  TID_REQ *new_req;
-
-  if (NULL == (new_req = malloc(sizeof(TID_REQ))))
-    return NULL;
-
-  /* Memcpy for flat fields, not valid until names are duped. */
-  memcpy(new_req, orig_req, sizeof(TID_REQ));
-  
-  new_req->rp_realm = tr_dup_name(orig_req->rp_realm);
-  new_req->realm = tr_dup_name(orig_req->realm);
-  new_req->comm = tr_dup_name(orig_req->comm);
-  new_req->orig_coi = tr_dup_name(orig_req->orig_coi);
-
-  return new_req;
-}
-
 int tidc_open_connection (TIDC_INSTANCE *tidc, 
                          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, &conn);
+  if (0 == port)
+    use_port = TID_PORT;
+  else 
+    use_port = port;
 
-  if (!err)
-    err = gsscon_active_authenticate(conn, NULL, "trustidentity", gssctx);
+  err = gsscon_connect(server, use_port, "trustidentity", &conn, gssctx);
 
   if (!err)
     return conn;
@@ -111,11 +95,9 @@ int tidc_send_request (TIDC_INSTANCE *tidc,
   TID_REQ *tid_req = NULL;
 
   /* 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;
 
@@ -127,9 +109,7 @@ int tidc_send_request (TIDC_INSTANCE *tidc,
   }
 
   tid_req->tidc_dh = tidc->client_dh;
-  tid_req->resp_func = resp_handler;
-  tid_req->cookie = cookie;
-  
+
   return (tidc_fwd_request(tidc, tid_req, resp_handler, cookie));
 }
 
@@ -151,21 +131,26 @@ int tidc_fwd_request (TIDC_INSTANCE *tidc,
     return -1;
 
   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))) {
-    printf("Error encoding TID request.\n");
+    fprintf(stderr, "tidc_fwd_request: Error encoding TID request.\n");
     return -1;
   }
 
-  printf ("Sending TID request:\n");
-  printf ("%s\n", req_buf);
+  fprintf (stderr, "tidc_fwd_request: Sending TID request:\n");
+  fprintf (stderr, "%s\n", req_buf);
 
   /* Send the request over the connection */
   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");
+    fprintf(stderr, "tidc_fwd_request: Error sending request over connection.\n");
     return -1;
   }
 
@@ -179,28 +164,30 @@ int tidc_fwd_request (TIDC_INSTANCE *tidc,
     return -1;
   }
 
-  fprintf(stdout, "Response Received (%u bytes).\n", (unsigned) resp_buflen);
+  fprintf(stdout, "tidc_fwd_request: Response Received (%u bytes).\n", (unsigned) resp_buflen);
   fprintf(stdout, "%s\n", resp_buf);
 
   if (NULL == (resp_msg = tr_msg_decode(resp_buf, resp_buflen))) {
-    fprintf(stderr, "Error decoding response.\n");
+    fprintf(stderr, "tidc_fwd_request: Error decoding response.\n");
     return -1;
   }
 
   /* TBD -- Check if this is actually a valid response */
-  if (!resp_msg->tid_resp) {
-    fprintf(stderr, "Error: No response in the response!\n");
+  if (TID_RESPONSE != tr_msg_get_msg_type(resp_msg)) {
+    fprintf(stderr, "tidc_fwd_request: Error, no response in the response!\n");
     return -1;
   }
   
-  /* Call the caller's response function */
-  (*tid_req->resp_func)(tidc, tid_req, resp_msg->tid_resp, cookie);
-
+  if (resp_handler)
+    /* Call the caller's response function */
+    (*resp_handler)(tidc, tid_req, tr_msg_get_resp(resp_msg), cookie);
+  else
+    fprintf(stderr, "tidc_fwd_request: NULL response function.\n");
 
   if (msg)
     free(msg);
   if (tid_req)
-    free(tid_req);
+    tid_req_free(tid_req);
   if (req_buf)
     free(req_buf);
   if (resp_buf)
@@ -212,6 +199,13 @@ int tidc_fwd_request (TIDC_INSTANCE *tidc,
 }
 
 
+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;
+}