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>
39 #include <openssl/crypto.h>
46 #include <trp_internal.h>
47 #include <mon_internal.h>
49 #include <tr_name_internal.h>
50 #include <trust_router/tr_constraint.h>
51 #include <trust_router/tr_dh.h>
55 /* Read attribute attr from msg as an integer. Returns nonzero on error. */
56 static int tr_msg_get_json_integer(json_t *jmsg, const char *attr, int *dest)
60 obj=json_object_get(jmsg, attr);
65 if (!json_is_integer(obj)) {
69 (*dest)=json_integer_value(obj);
73 /* Read attribute attr from msg as a string. Copies string into mem_ctx context so jmsg can
74 * be destroyed safely. Returns nonzero on error. */
75 static TRP_RC tr_msg_get_json_string(json_t *jmsg, const char *attr, char **dest, TALLOC_CTX *mem_ctx)
79 obj=json_object_get(jmsg, attr);
84 if (!json_is_string(obj))
87 *dest=talloc_strdup(mem_ctx, json_string_value(obj));
94 enum msg_type tr_msg_get_msg_type(TR_MSG *msg)
99 void tr_msg_set_msg_type(TR_MSG *msg, enum msg_type type)
101 msg->msg_type = type;
104 /* NOTE: If you are manipulating messages with these getters/setters, the msg_rep
105 * objects are *not* put in the talloc context of the msg. If you are allocating
106 * the message directly with talloc, then you can talloc_steal() the rep into the
107 * message's context, but this is not handled automatically. */
110 * Get a TID_REQ message payload
113 * @return the message payload, or null if it is not a TID_REQUEST message
115 TID_REQ *tr_msg_get_req(TR_MSG *msg)
117 if (msg->msg_type == TID_REQUEST)
118 return (TID_REQ *)msg->msg_rep;
123 * Set message's payload
125 * Does not manage talloc contexts, works with any means of allocating
128 void tr_msg_set_req(TR_MSG *msg, TID_REQ *req)
131 msg->msg_type = TID_REQUEST;
135 * Get a TID_RESP message payload
138 * @return the message payload, or null if it is not a TID_RESPONSE message
140 TID_RESP *tr_msg_get_resp(TR_MSG *msg)
142 if (msg->msg_type == TID_RESPONSE)
143 return (TID_RESP *)msg->msg_rep;
148 * Set message's payload
150 * Does not manage talloc contexts, works with any means of allocating
153 void tr_msg_set_resp(TR_MSG *msg, TID_RESP *resp)
156 msg->msg_type = TID_RESPONSE;
160 * Get a MON_REQ message payload
163 * @return the message payload, or null if it is not a MON_REQUEST message
165 MON_REQ *tr_msg_get_mon_req(TR_MSG *msg)
167 if (msg->msg_type == MON_REQUEST)
168 return (MON_REQ *)msg->msg_rep;
173 * Set message's payload
175 * Does not manage talloc contexts, works with any means of allocating
178 void tr_msg_set_mon_req(TR_MSG *msg, MON_REQ *req)
181 msg->msg_type = MON_REQUEST;
185 * Get a MON_RESP message payload
188 * @return the message payload, or null if it is not a MON_RESPONSE message
190 MON_RESP *tr_msg_get_mon_resp(TR_MSG *msg)
192 if (msg->msg_type == MON_RESPONSE)
193 return (MON_RESP *)msg->msg_rep;
198 * Set message's payload
200 * Does not manage talloc contexts, works with any means of allocating
203 void tr_msg_set_mon_resp(TR_MSG *msg, MON_RESP *resp)
206 msg->msg_type = MON_RESPONSE;
210 * Get a TRP_UPD message payload
213 * @return the message payload, or null if it is not a TRP_UPDATE message
215 TRP_UPD *tr_msg_get_trp_upd(TR_MSG *msg)
217 if (msg->msg_type == TRP_UPDATE)
218 return (TRP_UPD *)msg->msg_rep;
223 * Set message's payload
225 * Does not manage talloc contexts, works with any means of allocating
228 void tr_msg_set_trp_upd(TR_MSG *msg, TRP_UPD *update)
231 msg->msg_type=TRP_UPDATE;
235 * Get a TRP_REQ message payload
238 * @return the message payload, or null if it is not a TRP_REQUEST message
240 TRP_REQ *tr_msg_get_trp_req(TR_MSG *msg)
242 if (msg->msg_type == TRP_REQUEST)
243 return (TRP_REQ *)msg->msg_rep;
248 * Set message's payload
250 * Does not manage talloc contexts, works with any means of allocating
253 void tr_msg_set_trp_req(TR_MSG *msg, TRP_REQ *req)
256 msg->msg_type=TRP_REQUEST;
259 static json_t *tr_msg_encode_dh(DH *dh)
265 if ((!dh) || (!dh->p) || (!dh->g) || (!dh->pub_key))
270 jbn = json_string(s=BN_bn2hex(dh->p));
272 json_object_set_new(jdh, "dh_p", jbn);
274 jbn = json_string(s=BN_bn2hex(dh->g));
276 json_object_set_new(jdh, "dh_g", jbn);
278 jbn = json_string(s=BN_bn2hex(dh->pub_key));
280 json_object_set_new(jdh, "dh_pub_key", jbn);
285 static DH *tr_msg_decode_dh(json_t *jdh)
290 json_t *jpub_key = NULL;
292 if (!(dh=tr_dh_new())) {
293 tr_crit("tr_msg_decode_dh(): Error allocating DH structure.");
297 /* store required fields from dh object */
298 if ((NULL == (jp = json_object_get(jdh, "dh_p"))) ||
299 (NULL == (jg = json_object_get(jdh, "dh_g"))) ||
300 (NULL == (jpub_key = json_object_get(jdh, "dh_pub_key")))) {
301 tr_debug("tr_msg_decode_dh(): Error parsing dh_info.");
306 BN_hex2bn(&(dh->p), json_string_value(jp));
307 BN_hex2bn(&(dh->g), json_string_value(jg));
308 BN_hex2bn(&(dh->pub_key), json_string_value(jpub_key));
313 static json_t * tr_msg_encode_tidreq(TID_REQ *req)
318 if ((!req) || (!req->rp_realm) || (!req->realm) || !(req->comm))
321 jreq = json_object();
324 jstr = tr_name_to_json_string(req->rp_realm);
325 json_object_set_new(jreq, "rp_realm", jstr);
327 jstr = tr_name_to_json_string(req->realm);
328 json_object_set_new(jreq, "target_realm", jstr);
330 jstr = tr_name_to_json_string(req->comm);
331 json_object_set_new(jreq, "community", jstr);
334 jstr = tr_name_to_json_string(req->orig_coi);
335 json_object_set_new(jreq, "orig_coi", jstr);
338 if (tid_req_get_request_id(req)) {
339 jstr = tr_name_to_json_string(tid_req_get_request_id(req));
340 json_object_set_new(jreq, "request_id", jstr);
343 json_object_set_new(jreq, "dh_info", tr_msg_encode_dh(req->tidc_dh));
346 json_object_set(jreq, "constraints", (json_t *) req->cons);
349 json_object_set(jreq, "path", req->path);
350 if (req->expiration_interval)
351 json_object_set_new(jreq, "expiration_interval",
352 json_integer(req->expiration_interval));
357 static TID_REQ *tr_msg_decode_tidreq(TALLOC_CTX *mem_ctx, json_t *jreq)
359 TID_REQ *treq = NULL;
360 json_t *jrp_realm = NULL;
361 json_t *jrealm = NULL;
362 json_t *jcomm = NULL;
363 json_t *jorig_coi = NULL;
364 json_t *jrequest_id = NULL;
366 json_t *jpath = NULL;
367 json_t *jexpire_interval = NULL;
369 if (!(treq =tid_req_new())) {
370 tr_crit("tr_msg_decode_tidreq(): Error allocating TID_REQ structure.");
373 talloc_steal(mem_ctx, treq);
375 /* store required fields from request */
376 if ((NULL == (jrp_realm = json_object_get(jreq, "rp_realm"))) ||
377 (NULL == (jrealm = json_object_get(jreq, "target_realm"))) ||
378 (NULL == (jcomm = json_object_get(jreq, "community")))) {
379 tr_notice("tr_msg_decode(): Error parsing required fields.");
384 jpath = json_object_get(jreq, "path");
385 jexpire_interval = json_object_get(jreq, "expiration_interval");
387 treq->rp_realm = tr_new_name(json_string_value(jrp_realm));
388 treq->realm = tr_new_name(json_string_value(jrealm));
389 treq->comm = tr_new_name(json_string_value(jcomm));
391 /* Get DH Info from the request */
392 if (NULL == (jdh = json_object_get(jreq, "dh_info"))) {
393 tr_debug("tr_msg_decode(): Error parsing dh_info.");
397 treq->tidc_dh = tr_msg_decode_dh(jdh);
399 /* store optional "orig_coi" field */
400 if (NULL != (jorig_coi = json_object_get(jreq, "orig_coi"))) {
401 treq->orig_coi = tr_new_name(json_string_value(jorig_coi));
404 /* store optional "request_id" field */
405 if (NULL != (jrequest_id = json_object_get(jreq, "request_id"))) {
406 tid_req_set_request_id(treq, tr_new_name(json_string_value(jrequest_id)));
409 treq->cons = (TR_CONSTRAINT_SET *) json_object_get(jreq, "constraints");
411 if (!tr_constraint_set_validate(treq->cons)) {
412 tr_debug("Constraint set validation failed");
416 json_incref((json_t *) treq->cons);
417 tid_req_cleanup_json(treq, (json_t *) treq->cons);
422 tid_req_cleanup_json(treq, jpath);
424 if (jexpire_interval)
425 treq->expiration_interval = json_integer_value(jexpire_interval);
430 static json_t *tr_msg_encode_one_server(TID_SRVR_BLK *srvr)
432 json_t *jsrvr = NULL;
434 gchar *time_str = g_time_val_to_iso8601(&srvr->key_expiration);
436 tr_debug("Encoding one server.");
438 jsrvr = json_object();
440 jstr = json_string(srvr->aaa_server_addr);
441 json_object_set_new(jsrvr, "server_addr", jstr);
443 json_object_set_new(jsrvr,
444 "key_expiration", json_string(time_str));
446 /* Server DH Block */
447 jstr = tr_name_to_json_string(srvr->key_name);
448 json_object_set_new(jsrvr, "key_name", jstr);
449 json_object_set_new(jsrvr, "server_dh", tr_msg_encode_dh(srvr->aaa_server_dh));
451 /* The path is owned by the srvr, so grab an extra ref*/
452 json_object_set(jsrvr, "path", (json_t *)(srvr->path));
456 static int tr_msg_decode_one_server(json_t *jsrvr, TID_SRVR_BLK *srvr)
458 json_t *jsrvr_addr = NULL;
459 json_t *jsrvr_kn = NULL;
460 json_t *jsrvr_dh = NULL;
461 json_t *jsrvr_expire = NULL;
467 if ((NULL == (jsrvr_addr = json_object_get(jsrvr, "server_addr"))) ||
468 (NULL == (jsrvr_kn = json_object_get(jsrvr, "key_name"))) ||
469 (NULL == (jsrvr_dh = json_object_get(jsrvr, "server_dh")))) {
470 tr_notice("tr_msg_decode_one_server(): Error parsing required fields.");
474 srvr->aaa_server_addr=talloc_strdup(srvr, json_string_value(jsrvr_addr));
475 srvr->key_name = tr_new_name((char *)json_string_value(jsrvr_kn));
476 srvr->aaa_server_dh = tr_msg_decode_dh(jsrvr_dh);
477 tid_srvr_blk_set_path(srvr, (TID_PATH *) json_object_get(jsrvr, "path"));
478 jsrvr_expire = json_object_get(jsrvr, "key_expiration");
479 if (jsrvr_expire && json_is_string(jsrvr_expire)) {
480 if (!g_time_val_from_iso8601(json_string_value(jsrvr_expire),
481 &srvr->key_expiration))
482 tr_notice("Key expiration %s cannot be parsed", json_string_value(jsrvr_expire));
488 static json_t *tr_msg_encode_servers(TID_RESP *resp)
490 json_t *jservers = NULL;
491 json_t *jsrvr = NULL;
492 TID_SRVR_BLK *srvr = NULL;
495 jservers = json_array();
497 tid_resp_servers_foreach(resp, srvr, index) {
498 if ((NULL == (jsrvr = tr_msg_encode_one_server(srvr))) ||
499 (-1 == json_array_append_new(jservers, jsrvr))) {
504 // tr_debug("tr_msg_encode_servers(): servers contains:");
505 // tr_debug("%s", json_dumps(jservers, 0));
509 static TID_SRVR_BLK *tr_msg_decode_servers(TALLOC_CTX *mem_ctx, json_t *jservers)
511 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
512 TID_SRVR_BLK *servers=NULL;
513 TID_SRVR_BLK *new_srvr=NULL;
515 size_t i, num_servers;
517 num_servers = json_array_size(jservers);
518 tr_debug("tr_msg_decode_servers(): Number of servers = %u.", (unsigned) num_servers);
520 if (0 == num_servers) {
521 tr_debug("tr_msg_decode_servers(): Server array is empty.");
525 for (i = 0; i < num_servers; i++) {
526 jsrvr = json_array_get(jservers, i);
528 new_srvr=tid_srvr_blk_new(tmp_ctx);
529 if (new_srvr==NULL) {
530 servers=NULL; /* it's all in tmp_ctx, so we can just let go */
534 if (0 != tr_msg_decode_one_server(jsrvr, new_srvr)) {
535 servers=NULL; /* it's all in tmp_ctx, so we can just let go */
539 tid_srvr_blk_add(servers, new_srvr);
542 talloc_steal(mem_ctx, servers);
545 talloc_free(tmp_ctx);
549 static json_t * tr_msg_encode_tidresp(TID_RESP *resp)
551 json_t *jresp = NULL;
553 json_t *jservers = NULL;
555 if ((!resp) || (!resp->rp_realm) || (!resp->realm) || !(resp->comm))
558 jresp = json_object();
560 if (TID_ERROR == resp->result) {
561 jstr = json_string("error");
562 json_object_set_new(jresp, "result", jstr);
564 jstr = tr_name_to_json_string(resp->err_msg);
565 json_object_set_new(jresp, "err_msg", jstr);
569 jstr = json_string("success");
570 json_object_set_new(jresp, "result", jstr);
573 jstr = tr_name_to_json_string(resp->rp_realm);
574 json_object_set_new(jresp, "rp_realm", jstr);
576 jstr = tr_name_to_json_string(resp->realm);
577 json_object_set_new(jresp, "target_realm", jstr);
579 jstr = tr_name_to_json_string(resp->comm);
580 json_object_set_new(jresp, "comm", jstr);
582 if (resp->orig_coi) {
583 jstr = tr_name_to_json_string(resp->orig_coi);
584 json_object_set_new(jresp, "orig_coi", jstr);
587 if (tid_resp_get_request_id(resp)) {
588 jstr = tr_name_to_json_string(tid_resp_get_request_id(resp));
589 json_object_set_new(jresp, "request_id", jstr);
592 if (NULL == resp->servers) {
593 tr_debug("tr_msg_encode_tidresp(): No servers to encode.");
596 jservers = tr_msg_encode_servers(resp);
597 json_object_set_new(jresp, "servers", jservers);
599 if (resp->error_path)
600 json_object_set(jresp, "error_path", resp->error_path);
606 static TID_RESP *tr_msg_decode_tidresp(TALLOC_CTX *mem_ctx, json_t *jresp)
608 TID_RESP *tresp = NULL;
609 json_t *jresult = NULL;
610 json_t *jrp_realm = NULL;
611 json_t *jrealm = NULL;
612 json_t *jcomm = NULL;
613 json_t *jorig_coi = NULL;
614 json_t *jrequest_id = NULL;
615 json_t *jservers = NULL;
616 json_t *jerr_msg = NULL;
618 if (!(tresp=tid_resp_new(mem_ctx))) {
619 tr_crit("tr_msg_decode_tidresp(): Error allocating TID_RESP structure.");
623 /* store required fields from response */
624 if ((NULL == (jresult = json_object_get(jresp, "result"))) ||
625 (!json_is_string(jresult)) ||
626 (NULL == (jrp_realm = json_object_get(jresp, "rp_realm"))) ||
627 (!json_is_string(jrp_realm)) ||
628 (NULL == (jrealm = json_object_get(jresp, "target_realm"))) ||
629 (!json_is_string(jrealm)) ||
630 (NULL == (jcomm = json_object_get(jresp, "comm"))) ||
631 (!json_is_string(jcomm))) {
632 tr_debug("tr_msg_decode_tidresp(): Error parsing response.");
637 if (0 == (strcmp(json_string_value(jresult), "success"))) {
638 tr_debug("tr_msg_decode_tidresp(): Success! result = %s.", json_string_value(jresult));
639 if ((NULL != (jservers = json_object_get(jresp, "servers"))) ||
640 (!json_is_array(jservers))) {
641 tresp->servers = tr_msg_decode_servers(tresp, jservers);
647 tresp->result = TID_SUCCESS;
650 tresp->result = TID_ERROR;
651 tr_debug("tr_msg_decode_tidresp(): Error! result = %s.", json_string_value(jresult));
652 if ((NULL != (jerr_msg = json_object_get(jresp, "err_msg"))) ||
653 (!json_is_string(jerr_msg))) {
654 tresp->err_msg = tr_new_name(json_string_value(jerr_msg));
656 tresp->err_msg = tr_new_name("No error message set.");
658 if (NULL !=(tresp->error_path = json_object_get(jresp, "error_path")))
659 json_incref(tresp->error_path);
662 tresp->rp_realm = tr_new_name(json_string_value(jrp_realm));
663 tresp->realm = tr_new_name(json_string_value(jrealm));
664 tresp->comm = tr_new_name(json_string_value(jcomm));
666 /* store optional "orig_coi" field */
667 if ((NULL != (jorig_coi = json_object_get(jresp, "orig_coi"))) &&
668 json_is_string(jorig_coi)) {
669 tresp->orig_coi = tr_new_name(json_string_value(jorig_coi));
672 /* store optional "request_id" field */
673 if ((NULL != (jrequest_id = json_object_get(jresp, "request_id"))) &&
674 json_is_string(jrequest_id)) {
675 tid_resp_set_request_id(tresp, tr_new_name(json_string_value(jrequest_id)));
682 /* Information records for TRP update msg
683 * requires that jrec already be allocated */
684 static TRP_RC tr_msg_encode_inforec_route(json_t *jrec, TRP_INFOREC *rec)
693 if (trp_inforec_get_trust_router(rec)==NULL)
696 s=tr_name_strdup(trp_inforec_get_trust_router(rec));
703 json_object_set_new(jrec, "trust_router", jstr);
705 jint=json_integer(trp_inforec_get_metric(rec));
708 json_object_set_new(jrec, "metric", jint);
710 jint=json_integer(trp_inforec_get_interval(rec));
713 json_object_set_new(jrec, "interval", jint);
718 /* returns a json array */
719 static json_t *tr_msg_encode_apcs(TR_APC *apcs)
721 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
722 TR_APC_ITER *iter=tr_apc_iter_new(tmp_ctx);
734 for (apc=tr_apc_iter_first(iter, apcs); apc!=NULL; apc=tr_apc_iter_next(iter)) {
735 jid=tr_name_to_json_string(tr_apc_get_id(apc));
736 if ((jid==NULL) || (json_array_append_new(jarray, jid)!=0)) {
744 talloc_free(tmp_ctx);
748 static TR_APC *tr_msg_decode_apcs(TALLOC_CTX *mem_ctx, json_t *jarray, TRP_RC *rc)
750 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
752 TR_APC *apc_list=NULL;
758 for (ii=0; ii<json_array_size(jarray); ii++) {
759 jstr=json_array_get(jarray, ii);
760 new=tr_apc_new(tmp_ctx);
761 if ((jstr==NULL) || (new==NULL) || (!json_is_string(jstr))) {
762 apc_list=NULL; /* these are all in tmp_ctx, so they'll still get cleaned up */
766 tr_apc_set_id(new, tr_new_name(json_string_value(jstr)));
767 if (tr_apc_get_id(new)==NULL) {
768 apc_list=NULL; /* these are all in tmp_ctx, so they'll still get cleaned up */
772 tr_apc_add(apc_list, new);
778 talloc_steal(mem_ctx, apc_list);
781 talloc_free(tmp_ctx);
785 static TRP_RC tr_msg_encode_inforec_comm(json_t *jrec, TRP_INFOREC *rec)
790 const char *sconst=NULL;
791 TR_COMM_TYPE commtype=TR_COMM_UNKNOWN;
796 commtype=trp_inforec_get_comm_type(rec);
797 if (commtype==TR_COMM_UNKNOWN) {
798 tr_notice("tr_msg_encode_inforec_comm: unknown community type.");
801 sconst=tr_comm_type_to_str(commtype);
804 jstr=json_string(sconst);
807 json_object_set_new(jrec, "type", jstr);
809 sconst=tr_realm_role_to_str(trp_inforec_get_role(rec));
811 tr_notice("tr_msg_encode_inforec_comm: unknown realm role.");
814 jstr=json_string(sconst);
817 json_object_set_new(jrec, "role", jstr);
819 japcs=tr_msg_encode_apcs(trp_inforec_get_apcs(rec));
821 tr_notice("tr_msg_encode_inforec_comm: error encoding APCs.");
824 json_object_set_new(jrec, "apcs", japcs);
827 if (trp_inforec_get_owner_realm(rec)!=NULL) {
828 jstr=tr_name_to_json_string(trp_inforec_get_owner_realm(rec));
831 json_object_set_new(jrec, "owner_realm", jstr);
834 if (trp_inforec_get_owner_contact(rec)!=NULL) {
835 jstr=tr_name_to_json_string(trp_inforec_get_owner_contact(rec));
838 json_object_set_new(jrec, "owner_contact", jstr);
841 json_object_set(jrec, "provenance", trp_inforec_get_provenance(rec));
843 jint=json_integer(trp_inforec_get_interval(rec));
846 json_object_set_new(jrec, "interval", jint);
851 static json_t *tr_msg_encode_inforec(TRP_INFOREC *rec)
856 if ((rec==NULL) || (trp_inforec_get_type(rec)==TRP_INFOREC_TYPE_UNKNOWN))
863 jstr=json_string(trp_inforec_type_to_string(trp_inforec_get_type(rec)));
868 json_object_set_new(jrec, "record_type", jstr);
871 case TRP_INFOREC_TYPE_ROUTE:
872 if (TRP_SUCCESS!=tr_msg_encode_inforec_route(jrec, rec)) {
877 case TRP_INFOREC_TYPE_COMMUNITY:
878 if (TRP_SUCCESS!=tr_msg_encode_inforec_comm(jrec, rec)) {
890 static TRP_RC tr_msg_decode_trp_inforec_route(json_t *jrecord, TRP_INFOREC *rec)
892 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
897 rc=tr_msg_get_json_string(jrecord, "trust_router", &s, tmp_ctx);
898 if (rc != TRP_SUCCESS)
900 if (TRP_SUCCESS!=trp_inforec_set_trust_router(rec, tr_new_name(s))) {
904 talloc_free(s); s=NULL;
906 trp_inforec_set_next_hop(rec, NULL); /* make sure this is null (filled in later) */
908 rc=tr_msg_get_json_integer(jrecord, "metric", &num);
909 if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_metric(rec,num)))
912 rc=tr_msg_get_json_integer(jrecord, "interval", &num);
913 if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_interval(rec,num)))
917 talloc_free(tmp_ctx);
921 static TRP_RC tr_msg_decode_trp_inforec_comm(json_t *jrecord, TRP_INFOREC *rec)
923 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
929 rc=tr_msg_get_json_string(jrecord, "type", &s, tmp_ctx);
930 if (rc != TRP_SUCCESS)
932 if (TRP_SUCCESS!=trp_inforec_set_comm_type(rec, tr_comm_type_from_str(s))) {
936 talloc_free(s); s=NULL;
938 rc=tr_msg_get_json_string(jrecord, "role", &s, tmp_ctx);
939 if (rc != TRP_SUCCESS)
941 if (TRP_SUCCESS!=trp_inforec_set_role(rec, tr_realm_role_from_str(s))) {
945 talloc_free(s); s=NULL;
947 apcs=tr_msg_decode_apcs(rec, json_object_get(jrecord, "apcs"), &rc);
948 if (rc!=TRP_SUCCESS) {
952 trp_inforec_set_apcs(rec, apcs);
954 rc=tr_msg_get_json_integer(jrecord, "interval", &num);
955 tr_debug("tr_msg_decode_trp_inforec_comm: interval=%u", num);
956 if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_interval(rec,num)))
959 trp_inforec_set_provenance(rec, json_object_get(jrecord, "provenance"));
962 rc=tr_msg_get_json_string(jrecord, "owner_realm", &s, tmp_ctx);
963 if (rc == TRP_SUCCESS) {
964 if (TRP_SUCCESS!=trp_inforec_set_owner_realm(rec, tr_new_name(s))) {
974 rc=tr_msg_get_json_string(jrecord, "owner_contact", &s, tmp_ctx);
975 if (rc == TRP_SUCCESS) {
976 if (TRP_SUCCESS!=trp_inforec_set_owner_contact(rec, tr_new_name(s))) {
987 talloc_free(tmp_ctx);
991 /* decode a single record */
992 static TRP_INFOREC *tr_msg_decode_trp_inforec(TALLOC_CTX *mem_ctx, json_t *jrecord)
994 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
995 TRP_INFOREC_TYPE rectype;
996 TRP_INFOREC *rec=NULL;
1000 if (TRP_SUCCESS!=tr_msg_get_json_string(jrecord, "record_type", &s, tmp_ctx))
1003 rectype=trp_inforec_type_from_string(s);
1004 talloc_free(s); s=NULL;
1006 rec=trp_inforec_new(tmp_ctx, rectype);
1012 tr_debug("tr_msg_decode_trp_inforec: '%s' record found.", trp_inforec_type_to_string(rec->type));
1014 switch(trp_inforec_get_type(rec)) {
1015 case TRP_INFOREC_TYPE_ROUTE:
1016 rc=tr_msg_decode_trp_inforec_route(jrecord, rec);
1018 case TRP_INFOREC_TYPE_COMMUNITY:
1019 rc=tr_msg_decode_trp_inforec_comm(jrecord, rec);
1026 talloc_steal(mem_ctx, rec);
1030 if (rc != TRP_SUCCESS) {
1031 trp_inforec_free(rec);
1034 talloc_free(tmp_ctx);
1038 /* TRP update msg */
1039 static json_t *tr_msg_encode_trp_upd(TRP_UPD *update)
1041 json_t *jupdate=NULL;
1042 json_t *jrecords=NULL;
1051 jupdate=json_object();
1055 s=tr_name_strdup(trp_upd_get_comm(update));
1057 json_decref(jupdate);
1060 jstr=json_string(s);
1063 json_decref(jupdate);
1066 json_object_set_new(jupdate, "community", jstr);
1068 s=tr_name_strdup(trp_upd_get_realm(update));
1070 json_decref(jupdate);
1073 jstr=json_string(s);
1076 json_decref(jupdate);
1079 json_object_set_new(jupdate, "realm", jstr);
1081 jrecords=json_array();
1082 if (jrecords==NULL) {
1083 json_decref(jupdate);
1086 json_object_set_new(jupdate, "records", jrecords); /* jrecords now a "borrowed" reference */
1087 for (rec=trp_upd_get_inforec(update); rec!=NULL; rec=trp_inforec_get_next(rec)) {
1088 tr_debug("tr_msg_encode_trp_upd: encoding inforec.");
1089 jrec=tr_msg_encode_inforec(rec);
1091 json_decref(jupdate); /* also decs jrecords and any elements */
1094 if (0!=json_array_append_new(jrecords, jrec)) {
1095 json_decref(jupdate); /* also decs jrecords and any elements */
1096 json_decref(jrec); /* this one did not get added so dec explicitly */
1104 /* Creates a linked list of records in the msg->body talloc context.
1105 * An error will be returned if any unparseable records are encountered.
1107 static TRP_UPD *tr_msg_decode_trp_upd(TALLOC_CTX *mem_ctx, json_t *jupdate)
1109 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1110 json_t *jrecords=NULL;
1112 TRP_UPD *update=NULL;
1113 TRP_INFOREC *new_rec=NULL;
1114 TRP_INFOREC *list_tail=NULL;
1117 TRP_RC rc=TRP_ERROR;
1119 update=trp_upd_new(tmp_ctx);
1125 rc=tr_msg_get_json_string(jupdate, "community", &s, tmp_ctx);
1126 if (rc != TRP_SUCCESS) {
1127 tr_debug("tr_msg_decode_trp_upd: no community in TRP update message.");
1131 name=tr_new_name(s);
1133 tr_debug("tr_msg_decode_trp_upd: could not allocate community name.");
1137 talloc_free(s); s=NULL;
1138 trp_upd_set_comm(update, name);
1140 rc=tr_msg_get_json_string(jupdate, "realm", &s, tmp_ctx);
1141 if (rc != TRP_SUCCESS) {
1142 tr_debug("tr_msg_decode_trp_upd: no realm in TRP update message.");
1146 name=tr_new_name(s);
1148 tr_debug("tr_msg_decode_trp_upd: could not allocate realm name.");
1152 talloc_free(s); s=NULL;
1153 trp_upd_set_realm(update, name);
1155 jrecords=json_object_get(jupdate, "records");
1156 if ((jrecords==NULL) || (!json_is_array(jrecords))) {
1161 tr_debug("tr_msg_decode_trp_upd: found %d records", json_array_size(jrecords));
1162 /* process the array */
1163 for (ii=0; ii<json_array_size(jrecords); ii++) {
1164 new_rec=tr_msg_decode_trp_inforec(update, json_array_get(jrecords, ii));
1165 if (new_rec==NULL) {
1170 if (list_tail==NULL)
1171 trp_upd_set_inforec(update, new_rec); /* first is a special case */
1173 trp_inforec_set_next(list_tail, new_rec);
1178 /* Succeeded. Move new allocations into the correct talloc context */
1179 talloc_steal(mem_ctx, update);
1183 talloc_free(tmp_ctx);
1184 if (rc!=TRP_SUCCESS)
1189 static json_t *tr_msg_encode_trp_req(TRP_REQ *req)
1198 jbody=json_object();
1202 if ((NULL==trp_req_get_comm(req))
1203 || (NULL==trp_req_get_realm(req))) {
1208 s=tr_name_strdup(trp_req_get_comm(req)); /* ensures null termination */
1213 jstr=json_string(s);
1219 json_object_set_new(jbody, "community", jstr);
1221 s=tr_name_strdup(trp_req_get_realm(req)); /* ensures null termination */
1226 jstr=json_string(s);
1232 json_object_set_new(jbody, "realm", jstr);
1237 static TRP_REQ *tr_msg_decode_trp_req(TALLOC_CTX *mem_ctx, json_t *jreq)
1239 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1242 TRP_RC rc=TRP_ERROR;
1244 /* check message type and body type for agreement */
1245 req=trp_req_new(tmp_ctx);
1251 rc=tr_msg_get_json_string(jreq, "community", &s, tmp_ctx);
1252 if (rc!=TRP_SUCCESS)
1254 trp_req_set_comm(req, tr_new_name(s));
1255 talloc_free(s); s=NULL;
1257 rc=tr_msg_get_json_string(jreq, "realm", &s, tmp_ctx);
1258 if (rc!=TRP_SUCCESS)
1260 trp_req_set_realm(req, tr_new_name(s));
1261 talloc_free(s); s=NULL;
1264 talloc_steal(mem_ctx, req);
1267 talloc_free(tmp_ctx);
1268 if (rc!=TRP_SUCCESS)
1273 char *tr_msg_encode(TALLOC_CTX *mem_ctx, TR_MSG *msg)
1276 json_t *jmsg_type=NULL;
1277 char *encoded_tmp=NULL;
1279 TID_RESP *tidresp=NULL;
1280 TID_REQ *tidreq=NULL;
1281 TRP_UPD *trpupd=NULL;
1282 TRP_REQ *trpreq=NULL;
1283 MON_REQ *monreq=NULL;
1284 MON_RESP *monresp=NULL;
1286 /* TBD -- add error handling */
1287 jmsg = json_object();
1289 switch (msg->msg_type) {
1291 jmsg_type = json_string("tid_request");
1292 json_object_set_new(jmsg, "msg_type", jmsg_type);
1293 tidreq=tr_msg_get_req(msg);
1294 json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidreq(tidreq));
1298 jmsg_type = json_string("tid_response");
1299 json_object_set_new(jmsg, "msg_type", jmsg_type);
1300 tidresp=tr_msg_get_resp(msg);
1301 json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidresp(tidresp));
1305 jmsg_type = json_string("trp_update");
1306 json_object_set_new(jmsg, "msg_type", jmsg_type);
1307 trpupd=tr_msg_get_trp_upd(msg);
1308 json_object_set_new(jmsg, "msg_body", tr_msg_encode_trp_upd(trpupd));
1312 jmsg_type = json_string("trp_request");
1313 json_object_set_new(jmsg, "msg_type", jmsg_type);
1314 trpreq=tr_msg_get_trp_req(msg);
1315 json_object_set_new(jmsg, "msg_body", tr_msg_encode_trp_req(trpreq));
1319 jmsg_type = json_string("mon_request");
1320 json_object_set_new(jmsg, "msg_type", jmsg_type);
1321 monreq=tr_msg_get_mon_req(msg);
1322 json_object_set_new(jmsg, "msg_body", mon_req_encode(monreq));
1326 jmsg_type = json_string("mon_response");
1327 json_object_set_new(jmsg, "msg_type", jmsg_type);
1328 monresp=tr_msg_get_mon_resp(msg);
1329 json_object_set_new(jmsg, "msg_body", mon_resp_encode(monresp));
1337 /* We should perhaps use json_set_alloc_funcs to automatically use talloc, but for
1338 * now, we'll encode to a malloc'ed buffer, then copy that to a talloc'ed buffer. */
1339 encoded_tmp=json_dumps(jmsg, 0); // malloc'ed version
1340 json_decref(jmsg); // free the JSON structure
1341 encoded = talloc_strdup(mem_ctx, encoded_tmp); // get the talloc'ed version
1342 free(encoded_tmp); // free the malloc'ed version
1344 tr_debug("tr_msg_encode: outgoing msg=%s", encoded);
1348 TR_MSG *tr_msg_decode(TALLOC_CTX *mem_ctx, const char *jbuf, size_t buflen)
1351 json_t *jmsg = NULL;
1355 const char *mtype = NULL;
1357 if (NULL == (jmsg = json_loadb(jbuf, buflen, JSON_DISABLE_EOF_CHECK, &rc))) {
1358 tr_debug("tr_msg_decode(): error loading object");
1362 if (!(msg = talloc_zero(mem_ctx, TR_MSG))) {
1363 tr_debug("tr_msg_decode(): Error allocating TR_MSG structure.");
1368 if ((NULL == (jtype = json_object_get(jmsg, "msg_type"))) ||
1369 (NULL == (jbody = json_object_get(jmsg, "msg_body")))) {
1370 tr_debug("tr_msg_decode(): Error parsing message header.");
1372 tr_msg_free_decoded(msg);
1376 mtype = json_string_value(jtype);
1378 if (0 == strcmp(mtype, "tid_request")) {
1379 msg->msg_type = TID_REQUEST;
1380 tr_msg_set_req(msg, tr_msg_decode_tidreq(msg, jbody));
1382 else if (0 == strcmp(mtype, "tid_response")) {
1383 msg->msg_type = TID_RESPONSE;
1384 tr_msg_set_resp(msg, tr_msg_decode_tidresp(msg, jbody));
1386 else if (0 == strcmp(mtype, "trp_update")) {
1387 msg->msg_type = TRP_UPDATE;
1388 tr_msg_set_trp_upd(msg, tr_msg_decode_trp_upd(msg, jbody));
1390 else if (0 == strcmp(mtype, "trp_request")) {
1391 msg->msg_type = TRP_UPDATE;
1392 tr_msg_set_trp_req(msg, tr_msg_decode_trp_req(msg, jbody));
1394 else if (0 == strcmp(mtype, "mon_request")) {
1395 msg->msg_type = MON_REQUEST;
1396 tr_msg_set_mon_req(msg, mon_req_decode(msg, jbody));
1398 /* We do not currently handle monitoring responses */
1399 else if (0 == strcmp(mtype, "mon_response")) {
1400 msg->msg_type = MON_RESPONSE;
1401 tr_msg_set_mon_resp(msg, mon_resp_decode(msg, jbody));
1404 msg->msg_type = TR_UNKNOWN;
1405 msg->msg_rep = NULL;
1413 void tr_msg_free_encoded(char *jmsg)
1419 void tr_msg_free_decoded(TR_MSG *msg)