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>
47 #include <mon_internal.h>
48 #include <trp_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. Returns nonzero on error. */
57 static int 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 TID_REQ *tr_msg_get_req(TR_MSG *msg)
107 if (msg->msg_type == TID_REQUEST)
108 return (TID_REQ *)msg->msg_rep;
112 void tr_msg_set_req(TR_MSG *msg, TID_REQ *req)
115 msg->msg_type = TID_REQUEST;
118 TID_RESP *tr_msg_get_resp(TR_MSG *msg)
120 if (msg->msg_type == TID_RESPONSE)
121 return (TID_RESP *)msg->msg_rep;
125 void tr_msg_set_resp(TR_MSG *msg, TID_RESP *resp)
128 msg->msg_type = TID_RESPONSE;
131 MON_REQ *tr_msg_get_mon_req(TR_MSG *msg)
133 if (msg->msg_type == MON_REQUEST)
134 return (MON_REQ *)msg->msg_rep;
138 void tr_msg_set_mon_req(TR_MSG *msg, MON_REQ *req)
141 msg->msg_type = MON_REQUEST;
144 MON_RESP *tr_msg_get_mon_resp(TR_MSG *msg)
146 if (msg->msg_type == MON_RESPONSE)
147 return (MON_RESP *)msg->msg_rep;
151 void tr_msg_set_mon_resp(TR_MSG *msg, MON_RESP *resp)
154 msg->msg_type = MON_RESPONSE;
157 TRP_UPD *tr_msg_get_trp_upd(TR_MSG *msg)
159 if (msg->msg_type == TRP_UPDATE)
160 return (TRP_UPD *)msg->msg_rep;
164 void tr_msg_set_trp_upd(TR_MSG *msg, TRP_UPD *update)
167 talloc_steal(NULL, update); /* should attach to msg, but TR_MSG not usually talloc'ed */
168 msg->msg_type=TRP_UPDATE;
171 TRP_REQ *tr_msg_get_trp_req(TR_MSG *msg)
173 if (msg->msg_type == TRP_REQUEST)
174 return (TRP_REQ *)msg->msg_rep;
178 void tr_msg_set_trp_req(TR_MSG *msg, TRP_REQ *req)
181 msg->msg_type=TRP_REQUEST;
184 static json_t *tr_msg_encode_dh(DH *dh)
190 if ((!dh) || (!dh->p) || (!dh->g) || (!dh->pub_key))
195 jbn = json_string(s=BN_bn2hex(dh->p));
197 json_object_set_new(jdh, "dh_p", jbn);
199 jbn = json_string(s=BN_bn2hex(dh->g));
201 json_object_set_new(jdh, "dh_g", jbn);
203 jbn = json_string(s=BN_bn2hex(dh->pub_key));
205 json_object_set_new(jdh, "dh_pub_key", jbn);
210 static DH *tr_msg_decode_dh(json_t *jdh)
215 json_t *jpub_key = NULL;
217 if (!(dh=tr_dh_new())) {
218 tr_crit("tr_msg_decode_dh(): Error allocating DH structure.");
222 /* store required fields from dh object */
223 if ((NULL == (jp = json_object_get(jdh, "dh_p"))) ||
224 (NULL == (jg = json_object_get(jdh, "dh_g"))) ||
225 (NULL == (jpub_key = json_object_get(jdh, "dh_pub_key")))) {
226 tr_debug("tr_msg_decode_dh(): Error parsing dh_info.");
231 BN_hex2bn(&(dh->p), json_string_value(jp));
232 BN_hex2bn(&(dh->g), json_string_value(jg));
233 BN_hex2bn(&(dh->pub_key), json_string_value(jpub_key));
238 static json_t * tr_msg_encode_tidreq(TID_REQ *req)
243 if ((!req) || (!req->rp_realm) || (!req->realm) || !(req->comm))
246 assert(jreq = json_object());
248 jstr = tr_name_to_json_string(req->rp_realm);
249 json_object_set_new(jreq, "rp_realm", jstr);
251 jstr = tr_name_to_json_string(req->realm);
252 json_object_set_new(jreq, "target_realm", jstr);
254 jstr = tr_name_to_json_string(req->comm);
255 json_object_set_new(jreq, "community", jstr);
258 jstr = tr_name_to_json_string(req->orig_coi);
259 json_object_set_new(jreq, "orig_coi", jstr);
262 json_object_set_new(jreq, "dh_info", tr_msg_encode_dh(req->tidc_dh));
265 json_object_set(jreq, "constraints", (json_t *) req->cons);
268 json_object_set(jreq, "path", req->path);
269 if (req->expiration_interval)
270 json_object_set_new(jreq, "expiration_interval",
271 json_integer(req->expiration_interval));
276 static TID_REQ *tr_msg_decode_tidreq(TALLOC_CTX *mem_ctx, json_t *jreq)
278 TID_REQ *treq = NULL;
279 json_t *jrp_realm = NULL;
280 json_t *jrealm = NULL;
281 json_t *jcomm = NULL;
282 json_t *jorig_coi = NULL;
284 json_t *jpath = NULL;
285 json_t *jexpire_interval = NULL;
287 if (!(treq =tid_req_new())) {
288 tr_crit("tr_msg_decode_tidreq(): Error allocating TID_REQ structure.");
291 talloc_steal(mem_ctx, treq);
293 /* store required fields from request */
294 if ((NULL == (jrp_realm = json_object_get(jreq, "rp_realm"))) ||
295 (NULL == (jrealm = json_object_get(jreq, "target_realm"))) ||
296 (NULL == (jcomm = json_object_get(jreq, "community")))) {
297 tr_notice("tr_msg_decode(): Error parsing required fields.");
302 jpath = json_object_get(jreq, "path");
303 jexpire_interval = json_object_get(jreq, "expiration_interval");
305 treq->rp_realm = tr_new_name((char *)json_string_value(jrp_realm));
306 treq->realm = tr_new_name((char *)json_string_value(jrealm));
307 treq->comm = tr_new_name((char *)json_string_value(jcomm));
309 /* Get DH Info from the request */
310 if (NULL == (jdh = json_object_get(jreq, "dh_info"))) {
311 tr_debug("tr_msg_decode(): Error parsing dh_info.");
315 treq->tidc_dh = tr_msg_decode_dh(jdh);
317 /* store optional "orig_coi" field */
318 if (NULL != (jorig_coi = json_object_get(jreq, "orig_coi"))) {
319 treq->orig_coi = tr_new_name((char *)json_string_value(jorig_coi));
322 treq->cons = (TR_CONSTRAINT_SET *) json_object_get(jreq, "constraints");
324 if (!tr_constraint_set_validate(treq->cons)) {
325 tr_debug("Constraint set validation failed");
329 json_incref((json_t *) treq->cons);
330 tid_req_cleanup_json(treq, (json_t *) treq->cons);
335 tid_req_cleanup_json(treq, jpath);
337 if (jexpire_interval)
338 treq->expiration_interval = json_integer_value(jexpire_interval);
343 static json_t *tr_msg_encode_one_server(TID_SRVR_BLK *srvr)
345 json_t *jsrvr = NULL;
347 gchar *time_str = g_time_val_to_iso8601(&srvr->key_expiration);
349 tr_debug("Encoding one server.");
351 jsrvr = json_object();
353 jstr = json_string(srvr->aaa_server_addr);
354 json_object_set_new(jsrvr, "server_addr", jstr);
356 json_object_set_new(jsrvr,
357 "key_expiration", json_string(time_str));
359 /* Server DH Block */
360 jstr = tr_name_to_json_string(srvr->key_name);
361 json_object_set_new(jsrvr, "key_name", jstr);
362 json_object_set_new(jsrvr, "server_dh", tr_msg_encode_dh(srvr->aaa_server_dh));
364 /* The path is owned by the srvr, so grab an extra ref*/
365 json_object_set(jsrvr, "path", (json_t *)(srvr->path));
369 static int tr_msg_decode_one_server(json_t *jsrvr, TID_SRVR_BLK *srvr)
371 json_t *jsrvr_addr = NULL;
372 json_t *jsrvr_kn = NULL;
373 json_t *jsrvr_dh = NULL;
374 json_t *jsrvr_expire = NULL;
380 if ((NULL == (jsrvr_addr = json_object_get(jsrvr, "server_addr"))) ||
381 (NULL == (jsrvr_kn = json_object_get(jsrvr, "key_name"))) ||
382 (NULL == (jsrvr_dh = json_object_get(jsrvr, "server_dh")))) {
383 tr_notice("tr_msg_decode_one_server(): Error parsing required fields.");
387 srvr->aaa_server_addr=talloc_strdup(srvr, json_string_value(jsrvr_addr));
388 srvr->key_name = tr_new_name((char *)json_string_value(jsrvr_kn));
389 srvr->aaa_server_dh = tr_msg_decode_dh(jsrvr_dh);
390 tid_srvr_blk_set_path(srvr, (TID_PATH *) json_object_get(jsrvr, "path"));
391 jsrvr_expire = json_object_get(jsrvr, "key_expiration");
392 if (jsrvr_expire && json_is_string(jsrvr_expire)) {
393 if (!g_time_val_from_iso8601(json_string_value(jsrvr_expire),
394 &srvr->key_expiration))
395 tr_notice("Key expiration %s cannot be parsed", json_string_value(jsrvr_expire));
401 static json_t *tr_msg_encode_servers(TID_RESP *resp)
403 json_t *jservers = NULL;
404 json_t *jsrvr = NULL;
405 TID_SRVR_BLK *srvr = NULL;
408 jservers = json_array();
410 tid_resp_servers_foreach(resp, srvr, index) {
411 if ((NULL == (jsrvr = tr_msg_encode_one_server(srvr))) ||
412 (-1 == json_array_append_new(jservers, jsrvr))) {
417 // tr_debug("tr_msg_encode_servers(): servers contains:");
418 // tr_debug("%s", json_dumps(jservers, 0));
422 static TID_SRVR_BLK *tr_msg_decode_servers(TALLOC_CTX *mem_ctx, json_t *jservers)
424 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
425 TID_SRVR_BLK *servers=NULL;
426 TID_SRVR_BLK *new_srvr=NULL;
428 size_t i, num_servers;
430 num_servers = json_array_size(jservers);
431 tr_debug("tr_msg_decode_servers(): Number of servers = %u.", (unsigned) num_servers);
433 if (0 == num_servers) {
434 tr_debug("tr_msg_decode_servers(): Server array is empty.");
438 for (i = 0; i < num_servers; i++) {
439 jsrvr = json_array_get(jservers, i);
441 new_srvr=tid_srvr_blk_new(tmp_ctx);
442 if (new_srvr==NULL) {
443 servers=NULL; /* it's all in tmp_ctx, so we can just let go */
447 if (0 != tr_msg_decode_one_server(jsrvr, new_srvr)) {
448 servers=NULL; /* it's all in tmp_ctx, so we can just let go */
452 tid_srvr_blk_add(servers, new_srvr);
455 talloc_steal(mem_ctx, servers);
458 talloc_free(tmp_ctx);
462 static json_t * tr_msg_encode_tidresp(TID_RESP *resp)
464 json_t *jresp = NULL;
466 json_t *jservers = NULL;
468 if ((!resp) || (!resp->rp_realm) || (!resp->realm) || !(resp->comm))
471 jresp = json_object();
473 if (TID_ERROR == resp->result) {
474 jstr = json_string("error");
475 json_object_set_new(jresp, "result", jstr);
477 jstr = tr_name_to_json_string(resp->err_msg);
478 json_object_set_new(jresp, "err_msg", jstr);
482 jstr = json_string("success");
483 json_object_set_new(jresp, "result", jstr);
486 jstr = tr_name_to_json_string(resp->rp_realm);
487 json_object_set_new(jresp, "rp_realm", jstr);
489 jstr = tr_name_to_json_string(resp->realm);
490 json_object_set_new(jresp, "target_realm", jstr);
492 jstr = tr_name_to_json_string(resp->comm);
493 json_object_set_new(jresp, "comm", jstr);
495 if (resp->orig_coi) {
496 jstr = tr_name_to_json_string(resp->orig_coi);
497 json_object_set_new(jresp, "orig_coi", jstr);
500 if (NULL == resp->servers) {
501 tr_debug("tr_msg_encode_tidresp(): No servers to encode.");
504 jservers = tr_msg_encode_servers(resp);
505 json_object_set_new(jresp, "servers", jservers);
507 if (resp->error_path)
508 json_object_set(jresp, "error_path", resp->error_path);
514 static TID_RESP *tr_msg_decode_tidresp(TALLOC_CTX *mem_ctx, json_t *jresp)
516 TID_RESP *tresp = NULL;
517 json_t *jresult = NULL;
518 json_t *jrp_realm = NULL;
519 json_t *jrealm = NULL;
520 json_t *jcomm = NULL;
521 json_t *jorig_coi = NULL;
522 json_t *jservers = NULL;
523 json_t *jerr_msg = NULL;
525 if (!(tresp=tid_resp_new(mem_ctx))) {
526 tr_crit("tr_msg_decode_tidresp(): Error allocating TID_RESP structure.");
530 /* store required fields from response */
531 if ((NULL == (jresult = json_object_get(jresp, "result"))) ||
532 (!json_is_string(jresult)) ||
533 (NULL == (jrp_realm = json_object_get(jresp, "rp_realm"))) ||
534 (!json_is_string(jrp_realm)) ||
535 (NULL == (jrealm = json_object_get(jresp, "target_realm"))) ||
536 (!json_is_string(jrealm)) ||
537 (NULL == (jcomm = json_object_get(jresp, "comm"))) ||
538 (!json_is_string(jcomm))) {
539 tr_debug("tr_msg_decode_tidresp(): Error parsing response.");
544 if (0 == (strcmp(json_string_value(jresult), "success"))) {
545 tr_debug("tr_msg_decode_tidresp(): Success! result = %s.", json_string_value(jresult));
546 if ((NULL != (jservers = json_object_get(jresp, "servers"))) ||
547 (!json_is_array(jservers))) {
548 tresp->servers = tr_msg_decode_servers(tresp, jservers);
554 tresp->result = TID_SUCCESS;
557 tresp->result = TID_ERROR;
558 tr_debug("tr_msg_decode_tidresp(): Error! result = %s.", json_string_value(jresult));
559 if ((NULL != (jerr_msg = json_object_get(jresp, "err_msg"))) ||
560 (!json_is_string(jerr_msg))) {
561 tresp->err_msg = tr_new_name(json_string_value(jerr_msg));
563 tresp->err_msg = tr_new_name("No error message set.");
565 if (NULL !=(tresp->error_path = json_object_get(jresp, "error_path")))
566 json_incref(tresp->error_path);
569 tresp->rp_realm = tr_new_name(json_string_value(jrp_realm));
570 tresp->realm = tr_new_name(json_string_value(jrealm));
571 tresp->comm = tr_new_name(json_string_value(jcomm));
573 /* store optional "orig_coi" field */
574 if ((NULL != (jorig_coi = json_object_get(jresp, "orig_coi"))) &&
575 (!json_is_object(jorig_coi))) {
576 tresp->orig_coi = tr_new_name(json_string_value(jorig_coi));
583 /* Information records for TRP update msg
584 * requires that jrec already be allocated */
585 static TRP_RC tr_msg_encode_inforec_route(json_t *jrec, TRP_INFOREC *rec)
594 if (trp_inforec_get_trust_router(rec)==NULL)
597 s=tr_name_strdup(trp_inforec_get_trust_router(rec));
604 json_object_set_new(jrec, "trust_router", jstr);
606 jint=json_integer(trp_inforec_get_metric(rec));
609 json_object_set_new(jrec, "metric", jint);
611 jint=json_integer(trp_inforec_get_interval(rec));
614 json_object_set_new(jrec, "interval", jint);
619 /* returns a json array */
620 static json_t *tr_msg_encode_apcs(TR_APC *apcs)
622 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
623 TR_APC_ITER *iter=tr_apc_iter_new(tmp_ctx);
635 for (apc=tr_apc_iter_first(iter, apcs); apc!=NULL; apc=tr_apc_iter_next(iter)) {
636 jid=tr_name_to_json_string(tr_apc_get_id(apc));
637 if ((jid==NULL) || (json_array_append_new(jarray, jid)!=0)) {
645 talloc_free(tmp_ctx);
649 static TR_APC *tr_msg_decode_apcs(TALLOC_CTX *mem_ctx, json_t *jarray, TRP_RC *rc)
651 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
653 TR_APC *apc_list=NULL;
659 for (ii=0; ii<json_array_size(jarray); ii++) {
660 jstr=json_array_get(jarray, ii);
661 new=tr_apc_new(tmp_ctx);
662 if ((jstr==NULL) || (new==NULL) || (!json_is_string(jstr))) {
663 apc_list=NULL; /* these are all in tmp_ctx, so they'll still get cleaned up */
667 tr_apc_set_id(new, tr_new_name(json_string_value(jstr)));
668 if (tr_apc_get_id(new)==NULL) {
669 apc_list=NULL; /* these are all in tmp_ctx, so they'll still get cleaned up */
673 tr_apc_add(apc_list, new);
679 talloc_steal(mem_ctx, apc_list);
682 talloc_free(tmp_ctx);
686 static TRP_RC tr_msg_encode_inforec_comm(json_t *jrec, TRP_INFOREC *rec)
691 const char *sconst=NULL;
692 TR_COMM_TYPE commtype=TR_COMM_UNKNOWN;
697 commtype=trp_inforec_get_comm_type(rec);
698 if (commtype==TR_COMM_UNKNOWN) {
699 tr_notice("tr_msg_encode_inforec_comm: unknown community type.");
702 sconst=tr_comm_type_to_str(commtype);
705 jstr=json_string(sconst);
708 json_object_set_new(jrec, "type", jstr);
710 sconst=tr_realm_role_to_str(trp_inforec_get_role(rec));
712 tr_notice("tr_msg_encode_inforec_comm: unknown realm role.");
715 jstr=json_string(sconst);
718 json_object_set_new(jrec, "role", jstr);
720 japcs=tr_msg_encode_apcs(trp_inforec_get_apcs(rec));
722 tr_notice("tr_msg_encode_inforec_comm: error encoding APCs.");
725 json_object_set_new(jrec, "apcs", japcs);
728 if (trp_inforec_get_owner_realm(rec)!=NULL) {
729 jstr=tr_name_to_json_string(trp_inforec_get_owner_realm(rec));
732 json_object_set_new(jrec, "owner_realm", jstr);
735 if (trp_inforec_get_owner_contact(rec)!=NULL) {
736 jstr=tr_name_to_json_string(trp_inforec_get_owner_contact(rec));
739 json_object_set_new(jrec, "owner_contact", jstr);
742 json_object_set(jrec, "provenance", trp_inforec_get_provenance(rec));
744 jint=json_integer(trp_inforec_get_interval(rec));
747 json_object_set_new(jrec, "interval", jint);
752 static json_t *tr_msg_encode_inforec(TRP_INFOREC *rec)
757 if ((rec==NULL) || (trp_inforec_get_type(rec)==TRP_INFOREC_TYPE_UNKNOWN))
764 jstr=json_string(trp_inforec_type_to_string(trp_inforec_get_type(rec)));
769 json_object_set_new(jrec, "record_type", jstr);
772 case TRP_INFOREC_TYPE_ROUTE:
773 if (TRP_SUCCESS!=tr_msg_encode_inforec_route(jrec, rec)) {
778 case TRP_INFOREC_TYPE_COMMUNITY:
779 if (TRP_SUCCESS!=tr_msg_encode_inforec_comm(jrec, rec)) {
791 static TRP_RC tr_msg_decode_trp_inforec_route(json_t *jrecord, TRP_INFOREC *rec)
793 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
798 rc=tr_msg_get_json_string(jrecord, "trust_router", &s, tmp_ctx);
799 if (rc != TRP_SUCCESS)
801 if (TRP_SUCCESS!=trp_inforec_set_trust_router(rec, tr_new_name(s))) {
805 talloc_free(s); s=NULL;
807 trp_inforec_set_next_hop(rec, NULL); /* make sure this is null (filled in later) */
809 rc=tr_msg_get_json_integer(jrecord, "metric", &num);
810 if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_metric(rec,num)))
813 rc=tr_msg_get_json_integer(jrecord, "interval", &num);
814 if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_interval(rec,num)))
818 talloc_free(tmp_ctx);
822 static TRP_RC tr_msg_decode_trp_inforec_comm(json_t *jrecord, TRP_INFOREC *rec)
824 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
830 rc=tr_msg_get_json_string(jrecord, "type", &s, tmp_ctx);
831 if (rc != TRP_SUCCESS)
833 if (TRP_SUCCESS!=trp_inforec_set_comm_type(rec, tr_comm_type_from_str(s))) {
837 talloc_free(s); s=NULL;
839 rc=tr_msg_get_json_string(jrecord, "role", &s, tmp_ctx);
840 if (rc != TRP_SUCCESS)
842 if (TRP_SUCCESS!=trp_inforec_set_role(rec, tr_realm_role_from_str(s))) {
846 talloc_free(s); s=NULL;
848 apcs=tr_msg_decode_apcs(rec, json_object_get(jrecord, "apcs"), &rc);
849 if (rc!=TRP_SUCCESS) {
853 trp_inforec_set_apcs(rec, apcs);
855 rc=tr_msg_get_json_integer(jrecord, "interval", &num);
856 tr_debug("tr_msg_decode_trp_inforec_comm: interval=%u", num);
857 if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_interval(rec,num)))
860 trp_inforec_set_provenance(rec, json_object_get(jrecord, "provenance"));
863 rc=tr_msg_get_json_string(jrecord, "owner_realm", &s, tmp_ctx);
864 if (rc == TRP_SUCCESS) {
865 if (TRP_SUCCESS!=trp_inforec_set_owner_realm(rec, tr_new_name(s))) {
875 rc=tr_msg_get_json_string(jrecord, "owner_contact", &s, tmp_ctx);
876 if (rc == TRP_SUCCESS) {
877 if (TRP_SUCCESS!=trp_inforec_set_owner_contact(rec, tr_new_name(s))) {
888 talloc_free(tmp_ctx);
892 /* decode a single record */
893 static TRP_INFOREC *tr_msg_decode_trp_inforec(TALLOC_CTX *mem_ctx, json_t *jrecord)
895 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
896 TRP_INFOREC_TYPE rectype;
897 TRP_INFOREC *rec=NULL;
901 if (TRP_SUCCESS!=tr_msg_get_json_string(jrecord, "record_type", &s, tmp_ctx))
904 rectype=trp_inforec_type_from_string(s);
905 talloc_free(s); s=NULL;
907 rec=trp_inforec_new(tmp_ctx, rectype);
913 tr_debug("tr_msg_decode_trp_inforec: '%s' record found.", trp_inforec_type_to_string(rec->type));
915 switch(trp_inforec_get_type(rec)) {
916 case TRP_INFOREC_TYPE_ROUTE:
917 rc=tr_msg_decode_trp_inforec_route(jrecord, rec);
919 case TRP_INFOREC_TYPE_COMMUNITY:
920 rc=tr_msg_decode_trp_inforec_comm(jrecord, rec);
927 talloc_steal(mem_ctx, rec);
931 if (rc != TRP_SUCCESS) {
932 trp_inforec_free(rec);
935 talloc_free(tmp_ctx);
940 static json_t *tr_msg_encode_trp_upd(TRP_UPD *update)
942 json_t *jupdate=NULL;
943 json_t *jrecords=NULL;
952 jupdate=json_object();
956 s=tr_name_strdup(trp_upd_get_comm(update));
958 json_decref(jupdate);
964 json_decref(jupdate);
967 json_object_set_new(jupdate, "community", jstr);
969 s=tr_name_strdup(trp_upd_get_realm(update));
971 json_decref(jupdate);
977 json_decref(jupdate);
980 json_object_set_new(jupdate, "realm", jstr);
982 jrecords=json_array();
983 if (jrecords==NULL) {
984 json_decref(jupdate);
987 json_object_set_new(jupdate, "records", jrecords); /* jrecords now a "borrowed" reference */
988 for (rec=trp_upd_get_inforec(update); rec!=NULL; rec=trp_inforec_get_next(rec)) {
989 tr_debug("tr_msg_encode_trp_upd: encoding inforec.");
990 jrec=tr_msg_encode_inforec(rec);
992 json_decref(jupdate); /* also decs jrecords and any elements */
995 if (0!=json_array_append_new(jrecords, jrec)) {
996 json_decref(jupdate); /* also decs jrecords and any elements */
997 json_decref(jrec); /* this one did not get added so dec explicitly */
1005 /* Creates a linked list of records in the msg->body talloc context.
1006 * An error will be returned if any unparseable records are encountered.
1008 static TRP_UPD *tr_msg_decode_trp_upd(TALLOC_CTX *mem_ctx, json_t *jupdate)
1010 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1011 json_t *jrecords=NULL;
1013 TRP_UPD *update=NULL;
1014 TRP_INFOREC *new_rec=NULL;
1015 TRP_INFOREC *list_tail=NULL;
1018 TRP_RC rc=TRP_ERROR;
1020 update=trp_upd_new(tmp_ctx);
1026 rc=tr_msg_get_json_string(jupdate, "community", &s, tmp_ctx);
1027 if (rc != TRP_SUCCESS) {
1028 tr_debug("tr_msg_decode_trp_upd: no community in TRP update message.");
1032 name=tr_new_name(s);
1034 tr_debug("tr_msg_decode_trp_upd: could not allocate community name.");
1038 talloc_free(s); s=NULL;
1039 trp_upd_set_comm(update, name);
1041 rc=tr_msg_get_json_string(jupdate, "realm", &s, tmp_ctx);
1042 if (rc != TRP_SUCCESS) {
1043 tr_debug("tr_msg_decode_trp_upd: no realm in TRP update message.");
1047 name=tr_new_name(s);
1049 tr_debug("tr_msg_decode_trp_upd: could not allocate realm name.");
1053 talloc_free(s); s=NULL;
1054 trp_upd_set_realm(update, name);
1056 jrecords=json_object_get(jupdate, "records");
1057 if ((jrecords==NULL) || (!json_is_array(jrecords))) {
1062 tr_debug("tr_msg_decode_trp_upd: found %d records", json_array_size(jrecords));
1063 /* process the array */
1064 for (ii=0; ii<json_array_size(jrecords); ii++) {
1065 new_rec=tr_msg_decode_trp_inforec(update, json_array_get(jrecords, ii));
1066 if (new_rec==NULL) {
1071 if (list_tail==NULL)
1072 trp_upd_set_inforec(update, new_rec); /* first is a special case */
1074 trp_inforec_set_next(list_tail, new_rec);
1079 /* Succeeded. Move new allocations into the correct talloc context */
1080 talloc_steal(mem_ctx, update);
1084 talloc_free(tmp_ctx);
1085 if (rc!=TRP_SUCCESS)
1090 static json_t *tr_msg_encode_trp_req(TRP_REQ *req)
1099 jbody=json_object();
1103 if ((NULL==trp_req_get_comm(req))
1104 || (NULL==trp_req_get_realm(req))) {
1109 s=tr_name_strdup(trp_req_get_comm(req)); /* ensures null termination */
1114 jstr=json_string(s);
1120 json_object_set_new(jbody, "community", jstr);
1122 s=tr_name_strdup(trp_req_get_realm(req)); /* ensures null termination */
1127 jstr=json_string(s);
1133 json_object_set_new(jbody, "realm", jstr);
1138 static TRP_REQ *tr_msg_decode_trp_req(TALLOC_CTX *mem_ctx, json_t *jreq)
1140 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1143 TRP_RC rc=TRP_ERROR;
1145 /* check message type and body type for agreement */
1146 req=trp_req_new(tmp_ctx);
1152 rc=tr_msg_get_json_string(jreq, "community", &s, tmp_ctx);
1153 if (rc!=TRP_SUCCESS)
1155 trp_req_set_comm(req, tr_new_name(s));
1156 talloc_free(s); s=NULL;
1158 rc=tr_msg_get_json_string(jreq, "realm", &s, tmp_ctx);
1159 if (rc!=TRP_SUCCESS)
1161 trp_req_set_realm(req, tr_new_name(s));
1162 talloc_free(s); s=NULL;
1165 talloc_steal(mem_ctx, req);
1168 talloc_free(tmp_ctx);
1169 if (rc!=TRP_SUCCESS)
1174 char *tr_msg_encode(TALLOC_CTX *mem_ctx, TR_MSG *msg)
1177 json_t *jmsg_type=NULL;
1178 char *encoded_tmp=NULL;
1180 TID_RESP *tidresp=NULL;
1181 TID_REQ *tidreq=NULL;
1182 TRP_UPD *trpupd=NULL;
1183 TRP_REQ *trpreq=NULL;
1184 MON_REQ *monreq=NULL;
1185 MON_RESP *monresp=NULL;
1187 /* TBD -- add error handling */
1188 jmsg = json_object();
1190 switch (msg->msg_type) {
1192 jmsg_type = json_string("tid_request");
1193 json_object_set_new(jmsg, "msg_type", jmsg_type);
1194 tidreq=tr_msg_get_req(msg);
1195 json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidreq(tidreq));
1199 jmsg_type = json_string("tid_response");
1200 json_object_set_new(jmsg, "msg_type", jmsg_type);
1201 tidresp=tr_msg_get_resp(msg);
1202 json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidresp(tidresp));
1206 jmsg_type = json_string("trp_update");
1207 json_object_set_new(jmsg, "msg_type", jmsg_type);
1208 trpupd=tr_msg_get_trp_upd(msg);
1209 json_object_set_new(jmsg, "msg_body", tr_msg_encode_trp_upd(trpupd));
1213 jmsg_type = json_string("trp_request");
1214 json_object_set_new(jmsg, "msg_type", jmsg_type);
1215 trpreq=tr_msg_get_trp_req(msg);
1216 json_object_set_new(jmsg, "msg_body", tr_msg_encode_trp_req(trpreq));
1220 jmsg_type = json_string("mon_request");
1221 json_object_set_new(jmsg, "msg_type", jmsg_type);
1222 monreq=tr_msg_get_mon_req(msg);
1223 json_object_set_new(jmsg, "msg_body", mon_req_encode(monreq));
1227 jmsg_type = json_string("mon_response");
1228 json_object_set_new(jmsg, "msg_type", jmsg_type);
1229 monresp=tr_msg_get_mon_resp(msg);
1230 json_object_set_new(jmsg, "msg_body", mon_resp_encode(monresp));
1238 /* We should perhaps use json_set_alloc_funcs to automatically use talloc, but for
1239 * now, we'll encode to a malloc'ed buffer, then copy that to a talloc'ed buffer. */
1240 encoded_tmp=json_dumps(jmsg, 0); // malloc'ed version
1241 json_decref(jmsg); // free the JSON structure
1242 encoded = talloc_strdup(mem_ctx, encoded_tmp); // get the talloc'ed version
1243 free(encoded_tmp); // free the malloc'ed version
1245 tr_debug("tr_msg_encode: outgoing msg=%s", encoded);
1249 TR_MSG *tr_msg_decode(TALLOC_CTX *mem_ctx, const char *jbuf, size_t buflen)
1252 json_t *jmsg = NULL;
1256 const char *mtype = NULL;
1258 if (NULL == (jmsg = json_loadb(jbuf, buflen, JSON_DISABLE_EOF_CHECK, &rc))) {
1259 tr_debug("tr_msg_decode(): error loading object");
1263 if (!(msg = talloc_zero(mem_ctx, TR_MSG))) {
1264 tr_debug("tr_msg_decode(): Error allocating TR_MSG structure.");
1269 if ((NULL == (jtype = json_object_get(jmsg, "msg_type"))) ||
1270 (NULL == (jbody = json_object_get(jmsg, "msg_body")))) {
1271 tr_debug("tr_msg_decode(): Error parsing message header.");
1273 tr_msg_free_decoded(msg);
1277 mtype = json_string_value(jtype);
1279 if (0 == strcmp(mtype, "tid_request")) {
1280 msg->msg_type = TID_REQUEST;
1281 tr_msg_set_req(msg, tr_msg_decode_tidreq(msg, jbody));
1283 else if (0 == strcmp(mtype, "tid_response")) {
1284 msg->msg_type = TID_RESPONSE;
1285 tr_msg_set_resp(msg, tr_msg_decode_tidresp(msg, jbody));
1287 else if (0 == strcmp(mtype, "trp_update")) {
1288 msg->msg_type = TRP_UPDATE;
1289 tr_msg_set_trp_upd(msg, tr_msg_decode_trp_upd(msg, jbody)); /* null talloc context for now */
1291 else if (0 == strcmp(mtype, "trp_request")) {
1292 msg->msg_type = TRP_UPDATE;
1293 tr_msg_set_trp_req(msg, tr_msg_decode_trp_req(msg, jbody)); /* null talloc context for now */
1295 else if (0 == strcmp(mtype, "mon_request")) {
1296 msg->msg_type = MON_REQUEST;
1297 tr_msg_set_mon_req(msg, mon_req_decode(msg, jbody));
1299 /* We do not currently handle monitoring responses */
1300 // else if (0 == strcmp(mtype, "mon_response")) {
1301 // msg->msg_type = MON_RESPONSE;
1302 // tr_msg_set_mon_resp(msg, mon_resp_decode(NULL, jbody));
1305 msg->msg_type = TR_UNKNOWN;
1306 msg->msg_rep = NULL;
1314 void tr_msg_free_encoded(char *jmsg)
1320 void tr_msg_free_decoded(TR_MSG *msg)