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.
41 #include <tr_name_internal.h>
42 #include <trp_internal.h>
43 #include <trp_rtable.h>
45 #include <trust_router/trp.h>
46 #include <trust_router/tid.h>
48 /* Note: be careful mixing talloc with glib. */
50 static int trp_route_destructor(void *obj)
52 TRP_ROUTE *entry=talloc_get_type_abort(obj, TRP_ROUTE);
53 if (entry->comm!=NULL)
54 tr_free_name(entry->comm);
55 if (entry->realm!=NULL)
56 tr_free_name(entry->realm);
57 if (entry->trust_router!=NULL)
58 tr_free_name(entry->trust_router);
59 if (entry->peer!=NULL)
60 tr_free_name(entry->peer);
61 if (entry->next_hop!=NULL)
62 tr_free_name(entry->next_hop);
66 TRP_ROUTE *trp_route_new(TALLOC_CTX *mem_ctx)
68 TRP_ROUTE *entry=talloc(mem_ctx, TRP_ROUTE);
72 entry->trust_router=NULL;
73 entry->trp_port=TRP_PORT;
74 entry->tid_port=TID_PORT;
79 entry->expiry=talloc(entry, struct timespec);
80 if (entry->expiry==NULL) {
84 *(entry->expiry)=(struct timespec){0,0};
87 talloc_set_destructor((void *)entry, trp_route_destructor);
92 void trp_route_free(TRP_ROUTE *entry)
98 void trp_route_set_comm(TRP_ROUTE *entry, TR_NAME *comm)
100 if (entry->comm!=NULL)
101 tr_free_name(entry->comm);
105 TR_NAME *trp_route_get_comm(TRP_ROUTE *entry)
110 TR_NAME *trp_route_dup_comm(TRP_ROUTE *entry)
112 return tr_dup_name(trp_route_get_comm(entry));
115 void trp_route_set_realm(TRP_ROUTE *entry, TR_NAME *realm)
117 if (entry->realm!=NULL)
118 tr_free_name(entry->realm);
122 TR_NAME *trp_route_get_realm(TRP_ROUTE *entry)
127 TR_NAME *trp_route_dup_realm(TRP_ROUTE *entry)
129 return tr_dup_name(trp_route_get_realm(entry));
132 void trp_route_set_trust_router(TRP_ROUTE *entry, TR_NAME *tr)
134 if (entry->trust_router!=NULL)
135 tr_free_name(entry->trust_router);
136 entry->trust_router=tr;
139 TR_NAME *trp_route_get_trust_router(TRP_ROUTE *entry)
141 return entry->trust_router;
144 TR_NAME *trp_route_dup_trust_router(TRP_ROUTE *entry)
146 return tr_dup_name(trp_route_get_trust_router(entry));
149 void trp_route_set_peer(TRP_ROUTE *entry, TR_NAME *peer)
151 if (entry->peer!=NULL)
152 tr_free_name(entry->peer);
156 TR_NAME *trp_route_get_peer(TRP_ROUTE *entry)
161 TR_NAME *trp_route_dup_peer(TRP_ROUTE *entry)
163 return tr_dup_name(trp_route_get_peer(entry));
166 void trp_route_set_metric(TRP_ROUTE *entry, unsigned int metric)
168 entry->metric=metric;
171 unsigned int trp_route_get_metric(TRP_ROUTE *entry)
173 return entry->metric;
176 /* TODO: set the hostname and port for the next hop. Currently assume default TID port. --jlr */
177 void trp_route_set_next_hop(TRP_ROUTE *entry, TR_NAME *next_hop)
179 if (entry->next_hop!=NULL)
180 tr_free_name(entry->next_hop);
181 entry->next_hop=next_hop;
184 TR_NAME *trp_route_get_next_hop(TRP_ROUTE *entry)
186 return entry->next_hop;
189 TR_NAME *trp_route_dup_next_hop(TRP_ROUTE *entry)
191 return tr_dup_name(trp_route_get_next_hop(entry));
194 void trp_route_set_selected(TRP_ROUTE *entry, int sel)
199 int trp_route_is_selected(TRP_ROUTE *entry)
201 return entry->selected;
204 void trp_route_set_interval(TRP_ROUTE *entry, int interval)
206 entry->interval=interval;
209 int trp_route_get_interval(TRP_ROUTE *entry)
211 return entry->interval;
214 /* copies incoming value, does not assume responsibility for freeing */
215 void trp_route_set_expiry(TRP_ROUTE *entry, struct timespec *exp)
217 entry->expiry->tv_sec=exp->tv_sec;
218 entry->expiry->tv_nsec=exp->tv_nsec;
221 struct timespec *trp_route_get_expiry(TRP_ROUTE *entry)
223 return entry->expiry;
226 void trp_route_set_local(TRP_ROUTE *entry, int local)
231 int trp_route_is_local(TRP_ROUTE *entry)
236 void trp_route_set_triggered(TRP_ROUTE *entry, int trig)
238 tr_debug("trp_route_set_triggered: setting route to %.*s/%.*s through %.*s to %s",
239 entry->comm->len, entry->comm->buf,
240 entry->realm->len, entry->realm->buf,
241 entry->peer->len, entry->peer->buf,
242 trig ? "triggered" : "not triggered");
243 entry->triggered=trig;
246 int trp_route_is_triggered(TRP_ROUTE *entry)
248 return entry->triggered;
252 /* result must be freed with g_free */
253 static gchar *tr_name_to_g_str(const TR_NAME *n)
255 gchar *s=g_strndup(n->buf, n->len);
257 tr_debug("tr_name_to_g_str: allocation failure.");
261 /* hash function for TR_NAME keys */
262 static guint trp_tr_name_hash(gconstpointer key)
264 const TR_NAME *name=(TR_NAME *)key;
265 gchar *s=tr_name_to_g_str(name);
266 guint hash=g_str_hash(s);
271 /* hash equality function for TR_NAME keys */
272 static gboolean trp_tr_name_equal(gconstpointer key1, gconstpointer key2)
274 const TR_NAME *n1=(TR_NAME *)key1;
275 const TR_NAME *n2=(TR_NAME *)key2;
276 gchar *s1=tr_name_to_g_str(n1);
277 gchar *s2=tr_name_to_g_str(n2);
278 gboolean equal=g_str_equal(s1, s2);
284 /* free a value to the top level rtable (a hash of all entries in the comm) */
285 static void trp_rtable_destroy_table(gpointer data)
287 g_hash_table_destroy(data);
290 static void trp_rtable_destroy_rentry(gpointer data)
292 trp_route_free(data);
295 static void trp_rtable_destroy_tr_name(gpointer data)
300 TRP_RTABLE *trp_rtable_new(void)
302 GHashTable *new=g_hash_table_new_full(trp_tr_name_hash,
304 trp_rtable_destroy_tr_name,
305 trp_rtable_destroy_table);
309 void trp_rtable_free(TRP_RTABLE *rtbl)
311 g_hash_table_destroy(rtbl);
314 static GHashTable *trp_rtbl_get_or_add_table(GHashTable *tbl, TR_NAME *key, GDestroyNotify destroy)
316 GHashTable *val_tbl=NULL;
318 val_tbl=g_hash_table_lookup(tbl, key);
320 val_tbl=g_hash_table_new_full(trp_tr_name_hash,
322 trp_rtable_destroy_tr_name,
324 g_hash_table_insert(tbl, tr_dup_name(key), val_tbl);
329 void trp_rtable_add(TRP_RTABLE *rtbl, TRP_ROUTE *entry)
331 GHashTable *comm_tbl=NULL;
332 GHashTable *realm_tbl=NULL;
334 comm_tbl=trp_rtbl_get_or_add_table(rtbl, entry->comm, trp_rtable_destroy_table);
335 realm_tbl=trp_rtbl_get_or_add_table(comm_tbl, entry->realm, trp_rtable_destroy_rentry);
336 g_hash_table_insert(realm_tbl, tr_dup_name(entry->peer), entry); /* destroys and replaces a duplicate */
337 /* the route entry should not belong to any context, we will manage it ourselves */
338 talloc_steal(NULL, entry);
341 /* note: the entry pointer passed in is invalid after calling this because the entry is freed */
342 void trp_rtable_remove(TRP_RTABLE *rtbl, TRP_ROUTE *entry)
344 GHashTable *comm_tbl=NULL;
345 GHashTable *realm_tbl=NULL;
347 comm_tbl=g_hash_table_lookup(rtbl, entry->comm);
351 realm_tbl=g_hash_table_lookup(comm_tbl, entry->realm);
355 /* remove the element */
356 g_hash_table_remove(realm_tbl, entry->peer);
357 /* if that was the last entry in the realm, remove the realm table */
358 if (g_hash_table_size(realm_tbl)==0)
359 g_hash_table_remove(comm_tbl, entry->realm);
360 /* if that was the last realm in the comm, remove the comm table */
361 if (g_hash_table_size(comm_tbl)==0)
362 g_hash_table_remove(rtbl, entry->comm);
365 void trp_rtable_clear(TRP_RTABLE *rtbl)
367 g_hash_table_remove_all(rtbl); /* destructors should do all the cleanup */
370 /* gets the actual hash table, for internal use only */
371 static GHashTable *trp_rtable_get_comm_table(TRP_RTABLE *rtbl, TR_NAME *comm)
373 return g_hash_table_lookup(rtbl, comm);
376 /* gets the actual hash table, for internal use only */
377 static GHashTable *trp_rtable_get_realm_table(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
379 GHashTable *comm_tbl=trp_rtable_get_comm_table(rtbl, comm);
383 return g_hash_table_lookup(comm_tbl, realm);
386 struct table_size_cookie {
390 static void trp_rtable_size_helper(gpointer key, gpointer value, gpointer user_data)
392 struct table_size_cookie *data=(struct table_size_cookie *)user_data;
393 data->size += trp_rtable_comm_size(data->rtbl, (TR_NAME *)key);
395 size_t trp_rtable_size(TRP_RTABLE *rtbl)
397 struct table_size_cookie data={rtbl, 0};
398 g_hash_table_foreach(rtbl, trp_rtable_size_helper, &data);
402 struct table_comm_size_cookie {
407 static void table_comm_size_helper(gpointer key, gpointer value, gpointer user_data)
409 struct table_comm_size_cookie *data=(struct table_comm_size_cookie *)user_data;
410 data->size += trp_rtable_realm_size(data->rtbl, data->comm, (TR_NAME *)key);
412 size_t trp_rtable_comm_size(TRP_RTABLE *rtbl, TR_NAME *comm)
414 struct table_comm_size_cookie data={comm, rtbl, 0};
415 GHashTable *comm_tbl=trp_rtable_get_comm_table(rtbl, comm);
418 g_hash_table_foreach(comm_tbl, table_comm_size_helper, &data);
422 size_t trp_rtable_realm_size(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
424 GHashTable *realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
428 return g_hash_table_size(g_hash_table_lookup(
429 g_hash_table_lookup(rtbl, comm),
433 /* Returns an array of pointers to TRP_ROUTE, length of array in n_out.
434 * Caller must free the array (in the talloc NULL context), but must
435 * not free its contents. */
436 TRP_ROUTE **trp_rtable_get_entries(TRP_RTABLE *rtbl, size_t *n_out)
438 TRP_ROUTE **ret=NULL;
441 TRP_ROUTE **comm_entries=NULL;
445 *n_out=trp_rtable_size(rtbl);
449 ret=talloc_array(NULL, TRP_ROUTE *, *n_out);
451 tr_crit("trp_rtable_get_entries: unable to allocate return array.");
456 ii_ret=0; /* counts output entries */
457 comm=trp_rtable_get_comms(rtbl, &n_comm);
459 comm_entries=trp_rtable_get_comm_entries(rtbl, comm[n_comm], &n_entries);
461 ret[ii_ret++]=comm_entries[n_entries];
462 talloc_free(comm_entries);
466 if (ii_ret!=*n_out) {
467 tr_crit("trp_rtable_get_entries: found incorrect number of entries.");
475 /* Returns an array of pointers to TR_NAME, length of array in n_out.
476 * Caller must free the array (in the talloc NULL context). */
477 TR_NAME **trp_rtable_get_comms(TRP_RTABLE *rtbl, size_t *n_out)
479 size_t len=g_hash_table_size(rtbl); /* known comms are keys in top level hash table */
490 ret=talloc_array(NULL, TR_NAME *, len);
492 tr_crit("trp_rtable_get_comms: unable to allocate return array.");
496 comms=g_hash_table_get_keys(rtbl);
497 for (ii=0,p=comms; p!=NULL; ii++,p=g_list_next(p))
498 ret[ii]=(TR_NAME *)p->data;
506 /* Returns an array of pointers to TR_NAME, length of array in n_out.
507 * Caller must free the array (in the talloc NULL context). */
508 TR_NAME **trp_rtable_get_comm_realms(TRP_RTABLE *rtbl, TR_NAME *comm, size_t *n_out)
511 TRP_RTABLE *comm_tbl=g_hash_table_lookup(rtbl, comm);;
516 if (comm_tbl==NULL) {
520 *n_out=g_hash_table_size(comm_tbl); /* set output length */
521 ret=talloc_array(NULL, TR_NAME *, *n_out);
522 entries=g_hash_table_get_keys(comm_tbl);
523 for (ii=0,p=entries; p!=NULL; ii++,p=g_list_next(p))
524 ret[ii]=(TR_NAME *)p->data;
526 g_list_free(entries);
530 /* Get all entries in an comm. Returns an array of pointers in NULL talloc context.
531 * Caller must free this list with talloc_free, but must not free the entries in the
533 TRP_ROUTE **trp_rtable_get_comm_entries(TRP_RTABLE *rtbl, TR_NAME *comm, size_t *n_out)
536 TR_NAME **realm=NULL;
538 TRP_ROUTE **realm_entries=NULL;
540 TRP_ROUTE **ret=NULL;
543 *n_out=trp_rtable_comm_size(rtbl, comm);
547 ret=talloc_array(NULL, TRP_ROUTE *, *n_out);
549 tr_crit("trp_rtable_get_comm_entries: could not allocate return array.");
554 ii_ret=0; /* counts entries in the output array */
555 realm=trp_rtable_get_comm_realms(rtbl, comm, &n_realms);
556 for (ii=0; ii<n_realms; ii++) {
557 realm_entries=trp_rtable_get_realm_entries(rtbl, comm, realm[ii], &n_entries);
558 for (jj=0; jj<n_entries; jj++)
559 ret[ii_ret++]=realm_entries[jj];
560 talloc_free(realm_entries);
564 if (ii_ret!=*n_out) {
565 tr_crit("trp_rtable_get_comm_entries: found incorrect number of entries.");
574 /* Get all entries in an comm/realm. Returns an array of pointers in NULL talloc context.
575 * Caller must free this list with talloc_free, but must not free the entries in the
577 TRP_ROUTE **trp_rtable_get_realm_entries(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, size_t *n_out)
580 TRP_ROUTE **ret=NULL;
583 tr_debug("trp_rtable_get_realm_entries: entered.");
584 peer=trp_rtable_get_comm_realm_peers(rtbl, comm, realm, n_out);
585 ret=talloc_array(NULL, TRP_ROUTE *, *n_out);
587 tr_crit("trp_rtable_get_realm_entries: could not allocate return array.");
592 for (ii=0; ii<*n_out; ii++)
593 ret[ii]=trp_rtable_get_entry(rtbl, comm, realm, peer[ii]);
598 TR_NAME **trp_rtable_get_comm_realm_peers(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, size_t *n_out)
601 GHashTable *realm_tbl=NULL;
606 *n_out=trp_rtable_realm_size(rtbl, comm, realm);
609 realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
610 ret=talloc_array(NULL, TR_NAME *, *n_out);
612 tr_crit("trp_rtable_get_comm_realm_peers: could not allocate return array.");
616 keys=g_hash_table_get_keys(realm_tbl);
617 for (ii=0,p=keys; p!=NULL; ii++,p=g_list_next(p))
618 ret[ii]=(TR_NAME *)p->data;
623 /* Gets a single entry. Do not free it. */
624 TRP_ROUTE *trp_rtable_get_entry(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
626 GHashTable *realm_tbl=NULL;
628 realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
632 return g_hash_table_lookup(realm_tbl, peer); /* does not copy or increment ref count */
635 static char *timespec_to_str(struct timespec *ts)
640 if (localtime_r(&(ts->tv_sec), &tm)==NULL)
643 s=malloc(40); /* long enough to contain strftime result */
647 if (strftime(s, 40, "%F %T", &tm)==0) {
654 TRP_ROUTE *trp_rtable_get_selected_entry(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
658 TRP_ROUTE **entry=trp_rtable_get_realm_entries(rtbl, comm, realm, &n);
659 TRP_ROUTE *selected=NULL;
664 tr_debug("trp_rtable_get_selected_entry: looking through route table entries for realm %.*s.",
665 realm->len, realm->buf);
666 for(ii=0; ii<n; ii++) {
667 if (trp_route_is_selected(entry[ii])) {
672 tr_debug("trp_rtable_get_selected_entry: ii=%d.", ii);
678 /* Pretty print a route table entry to a newly allocated string. If sep is NULL,
679 * returns comma+space separated string. */
680 char *trp_route_to_str(TALLOC_CTX *mem_ctx, TRP_ROUTE *entry, const char *sep)
682 char *comm=tr_name_strdup(entry->comm);
683 char *realm=tr_name_strdup(entry->realm);
684 char *peer=tr_name_strdup(entry->peer);
685 char *trust_router=tr_name_strdup(entry->trust_router);
686 char *next_hop=tr_name_strdup(entry->next_hop);
687 char *expiry=timespec_to_str(entry->expiry);
693 result=talloc_asprintf(mem_ctx,
694 "%s%s%s%s%s%s%u%s%s%s%s%s%u%s%u%s%s%s%u",
701 entry->selected, sep,
714 void trp_rtable_clear_triggered(TRP_RTABLE *rtbl)
717 TRP_ROUTE **entries=trp_rtable_get_entries(rtbl, &n_entries);
721 for (ii=0; ii<n_entries; ii++)
722 trp_route_set_triggered(entries[ii], 0);
723 talloc_free(entries);
727 static int sort_tr_names_cmp(const void *a, const void *b)
729 TR_NAME **n1=(TR_NAME **)a;
730 TR_NAME **n2=(TR_NAME **)b;
731 return tr_name_cmp(*n1, *n2);
734 static void sort_tr_names(TR_NAME **names, size_t n_names)
736 qsort(names, n_names, sizeof(TR_NAME *), sort_tr_names_cmp);
739 char *trp_rtable_to_str(TALLOC_CTX *mem_ctx, TRP_RTABLE *rtbl, const char *sep, const char *lineterm)
741 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
742 TR_NAME **comms=NULL;
744 TR_NAME **realms=NULL;
746 TRP_ROUTE **entries=NULL;
748 char **tbl_strings=NULL;
749 size_t ii_tbl=0; /* counts tbl_strings */
752 size_t ii=0, jj=0, kk=0;
759 tbl_size=trp_rtable_size(rtbl);
761 result=talloc_strdup(mem_ctx, lineterm);
765 tbl_strings=talloc_array(tmp_ctx, char *, tbl_size);
766 if (tbl_strings==NULL) {
767 result=talloc_strdup(mem_ctx, "error");
771 comms=trp_rtable_get_comms(rtbl, &n_comms);
772 talloc_steal(tmp_ctx, comms);
773 sort_tr_names(comms, n_comms);
776 for (ii=0; ii<n_comms; ii++) {
777 realms=trp_rtable_get_comm_realms(rtbl, comms[ii], &n_realms);
778 talloc_steal(tmp_ctx, realms);
779 sort_tr_names(realms, n_realms);
780 for (jj=0; jj<n_realms; jj++) {
781 entries=trp_rtable_get_realm_entries(rtbl, comms[ii], realms[jj], &n_entries);
782 talloc_steal(tmp_ctx, entries);
783 for (kk=0; kk<n_entries; kk++) {
784 tbl_strings[ii_tbl]=trp_route_to_str(tmp_ctx, entries[kk], sep);
785 len+=strlen(tbl_strings[ii_tbl]);
788 talloc_free(entries);
794 /* now combine all the strings */
795 len += tbl_size*strlen(lineterm); /* space for line terminations*/
796 len += 1; /* nul terminator */
797 result=(char *)talloc_size(tmp_ctx, len);
798 for (p=result,ii=0; ii < tbl_size; ii++) {
799 p+=sprintf(p, "%s%s", tbl_strings[ii], lineterm);
801 talloc_steal(mem_ctx, result);
804 talloc_free(tmp_ctx);