Merge branch 'milestone/monitoring' into jennifer/request_id
[trust_router.git] / tid / tid_req.c
index 23599ad..002b720 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 <unistd.h>
 #include <stdlib.h>
 #include <assert.h>
 #include <talloc.h>
 
 #include <tid_internal.h>
+#include <tr_debug.h>
+
 #include <jansson.h>
 
 static int destroy_tid_req(TID_REQ *req)
 {
   if (req->json_references)
     json_decref(req->json_references);
+  if (req->free_conn) {
+    if (req->conn)
+      close(req->conn);
+    if (req->gssctx) {
+      OM_uint32 minor;
+      gss_delete_sec_context( &minor, &req->gssctx, NULL);
+    }
+  }
+  if (req->rp_realm!=NULL)
+    tr_free_name(req->rp_realm);
+  if (req->realm!=NULL)
+    tr_free_name(req->realm);
+  if (req->comm!=NULL)
+    tr_free_name(req->comm);
+  if (req->orig_coi!=NULL)
+    tr_free_name(req->orig_coi);
+  if (req->request_id!=NULL)
+    tr_free_name(req->request_id);
   return 0;
 }
 
@@ -56,6 +77,8 @@ TID_REQ *tid_req_new()
   req->json_references = json_array();
   assert(req->json_references);
   req->conn = -1;
+  req->free_conn = 1;
+  req->request_id = NULL;
   return req;
 }
 
@@ -144,11 +167,21 @@ TR_NAME *tid_req_get_orig_coi(TID_REQ *req)
   return(req->orig_coi);
 }
 
-void tid_req_set_rp_orig_coi(TID_REQ *req, TR_NAME *orig_coi)
+void tid_req_set_orig_coi(TID_REQ *req, TR_NAME *orig_coi)
 {
   req->orig_coi = orig_coi;
 }
 
+void tid_req_set_request_id(TID_REQ *req, TR_NAME *request_id)
+{
+  req->request_id = request_id;
+}
+
+TR_NAME *tid_req_get_request_id(TID_REQ *req)
+{
+  return(req->request_id);
+}
+
 TIDC_RESP_FUNC *tid_req_get_resp_func(TID_REQ *req)
 {
   return(req->resp_func);
@@ -169,35 +202,46 @@ void tid_req_set_cookie(TID_REQ *req, void *cookie)
   req->cookie = cookie;
 }
 
+/* struct is allocated in talloc null context */
 TID_REQ *tid_dup_req (TID_REQ *orig_req) 
 {
   TID_REQ *new_req = NULL;
 
-  if (NULL == (new_req = malloc(sizeof(TID_REQ)))) {
-    fprintf(stderr, "tid_dup_req: Can't allocated duplicate request.\n");
+  if (NULL == (new_req = talloc_zero(NULL, TID_REQ))) {
+    tr_crit("tid_dup_req: Can't allocated duplicate request.");
     return NULL;
   }
 
   /* Memcpy for flat fields, not valid until names are duped. */
   memcpy(new_req, orig_req, sizeof(TID_REQ));
   json_incref(new_req->json_references);
+  new_req->free_conn = 0;
   
   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");
+       tr_crit("tid_dup_req: Can't duplicate request (names).");
   }
 
   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");
+      tr_crit("tid_dup_req: Can't duplicate request (orig_coi).");
     }
   }
-  
+
+  if (orig_req->request_id) {
+    if (NULL == (new_req->request_id = tr_dup_name(orig_req->request_id))) {
+      tr_crit("tid_dup_req: Can't duplicate request (request_id).");
+    }
+  }
+
   return new_req;
 }
 
 
+/* Adds the JSON object ref to req's list of objects to release when the
+ * req is freed.
+ */
 void tid_req_cleanup_json( TID_REQ *req, json_t *ref)
 {
   (void) json_array_append_new(req->json_references, ref);
@@ -208,6 +252,21 @@ void tid_req_free(TID_REQ *req)
   talloc_free(req);
 }
 
+int tid_req_add_path(TID_REQ *req,
+                    const char *this_system, unsigned port)
+{
+  char *path_element = talloc_asprintf(req, "%s:%u",
+                                      this_system, port);
+  if (!req->path) {
+    req->path = json_array();
+    if (!req->path)
+      return -1;
+    tid_req_cleanup_json(req, req->path);
+  }
+  return json_array_append( req->path, json_string(path_element));
+}
+
+
 
 void tid_srvr_get_address(const TID_SRVR_BLK *blk,
                          const struct sockaddr **out_addr,
@@ -217,8 +276,8 @@ void tid_srvr_get_address(const TID_SRVR_BLK *blk,
     assert(blk);
     sa = talloc_zero(blk, struct sockaddr_in);
     sa->sin_family = AF_INET;
-    sa->sin_addr = blk->aaa_server_addr;
-    sa->sin_port = htons(2083);
+    inet_aton(blk->aaa_server_addr, &(sa->sin_addr));
+    sa->sin_port = htons(2083); /* radsec port */
     *out_addr = (struct sockaddr *) sa;
     *out_len = sizeof( struct sockaddr_in);
 }