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 entry->triggered=trig;
207 int trp_route_is_triggered(TRP_ROUTE *entry)
209 return entry->triggered;
213 /* result must be freed with g_free */
214 static gchar *tr_name_to_g_str(const TR_NAME *n)
216 gchar *s=g_strndup(n->buf, n->len);
218 tr_debug("tr_name_to_g_str: allocation failure.");
222 /* hash function for TR_NAME keys */
223 static guint trp_tr_name_hash(gconstpointer key)
225 const TR_NAME *name=(TR_NAME *)key;
226 gchar *s=tr_name_to_g_str(name);
227 guint hash=g_str_hash(s);
232 /* hash equality function for TR_NAME keys */
233 static gboolean trp_tr_name_equal(gconstpointer key1, gconstpointer key2)
235 const TR_NAME *n1=(TR_NAME *)key1;
236 const TR_NAME *n2=(TR_NAME *)key2;
237 gchar *s1=tr_name_to_g_str(n1);
238 gchar *s2=tr_name_to_g_str(n2);
239 gboolean equal=g_str_equal(s1, s2);
245 /* free a value to the top level rtable (a hash of all entries in the comm) */
246 static void trp_rtable_destroy_table(gpointer data)
248 g_hash_table_destroy(data);
251 static void trp_rtable_destroy_rentry(gpointer data)
253 trp_route_free(data);
256 static void trp_rtable_destroy_tr_name(gpointer data)
261 TRP_RTABLE *trp_rtable_new(void)
263 GHashTable *new=g_hash_table_new_full(trp_tr_name_hash,
265 trp_rtable_destroy_tr_name,
266 trp_rtable_destroy_table);
270 void trp_rtable_free(TRP_RTABLE *rtbl)
272 g_hash_table_destroy(rtbl);
275 static GHashTable *trp_rtbl_get_or_add_table(GHashTable *tbl, TR_NAME *key, GDestroyNotify destroy)
277 GHashTable *val_tbl=NULL;
279 val_tbl=g_hash_table_lookup(tbl, key);
281 val_tbl=g_hash_table_new_full(trp_tr_name_hash,
283 trp_rtable_destroy_tr_name,
285 g_hash_table_insert(tbl, tr_dup_name(key), val_tbl);
290 void trp_rtable_add(TRP_RTABLE *rtbl, TRP_ROUTE *entry)
292 GHashTable *comm_tbl=NULL;
293 GHashTable *realm_tbl=NULL;
295 comm_tbl=trp_rtbl_get_or_add_table(rtbl, entry->comm, trp_rtable_destroy_table);
296 realm_tbl=trp_rtbl_get_or_add_table(comm_tbl, entry->realm, trp_rtable_destroy_rentry);
297 g_hash_table_insert(realm_tbl, tr_dup_name(entry->peer), entry); /* destroys and replaces a duplicate */
298 /* the route entry should not belong to any context, we will manage it ourselves */
299 talloc_steal(NULL, entry);
302 /* note: the entry pointer passed in is invalid after calling this because the entry is freed */
303 void trp_rtable_remove(TRP_RTABLE *rtbl, TRP_ROUTE *entry)
305 GHashTable *comm_tbl=NULL;
306 GHashTable *realm_tbl=NULL;
308 comm_tbl=g_hash_table_lookup(rtbl, entry->comm);
312 realm_tbl=g_hash_table_lookup(comm_tbl, entry->realm);
316 /* remove the element */
317 g_hash_table_remove(realm_tbl, entry->peer);
318 /* if that was the last entry in the realm, remove the realm table */
319 if (g_hash_table_size(realm_tbl)==0)
320 g_hash_table_remove(comm_tbl, entry->realm);
321 /* if that was the last realm in the comm, remove the comm table */
322 if (g_hash_table_size(comm_tbl)==0)
323 g_hash_table_remove(rtbl, entry->comm);
326 void trp_rtable_clear(TRP_RTABLE *rtbl)
328 g_hash_table_remove_all(rtbl); /* destructors should do all the cleanup */
331 /* gets the actual hash table, for internal use only */
332 static GHashTable *trp_rtable_get_comm_table(TRP_RTABLE *rtbl, TR_NAME *comm)
334 return g_hash_table_lookup(rtbl, comm);
337 /* gets the actual hash table, for internal use only */
338 static GHashTable *trp_rtable_get_realm_table(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
340 GHashTable *comm_tbl=trp_rtable_get_comm_table(rtbl, comm);
344 return g_hash_table_lookup(comm_tbl, realm);
347 struct table_size_cookie {
351 static void trp_rtable_size_helper(gpointer key, gpointer value, gpointer user_data)
353 struct table_size_cookie *data=(struct table_size_cookie *)user_data;
354 data->size += trp_rtable_comm_size(data->rtbl, (TR_NAME *)key);
356 size_t trp_rtable_size(TRP_RTABLE *rtbl)
358 struct table_size_cookie data={rtbl, 0};
359 g_hash_table_foreach(rtbl, trp_rtable_size_helper, &data);
363 struct table_comm_size_cookie {
368 static void table_comm_size_helper(gpointer key, gpointer value, gpointer user_data)
370 struct table_comm_size_cookie *data=(struct table_comm_size_cookie *)user_data;
371 data->size += trp_rtable_realm_size(data->rtbl, data->comm, (TR_NAME *)key);
373 size_t trp_rtable_comm_size(TRP_RTABLE *rtbl, TR_NAME *comm)
375 struct table_comm_size_cookie data={comm, rtbl, 0};
376 GHashTable *comm_tbl=trp_rtable_get_comm_table(rtbl, comm);
379 g_hash_table_foreach(comm_tbl, table_comm_size_helper, &data);
383 size_t trp_rtable_realm_size(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
385 GHashTable *realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
389 return g_hash_table_size(g_hash_table_lookup(
390 g_hash_table_lookup(rtbl, comm),
394 /* Returns an array of pointers to TRP_ROUTE, length of array in n_out.
395 * Caller must free the array (in the talloc NULL context), but must
396 * not free its contents. */
397 TRP_ROUTE **trp_rtable_get_entries(TRP_RTABLE *rtbl, size_t *n_out)
399 TRP_ROUTE **ret=NULL;
402 TRP_ROUTE **comm_entries=NULL;
406 *n_out=trp_rtable_size(rtbl);
410 ret=talloc_array(NULL, TRP_ROUTE *, *n_out);
412 tr_crit("trp_rtable_get_entries: unable to allocate return array.");
417 ii_ret=0; /* counts output entries */
418 comm=trp_rtable_get_comms(rtbl, &n_comm);
420 comm_entries=trp_rtable_get_comm_entries(rtbl, comm[n_comm], &n_entries);
422 ret[ii_ret++]=comm_entries[n_entries];
423 talloc_free(comm_entries);
427 if (ii_ret!=*n_out) {
428 tr_crit("trp_rtable_get_entries: found incorrect number of entries.");
436 /* Returns an array of pointers to TR_NAME, length of array in n_out.
437 * Caller must free the array (in the talloc NULL context). */
438 TR_NAME **trp_rtable_get_comms(TRP_RTABLE *rtbl, size_t *n_out)
440 size_t len=g_hash_table_size(rtbl); /* known comms are keys in top level hash table */
451 ret=talloc_array(NULL, TR_NAME *, len);
453 tr_crit("trp_rtable_get_comms: unable to allocate return array.");
457 comms=g_hash_table_get_keys(rtbl);
458 for (ii=0,p=comms; p!=NULL; ii++,p=g_list_next(p))
459 ret[ii]=(TR_NAME *)p->data;
467 /* Returns an array of pointers to TR_NAME, length of array in n_out.
468 * Caller must free the array (in the talloc NULL context). */
469 TR_NAME **trp_rtable_get_comm_realms(TRP_RTABLE *rtbl, TR_NAME *comm, size_t *n_out)
472 TRP_RTABLE *comm_tbl=g_hash_table_lookup(rtbl, comm);;
477 if (comm_tbl==NULL) {
481 *n_out=g_hash_table_size(comm_tbl); /* set output length */
482 ret=talloc_array(NULL, TR_NAME *, *n_out);
483 entries=g_hash_table_get_keys(comm_tbl);
484 for (ii=0,p=entries; p!=NULL; ii++,p=g_list_next(p))
485 ret[ii]=(TR_NAME *)p->data;
487 g_list_free(entries);
491 /* Get all entries in an comm. Returns an array of pointers in NULL talloc context.
492 * Caller must free this list with talloc_free, but must not free the entries in the
494 TRP_ROUTE **trp_rtable_get_comm_entries(TRP_RTABLE *rtbl, TR_NAME *comm, size_t *n_out)
497 TR_NAME **realm=NULL;
499 TRP_ROUTE **realm_entries=NULL;
501 TRP_ROUTE **ret=NULL;
504 *n_out=trp_rtable_comm_size(rtbl, comm);
508 ret=talloc_array(NULL, TRP_ROUTE *, *n_out);
510 tr_crit("trp_rtable_get_comm_entries: could not allocate return array.");
515 ii_ret=0; /* counts entries in the output array */
516 realm=trp_rtable_get_comm_realms(rtbl, comm, &n_realms);
517 for (ii=0; ii<n_realms; ii++) {
518 realm_entries=trp_rtable_get_realm_entries(rtbl, comm, realm[ii], &n_entries);
519 for (jj=0; jj<n_entries; jj++)
520 ret[ii_ret++]=realm_entries[jj];
521 talloc_free(realm_entries);
525 if (ii_ret!=*n_out) {
526 tr_crit("trp_rtable_get_comm_entries: found incorrect number of entries.");
535 /* Get all entries in an comm/realm. Returns an array of pointers in NULL talloc context.
536 * Caller must free this list with talloc_free, but must not free the entries in the
538 TRP_ROUTE **trp_rtable_get_realm_entries(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, size_t *n_out)
541 TRP_ROUTE **ret=NULL;
544 tr_debug("trp_rtable_get_realm_entries: entered.");
545 peer=trp_rtable_get_comm_realm_peers(rtbl, comm, realm, n_out);
546 ret=talloc_array(NULL, TRP_ROUTE *, *n_out);
548 tr_crit("trp_rtable_get_realm_entries: could not allocate return array.");
553 for (ii=0; ii<*n_out; ii++)
554 ret[ii]=trp_rtable_get_entry(rtbl, comm, realm, peer[ii]);
559 TR_NAME **trp_rtable_get_comm_realm_peers(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, size_t *n_out)
562 GHashTable *realm_tbl=NULL;
567 *n_out=trp_rtable_realm_size(rtbl, comm, realm);
570 realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
571 ret=talloc_array(NULL, TR_NAME *, *n_out);
573 tr_crit("trp_rtable_get_comm_realm_peers: could not allocate return array.");
577 keys=g_hash_table_get_keys(realm_tbl);
578 for (ii=0,p=keys; p!=NULL; ii++,p=g_list_next(p))
579 ret[ii]=(TR_NAME *)p->data;
584 /* Gets a single entry. Do not free it. */
585 TRP_ROUTE *trp_rtable_get_entry(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
587 GHashTable *realm_tbl=NULL;
589 realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
593 return g_hash_table_lookup(realm_tbl, peer); /* does not copy or increment ref count */
596 static char *timespec_to_str(struct timespec *ts)
601 if (localtime_r(&(ts->tv_sec), &tm)==NULL)
604 s=malloc(40); /* long enough to contain strftime result */
608 if (strftime(s, 40, "%F %T", &tm)==0) {
615 TRP_ROUTE *trp_rtable_get_selected_entry(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
619 TRP_ROUTE **entry=trp_rtable_get_realm_entries(rtbl, comm, realm, &n);
620 TRP_ROUTE *selected=NULL;
625 tr_debug("trp_rtable_get_selected_entry: looking through route table entries for realm %.*s.",
626 realm->len, realm->buf);
627 for(ii=0; ii<n; ii++) {
628 if (trp_route_is_selected(entry[ii])) {
633 tr_debug("trp_rtable_get_selected_entry: ii=%d.", ii);
639 /* Pretty print a route table entry to a newly allocated string. If sep is NULL,
640 * returns comma+space separated string. */
641 char *trp_route_to_str(TALLOC_CTX *mem_ctx, TRP_ROUTE *entry, const char *sep)
643 char *comm=tr_name_strdup(entry->comm);
644 char *realm=tr_name_strdup(entry->realm);
645 char *peer=tr_name_strdup(entry->peer);
646 char *trust_router=tr_name_strdup(entry->trust_router);
647 char *next_hop=tr_name_strdup(entry->next_hop);
648 char *expiry=timespec_to_str(entry->expiry);
654 result=talloc_asprintf(mem_ctx,
655 "%s%s%s%s%s%s%u%s%s%s%s%s%u%s%u%s%s%s%u",
662 entry->selected, sep,
675 void trp_rtable_clear_triggered(TRP_RTABLE *rtbl)
678 TRP_ROUTE **entries=trp_rtable_get_entries(rtbl, &n_entries);
682 for (ii=0; ii<n_entries; ii++)
683 trp_route_set_triggered(entries[ii], 0);
684 talloc_free(entries);
688 static int sort_tr_names_cmp(const void *a, const void *b)
690 TR_NAME **n1=(TR_NAME **)a;
691 TR_NAME **n2=(TR_NAME **)b;
692 return tr_name_cmp(*n1, *n2);
695 static void sort_tr_names(TR_NAME **names, size_t n_names)
697 qsort(names, n_names, sizeof(TR_NAME *), sort_tr_names_cmp);
700 char *trp_rtable_to_str(TALLOC_CTX *mem_ctx, TRP_RTABLE *rtbl, const char *sep, const char *lineterm)
702 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
703 TR_NAME **comms=NULL;
705 TR_NAME **realms=NULL;
707 TRP_ROUTE **entries=NULL;
709 char **tbl_strings=NULL;
710 size_t ii_tbl=0; /* counts tbl_strings */
713 size_t ii=0, jj=0, kk=0;
720 tbl_size=trp_rtable_size(rtbl);
722 result=talloc_strdup(mem_ctx, lineterm);
726 tbl_strings=talloc_array(tmp_ctx, char *, tbl_size);
727 if (tbl_strings==NULL) {
728 result=talloc_strdup(mem_ctx, "error");
732 comms=trp_rtable_get_comms(rtbl, &n_comms);
733 talloc_steal(tmp_ctx, comms);
734 sort_tr_names(comms, n_comms);
737 for (ii=0; ii<n_comms; ii++) {
738 realms=trp_rtable_get_comm_realms(rtbl, comms[ii], &n_realms);
739 talloc_steal(tmp_ctx, realms);
740 sort_tr_names(realms, n_realms);
741 for (jj=0; jj<n_realms; jj++) {
742 entries=trp_rtable_get_realm_entries(rtbl, comms[ii], realms[jj], &n_entries);
743 talloc_steal(tmp_ctx, entries);
744 for (kk=0; kk<n_entries; kk++) {
745 tbl_strings[ii_tbl]=trp_route_to_str(tmp_ctx, entries[kk], sep);
746 len+=strlen(tbl_strings[ii_tbl]);
749 talloc_free(entries);
755 /* now combine all the strings */
756 len += tbl_size*strlen(lineterm); /* space for line terminations*/
757 len += 1; /* nul terminator */
758 result=(char *)talloc_size(tmp_ctx, len);
759 for (p=result,ii=0; ii < tbl_size; ii++) {
760 p+=sprintf(p, "%s%s", tbl_strings[ii], lineterm);
762 talloc_steal(mem_ctx, result);
765 talloc_free(tmp_ctx);