2 * Copyright (c) 2012-2014 , JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
38 #include <openssl/dh.h>
47 #include <trust_router/tr_name.h>
48 #include <trp_internal.h>
49 #include <trust_router/tr_constraint.h>
53 /* Read attribute attr from msg as an integer. Returns nonzero on error. */
54 static int tr_msg_get_json_integer(json_t *jmsg, const char *attr, int *dest)
58 obj=json_object_get(jmsg, attr);
63 if (!json_is_integer(obj)) {
67 (*dest)=json_integer_value(obj);
71 /* Read attribute attr from msg as a string. Copies string into mem_ctx context so jmsg can
72 * be destroyed safely. Returns nonzero on error. */
73 static TRP_RC tr_msg_get_json_string(json_t *jmsg, const char *attr, char **dest, TALLOC_CTX *mem_ctx)
77 obj=json_object_get(jmsg, attr);
82 if (!json_is_string(obj))
85 *dest=talloc_strdup(mem_ctx, json_string_value(obj));
92 enum msg_type tr_msg_get_msg_type(TR_MSG *msg)
97 void tr_msg_set_msg_type(TR_MSG *msg, enum msg_type type)
102 TID_REQ *tr_msg_get_req(TR_MSG *msg)
104 if (msg->msg_type == TID_REQUEST)
105 return (TID_REQ *)msg->msg_rep;
109 void tr_msg_set_req(TR_MSG *msg, TID_REQ *req)
112 msg->msg_type = TID_REQUEST;
115 TID_RESP *tr_msg_get_resp(TR_MSG *msg)
117 if (msg->msg_type == TID_RESPONSE)
118 return (TID_RESP *)msg->msg_rep;
122 void tr_msg_set_resp(TR_MSG *msg, TID_RESP *resp)
125 msg->msg_type = TID_RESPONSE;
128 TRP_UPD *tr_msg_get_trp_upd(TR_MSG *msg)
130 if (msg->msg_type == TRP_UPDATE)
131 return (TRP_UPD *)msg->msg_rep;
135 void tr_msg_set_trp_upd(TR_MSG *msg, TRP_UPD *update)
138 talloc_steal(NULL, update); /* should attach to msg, but TR_MSG not usually talloc'ed */
139 msg->msg_type=TRP_UPDATE;
142 TRP_REQ *tr_msg_get_trp_req(TR_MSG *msg)
144 if (msg->msg_type == TRP_REQUEST)
145 return (TRP_REQ *)msg->msg_rep;
149 void tr_msg_set_trp_req(TR_MSG *msg, TRP_REQ *req)
152 msg->msg_type=TRP_REQUEST;
155 static json_t *tr_msg_encode_dh(DH *dh)
160 if ((!dh) || (!dh->p) || (!dh->g) || (!dh->pub_key))
165 jbn = json_string(BN_bn2hex(dh->p));
166 json_object_set_new(jdh, "dh_p", jbn);
168 jbn = json_string(BN_bn2hex(dh->g));
169 json_object_set_new(jdh, "dh_g", jbn);
171 jbn = json_string(BN_bn2hex(dh->pub_key));
172 json_object_set_new(jdh, "dh_pub_key", jbn);
177 static DH *tr_msg_decode_dh(json_t *jdh)
182 json_t *jpub_key = NULL;
184 if (!(dh = malloc(sizeof(DH)))) {
185 tr_crit("tr_msg_decode_dh(): Error allocating DH structure.");
189 memset(dh, 0, sizeof(DH));
191 /* store required fields from dh object */
192 if ((NULL == (jp = json_object_get(jdh, "dh_p"))) ||
193 (NULL == (jg = json_object_get(jdh, "dh_g"))) ||
194 (NULL == (jpub_key = json_object_get(jdh, "dh_pub_key")))) {
195 tr_debug("tr_msg_decode_dh(): Error parsing dh_info.");
200 BN_hex2bn(&(dh->p), json_string_value(jp));
201 BN_hex2bn(&(dh->g), json_string_value(jg));
202 BN_hex2bn(&(dh->pub_key), json_string_value(jpub_key));
207 static json_t * tr_msg_encode_tidreq(TID_REQ *req)
212 if ((!req) || (!req->rp_realm) || (!req->realm) || !(req->comm))
215 assert(jreq = json_object());
217 jstr = json_string(req->rp_realm->buf);
218 json_object_set_new(jreq, "rp_realm", jstr);
220 jstr = json_string(req->realm->buf);
221 json_object_set_new(jreq, "target_realm", jstr);
223 jstr = json_string(req->comm->buf);
224 json_object_set_new(jreq, "community", jstr);
227 jstr = json_string(req->orig_coi->buf);
228 json_object_set_new(jreq, "orig_coi", jstr);
231 json_object_set_new(jreq, "dh_info", tr_msg_encode_dh(req->tidc_dh));
234 json_object_set(jreq, "constraints", (json_t *) req->cons);
237 json_object_set(jreq, "path", req->path);
238 if (req->expiration_interval)
239 json_object_set_new(jreq, "expiration_interval",
240 json_integer(req->expiration_interval));
245 static TID_REQ *tr_msg_decode_tidreq(json_t *jreq)
247 TID_REQ *treq = NULL;
248 json_t *jrp_realm = NULL;
249 json_t *jrealm = NULL;
250 json_t *jcomm = NULL;
251 json_t *jorig_coi = NULL;
253 json_t *jpath = NULL;
254 json_t *jexpire_interval = NULL;
256 if (!(treq =tid_req_new())) {
257 tr_crit("tr_msg_decode_tidreq(): Error allocating TID_REQ structure.");
261 /* store required fields from request */
262 if ((NULL == (jrp_realm = json_object_get(jreq, "rp_realm"))) ||
263 (NULL == (jrealm = json_object_get(jreq, "target_realm"))) ||
264 (NULL == (jcomm = json_object_get(jreq, "community")))) {
265 tr_notice("tr_msg_decode(): Error parsing required fields.");
270 jpath = json_object_get(jreq, "path");
271 jexpire_interval = json_object_get(jreq, "expiration_interval");
273 treq->rp_realm = tr_new_name((char *)json_string_value(jrp_realm));
274 treq->realm = tr_new_name((char *)json_string_value(jrealm));
275 treq->comm = tr_new_name((char *)json_string_value(jcomm));
277 /* Get DH Info from the request */
278 if (NULL == (jdh = json_object_get(jreq, "dh_info"))) {
279 tr_debug("tr_msg_decode(): Error parsing dh_info.");
283 treq->tidc_dh = tr_msg_decode_dh(jdh);
285 /* store optional "orig_coi" field */
286 if (NULL != (jorig_coi = json_object_get(jreq, "orig_coi"))) {
287 treq->orig_coi = tr_new_name((char *)json_string_value(jorig_coi));
290 treq->cons = (TR_CONSTRAINT_SET *) json_object_get(jreq, "constraints");
292 if (!tr_constraint_set_validate(treq->cons)) {
293 tr_debug("Constraint set validation failed");
297 json_incref((json_t *) treq->cons);
298 tid_req_cleanup_json(treq, (json_t *) treq->cons);
303 tid_req_cleanup_json(treq, jpath);
305 if (jexpire_interval)
306 treq->expiration_interval = json_integer_value(jexpire_interval);
311 static json_t *tr_msg_encode_one_server(TID_SRVR_BLK *srvr)
313 json_t *jsrvr = NULL;
315 gchar *time_str = g_time_val_to_iso8601(&srvr->key_expiration);
317 tr_debug("Encoding one server.");
319 jsrvr = json_object();
321 jstr = json_string(srvr->aaa_server_addr);
322 json_object_set_new(jsrvr, "server_addr", jstr);
324 json_object_set_new(jsrvr,
325 "key_expiration", json_string(time_str));
327 /* Server DH Block */
328 jstr = json_string(srvr->key_name->buf);
329 json_object_set_new(jsrvr, "key_name", jstr);
330 json_object_set_new(jsrvr, "server_dh", tr_msg_encode_dh(srvr->aaa_server_dh));
332 /* The path is owned by the srvr, so grab an extra ref*/
333 json_object_set(jsrvr, "path", srvr->path);
337 static int tr_msg_decode_one_server(json_t *jsrvr, TID_SRVR_BLK *srvr)
339 json_t *jsrvr_addr = NULL;
340 json_t *jsrvr_kn = NULL;
341 json_t *jsrvr_dh = NULL;
342 json_t *jsrvr_expire = NULL;
348 if ((NULL == (jsrvr_addr = json_object_get(jsrvr, "server_addr"))) ||
349 (NULL == (jsrvr_kn = json_object_get(jsrvr, "key_name"))) ||
350 (NULL == (jsrvr_dh = json_object_get(jsrvr, "server_dh")))) {
351 tr_notice("tr_msg_decode_one_server(): Error parsing required fields.");
355 srvr->aaa_server_addr=talloc_strdup(srvr, json_string_value(jsrvr_addr));
356 srvr->key_name = tr_new_name((char *)json_string_value(jsrvr_kn));
357 srvr->aaa_server_dh = tr_msg_decode_dh(jsrvr_dh);
358 srvr->path = json_object_get(jsrvr, "path");
359 jsrvr_expire = json_object_get(jsrvr, "key_expiration");
360 if (jsrvr_expire && json_is_string(jsrvr_expire)) {
361 if (!g_time_val_from_iso8601(json_string_value(jsrvr_expire),
362 &srvr->key_expiration))
363 tr_notice("Key expiration %s cannot be parsed", json_string_value(jsrvr_expire));
369 static json_t *tr_msg_encode_servers(TID_RESP *resp)
371 json_t *jservers = NULL;
372 json_t *jsrvr = NULL;
373 TID_SRVR_BLK *srvr = NULL;
376 jservers = json_array();
378 tid_resp_servers_foreach(resp, srvr, index) {
379 if ((NULL == (jsrvr = tr_msg_encode_one_server(srvr))) ||
380 (-1 == json_array_append_new(jservers, jsrvr))) {
385 // tr_debug("tr_msg_encode_servers(): servers contains:");
386 // tr_debug("%s", json_dumps(jservers, 0));
390 static TID_SRVR_BLK *tr_msg_decode_servers(TALLOC_CTX *ctx, json_t *jservers, size_t *out_len)
392 TID_SRVR_BLK *servers = NULL;
394 size_t i, num_servers;
396 num_servers = json_array_size(jservers);
397 tr_debug("tr_msg_decode_servers(): Number of servers = %u.", (unsigned) num_servers);
399 if (0 == num_servers) {
400 tr_debug("tr_msg_decode_servers(): Server array is empty.");
403 servers = talloc_zero_array(ctx, TID_SRVR_BLK, num_servers);
405 for (i = 0; i < num_servers; i++) {
406 jsrvr = json_array_get(jservers, i);
407 if (0 != tr_msg_decode_one_server(jsrvr, &servers[i])) {
408 talloc_free(servers);
414 *out_len = num_servers;
418 static json_t * tr_msg_encode_tidresp(TID_RESP *resp)
420 json_t *jresp = NULL;
422 json_t *jservers = NULL;
424 if ((!resp) || (!resp->rp_realm) || (!resp->realm) || !(resp->comm))
427 jresp = json_object();
429 if (TID_ERROR == resp->result) {
430 jstr = json_string("error");
431 json_object_set_new(jresp, "result", jstr);
433 jstr = json_string(resp->err_msg->buf);
434 json_object_set_new(jresp, "err_msg", jstr);
438 jstr = json_string("success");
439 json_object_set_new(jresp, "result", jstr);
442 jstr = json_string(resp->rp_realm->buf);
443 json_object_set_new(jresp, "rp_realm", jstr);
445 jstr = json_string(resp->realm->buf);
446 json_object_set_new(jresp, "target_realm", jstr);
448 jstr = json_string(resp->comm->buf);
449 json_object_set_new(jresp, "comm", jstr);
451 if (resp->orig_coi) {
452 jstr = json_string(resp->orig_coi->buf);
453 json_object_set_new(jresp, "orig_coi", jstr);
456 if (NULL == resp->servers) {
457 tr_debug("tr_msg_encode_tidresp(): No servers to encode.");
460 jservers = tr_msg_encode_servers(resp);
461 json_object_set_new(jresp, "servers", jservers);
463 if (resp->error_path)
464 json_object_set(jresp, "error_path", resp->error_path);
470 static TID_RESP *tr_msg_decode_tidresp(json_t *jresp)
472 TID_RESP *tresp = NULL;
473 json_t *jresult = NULL;
474 json_t *jrp_realm = NULL;
475 json_t *jrealm = NULL;
476 json_t *jcomm = NULL;
477 json_t *jorig_coi = NULL;
478 json_t *jservers = NULL;
479 json_t *jerr_msg = NULL;
481 if (!(tresp=tid_resp_new(NULL))) {
482 tr_crit("tr_msg_decode_tidresp(): Error allocating TID_RESP structure.");
487 /* store required fields from response */
488 if ((NULL == (jresult = json_object_get(jresp, "result"))) ||
489 (!json_is_string(jresult)) ||
490 (NULL == (jrp_realm = json_object_get(jresp, "rp_realm"))) ||
491 (!json_is_string(jrp_realm)) ||
492 (NULL == (jrealm = json_object_get(jresp, "target_realm"))) ||
493 (!json_is_string(jrealm)) ||
494 (NULL == (jcomm = json_object_get(jresp, "comm"))) ||
495 (!json_is_string(jcomm))) {
496 tr_debug("tr_msg_decode_tidresp(): Error parsing response.");
501 if (0 == (strcmp(json_string_value(jresult), "success"))) {
502 tr_debug("tr_msg_decode_tidresp(): Success! result = %s.", json_string_value(jresult));
503 if ((NULL != (jservers = json_object_get(jresp, "servers"))) ||
504 (!json_is_array(jservers))) {
505 tresp->servers = tr_msg_decode_servers(tresp, jservers, &tresp->num_servers);
511 tresp->result = TID_SUCCESS;
514 tresp->result = TID_ERROR;
515 tr_debug("tr_msg_decode_tidresp(): Error! result = %s.", json_string_value(jresult));
516 if ((NULL != (jerr_msg = json_object_get(jresp, "err_msg"))) ||
517 (!json_is_string(jerr_msg))) {
518 tresp->err_msg = tr_new_name((char *)json_string_value(jerr_msg));
522 tresp->rp_realm = tr_new_name((char *)json_string_value(jrp_realm));
523 tresp->realm = tr_new_name((char *)json_string_value(jrealm));
524 tresp->comm = tr_new_name((char *)json_string_value(jcomm));
526 /* store optional "orig_coi" field */
527 if ((NULL != (jorig_coi = json_object_get(jresp, "orig_coi"))) &&
528 (!json_is_object(jorig_coi))) {
529 tresp->orig_coi = tr_new_name((char *)json_string_value(jorig_coi));
536 /* Information records for TRP update msg
537 * requires that jrec already be allocated */
538 static TRP_RC tr_msg_encode_inforec_route(json_t *jrec, TRP_INFOREC *rec)
547 if (trp_inforec_get_trust_router(rec)==NULL)
550 s=tr_name_strdup(trp_inforec_get_trust_router(rec));
557 json_object_set_new(jrec, "trust_router", jstr);
559 jint=json_integer(trp_inforec_get_metric(rec));
562 json_object_set_new(jrec, "metric", jint);
564 jint=json_integer(trp_inforec_get_interval(rec));
567 json_object_set_new(jrec, "interval", jint);
572 /* returns a json array */
573 static json_t *tr_msg_encode_apcs(TR_APC *apcs)
575 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
576 TR_APC_ITER *iter=tr_apc_iter_new(tmp_ctx);
588 for (apc=tr_apc_iter_first(iter, apcs); apc!=NULL; apc=tr_apc_iter_next(iter)) {
589 jid=tr_name_to_json_string(tr_apc_get_id(apc));
590 if ((jid==NULL) || (json_array_append_new(jarray, jid)!=0)) {
598 talloc_free(tmp_ctx);
602 static TR_APC *tr_msg_decode_apcs(TALLOC_CTX *mem_ctx, json_t *jarray, TRP_RC *rc)
604 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
606 TR_APC *apc_list=NULL;
612 for (ii=0; ii<json_array_size(jarray); ii++) {
613 jstr=json_array_get(jarray, ii);
614 new=tr_apc_new(tmp_ctx);
615 if ((jstr==NULL) || (new==NULL) || (!json_is_string(jstr))) {
616 apc_list=NULL; /* these are all in tmp_ctx, so they'll still get cleaned up */
620 tr_apc_set_id(new, tr_new_name(json_string_value(jstr)));
621 if (tr_apc_get_id(new)==NULL) {
622 apc_list=NULL; /* these are all in tmp_ctx, so they'll still get cleaned up */
626 tr_apc_add(apc_list, new);
632 talloc_steal(mem_ctx, apc_list);
635 talloc_free(tmp_ctx);
639 static TRP_RC tr_msg_encode_inforec_comm(json_t *jrec, TRP_INFOREC *rec)
644 const char *sconst=NULL;
645 TR_COMM_TYPE commtype=TR_COMM_UNKNOWN;
650 commtype=trp_inforec_get_comm_type(rec);
651 if (commtype==TR_COMM_UNKNOWN) {
652 tr_notice("tr_msg_encode_inforec_comm: unknown community type.");
655 sconst=tr_comm_type_to_str(commtype);
658 jstr=json_string(sconst);
661 json_object_set_new(jrec, "type", jstr);
663 sconst=tr_realm_role_to_str(trp_inforec_get_role(rec));
665 tr_notice("tr_msg_encode_inforec_comm: unknown realm role.");
668 jstr=json_string(sconst);
671 json_object_set_new(jrec, "role", jstr);
673 japcs=tr_msg_encode_apcs(trp_inforec_get_apcs(rec));
675 tr_notice("tr_msg_encode_inforec_comm: error encoding APCs.");
678 json_object_set_new(jrec, "apcs", japcs);
681 if (trp_inforec_get_owner_realm(rec)!=NULL) {
682 jstr=tr_name_to_json_string(trp_inforec_get_owner_realm(rec));
685 json_object_set_new(jrec, "owner_realm", jstr);
688 if (trp_inforec_get_owner_contact(rec)!=NULL) {
689 jstr=tr_name_to_json_string(trp_inforec_get_owner_contact(rec));
692 json_object_set_new(jrec, "owner_contact", jstr);
695 json_object_set(jrec, "provenance", trp_inforec_get_provenance(rec));
697 jint=json_integer(trp_inforec_get_interval(rec));
700 json_object_set_new(jrec, "interval", jint);
705 static json_t *tr_msg_encode_inforec(TRP_INFOREC *rec)
710 if ((rec==NULL) || (trp_inforec_get_type(rec)==TRP_INFOREC_TYPE_UNKNOWN))
717 jstr=json_string(trp_inforec_type_to_string(trp_inforec_get_type(rec)));
722 json_object_set_new(jrec, "record_type", jstr);
725 case TRP_INFOREC_TYPE_ROUTE:
726 if (TRP_SUCCESS!=tr_msg_encode_inforec_route(jrec, rec)) {
731 case TRP_INFOREC_TYPE_COMMUNITY:
732 if (TRP_SUCCESS!=tr_msg_encode_inforec_comm(jrec, rec)) {
744 static TRP_RC tr_msg_decode_trp_inforec_route(json_t *jrecord, TRP_INFOREC *rec)
746 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
751 rc=tr_msg_get_json_string(jrecord, "trust_router", &s, tmp_ctx);
752 if (rc != TRP_SUCCESS)
754 if (TRP_SUCCESS!=trp_inforec_set_trust_router(rec, tr_new_name(s))) {
758 talloc_free(s); s=NULL;
760 trp_inforec_set_next_hop(rec, NULL); /* make sure this is null (filled in later) */
762 rc=tr_msg_get_json_integer(jrecord, "metric", &num);
763 if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_metric(rec,num)))
766 rc=tr_msg_get_json_integer(jrecord, "interval", &num);
767 if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_interval(rec,num)))
771 talloc_free(tmp_ctx);
775 static TRP_RC tr_msg_decode_trp_inforec_comm(json_t *jrecord, TRP_INFOREC *rec)
777 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
783 rc=tr_msg_get_json_string(jrecord, "type", &s, tmp_ctx);
784 if (rc != TRP_SUCCESS)
786 if (TRP_SUCCESS!=trp_inforec_set_comm_type(rec, tr_comm_type_from_str(s))) {
790 talloc_free(s); s=NULL;
792 rc=tr_msg_get_json_string(jrecord, "role", &s, tmp_ctx);
793 if (rc != TRP_SUCCESS)
795 if (TRP_SUCCESS!=trp_inforec_set_role(rec, tr_realm_role_from_str(s))) {
799 talloc_free(s); s=NULL;
801 apcs=tr_msg_decode_apcs(rec, json_object_get(jrecord, "apcs"), &rc);
802 if (rc!=TRP_SUCCESS) {
806 trp_inforec_set_apcs(rec, apcs);
808 rc=tr_msg_get_json_integer(jrecord, "interval", &num);
809 tr_debug("tr_msg_decode_trp_inforec_comm: interval=%u", num);
810 if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_interval(rec,num)))
813 trp_inforec_set_provenance(rec, json_object_get(jrecord, "provenance"));
816 rc=tr_msg_get_json_string(jrecord, "owner_realm", &s, tmp_ctx);
817 if (rc == TRP_SUCCESS) {
818 if (TRP_SUCCESS!=trp_inforec_set_owner_realm(rec, tr_new_name(s))) {
828 rc=tr_msg_get_json_string(jrecord, "owner_contact", &s, tmp_ctx);
829 if (rc == TRP_SUCCESS) {
830 if (TRP_SUCCESS!=trp_inforec_set_owner_contact(rec, tr_new_name(s))) {
841 talloc_free(tmp_ctx);
845 /* decode a single record */
846 static TRP_INFOREC *tr_msg_decode_trp_inforec(TALLOC_CTX *mem_ctx, json_t *jrecord)
848 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
849 TRP_INFOREC_TYPE rectype;
850 TRP_INFOREC *rec=NULL;
854 if (TRP_SUCCESS!=tr_msg_get_json_string(jrecord, "record_type", &s, tmp_ctx))
857 rectype=trp_inforec_type_from_string(s);
858 talloc_free(s); s=NULL;
860 rec=trp_inforec_new(tmp_ctx, rectype);
866 tr_debug("tr_msg_decode_trp_inforec: '%s' record found.", trp_inforec_type_to_string(rec->type));
868 switch(trp_inforec_get_type(rec)) {
869 case TRP_INFOREC_TYPE_ROUTE:
870 rc=tr_msg_decode_trp_inforec_route(jrecord, rec);
872 case TRP_INFOREC_TYPE_COMMUNITY:
873 rc=tr_msg_decode_trp_inforec_comm(jrecord, rec);
880 talloc_steal(mem_ctx, rec);
884 if (rc != TRP_SUCCESS) {
885 trp_inforec_free(rec);
888 talloc_free(tmp_ctx);
893 static json_t *tr_msg_encode_trp_upd(TRP_UPD *update)
895 json_t *jupdate=NULL;
896 json_t *jrecords=NULL;
905 jupdate=json_object();
909 s=tr_name_strdup(trp_upd_get_comm(update));
911 json_decref(jupdate);
917 json_decref(jupdate);
920 json_object_set_new(jupdate, "community", jstr);
922 s=tr_name_strdup(trp_upd_get_realm(update));
924 json_decref(jupdate);
930 json_decref(jupdate);
933 json_object_set_new(jupdate, "realm", jstr);
935 jrecords=json_array();
936 if (jrecords==NULL) {
937 json_decref(jupdate);
940 json_object_set_new(jupdate, "records", jrecords); /* jrecords now a "borrowed" reference */
941 for (rec=trp_upd_get_inforec(update); rec!=NULL; rec=trp_inforec_get_next(rec)) {
942 tr_debug("tr_msg_encode_trp_upd: encoding inforec.");
943 jrec=tr_msg_encode_inforec(rec);
945 json_decref(jupdate); /* also decs jrecords and any elements */
948 if (0!=json_array_append_new(jrecords, jrec)) {
949 json_decref(jupdate); /* also decs jrecords and any elements */
950 json_decref(jrec); /* this one did not get added so dec explicitly */
958 /* Creates a linked list of records in the msg->body talloc context.
959 * An error will be returned if any unparseable records are encountered.
961 static TRP_UPD *tr_msg_decode_trp_upd(TALLOC_CTX *mem_ctx, json_t *jupdate)
963 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
964 json_t *jrecords=NULL;
966 TRP_UPD *update=NULL;
967 TRP_INFOREC *new_rec=NULL;
968 TRP_INFOREC *list_tail=NULL;
973 update=trp_upd_new(tmp_ctx);
979 rc=tr_msg_get_json_string(jupdate, "community", &s, tmp_ctx);
980 if (rc != TRP_SUCCESS) {
981 tr_debug("tr_msg_decode_trp_upd: no community in TRP update message.");
987 tr_debug("tr_msg_decode_trp_upd: could not allocate community name.");
991 talloc_free(s); s=NULL;
992 trp_upd_set_comm(update, name);
994 rc=tr_msg_get_json_string(jupdate, "realm", &s, tmp_ctx);
995 if (rc != TRP_SUCCESS) {
996 tr_debug("tr_msg_decode_trp_upd: no realm in TRP update message.");
1000 name=tr_new_name(s);
1002 tr_debug("tr_msg_decode_trp_upd: could not allocate realm name.");
1006 talloc_free(s); s=NULL;
1007 trp_upd_set_realm(update, name);
1009 jrecords=json_object_get(jupdate, "records");
1010 if ((jrecords==NULL) || (!json_is_array(jrecords))) {
1015 tr_debug("tr_msg_decode_trp_upd: found %d records", json_array_size(jrecords));
1016 /* process the array */
1017 for (ii=0; ii<json_array_size(jrecords); ii++) {
1018 new_rec=tr_msg_decode_trp_inforec(update, json_array_get(jrecords, ii));
1019 if (new_rec==NULL) {
1024 if (list_tail==NULL)
1025 trp_upd_set_inforec(update, new_rec); /* first is a special case */
1027 trp_inforec_set_next(list_tail, new_rec);
1032 /* Succeeded. Move new allocations into the correct talloc context */
1033 talloc_steal(mem_ctx, update);
1037 talloc_free(tmp_ctx);
1038 if (rc!=TRP_SUCCESS)
1043 static json_t *tr_msg_encode_trp_req(TRP_REQ *req)
1052 jbody=json_object();
1056 if ((NULL==trp_req_get_comm(req))
1057 || (NULL==trp_req_get_realm(req))) {
1062 s=tr_name_strdup(trp_req_get_comm(req)); /* ensures null termination */
1067 jstr=json_string(s);
1073 json_object_set_new(jbody, "community", jstr);
1075 s=tr_name_strdup(trp_req_get_realm(req)); /* ensures null termination */
1080 jstr=json_string(s);
1086 json_object_set_new(jbody, "realm", jstr);
1091 static TRP_REQ *tr_msg_decode_trp_req(TALLOC_CTX *mem_ctx, json_t *jreq)
1093 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1096 TRP_RC rc=TRP_ERROR;
1098 /* check message type and body type for agreement */
1099 req=trp_req_new(tmp_ctx);
1105 rc=tr_msg_get_json_string(jreq, "community", &s, tmp_ctx);
1106 if (rc!=TRP_SUCCESS)
1108 trp_req_set_comm(req, tr_new_name(s));
1109 talloc_free(s); s=NULL;
1111 rc=tr_msg_get_json_string(jreq, "realm", &s, tmp_ctx);
1112 if (rc!=TRP_SUCCESS)
1114 trp_req_set_realm(req, tr_new_name(s));
1115 talloc_free(s); s=NULL;
1118 talloc_steal(mem_ctx, req);
1121 talloc_free(tmp_ctx);
1122 if (rc!=TRP_SUCCESS)
1127 char *tr_msg_encode(TR_MSG *msg)
1130 json_t *jmsg_type=NULL;
1132 TID_RESP *tidresp=NULL;
1133 TID_REQ *tidreq=NULL;
1134 TRP_UPD *trpupd=NULL;
1135 TRP_REQ *trpreq=NULL;
1137 /* TBD -- add error handling */
1138 jmsg = json_object();
1140 switch (msg->msg_type)
1143 jmsg_type = json_string("tid_request");
1144 json_object_set_new(jmsg, "msg_type", jmsg_type);
1145 tidreq=tr_msg_get_req(msg);
1146 json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidreq(tidreq));
1150 jmsg_type = json_string("tid_response");
1151 json_object_set_new(jmsg, "msg_type", jmsg_type);
1152 tidresp=tr_msg_get_resp(msg);
1153 json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidresp(tidresp));
1157 jmsg_type = json_string("trp_update");
1158 json_object_set_new(jmsg, "msg_type", jmsg_type);
1159 trpupd=tr_msg_get_trp_upd(msg);
1160 json_object_set_new(jmsg, "msg_body", tr_msg_encode_trp_upd(trpupd));
1164 jmsg_type = json_string("trp_request");
1165 json_object_set_new(jmsg, "msg_type", jmsg_type);
1166 trpreq=tr_msg_get_trp_req(msg);
1167 json_object_set_new(jmsg, "msg_body", tr_msg_encode_trp_req(trpreq));
1175 encoded=json_dumps(jmsg, 0);
1176 tr_debug("tr_msg_encode: outgoing msg=%s", encoded);
1181 TR_MSG *tr_msg_decode(char *jbuf, size_t buflen)
1184 json_t *jmsg = NULL;
1188 const char *mtype = NULL;
1190 if (NULL == (jmsg = json_loadb(jbuf, buflen, JSON_DISABLE_EOF_CHECK, &rc))) {
1191 tr_debug("tr_msg_decode(): error loading object");
1195 if (!(msg = malloc(sizeof(TR_MSG)))) {
1196 tr_debug("tr_msg_decode(): Error allocating TR_MSG structure.");
1201 memset(msg, 0, sizeof(TR_MSG));
1203 if ((NULL == (jtype = json_object_get(jmsg, "msg_type"))) ||
1204 (NULL == (jbody = json_object_get(jmsg, "msg_body")))) {
1205 tr_debug("tr_msg_decode(): Error parsing message header.");
1207 tr_msg_free_decoded(msg);
1211 mtype = json_string_value(jtype);
1213 if (0 == strcmp(mtype, "tid_request")) {
1214 msg->msg_type = TID_REQUEST;
1215 tr_msg_set_req(msg, tr_msg_decode_tidreq(jbody));
1217 else if (0 == strcmp(mtype, "tid_response")) {
1218 msg->msg_type = TID_RESPONSE;
1219 tr_msg_set_resp(msg, tr_msg_decode_tidresp(jbody));
1221 else if (0 == strcmp(mtype, "trp_update")) {
1222 msg->msg_type = TRP_UPDATE;
1223 tr_msg_set_trp_upd(msg, tr_msg_decode_trp_upd(NULL, jbody)); /* null talloc context for now */
1225 else if (0 == strcmp(mtype, "trp_request")) {
1226 msg->msg_type = TRP_UPDATE;
1227 tr_msg_set_trp_req(msg, tr_msg_decode_trp_req(NULL, jbody)); /* null talloc context for now */
1230 msg->msg_type = TR_UNKNOWN;
1231 msg->msg_rep = NULL;
1236 void tr_msg_free_encoded(char *jmsg)
1242 void tr_msg_free_decoded(TR_MSG *msg)
1245 switch (msg->msg_type) {
1247 tid_req_free(tr_msg_get_req(msg));
1250 tid_resp_free(tr_msg_get_resp(msg));
1253 trp_upd_free(tr_msg_get_trp_upd(msg));
1256 trp_req_free(tr_msg_get_trp_req(msg));