Additional message encoding/decoding fields and functions.
authorMargaret Wasserman <margaret@debian.(none)>
Wed, 20 Mar 2013 00:53:18 +0000 (20:53 -0400)
committerMargaret Wasserman <margaret@debian.(none)>
Wed, 20 Mar 2013 00:53:18 +0000 (20:53 -0400)
common/tr_msg.c
include/tid.h
include/tr_msg.h
tid/example/tids_main.c
tid/tidc.c
tr/tr_main.c

index 42c725b..cb60292 100644 (file)
@@ -64,6 +64,35 @@ static json_t *tr_msg_encode_dh(DH *dh)
 static DH *tr_msg_decode_dh(json_t *jdh)
 {
   DH *dh = NULL;
+  json_error_t rc;
+  json_t *jp = NULL;
+  json_t *jg = NULL;
+  json_t *jpub_key = NULL;
+  int msize;
+
+  if (!(dh = malloc(sizeof(DH)))) {
+    fprintf (stderr, "tr_msg_decode_dh(): Error allocating DH structure.\n");
+    return NULL;
+  }
+  memset(dh, 0, sizeof(DH));
+
+  /* store required fields from dh object */
+  if (((msize = json_object_size(jdh)) < 3) ||
+      (NULL == (jp = json_object_get(jdh, "dh_p"))) ||
+      (!json_is_string(jp)) ||
+      (NULL == (jg = json_object_get(jdh, "dh_g"))) ||
+      (!json_is_string(jg)) ||
+      (NULL == (jpub_key = json_object_get(jdh, "dh_pub_key"))) ||
+      (!json_is_string(jdh))) {
+    fprintf (stderr, "tr_msg_decode(): Error parsing message.\n");
+    free(dh);
+    return NULL;
+  }
+
+  dh->p = json_string_value(jp);
+  dh->g = json_string_value(jg);
+  dh->pub_key = json_string_value(jpub_key);
 
   return dh;
 }
@@ -73,7 +102,7 @@ json_t *tr_msg_encode_tidreq(TID_REQ *req)
   json_t *jreq = NULL;
   json_t *jstr = NULL;
 
-  if ((!req) || (!req->rp_realm) || (!req->realm) || !(req->coi))
+  if ((!req) || (!req->rp_realm) || (!req->realm) || !(req->comm))
     return NULL;
 
   jreq = json_object();
@@ -84,7 +113,7 @@ json_t *tr_msg_encode_tidreq(TID_REQ *req)
   jstr = json_string(req->realm->buf);
   json_object_set_new(jreq, "target_realm", jstr);
 
-  jstr = json_string(req->coi->buf);
+  jstr = json_string(req->comm->buf);
   json_object_set_new(jreq, "community", jstr);
 
   json_object_set_new(jreq, "dh_info", tr_msg_encode_dh(req->tidc_dh));
