Use json_is_true() in place of json_boolean_value() for compatibility
[trust_router.git] / trp / trp_rtable_encoders.c
1 /*
2  * Copyright (c) 2016-2018, 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
35 #include <stdlib.h>
36
37 #include <talloc.h>
38 #include <jansson.h>
39
40 #include <tr_name_internal.h>
41 #include <trp_route.h>
42 #include <trp_internal.h>
43 #include <trp_rtable.h>
44 #include <trust_router/trp.h>
45
46
47 static int sort_tr_names_cmp(const void *a, const void *b)
48 {
49   TR_NAME **n1=(TR_NAME **)a;
50   TR_NAME **n2=(TR_NAME **)b;
51   return tr_name_cmp(*n1, *n2);
52 }
53
54 static void sort_tr_names(TR_NAME **names, size_t n_names)
55 {
56   qsort(names, n_names, sizeof(TR_NAME *), sort_tr_names_cmp);
57 }
58
59 char *trp_rtable_to_str(TALLOC_CTX *mem_ctx, TRP_RTABLE *rtbl, const char *sep, const char *lineterm)
60 {
61   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
62   TR_NAME **comms=NULL;
63   size_t n_comms=0;
64   TR_NAME **realms=NULL;
65   size_t n_realms=0;
66   TRP_ROUTE **entries=NULL;
67   size_t n_entries=0;
68   char **tbl_strings=NULL;
69   size_t ii_tbl=0; /* counts tbl_strings */
70   size_t tbl_size=0;
71   size_t len=0;
72   size_t ii=0, jj=0, kk=0;
73   char *p=NULL;
74   char *result=NULL;
75
76   if (lineterm==NULL)
77     lineterm="\n";
78
79   tbl_size=trp_rtable_size(rtbl);
80   if (tbl_size==0) {
81     result=talloc_strdup(mem_ctx, lineterm);
82     goto cleanup;
83   }
84
85   tbl_strings=talloc_array(tmp_ctx, char *, tbl_size);
86   if (tbl_strings==NULL) {
87     result=talloc_strdup(mem_ctx, "error");
88     goto cleanup;
89   }
90
91   comms=trp_rtable_get_comms(rtbl, &n_comms);
92   talloc_steal(tmp_ctx, comms);
93   sort_tr_names(comms, n_comms);
94   ii_tbl=0;
95   len=0;
96   for (ii=0; ii<n_comms; ii++) {
97     realms=trp_rtable_get_comm_realms(rtbl, comms[ii], &n_realms);
98     talloc_steal(tmp_ctx, realms);
99     sort_tr_names(realms, n_realms);
100     for (jj=0; jj<n_realms; jj++) {
101       entries=trp_rtable_get_realm_entries(rtbl, comms[ii], realms[jj], &n_entries);
102       talloc_steal(tmp_ctx, entries);
103       for (kk=0; kk<n_entries; kk++) {
104         tbl_strings[ii_tbl]=trp_route_to_str(tmp_ctx, entries[kk], sep);
105         len+=strlen(tbl_strings[ii_tbl]);
106         ii_tbl++;
107       }
108       talloc_free(entries);
109     }
110     talloc_free(realms);
111   }
112   talloc_free(comms);
113
114   /* now combine all the strings */
115   len += tbl_size*strlen(lineterm); /* space for line terminations*/
116   len += 1; /* nul terminator */
117   result=(char *)talloc_size(tmp_ctx, len);
118   for (p=result,ii=0; ii < tbl_size; ii++) {
119     p+=sprintf(p, "%s%s", tbl_strings[ii], lineterm);
120   }
121   talloc_steal(mem_ctx, result);
122
123 cleanup:
124   talloc_free(tmp_ctx);
125   return result;
126 }
127
128
129 json_t *trp_rtable_to_json(TRP_RTABLE *rtbl)
130 {
131   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
132   json_t *rtable_json = NULL;
133   json_t *route_json = NULL;
134   TRP_ROUTE **routes = NULL;
135   size_t n_routes = 0;
136   json_t *retval = NULL;
137
138   /* Get the JSON array to return */
139   rtable_json = json_array();
140   if (rtable_json == NULL)
141     goto cleanup;
142
143   /* Get the array of routes */
144   routes = trp_rtable_get_entries(tmp_ctx, rtbl, &n_routes);
145   if (routes == NULL)
146     goto cleanup;
147
148   /* Gather JSON for each route */
149   while (n_routes > 0) {
150     route_json = trp_route_to_json(routes[--n_routes]);
151     if (route_json == NULL)
152       goto cleanup;
153     json_array_append_new(rtable_json, route_json);
154   }
155
156   /* Success - set the return value and increment the reference count */
157   retval = rtable_json;
158   json_incref(retval);
159
160 cleanup:
161   if (rtable_json)
162     json_decref(rtable_json);
163   talloc_free(tmp_ctx);
164   return retval;
165 }