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>
50 /* Note: be careful mixing talloc with glib. */
52 static int trp_route_destructor(void *obj)
54 TRP_ROUTE *entry=talloc_get_type_abort(obj, TRP_ROUTE);
55 if (entry->comm!=NULL)
56 tr_free_name(entry->comm);
57 if (entry->realm!=NULL)
58 tr_free_name(entry->realm);
59 if (entry->trust_router!=NULL)
60 tr_free_name(entry->trust_router);
61 if (entry->peer!=NULL)
62 tr_free_name(entry->peer);
63 if (entry->next_hop!=NULL)
64 tr_free_name(entry->next_hop);
68 TRP_ROUTE *trp_route_new(TALLOC_CTX *mem_ctx)
70 TRP_ROUTE *entry=talloc(mem_ctx, TRP_ROUTE);
74 entry->trust_router=NULL;
75 entry->trp_port=TRP_PORT;
76 entry->tid_port=TID_PORT;
81 entry->expiry=talloc(entry, struct timespec);
82 if (entry->expiry==NULL) {
86 *(entry->expiry)=(struct timespec){0,0};
89 talloc_set_destructor((void *)entry, trp_route_destructor);
94 void trp_route_free(TRP_ROUTE *entry)
100 void trp_route_set_comm(TRP_ROUTE *entry, TR_NAME *comm)
102 if (entry->comm!=NULL)
103 tr_free_name(entry->comm);
107 TR_NAME *trp_route_get_comm(TRP_ROUTE *entry)
112 TR_NAME *trp_route_dup_comm(TRP_ROUTE *entry)
114 return tr_dup_name(trp_route_get_comm(entry));
117 void trp_route_set_realm(TRP_ROUTE *entry, TR_NAME *realm)
119 if (entry->realm!=NULL)
120 tr_free_name(entry->realm);
124 TR_NAME *trp_route_get_realm(TRP_ROUTE *entry)
129 TR_NAME *trp_route_dup_realm(TRP_ROUTE *entry)
131 return tr_dup_name(trp_route_get_realm(entry));
134 void trp_route_set_trust_router(TRP_ROUTE *entry, TR_NAME *tr)
136 if (entry->trust_router!=NULL)
137 tr_free_name(entry->trust_router);
138 entry->trust_router=tr;
141 TR_NAME *trp_route_get_trust_router(TRP_ROUTE *entry)
143 return entry->trust_router;
146 TR_NAME *trp_route_dup_trust_router(TRP_ROUTE *entry)
148 return tr_dup_name(trp_route_get_trust_router(entry));
151 void trp_route_set_peer(TRP_ROUTE *entry, TR_NAME *peer)
153 if (entry->peer!=NULL)
154 tr_free_name(entry->peer);
158 TR_NAME *trp_route_get_peer(TRP_ROUTE *entry)
163 TR_NAME *trp_route_dup_peer(TRP_ROUTE *entry)
165 return tr_dup_name(trp_route_get_peer(entry));
168 void trp_route_set_metric(TRP_ROUTE *entry, unsigned int metric)
170 entry->metric=metric;
173 unsigned int trp_route_get_metric(TRP_ROUTE *entry)
175 return entry->metric;
178 /* TODO: set the hostname and port for the next hop. Currently assume default TID port. --jlr */
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;
228 void trp_route_set_local(TRP_ROUTE *entry, int local)
233 int trp_route_is_local(TRP_ROUTE *entry)
238 void trp_route_set_triggered(TRP_ROUTE *entry, int trig)
240 tr_debug("trp_route_set_triggered: setting route to %.*s/%.*s through %.*s to %s",
241 entry->comm->len, entry->comm->buf,
242 entry->realm->len, entry->realm->buf,
243 entry->peer->len, entry->peer->buf,
244 trig ? "triggered" : "not triggered");
245 entry->triggered=trig;
248 int trp_route_is_triggered(TRP_ROUTE *entry)
250 return entry->triggered;