@@ -94,24 +123,143 @@ json_t *tr_msg_encode_tidreq(TID_REQ *req)
 
 TID_REQ *tr_msg_decode_tidreq(json_t *jreq)
 {
-  TID_REQ *req = NULL;
+  TID_REQ *treq = NULL;
+  json_error_t rc;
+  json_t *jrp_realm = NULL;
+  json_t *jrealm = NULL;
+  json_t *jcomm = NULL;
+  json_t *jorig_coi = NULL;
+  json_t *jdh = NULL;
+  int msize;
+
+  if (!(treq = malloc(sizeof(TID_REQ)))) {
+    fprintf (stderr, "tr_msg_decode_tidreq(): Error allocating TID_REQ structure.\n");
+    return NULL;
+  }
+  memset(treq, 0, sizeof(TID_REQ));
+
+  /* store required fields from request */
+  if (((msize = json_object_size(jreq)) < 4) ||
+      (NULL == (jrp_realm = json_object_get(jreq, "rp_realm"))) ||
+      (!json_is_string(jrp_realm)) ||
+      (NULL == (jrealm = json_object_get(jreq, "realm"))) ||
+      (!json_is_string(jrealm)) ||
+      (NULL == (jcomm = json_object_get(jreq, "comm"))) ||
+      (!json_is_string(jcomm)) ||
+      (NULL == (jdh = json_object_get(jreq, "dh_info"))) ||
+      (!json_is_object(jdh))) {
+    fprintf (stderr, "tr_msg_decode(): Error parsing message.\n");
+    free(treq);
+    return NULL;
+  }
+
+  treq->rp_realm->buf = json_string_value(jrp_realm);
+  treq->rp_realm->len = strlen(treq->rp_realm->buf);
+  treq->realm->buf = json_string_value(jrealm);
+  treq->realm->len = strlen(treq->realm->buf);
+  treq->comm->buf = json_string_value(jcomm);
+  treq->comm->len = strlen(treq->comm->buf);
+  treq->tidc_dh = tr_msg_decode_dh(jdh);
+
+  /* store optional "orig_coi" field */
+  if ((NULL != (jorig_coi = json_object_get(jreq, "orig_coi"))) &&
+      (!json_is_object(jorig_coi))) {
+    treq->orig_coi->buf = json_string_value(jorig_coi);
+    treq->orig_coi->len = strlen(treq->orig_coi->buf);
+  }
 
-  return req;
+  return treq;
 }
 
 json_t *tr_msg_encode_tidresp(TID_RESP *resp)
 {
   json_t *jresp = NULL;
+  json_t *jstr = NULL;
+
+  if ((!resp) || (!resp->result) || (!resp->rp_realm) || (!resp->realm) || !(resp->comm))
+    return NULL;
+
+  jresp = json_object();
+
+  jstr = json_string(resp->result->buf);
+  json_object_set_new(jresp, "result", jstr);
+
+  jstr = json_string(resp->rp_realm->buf);
+  json_object_set_new(jresp, "rp_realm", jstr);
+
+  jstr = json_string(resp->realm->buf);
+  json_object_set_new(jresp, "target_realm", jstr);
 
+  jstr = json_string(resp->comm->buf);
+  json_object_set_new(jresp, "comm", jstr);
+
+  if (resp->orig_coi) {
+    jstr = json_string(resp->orig_coi->buf);
+    json_object_set_new(jresp, "orig_coi", jstr);
+  }
+
+  // TBD -- Encode server info.
+  
   return jresp;
 }
 
-
 TID_RESP *tr_msg_decode_tidresp(json_t *jresp)
 {
-  TID_RESP *resp = NULL;
+  TID_RESP *tresp = NULL;
+  json_error_t rc;
+  json_t *jresult = NULL;
+  json_t *jrp_realm = NULL;
+  json_t *jrealm = NULL;
+  json_t *jcomm = NULL;
+  json_t *jorig_coi = NULL;
+  json_t *jservers = NULL;
+  int msize;
+
+  if (!(tresp = malloc(sizeof(TID_RESP)))) {
+    fprintf (stderr, "tr_msg_decode_tidresp(): Error allocating TID_RESP structure.\n");
+    return NULL;
+  }
+  memset(tresp, 0, sizeof(TID_RESP));
+
+  /* store required fields from request */
+  if (((msize = json_object_size(jresp)) < 5) ||
+      (NULL == (jresult = json_object_get(jresp, "result"))) ||
+      (!json_is_string(jresult)) ||
+      (NULL == (jrp_realm = json_object_get(jresp, "rp_realm"))) ||
+      (!json_is_string(jrp_realm)) ||
+      (NULL == (jrealm = json_object_get(jresp, "realm"))) ||
+      (!json_is_string(jrealm)) ||
+      (NULL == (jcomm = json_object_get(jresp, "comm"))) ||
+      (!json_is_string(jcomm)) ||
+      (NULL == (jservers = json_object_get(jresp, "servers"))) ||
+      (!json_is_object(jservers))) {
+    fprintf (stderr, "tr_msg_decode(): Error parsing message.\n");
+    free(tresp);
+    return NULL;
+  }
+
+  tresp->result->buf = json_string_value(jresult);
+  tresp->result->len = strlen(tresp->result->buf);
+  tresp->rp_realm->buf = json_string_value(jrp_realm);
+  tresp->rp_realm->len = strlen(tresp->rp_realm->buf);
+  tresp->realm->buf = json_string_value(jrealm);
+  tresp->realm->len = strlen(tresp->realm->buf);
+  tresp->comm->buf = json_string_value(jcomm);
+  tresp->comm->len = strlen(tresp->comm->buf);
+
+  /* store optional "orig_coi" field */
+  if ((NULL != (jorig_coi = json_object_get(jresp, "orig_coi"))) &&
+      (!json_is_object(jorig_coi))) {
+    tresp->orig_coi->buf = json_string_value(jorig_coi);
+    tresp->orig_coi->len = strlen(tresp->orig_coi->buf);
+  }
 
-  return resp;
+  //  Decode server info
+  //  tresp->servers = tr_msg_decode_servers(jservers); 
+  
+  return tresp;
 }
 
 char *tr_msg_encode(TR_MSG *msg) 
