2 * Copyright (c) 2016, 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.
38 #include <trust_router/tr_name.h>
39 #include <trp_internal.h>
43 /* static prototypes */
44 static void *trp_inforec_route_new(TALLOC_CTX *mem_ctx);
45 static void trp_inforec_route_print(TRP_INFOREC_DATA);
48 struct trp_inforec_type_entry {
50 TRP_INFOREC_TYPE type;
51 void *(*allocate)(TALLOC_CTX *);
52 void (*print)(TRP_INFOREC_DATA);
54 static struct trp_inforec_type_entry trp_inforec_type_table[] = {
55 { "route", TRP_INFOREC_TYPE_ROUTE, trp_inforec_route_new, trp_inforec_route_print },
56 { "comm", TRP_INFOREC_TYPE_COMMUNITY, NULL, NULL },
57 { NULL, TRP_INFOREC_TYPE_UNKNOWN, NULL, NULL } /* must be the last entry */
61 /* look up an entry in the trp_inforec_type_table */
62 static struct trp_inforec_type_entry *get_trp_inforec_type_entry(TRP_INFOREC_TYPE msgtype)
64 struct trp_inforec_type_entry *entry=trp_inforec_type_table;
66 while ((entry->type != TRP_INFOREC_TYPE_UNKNOWN)
67 && (entry->type != msgtype)) {
73 /* translate strings to codes */
74 TRP_INFOREC_TYPE trp_inforec_type_from_string(const char *s)
76 struct trp_inforec_type_entry *entry=trp_inforec_type_table;
78 while ((entry->type != TRP_INFOREC_TYPE_UNKNOWN)
79 && (strcmp(s, entry->name)!=0)) {
84 /* translate codes to strings (do not need to be freed)
85 * Returns NULL on an unknown code */
86 const char *trp_inforec_type_to_string(TRP_INFOREC_TYPE msgtype)
88 struct trp_inforec_type_entry *entry=get_trp_inforec_type_entry(msgtype);
92 /* called by talloc when destroying an update message body */
93 static int trp_inforec_route_destructor(void *object)
95 TRP_INFOREC_ROUTE *body=talloc_get_type_abort(object, TRP_INFOREC_ROUTE);
97 /* clean up TR_NAME data, which are not managed by talloc */
98 if (body->comm != NULL) {
99 tr_free_name(body->comm);
101 tr_debug("trp_inforec_route_destructor: freed community");
103 if (body->realm != NULL) {
104 tr_free_name(body->realm);
106 tr_debug("trp_inforec_route_destructor: freed realm");
108 if (body->trust_router != NULL) {
109 tr_free_name(body->trust_router);
110 body->trust_router=NULL;
111 tr_debug("trp_inforec_route_destructor: freed trust_router");
113 if (body->next_hop != NULL) {
114 tr_free_name(body->next_hop);
116 tr_debug("trp_inforec_route_destructor: freed next_hop");
122 static void *trp_inforec_route_new(TALLOC_CTX *mem_ctx)
124 TRP_INFOREC_ROUTE *new_rec=talloc(mem_ctx, TRP_INFOREC_ROUTE);
126 if (new_rec != NULL) {
129 new_rec->trust_router=NULL;
130 new_rec->next_hop=NULL;
131 new_rec->next_hop_port=0;
132 new_rec->metric=TRP_METRIC_INFINITY;
134 talloc_set_destructor((void *)new_rec, trp_inforec_route_destructor);
139 TRP_INFOREC *trp_inforec_get_next(TRP_INFOREC *rec)
147 static TRP_INFOREC *trp_inforec_get_tail(TRP_INFOREC *rec)
149 while ((rec->next)!=NULL)
150 rec=trp_inforec_get_next(rec);
154 void trp_inforec_set_next(TRP_INFOREC *rec, TRP_INFOREC *next_rec)
160 TRP_INFOREC_TYPE trp_inforec_get_type(TRP_INFOREC *rec)
165 return TRP_INFOREC_TYPE_UNKNOWN;
168 void trp_inforec_set_type(TRP_INFOREC *rec, TRP_INFOREC_TYPE type)
174 TR_NAME *trp_inforec_get_comm(TRP_INFOREC *rec)
177 case TRP_INFOREC_TYPE_ROUTE:
178 if (rec->data.route!=NULL)
179 return rec->data.route->comm;
187 TR_NAME *trp_inforec_dup_comm(TRP_INFOREC *rec)
189 return tr_dup_name(trp_inforec_get_comm(rec));
192 TRP_RC trp_inforec_set_comm(TRP_INFOREC *rec, TR_NAME *comm)
195 case TRP_INFOREC_TYPE_ROUTE:
196 if (rec->data.route!=NULL) {
197 rec->data.route->comm=comm;
207 TR_NAME *trp_inforec_get_realm(TRP_INFOREC *rec)
210 case TRP_INFOREC_TYPE_ROUTE:
211 if (rec->data.route!=NULL)
212 return rec->data.route->realm;
220 TR_NAME *trp_inforec_dup_realm(TRP_INFOREC *rec)
222 return tr_dup_name(trp_inforec_get_realm(rec));
225 TRP_RC trp_inforec_set_realm(TRP_INFOREC *rec, TR_NAME *realm)
228 case TRP_INFOREC_TYPE_ROUTE:
229 if (rec->data.route!=NULL) {
230 rec->data.route->realm=realm;
240 TR_NAME *trp_inforec_get_trust_router(TRP_INFOREC *rec)
243 case TRP_INFOREC_TYPE_ROUTE:
244 if (rec->data.route!=NULL)
245 return rec->data.route->trust_router;
253 TR_NAME *trp_inforec_dup_trust_router(TRP_INFOREC *rec)
255 return tr_dup_name(trp_inforec_get_trust_router(rec));
258 TRP_RC trp_inforec_set_trust_router(TRP_INFOREC *rec, TR_NAME *trust_router)
261 case TRP_INFOREC_TYPE_ROUTE:
262 if (rec->data.route!=NULL) {
263 rec->data.route->trust_router=trust_router;
273 /* TODO: need to return hostname/port --jlr */
274 TR_NAME *trp_inforec_get_next_hop(TRP_INFOREC *rec)
277 case TRP_INFOREC_TYPE_ROUTE:
278 if (rec->data.route!=NULL)
279 return rec->data.route->next_hop;
287 TR_NAME *trp_inforec_dup_next_hop(TRP_INFOREC *rec)
289 return tr_dup_name(trp_inforec_get_next_hop(rec));
292 TRP_RC trp_inforec_set_next_hop(TRP_INFOREC *rec, TR_NAME *next_hop)
295 case TRP_INFOREC_TYPE_ROUTE:
296 if (rec->data.route!=NULL) {
297 rec->data.route->next_hop=next_hop;
307 unsigned int trp_inforec_get_metric(TRP_INFOREC *rec)
310 case TRP_INFOREC_TYPE_ROUTE:
311 if (rec->data.route!=NULL)
312 return rec->data.route->metric;
317 return TRP_METRIC_INVALID;
320 TRP_RC trp_inforec_set_metric(TRP_INFOREC *rec, unsigned int metric)
323 case TRP_INFOREC_TYPE_ROUTE:
324 if (rec->data.route!=NULL) {
325 rec->data.route->metric=metric;
335 unsigned int trp_inforec_get_interval(TRP_INFOREC *rec)
338 case TRP_INFOREC_TYPE_ROUTE:
339 if (rec->data.route!=NULL)
340 return rec->data.route->interval;
345 return TRP_INTERVAL_INVALID;
348 TRP_RC trp_inforec_set_interval(TRP_INFOREC *rec, unsigned int interval)
351 case TRP_INFOREC_TYPE_ROUTE:
352 if (rec->data.route!=NULL) {
353 rec->data.route->interval=interval;
363 /* for internal use only; must set rec->type before calling this */
364 static TRP_RC trp_inforec_set_data(TRP_INFOREC *rec, void *data)
370 case TRP_INFOREC_TYPE_ROUTE:
371 rec->data.route=talloc_get_type(data, TRP_INFOREC_ROUTE);
379 /* generic record type */
380 TRP_INFOREC *trp_inforec_new(TALLOC_CTX *mem_ctx, TRP_INFOREC_TYPE type)
382 TRP_INFOREC *new_rec=talloc(mem_ctx, TRP_INFOREC);
383 struct trp_inforec_type_entry *dtype=get_trp_inforec_type_entry(type);
385 if ((new_rec != NULL) && (dtype->type != TRP_INFOREC_TYPE_UNKNOWN)) {
386 trp_inforec_set_type(new_rec, type);
387 trp_inforec_set_next(new_rec, NULL);
388 if (dtype->allocate!=NULL) {
389 if (TRP_SUCCESS!=trp_inforec_set_data(new_rec, dtype->allocate(new_rec))) {
390 talloc_free(new_rec);
398 void trp_inforec_free(TRP_INFOREC *rec)
404 static int trp_upd_destructor(void *object)
406 TRP_UPD *upd=talloc_get_type_abort(object, TRP_UPD);
408 tr_free_name(upd->peer);
412 TRP_UPD *trp_upd_new(TALLOC_CTX *mem_ctx)
414 TRP_UPD *new_body=talloc(mem_ctx, TRP_UPD);
416 if (new_body!=NULL) {
417 new_body->records=NULL;
419 talloc_set_destructor((void *)new_body, trp_upd_destructor);
424 void trp_upd_free(TRP_UPD *update)
430 TRP_INFOREC *trp_upd_get_inforec(TRP_UPD *upd)
438 void trp_upd_set_inforec(TRP_UPD *upd, TRP_INFOREC *rec)
444 void trp_upd_add_inforec(TRP_UPD *upd, TRP_INFOREC *rec)
446 tr_debug("trp_upd_add_inforec: adding record.");
447 if (upd->records==NULL)
450 trp_inforec_set_next(trp_inforec_get_tail(upd->records), rec);
451 talloc_steal(upd, rec);
454 TR_NAME *trp_upd_get_peer(TRP_UPD *upd)
459 TR_NAME *trp_upd_dup_peer(TRP_UPD *upd)
461 return tr_dup_name(upd->peer);
464 void trp_upd_set_peer(TRP_UPD *upd, TR_NAME *peer)
469 void trp_upd_set_next_hop(TRP_UPD *upd, const char *hostname, unsigned int port)
471 TRP_INFOREC *rec=NULL;
474 for (rec=trp_upd_get_inforec(upd); rec!=NULL; rec=trp_inforec_get_next(rec)) {
475 if (trp_inforec_set_next_hop(rec, cpy=tr_new_name(hostname)) != TRP_SUCCESS) {
476 tr_err("trp_upd_set_peer: error setting peer.");
483 static void trp_inforec_route_print(TRP_INFOREC_DATA data)
485 if (data.route!=NULL) {
486 printf(" community=%.*s\n realm=%.*s\n trust_router=%.*s\n metric=%d\n interval=%d]\n",
487 data.route->comm->len, data.route->comm->buf,
488 data.route->realm->len, data.route->realm->buf,
489 data.route->trust_router->len, data.route->trust_router->buf,
490 data.route->metric, data.route->interval);