Merge pull request #86 from painless-security/jennifer/aaa_server_port
[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 void trp_route_set_next_hop(TRP_ROUTE *entry, TR_NAME *next_hop)
180 {
181   if (entry->next_hop!=NULL)
182     tr_free_name(entry->next_hop);
183   entry->next_hop=next_hop;
184 }
185
186 TR_NAME *trp_route_get_next_hop(TRP_ROUTE *entry)
187 {
188   return entry->next_hop;
189 }
190
191 TR_NAME *trp_route_dup_next_hop(TRP_ROUTE *entry)
192 {
193   return tr_dup_name(trp_route_get_next_hop(entry));
194 }
195
196 void trp_route_set_selected(TRP_ROUTE *entry, int sel)
197 {
198   entry->selected=sel;
199 }
200
201 int trp_route_is_selected(TRP_ROUTE *entry)
202 {
203   return entry->selected;
204 }
205
206 void trp_route_set_interval(TRP_ROUTE *entry, int interval)
207 {
208   entry->interval=interval;
209 }
210
211 int trp_route_get_interval(TRP_ROUTE *entry)
212 {
213   return entry->interval;
214 }
215
216 /* copies incoming value, does not assume responsibility for freeing */
217 void trp_route_set_expiry(TRP_ROUTE *entry, struct timespec *exp)
218 {
219   entry->expiry->tv_sec=exp->tv_sec;
220   entry->expiry->tv_nsec=exp->tv_nsec;
221 }
222
223 struct timespec *trp_route_get_expiry(TRP_ROUTE *entry)
224 {
225   return entry->expiry;
226 }
227
228 /**
229  * Get the expiration according to the realtime clock
230  *
231  * @param entry
232  * @param result space to store the result
233  * @return pointer to the result, or null on error
234  */
235 struct timespec *trp_route_get_expiry_realtime(TRP_ROUTE *entry, struct timespec *result)
236 {
237   return tr_clock_convert(TRP_CLOCK, entry->expiry, CLOCK_REALTIME, result);
238 }
239
240 void trp_route_set_local(TRP_ROUTE *entry, int local)
241 {
242   entry->local=local;
243 }
244
245 int trp_route_is_local(TRP_ROUTE *entry)
246 {
247   return entry->local;
248 }
249
250 void trp_route_set_triggered(TRP_ROUTE *entry, int trig)
251 {
252   tr_debug("trp_route_set_triggered: setting route to %.*s/%.*s through %.*s to %s",
253            entry->comm->len, entry->comm->buf,
254            entry->realm->len, entry->realm->buf,
255            entry->peer->len, entry->peer->buf,
256            trig ? "triggered" : "not triggered");
257   entry->triggered=trig;
258 }
259
260 int trp_route_is_triggered(TRP_ROUTE *entry)
261 {
262   return entry->triggered;
263 }
264
265 void trp_route_set_trust_router_port(TRP_ROUTE *entry, int port)
266 {
267   if (entry)
268     entry->trust_router_port = port;
269 }
270
271 /**
272  * Get the port to use for TRP connections to the trust router
273  *
274  * @param entry
275  * @return port, or -1 if entry is null
276  */
277 int trp_route_get_trust_router_port(TRP_ROUTE *entry)
278 {
279   if (entry)
280     return entry->trust_router_port;
281
282   return -1;
283 }
284
285 void trp_route_set_next_hop_port(TRP_ROUTE *entry, int port)
286 {
287   if (entry)
288     entry->next_hop_port = port;
289 }
290
291 /**
292  * Get the port to use for TID connections to the next hop
293  *
294  * @param entry
295  * @return port, or -1 if entry is null
296  */
297 int trp_route_get_next_hop_port(TRP_ROUTE *entry)
298 {
299   if (entry)
300     return entry->next_hop_port;
301
302   return -1;
303 }