@@ -146,15 +294,54 @@ char *tr_msg_encode(TR_MSG *msg)
   return(json_dumps(jmsg, 0));
 }
 
-TR_MSG *tr_msg_decode(char *jmsg)
+TR_MSG *tr_msg_decode(char *jbuf, size_t buflen)
 {
   TR_MSG *msg;
+  json_t *jmsg = NULL;
+  json_error_t rc;
+  size_t msize;
+  json_t *jtype;
+  json_t *jbody;
+  const char *mtype = NULL;
+
+  if (NULL == (jmsg = json_loadb(jbuf, buflen, 0, &rc))) {
+    fprintf (stderr, "tr_msg_decode(): error loading object, rc = %d.\n", rc);
+    return NULL;
+  }
 
-  if (!(msg = malloc(sizeof(TR_MSG *)))) {
+  if (!(msg = malloc(sizeof(TR_MSG)))) {
     fprintf (stderr, "tr_msg_decode(): Error allocating TR_MSG structure.\n");
+    json_decref(jmsg);
+    return NULL;
+  }
+  memset(msg, 0, sizeof(TR_MSG));
+
+  if ((2 != (msize = json_object_size(jmsg))) ||
+      (NULL == (jtype = json_object_get(jmsg, "msg_type"))) ||
+      (!json_is_string(jtype)) ||
+      (NULL == (jbody = json_object_get(jmsg, "msg_body"))) ||
+      (!json_is_object(jbody))) {
+    fprintf (stderr, "tr_msg_decode(): Error parsing message.\n");
+    json_decref(jmsg);
+    tr_msg_free_decoded(msg);
     return NULL;
   }
 
+  mtype = json_string_value(jtype);
+
+  if (0 == strcmp(mtype, "TIDRequest")) {
+    msg->msg_type = TID_REQUEST;
+    msg->tid_req = tr_msg_decode_tidreq(jbody);
+  }
+  else if (0 == strcmp(mtype, "TIDResponse")) {
+    msg->msg_type = TID_RESPONSE;
+    msg->tid_resp = tr_msg_decode_tidresp(jbody);
+  }
+  else {
+    msg->msg_type = TR_UNKNOWN;
+    msg->tid_req = NULL;
+  }
   return msg;
 }
 
index 76bca44..69aa32a 100644 (file)
@@ -48,18 +48,28 @@ typedef struct tid_req {
   int conn;
   TR_NAME *rp_realm;
   TR_NAME *realm;
-  TR_NAME *coi;
-  DH *tidc_dh;         /* Client's public dh information */
+  TR_NAME *comm;
+  TR_NAME *orig_coi;
+  DH *tidc_dh;                 /* Client's public dh information */
   void *resp_func;
   void *cookie;
 } TID_REQ;
 
-typedef struct tid_resp {
-  TR_NAME *realm;
-  TR_NAME *coi;
+typedef struct tid_srvr_blk {
+  struct tid_srvr_blk *next;
   in_addr_t aaa_server_addr;
   DH *aaa_server_dh;           /* AAA server's public dh information */
-  /* Trust Path Used */
+} TID_SRVR_BLK;
+  
+
+typedef struct tid_resp {
+  TR_NAME *result;
+  TR_NAME *rp_realm;
+  TR_NAME *realm;
+  TR_NAME *comm;
+  TR_NAME *orig_coi;
+  TID_SRVR_BLK *servers;               /* Linked list of servers */
+  /* TBD -- Trust Path Used */
 } TID_RESP;
 
 typedef struct tidc_instance {
index 5c0583d..c30fe23 100644 (file)
@@ -39,7 +39,7 @@
 #include <jansson.h>
 
 enum msg_type {
-  TR_UNKNOWN,
+  TR_UNKNOWN = 0,
   TID_REQUEST,
   TID_RESPONSE
 };
@@ -54,7 +54,7 @@ typedef struct tr_msg {
 } TR_MSG;
 
 char *tr_msg_encode(TR_MSG *msg);
-TR_MSG *tr_msg_decode(char *jmsg);
+TR_MSG *tr_msg_decode(char *jmsg, size_t len);
 void tr_msg_free_encoded(char *jmsg);
 void tr_msg_free_decoded(TR_MSG *msg);
 
index c10452f..3963233 100644 (file)
@@ -41,12 +41,12 @@ int tids_req_handler (TIDS_INSTANCE * tids,
                      TID_RESP *resp,
                      void *cookie)
 {
-  printf("Request received! Realm = %s, COI = %s\n", req->realm->buf, req->coi->buf);
+  printf("Request received! Realm = %s, Comm = %s\n", req->realm->buf, req->comm->buf);
   if (tids)
     tids->req_count++;
 
   if ((NULL == (resp->realm = tr_dup_name(req->realm))) ||
-      (NULL == (resp->coi = tr_dup_name(req->coi)))) {
+      (NULL == (resp->comm = tr_dup_name(req->comm)))) {
     printf ("Error in tid_dup_name, not responding.\n");
     return 1;
   }
index 352baaf..deff4ed 100644 (file)
@@ -100,7 +100,7 @@ int tidc_send_request (TIDC_INSTANCE *tidc,
                       gss_ctx_id_t gssctx,
                       char *rp_realm,
                       char *realm, 
-                      char *coi,
+                      char *comm,
                       TIDC_RESP_FUNC *resp_handler,
                       void *cookie)
 
@@ -129,7 +129,7 @@ int tidc_send_request (TIDC_INSTANCE *tidc,
   /* TBD -- error handling */
   tid_req->rp_realm = tr_new_name(rp_realm);
   tid_req->realm = tr_new_name(realm);
-  tid_req->coi = tr_new_name(coi);
+  tid_req->comm = tr_new_name(comm);
 
   tid_req->tidc_dh = tidc->priv_dh;
   
index a595d38..8625383 100644 (file)
@@ -41,12 +41,12 @@ int tids_req_handler (TIDS_INSTANCE * tids,
                      TID_RESP *resp,
                      void *cookie)
 {
-  printf("Request received! Realm = %s, COI = %s\n", req->realm->buf, req->coi->buf);
+  printf("Request received! Realm = %s, Comm = %s\n", req->realm->buf, req->comm->buf);
   if (tids)
     tids->req_count++;
 
   if ((NULL == (resp->realm = tr_dup_name(req->realm))) ||
-      (NULL == (resp->coi = tr_dup_name(req->coi)))) {
+      (NULL == (resp->comm = tr_dup_name(req->comm)))) {
     printf ("Error in tid_dup_name, not responding.\n");
     return 1;
   }