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>
48 #include <tr_name_internal.h>
49 #include <trp_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 TID_REQ *tr_msg_get_req(TR_MSG *msg)
106 if (msg->msg_type == TID_REQUEST)
107 return (TID_REQ *)msg->msg_rep;
111 void tr_msg_set_req(TR_MSG *msg, TID_REQ *req)
114 msg->msg_type = TID_REQUEST;
117 TID_RESP *tr_msg_get_resp(TR_MSG *msg)
119 if (msg->msg_type == TID_RESPONSE)
120 return (TID_RESP *)msg->msg_rep;
124 void tr_msg_set_resp(TR_MSG *msg, TID_RESP *resp)
127 msg->msg_type = TID_RESPONSE;
130 TRP_UPD *tr_msg_get_trp_upd(TR_MSG *msg)
132 if (msg->msg_type == TRP_UPDATE)
133 return (TRP_UPD *)msg->msg_rep;
137 void tr_msg_set_trp_upd(TR_MSG *msg, TRP_UPD *update)
140 talloc_steal(NULL, update); /* should attach to msg, but TR_MSG not usually talloc'ed */
141 msg->msg_type=TRP_UPDATE;
144 TRP_REQ *tr_msg_get_trp_req(TR_MSG *msg)
146 if (msg->msg_type == TRP_REQUEST)
147 return (TRP_REQ *)msg->msg_rep;
151 void tr_msg_set_trp_req(TR_MSG *msg, TRP_REQ *req)
154 msg->msg_type=TRP_REQUEST;
157 static json_t *tr_msg_encode_dh(DH *dh)
163 if ((!dh) || (!dh->p) || (!dh->g) || (!dh->pub_key))
168 jbn = json_string(s=BN_bn2hex(dh->p));
170 json_object_set_new(jdh, "dh_p", jbn);
172 jbn = json_string(s=BN_bn2hex(dh->g));
174 json_object_set_new(jdh, "dh_g", jbn);
176 jbn = json_string(s=BN_bn2hex(dh->pub_key));
178 json_object_set_new(jdh, "dh_pub_key", jbn);
183 static DH *tr_msg_decode_dh(json_t *jdh)
188 json_t *jpub_key = NULL;
190 if (!(dh=tr_dh_new())) {
191 tr_crit("tr_msg_decode_dh(): Error allocating DH structure.");
195 /* store required fields from dh object */
196 if ((NULL == (jp = json_object_get(jdh, "dh_p"))) ||
197 (NULL == (jg = json_object_get(jdh, "dh_g"))) ||
198 (NULL == (jpub_key = json_object_get(jdh, "dh_pub_key")))) {
199 tr_debug("tr_msg_decode_dh(): Error parsing dh_info.");
204 BN_hex2bn(&(dh->p), json_string_value(jp));
205 BN_hex2bn(&(dh->g), json_string_value(jg));
206 BN_hex2bn(&(dh->pub_key), json_string_value(jpub_key));
211 static json_t * tr_msg_encode_tidreq(TID_REQ *req)
216 if ((!req) || (!req->rp_realm) || (!req->realm) || !(req->comm))
219 assert(jreq = json_object());
221 jstr = tr_name_to_json_string(req->rp_realm);
222 json_object_set_new(jreq, "rp_realm", jstr);
224 jstr = tr_name_to_json_string(req->realm);
225 json_object_set_new(jreq, "target_realm", jstr);
227 jstr = tr_name_to_json_string(req->comm);
228 json_object_set_new(jreq, "community", jstr);
231 jstr = tr_name_to_json_string(req->orig_coi);
232 json_object_set_new(jreq, "orig_coi", jstr);
235 json_object_set_new(jreq, "dh_info", tr_msg_encode_dh(req->tidc_dh));
238 json_object_set(jreq, "constraints", (json_t *) req->cons);
241 json_object_set(jreq, "path", req->path);
242 if (req->expiration_interval)
243 json_object_set_new(jreq, "expiration_interval",
244 json_integer(req->expiration_interval));
249 static TID_REQ *tr_msg_decode_tidreq(json_t *jreq)
251 TID_REQ *treq = NULL;
252 json_t *jrp_realm = NULL;
253 json_t *jrealm = NULL;
254 json_t *jcomm = NULL;
255 json_t *jorig_coi = NULL;
257 json_t *jpath = NULL;
258 json_t *jexpire_interval = NULL;
260 if (!(treq =tid_req_new())) {
261 tr_crit("tr_msg_decode_tidreq(): Error allocating TID_REQ structure.");
265 /* store required fields from request */
266 if ((NULL == (jrp_realm = json_object_get(jreq, "rp_realm"))) ||
267 (NULL == (jrealm = json_object_get(jreq, "target_realm"))) ||
268 (NULL == (jcomm = json_object_get(jreq, "community")))) {
269 tr_notice("tr_msg_decode(): Error parsing required fields.");
274 jpath = json_object_get(jreq, "path");
275 jexpire_interval = json_object_get(jreq, "expiration_interval");
277 treq->rp_realm = tr_new_name((char *)json_string_value(jrp_realm));
278 treq->realm = tr_new_name((char *)json_string_value(jrealm));
279 treq->comm = tr_new_name((char *)json_string_value(jcomm));
281 /* Get DH Info from the request */
282 if (NULL == (jdh = json_object_get(jreq, "dh_info"))) {
283 tr_debug("tr_msg_decode(): Error parsing dh_info.");
287 treq->tidc_dh = tr_msg_decode_dh(jdh);
289 /* store optional "orig_coi" field */
290 if (NULL != (jorig_coi = json_object_get(jreq, "orig_coi"))) {
291 treq->orig_coi = tr_new_name((char *)json_string_value(jorig_coi));
294 treq->cons = (TR_CONSTRAINT_SET *) json_object_get(jreq, "constraints");
296 if (!tr_constraint_set_validate(treq->cons)) {
297 tr_debug("Constraint set validation failed");
301 json_incref((json_t *) treq->cons);
302 tid_req_cleanup_json(treq, (json_t *) treq->cons);
307 tid_req_cleanup_json(treq, jpath);
309 if (jexpire_interval)
310 treq->expiration_interval = json_integer_value(jexpire_interval);
315 static json_t *tr_msg_encode_one_server(TID_SRVR_BLK *srvr)
317 json_t *jsrvr = NULL;
319 gchar *time_str = g_time_val_to_iso8601(&srvr->key_expiration);
321 tr_debug("Encoding one server.");
323 jsrvr = json_object();
325 jstr = json_string(srvr->aaa_server_addr);
326 json_object_set_new(jsrvr, "server_addr", jstr);
328 json_object_set_new(jsrvr,
329 "key_expiration", json_string(time_str));
331 /* Server DH Block */
332 jstr = tr_name_to_json_string(srvr->key_name);
333 json_object_set_new(jsrvr, "key_name", jstr);
334 json_object_set_new(jsrvr, "server_dh", tr_msg_encode_dh(srvr->aaa_server_dh));
336 /* The path is owned by the srvr, so grab an extra ref*/
337 json_object_set(jsrvr, "path", (json_t *)(srvr->path));
341 static int tr_msg_decode_one_server(json_t *jsrvr, TID_SRVR_BLK *srvr)
343 json_t *jsrvr_addr = NULL;
344 json_t *jsrvr_kn = NULL;
345 json_t *jsrvr_dh = NULL;
346 json_t *jsrvr_expire = NULL;
352 if ((NULL == (jsrvr_addr = json_object_get(jsrvr, "server_addr"))) ||
353 (NULL == (jsrvr_kn = json_object_get(jsrvr, "key_name"))) ||
354 (NULL == (jsrvr_dh = json_object_get(jsrvr, "server_dh")))) {
355 tr_notice("tr_msg_decode_one_server(): Error parsing required fields.");
359 srvr->aaa_server_addr=talloc_strdup(srvr, json_string_value(jsrvr_addr));
360 srvr->key_name = tr_new_name((char *)json_string_value(jsrvr_kn));
361 srvr->aaa_server_dh = tr_msg_decode_dh(jsrvr_dh);
362 tid_srvr_blk_set_path(srvr, (TID_PATH *) json_object_get(jsrvr, "path"));
363 jsrvr_expire = json_object_get(jsrvr, "key_expiration");
364 if (jsrvr_expire && json_is_string(jsrvr_expire)) {
365 if (!g_time_val_from_iso8601(json_string_value(jsrvr_expire),
366 &srvr->key_expiration))
367 tr_notice("Key expiration %s cannot be parsed", json_string_value(jsrvr_expire));
373 static json_t *tr_msg_encode_servers(TID_RESP *resp)
375 json_t *jservers = NULL;
376 json_t *jsrvr = NULL;
377 TID_SRVR_BLK *srvr = NULL;
380 jservers = json_array();
382 tid_resp_servers_foreach(resp, srvr, index) {
383 if ((NULL == (jsrvr = tr_msg_encode_one_server(srvr))) ||
384 (-1 == json_array_append_new(jservers, jsrvr))) {
389 // tr_debug("tr_msg_encode_servers(): servers contains:");
390 // tr_debug("%s", json_dumps(jservers, 0));
394 static TID_SRVR_BLK *tr_msg_decode_servers(TALLOC_CTX *mem_ctx, json_t *jservers)
396 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
397 TID_SRVR_BLK *servers=NULL;
398 TID_SRVR_BLK *new_srvr=NULL;
400 size_t i, num_servers;
402 num_servers = json_array_size(jservers);
403 tr_debug("tr_msg_decode_servers(): Number of servers = %u.", (unsigned) num_servers);
405 if (0 == num_servers) {
406 tr_debug("tr_msg_decode_servers(): Server array is empty.");
410 for (i = 0; i < num_servers; i++) {
411 jsrvr = json_array_get(jservers, i);
413 new_srvr=tid_srvr_blk_new(tmp_ctx);
414 if (new_srvr==NULL) {
415 servers=NULL; /* it's all in tmp_ctx, so we can just let go */
419 if (0 != tr_msg_decode_one_server(jsrvr, new_srvr)) {
420 servers=NULL; /* it's all in tmp_ctx, so we can just let go */
424 tid_srvr_blk_add(servers, new_srvr);
427 talloc_steal(mem_ctx, servers);
430 talloc_free(tmp_ctx);
434 static json_t * tr_msg_encode_tidresp(TID_RESP *resp)
436 json_t *jresp = NULL;
438 json_t *jservers = NULL;
440 if ((!resp) || (!resp->rp_realm) || (!resp->realm) || !(resp->comm))
443 jresp = json_object();
445 if (TID_ERROR == resp->result) {
446 jstr = json_string("error");
447 json_object_set_new(jresp, "result", jstr);
449 jstr = tr_name_to_json_string(resp->err_msg);
450 json_object_set_new(jresp, "err_msg", jstr);
454 jstr = json_string("success");
455 json_object_set_new(jresp, "result", jstr);
458 jstr = tr_name_to_json_string(resp->rp_realm);
459 json_object_set_new(jresp, "rp_realm", jstr);
461 jstr = tr_name_to_json_string(resp->realm);
462 json_object_set_new(jresp, "target_realm", jstr);
464 jstr = tr_name_to_json_string(resp->comm);
465 json_object_set_new(jresp, "comm", jstr);
467 if (resp->orig_coi) {
468 jstr = tr_name_to_json_string(resp->orig_coi);
469 json_object_set_new(jresp, "orig_coi", jstr);
472 if (NULL == resp->servers) {
473 tr_debug("tr_msg_encode_tidresp(): No servers to encode.");
476 jservers = tr_msg_encode_servers(resp);
477 json_object_set_new(jresp, "servers", jservers);
479 if (resp->error_path)
480 json_object_set(jresp, "error_path", resp->error_path);
486 static TID_RESP *tr_msg_decode_tidresp(json_t *jresp)
488 TID_RESP *tresp = NULL;
489 json_t *jresult = NULL;
490 json_t *jrp_realm = NULL;
491 json_t *jrealm = NULL;
492 json_t *jcomm = NULL;
493 json_t *jorig_coi = NULL;
494 json_t *jservers = NULL;
495 json_t *jerr_msg = NULL;
497 if (!(tresp=tid_resp_new(NULL))) {
498 tr_crit("tr_msg_decode_tidresp(): Error allocating TID_RESP structure.");
503 /* store required fields from response */
504 if ((NULL == (jresult = json_object_get(jresp, "result"))) ||
505 (!json_is_string(jresult)) ||
506 (NULL == (jrp_realm = json_object_get(jresp, "rp_realm"))) ||
507 (!json_is_string(jrp_realm)) ||
508 (NULL == (jrealm = json_object_get(jresp, "target_realm"))) ||
509 (!json_is_string(jrealm)) ||
510 (NULL == (jcomm = json_object_get(jresp, "comm"))) ||
511 (!json_is_string(jcomm))) {
512 tr_debug("tr_msg_decode_tidresp(): Error parsing response.");
517 if (0 == (strcmp(json_string_value(jresult), "success"))) {
518 tr_debug("tr_msg_decode_tidresp(): Success! result = %s.", json_string_value(jresult));
519 if ((NULL != (jservers = json_object_get(jresp, "servers"))) ||
520 (!json_is_array(jservers))) {
521 tresp->servers = tr_msg_decode_servers(tresp, jservers);
527 tresp->result = TID_SUCCESS;
530 tresp->result = TID_ERROR;
531 tr_debug("tr_msg_decode_tidresp(): Error! result = %s.", json_string_value(jresult));
532 if ((NULL != (jerr_msg = json_object_get(jresp, "err_msg"))) ||
533 (!json_is_string(jerr_msg))) {
534 tresp->err_msg = tr_new_name(json_string_value(jerr_msg));
536 tresp->err_msg = tr_new_name("No error message set.");
538 if (NULL !=(tresp->error_path = json_object_get(jresp, "error_path")))
539 json_incref(tresp->error_path);
542 tresp->rp_realm = tr_new_name(json_string_value(jrp_realm));
543 tresp->realm = tr_new_name(json_string_value(jrealm));
544 tresp->comm = tr_new_name(json_string_value(jcomm));
546 /* store optional "orig_coi" field */
547 if ((NULL != (jorig_coi = json_object_get(jresp, "orig_coi"))) &&
548 (!json_is_object(jorig_coi))) {
549 tresp->orig_coi = tr_new_name(json_string_value(jorig_coi));
556 /* Information records for TRP update msg
557 * requires that jrec already be allocated */
558 static TRP_RC tr_msg_encode_inforec_route(json_t *jrec, TRP_INFOREC *rec)
567 if (trp_inforec_get_trust_router(rec)==NULL)
570 s=tr_name_strdup(trp_inforec_get_trust_router(rec));
577 json_object_set_new(jrec, "trust_router", jstr);
579 jint=json_integer(trp_inforec_get_metric(rec));
582 json_object_set_new(jrec, "metric", jint);
584 jint=json_integer(trp_inforec_get_interval(rec));
587 json_object_set_new(jrec, "interval", jint);
592 /* returns a json array */
593 static json_t *tr_msg_encode_apcs(TR_APC *apcs)
595 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
596 TR_APC_ITER *iter=tr_apc_iter_new(tmp_ctx);
608 for (apc=tr_apc_iter_first(iter, apcs); apc!=NULL; apc=tr_apc_iter_next(iter)) {
609 jid=tr_name_to_json_string(tr_apc_get_id(apc));
610 if ((jid==NULL) || (json_array_append_new(jarray, jid)!=0)) {
618 talloc_free(tmp_ctx);
622 static TR_APC *tr_msg_decode_apcs(TALLOC_CTX *mem_ctx, json_t *jarray, TRP_RC *rc)
624 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
626 TR_APC *apc_list=NULL;
632 for (ii=0; ii<json_array_size(jarray); ii++) {
633 jstr=json_array_get(jarray, ii);
634 new=tr_apc_new(tmp_ctx);
635 if ((jstr==NULL) || (new==NULL) || (!json_is_string(jstr))) {
636 apc_list=NULL; /* these are all in tmp_ctx, so they'll still get cleaned up */
640 tr_apc_set_id(new, tr_new_name(json_string_value(jstr)));
641 if (tr_apc_get_id(new)==NULL) {
642 apc_list=NULL; /* these are all in tmp_ctx, so they'll still get cleaned up */
646 tr_apc_add(apc_list, new);
652 talloc_steal(mem_ctx, apc_list);
655 talloc_free(tmp_ctx);
659 static TRP_RC tr_msg_encode_inforec_comm(json_t *jrec, TRP_INFOREC *rec)
664 const char *sconst=NULL;
665 TR_COMM_TYPE commtype=TR_COMM_UNKNOWN;
670 commtype=trp_inforec_get_comm_type(rec);
671 if (commtype==TR_COMM_UNKNOWN) {
672 tr_notice("tr_msg_encode_inforec_comm: unknown community type.");
675 sconst=tr_comm_type_to_str(commtype);
678 jstr=json_string(sconst);
681 json_object_set_new(jrec, "type", jstr);
683 sconst=tr_realm_role_to_str(trp_inforec_get_role(rec));
685 tr_notice("tr_msg_encode_inforec_comm: unknown realm role.");
688 jstr=json_string(sconst);
691 json_object_set_new(jrec, "role", jstr);
693 japcs=tr_msg_encode_apcs(trp_inforec_get_apcs(rec));
695 tr_notice("tr_msg_encode_inforec_comm: error encoding APCs.");
698 json_object_set_new(jrec, "apcs", japcs);
701 if (trp_inforec_get_owner_realm(rec)!=NULL) {
702 jstr=tr_name_to_json_string(trp_inforec_get_owner_realm(rec));
705 json_object_set_new(jrec, "owner_realm", jstr);
708 if (trp_inforec_get_owner_contact(rec)!=NULL) {
709 jstr=tr_name_to_json_string(trp_inforec_get_owner_contact(rec));
712 json_object_set_new(jrec, "owner_contact", jstr);
715 json_object_set(jrec, "provenance", trp_inforec_get_provenance(rec));
717 jint=json_integer(trp_inforec_get_interval(rec));
720 json_object_set_new(jrec, "interval", jint);
725 static json_t *tr_msg_encode_inforec(TRP_INFOREC *rec)
730 if ((rec==NULL) || (trp_inforec_get_type(rec)==TRP_INFOREC_TYPE_UNKNOWN))
737 jstr=json_string(trp_inforec_type_to_string(trp_inforec_get_type(rec)));
742 json_object_set_new(jrec, "record_type", jstr);
745 case TRP_INFOREC_TYPE_ROUTE:
746 if (TRP_SUCCESS!=tr_msg_encode_inforec_route(jrec, rec)) {
751 case TRP_INFOREC_TYPE_COMMUNITY:
752 if (TRP_SUCCESS!=tr_msg_encode_inforec_comm(jrec, rec)) {
764 static TRP_RC tr_msg_decode_trp_inforec_route(json_t *jrecord, TRP_INFOREC *rec)
766 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
771 rc=tr_msg_get_json_string(jrecord, "trust_router", &s, tmp_ctx);
772 if (rc != TRP_SUCCESS)
774 if (TRP_SUCCESS!=trp_inforec_set_trust_router(rec, tr_new_name(s))) {
778 talloc_free(s); s=NULL;
780 trp_inforec_set_next_hop(rec, NULL); /* make sure this is null (filled in later) */
782 rc=tr_msg_get_json_integer(jrecord, "metric", &num);
783 if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_metric(rec,num)))
786 rc=tr_msg_get_json_integer(jrecord, "interval", &num);
787 if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_interval(rec,num)))
791 talloc_free(tmp_ctx);
795 static TRP_RC tr_msg_decode_trp_inforec_comm(json_t *jrecord, TRP_INFOREC *rec)
797 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
803 rc=tr_msg_get_json_string(jrecord, "type", &s, tmp_ctx);
804 if (rc != TRP_SUCCESS)
806 if (TRP_SUCCESS!=trp_inforec_set_comm_type(rec, tr_comm_type_from_str(s))) {
810 talloc_free(s); s=NULL;
812 rc=tr_msg_get_json_string(jrecord, "role", &s, tmp_ctx);
813 if (rc != TRP_SUCCESS)
815 if (TRP_SUCCESS!=trp_inforec_set_role(rec, tr_realm_role_from_str(s))) {
819 talloc_free(s); s=NULL;
821 apcs=tr_msg_decode_apcs(rec, json_object_get(jrecord, "apcs"), &rc);
822 if (rc!=TRP_SUCCESS) {
826 trp_inforec_set_apcs(rec, apcs);
828 rc=tr_msg_get_json_integer(jrecord, "interval", &num);
829 tr_debug("tr_msg_decode_trp_inforec_comm: interval=%u", num);
830 if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_interval(rec,num)))
833 trp_inforec_set_provenance(rec, json_object_get(jrecord, "provenance"));
836 rc=tr_msg_get_json_string(jrecord, "owner_realm", &s, tmp_ctx);
837 if (rc == TRP_SUCCESS) {
838 if (TRP_SUCCESS!=trp_inforec_set_owner_realm(rec, tr_new_name(s))) {
848 rc=tr_msg_get_json_string(jrecord, "owner_contact", &s, tmp_ctx);
849 if (rc == TRP_SUCCESS) {
850 if (TRP_SUCCESS!=trp_inforec_set_owner_contact(rec, tr_new_name(s))) {
861 talloc_free(tmp_ctx);
865 /* decode a single record */
866 static TRP_INFOREC *tr_msg_decode_trp_inforec(TALLOC_CTX *mem_ctx, json_t *jrecord)
868 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
869 TRP_INFOREC_TYPE rectype;
870 TRP_INFOREC *rec=NULL;
874 if (TRP_SUCCESS!=tr_msg_get_json_string(jrecord, "record_type", &s, tmp_ctx))
877 rectype=trp_inforec_type_from_string(s);
878 talloc_free(s); s=NULL;
880 rec=trp_inforec_new(tmp_ctx, rectype);
886 tr_debug("tr_msg_decode_trp_inforec: '%s' record found.", trp_inforec_type_to_string(rec->type));
888 switch(trp_inforec_get_type(rec)) {
889 case TRP_INFOREC_TYPE_ROUTE:
890 rc=tr_msg_decode_trp_inforec_route(jrecord, rec);
892 case TRP_INFOREC_TYPE_COMMUNITY:
893 rc=tr_msg_decode_trp_inforec_comm(jrecord, rec);
900 talloc_steal(mem_ctx, rec);
904 if (rc != TRP_SUCCESS) {
905 trp_inforec_free(rec);
908 talloc_free(tmp_ctx);
913 static json_t *tr_msg_encode_trp_upd(TRP_UPD *update)
915 json_t *jupdate=NULL;
916 json_t *jrecords=NULL;
925 jupdate=json_object();
929 s=tr_name_strdup(trp_upd_get_comm(update));
931 json_decref(jupdate);
937 json_decref(jupdate);
940 json_object_set_new(jupdate, "community", jstr);
942 s=tr_name_strdup(trp_upd_get_realm(update));
944 json_decref(jupdate);
950 json_decref(jupdate);
953 json_object_set_new(jupdate, "realm", jstr);
955 jrecords=json_array();
956 if (jrecords==NULL) {
957 json_decref(jupdate);
960 json_object_set_new(jupdate, "records", jrecords); /* jrecords now a "borrowed" reference */
961 for (rec=trp_upd_get_inforec(update); rec!=NULL; rec=trp_inforec_get_next(rec)) {
962 tr_debug("tr_msg_encode_trp_upd: encoding inforec.");
963 jrec=tr_msg_encode_inforec(rec);
965 json_decref(jupdate); /* also decs jrecords and any elements */
968 if (0!=json_array_append_new(jrecords, jrec)) {
969 json_decref(jupdate); /* also decs jrecords and any elements */
970 json_decref(jrec); /* this one did not get added so dec explicitly */
978 /* Creates a linked list of records in the msg->body talloc context.
979 * An error will be returned if any unparseable records are encountered.
981 static TRP_UPD *tr_msg_decode_trp_upd(TALLOC_CTX *mem_ctx, json_t *jupdate)
983 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
984 json_t *jrecords=NULL;
986 TRP_UPD *update=NULL;
987 TRP_INFOREC *new_rec=NULL;
988 TRP_INFOREC *list_tail=NULL;
993 update=trp_upd_new(tmp_ctx);
999 rc=tr_msg_get_json_string(jupdate, "community", &s, tmp_ctx);
1000 if (rc != TRP_SUCCESS) {
1001 tr_debug("tr_msg_decode_trp_upd: no community in TRP update message.");
1005 name=tr_new_name(s);
1007 tr_debug("tr_msg_decode_trp_upd: could not allocate community name.");
1011 talloc_free(s); s=NULL;
1012 trp_upd_set_comm(update, name);
1014 rc=tr_msg_get_json_string(jupdate, "realm", &s, tmp_ctx);
1015 if (rc != TRP_SUCCESS) {
1016 tr_debug("tr_msg_decode_trp_upd: no realm in TRP update message.");
1020 name=tr_new_name(s);
1022 tr_debug("tr_msg_decode_trp_upd: could not allocate realm name.");
1026 talloc_free(s); s=NULL;
1027 trp_upd_set_realm(update, name);
1029 jrecords=json_object_get(jupdate, "records");
1030 if ((jrecords==NULL) || (!json_is_array(jrecords))) {
1035 tr_debug("tr_msg_decode_trp_upd: found %d records", json_array_size(jrecords));
1036 /* process the array */
1037 for (ii=0; ii<json_array_size(jrecords); ii++) {
1038 new_rec=tr_msg_decode_trp_inforec(update, json_array_get(jrecords, ii));
1039 if (new_rec==NULL) {
1044 if (list_tail==NULL)
1045 trp_upd_set_inforec(update, new_rec); /* first is a special case */
1047 trp_inforec_set_next(list_tail, new_rec);
1052 /* Succeeded. Move new allocations into the correct talloc context */
1053 talloc_steal(mem_ctx, update);
1057 talloc_free(tmp_ctx);
1058 if (rc!=TRP_SUCCESS)
1063 static json_t *tr_msg_encode_trp_req(TRP_REQ *req)
1072 jbody=json_object();
1076 if ((NULL==trp_req_get_comm(req))
1077 || (NULL==trp_req_get_realm(req))) {
1082 s=tr_name_strdup(trp_req_get_comm(req)); /* ensures null termination */
1087 jstr=json_string(s);
1093 json_object_set_new(jbody, "community", jstr);
1095 s=tr_name_strdup(trp_req_get_realm(req)); /* ensures null termination */
1100 jstr=json_string(s);
1106 json_object_set_new(jbody, "realm", jstr);
1111 static TRP_REQ *tr_msg_decode_trp_req(TALLOC_CTX *mem_ctx, json_t *jreq)
1113 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1116 TRP_RC rc=TRP_ERROR;
1118 /* check message type and body type for agreement */
1119 req=trp_req_new(tmp_ctx);
1125 rc=tr_msg_get_json_string(jreq, "community", &s, tmp_ctx);
1126 if (rc!=TRP_SUCCESS)
1128 trp_req_set_comm(req, tr_new_name(s));
1129 talloc_free(s); s=NULL;
1131 rc=tr_msg_get_json_string(jreq, "realm", &s, tmp_ctx);
1132 if (rc!=TRP_SUCCESS)
1134 trp_req_set_realm(req, tr_new_name(s));
1135 talloc_free(s); s=NULL;
1138 talloc_steal(mem_ctx, req);
1141 talloc_free(tmp_ctx);
1142 if (rc!=TRP_SUCCESS)
1147 char *tr_msg_encode(TR_MSG *msg)
1150 json_t *jmsg_type=NULL;
1152 TID_RESP *tidresp=NULL;
1153 TID_REQ *tidreq=NULL;
1154 TRP_UPD *trpupd=NULL;
1155 TRP_REQ *trpreq=NULL;
1157 /* TBD -- add error handling */
1158 jmsg = json_object();
1160 switch (msg->msg_type)
1163 jmsg_type = json_string("tid_request");
1164 json_object_set_new(jmsg, "msg_type", jmsg_type);
1165 tidreq=tr_msg_get_req(msg);
1166 json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidreq(tidreq));
1170 jmsg_type = json_string("tid_response");
1171 json_object_set_new(jmsg, "msg_type", jmsg_type);
1172 tidresp=tr_msg_get_resp(msg);
1173 json_object_set_new(jmsg, "msg_body", tr_msg_encode_tidresp(tidresp));
1177 jmsg_type = json_string("trp_update");
1178 json_object_set_new(jmsg, "msg_type", jmsg_type);
1179 trpupd=tr_msg_get_trp_upd(msg);
1180 json_object_set_new(jmsg, "msg_body", tr_msg_encode_trp_upd(trpupd));
1184 jmsg_type = json_string("trp_request");
1185 json_object_set_new(jmsg, "msg_type", jmsg_type);
1186 trpreq=tr_msg_get_trp_req(msg);
1187 json_object_set_new(jmsg, "msg_body", tr_msg_encode_trp_req(trpreq));
1195 encoded=json_dumps(jmsg, 0);
1196 tr_debug("tr_msg_encode: outgoing msg=%s", encoded);
1201 TR_MSG *tr_msg_decode(const char *jbuf, size_t buflen)
1204 json_t *jmsg = NULL;
1208 const char *mtype = NULL;
1210 if (NULL == (jmsg = json_loadb(jbuf, buflen, JSON_DISABLE_EOF_CHECK, &rc))) {
1211 tr_debug("tr_msg_decode(): error loading object");
1215 if (!(msg = malloc(sizeof(TR_MSG)))) {
1216 tr_debug("tr_msg_decode(): Error allocating TR_MSG structure.");
1221 memset(msg, 0, sizeof(TR_MSG));
1223 if ((NULL == (jtype = json_object_get(jmsg, "msg_type"))) ||
1224 (NULL == (jbody = json_object_get(jmsg, "msg_body")))) {
1225 tr_debug("tr_msg_decode(): Error parsing message header.");
1227 tr_msg_free_decoded(msg);
1231 mtype = json_string_value(jtype);
1233 if (0 == strcmp(mtype, "tid_request")) {
1234 msg->msg_type = TID_REQUEST;
1235 tr_msg_set_req(msg, tr_msg_decode_tidreq(jbody));
1237 else if (0 == strcmp(mtype, "tid_response")) {
1238 msg->msg_type = TID_RESPONSE;
1239 tr_msg_set_resp(msg, tr_msg_decode_tidresp(jbody));
1241 else if (0 == strcmp(mtype, "trp_update")) {
1242 msg->msg_type = TRP_UPDATE;
1243 tr_msg_set_trp_upd(msg, tr_msg_decode_trp_upd(NULL, jbody)); /* null talloc context for now */
1245 else if (0 == strcmp(mtype, "trp_request")) {
1246 msg->msg_type = TRP_UPDATE;
1247 tr_msg_set_trp_req(msg, tr_msg_decode_trp_req(NULL, jbody)); /* null talloc context for now */
1250 msg->msg_type = TR_UNKNOWN;
1251 msg->msg_rep = NULL;
1259 void tr_msg_free_encoded(char *jmsg)
1265 void tr_msg_free_decoded(TR_MSG *msg)
1268 if (msg->msg_rep!=NULL) {
1269 switch (msg->msg_type) {
1271 tid_req_free(tr_msg_get_req(msg));
1274 tid_resp_free(tr_msg_get_resp(msg));
1277 trp_upd_free(tr_msg_get_trp_upd(msg));
1280 trp_req_free(tr_msg_get_trp_req(msg));