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_rentry_destructor(void *obj)
16 TRP_RENTRY *entry=talloc_get_type_abort(obj, TRP_RENTRY);
18 tr_free_name(entry->apc);
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_RENTRY *trp_rentry_new(TALLOC_CTX *mem_ctx)
32 TRP_RENTRY *entry=talloc(mem_ctx, TRP_RENTRY);
36 entry->trust_router=NULL;
40 entry->expiry=talloc(entry, struct timespec);
41 if (entry->expiry==NULL) {
45 talloc_set_destructor((void *)entry, trp_rentry_destructor);
50 void trp_rentry_free(TRP_RENTRY *entry)
56 void trp_rentry_set_apc(TRP_RENTRY *entry, TR_NAME *apc)
61 TR_NAME *trp_rentry_get_apc(TRP_RENTRY *entry)
66 void trp_rentry_set_realm(TRP_RENTRY *entry, TR_NAME *realm)
71 TR_NAME *trp_rentry_get_realm(TRP_RENTRY *entry)
76 void trp_rentry_set_trust_router(TRP_RENTRY *entry, TR_NAME *tr)
78 entry->trust_router=tr;
81 TR_NAME *trp_rentry_get_trust_router(TRP_RENTRY *entry)
83 return entry->trust_router;
86 void trp_rentry_set_peer(TRP_RENTRY *entry, TR_NAME *peer)
91 TR_NAME *trp_rentry_get_peer(TRP_RENTRY *entry)
96 void trp_rentry_set_metric(TRP_RENTRY *entry, unsigned int metric)
101 unsigned int trp_rentry_get_metric(TRP_RENTRY *entry)
103 return entry->metric;
106 void trp_rentry_set_next_hop(TRP_RENTRY *entry, TR_NAME *next_hop)
108 entry->next_hop=next_hop;
111 TR_NAME *trp_rentry_get_next_hop(TRP_RENTRY *entry)
113 return entry->next_hop;
116 void trp_rentry_set_selected(TRP_RENTRY *entry, int sel)
121 int trp_rentry_get_selected(TRP_RENTRY *entry)
123 return entry->selected;
126 /* copies incoming value, does not assume responsibility for freeing */
127 void trp_rentry_set_expiry(TRP_RENTRY *entry, struct timespec *exp)
129 entry->expiry->tv_sec=exp->tv_sec;
130 entry->expiry->tv_nsec=exp->tv_nsec;
133 struct timespec *trp_rentry_get_expiry(TRP_RENTRY *entry)
135 return entry->expiry;
139 /* result must be freed with g_free */
140 static gchar *tr_name_to_g_str(const TR_NAME *n)
142 gchar *s=g_strndup(n->buf, n->len);
146 /* hash function for TR_NAME keys */
147 static guint trp_tr_name_hash(gconstpointer key)
149 const TR_NAME *name=(TR_NAME *)key;
150 gchar *s=tr_name_to_g_str(name);
151 guint hash=g_str_hash(s);
156 /* hash equality function for TR_NAME keys */
157 static gboolean trp_tr_name_equal(gconstpointer key1, gconstpointer key2)
159 const TR_NAME *n1=(TR_NAME *)key1;
160 const TR_NAME *n2=(TR_NAME *)key2;
161 gchar *s1=tr_name_to_g_str(n1);
162 gchar *s2=tr_name_to_g_str(n2);
163 gboolean equal=g_str_equal(s1, s2);
169 /* free a value to the top level rtable (a hash of all entries in the apc) */
170 static void trp_rtable_destroy_table(gpointer data)
172 g_hash_table_destroy(data);
175 static void trp_rtable_destroy_rentry(gpointer data)
177 trp_rentry_free(data);
180 static void trp_rtable_destroy_tr_name(gpointer data)
185 TRP_RTABLE *trp_rtable_new(void)
187 GHashTable *new=g_hash_table_new_full(trp_tr_name_hash,
189 trp_rtable_destroy_tr_name,
190 trp_rtable_destroy_table);
194 void trp_rtable_free(TRP_RTABLE *rtbl)
196 g_hash_table_destroy(rtbl);
199 static GHashTable *trp_rtbl_get_or_add_table(GHashTable *tbl, TR_NAME *key, GDestroyNotify destroy)
201 GHashTable *val_tbl=NULL;
203 val_tbl=g_hash_table_lookup(tbl, key);
205 val_tbl=g_hash_table_new_full(trp_tr_name_hash,
207 trp_rtable_destroy_tr_name,
209 g_hash_table_insert(tbl, tr_dup_name(key), val_tbl);
214 void trp_rtable_add(TRP_RTABLE *rtbl, TRP_RENTRY *entry)
216 GHashTable *apc_tbl=NULL;
217 GHashTable *realm_tbl=NULL;
219 apc_tbl=trp_rtbl_get_or_add_table(rtbl, entry->apc, trp_rtable_destroy_table);
220 realm_tbl=trp_rtbl_get_or_add_table(apc_tbl, entry->realm, trp_rtable_destroy_rentry);
221 g_hash_table_insert(realm_tbl, tr_dup_name(entry->peer), entry); /* destroys and replaces a duplicate */
224 /* note: the entry pointer passed in is invalid after calling this because the entry is freed */
225 void trp_rtable_remove(TRP_RTABLE *rtbl, TRP_RENTRY *entry)
227 GHashTable *apc_tbl=NULL;
228 GHashTable *realm_tbl=NULL;
230 apc_tbl=g_hash_table_lookup(rtbl, entry->apc);
234 realm_tbl=g_hash_table_lookup(apc_tbl, entry->realm);
238 /* remove the element */
239 g_hash_table_remove(realm_tbl, entry->peer);
240 /* if that was the last entry in the realm, remove the realm table */
241 if (g_hash_table_size(realm_tbl)==0)
242 g_hash_table_remove(apc_tbl, entry->realm);
243 /* if that was the last realm in the apc, remove the apc table */
244 if (g_hash_table_size(apc_tbl)==0)
245 g_hash_table_remove(rtbl, entry->apc);
248 /* gets the actual hash table, for internal use only */
249 static GHashTable *trp_rtable_get_apc_table(TRP_RTABLE *rtbl, TR_NAME *apc)
251 return g_hash_table_lookup(rtbl, apc);
254 /* gets the actual hash table, for internal use only */
255 static GHashTable *trp_rtable_get_realm_table(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm)
257 GHashTable *apc_tbl=trp_rtable_get_apc_table(rtbl, apc);
261 return g_hash_table_lookup(apc_tbl, realm);
264 struct table_size_cookie {
268 static void trp_rtable_size_helper(gpointer key, gpointer value, gpointer user_data)
270 struct table_size_cookie *data=(struct table_size_cookie *)user_data;
271 data->size += trp_rtable_apc_size(data->rtbl, (TR_NAME *)key);
273 size_t trp_rtable_size(TRP_RTABLE *rtbl)
275 struct table_size_cookie data={rtbl, 0};
276 g_hash_table_foreach(rtbl, trp_rtable_size_helper, &data);
280 struct table_apc_size_cookie {
285 static void table_apc_size_helper(gpointer key, gpointer value, gpointer user_data)
287 struct table_apc_size_cookie *data=(struct table_apc_size_cookie *)user_data;
288 data->size += trp_rtable_realm_size(data->rtbl, data->apc, (TR_NAME *)key);
290 size_t trp_rtable_apc_size(TRP_RTABLE *rtbl, TR_NAME *apc)
292 struct table_apc_size_cookie data={apc, rtbl, 0};
293 GHashTable *apc_tbl=trp_rtable_get_apc_table(rtbl, apc);
296 g_hash_table_foreach(apc_tbl, table_apc_size_helper, &data);
300 size_t trp_rtable_realm_size(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm)
302 GHashTable *realm_tbl=trp_rtable_get_realm_table(rtbl, apc, realm);
306 return g_hash_table_size(g_hash_table_lookup(
307 g_hash_table_lookup(rtbl, apc),
311 /* Returns an array of pointers to TRP_RENTRY, length of array in n_out.
312 * Caller must free the array (in the talloc NULL context), but must
313 * not free its contents. */
314 TRP_RENTRY **trp_rtable_get_entries(TRP_RTABLE *rtbl, size_t *n_out)
316 TRP_RENTRY **ret=NULL;
319 TRP_RENTRY **apc_entries=NULL;
323 *n_out=trp_rtable_size(rtbl);
327 ret=talloc_array(NULL, TRP_RENTRY *, *n_out);
329 tr_crit("trp_rtable_get_entries: unable to allocate return array.");
334 ii_ret=0; /* counts output entries */
335 apc=trp_rtable_get_apcs(rtbl, &n_apc);
337 apc_entries=trp_rtable_get_apc_entries(rtbl, apc[n_apc], &n_entries);
339 ret[ii_ret++]=apc_entries[n_entries];
340 talloc_free(apc_entries);
344 if (ii_ret!=*n_out) {
345 tr_crit("trp_rtable_get_entries: found incorrect number of entries.");
353 /* Returns an array of pointers to TR_NAME, length of array in n_out.
354 * Caller must free the array (in the talloc NULL context). */
355 TR_NAME **trp_rtable_get_apcs(TRP_RTABLE *rtbl, size_t *n_out)
357 size_t len=g_hash_table_size(rtbl); /* known apcs are keys in top level hash table */
368 ret=talloc_array(NULL, TR_NAME *, len);
370 tr_crit("trp_rtable_get_apcs: unable to allocate return array.");
374 apcs=g_hash_table_get_keys(rtbl);
375 for (ii=0,p=apcs; p!=NULL; ii++,p=g_list_next(p))
376 ret[ii]=(TR_NAME *)p->data;
384 /* Returns an array of pointers to TR_NAME, length of array in n_out.
385 * Caller must free the array (in the talloc NULL context). */
386 TR_NAME **trp_rtable_get_apc_realms(TRP_RTABLE *rtbl, TR_NAME *apc, size_t *n_out)
389 TRP_RTABLE *apc_tbl=g_hash_table_lookup(rtbl, apc);;
398 *n_out=g_hash_table_size(apc_tbl); /* set output length */
399 ret=talloc_array(NULL, TR_NAME *, *n_out);
400 entries=g_hash_table_get_keys(apc_tbl);
401 for (ii=0,p=entries; p!=NULL; ii++,p=g_list_next(p))
402 ret[ii]=(TR_NAME *)p->data;
404 g_list_free(entries);
408 /* Get all entries in an apc. Returns an array of pointers in NULL talloc context.
409 * Caller must free this list with talloc_free, but must not free the entries in the
411 TRP_RENTRY **trp_rtable_get_apc_entries(TRP_RTABLE *rtbl, TR_NAME *apc, size_t *n_out)
414 TR_NAME **realm=NULL;
416 TRP_RENTRY **realm_entries=NULL;
418 TRP_RENTRY **ret=NULL;
421 *n_out=trp_rtable_apc_size(rtbl, apc);
425 ret=talloc_array(NULL, TRP_RENTRY *, *n_out);
427 tr_crit("trp_rtable_get_apc_entries: could not allocate return array.");
432 ii_ret=0; /* counts entries in the output array */
433 realm=trp_rtable_get_apc_realms(rtbl, apc, &n_realms);
434 for (ii=0; ii<n_realms; ii++) {
435 realm_entries=trp_rtable_get_realm_entries(rtbl, apc, realm[ii], &n_entries);
436 for (jj=0; jj<n_entries; jj++)
437 ret[ii_ret++]=realm_entries[jj];
438 talloc_free(realm_entries);
442 if (ii_ret!=*n_out) {
443 tr_crit("trp_rtable_get_apc_entries: found incorrect number of entries.");
452 /* Get all entries in an apc/realm. Returns an array of pointers in NULL talloc context.
453 * Caller must free this list with talloc_free, but must not free the entries in the
455 TRP_RENTRY **trp_rtable_get_realm_entries(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, size_t *n_out)
458 TRP_RENTRY **ret=NULL;
461 peer=trp_rtable_get_apc_realm_peers(rtbl, apc, realm, n_out);
462 ret=talloc_array(NULL, TRP_RENTRY *, *n_out);
464 tr_crit("trp_rtable_get_realm_entries: could not allocate return array.");
469 for (ii=0; ii<*n_out; ii++)
470 ret[ii]=trp_rtable_get_entry(rtbl, apc, realm, peer[ii]);
475 TR_NAME **trp_rtable_get_apc_realm_peers(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, size_t *n_out)
478 GHashTable *realm_tbl=NULL;
483 *n_out=trp_rtable_realm_size(rtbl, apc, realm);
486 realm_tbl=trp_rtable_get_realm_table(rtbl, apc, realm);
487 ret=talloc_array(NULL, TR_NAME *, *n_out);
489 tr_crit("trp_rtable_get_apc_realm_peers: could not allocate return array.");
493 keys=g_hash_table_get_keys(realm_tbl);
494 for (ii=0,p=keys; p!=NULL; ii++,p=g_list_next(p))
495 ret[ii]=(TR_NAME *)p->data;
500 /* Gets a single entry. Do not free it. */
501 TRP_RENTRY *trp_rtable_get_entry(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, TR_NAME *peer)
503 GHashTable *realm_tbl=NULL;
505 realm_tbl=trp_rtable_get_realm_table(rtbl, apc, realm);
508 return g_hash_table_lookup(realm_tbl, peer); /* does not copy or increment ref count */
511 static char *timespec_to_str(struct timespec *ts)
516 if (localtime_r(&(ts->tv_sec), &tm)==NULL)
519 s=malloc(40); /* long enough to contain strftime result */
523 if (strftime(s, 40, "%F %T", &tm)==0) {
530 TRP_RENTRY *trp_rtable_get_selected_entry(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm)
533 TRP_RENTRY **entry=trp_rtable_get_realm_entries(rtbl, apc, realm, &n);
534 TRP_RENTRY *selected=NULL;
539 while(n-- && !trp_rentry_get_selected(entry[n])) { }
545 /* Pretty print a route table entry to a newly allocated string. If sep is NULL,
546 * returns comma+space separated string. */
547 char *trp_rentry_to_str(TALLOC_CTX *mem_ctx, TRP_RENTRY *entry, const char *sep)
549 char *apc=tr_name_strdup(entry->apc);
550 char *realm=tr_name_strdup(entry->realm);
551 char *peer=tr_name_strdup(entry->peer);
552 char *trust_router=tr_name_strdup(entry->trust_router);
553 char *next_hop=tr_name_strdup(entry->next_hop);
554 char *expiry=timespec_to_str(entry->expiry);
560 result=talloc_asprintf(mem_ctx,
561 "%s%s%s%s%s%s%u%s%s%s%s%s%d%s%s",
568 entry->selected, sep,
579 static int sort_tr_names_cmp(const void *a, const void *b)
581 TR_NAME **n1=(TR_NAME **)a;
582 TR_NAME **n2=(TR_NAME **)b;
583 return tr_name_cmp(*n1, *n2);
586 static void sort_tr_names(TR_NAME **names, size_t n_names)
588 qsort(names, n_names, sizeof(TR_NAME *), sort_tr_names_cmp);
591 char *trp_rtable_to_str(TALLOC_CTX *mem_ctx, TRP_RTABLE *rtbl, const char *sep, const char *lineterm)
593 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
596 TR_NAME **realms=NULL;
598 TRP_RENTRY **entries=NULL;
600 char **tbl_strings=NULL;
601 size_t ii_tbl=0; /* counts tbl_strings */
604 size_t ii=0, jj=0, kk=0;
611 tbl_size=trp_rtable_size(rtbl);
613 result=talloc_strdup(mem_ctx, lineterm);
617 tbl_strings=talloc_array(tmp_ctx, char *, tbl_size);
618 if (tbl_strings==NULL) {
619 result=talloc_strdup(mem_ctx, "error");
623 apcs=trp_rtable_get_apcs(rtbl, &n_apcs);
624 talloc_steal(tmp_ctx, apcs);
625 sort_tr_names(apcs, n_apcs);
628 for (ii=0; ii<n_apcs; ii++) {
629 realms=trp_rtable_get_apc_realms(rtbl, apcs[ii], &n_realms);
630 talloc_steal(tmp_ctx, realms);
631 sort_tr_names(realms, n_realms);
632 for (jj=0; jj<n_realms; jj++) {
633 entries=trp_rtable_get_realm_entries(rtbl, apcs[ii], realms[jj], &n_entries);
634 talloc_steal(tmp_ctx, entries);
635 for (kk=0; kk<n_entries; kk++) {
636 tbl_strings[ii_tbl]=trp_rentry_to_str(tmp_ctx, entries[kk], sep);
637 len+=strlen(tbl_strings[ii_tbl]);
640 talloc_free(entries);
646 /* now combine all the strings */
647 len += tbl_size*strlen(lineterm); /* space for line terminations*/
648 len += 1; /* nul terminator */
649 result=(char *)talloc_size(tmp_ctx, len);
650 for (p=result,ii=0; ii < tbl_size; ii++) {
651 p+=sprintf(p, "%s%s", tbl_strings[ii], lineterm);
653 talloc_steal(mem_ctx, result);
656 talloc_free(tmp_ctx);