7 #include <trust_router/tr_name.h>
8 #include <trp_internal.h>
9 #include <trp_rtable.h>
12 /* Note: be careful mixing talloc with glib. */
14 static int trp_route_destructor(void *obj)
16 TRP_ROUTE *entry=talloc_get_type_abort(obj, TRP_ROUTE);
17 if (entry->comm!=NULL)
18 tr_free_name(entry->comm);
19 if (entry->realm!=NULL)
20 tr_free_name(entry->realm);
21 if (entry->trust_router!=NULL)
22 tr_free_name(entry->trust_router);
23 if (entry->peer!=NULL)
24 tr_free_name(entry->peer);
25 if (entry->next_hop!=NULL)
26 tr_free_name(entry->next_hop);
30 TRP_ROUTE *trp_route_new(TALLOC_CTX *mem_ctx)
32 TRP_ROUTE *entry=talloc(mem_ctx, TRP_ROUTE);
36 entry->trust_router=NULL;
41 entry->expiry=talloc(entry, struct timespec);
42 if (entry->expiry==NULL) {
46 *(entry->expiry)=(struct timespec){0,0};
49 talloc_set_destructor((void *)entry, trp_route_destructor);
54 void trp_route_free(TRP_ROUTE *entry)
60 void trp_route_set_comm(TRP_ROUTE *entry, TR_NAME *comm)
62 if (entry->comm!=NULL)
63 tr_free_name(entry->comm);
67 TR_NAME *trp_route_get_comm(TRP_ROUTE *entry)
72 TR_NAME *trp_route_dup_comm(TRP_ROUTE *entry)
74 return tr_dup_name(trp_route_get_comm(entry));
77 void trp_route_set_realm(TRP_ROUTE *entry, TR_NAME *realm)
79 if (entry->realm!=NULL)
80 tr_free_name(entry->realm);
84 TR_NAME *trp_route_get_realm(TRP_ROUTE *entry)
89 TR_NAME *trp_route_dup_realm(TRP_ROUTE *entry)
91 return tr_dup_name(trp_route_get_realm(entry));
94 void trp_route_set_trust_router(TRP_ROUTE *entry, TR_NAME *tr)
96 if (entry->trust_router!=NULL)
97 tr_free_name(entry->trust_router);
98 entry->trust_router=tr;
101 TR_NAME *trp_route_get_trust_router(TRP_ROUTE *entry)
103 return entry->trust_router;
106 TR_NAME *trp_route_dup_trust_router(TRP_ROUTE *entry)
108 return tr_dup_name(trp_route_get_trust_router(entry));
111 void trp_route_set_peer(TRP_ROUTE *entry, TR_NAME *peer)
113 if (entry->peer!=NULL)
114 tr_free_name(entry->peer);
118 TR_NAME *trp_route_get_peer(TRP_ROUTE *entry)
123 TR_NAME *trp_route_dup_peer(TRP_ROUTE *entry)
125 return tr_dup_name(trp_route_get_peer(entry));
128 void trp_route_set_metric(TRP_ROUTE *entry, unsigned int metric)
130 entry->metric=metric;
133 unsigned int trp_route_get_metric(TRP_ROUTE *entry)
135 return entry->metric;
138 void trp_route_set_next_hop(TRP_ROUTE *entry, TR_NAME *next_hop)
140 if (entry->next_hop!=NULL)
141 tr_free_name(entry->next_hop);
142 entry->next_hop=next_hop;
145 TR_NAME *trp_route_get_next_hop(TRP_ROUTE *entry)
147 return entry->next_hop;
150 TR_NAME *trp_route_dup_next_hop(TRP_ROUTE *entry)
152 return tr_dup_name(trp_route_get_next_hop(entry));
155 void trp_route_set_selected(TRP_ROUTE *entry, int sel)
160 int trp_route_is_selected(TRP_ROUTE *entry)
162 return entry->selected;
165 void trp_route_set_interval(TRP_ROUTE *entry, int interval)
167 entry->interval=interval;
170 int trp_route_get_interval(TRP_ROUTE *entry)
172 return entry->interval;
175 /* copies incoming value, does not assume responsibility for freeing */
176 void trp_route_set_expiry(TRP_ROUTE *entry, struct timespec *exp)
178 entry->expiry->tv_sec=exp->tv_sec;
179 entry->expiry->tv_nsec=exp->tv_nsec;
182 struct timespec *trp_route_get_expiry(TRP_ROUTE *entry)
184 return entry->expiry;
187 void trp_route_set_local(TRP_ROUTE *entry, int local)
192 int trp_route_is_local(TRP_ROUTE *entry)
197 void trp_route_set_triggered(TRP_ROUTE *entry, int trig)
199 entry->triggered=trig;
202 int trp_route_is_triggered(TRP_ROUTE *entry)
204 return entry->triggered;
208 /* result must be freed with g_free */
209 static gchar *tr_name_to_g_str(const TR_NAME *n)
211 gchar *s=g_strndup(n->buf, n->len);
213 tr_debug("tr_name_to_g_str: allocation failure.");
217 /* hash function for TR_NAME keys */
218 static guint trp_tr_name_hash(gconstpointer key)
220 const TR_NAME *name=(TR_NAME *)key;
221 gchar *s=tr_name_to_g_str(name);
222 guint hash=g_str_hash(s);
227 /* hash equality function for TR_NAME keys */
228 static gboolean trp_tr_name_equal(gconstpointer key1, gconstpointer key2)
230 const TR_NAME *n1=(TR_NAME *)key1;
231 const TR_NAME *n2=(TR_NAME *)key2;
232 gchar *s1=tr_name_to_g_str(n1);
233 gchar *s2=tr_name_to_g_str(n2);
234 gboolean equal=g_str_equal(s1, s2);
240 /* free a value to the top level rtable (a hash of all entries in the comm) */
241 static void trp_rtable_destroy_table(gpointer data)
243 g_hash_table_destroy(data);
246 static void trp_rtable_destroy_rentry(gpointer data)
248 trp_route_free(data);
251 static void trp_rtable_destroy_tr_name(gpointer data)
256 TRP_RTABLE *trp_rtable_new(void)
258 GHashTable *new=g_hash_table_new_full(trp_tr_name_hash,
260 trp_rtable_destroy_tr_name,
261 trp_rtable_destroy_table);
265 void trp_rtable_free(TRP_RTABLE *rtbl)
267 g_hash_table_destroy(rtbl);
270 static GHashTable *trp_rtbl_get_or_add_table(GHashTable *tbl, TR_NAME *key, GDestroyNotify destroy)
272 GHashTable *val_tbl=NULL;
274 val_tbl=g_hash_table_lookup(tbl, key);
276 val_tbl=g_hash_table_new_full(trp_tr_name_hash,
278 trp_rtable_destroy_tr_name,
280 g_hash_table_insert(tbl, tr_dup_name(key), val_tbl);
285 void trp_rtable_add(TRP_RTABLE *rtbl, TRP_ROUTE *entry)
287 GHashTable *comm_tbl=NULL;
288 GHashTable *realm_tbl=NULL;
290 comm_tbl=trp_rtbl_get_or_add_table(rtbl, entry->comm, trp_rtable_destroy_table);
291 realm_tbl=trp_rtbl_get_or_add_table(comm_tbl, entry->realm, trp_rtable_destroy_rentry);
292 g_hash_table_insert(realm_tbl, tr_dup_name(entry->peer), entry); /* destroys and replaces a duplicate */
293 /* the route entry should not belong to any context, we will manage it ourselves */
294 talloc_steal(NULL, entry);
297 /* note: the entry pointer passed in is invalid after calling this because the entry is freed */
298 void trp_rtable_remove(TRP_RTABLE *rtbl, TRP_ROUTE *entry)
300 GHashTable *comm_tbl=NULL;
301 GHashTable *realm_tbl=NULL;
303 comm_tbl=g_hash_table_lookup(rtbl, entry->comm);
307 realm_tbl=g_hash_table_lookup(comm_tbl, entry->realm);
311 /* remove the element */
312 g_hash_table_remove(realm_tbl, entry->peer);
313 /* if that was the last entry in the realm, remove the realm table */
314 if (g_hash_table_size(realm_tbl)==0)
315 g_hash_table_remove(comm_tbl, entry->realm);
316 /* if that was the last realm in the comm, remove the comm table */
317 if (g_hash_table_size(comm_tbl)==0)
318 g_hash_table_remove(rtbl, entry->comm);
321 void trp_rtable_clear(TRP_RTABLE *rtbl)
323 g_hash_table_remove_all(rtbl); /* destructors should do all the cleanup */
326 /* gets the actual hash table, for internal use only */
327 static GHashTable *trp_rtable_get_comm_table(TRP_RTABLE *rtbl, TR_NAME *comm)
329 return g_hash_table_lookup(rtbl, comm);
332 /* gets the actual hash table, for internal use only */
333 static GHashTable *trp_rtable_get_realm_table(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
335 GHashTable *comm_tbl=trp_rtable_get_comm_table(rtbl, comm);
339 return g_hash_table_lookup(comm_tbl, realm);
342 struct table_size_cookie {
346 static void trp_rtable_size_helper(gpointer key, gpointer value, gpointer user_data)
348 struct table_size_cookie *data=(struct table_size_cookie *)user_data;
349 data->size += trp_rtable_comm_size(data->rtbl, (TR_NAME *)key);
351 size_t trp_rtable_size(TRP_RTABLE *rtbl)
353 struct table_size_cookie data={rtbl, 0};
354 g_hash_table_foreach(rtbl, trp_rtable_size_helper, &data);
358 struct table_comm_size_cookie {
363 static void table_comm_size_helper(gpointer key, gpointer value, gpointer user_data)
365 struct table_comm_size_cookie *data=(struct table_comm_size_cookie *)user_data;
366 data->size += trp_rtable_realm_size(data->rtbl, data->comm, (TR_NAME *)key);
368 size_t trp_rtable_comm_size(TRP_RTABLE *rtbl, TR_NAME *comm)
370 struct table_comm_size_cookie data={comm, rtbl, 0};
371 GHashTable *comm_tbl=trp_rtable_get_comm_table(rtbl, comm);
374 g_hash_table_foreach(comm_tbl, table_comm_size_helper, &data);
378 size_t trp_rtable_realm_size(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
380 GHashTable *realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
384 return g_hash_table_size(g_hash_table_lookup(
385 g_hash_table_lookup(rtbl, comm),
389 /* Returns an array of pointers to TRP_ROUTE, length of array in n_out.
390 * Caller must free the array (in the talloc NULL context), but must
391 * not free its contents. */
392 TRP_ROUTE **trp_rtable_get_entries(TRP_RTABLE *rtbl, size_t *n_out)
394 TRP_ROUTE **ret=NULL;
397 TRP_ROUTE **comm_entries=NULL;
401 *n_out=trp_rtable_size(rtbl);
405 ret=talloc_array(NULL, TRP_ROUTE *, *n_out);
407 tr_crit("trp_rtable_get_entries: unable to allocate return array.");
412 ii_ret=0; /* counts output entries */
413 comm=trp_rtable_get_comms(rtbl, &n_comm);
415 comm_entries=trp_rtable_get_comm_entries(rtbl, comm[n_comm], &n_entries);
417 ret[ii_ret++]=comm_entries[n_entries];
418 talloc_free(comm_entries);
422 if (ii_ret!=*n_out) {
423 tr_crit("trp_rtable_get_entries: found incorrect number of entries.");
431 /* Returns an array of pointers to TR_NAME, length of array in n_out.
432 * Caller must free the array (in the talloc NULL context). */
433 TR_NAME **trp_rtable_get_comms(TRP_RTABLE *rtbl, size_t *n_out)
435 size_t len=g_hash_table_size(rtbl); /* known comms are keys in top level hash table */
446 ret=talloc_array(NULL, TR_NAME *, len);
448 tr_crit("trp_rtable_get_comms: unable to allocate return array.");
452 comms=g_hash_table_get_keys(rtbl);
453 for (ii=0,p=comms; p!=NULL; ii++,p=g_list_next(p))
454 ret[ii]=(TR_NAME *)p->data;
462 /* Returns an array of pointers to TR_NAME, length of array in n_out.
463 * Caller must free the array (in the talloc NULL context). */
464 TR_NAME **trp_rtable_get_comm_realms(TRP_RTABLE *rtbl, TR_NAME *comm, size_t *n_out)
467 TRP_RTABLE *comm_tbl=g_hash_table_lookup(rtbl, comm);;
472 if (comm_tbl==NULL) {
476 *n_out=g_hash_table_size(comm_tbl); /* set output length */
477 ret=talloc_array(NULL, TR_NAME *, *n_out);
478 entries=g_hash_table_get_keys(comm_tbl);
479 for (ii=0,p=entries; p!=NULL; ii++,p=g_list_next(p))
480 ret[ii]=(TR_NAME *)p->data;
482 g_list_free(entries);
486 /* Get all entries in an comm. Returns an array of pointers in NULL talloc context.
487 * Caller must free this list with talloc_free, but must not free the entries in the
489 TRP_ROUTE **trp_rtable_get_comm_entries(TRP_RTABLE *rtbl, TR_NAME *comm, size_t *n_out)
492 TR_NAME **realm=NULL;
494 TRP_ROUTE **realm_entries=NULL;
496 TRP_ROUTE **ret=NULL;
499 *n_out=trp_rtable_comm_size(rtbl, comm);
503 ret=talloc_array(NULL, TRP_ROUTE *, *n_out);
505 tr_crit("trp_rtable_get_comm_entries: could not allocate return array.");
510 ii_ret=0; /* counts entries in the output array */
511 realm=trp_rtable_get_comm_realms(rtbl, comm, &n_realms);
512 for (ii=0; ii<n_realms; ii++) {
513 realm_entries=trp_rtable_get_realm_entries(rtbl, comm, realm[ii], &n_entries);
514 for (jj=0; jj<n_entries; jj++)
515 ret[ii_ret++]=realm_entries[jj];
516 talloc_free(realm_entries);
520 if (ii_ret!=*n_out) {
521 tr_crit("trp_rtable_get_comm_entries: found incorrect number of entries.");
530 /* Get all entries in an comm/realm. 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_realm_entries(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, size_t *n_out)
536 TRP_ROUTE **ret=NULL;
539 tr_debug("trp_rtable_get_realm_entries: entered.");
540 peer=trp_rtable_get_comm_realm_peers(rtbl, comm, realm, n_out);
541 ret=talloc_array(NULL, TRP_ROUTE *, *n_out);
543 tr_crit("trp_rtable_get_realm_entries: could not allocate return array.");
548 for (ii=0; ii<*n_out; ii++)
549 ret[ii]=trp_rtable_get_entry(rtbl, comm, realm, peer[ii]);
554 TR_NAME **trp_rtable_get_comm_realm_peers(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, size_t *n_out)
557 GHashTable *realm_tbl=NULL;
562 *n_out=trp_rtable_realm_size(rtbl, comm, realm);
565 realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
566 ret=talloc_array(NULL, TR_NAME *, *n_out);
568 tr_crit("trp_rtable_get_comm_realm_peers: could not allocate return array.");
572 keys=g_hash_table_get_keys(realm_tbl);
573 for (ii=0,p=keys; p!=NULL; ii++,p=g_list_next(p))
574 ret[ii]=(TR_NAME *)p->data;
579 /* Gets a single entry. Do not free it. */
580 TRP_ROUTE *trp_rtable_get_entry(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
582 GHashTable *realm_tbl=NULL;
584 realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
588 return g_hash_table_lookup(realm_tbl, peer); /* does not copy or increment ref count */
591 static char *timespec_to_str(struct timespec *ts)
596 if (localtime_r(&(ts->tv_sec), &tm)==NULL)
599 s=malloc(40); /* long enough to contain strftime result */
603 if (strftime(s, 40, "%F %T", &tm)==0) {
610 TRP_ROUTE *trp_rtable_get_selected_entry(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
613 TRP_ROUTE **entry=trp_rtable_get_realm_entries(rtbl, comm, realm, &n);
614 TRP_ROUTE *selected=NULL;
619 tr_debug("trp_rtable_get_selected_entry: looking through route table entries for realm %.*s.",
620 realm->len, realm->buf);
621 while(n-- && !trp_route_is_selected(entry[n])) { }
622 tr_debug("trp_rtable_get_selected_entry: n=%d.", n);
631 /* Pretty print a route table entry to a newly allocated string. If sep is NULL,
632 * returns comma+space separated string. */
633 char *trp_route_to_str(TALLOC_CTX *mem_ctx, TRP_ROUTE *entry, const char *sep)
635 char *comm=tr_name_strdup(entry->comm);
636 char *realm=tr_name_strdup(entry->realm);
637 char *peer=tr_name_strdup(entry->peer);
638 char *trust_router=tr_name_strdup(entry->trust_router);
639 char *next_hop=tr_name_strdup(entry->next_hop);
640 char *expiry=timespec_to_str(entry->expiry);
646 result=talloc_asprintf(mem_ctx,
647 "%s%s%s%s%s%s%u%s%s%s%s%s%u%s%u%s%s%s%u",
654 entry->selected, sep,
667 void trp_rtable_clear_triggered(TRP_RTABLE *rtbl)
670 TRP_ROUTE **entries=trp_rtable_get_entries(rtbl, &n_entries);
674 for (ii=0; ii<n_entries; ii++)
675 trp_route_set_triggered(entries[ii], 0);
676 talloc_free(entries);
680 static int sort_tr_names_cmp(const void *a, const void *b)
682 TR_NAME **n1=(TR_NAME **)a;
683 TR_NAME **n2=(TR_NAME **)b;
684 return tr_name_cmp(*n1, *n2);
687 static void sort_tr_names(TR_NAME **names, size_t n_names)
689 qsort(names, n_names, sizeof(TR_NAME *), sort_tr_names_cmp);
692 char *trp_rtable_to_str(TALLOC_CTX *mem_ctx, TRP_RTABLE *rtbl, const char *sep, const char *lineterm)
694 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
695 TR_NAME **comms=NULL;
697 TR_NAME **realms=NULL;
699 TRP_ROUTE **entries=NULL;
701 char **tbl_strings=NULL;
702 size_t ii_tbl=0; /* counts tbl_strings */
705 size_t ii=0, jj=0, kk=0;
712 tbl_size=trp_rtable_size(rtbl);
714 result=talloc_strdup(mem_ctx, lineterm);
718 tbl_strings=talloc_array(tmp_ctx, char *, tbl_size);
719 if (tbl_strings==NULL) {
720 result=talloc_strdup(mem_ctx, "error");
724 comms=trp_rtable_get_comms(rtbl, &n_comms);
725 talloc_steal(tmp_ctx, comms);
726 sort_tr_names(comms, n_comms);
729 for (ii=0; ii<n_comms; ii++) {
730 realms=trp_rtable_get_comm_realms(rtbl, comms[ii], &n_realms);
731 talloc_steal(tmp_ctx, realms);
732 sort_tr_names(realms, n_realms);
733 for (jj=0; jj<n_realms; jj++) {
734 entries=trp_rtable_get_realm_entries(rtbl, comms[ii], realms[jj], &n_entries);
735 talloc_steal(tmp_ctx, entries);
736 for (kk=0; kk<n_entries; kk++) {
737 tbl_strings[ii_tbl]=trp_route_to_str(tmp_ctx, entries[kk], sep);
738 len+=strlen(tbl_strings[ii_tbl]);
741 talloc_free(entries);
747 /* now combine all the strings */
748 len += tbl_size*strlen(lineterm); /* space for line terminations*/
749 len += 1; /* nul terminator */
750 result=(char *)talloc_size(tmp_ctx, len);
751 for (p=result,ii=0; ii < tbl_size; ii++) {
752 p+=sprintf(p, "%s%s", tbl_strings[ii], lineterm);
754 talloc_steal(mem_ctx, result);
757 talloc_free(tmp_ctx);