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>
46 #include <trust_router/trp.h>
47 #include <trust_router/tid.h>
51 /* Note: be careful mixing talloc with glib. */
53 static int trp_route_destructor(void *obj)
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);
69 TRP_ROUTE *trp_route_new(TALLOC_CTX *mem_ctx)
71 TRP_ROUTE *entry=talloc(mem_ctx, TRP_ROUTE);
75 entry->trust_router=NULL;
76 entry->trust_router_port=TRP_PORT;
77 entry->next_hop_port=TID_PORT;
82 entry->expiry=talloc(entry, struct timespec);
83 if (entry->expiry==NULL) {
87 *(entry->expiry)=(struct timespec){0,0};
90 talloc_set_destructor((void *)entry, trp_route_destructor);
95 void trp_route_free(TRP_ROUTE *entry)
101 void trp_route_set_comm(TRP_ROUTE *entry, TR_NAME *comm)
103 if (entry->comm!=NULL)
104 tr_free_name(entry->comm);
108 TR_NAME *trp_route_get_comm(TRP_ROUTE *entry)
113 TR_NAME *trp_route_dup_comm(TRP_ROUTE *entry)
115 return tr_dup_name(trp_route_get_comm(entry));
118 void trp_route_set_realm(TRP_ROUTE *entry, TR_NAME *realm)
120 if (entry->realm!=NULL)
121 tr_free_name(entry->realm);
125 TR_NAME *trp_route_get_realm(TRP_ROUTE *entry)
130 TR_NAME *trp_route_dup_realm(TRP_ROUTE *entry)
132 return tr_dup_name(trp_route_get_realm(entry));
135 void trp_route_set_trust_router(TRP_ROUTE *entry, TR_NAME *tr)
137 if (entry->trust_router!=NULL)
138 tr_free_name(entry->trust_router);
139 entry->trust_router=tr;
142 TR_NAME *trp_route_get_trust_router(TRP_ROUTE *entry)
144 return entry->trust_router;
147 TR_NAME *trp_route_dup_trust_router(TRP_ROUTE *entry)
149 return tr_dup_name(trp_route_get_trust_router(entry));
152 void trp_route_set_peer(TRP_ROUTE *entry, TR_NAME *peer)
154 if (entry->peer!=NULL)
155 tr_free_name(entry->peer);
159 TR_NAME *trp_route_get_peer(TRP_ROUTE *entry)
164 TR_NAME *trp_route_dup_peer(TRP_ROUTE *entry)
166 return tr_dup_name(trp_route_get_peer(entry));
169 void trp_route_set_metric(TRP_ROUTE *entry, unsigned int metric)
171 entry->metric=metric;
174 unsigned int trp_route_get_metric(TRP_ROUTE *entry)
176 return entry->metric;
179 void trp_route_set_next_hop(TRP_ROUTE *entry, TR_NAME *next_hop)
181 if (entry->next_hop!=NULL)
182 tr_free_name(entry->next_hop);
183 entry->next_hop=next_hop;
186 TR_NAME *trp_route_get_next_hop(TRP_ROUTE *entry)
188 return entry->next_hop;
191 TR_NAME *trp_route_dup_next_hop(TRP_ROUTE *entry)
193 return tr_dup_name(trp_route_get_next_hop(entry));
196 void trp_route_set_selected(TRP_ROUTE *entry, int sel)
201 int trp_route_is_selected(TRP_ROUTE *entry)
203 return entry->selected;
206 void trp_route_set_interval(TRP_ROUTE *entry, int interval)
208 entry->interval=interval;
211 int trp_route_get_interval(TRP_ROUTE *entry)
213 return entry->interval;
216 /* copies incoming value, does not assume responsibility for freeing */
217 void trp_route_set_expiry(TRP_ROUTE *entry, struct timespec *exp)
219 entry->expiry->tv_sec=exp->tv_sec;
220 entry->expiry->tv_nsec=exp->tv_nsec;
223 struct timespec *trp_route_get_expiry(TRP_ROUTE *entry)
225 return entry->expiry;
229 * Get the expiration according to the realtime clock
232 * @param result space to store the result
233 * @return pointer to the result, or null on error
235 struct timespec *trp_route_get_expiry_realtime(TRP_ROUTE *entry, struct timespec *result)
237 return tr_clock_convert(TRP_CLOCK, entry->expiry, CLOCK_REALTIME, result);
240 void trp_route_set_local(TRP_ROUTE *entry, int local)
245 int trp_route_is_local(TRP_ROUTE *entry)
250 void trp_route_set_triggered(TRP_ROUTE *entry, int trig)
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;
260 int trp_route_is_triggered(TRP_ROUTE *entry)
262 return entry->triggered;
265 void trp_route_set_trust_router_port(TRP_ROUTE *entry, int port)
268 entry->trust_router_port = port;
272 * Get the port to use for TRP connections to the trust router
275 * @return port, or -1 if entry is null
277 int trp_route_get_trust_router_port(TRP_ROUTE *entry)
280 return entry->trust_router_port;
285 void trp_route_set_next_hop_port(TRP_ROUTE *entry, int port)
288 entry->next_hop_port = port;
292 * Get the port to use for TID connections to the next hop
295 * @return port, or -1 if entry is null
297 int trp_route_get_next_hop_port(TRP_ROUTE *entry)
300 return entry->next_hop_port;