2 * Copyright (c) 2016-2018, 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.
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>
47 static int sort_tr_names_cmp(const void *a, const void *b)
49 TR_NAME **n1=(TR_NAME **)a;
50 TR_NAME **n2=(TR_NAME **)b;
51 return tr_name_cmp(*n1, *n2);
54 static void sort_tr_names(TR_NAME **names, size_t n_names)
56 qsort(names, n_names, sizeof(TR_NAME *), sort_tr_names_cmp);
59 char *trp_rtable_to_str(TALLOC_CTX *mem_ctx, TRP_RTABLE *rtbl, const char *sep, const char *lineterm)
61 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
64 TR_NAME **realms=NULL;
66 TRP_ROUTE **entries=NULL;
68 char **tbl_strings=NULL;
69 size_t ii_tbl=0; /* counts tbl_strings */
72 size_t ii=0, jj=0, kk=0;
79 tbl_size=trp_rtable_size(rtbl);
81 result=talloc_strdup(mem_ctx, lineterm);
85 tbl_strings=talloc_array(tmp_ctx, char *, tbl_size);
86 if (tbl_strings==NULL) {
87 result=talloc_strdup(mem_ctx, "error");
91 comms=trp_rtable_get_comms(rtbl, &n_comms);
92 talloc_steal(tmp_ctx, comms);
93 sort_tr_names(comms, n_comms);
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]);
108 talloc_free(entries);
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);
121 talloc_steal(mem_ctx, result);
124 talloc_free(tmp_ctx);
129 json_t *trp_rtable_to_json(TRP_RTABLE *rtbl)
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;
136 json_t *retval = NULL;
138 /* Get the JSON array to return */
139 rtable_json = json_array();
140 if (rtable_json == NULL)
143 /* Get the array of routes */
144 routes = trp_rtable_get_entries(tmp_ctx, rtbl, &n_routes);
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)
153 json_array_append_new(rtable_json, route_json);
156 /* Success - set the return value and increment the reference count */
157 retval = rtable_json;
162 json_decref(rtable_json);
163 talloc_free(tmp_ctx);