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.
41 #include <tr_name_internal.h>
42 #include <trp_route.h>
43 #include <trp_internal.h>
44 #include <trp_rtable.h>
45 #include <trust_router/trp.h>
48 /* Pretty print a route table entry to a newly allocated string. If sep is NULL,
49 * returns comma+space separated string. */
50 char *trp_route_to_str(TALLOC_CTX *mem_ctx, TRP_ROUTE *entry, const char *sep)
52 char *comm=tr_name_strdup(entry->comm);
53 char *realm=tr_name_strdup(entry->realm);
54 char *peer=tr_name_strdup(entry->peer);
55 char *trust_router=tr_name_strdup(entry->trust_router);
56 char *next_hop=tr_name_strdup(entry->next_hop);
57 char *expiry=timespec_to_str(entry->expiry);
63 result=talloc_asprintf(mem_ctx,
64 "%s%s%s%s%s%s%u%s%s%s%s%s%u%s%u%s%s%s%u",
84 /* helper for below */
85 #define OBJECT_SET_OR_FAIL(jobj, key, val) \
88 json_object_set_new((jobj),(key),(val)); \
93 json_t *trp_route_to_json(TRP_ROUTE *route)
95 json_t *route_json = NULL;
97 struct timespec ts_zero = {0, 0};
99 route_json = json_object();
100 if (route_json == NULL)
103 if (tr_cmp_timespec(trp_route_get_expiry(route), &ts_zero) == 0) {
104 expires = strdup("");
106 expires = timespec_to_str(route->expiry);
107 if (expires == NULL) {
108 json_decref(route_json);
113 OBJECT_SET_OR_FAIL(route_json, "community", tr_name_to_json_string(route->comm));
114 OBJECT_SET_OR_FAIL(route_json, "realm", tr_name_to_json_string(route->realm));
115 OBJECT_SET_OR_FAIL(route_json, "peer", tr_name_to_json_string(route->peer));
116 OBJECT_SET_OR_FAIL(route_json, "metric", json_integer(route->metric));
117 OBJECT_SET_OR_FAIL(route_json, "trust_router", tr_name_to_json_string(route->trust_router));
118 OBJECT_SET_OR_FAIL(route_json, "next_hop", tr_name_to_json_string(route->next_hop));
119 OBJECT_SET_OR_FAIL(route_json, "selected", json_boolean(route->selected));
120 OBJECT_SET_OR_FAIL(route_json, "local", json_boolean(route->local));
121 OBJECT_SET_OR_FAIL(route_json, "expires", json_string(expires));