7cbb3ac4215945976532bc00e59c8faea15dbe08
[trust_router.git] / trp / trp_route_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 #include <string.h>
37
38 #include <talloc.h>
39 #include <jansson.h>
40
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>
46 #include <tr_util.h>
47 #include <tr_json_util.h>
48
49 /* Pretty print a route table entry to a newly allocated string. If sep is NULL,
50  * returns comma+space separated string. */
51 char *trp_route_to_str(TALLOC_CTX *mem_ctx, TRP_ROUTE *entry, const char *sep)
52 {
53   char *comm=tr_name_strdup(entry->comm);
54   char *realm=tr_name_strdup(entry->realm);
55   char *peer=tr_name_strdup(entry->peer);
56   char *trust_router=tr_name_strdup(entry->trust_router);
57   char *next_hop=tr_name_strdup(entry->next_hop);
58   char *expiry=timespec_to_str(entry->expiry);
59   char *result=NULL;
60
61   if (sep==NULL)
62     sep=", ";
63
64   result=talloc_asprintf(mem_ctx,
65                          "%s%s%s%s%s%s%u%s%s%s%s%s%u%s%u%s%s%s%u",
66                          comm, sep,
67                          realm, sep,
68                          peer, sep,
69                          entry->metric, sep,
70                          trust_router, sep,
71                          next_hop, sep,
72                          entry->selected, sep,
73                          entry->local, sep,
74                          expiry, sep,
75                          entry->triggered);
76   free(comm);
77   free(realm);
78   free(peer);
79   free(trust_router);
80   free(next_hop);
81   free(expiry);
82   return result;
83 }
84
85 /* helper */
86 static json_t *expiry_to_json_string(TRP_ROUTE *route)
87 {
88   struct timespec ts = {0}; /* initialization to zero is important */
89   char *s = NULL;
90   json_t *jstr = NULL;
91
92   if (tr_cmp_timespec(trp_route_get_expiry(route), &ts) > 0) {
93     if (trp_route_get_expiry_realtime(route, &ts) == NULL)
94       s = strdup("error");
95     else
96       s = timespec_to_str(&ts);
97
98     if (s) {
99       jstr = json_string(s);
100       free(s);
101     }
102   }
103
104   return jstr;
105 }
106
107 json_t *trp_route_to_json(TRP_ROUTE *route)
108 {
109   json_t *route_json = NULL;
110   json_t *retval = NULL;
111
112   route_json = json_object();
113   if (route_json == NULL)
114     goto cleanup;
115
116   OBJECT_SET_OR_FAIL(route_json, "community", tr_name_to_json_string(trp_route_get_comm(route)));
117   OBJECT_SET_OR_FAIL(route_json, "realm", tr_name_to_json_string(trp_route_get_realm(route)));
118   if (trp_route_get_peer(route)->len > 0)
119     OBJECT_SET_OR_FAIL(route_json, "peer", tr_name_to_json_string(trp_route_get_peer(route)));
120   OBJECT_SET_OR_FAIL(route_json, "metric", json_integer(trp_route_get_metric(route)));
121   OBJECT_SET_OR_FAIL(route_json, "trust_router", tr_name_to_json_string(trp_route_get_trust_router(route)));
122   if (trp_route_get_next_hop(route)->len > 0)
123     OBJECT_SET_OR_FAIL(route_json, "next_hop", tr_name_to_json_string(trp_route_get_next_hop(route)));
124   OBJECT_SET_OR_FAIL(route_json, "selected", json_boolean(trp_route_is_selected(route)));
125   OBJECT_SET_OR_FAIL(route_json, "local", json_boolean(trp_route_is_local(route)));
126   OBJECT_SET_OR_SKIP(route_json, "expires", expiry_to_json_string(route));
127
128   /* succeeded - set the return value and increment the reference count */
129   retval = route_json;
130   json_incref(retval);
131
132
133 cleanup:
134   if (route_json)
135     json_decref(route_json);
136   return retval;
137 }