4cbadd95a8f465acdef24f8fa13fa2ec0034083d
[trust_router.git] / common / tr_msg.c
1 /*
2  * Copyright (c) 2012-2014 , JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <string.h>
38 #include <openssl/dh.h>
39 #include <openssl/crypto.h>
40 #include <jansson.h>
41 #include <assert.h>
42 #include <talloc.h>
43
44
45 #include <tr_apc.h>
46 #include <tr_comm.h>
47 #include <mon_internal.h>
48 #include <trp_internal.h>
49 #include <tr_msg.h>
50 #include <tr_name_internal.h>
51 #include <trust_router/tr_constraint.h>
52 #include <trust_router/tr_dh.h>
53 #include <tr_debug.h>
54
55 /* JSON helpers */
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)
58 {
59   json_t *obj;
60
61   obj=json_object_get(jmsg, attr);
62   if (obj == NULL) {
63     return -1;
64   }
65   /* check type */
66   if (!json_is_integer(obj)) {
67     return -1;
68   }
69
70   (*dest)=json_integer_value(obj);
71   return 0;
72 }
73
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)
77 {
78   json_t *obj;
79
80   obj=json_object_get(jmsg, attr);
81   if (obj == NULL)
82     return TRP_ERROR;
83
84   /* check type */
85   if (!json_is_string(obj))
86     return TRP_ERROR;
87
88   *dest=talloc_strdup(mem_ctx, json_string_value(obj));
89   if (*dest==NULL)
90     return TRP_ERROR;
91
92   return TRP_SUCCESS;
93 }
94
95 enum msg_type tr_msg_get_msg_type(TR_MSG *msg) 
96 {
97   return msg->msg_type;
98 }
99
100 void tr_msg_set_msg_type(TR_MSG *msg, enum msg_type type)
101 {
102   msg->msg_type = type;
103 }
104
105 TID_REQ *tr_msg_get_req(TR_MSG *msg)
106 {
107   if (msg->msg_type == TID_REQUEST)
108     return (TID_REQ *)msg->msg_rep;
109   return NULL;
110 }
111
112 void tr_msg_set_req(TR_MSG *msg, TID_REQ *req)
113 {
114   msg->msg_rep = req;
115   msg->msg_type = TID_REQUEST;
116 }
117
118 TID_RESP *tr_msg_get_resp(TR_MSG *msg)
119 {
120   if (msg->msg_type == TID_RESPONSE)
121     return (TID_RESP *)msg->msg_rep;
122   return NULL;
123 }
124
125 void tr_msg_set_resp(TR_MSG *msg, TID_RESP *resp)
126 {
127   msg->msg_rep = resp;
128   msg->msg_type = TID_RESPONSE;
129 }
130
131 MON_REQ *tr_msg_get_mon_req(TR_MSG *msg)
132 {
133   if (msg->msg_type == MON_REQUEST)
134     return (MON_REQ *)msg->msg_rep;
135   return NULL;
136 }
137
138 void tr_msg_set_mon_req(TR_MSG *msg, MON_REQ *req)
139 {
140   msg->msg_rep = req;
141   msg->msg_type = MON_REQUEST;
142 }
143
144 MON_RESP *tr_msg_get_mon_resp(TR_MSG *msg)
145 {
146   if (msg->msg_type == MON_RESPONSE)
147     return (MON_RESP *)msg->msg_rep;
148   return NULL;
149 }
150
151 void tr_msg_set_mon_resp(TR_MSG *msg, MON_RESP *resp)
152 {
153   msg->msg_rep = resp;
154   msg->msg_type = MON_RESPONSE;
155 }
156
157 TRP_UPD *tr_msg_get_trp_upd(TR_MSG *msg)
158 {
159   if (msg->msg_type == TRP_UPDATE)
160     return (TRP_UPD *)msg->msg_rep;
161   return NULL;
162 }
163
164 void tr_msg_set_trp_upd(TR_MSG *msg, TRP_UPD *update)
165 {
166   msg->msg_rep=update;
167   talloc_steal(NULL, update); /* should attach to msg, but TR_MSG not usually talloc'ed */
168   msg->msg_type=TRP_UPDATE;
169 }
170
171 TRP_REQ *tr_msg_get_trp_req(TR_MSG *msg)
172 {
173   if (msg->msg_type == TRP_REQUEST)
174     return (TRP_REQ *)msg->msg_rep;
175   return NULL;
176 }
177
178 void tr_msg_set_trp_req(TR_MSG *msg, TRP_REQ *req)
179 {
180   msg->msg_rep=req;
181   msg->msg_type=TRP_REQUEST;
182 }
183
184 static json_t *tr_msg_encode_dh(DH *dh)
185 {
186   json_t *jdh = NULL;
187   json_t *jbn = NULL;
188   char *s=NULL;
189
190   if ((!dh) || (!dh->p) || (!dh->g) || (!dh->pub_key))
191     return NULL;
192
193   jdh = json_object();
194
195   jbn = json_string(s=BN_bn2hex(dh->p));
196   OPENSSL_free(s);
197   json_object_set_new(jdh, "dh_p", jbn);
198
199   jbn = json_string(s=BN_bn2hex(dh->g));
200   OPENSSL_free(s);
201   json_object_set_new(jdh, "dh_g", jbn);
202
203   jbn = json_string(s=BN_bn2hex(dh->pub_key));
204   OPENSSL_free(s);
205   json_object_set_new(jdh, "dh_pub_key", jbn);
206
207   return jdh;
208 }
209
210 static DH *tr_msg_decode_dh(json_t *jdh)
211 {
212   DH *dh = NULL;
213   json_t *jp = NULL;
214   json_t *jg = NULL;
215   json_t *jpub_key = NULL;
216
217   if (!(dh=tr_dh_new())) {
218     tr_crit("tr_msg_decode_dh(): Error allocating DH structure.");
219     return NULL;
220   }
221  
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.");
227     tr_dh_destroy(dh);
228     return NULL;
229   }
230
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));
234
235   return dh;
236 }
237
238 static json_t * tr_msg_encode_tidreq(TID_REQ *req)
239 {
240   json_t *jreq = NULL;
241   json_t *jstr = NULL;
242
243   if ((!req) || (!req->rp_realm) || (!req->realm) || !(req->comm))
244     return NULL;
245
246   assert(jreq = json_object());
247
248   jstr = tr_name_to_json_string(req->rp_realm);
249   json_object_set_new(jreq, "rp_realm", jstr);
250
251   jstr = tr_name_to_json_string(req->realm);
252   json_object_set_new(jreq, "target_realm", jstr);
253
254   jstr = tr_name_to_json_string(req->comm);
255   json_object_set_new(jreq, "community", jstr);
256   
257   if (req->orig_coi) {
258     jstr = tr_name_to_json_string(req->orig_coi);
259     json_object_set_new(jreq, "orig_coi", jstr);
260   }
261
262   json_object_set_new(jreq, "dh_info", tr_msg_encode_dh(req->tidc_dh));
263
264   if (req->cons)
265     json_object_set(jreq, "constraints", (json_t *) req->cons);
266
267   if (req->path)
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));
272   
273   return jreq;
274 }
275
276 static TID_REQ *tr_msg_decode_tidreq(TALLOC_CTX *mem_ctx, json_t *jreq)
277 {
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;
283   json_t *jdh = NULL;
284   json_t *jpath = NULL;
285   json_t *jexpire_interval = NULL;
286
287   if (!(treq =tid_req_new())) {
288     tr_crit("tr_msg_decode_tidreq(): Error allocating TID_REQ structure.");
289     return NULL;
290   }
291   talloc_steal(mem_ctx, treq);
292
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.");
298     tid_req_free(treq);
299     return NULL;
300   }
301
302   jpath = json_object_get(jreq, "path");
303   jexpire_interval = json_object_get(jreq, "expiration_interval");
304
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));
308
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.");
312     tid_req_free(treq);
313     return NULL;
314   }
315   treq->tidc_dh = tr_msg_decode_dh(jdh);
316
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));
320   }
321
322   treq->cons = (TR_CONSTRAINT_SET *) json_object_get(jreq, "constraints");
323   if (treq->cons) {
324     if (!tr_constraint_set_validate(treq->cons)) {
325       tr_debug("Constraint set validation failed");
326     tid_req_free(treq);
327     return NULL;
328     }
329     json_incref((json_t *) treq->cons);
330     tid_req_cleanup_json(treq, (json_t *) treq->cons);
331   }
332   if (jpath) {
333     json_incref(jpath);
334     treq->path = jpath;
335     tid_req_cleanup_json(treq, jpath);
336   }
337   if (jexpire_interval)
338     treq->expiration_interval = json_integer_value(jexpire_interval);
339   
340   return treq;
341 }
342
343 static json_t *tr_msg_encode_one_server(TID_SRVR_BLK *srvr)
344 {
345   json_t *jsrvr = NULL;
346   json_t *jstr = NULL;
347   gchar *time_str = g_time_val_to_iso8601(&srvr->key_expiration);
348
349   tr_debug("Encoding one server.");
350
351   jsrvr = json_object();
352
353   jstr = json_string(srvr->aaa_server_addr);
354   json_object_set_new(jsrvr, "server_addr", jstr);
355
356   json_object_set_new(jsrvr,
357                       "key_expiration", json_string(time_str));
358   g_free(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));
363   if (srvr->path)
364     /* The path is owned by the srvr, so grab an extra ref*/
365     json_object_set(jsrvr, "path", (json_t *)(srvr->path));
366   return jsrvr;
367 }
368
369 static int tr_msg_decode_one_server(json_t *jsrvr, TID_SRVR_BLK *srvr) 
370 {
371   json_t *jsrvr_addr = NULL;
372   json_t *jsrvr_kn = NULL;
373   json_t *jsrvr_dh = NULL;
374   json_t *jsrvr_expire = NULL;
375
376   if (jsrvr == NULL)
377     return -1;
378
379
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.");
384     return -1;
385   }
386
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));
396   }
397   
398   return 0;
399 }
400
401 static json_t *tr_msg_encode_servers(TID_RESP *resp)
402 {
403   json_t *jservers = NULL;
404   json_t *jsrvr = NULL;
405   TID_SRVR_BLK *srvr = NULL;
406   size_t index;
407
408   jservers = json_array();
409
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))) {
413       return NULL;
414     }
415   }
416
417   //  tr_debug("tr_msg_encode_servers(): servers contains:");
418   //  tr_debug("%s", json_dumps(jservers, 0));
419   return jservers;
420 }
421
422 static TID_SRVR_BLK *tr_msg_decode_servers(TALLOC_CTX *mem_ctx, json_t *jservers)
423 {
424   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
425   TID_SRVR_BLK *servers=NULL;
426   TID_SRVR_BLK *new_srvr=NULL;
427   json_t *jsrvr;
428   size_t i, num_servers;
429
430   num_servers = json_array_size(jservers);
431   tr_debug("tr_msg_decode_servers(): Number of servers = %u.", (unsigned) num_servers);
432   
433   if (0 == num_servers) {
434     tr_debug("tr_msg_decode_servers(): Server array is empty."); 
435     goto cleanup;
436   }
437
438   for (i = 0; i < num_servers; i++) {
439     jsrvr = json_array_get(jservers, i);
440
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 */
444       goto cleanup;
445     }
446     
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 */
449       goto cleanup;
450     }
451
452     tid_srvr_blk_add(servers, new_srvr);
453   }
454
455   talloc_steal(mem_ctx, servers);
456
457 cleanup:
458   talloc_free(tmp_ctx);
459   return servers;
460 }
461
462 static json_t * tr_msg_encode_tidresp(TID_RESP *resp)
463 {
464   json_t *jresp = NULL;
465   json_t *jstr = NULL;
466   json_t *jservers = NULL;
467
468   if ((!resp) || (!resp->rp_realm) || (!resp->realm) || !(resp->comm))
469     return NULL;
470
471   jresp = json_object();
472
473   if (TID_ERROR == resp->result) {
474     jstr = json_string("error");
475     json_object_set_new(jresp, "result", jstr);
476     if (resp->err_msg) {
477       jstr = tr_name_to_json_string(resp->err_msg);
478       json_object_set_new(jresp, "err_msg", jstr);
479     }
480   }
481   else {
482     jstr = json_string("success");
483     json_object_set_new(jresp, "result", jstr);
484   }
485
486   jstr = tr_name_to_json_string(resp->rp_realm);
487   json_object_set_new(jresp, "rp_realm", jstr);
488
489   jstr = tr_name_to_json_string(resp->realm);
490   json_object_set_new(jresp, "target_realm", jstr);
491
492   jstr = tr_name_to_json_string(resp->comm);
493   json_object_set_new(jresp, "comm", jstr);
494
495   if (resp->orig_coi) {
496     jstr = tr_name_to_json_string(resp->orig_coi);
497     json_object_set_new(jresp, "orig_coi", jstr);
498   }
499
500   if (NULL == resp->servers) {
501     tr_debug("tr_msg_encode_tidresp(): No servers to encode.");
502   }
503   else {
504     jservers = tr_msg_encode_servers(resp);
505     json_object_set_new(jresp, "servers", jservers);
506   }
507   if (resp->error_path)
508     json_object_set(jresp, "error_path", resp->error_path);
509   
510   
511   return jresp;
512 }
513
514 static TID_RESP *tr_msg_decode_tidresp(TALLOC_CTX *mem_ctx, json_t *jresp)
515 {
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;
524
525   if (!(tresp=tid_resp_new(mem_ctx))) {
526     tr_crit("tr_msg_decode_tidresp(): Error allocating TID_RESP structure.");
527     return NULL;
528   }
529
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.");
540     talloc_free(tresp);
541     return NULL;
542   }
543
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); 
549     } 
550     else {
551       talloc_free(tresp);
552       return NULL;
553     }
554     tresp->result = TID_SUCCESS;
555   }
556   else {
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));
562     } else
563       tresp->err_msg = tr_new_name("No error message set.");
564
565     if (NULL !=(tresp->error_path = json_object_get(jresp, "error_path")))
566       json_incref(tresp->error_path);
567   }
568
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));
572
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));
577   }
578      
579   return tresp;
580 }
581
582
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)
586 {
587   json_t *jstr=NULL;
588   json_t *jint=NULL;
589   char *s=NULL;
590
591   if (rec==NULL)
592     return TRP_BADTYPE;
593
594   if (trp_inforec_get_trust_router(rec)==NULL)
595     return TRP_ERROR;
596
597   s=tr_name_strdup(trp_inforec_get_trust_router(rec));
598   if (s==NULL)
599     return TRP_NOMEM;
600   jstr=json_string(s);
601   free(s);s=NULL;
602   if(jstr==NULL)
603     return TRP_ERROR;
604   json_object_set_new(jrec, "trust_router", jstr);
605
606   jint=json_integer(trp_inforec_get_metric(rec));
607   if(jint==NULL)
608     return TRP_ERROR;
609   json_object_set_new(jrec, "metric", jint);
610
611   jint=json_integer(trp_inforec_get_interval(rec));
612   if(jint==NULL)
613     return TRP_ERROR;
614   json_object_set_new(jrec, "interval", jint);
615
616   return TRP_SUCCESS;
617 }
618
619 /* returns a json array */
620 static json_t *tr_msg_encode_apcs(TR_APC *apcs)
621 {
622   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
623   TR_APC_ITER *iter=tr_apc_iter_new(tmp_ctx);
624   TR_APC *apc=NULL;
625   json_t *jarray=NULL;
626   json_t *jid=NULL;
627
628   if (iter==NULL)
629     goto cleanup;
630
631   jarray=json_array();
632   if (jarray==NULL)
633     goto cleanup;
634
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)) {
638       json_decref(jarray);
639       jarray=NULL;
640       goto cleanup;
641     }
642   }
643   
644 cleanup:
645   talloc_free(tmp_ctx);
646   return jarray;
647 }
648
649 static TR_APC *tr_msg_decode_apcs(TALLOC_CTX *mem_ctx, json_t *jarray, TRP_RC *rc)
650 {
651   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
652   size_t ii=0;
653   TR_APC *apc_list=NULL;
654   TR_APC *new=NULL;
655   json_t *jstr=NULL;
656
657   *rc=TRP_ERROR;
658
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 */
664       goto cleanup;
665     }
666
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 */
670       goto cleanup;
671     }
672
673     tr_apc_add(apc_list, new);
674   }
675
676   *rc=TRP_SUCCESS;
677
678   if (apc_list!=NULL)
679     talloc_steal(mem_ctx, apc_list);
680
681 cleanup:
682   talloc_free(tmp_ctx);
683   return apc_list;
684 }
685
686 static TRP_RC tr_msg_encode_inforec_comm(json_t *jrec, TRP_INFOREC *rec)
687 {
688   json_t *jstr=NULL;
689   json_t *jint=NULL;
690   json_t *japcs=NULL;
691   const char *sconst=NULL;
692   TR_COMM_TYPE commtype=TR_COMM_UNKNOWN;
693
694   if (rec==NULL)
695     return TRP_BADTYPE;
696
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.");
700     return TRP_ERROR;
701   }
702   sconst=tr_comm_type_to_str(commtype);
703   if (sconst==NULL)
704     return TRP_ERROR;
705   jstr=json_string(sconst);
706   if(jstr==NULL)
707     return TRP_ERROR;
708   json_object_set_new(jrec, "type", jstr);
709
710   sconst=tr_realm_role_to_str(trp_inforec_get_role(rec));
711   if (sconst==NULL) {
712     tr_notice("tr_msg_encode_inforec_comm: unknown realm role.");
713     return TRP_ERROR;
714   }
715   jstr=json_string(sconst);
716   if(jstr==NULL)
717     return TRP_ERROR;
718   json_object_set_new(jrec, "role", jstr);
719
720   japcs=tr_msg_encode_apcs(trp_inforec_get_apcs(rec));
721   if (japcs==NULL) {
722     tr_notice("tr_msg_encode_inforec_comm: error encoding APCs.");
723     return TRP_ERROR;
724   }
725   json_object_set_new(jrec, "apcs", japcs);
726   
727
728   if (trp_inforec_get_owner_realm(rec)!=NULL) {
729     jstr=tr_name_to_json_string(trp_inforec_get_owner_realm(rec));
730     if(jstr==NULL)
731       return TRP_ERROR;
732     json_object_set_new(jrec, "owner_realm", jstr);
733   }  
734
735   if (trp_inforec_get_owner_contact(rec)!=NULL) {
736     jstr=tr_name_to_json_string(trp_inforec_get_owner_contact(rec));
737     if(jstr==NULL)
738       return TRP_ERROR;
739     json_object_set_new(jrec, "owner_contact", jstr);
740   }  
741
742   json_object_set(jrec, "provenance", trp_inforec_get_provenance(rec));
743
744   jint=json_integer(trp_inforec_get_interval(rec));
745   if(jint==NULL)
746     return TRP_ERROR;
747   json_object_set_new(jrec, "interval", jint);
748
749   return TRP_SUCCESS;
750 }
751
752 static json_t *tr_msg_encode_inforec(TRP_INFOREC *rec)
753 {
754   json_t *jrec=NULL;
755   json_t *jstr=NULL;
756
757   if ((rec==NULL) || (trp_inforec_get_type(rec)==TRP_INFOREC_TYPE_UNKNOWN))
758     return NULL;
759
760   jrec=json_object();
761   if (jrec==NULL)
762     return NULL;
763
764   jstr=json_string(trp_inforec_type_to_string(trp_inforec_get_type(rec)));
765   if (jstr==NULL) {
766     json_decref(jrec);
767     return NULL;
768   }
769   json_object_set_new(jrec, "record_type", jstr);
770
771   switch (rec->type) {
772   case TRP_INFOREC_TYPE_ROUTE:
773     if (TRP_SUCCESS!=tr_msg_encode_inforec_route(jrec, rec)) {
774       json_decref(jrec);
775       return NULL;
776     }
777     break;
778   case TRP_INFOREC_TYPE_COMMUNITY:
779     if (TRP_SUCCESS!=tr_msg_encode_inforec_comm(jrec, rec)) {
780       json_decref(jrec);
781       return NULL;
782     }
783     break;
784   default:
785     json_decref(jrec);
786     return NULL;
787   }
788   return jrec;
789 }
790
791 static TRP_RC tr_msg_decode_trp_inforec_route(json_t *jrecord, TRP_INFOREC *rec)
792 {
793   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
794   TRP_RC rc=TRP_ERROR;
795   char *s=NULL;
796   int num=0;
797
798   rc=tr_msg_get_json_string(jrecord, "trust_router", &s, tmp_ctx);
799   if (rc != TRP_SUCCESS)
800     goto cleanup;
801   if (TRP_SUCCESS!=trp_inforec_set_trust_router(rec, tr_new_name(s))) {
802     rc=TRP_ERROR;
803     goto cleanup;
804   }
805   talloc_free(s); s=NULL;
806
807   trp_inforec_set_next_hop(rec, NULL); /* make sure this is null (filled in later) */
808
809   rc=tr_msg_get_json_integer(jrecord, "metric", &num);
810   if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_metric(rec,num)))
811     goto cleanup;
812
813   rc=tr_msg_get_json_integer(jrecord, "interval", &num);
814   if ((rc != TRP_SUCCESS) || (TRP_SUCCESS!=trp_inforec_set_interval(rec,num)))
815     goto cleanup;
816
817 cleanup:
818   talloc_free(tmp_ctx);
819   return rc;
820 }
821
822 static TRP_RC tr_msg_decode_trp_inforec_comm(json_t *jrecord, TRP_INFOREC *rec)
823 {
824   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
825   TRP_RC rc=TRP_ERROR;
826   char *s=NULL;
827   int num=0;
828   TR_APC *apcs=NULL;
829
830   rc=tr_msg_get_json_string(jrecord, "type", &s, tmp_ctx);
831   if (rc != TRP_SUCCESS)
832     goto cleanup;
833   if (TRP_SUCCESS!=trp_inforec_set_comm_type(rec, tr_comm_type_from_str(s))) {
834     rc=TRP_ERROR;
835     goto cleanup;
836   }
837   talloc_free(s); s=NULL;
838
839   rc=tr_msg_get_json_string(jrecord, "role", &s, tmp_ctx);
840   if (rc != TRP_SUCCESS)
841     goto cleanup;
842   if (TRP_SUCCESS!=trp_inforec_set_role(rec, tr_realm_role_from_str(s))) {
843     rc=TRP_ERROR;
844     goto cleanup;
845   }
846   talloc_free(s); s=NULL;
847
848   apcs=tr_msg_decode_apcs(rec, json_object_get(jrecord, "apcs"), &rc);
849   if (rc!=TRP_SUCCESS) {
850     rc=TRP_ERROR;
851     goto cleanup;
852   }
853   trp_inforec_set_apcs(rec, apcs);
854
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)))
858     goto cleanup;
859
860   trp_inforec_set_provenance(rec, json_object_get(jrecord, "provenance"));
861
862   /* optional */
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))) {
866       rc=TRP_ERROR;
867       goto cleanup;
868     }
869     if (s!=NULL) {
870       talloc_free(s);
871       s=NULL;
872     }
873   }
874
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))) {
878       rc=TRP_ERROR;
879       goto cleanup;
880     }
881     if (s!=NULL) {
882       talloc_free(s);
883       s=NULL;
884     }
885   }
886
887 cleanup:
888   talloc_free(tmp_ctx);
889   return rc;
890 }
891
892 /* decode a single record */
893 static TRP_INFOREC *tr_msg_decode_trp_inforec(TALLOC_CTX *mem_ctx, json_t *jrecord)
894 {
895   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
896   TRP_INFOREC_TYPE rectype;
897   TRP_INFOREC *rec=NULL;
898   TRP_RC rc=TRP_ERROR;
899   char *s=NULL;
900   
901   if (TRP_SUCCESS!=tr_msg_get_json_string(jrecord, "record_type", &s, tmp_ctx))
902     goto cleanup;
903
904   rectype=trp_inforec_type_from_string(s);
905   talloc_free(s); s=NULL;
906
907   rec=trp_inforec_new(tmp_ctx, rectype);
908   if (rec==NULL) {
909     rc=TRP_NOMEM;
910     goto cleanup;
911   }
912
913   tr_debug("tr_msg_decode_trp_inforec: '%s' record found.", trp_inforec_type_to_string(rec->type));
914
915   switch(trp_inforec_get_type(rec)) {
916   case TRP_INFOREC_TYPE_ROUTE:
917     rc=tr_msg_decode_trp_inforec_route(jrecord, rec);
918     break;
919   case TRP_INFOREC_TYPE_COMMUNITY:
920     rc=tr_msg_decode_trp_inforec_comm(jrecord, rec);
921     break;
922   default:
923     rc=TRP_UNSUPPORTED;
924     goto cleanup;
925   }
926
927   talloc_steal(mem_ctx, rec);
928   rc=TRP_SUCCESS;
929
930 cleanup:
931   if (rc != TRP_SUCCESS) {
932     trp_inforec_free(rec);
933     rec=NULL;
934   }
935   talloc_free(tmp_ctx);
936   return rec;
937 }
938
939 /* TRP update msg */
940 static json_t *tr_msg_encode_trp_upd(TRP_UPD *update)
941 {
942   json_t *jupdate=NULL;
943   json_t *jrecords=NULL;
944   json_t *jrec=NULL;
945   json_t *jstr=NULL;
946   TRP_INFOREC *rec;
947   char *s=NULL;
948
949   if (update==NULL)
950     return NULL;
951
952   jupdate=json_object();
953   if (jupdate==NULL)
954     return NULL;
955
956   s=tr_name_strdup(trp_upd_get_comm(update));
957   if (s==NULL) {
958     json_decref(jupdate);
959     return NULL;
960   }
961   jstr=json_string(s);
962   free(s);s=NULL;
963   if(jstr==NULL) {
964     json_decref(jupdate);
965     return NULL;
966   }
967   json_object_set_new(jupdate, "community", jstr);
968
969   s=tr_name_strdup(trp_upd_get_realm(update));
970   if (s==NULL) {
971     json_decref(jupdate);
972     return NULL;
973   }
974   jstr=json_string(s);
975   free(s);s=NULL;
976   if(jstr==NULL) {
977     json_decref(jupdate);
978     return NULL;
979   }
980   json_object_set_new(jupdate, "realm", jstr);
981
982   jrecords=json_array();
983   if (jrecords==NULL) {
984     json_decref(jupdate);
985     return NULL;
986   }
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);
991     if (jrec==NULL) {
992       json_decref(jupdate); /* also decs jrecords and any elements */
993       return NULL;
994     }
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 */
998       return NULL;
999     }
1000   }
1001
1002   return jupdate;
1003 }
1004
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. 
1007  */
1008 static TRP_UPD *tr_msg_decode_trp_upd(TALLOC_CTX *mem_ctx, json_t *jupdate)
1009 {
1010   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1011   json_t *jrecords=NULL;
1012   size_t ii=0;
1013   TRP_UPD *update=NULL;
1014   TRP_INFOREC *new_rec=NULL;
1015   TRP_INFOREC *list_tail=NULL;
1016   char *s=NULL;
1017   TR_NAME *name;
1018   TRP_RC rc=TRP_ERROR;
1019
1020   update=trp_upd_new(tmp_ctx);
1021   if (update==NULL) {
1022     rc=TRP_NOMEM;
1023     goto cleanup;
1024   }
1025
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.");
1029     rc=TRP_NOPARSE;
1030     goto cleanup;
1031   }
1032   name=tr_new_name(s);
1033   if (name==NULL) {
1034     tr_debug("tr_msg_decode_trp_upd: could not allocate community name.");
1035     rc=TRP_NOMEM;
1036     goto cleanup;
1037   }
1038   talloc_free(s); s=NULL;
1039   trp_upd_set_comm(update, name);
1040
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.");
1044     rc=TRP_NOPARSE;
1045     goto cleanup;
1046   }
1047   name=tr_new_name(s);
1048   if (name==NULL) {
1049     tr_debug("tr_msg_decode_trp_upd: could not allocate realm name.");
1050     rc=TRP_NOMEM;
1051     goto cleanup;
1052   }
1053   talloc_free(s); s=NULL;
1054   trp_upd_set_realm(update, name);
1055
1056   jrecords=json_object_get(jupdate, "records");
1057   if ((jrecords==NULL) || (!json_is_array(jrecords))) {
1058     rc=TRP_NOPARSE;
1059     goto cleanup;
1060   }
1061
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) {
1067       rc=TRP_NOPARSE;
1068       goto cleanup;
1069     }
1070
1071     if (list_tail==NULL)
1072       trp_upd_set_inforec(update, new_rec); /* first is a special case */
1073     else
1074       trp_inforec_set_next(list_tail, new_rec);
1075
1076     list_tail=new_rec;
1077   }
1078
1079   /* Succeeded. Move new allocations into the correct talloc context */
1080   talloc_steal(mem_ctx, update);
1081   rc=TRP_SUCCESS;
1082
1083 cleanup:
1084   talloc_free(tmp_ctx);
1085   if (rc!=TRP_SUCCESS)
1086     return NULL;
1087   return update;
1088 }
1089
1090 static json_t *tr_msg_encode_trp_req(TRP_REQ *req)
1091 {
1092   json_t *jbody=NULL;
1093   json_t *jstr=NULL;
1094   char *s=NULL;
1095
1096   if (req==NULL)
1097     return NULL;
1098
1099   jbody=json_object();
1100   if (jbody==NULL)
1101     return NULL;
1102
1103   if ((NULL==trp_req_get_comm(req))
1104      || (NULL==trp_req_get_realm(req))) {
1105     json_decref(jbody);
1106     return NULL;
1107   }
1108
1109   s=tr_name_strdup(trp_req_get_comm(req)); /* ensures null termination */
1110   if (s==NULL) {
1111     json_decref(jbody);
1112     return NULL;
1113   }
1114   jstr=json_string(s);
1115   free(s); s=NULL;
1116   if (jstr==NULL) {
1117     json_decref(jbody);
1118     return NULL;
1119   }
1120   json_object_set_new(jbody, "community", jstr);
1121     
1122   s=tr_name_strdup(trp_req_get_realm(req)); /* ensures null termination */
1123   if (s==NULL) {
1124     json_decref(jbody);
1125     return NULL;
1126   }
1127   jstr=json_string(s);
1128   free(s); s=NULL;
1129   if (jstr==NULL) {
1130     json_decref(jbody);
1131     return NULL;
1132   }
1133   json_object_set_new(jbody, "realm", jstr);
1134
1135   return jbody;
1136 }
1137
1138 static TRP_REQ *tr_msg_decode_trp_req(TALLOC_CTX *mem_ctx, json_t *jreq)
1139 {
1140   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
1141   TRP_REQ *req=NULL;
1142   char *s=NULL;
1143   TRP_RC rc=TRP_ERROR;
1144
1145   /* check message type and body type for agreement */
1146   req=trp_req_new(tmp_ctx);
1147   if (req==NULL) {
1148     rc=TRP_NOMEM;
1149     goto cleanup;
1150   }
1151
1152   rc=tr_msg_get_json_string(jreq, "community", &s, tmp_ctx);
1153   if (rc!=TRP_SUCCESS)
1154     goto cleanup;
1155   trp_req_set_comm(req, tr_new_name(s));
1156   talloc_free(s); s=NULL;
1157
1158   rc=tr_msg_get_json_string(jreq, "realm", &s, tmp_ctx);
1159   if (rc!=TRP_SUCCESS)
1160     goto cleanup;
1161   trp_req_set_realm(req, tr_new_name(s));
1162   talloc_free(s); s=NULL;
1163
1164   rc=TRP_SUCCESS;
1165   talloc_steal(mem_ctx, req);
1166
1167 cleanup:
1168   talloc_free(tmp_ctx);
1169   if (rc!=TRP_SUCCESS)
1170     return NULL;
1171   return req;
1172 }
1173
1174 char *tr_msg_encode(TALLOC_CTX *mem_ctx, TR_MSG *msg)
1175 {
1176   json_t *jmsg=NULL;
1177   json_t *jmsg_type=NULL;
1178   char *encoded_tmp=NULL;
1179   char *encoded=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;
1186
1187   /* TBD -- add error handling */
1188   jmsg = json_object();
1189
1190   switch (msg->msg_type) {
1191     case TID_REQUEST:
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));
1196       break;
1197
1198     case TID_RESPONSE:
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));
1203       break;
1204
1205     case TRP_UPDATE:
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));
1210       break;
1211
1212     case TRP_REQUEST:
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));
1217       break;
1218
1219     case MON_REQUEST:
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));
1224       break;
1225
1226     case MON_RESPONSE:
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));
1231       break;
1232
1233     default:
1234       json_decref(jmsg);
1235       return NULL;
1236   }
1237
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
1244
1245   tr_debug("tr_msg_encode: outgoing msg=%s", encoded);
1246   return encoded;
1247 }
1248
1249 TR_MSG *tr_msg_decode(TALLOC_CTX *mem_ctx, const char *jbuf, size_t buflen)
1250 {
1251   TR_MSG *msg=NULL;
1252   json_t *jmsg = NULL;
1253   json_error_t rc;
1254   json_t *jtype=NULL;
1255   json_t *jbody=NULL;
1256   const char *mtype = NULL;
1257
1258   if (NULL == (jmsg = json_loadb(jbuf, buflen, JSON_DISABLE_EOF_CHECK, &rc))) {
1259     tr_debug("tr_msg_decode(): error loading object");
1260     return NULL;
1261   }
1262
1263   if (!(msg = talloc_zero(mem_ctx, TR_MSG))) {
1264     tr_debug("tr_msg_decode(): Error allocating TR_MSG structure.");
1265     json_decref(jmsg);
1266     return NULL;
1267   }
1268  
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.");
1272     json_decref(jmsg);
1273     tr_msg_free_decoded(msg);
1274     return NULL;
1275   }
1276
1277   mtype = json_string_value(jtype);
1278
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));
1282   }
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));
1286   }
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 */
1290   }
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 */
1294   }
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));
1298   }
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));
1303 //  }
1304   else {
1305     msg->msg_type = TR_UNKNOWN;
1306     msg->msg_rep = NULL;
1307   }
1308
1309   json_decref(jmsg);
1310
1311   return msg;
1312 }
1313
1314 void tr_msg_free_encoded(char *jmsg)
1315 {
1316   if (jmsg)
1317     talloc_free(jmsg);
1318 }
1319
1320 void tr_msg_free_decoded(TR_MSG *msg)
1321 {
1322   if (msg)
1323     talloc_free(msg);
1324 }