Support non-default TRP and TID ports
[trust_router.git] / trp / trp_route.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 <glib.h>
38 #include <talloc.h>
39 #include <time.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 <tr_debug.h>
46 #include <trust_router/trp.h>
47 #include <trust_router/tid.h>
48 #include <tr_util.h>
49
50
51 /* Note: be careful mixing talloc with glib. */
52
53 static int trp_route_destructor(void *obj)
54 {
55   TRP_ROUTE *entry=talloc_get_type_abort(obj, TRP_ROUTE);
56   if (entry->comm!=NULL)
57     tr_free_name(entry->comm);
58   if (entry->realm!=NULL)
59     tr_free_name(entry->realm);
60   if (entry->trust_router!=NULL)
61     tr_free_name(entry->trust_router);
62   if (entry->peer!=NULL)
63     tr_free_name(entry->peer);
64   if (entry->next_hop!=NULL)
65     tr_free_name(entry->next_hop);
66   return 0;
67 }
68
69 TRP_ROUTE *trp_route_new(TALLOC_CTX *mem_ctx)
70 {
71   TRP_ROUTE *entry=talloc(mem_ctx, TRP_ROUTE);
72   if (entry!=NULL) {
73     entry->comm=NULL;
74     entry->realm=NULL;
75     entry->trust_router=NULL;
76     entry->trust_router_port=TRP_PORT;
77     entry->next_hop_port=TID_PORT;
78     entry->peer=NULL;
79     entry->next_hop=NULL;
80     entry->selected=0;
81     entry->interval=0;
82     entry->expiry=talloc(entry, struct timespec);
83     if (entry->expiry==NULL) {
84       talloc_free(entry);
85       return NULL;
86     }
87     *(entry->expiry)=(struct timespec){0,0};
88     entry->local=0;
89     entry->triggered=0;
90     talloc_set_destructor((void *)entry, trp_route_destructor);
91   }
92   return entry;
93 }
94
95 void trp_route_free(TRP_ROUTE *entry)
96 {
97   if (entry!=NULL)
98     talloc_free(entry);
99 }
100
101 void trp_route_set_comm(TRP_ROUTE *entry, TR_NAME *comm)
102 {
103   if (entry->comm!=NULL)
104     tr_free_name(entry->comm);
105   entry->comm=comm;
106 }
107
108 TR_NAME *trp_route_get_comm(TRP_ROUTE *entry)
109 {
110   return entry->comm;
111 }
112
113 TR_NAME *trp_route_dup_comm(TRP_ROUTE *entry)
114 {
115   return tr_dup_name(trp_route_get_comm(entry));
116 }
117
118 void trp_route_set_realm(TRP_ROUTE *entry, TR_NAME *realm)
119 {
120   if (entry->realm!=NULL)
121     tr_free_name(entry->realm);
122   entry->realm=realm;
123 }
124
125 TR_NAME *trp_route_get_realm(TRP_ROUTE *entry)
126 {
127   return entry->realm;
128 }
129
130 TR_NAME *trp_route_dup_realm(TRP_ROUTE *entry)
131 {
132   return tr_dup_name(trp_route_get_realm(entry));
133 }
134
135 void trp_route_set_trust_router(TRP_ROUTE *entry, TR_NAME *tr)
136 {
137   if (entry->trust_router!=NULL)
138     tr_free_name(entry->trust_router);
139   entry->trust_router=tr;
140 }
141
142 TR_NAME *trp_route_get_trust_router(TRP_ROUTE *entry)
143 {
144   return entry->trust_router;
145 }
146
147 TR_NAME *trp_route_dup_trust_router(TRP_ROUTE *entry)
148 {
149   return tr_dup_name(trp_route_get_trust_router(entry));
150 }
151
152 void trp_route_set_peer(TRP_ROUTE *entry, TR_NAME *peer)
153 {
154   if (entry->peer!=NULL)
155     tr_free_name(entry->peer);
156   entry->peer=peer;
157 }
158
159 TR_NAME *trp_route_get_peer(TRP_ROUTE *entry)
160 {
161   return entry->peer;
162 }
163
164 TR_NAME *trp_route_dup_peer(TRP_ROUTE *entry)
165 {
166   return tr_dup_name(trp_route_get_peer(entry));
167 }
168
169 void trp_route_set_metric(TRP_ROUTE *entry, unsigned int metric)
170 {
171   entry->metric=metric;
172 }
173
174 unsigned int trp_route_get_metric(TRP_ROUTE *entry)
175 {
176   return entry->metric;
177 }
178
179 /* TODO: set the hostname and port for the next hop. Currently assume default TID port. --jlr */
180 void trp_route_set_next_hop(TRP_ROUTE *entry, TR_NAME *next_hop)
181 {
182   if (entry->next_hop!=NULL)
183     tr_free_name(entry->next_hop);
184   entry->next_hop=next_hop;
185 }
186
187 TR_NAME *trp_route_get_next_hop(TRP_ROUTE *entry)
188 {
189   return entry->next_hop;
190 }
191
192 TR_NAME *trp_route_dup_next_hop(TRP_ROUTE *entry)
193 {
194   return tr_dup_name(trp_route_get_next_hop(entry));
195 }
196
197 void trp_route_set_selected(TRP_ROUTE *entry, int sel)
198 {
199   entry->selected=sel;
200 }
201
202 int trp_route_is_selected(TRP_ROUTE *entry)
203 {
204   return entry->selected;
205 }
206
207 void trp_route_set_interval(TRP_ROUTE *entry, int interval)
208 {
209   entry->interval=interval;
210 }
211
212 int trp_route_get_interval(TRP_ROUTE *entry)
213 {
214   return entry->interval;
215 }
216
217 /* copies incoming value, does not assume responsibility for freeing */
218 void trp_route_set_expiry(TRP_ROUTE *entry, struct timespec *exp)
219 {
220   entry->expiry->tv_sec=exp->tv_sec;
221   entry->expiry->tv_nsec=exp->tv_nsec;
222 }
223
224 struct timespec *trp_route_get_expiry(TRP_ROUTE *entry)
225 {
226   return entry->expiry;
227 }
228
229 /**
230  * Get the expiration according to the realtime clock
231  *
232  * @param entry
233  * @param result space to store the result
234  * @return pointer to the result, or null on error
235  */
236 struct timespec *trp_route_get_expiry_realtime(TRP_ROUTE *entry, struct timespec *result)
237 {
238   return tr_clock_convert(TRP_CLOCK, entry->expiry, CLOCK_REALTIME, result);
239 }
240
241 void trp_route_set_local(TRP_ROUTE *entry, int local)
242 {
243   entry->local=local;
244 }
245
246 int trp_route_is_local(TRP_ROUTE *entry)
247 {
248   return entry->local;
249 }
250
251 void trp_route_set_triggered(TRP_ROUTE *entry, int trig)
252 {
253   tr_debug("trp_route_set_triggered: setting route to %.*s/%.*s through %.*s to %s",
254            entry->comm->len, entry->comm->buf,
255            entry->realm->len, entry->realm->buf,
256            entry->peer->len, entry->peer->buf,
257            trig ? "triggered" : "not triggered");
258   entry->triggered=trig;
259 }
260
261 int trp_route_is_triggered(TRP_ROUTE *entry)
262 {
263   return entry->triggered;
264 }
265
266 void trp_route_set_trust_router_port(TRP_ROUTE *entry, int port)
267 {
268   if (entry)
269     entry->trust_router_port = port;
270 }
271
272 /**
273  * Get the port to use for TRP connections to the trust router
274  *
275  * @param entry
276  * @return port, or -1 if entry is null
277  */
278 int trp_route_get_trust_router_port(TRP_ROUTE *entry)
279 {
280   if (entry)
281     return entry->trust_router_port;
282
283   return -1;
284 }
285
286 void trp_route_set_next_hop_port(TRP_ROUTE *entry, int port)
287 {
288   if (entry)
289     entry->next_hop_port = port;
290 }
291
292 /**
293  * Get the port to use for TID connections to the next hop
294  *
295  * @param entry
296  * @return port, or -1 if entry is null
297  */
298 int trp_route_get_next_hop_port(TRP_ROUTE *entry)
299 {
300   if (entry)
301     return entry->next_hop_port;
302
303   return -1;
304 }