7 #include <trust_router/tr_name.h>
8 #include <trp_internal.h>
9 #include <trp_rtable.h>
11 #include <trust_router/trp.h>
12 #include <trust_router/tid.h>
14 /* Note: be careful mixing talloc with glib. */
16 static int trp_route_destructor(void *obj)
18 TRP_ROUTE *entry=talloc_get_type_abort(obj, TRP_ROUTE);
19 if (entry->comm!=NULL)
20 tr_free_name(entry->comm);
21 if (entry->realm!=NULL)
22 tr_free_name(entry->realm);
23 if (entry->trust_router!=NULL)
24 tr_free_name(entry->trust_router);
25 if (entry->peer!=NULL)
26 tr_free_name(entry->peer);
27 if (entry->next_hop!=NULL)
28 tr_free_name(entry->next_hop);
32 TRP_ROUTE *trp_route_new(TALLOC_CTX *mem_ctx)
34 TRP_ROUTE *entry=talloc(mem_ctx, TRP_ROUTE);
38 entry->trust_router=NULL;
39 entry->trp_port=TRP_PORT;
40 entry->tid_port=TID_PORT;
45 entry->expiry=talloc(entry, struct timespec);
46 if (entry->expiry==NULL) {
50 *(entry->expiry)=(struct timespec){0,0};
53 talloc_set_destructor((void *)entry, trp_route_destructor);
58 void trp_route_free(TRP_ROUTE *entry)
64 void trp_route_set_comm(TRP_ROUTE *entry, TR_NAME *comm)
66 if (entry->comm!=NULL)
67 tr_free_name(entry->comm);
71 TR_NAME *trp_route_get_comm(TRP_ROUTE *entry)
76 TR_NAME *trp_route_dup_comm(TRP_ROUTE *entry)
78 return tr_dup_name(trp_route_get_comm(entry));
81 void trp_route_set_realm(TRP_ROUTE *entry, TR_NAME *realm)
83 if (entry->realm!=NULL)
84 tr_free_name(entry->realm);
88 TR_NAME *trp_route_get_realm(TRP_ROUTE *entry)
93 TR_NAME *trp_route_dup_realm(TRP_ROUTE *entry)
95 return tr_dup_name(trp_route_get_realm(entry));
98 void trp_route_set_trust_router(TRP_ROUTE *entry, TR_NAME *tr)
100 if (entry->trust_router!=NULL)
101 tr_free_name(entry->trust_router);
102 entry->trust_router=tr;
105 TR_NAME *trp_route_get_trust_router(TRP_ROUTE *entry)
107 return entry->trust_router;
110 TR_NAME *trp_route_dup_trust_router(TRP_ROUTE *entry)
112 return tr_dup_name(trp_route_get_trust_router(entry));
115 void trp_route_set_peer(TRP_ROUTE *entry, TR_NAME *peer)
117 if (entry->peer!=NULL)
118 tr_free_name(entry->peer);
122 TR_NAME *trp_route_get_peer(TRP_ROUTE *entry)
127 TR_NAME *trp_route_dup_peer(TRP_ROUTE *entry)
129 return tr_dup_name(trp_route_get_peer(entry));
132 void trp_route_set_metric(TRP_ROUTE *entry, unsigned int metric)
134 entry->metric=metric;
137 unsigned int trp_route_get_metric(TRP_ROUTE *entry)
139 return entry->metric;
142 /* TODO: set the hostname and port for the next hop. Currently assume default TID port. --jlr */
143 void trp_route_set_next_hop(TRP_ROUTE *entry, TR_NAME *next_hop)
145 if (entry->next_hop!=NULL)
146 tr_free_name(entry->next_hop);
147 entry->next_hop=next_hop;
150 TR_NAME *trp_route_get_next_hop(TRP_ROUTE *entry)
152 return entry->next_hop;
155 TR_NAME *trp_route_dup_next_hop(TRP_ROUTE *entry)
157 return tr_dup_name(trp_route_get_next_hop(entry));
160 void trp_route_set_selected(TRP_ROUTE *entry, int sel)
165 int trp_route_is_selected(TRP_ROUTE *entry)
167 return entry->selected;
170 void trp_route_set_interval(TRP_ROUTE *entry, int interval)
172 entry->interval=interval;
175 int trp_route_get_interval(TRP_ROUTE *entry)
177 return entry->interval;
180 /* copies incoming value, does not assume responsibility for freeing */
181 void trp_route_set_expiry(TRP_ROUTE *entry, struct timespec *exp)
183 entry->expiry->tv_sec=exp->tv_sec;
184 entry->expiry->tv_nsec=exp->tv_nsec;
187 struct timespec *trp_route_get_expiry(TRP_ROUTE *entry)
189 return entry->expiry;
192 void trp_route_set_local(TRP_ROUTE *entry, int local)
197 int trp_route_is_local(TRP_ROUTE *entry)
202 void trp_route_set_triggered(TRP_ROUTE *entry, int trig)
204 tr_debug("trp_route_set_triggered: setting route to %.*s/%.*s through %.*s to %s",
205 entry->comm->len, entry->comm->buf,
206 entry->realm->len, entry->realm->buf,
207 entry->peer->len, entry->peer->buf,
208 trig ? "triggered" : "not triggered");
209 entry->triggered=trig;
212 int trp_route_is_triggered(TRP_ROUTE *entry)
214 return entry->triggered;
218 /* result must be freed with g_free */
219 static gchar *tr_name_to_g_str(const TR_NAME *n)
221 gchar *s=g_strndup(n->buf, n->len);
223 tr_debug("tr_name_to_g_str: allocation failure.");
227 /* hash function for TR_NAME keys */
228 static guint trp_tr_name_hash(gconstpointer key)
230 const TR_NAME *name=(TR_NAME *)key;
231 gchar *s=tr_name_to_g_str(name);
232 guint hash=g_str_hash(s);
237 /* hash equality function for TR_NAME keys */
238 static gboolean trp_tr_name_equal(gconstpointer key1, gconstpointer key2)
240 const TR_NAME *n1=(TR_NAME *)key1;
241 const TR_NAME *n2=(TR_NAME *)key2;
242 gchar *s1=tr_name_to_g_str(n1);
243 gchar *s2=tr_name_to_g_str(n2);
244 gboolean equal=g_str_equal(s1, s2);
250 /* free a value to the top level rtable (a hash of all entries in the comm) */
251 static void trp_rtable_destroy_table(gpointer data)
253 g_hash_table_destroy(data);
256 static void trp_rtable_destroy_rentry(gpointer data)
258 trp_route_free(data);
261 static void trp_rtable_destroy_tr_name(gpointer data)
266 TRP_RTABLE *trp_rtable_new(void)
268 GHashTable *new=g_hash_table_new_full(trp_tr_name_hash,
270 trp_rtable_destroy_tr_name,
271 trp_rtable_destroy_table);
275 void trp_rtable_free(TRP_RTABLE *rtbl)
277 g_hash_table_destroy(rtbl);
280 static GHashTable *trp_rtbl_get_or_add_table(GHashTable *tbl, TR_NAME *key, GDestroyNotify destroy)
282 GHashTable *val_tbl=NULL;
284 val_tbl=g_hash_table_lookup(tbl, key);
286 val_tbl=g_hash_table_new_full(trp_tr_name_hash,
288 trp_rtable_destroy_tr_name,
290 g_hash_table_insert(tbl, tr_dup_name(key), val_tbl);
295 void trp_rtable_add(TRP_RTABLE *rtbl, TRP_ROUTE *entry)
297 GHashTable *comm_tbl=NULL;
298 GHashTable *realm_tbl=NULL;
300 comm_tbl=trp_rtbl_get_or_add_table(rtbl, entry->comm, trp_rtable_destroy_table);
301 realm_tbl=trp_rtbl_get_or_add_table(comm_tbl, entry->realm, trp_rtable_destroy_rentry);
302 g_hash_table_insert(realm_tbl, tr_dup_name(entry->peer), entry); /* destroys and replaces a duplicate */
303 /* the route entry should not belong to any context, we will manage it ourselves */
304 talloc_steal(NULL, entry);
307 /* note: the entry pointer passed in is invalid after calling this because the entry is freed */
308 void trp_rtable_remove(TRP_RTABLE *rtbl, TRP_ROUTE *entry)
310 GHashTable *comm_tbl=NULL;
311 GHashTable *realm_tbl=NULL;
313 comm_tbl=g_hash_table_lookup(rtbl, entry->comm);
317 realm_tbl=g_hash_table_lookup(comm_tbl, entry->realm);
321 /* remove the element */
322 g_hash_table_remove(realm_tbl, entry->peer);
323 /* if that was the last entry in the realm, remove the realm table */
324 if (g_hash_table_size(realm_tbl)==0)
325 g_hash_table_remove(comm_tbl, entry->realm);
326 /* if that was the last realm in the comm, remove the comm table */
327 if (g_hash_table_size(comm_tbl)==0)
328 g_hash_table_remove(rtbl, entry->comm);
331 void trp_rtable_clear(TRP_RTABLE *rtbl)
333 g_hash_table_remove_all(rtbl); /* destructors should do all the cleanup */
336 /* gets the actual hash table, for internal use only */
337 static GHashTable *trp_rtable_get_comm_table(TRP_RTABLE *rtbl, TR_NAME *comm)
339 return g_hash_table_lookup(rtbl, comm);
342 /* gets the actual hash table, for internal use only */
343 static GHashTable *trp_rtable_get_realm_table(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
345 GHashTable *comm_tbl=trp_rtable_get_comm_table(rtbl, comm);
349 return g_hash_table_lookup(comm_tbl, realm);
352 struct table_size_cookie {
356 static void trp_rtable_size_helper(gpointer key, gpointer value, gpointer user_data)
358 struct table_size_cookie *data=(struct table_size_cookie *)user_data;
359 data->size += trp_rtable_comm_size(data->rtbl, (TR_NAME *)key);
361 size_t trp_rtable_size(TRP_RTABLE *rtbl)
363 struct table_size_cookie data={rtbl, 0};
364 g_hash_table_foreach(rtbl, trp_rtable_size_helper, &data);
368 struct table_comm_size_cookie {
373 static void table_comm_size_helper(gpointer key, gpointer value, gpointer user_data)
375 struct table_comm_size_cookie *data=(struct table_comm_size_cookie *)user_data;
376 data->size += trp_rtable_realm_size(data->rtbl, data->comm, (TR_NAME *)key);
378 size_t trp_rtable_comm_size(TRP_RTABLE *rtbl, TR_NAME *comm)
380 struct table_comm_size_cookie data={comm, rtbl, 0};
381 GHashTable *comm_tbl=trp_rtable_get_comm_table(rtbl, comm);
384 g_hash_table_foreach(comm_tbl, table_comm_size_helper, &data);
388 size_t trp_rtable_realm_size(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
390 GHashTable *realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
394 return g_hash_table_size(g_hash_table_lookup(
395 g_hash_table_lookup(rtbl, comm),
399 /* Returns an array of pointers to TRP_ROUTE, length of array in n_out.
400 * Caller must free the array (in the talloc NULL context), but must
401 * not free its contents. */
402 TRP_ROUTE **trp_rtable_get_entries(TRP_RTABLE *rtbl, size_t *n_out)
404 TRP_ROUTE **ret=NULL;
407 TRP_ROUTE **comm_entries=NULL;
411 *n_out=trp_rtable_size(rtbl);
415 ret=talloc_array(NULL, TRP_ROUTE *, *n_out);
417 tr_crit("trp_rtable_get_entries: unable to allocate return array.");
422 ii_ret=0; /* counts output entries */
423 comm=trp_rtable_get_comms(rtbl, &n_comm);
425 comm_entries=trp_rtable_get_comm_entries(rtbl, comm[n_comm], &n_entries);
427 ret[ii_ret++]=comm_entries[n_entries];
428 talloc_free(comm_entries);
432 if (ii_ret!=*n_out) {
433 tr_crit("trp_rtable_get_entries: found incorrect number of entries.");
441 /* Returns an array of pointers to TR_NAME, length of array in n_out.
442 * Caller must free the array (in the talloc NULL context). */
443 TR_NAME **trp_rtable_get_comms(TRP_RTABLE *rtbl, size_t *n_out)
445 size_t len=g_hash_table_size(rtbl); /* known comms are keys in top level hash table */
456 ret=talloc_array(NULL, TR_NAME *, len);
458 tr_crit("trp_rtable_get_comms: unable to allocate return array.");
462 comms=g_hash_table_get_keys(rtbl);
463 for (ii=0,p=comms; p!=NULL; ii++,p=g_list_next(p))
464 ret[ii]=(TR_NAME *)p->data;
472 /* Returns an array of pointers to TR_NAME, length of array in n_out.
473 * Caller must free the array (in the talloc NULL context). */
474 TR_NAME **trp_rtable_get_comm_realms(TRP_RTABLE *rtbl, TR_NAME *comm, size_t *n_out)
477 TRP_RTABLE *comm_tbl=g_hash_table_lookup(rtbl, comm);;
482 if (comm_tbl==NULL) {
486 *n_out=g_hash_table_size(comm_tbl); /* set output length */
487 ret=talloc_array(NULL, TR_NAME *, *n_out);
488 entries=g_hash_table_get_keys(comm_tbl);
489 for (ii=0,p=entries; p!=NULL; ii++,p=g_list_next(p))
490 ret[ii]=(TR_NAME *)p->data;
492 g_list_free(entries);
496 /* Get all entries in an comm. Returns an array of pointers in NULL talloc context.
497 * Caller must free this list with talloc_free, but must not free the entries in the
499 TRP_ROUTE **trp_rtable_get_comm_entries(TRP_RTABLE *rtbl, TR_NAME *comm, size_t *n_out)
502 TR_NAME **realm=NULL;
504 TRP_ROUTE **realm_entries=NULL;
506 TRP_ROUTE **ret=NULL;
509 *n_out=trp_rtable_comm_size(rtbl, comm);
513 ret=talloc_array(NULL, TRP_ROUTE *, *n_out);
515 tr_crit("trp_rtable_get_comm_entries: could not allocate return array.");
520 ii_ret=0; /* counts entries in the output array */
521 realm=trp_rtable_get_comm_realms(rtbl, comm, &n_realms);
522 for (ii=0; ii<n_realms; ii++) {
523 realm_entries=trp_rtable_get_realm_entries(rtbl, comm, realm[ii], &n_entries);
524 for (jj=0; jj<n_entries; jj++)
525 ret[ii_ret++]=realm_entries[jj];
526 talloc_free(realm_entries);
530 if (ii_ret!=*n_out) {
531 tr_crit("trp_rtable_get_comm_entries: found incorrect number of entries.");
540 /* Get all entries in an comm/realm. Returns an array of pointers in NULL talloc context.
541 * Caller must free this list with talloc_free, but must not free the entries in the
543 TRP_ROUTE **trp_rtable_get_realm_entries(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, size_t *n_out)
546 TRP_ROUTE **ret=NULL;
549 tr_debug("trp_rtable_get_realm_entries: entered.");
550 peer=trp_rtable_get_comm_realm_peers(rtbl, comm, realm, n_out);
551 ret=talloc_array(NULL, TRP_ROUTE *, *n_out);
553 tr_crit("trp_rtable_get_realm_entries: could not allocate return array.");
558 for (ii=0; ii<*n_out; ii++)
559 ret[ii]=trp_rtable_get_entry(rtbl, comm, realm, peer[ii]);
564 TR_NAME **trp_rtable_get_comm_realm_peers(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, size_t *n_out)
567 GHashTable *realm_tbl=NULL;
572 *n_out=trp_rtable_realm_size(rtbl, comm, realm);
575 realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
576 ret=talloc_array(NULL, TR_NAME *, *n_out);
578 tr_crit("trp_rtable_get_comm_realm_peers: could not allocate return array.");
582 keys=g_hash_table_get_keys(realm_tbl);
583 for (ii=0,p=keys; p!=NULL; ii++,p=g_list_next(p))
584 ret[ii]=(TR_NAME *)p->data;
589 /* Gets a single entry. Do not free it. */
590 TRP_ROUTE *trp_rtable_get_entry(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
592 GHashTable *realm_tbl=NULL;
594 realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
598 return g_hash_table_lookup(realm_tbl, peer); /* does not copy or increment ref count */
601 static char *timespec_to_str(struct timespec *ts)
606 if (localtime_r(&(ts->tv_sec), &tm)==NULL)
609 s=malloc(40); /* long enough to contain strftime result */
613 if (strftime(s, 40, "%F %T", &tm)==0) {
620 TRP_ROUTE *trp_rtable_get_selected_entry(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
624 TRP_ROUTE **entry=trp_rtable_get_realm_entries(rtbl, comm, realm, &n);
625 TRP_ROUTE *selected=NULL;
630 tr_debug("trp_rtable_get_selected_entry: looking through route table entries for realm %.*s.",
631 realm->len, realm->buf);
632 for(ii=0; ii<n; ii++) {
633 if (trp_route_is_selected(entry[ii])) {
638 tr_debug("trp_rtable_get_selected_entry: ii=%d.", ii);
644 /* Pretty print a route table entry to a newly allocated string. If sep is NULL,
645 * returns comma+space separated string. */
646 char *trp_route_to_str(TALLOC_CTX *mem_ctx, TRP_ROUTE *entry, const char *sep)
648 char *comm=tr_name_strdup(entry->comm);
649 char *realm=tr_name_strdup(entry->realm);
650 char *peer=tr_name_strdup(entry->peer);
651 char *trust_router=tr_name_strdup(entry->trust_router);
652 char *next_hop=tr_name_strdup(entry->next_hop);
653 char *expiry=timespec_to_str(entry->expiry);
659 result=talloc_asprintf(mem_ctx,
660 "%s%s%s%s%s%s%u%s%s%s%s%s%u%s%u%s%s%s%u",
667 entry->selected, sep,
680 void trp_rtable_clear_triggered(TRP_RTABLE *rtbl)
683 TRP_ROUTE **entries=trp_rtable_get_entries(rtbl, &n_entries);
687 for (ii=0; ii<n_entries; ii++)
688 trp_route_set_triggered(entries[ii], 0);
689 talloc_free(entries);
693 static int sort_tr_names_cmp(const void *a, const void *b)
695 TR_NAME **n1=(TR_NAME **)a;
696 TR_NAME **n2=(TR_NAME **)b;
697 return tr_name_cmp(*n1, *n2);
700 static void sort_tr_names(TR_NAME **names, size_t n_names)
702 qsort(names, n_names, sizeof(TR_NAME *), sort_tr_names_cmp);
705 char *trp_rtable_to_str(TALLOC_CTX *mem_ctx, TRP_RTABLE *rtbl, const char *sep, const char *lineterm)
707 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
708 TR_NAME **comms=NULL;
710 TR_NAME **realms=NULL;
712 TRP_ROUTE **entries=NULL;
714 char **tbl_strings=NULL;
715 size_t ii_tbl=0; /* counts tbl_strings */
718 size_t ii=0, jj=0, kk=0;
725 tbl_size=trp_rtable_size(rtbl);
727 result=talloc_strdup(mem_ctx, lineterm);
731 tbl_strings=talloc_array(tmp_ctx, char *, tbl_size);
732 if (tbl_strings==NULL) {
733 result=talloc_strdup(mem_ctx, "error");
737 comms=trp_rtable_get_comms(rtbl, &n_comms);
738 talloc_steal(tmp_ctx, comms);
739 sort_tr_names(comms, n_comms);
742 for (ii=0; ii<n_comms; ii++) {
743 realms=trp_rtable_get_comm_realms(rtbl, comms[ii], &n_realms);
744 talloc_steal(tmp_ctx, realms);
745 sort_tr_names(realms, n_realms);
746 for (jj=0; jj<n_realms; jj++) {
747 entries=trp_rtable_get_realm_entries(rtbl, comms[ii], realms[jj], &n_entries);
748 talloc_steal(tmp_ctx, entries);
749 for (kk=0; kk<n_entries; kk++) {
750 tbl_strings[ii_tbl]=trp_route_to_str(tmp_ctx, entries[kk], sep);
751 len+=strlen(tbl_strings[ii_tbl]);
754 talloc_free(entries);
760 /* now combine all the strings */
761 len += tbl_size*strlen(lineterm); /* space for line terminations*/
762 len += 1; /* nul terminator */
763 result=(char *)talloc_size(tmp_ctx, len);
764 for (p=result,ii=0; ii < tbl_size; ii++) {
765 p+=sprintf(p, "%s%s", tbl_strings[ii], lineterm);
767 talloc_steal(mem_ctx, result);
770 talloc_free(tmp_ctx);