Merge branch 'milestone/monitoring' into jennifer/request_id
[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
49
50 /* Note: be careful mixing talloc with glib. */
51
52 static int trp_route_destructor(void *obj)
53 {
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);
65   return 0;
66 }
67
68 TRP_ROUTE *trp_route_new(TALLOC_CTX *mem_ctx)
69 {
70   TRP_ROUTE *entry=talloc(mem_ctx, TRP_ROUTE);
71   if (entry!=NULL) {
72     entry->comm=NULL;
73     entry->realm=NULL;
74     entry->trust_router=NULL;
75     entry->trp_port=TRP_PORT;
76     entry->tid_port=TID_PORT;
77     entry->peer=NULL;
78     entry->next_hop=NULL;
79     entry->selected=0;
80     entry->interval=0;
81     entry->expiry=talloc(entry, struct timespec);
82     if (entry->expiry==NULL) {
83       talloc_free(entry);
84       return NULL;
85     }
86     *(entry->expiry)=(struct timespec){0,0};
87     entry->local=0;
88     entry->triggered=0;
89     talloc_set_destructor((void *)entry, trp_route_destructor);
90   }
91   return entry;
92 }
93
94 void trp_route_free(TRP_ROUTE *entry)
95 {
96   if (entry!=NULL)
97     talloc_free(entry);
98 }
99
100 void trp_route_set_comm(TRP_ROUTE *entry, TR_NAME *comm)
101 {
102   if (entry->comm!=NULL)
103     tr_free_name(entry->comm);
104   entry->comm=comm;
105 }
106
107 TR_NAME *trp_route_get_comm(TRP_ROUTE *entry)
108 {
109   return entry->comm;
110 }
111
112 TR_NAME *trp_route_dup_comm(TRP_ROUTE *entry)
113 {
114   return tr_dup_name(trp_route_get_comm(entry));
115 }
116
117 void trp_route_set_realm(TRP_ROUTE *entry, TR_NAME *realm)
118 {
119   if (entry->realm!=NULL)
120     tr_free_name(entry->realm);
121   entry->realm=realm;
122 }
123
124 TR_NAME *trp_route_get_realm(TRP_ROUTE *entry)
125 {
126   return entry->realm;
127 }
128
129 TR_NAME *trp_route_dup_realm(TRP_ROUTE *entry)
130 {
131   return tr_dup_name(trp_route_get_realm(entry));
132 }
133
134 void trp_route_set_trust_router(TRP_ROUTE *entry, TR_NAME *tr)
135 {
136   if (entry->trust_router!=NULL)
137     tr_free_name(entry->trust_router);
138   entry->trust_router=tr;
139 }
140
141 TR_NAME *trp_route_get_trust_router(TRP_ROUTE *entry)
142 {
143   return entry->trust_router;
144 }
145
146 TR_NAME *trp_route_dup_trust_router(TRP_ROUTE *entry)
147 {
148   return tr_dup_name(trp_route_get_trust_router(entry));
149 }
150
151 void trp_route_set_peer(TRP_ROUTE *entry, TR_NAME *peer)
152 {
153   if (entry->peer!=NULL)
154     tr_free_name(entry->peer);
155   entry->peer=peer;
156 }
157
158 TR_NAME *trp_route_get_peer(TRP_ROUTE *entry)
159 {
160   return entry->peer;
161 }
162
163 TR_NAME *trp_route_dup_peer(TRP_ROUTE *entry)
164 {
165   return tr_dup_name(trp_route_get_peer(entry));
166 }
167
168 void trp_route_set_metric(TRP_ROUTE *entry, unsigned int metric)
169 {
170   entry->metric=metric;
171 }
172
173 unsigned int trp_route_get_metric(TRP_ROUTE *entry)
174 {
175   return entry->metric;
176 }
177
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)
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 void trp_route_set_local(TRP_ROUTE *entry, int local)
229 {
230   entry->local=local;
231 }
232
233 int trp_route_is_local(TRP_ROUTE *entry)
234 {
235   return entry->local;
236 }
237
238 void trp_route_set_triggered(TRP_ROUTE *entry, int trig)
239 {
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;
246 }
247
248 int trp_route_is_triggered(TRP_ROUTE *entry)
249 {
250   return entry->triggered;
251 }