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>
50 #include <tr_name_internal.h>
51 #include <trust_router/tr_constraint.h>
52 #include <trust_router/tr_dh.h>
56 /* Read attribute attr from msg as an integer. */
57 static TRP_RC tr_msg_get_json_integer(json_t *jmsg, const char *attr, int *dest)
61 obj=json_object_get(jmsg, attr);
66 if (!json_is_integer(obj)) {
70 (*dest)=json_integer_value(obj);
74 /* Read attribute attr from msg as a string. Copies string into mem_ctx context so jmsg can
75 * be destroyed safely. Returns nonzero on error. */
76 static TRP_RC tr_msg_get_json_string(json_t *jmsg, const char *attr, char **dest, TALLOC_CTX *mem_ctx)
80 obj=json_object_get(jmsg, attr);
85 if (!json_is_string(obj))
88 *dest=talloc_strdup(mem_ctx, json_string_value(obj));
95 enum msg_type tr_msg_get_msg_type(TR_MSG *msg)
100 void tr_msg_set_msg_type(TR_MSG *msg, enum msg_type type)
102 msg->msg_type = type;
105 /* NOTE: If you are manipulating messages with these getters/setters, the msg_rep
106 * objects are *not* put in the talloc context of the msg. If you are allocating
107 * the message directly with talloc, then you can talloc_steal() the rep into the
108 * message's context, but this is not handled automatically. */
111 * Get a TID_REQ message payload
114 * @return the message payload, or null if it is not a TID_REQUEST message
116 TID_REQ *tr_msg_get_req(TR_MSG *msg)
118 if (msg->msg_type == TID_REQUEST)
119 return (TID_REQ *)msg->msg_rep;
124 * Set message's payload
126 * Does not manage talloc contexts, works with any means of allocating
129 void tr_msg_set_req(TR_MSG *msg, TID_REQ *req)
132 msg->msg_type = TID_REQUEST;
136 * Get a TID_RESP message payload
139 * @return the message payload, or null if it is not a TID_RESPONSE message
141 TID_RESP *tr_msg_get_resp(TR_MSG *msg)
143 if (msg->msg_type == TID_RESPONSE)
144 return (TID_RESP *)msg->msg_rep;
149 * Set message's payload
151 * Does not manage talloc contexts, works with any means of allocating
154 void tr_msg_set_resp(TR_MSG *msg, TID_RESP *resp)
157 msg->msg_type = TID_RESPONSE;
161 * Get a MON_REQ message payload
164 * @return the message payload, or null if it is not a MON_REQUEST message
166 MON_REQ *tr_msg_get_mon_req(TR_MSG *msg)
168 if (msg->msg_type == MON_REQUEST)
169 return (MON_REQ *)msg->msg_rep;
174 * Set message's payload
176 * Does not manage talloc contexts, works with any means of allocating
179 void tr_msg_set_mon_req(TR_MSG *msg, MON_REQ *req)
182 msg->msg_type = MON_REQUEST;
186 * Get a MON_RESP message payload
189 * @return the message payload, or null if it is not a MON_RESPONSE message
191 MON_RESP *tr_msg_get_mon_resp(TR_MSG *msg)
193 if (msg->msg_type == MON_RESPONSE)
194 return (MON_RESP *)msg->msg_rep;
199 * Set message's payload
201 * Does not manage talloc contexts, works with any means of allocating
204 void tr_msg_set_mon_resp(TR_MSG *msg, MON_RESP *resp)
207 msg->msg_type = MON_RESPONSE;
211 * Get a TRP_UPD message payload
214 * @return the message payload, or null if it is not a TRP_UPDATE message
216 TRP_UPD *tr_msg_get_trp_upd(TR_MSG *msg)
218 if (msg->msg_type == TRP_UPDATE)
219 return (TRP_UPD *)msg->msg_rep;
224 * Set message's payload
226 * Does not manage talloc contexts, works with any means of allocating
229 void tr_msg_set_trp_upd(TR_MSG *msg, TRP_UPD *update)
232 msg->msg_type=TRP_UPDATE;
236 * Get a TRP_REQ message payload
239 * @return the message payload, or null if it is not a TRP_REQUEST message
241 TRP_REQ *tr_msg_get_trp_req(TR_MSG *msg)
243 if (msg->msg_type == TRP_REQUEST)
244 return (TRP_REQ *)msg->msg_rep;
249 * Set message's payload
251 * Does not manage talloc contexts, works with any means of allocating
254 void tr_msg_set_trp_req(TR_MSG *msg, TRP_REQ *req)
257 msg->msg_type=TRP_REQUEST;
260 static json_t *tr_msg_encode_dh(DH *dh)
266 if ((!dh) || (!dh->p) || (!dh->g) || (!dh->pub_key))
271 jbn = json_string(s=BN_bn2hex(dh->p));
273 json_object_set_new(jdh, "dh_p", jbn);
275 jbn = json_string(s=BN_bn2hex(dh->g));
277 json_object_set_new(jdh, "dh_g", jbn);
279 jbn = json_string(s=BN_bn2hex(dh->pub_key));
281 json_object_set_new(jdh, "dh_pub_key", jbn);
286 static DH *tr_msg_decode_dh(json_t *jdh)
291 json_t *jpub_key = NULL;
293 if (!(dh=tr_dh_new())) {
294 tr_crit("tr_msg_decode_dh(): Error allocating DH structure.");
298 /* store required fields from dh object */
299 if ((NULL == (jp = json_object_get(jdh, "dh_p"))) ||
300 (NULL == (jg = json_object_get(jdh, "dh_g"))) ||
301 (NULL == (jpub_key = json_object_get(jdh, "dh_pub_key")))) {
302 tr_debug("tr_msg_decode_dh(): Error parsing dh_info.");
307 BN_hex2bn(&(dh->p), json_string_value(jp));
308 BN_hex2bn(&(dh->g), json_string_value(jg));
309 BN_hex2bn(&(dh->pub_key), json_string_value(jpub_key));
314 static json_t * tr_msg_encode_tidreq(TID_REQ *req)
319 if ((!req) || (!req->rp_realm) || (!req->realm) || !(req->comm))
322 jreq = json_object();
325 jstr = tr_name_to_json_string(req->rp_realm);
326 json_object_set_new(jreq, "rp_realm", jstr);
328 jstr = tr_name_to_json_string(req->realm);
329 json_object_set_new(jreq, "target_realm", jstr);
331 jstr = tr_name_to_json_string(req->comm);
332 json_object_set_new(jreq, "community", jstr);
335 jstr = tr_name_to_json_string(req->orig_coi);
336 json_object_set_new(jreq, "orig_coi", jstr);
339 if (tid_req_get_request_id(req)) {
340 jstr = tr_name_to_json_string(tid_req_get_request_id(req));
341 json_object_set_new(jreq, "request_id", jstr);
344 json_object_set_new(jreq, "dh_info", tr_msg_encode_dh(req->tidc_dh));
347 json_object_set(jreq, "constraints", (json_t *) req->cons);
350 json_object_set(jreq, "path", req->path);
351 if (req->expiration_interval)
352 json_object_set_new(jreq, "expiration_interval",
353 json_integer(req->expiration_interval));
358 static TID_REQ *tr_msg_decode_tidreq(TALLOC_CTX *mem_ctx, json_t *jreq)
360 TID_REQ *treq = NULL;
361 json_t *jrp_realm = NULL;
362 json_t *jrealm = NULL;
363 json_t *jcomm = NULL;
364 json_t *jorig_coi = NULL;
365 json_t *jrequest_id = NULL;
367 json_t *jpath = NULL;
368 json_t *jexpire_interval = NULL;
370 if (!(treq =tid_req_new())) {
371 tr_crit("tr_msg_decode_tidreq(): Error allocating TID_REQ structure.");
374 talloc_steal(mem_ctx, treq);
376 /* store required fields from request */
377 if ((NULL == (jrp_realm = json_object_get(jreq, "rp_realm"))) ||
378 (NULL == (jrealm = json_object_get(jreq, "target_realm"))) ||
379 (NULL == (jcomm = json_object_get(jreq, "community")))) {
380 tr_notice("tr_msg_decode(): Error parsing required fields.");
385 jpath = json_object_get(jreq, "path");
386 jexpire_interval = json_object_get(jreq, "expiration_interval");
388 treq->rp_realm = tr_new_name(json_string_value(jrp_realm));
389 treq->realm = tr_new_name(json_string_value(jrealm));
390 treq->comm = tr_new_name(json_string_value(jcomm));
392 /* Get DH Info from the request */
393 if (NULL == (jdh = json_object_get(jreq, "dh_info"))) {
394 tr_debug("tr_msg_decode(): Error parsing dh_info.");
398 treq->tidc_dh = tr_msg_decode_dh(jdh);
400 /* store optional "orig_coi" field */
401 if (NULL != (jorig_coi = json_object_get(jreq, "orig_coi"))) {
402 treq->orig_coi = tr_new_name(json_string_value(jorig_coi));
405 /* store optional "request_id" field */
406 if (NULL != (jrequest_id = json_object_get(jreq, "request_id"))) {
407 tid_req_set_request_id(treq, tr_new_name(json_string_value(jrequest_id)));
410 treq->cons = (TR_CONSTRAINT_SET *) json_object_get(jreq, "constraints");
412 if (!tr_constraint_set_validate(treq->cons)) {
413 tr_debug("Constraint set validation failed");
417 json_incref((json_t *) treq->cons);
418 tid_req_cleanup_json(treq, (json_t *) treq->cons);
423 tid_req_cleanup_json(treq, jpath);
425 if (jexpire_interval)
426 treq->expiration_interval = json_integer_value(jexpire_interval);
431 static json_t *tr_msg_encode_one_server(TID_SRVR_BLK *srvr)
433 json_t *jsrvr = NULL;
435 gchar *time_str = g_time_val_to_iso8601(&srvr->key_expiration);
437 tr_debug("Encoding one server.");
439 jsrvr = json_object();
441 jstr = json_string(srvr->aaa_server_addr);
442 json_object_set_new(jsrvr, "server_addr", jstr);
444 json_object_set_new(jsrvr,
445 "key_expiration", json_string(time_str));
447 /* Server DH Block */
448 jstr = tr_name_to_json_string(srvr->key_name);
449 json_object_set_new(jsrvr, "key_name", jstr);
450 json_object_set_new(jsrvr, "server_dh", tr_msg_encode_dh(srvr->aaa_server_dh));
452 /* The path is owned by the srvr, so grab an extra ref*/
453 json_object_set(jsrvr, "path", (json_t *)(srvr->path));
457 static int tr_msg_decode_one_server(json_t *jsrvr, TID_SRVR_BLK *srvr)
459 json_t *jsrvr_addr = NULL;
460 json_t *jsrvr_kn = NULL;
461 json_t *jsrvr_dh = NULL;
462 json_t *jsrvr_expire = NULL;
468 if ((NULL == (jsrvr_addr = json_object_get(jsrvr, "server_addr"))) ||
469 (NULL == (jsrvr_kn = json_object_get(jsrvr, "key_name"))) ||
470 (NULL == (jsrvr_dh = json_object_get(jsrvr, "server_dh")))) {
471 tr_notice("tr_msg_decode_one_server(): Error parsing required fields.");
475 srvr->aaa_server_addr=talloc_strdup(srvr, json_string_value(jsrvr_addr));
476 srvr->key_name = tr_new_name((char *)json_string_value(jsrvr_kn));
477 srvr->aaa_server_dh = tr_msg_decode_dh(jsrvr_dh);
478 tid_srvr_blk_set_path(srvr, (TID_PATH *) json_object_get(jsrvr, "path"));
479 jsrvr_expire = json_object_get(jsrvr, "key_expiration");
480 if (jsrvr_expire && json_is_string(jsrvr_expire)) {
481 if (!g_time_val_from_iso8601(json_string_value(jsrvr_expire),
482 &srvr->key_expiration))
483 tr_notice("Key expiration %s cannot be parsed", json_string_value(jsrvr_expire));
489 static json_t *tr_msg_encode_servers(TID_RESP *resp)
491 json_t *jservers = NULL;
492 json_t *jsrvr = NULL;
493 TID_SRVR_BLK *srvr = NULL;
496 jservers = json_array();
498 tid_resp_servers_foreach(resp, srvr, index) {
499 if ((NULL == (jsrvr = tr_msg_encode_one_server(srvr))) ||
500 (-1 == json_array_append_new(jservers, jsrvr))) {
505 // tr_debug("tr_msg_encode_servers(): servers contains:");
506 // tr_debug("%s", json_dumps(jservers, 0));
510 static TID_SRVR_BLK *tr_msg_decode_servers(TALLOC_CTX *mem_ctx, json_t *jservers)
512 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
513 TID_SRVR_BLK *servers=NULL;
514 TID_SRVR_BLK *new_srvr=NULL;
516 size_t i, num_servers;
518 num_servers = json_array_size(jservers);
519 tr_debug("tr_msg_decode_servers(): Number of servers = %u.", (unsigned) num_servers);
521 if (0 == num_servers) {
522 tr_debug("tr_msg_decode_servers(): Server array is empty.");
526 for (i = 0; i < num_servers; i++) {
527 jsrvr = json_array_get(jservers, i);
529 new_srvr=tid_srvr_blk_new(tmp_ctx);
530 if (new_srvr==NULL) {
531 servers=NULL; /* it's all in tmp_ctx, so we can just let go */
535 if (0 != tr_msg_decode_one_server(jsrvr, new_srvr)) {
536 servers=NULL; /* it's all in tmp_ctx, so we can just let go */
540 tid_srvr_blk_add(servers, new_srvr);
543 talloc_steal(mem_ctx, servers);
546 talloc_free(tmp_ctx);
550 static json_t * tr_msg_encode_tidresp(TID_RESP *resp)
552 json_t *jresp = NULL;
554 json_t *jservers = NULL;
556 if ((!resp) || (!resp->rp_realm) || (!resp->realm) || !(resp->comm))
559 jresp = json_object();
561 if (TID_ERROR == resp->result) {
562 jstr = json_string("error");
563 json_object_set_new(jresp, "result", jstr);
565 jstr = tr_name_to_json_string(resp->err_msg);
566 json_object_set_new(jresp, "err_msg", jstr);
570 jstr = json_string("success");
571 json_object_set_new(jresp, "result", jstr);
574 jstr = tr_name_to_json_string(resp->rp_realm);
575 json_object_set_new(jresp, "rp_realm", jstr);
577 jstr = tr_name_to_json_string(resp->realm);
578 json_object_set_new(jresp, "target_realm", jstr);
580 jstr = tr_name_to_json_string(resp->comm);
581 json_object_set_new(jresp, "comm", jstr);
583 if (resp->orig_coi) {
584 jstr = tr_name_to_json_string(resp->orig_coi);
585 json_object_set_new(jresp, "orig_coi", jstr);
588 if (tid_resp_get_request_id(resp)) {
589 jstr = tr_name_to_json_string(tid_resp_get_request_id(resp));
590 json_object_set_new(jresp, "request_id", jstr);
593 if (NULL == resp->servers) {
594 tr_debug("tr_msg_encode_tidresp(): No servers to encode.");
597 jservers = tr_msg_encode_servers(resp);
598 json_object_set_new(jresp, "servers", jservers);
600 if (resp->error_path)
601 json_object_set(jresp, "error_path", resp->error_path);
607 static TID_RESP *tr_msg_decode_tidresp(TALLOC_CTX *mem_ctx, json_t *jresp)
609 TID_RESP *tresp = NULL;
610 json_t *jresult = NULL;
611 json_t *jrp_realm = NULL;
612 json_t *jrealm = NULL;
613 json_t *jcomm = NULL;
614 json_t *jorig_coi = NULL;
615 json_t *jrequest_id = NULL;
616 json_t *jservers = NULL;
617 json_t *jerr_msg = NULL;
619 if (!(tresp=tid_resp_new(mem_ctx))) {
620 tr_crit("tr_msg_decode_tidresp(): Error allocating TID_RESP structure.");
624 /* store required fields from response */
625 if ((NULL == (jresult = json_object_get(jresp, "result"))) ||
626 (!json_is_string(jresult)) ||
627 (NULL == (jrp_realm = json_object_get(jresp, "rp_realm"))) ||
628 (!json_is_string(jrp_realm)) ||
629 (NULL == (jrealm = json_object_get(jresp, "target_realm"))) ||
630 (!json_is_string(jrealm)) ||
631 (NULL == (jcomm = json_object_get(jresp, "comm"))) ||
632 (!json_is_string(jcomm))) {
633 tr_debug("tr_msg_decode_tidresp(): Error parsing response.");
638 if (0 == (strcmp(json_string_value(jresult), "success"))) {
639 tr_debug("tr_msg_decode_tidresp(): Success! result = %s.", json_string_value(jresult));
640 if ((NULL != (jservers = json_object_get(jresp, "servers"))) ||
641 (!json_is_array(jservers))) {
642 tresp->servers = tr_msg_decode_servers(tresp, jservers);
648 tresp->result = TID_SUCCESS;
651 tresp->result = TID_ERROR;
652 tr_debug("tr_msg_decode_tidresp(): Error! result = %s.", json_string_value(jresult));
653 if ((NULL != (jerr_msg = json_object_get(jresp, "err_msg"))) ||
654 (!json_is_string(jerr_msg))) {
655 tresp->err_msg = tr_new_name(json_string_value(jerr_msg));
657 tresp->err_msg = tr_new_name("No error message set.");
659 if (NULL !=(tresp->error_path = json_object_get(jresp, "error_path")))
660 json_incref(tresp->error_path);
663 tresp->rp_realm = tr_new_name(json_string_value(jrp_realm));
664 tresp->realm = tr_new_name(json_string_value(jrealm));
665 tresp->comm = tr_new_name(json_string_value(jcomm));
667 /* store optional "orig_coi" field */
668 if ((NULL != (jorig_coi = json_object_get(jresp, "orig_coi"))) &&
669 json_is_string(jorig_coi)) {
670 tresp->orig_coi = tr_new_name(json_string_value(jorig_coi));
673 /* store optional "request_id" field */
674 if ((NULL != (jrequest_id = json_object_get(jresp, "request_id"))) &&
675 json_is_string(jrequest_id)) {
676 tid_resp_set_request_id(tresp, tr_new_name(json_string_value(jrequest_id)));
682 static json_t *hostname_and_port_to_json(TR_NAME *hostname, int port)
684 char *s_hostname = tr_name_strdup(hostname);
688 if (s_hostname == NULL)
691 s = talloc_asprintf(NULL, "%s:%d", s_hostname, port);
703 /* Information records for TRP update msg
704 * requires that jrec already be allocated */
705 static TRP_RC tr_msg_encode_inforec_route(json_t *jrec, TRP_INFOREC *rec)
713 if (trp_inforec_get_trust_router(rec)==NULL)
716 jstr=hostname_and_port_to_json(trp_inforec_get_trust_router(rec),
717 trp_inforec_get_trust_router_port(rec));
720 json_object_set_new(jrec, "trust_router", jstr);
722 jstr=hostname_and_port_to_json(trp_inforec_get_next_hop(rec),
723 trp_inforec_get_next_hop_port(rec));
726 json_object_set_new(jrec, "next_hop", jstr);
728 jint=json_integer(trp_inforec_get_metric(rec));
731 json_object_set_new(jrec, "metric", jint);
733 jint=json_integer(trp_inforec_get_interval(rec));
736 json_object_set_new(jrec, "interval", jint);
741 /* returns a json array */
742 static json_t *tr_msg_encode_apcs(TR_APC *apcs)
744 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
745 TR_APC_ITER *iter=tr_apc_iter_new(tmp_ctx);
757 for (apc=tr_apc_iter_first(iter, apcs); apc!=NULL; apc=tr_apc_iter_next(iter)) {
758 jid=tr_name_to_json_string(tr_apc_get_id(apc));
759 if ((jid==NULL) || (json_array_append_new(jarray, jid)!=0)) {
767 talloc_free(tmp_ctx);
771 static TR_APC *tr_msg_decode_apcs(TALLOC_CTX *mem_ctx, json_t *jarray, TRP_RC *rc)
773 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
775 TR_APC *apc_list=NULL;
781 for (ii=0; ii<json_array_size(jarray); ii++) {
782 jstr=json_array_get(jarray, ii);
783 new=tr_apc_new(tmp_ctx);
784 if ((jstr==NULL) || (new==NULL) || (!json_is_string(jstr))) {
785 apc_list=NULL; /* these are all in tmp_ctx, so they'll still get cleaned up */
789 tr_apc_set_id(new, tr_new_name(json_string_value(jstr)));
790 if (tr_apc_get_id(new)==NULL) {
791 apc_list=NULL; /* these are all in tmp_ctx, so they'll still get cleaned up */
795 tr_apc_add(apc_list, new);
801 talloc_steal(mem_ctx, apc_list);
804 talloc_free(tmp_ctx);
808 static TRP_RC tr_msg_encode_inforec_comm(json_t *jrec, TRP_INFOREC *rec)
813 const char *sconst=NULL;
814 TR_COMM_TYPE commtype=TR_COMM_UNKNOWN;
819 commtype=trp_inforec_get_comm_type(rec);
820 if (commtype==TR_COMM_UNKNOWN) {
821 tr_notice("tr_msg_encode_inforec_comm: unknown community type.");
824 sconst=tr_comm_type_to_str(commtype);
827 jstr=json_string(sconst);
830 json_object_set_new(jrec, "type", jstr);
832 sconst=tr_realm_role_to_str(trp_inforec_get_role(rec));
834 tr_notice("tr_msg_encode_inforec_comm: unknown realm role.");
837 jstr=json_string(sconst);
840 json_object_set_new(jrec, "role", jstr);
842 japcs=tr_msg_encode_apcs(trp_inforec_get_apcs(rec));
844 tr_notice("tr_msg_encode_inforec_comm: error encoding APCs.");
847 json_object_set_new(jrec, "apcs", japcs);
850 if (trp_inforec_get_owner_realm(rec)!=NULL) {
851 jstr=tr_name_to_json_string(trp_inforec_get_owner_realm(rec));
854 json_object_set_new(jrec, "owner_realm", jstr);
857 if (trp_inforec_get_owner_contact(rec)!=NULL) {
858 jstr=tr_name_to_json_string(trp_inforec_get_owner_contact(rec));
861 json_object_set_new(jrec, "owner_contact", jstr);
864 json_object_set(jrec, "provenance", trp_inforec_get_provenance(rec));
866 jint=json_integer(trp_inforec_get_interval(rec));
869 json_object_set_new(jrec, "interval", jint);
874 static json_t *tr_msg_encode_inforec(TRP_INFOREC *rec)
879 if ((rec==NULL) || (trp_inforec_get_type(rec)==TRP_INFOREC_TYPE_UNKNOWN))
886 jstr=json_string(trp_inforec_type_to_string(trp_inforec_get_type(rec)));
891 json_object_set_new(jrec, "record_type", jstr);
894 case TRP_INFOREC_TYPE_ROUTE:
895 if (TRP_SUCCESS!=tr_msg_encode_inforec_route(jrec, rec)) {
900 case TRP_INFOREC_TYPE_COMMUNITY:
901 if (TRP_SUCCESS!=tr_msg_encode_inforec_comm(jrec, rec)) {
913 static TRP_RC tr_msg_decode_trp_inforec_route(json_t *jrecord, TRP_INFOREC *rec)
915 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
922 /* get the trust router */
923 rc=tr_msg_get_json_string(jrecord, "trust_router", &s, tmp_ctx);
924 if (rc != TRP_SUCCESS)
927 if (0 != tr_parse_hostname_and_port(s, &name, &port)) {
931 talloc_free(s); s=NULL;
936 if (TRP_SUCCESS!= trp_inforec_set_trust_router(rec, name, port)) {
941 /* Now do the next hop. If it's not present, use the trust_router for backward
943 switch(tr_msg_get_json_string(jrecord, "next_hop", &s, tmp_ctx)) {
945 /* we got a next_hop field */
946 if (0 != tr_parse_hostname_and_port(s, &name, &port)) {
953 /* no next_hop field; use the trust router */
954 name = tr_dup_name(trp_inforec_get_trust_router(rec));
962 /* something went wrong */
966 talloc_free(s); s=NULL;
971 if (TRP_SUCCESS!= trp_inforec_set_next_hop(rec, name, port)) {
976 rc=tr_msg_get_json_integer(jrecord, "metric", &num);
977 if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_metric(rec,num)))
980 rc=tr_msg_get_json_integer(jrecord, "interval", &num);
981 if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_interval(rec,num)))
985 talloc_free(tmp_ctx);
989 static TRP_RC tr_msg_decode_trp_inforec_comm(json_t *jrecord, TRP_INFOREC *rec)
991 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
997 rc=tr_msg_get_json_string(jrecord, "type", &s, tmp_ctx);
998 if (rc != TRP_SUCCESS)
1000 if (TRP_SUCCESS!=trp_inforec_set_comm_type(rec, tr_comm_type_from_str(s))) {
1004 talloc_free(s); s=NULL;
1006 rc=tr_msg_get_json_string(jrecord, "role", &s, tmp_ctx);
1007 if (rc != TRP_SUCCESS)
1009 if (TRP_SUCCESS!=trp_inforec_set_role(rec, tr_realm_role_from_str(s))) {
1013 talloc_free(s); s=NULL;
1015 apcs=tr_msg_decode_apcs(rec, json_object_get(jrecord, "apcs"), &rc);
1016 if (rc!=TRP_SUCCESS) {
1020 trp_inforec_set_apcs(rec, apcs);
1022 rc=tr_msg_get_json_integer(jrecord, "interval", &num);
1023 tr_debug("tr_msg_decode_trp_inforec_comm: interval=%u", num);
1024 if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_interval(rec,num)))
1027 trp_inforec_set_provenance(rec, json_object_get(jrecord, "provenance"));
1030 rc=tr_msg_get_json_string(jrecord, "owner_realm", &s, tmp_ctx);
1031 if (rc == TRP_SUCCESS) {
1032 if (TRP_SUCCESS!=trp_inforec_set_owner_realm(rec, tr_new_name(s))) {
1042 rc=tr_msg_get_json_string(jrecord, "owner_contact", &s, tmp_ctx);
1043 if (rc == TRP_SUCCESS) {
1044 if (TRP_SUCCESS!=trp_inforec_set_owner_contact(rec, tr_new_name(s))) {
1055 talloc_free(tmp_ctx);
1059 /* decode a single record */
1060 static TRP_INFOREC *tr_msg_decode_trp_inforec(TALLOC_CTX *mem_ctx, json_t *jrecord)
1062 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1063 TRP_INFOREC_TYPE rectype;
1064 TRP_INFOREC *rec=NULL;
1065 TRP_RC rc=TRP_ERROR;
1068 if (TRP_SUCCESS!=tr_msg_get_json_string(jrecord, "record_type", &s, tmp_ctx))
1071 rectype=trp_inforec_type_from_string(s);
1072 talloc_free(s); s=NULL;
1074 rec=trp_inforec_new(tmp_ctx, rectype);
1080 tr_debug("tr_msg_decode_trp_inforec: '%s' record found.", trp_inforec_type_to_string(rec->type));
1082 switch(trp_inforec_get_type(rec)) {
1083 case TRP_INFOREC_TYPE_ROUTE:
1084 rc=tr_msg_decode_trp_inforec_route(jrecord, rec);
1086 case TRP_INFOREC_TYPE_COMMUNITY:
1087 rc=tr_msg_decode_trp_inforec_comm(jrecord, rec);
1094 talloc_steal(mem_ctx, rec);
1098 if (rc != TRP_SUCCESS) {
1099 trp_inforec_free(rec);
1102 talloc_free(tmp_ctx);
1106 /* TRP update msg */
1107 static json_t *tr_msg_encode_trp_upd(TRP_UPD *update)
1109 json_t *jupdate=NULL;
1110 json_t *jrecords=NULL;
1119 jupdate=json_object();
1123 s=tr_name_strdup(trp_upd_get_comm(update));
1125 json_decref(jupdate);
1128 jstr=json_string(s);
1131 json_decref(jupdate);
1134 json_object_set_new(jupdate, "community", jstr);
1136 s=tr_name_strdup(trp_upd_get_realm(update));
1138 json_decref(jupdate);
1141 jstr=json_string(s);
1144 json_decref(jupdate);
1147 json_object_set_new(jupdate, "realm", jstr);
1149 jrecords=json_array();
1150 if (jrecords==NULL) {
1151 json_decref(jupdate);
1154 json_object_set_new(jupdate, "records", jrecords); /* jrecords now a "borrowed" reference */
1155 for (rec=trp_upd_get_inforec(update); rec!=NULL; rec=trp_inforec_get_next(rec)) {
1156 tr_debug("tr_msg_encode_trp_upd: encoding inforec.");
1157 jrec=tr_msg_encode_inforec(rec);
1159 json_decref(jupdate); /* also decs jrecords and any elements */
1162 if (0!=json_array_append_new(jrecords, jrec)) {
1163 json_decref(jupdate); /* also decs jrecords and any elements */
1164 json_decref(jrec); /* this one did not get added so dec explicitly */
1172 /* Creates a linked list of records in the msg->body talloc context.
1173 * An error will be returned if any unparseable records are encountered.
1175 static TRP_UPD *tr_msg_decode_trp_upd(TALLOC_CTX *mem_ctx, json_t *jupdate)
1177 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1178 json_t *jrecords=NULL;
1180 TRP_UPD *update=NULL;
1181 TRP_INFOREC *new_rec=NULL;
1182 TRP_INFOREC *list_tail=NULL;
1185 TRP_RC rc=TRP_ERROR;
1187 update=trp_upd_new(tmp_ctx);
1193 rc=tr_msg_get_json_string(jupdate, "community", &s, tmp_ctx);
1194 if (rc != TRP_SUCCESS) {
1195 tr_debug("tr_msg_decode_trp_upd: no community in TRP update message.");
1199 name=tr_new_name(s);
1201 tr_debug("tr_msg_decode_trp_upd: could not allocate community name.");
1205 talloc_free(s); s=NULL;
1206 trp_upd_set_comm(update, name);
1208 rc=tr_msg_get_json_string(jupdate, "realm", &s, tmp_ctx);
1209 if (rc != TRP_SUCCESS) {
1210 tr_debug("tr_msg_decode_trp_upd: no realm in TRP update message.");
1214 name=tr_new_name(s);
1216 tr_debug("tr_msg_decode_trp_upd: could not allocate realm name.");
1220 talloc_free(s); s=NULL;
1221 trp_upd_set_realm(update, name);
1223 jrecords=json_object_get(jupdate, "records");
1224 if ((jrecords==NULL) || (!json_is_array(jrecords))) {
1229 tr_debug("tr_msg_decode_trp_upd: found %d records", json_array_size(jrecords));
1230 /* process the array */
1231 for (ii=0; ii<json_array_size(jrecords); ii++) {
1232 new_rec=tr_msg_decode_trp_inforec(update, json_array_get(jrecords, ii));
1233 if (new_rec==NULL) {
1238 if (list_tail==NULL)
1239 trp_upd_set_inforec(update, new_rec); /* first is a special case */
1241 trp_inforec_set_next(list_tail, new_rec);
1246 /* Succeeded. Move new allocations into the correct talloc context */
1247 talloc_steal(mem_ctx, update);
1251 talloc_free(tmp_ctx);
1252 if (rc!=TRP_SUCCESS)
1257 static json_t *tr_msg_encode_trp_req(TRP_REQ *req)
1266 jbody=json_object();
1270 if ((NULL==trp_req_get_comm(req))
1271 || (NULL==trp_req_get_realm(req))) {
1276 s=tr_name_strdup(trp_req_get_comm(req)); /* ensures null termination */
1281 jstr=json_string(s);
1287 json_object_set_new(jbody, "community", jstr);
1289 s=tr_name_strdup(trp_req_get_realm(req)); /* ensures null termination */
1294 jstr=json_string(s);
1300 json_object_set_new(jbody, "realm", jstr);
1305 static TRP_REQ *tr_msg_decode_trp_req(TALLOC_CTX *mem_ctx, json_t *jreq)
1307 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1310 TRP_RC rc=TRP_ERROR;
1312 /* check message type and body type for agreement */
1313 req=trp_req_new(tmp_ctx);
1319 rc=tr_msg_get_json_string(jreq, "community", &s, tmp_ctx);
1320 if (rc!=TRP_SUCCESS)
1322 trp_req_set_comm(req, tr_new_name(s));
1323 talloc_free(s); s=NULL;
1325 rc=tr_msg_get_json_string(jreq, "realm", &s, tmp_ctx);
1326 if (rc!=TRP_SUCCESS)
1328 trp_req_set_realm(req, tr_new_name(s));
1329 talloc_free(s); s=NULL;
1332 talloc_steal(mem_ctx, req);
1335 talloc_free(tmp_ctx);
1336 if (rc!=TRP_SUCCESS)
1341 char *tr_msg_encode(TALLOC_CTX *mem_ctx, TR_MSG *msg)
1344 json_t *jmsg_type=NULL;
1345 char *encoded_tmp=NULL;
1347 TID_RESP *tidresp=NULL;
1348 TID_REQ *tidreq=NULL;
1349 TRP_UPD *trpupd=NULL;
1350 TRP_REQ *trpreq=NULL;
1351 MON_REQ *monreq=NULL;
1352 MON_RESP *monresp=NULL;
1354 /* TBD -- add error handling */
1355 jmsg = json_object();
1357 switch (msg->msg_type) {
1359 jmsg_type = json_string("tid_request");
1360 json_object_set_new(jmsg, "msg_type", jmsg_type);
1361 tidreq=tr_msg_get_req(msg);
1362 json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidreq(tidreq));
1366 jmsg_type = json_string("tid_response");
1367 json_object_set_new(jmsg, "msg_type", jmsg_type);
1368 tidresp=tr_msg_get_resp(msg);
1369 json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidresp(tidresp));
1373 jmsg_type = json_string("trp_update");
1374 json_object_set_new(jmsg, "msg_type", jmsg_type);
1375 trpupd=tr_msg_get_trp_upd(msg);
1376 json_object_set_new(jmsg, "msg_body", tr_msg_encode_trp_upd(trpupd));
1380 jmsg_type = json_string("trp_request");
1381 json_object_set_new(jmsg, "msg_type", jmsg_type);
1382 trpreq=tr_msg_get_trp_req(msg);
1383 json_object_set_new(jmsg, "msg_body", tr_msg_encode_trp_req(trpreq));
1387 jmsg_type = json_string("mon_request");
1388 json_object_set_new(jmsg, "msg_type", jmsg_type);
1389 monreq=tr_msg_get_mon_req(msg);
1390 json_object_set_new(jmsg, "msg_body", mon_req_encode(monreq));
1394 jmsg_type = json_string("mon_response");
1395 json_object_set_new(jmsg, "msg_type", jmsg_type);
1396 monresp=tr_msg_get_mon_resp(msg);
1397 json_object_set_new(jmsg, "msg_body", mon_resp_encode(monresp));
1405 /* We should perhaps use json_set_alloc_funcs to automatically use talloc, but for
1406 * now, we'll encode to a malloc'ed buffer, then copy that to a talloc'ed buffer. */
1407 encoded_tmp=json_dumps(jmsg, 0); // malloc'ed version
1408 json_decref(jmsg); // free the JSON structure
1409 encoded = talloc_strdup(mem_ctx, encoded_tmp); // get the talloc'ed version
1410 free(encoded_tmp); // free the malloc'ed version
1412 tr_debug("tr_msg_encode: outgoing msg=%s", encoded);
1416 TR_MSG *tr_msg_decode(TALLOC_CTX *mem_ctx, const char *jbuf, size_t buflen)
1419 json_t *jmsg = NULL;
1423 const char *mtype = NULL;
1425 if (NULL == (jmsg = json_loadb(jbuf, buflen, JSON_DISABLE_EOF_CHECK, &rc))) {
1426 tr_debug("tr_msg_decode(): error loading object");
1430 if (!(msg = talloc_zero(mem_ctx, TR_MSG))) {
1431 tr_debug("tr_msg_decode(): Error allocating TR_MSG structure.");
1436 if ((NULL == (jtype = json_object_get(jmsg, "msg_type"))) ||
1437 (NULL == (jbody = json_object_get(jmsg, "msg_body")))) {
1438 tr_debug("tr_msg_decode(): Error parsing message header.");
1440 tr_msg_free_decoded(msg);
1444 mtype = json_string_value(jtype);
1446 if (0 == strcmp(mtype, "tid_request")) {
1447 msg->msg_type = TID_REQUEST;
1448 tr_msg_set_req(msg, tr_msg_decode_tidreq(msg, jbody));
1450 else if (0 == strcmp(mtype, "tid_response")) {
1451 msg->msg_type = TID_RESPONSE;
1452 tr_msg_set_resp(msg, tr_msg_decode_tidresp(msg, jbody));
1454 else if (0 == strcmp(mtype, "trp_update")) {
1455 msg->msg_type = TRP_UPDATE;
1456 tr_msg_set_trp_upd(msg, tr_msg_decode_trp_upd(msg, jbody));
1458 else if (0 == strcmp(mtype, "trp_request")) {
1459 msg->msg_type = TRP_UPDATE;
1460 tr_msg_set_trp_req(msg, tr_msg_decode_trp_req(msg, jbody));
1462 else if (0 == strcmp(mtype, "mon_request")) {
1463 msg->msg_type = MON_REQUEST;
1464 tr_msg_set_mon_req(msg, mon_req_decode(msg, jbody));
1466 /* We do not currently handle monitoring responses */
1467 else if (0 == strcmp(mtype, "mon_response")) {
1468 msg->msg_type = MON_RESPONSE;
1469 tr_msg_set_mon_resp(msg, mon_resp_decode(msg, jbody));
1472 msg->msg_type = TR_UNKNOWN;
1473 msg->msg_rep = NULL;
1481 void tr_msg_free_encoded(char *jmsg)
1487 void tr_msg_free_decoded(TR_MSG *msg)