Merge pull request #86 from painless-security/jennifer/aaa_server_port
[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 #include <tr_inet_util.h>
49
50 /* Pretty print a route table entry to a newly allocated string. If sep is NULL,
51  * returns comma+space separated string. */
52 char *trp_route_to_str(TALLOC_CTX *mem_ctx, TRP_ROUTE *entry, const char *sep)
53 {
54   char *comm=tr_name_strdup(entry->comm);
55   char *realm=tr_name_strdup(entry->realm);
56   char *peer=tr_name_strdup(entry->peer);
57   char *trust_router=tr_name_strdup(entry->trust_router);
58   char *next_hop=tr_name_strdup(entry->next_hop);
59   char *expiry=timespec_to_str(entry->expiry);
60   char *result=NULL;
61
62   if (sep==NULL)
63     sep=", ";
64
65   result=talloc_asprintf(mem_ctx,
66                          "%s%s%s%s%s%s%u%s%s:%d%s%s:%d%s%u%s%u%s%s%s%u",
67                          comm, sep,
68                          realm, sep,
69                          peer, sep,
70                          entry->metric, sep,
71                          trust_router, entry->trust_router_port, sep,
72                          next_hop, entry->next_hop_port, sep,
73                          entry->selected, sep,
74                          entry->local, sep,
75                          expiry, sep,
76                          entry->triggered);
77   free(comm);
78   free(realm);
79   free(peer);
80   free(trust_router);
81   free(next_hop);
82   free(expiry);
83   return result;
84 }
85
86 /* helper */
87 static json_t *expiry_to_json_string(TRP_ROUTE *route)
88 {
89   struct timespec ts = {0}; /* initialization to zero is important */
90   char *s = NULL;
91   json_t *jstr = NULL;
92
93   if (tr_cmp_timespec(trp_route_get_expiry(route), &ts) > 0) {
94     if (trp_route_get_expiry_realtime(route, &ts) == NULL)
95       s = strdup("error");
96     else
97       s = timespec_to_str(&ts);
98
99     if (s) {
100       jstr = json_string(s);
101       free(s);
102     }
103   }
104
105   return jstr;
106 }
107
108 json_t *trp_route_to_json(TRP_ROUTE *route)
109 {
110   json_t *route_json = NULL;
111   json_t *retval = NULL;
112   TR_NAME *n;
113
114   route_json = json_object();
115   if (route_json == NULL)
116     goto cleanup;
117
118   OBJECT_SET_OR_FAIL(route_json, "community", tr_name_to_json_string(trp_route_get_comm(route)));
119   OBJECT_SET_OR_FAIL(route_json, "realm", tr_name_to_json_string(trp_route_get_realm(route)));
120   if (trp_route_get_peer(route)->len > 0)
121     OBJECT_SET_OR_FAIL(route_json, "peer", tr_name_to_json_string(trp_route_get_peer(route)));
122   OBJECT_SET_OR_FAIL(route_json, "metric", json_integer(trp_route_get_metric(route)));
123
124   /* add trust_router as hostname:port */
125   n = tr_hostname_and_port_to_name(
126           trp_route_get_trust_router(route),
127           trp_route_get_trust_router_port(route));
128   if (n == NULL)
129     goto cleanup;
130   OBJECT_SET_OR_FAIL(route_json, "trust_router", tr_name_to_json_string(n));
131   tr_free_name(n);
132
133   /* add next_hop as hostname:port */
134   n = tr_hostname_and_port_to_name(
135       trp_route_get_next_hop(route),
136       trp_route_get_next_hop_port(route));
137   if (n == NULL)
138     goto cleanup;
139   OBJECT_SET_OR_FAIL(route_json, "next_hop", tr_name_to_json_string(n));
140   tr_free_name(n);
141
142   OBJECT_SET_OR_FAIL(route_json, "selected", json_boolean(trp_route_is_selected(route)));
143   OBJECT_SET_OR_FAIL(route_json, "local", json_boolean(trp_route_is_local(route)));
144   OBJECT_SET_OR_SKIP(route_json, "expires", expiry_to_json_string(route));
145
146   /* succeeded - set the return value and increment the reference count */
147   retval = route_json;
148   json_incref(retval);
149
150
151 cleanup:
152   if (route_json)
153     json_decref(route_json);
154   return retval;
155 }