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;
41 entry->expiry=talloc(entry, struct timespec);
42 if (entry->expiry==NULL) {
46 talloc_set_destructor((void *)entry, trp_rentry_destructor);
51 void trp_rentry_free(TRP_RENTRY *entry)
57 void trp_rentry_set_apc(TRP_RENTRY *entry, TR_NAME *apc)
62 TR_NAME *trp_rentry_get_apc(TRP_RENTRY *entry)
67 TR_NAME *trp_rentry_dup_apc(TRP_RENTRY *entry)
69 return tr_dup_name(trp_rentry_get_apc(entry));
72 void trp_rentry_set_realm(TRP_RENTRY *entry, TR_NAME *realm)
77 TR_NAME *trp_rentry_get_realm(TRP_RENTRY *entry)
82 TR_NAME *trp_rentry_dup_realm(TRP_RENTRY *entry)
84 return tr_dup_name(trp_rentry_get_realm(entry));
87 void trp_rentry_set_trust_router(TRP_RENTRY *entry, TR_NAME *tr)
89 entry->trust_router=tr;
92 TR_NAME *trp_rentry_get_trust_router(TRP_RENTRY *entry)
94 return entry->trust_router;
97 TR_NAME *trp_rentry_dup_trust_router(TRP_RENTRY *entry)
99 return tr_dup_name(trp_rentry_get_trust_router(entry));
102 void trp_rentry_set_peer(TRP_RENTRY *entry, TR_NAME *peer)
107 TR_NAME *trp_rentry_get_peer(TRP_RENTRY *entry)
112 TR_NAME *trp_rentry_dup_peer(TRP_RENTRY *entry)
114 return tr_dup_name(trp_rentry_get_peer(entry));
117 void trp_rentry_set_metric(TRP_RENTRY *entry, unsigned int metric)
119 entry->metric=metric;
122 unsigned int trp_rentry_get_metric(TRP_RENTRY *entry)
124 return entry->metric;
127 void trp_rentry_set_next_hop(TRP_RENTRY *entry, TR_NAME *next_hop)
129 entry->next_hop=next_hop;
132 TR_NAME *trp_rentry_get_next_hop(TRP_RENTRY *entry)
134 return entry->next_hop;
137 TR_NAME *trp_rentry_dup_next_hop(TRP_RENTRY *entry)
139 return tr_dup_name(trp_rentry_get_next_hop(entry));
142 void trp_rentry_set_selected(TRP_RENTRY *entry, int sel)
147 int trp_rentry_get_selected(TRP_RENTRY *entry)
149 return entry->selected;
152 void trp_rentry_set_interval(TRP_RENTRY *entry, int interval)
154 entry->interval=interval;
157 int trp_rentry_get_interval(TRP_RENTRY *entry)
159 return entry->interval;
162 /* copies incoming value, does not assume responsibility for freeing */
163 void trp_rentry_set_expiry(TRP_RENTRY *entry, struct timespec *exp)
165 entry->expiry->tv_sec=exp->tv_sec;
166 entry->expiry->tv_nsec=exp->tv_nsec;
169 struct timespec *trp_rentry_get_expiry(TRP_RENTRY *entry)
171 return entry->expiry;
175 /* result must be freed with g_free */
176 static gchar *tr_name_to_g_str(const TR_NAME *n)
178 gchar *s=g_strndup(n->buf, n->len);
182 /* hash function for TR_NAME keys */
183 static guint trp_tr_name_hash(gconstpointer key)
185 const TR_NAME *name=(TR_NAME *)key;
186 gchar *s=tr_name_to_g_str(name);
187 guint hash=g_str_hash(s);
192 /* hash equality function for TR_NAME keys */
193 static gboolean trp_tr_name_equal(gconstpointer key1, gconstpointer key2)
195 const TR_NAME *n1=(TR_NAME *)key1;
196 const TR_NAME *n2=(TR_NAME *)key2;
197 gchar *s1=tr_name_to_g_str(n1);
198 gchar *s2=tr_name_to_g_str(n2);
199 gboolean equal=g_str_equal(s1, s2);
205 /* free a value to the top level rtable (a hash of all entries in the apc) */
206 static void trp_rtable_destroy_table(gpointer data)
208 g_hash_table_destroy(data);
211 static void trp_rtable_destroy_rentry(gpointer data)
213 trp_rentry_free(data);
216 static void trp_rtable_destroy_tr_name(gpointer data)
221 TRP_RTABLE *trp_rtable_new(void)
223 GHashTable *new=g_hash_table_new_full(trp_tr_name_hash,
225 trp_rtable_destroy_tr_name,
226 trp_rtable_destroy_table);
230 void trp_rtable_free(TRP_RTABLE *rtbl)
232 g_hash_table_destroy(rtbl);
235 static GHashTable *trp_rtbl_get_or_add_table(GHashTable *tbl, TR_NAME *key, GDestroyNotify destroy)
237 GHashTable *val_tbl=NULL;
239 val_tbl=g_hash_table_lookup(tbl, key);
241 val_tbl=g_hash_table_new_full(trp_tr_name_hash,
243 trp_rtable_destroy_tr_name,
245 g_hash_table_insert(tbl, tr_dup_name(key), val_tbl);
250 void trp_rtable_add(TRP_RTABLE *rtbl, TRP_RENTRY *entry)
252 GHashTable *apc_tbl=NULL;
253 GHashTable *realm_tbl=NULL;
255 apc_tbl=trp_rtbl_get_or_add_table(rtbl, entry->apc, trp_rtable_destroy_table);
256 realm_tbl=trp_rtbl_get_or_add_table(apc_tbl, entry->realm, trp_rtable_destroy_rentry);
257 g_hash_table_insert(realm_tbl, tr_dup_name(entry->peer), entry); /* destroys and replaces a duplicate */
260 /* note: the entry pointer passed in is invalid after calling this because the entry is freed */
261 void trp_rtable_remove(TRP_RTABLE *rtbl, TRP_RENTRY *entry)
263 GHashTable *apc_tbl=NULL;
264 GHashTable *realm_tbl=NULL;
266 apc_tbl=g_hash_table_lookup(rtbl, entry->apc);
270 realm_tbl=g_hash_table_lookup(apc_tbl, entry->realm);
274 /* remove the element */
275 g_hash_table_remove(realm_tbl, entry->peer);
276 /* if that was the last entry in the realm, remove the realm table */
277 if (g_hash_table_size(realm_tbl)==0)
278 g_hash_table_remove(apc_tbl, entry->realm);
279 /* if that was the last realm in the apc, remove the apc table */
280 if (g_hash_table_size(apc_tbl)==0)
281 g_hash_table_remove(rtbl, entry->apc);
284 /* gets the actual hash table, for internal use only */
285 static GHashTable *trp_rtable_get_apc_table(TRP_RTABLE *rtbl, TR_NAME *apc)
287 return g_hash_table_lookup(rtbl, apc);
290 /* gets the actual hash table, for internal use only */
291 static GHashTable *trp_rtable_get_realm_table(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm)
293 GHashTable *apc_tbl=trp_rtable_get_apc_table(rtbl, apc);
297 return g_hash_table_lookup(apc_tbl, realm);
300 struct table_size_cookie {
304 static void trp_rtable_size_helper(gpointer key, gpointer value, gpointer user_data)
306 struct table_size_cookie *data=(struct table_size_cookie *)user_data;
307 data->size += trp_rtable_apc_size(data->rtbl, (TR_NAME *)key);
309 size_t trp_rtable_size(TRP_RTABLE *rtbl)
311 struct table_size_cookie data={rtbl, 0};
312 g_hash_table_foreach(rtbl, trp_rtable_size_helper, &data);
316 struct table_apc_size_cookie {
321 static void table_apc_size_helper(gpointer key, gpointer value, gpointer user_data)
323 struct table_apc_size_cookie *data=(struct table_apc_size_cookie *)user_data;
324 data->size += trp_rtable_realm_size(data->rtbl, data->apc, (TR_NAME *)key);
326 size_t trp_rtable_apc_size(TRP_RTABLE *rtbl, TR_NAME *apc)
328 struct table_apc_size_cookie data={apc, rtbl, 0};
329 GHashTable *apc_tbl=trp_rtable_get_apc_table(rtbl, apc);
332 g_hash_table_foreach(apc_tbl, table_apc_size_helper, &data);
336 size_t trp_rtable_realm_size(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm)
338 GHashTable *realm_tbl=trp_rtable_get_realm_table(rtbl, apc, realm);
342 return g_hash_table_size(g_hash_table_lookup(
343 g_hash_table_lookup(rtbl, apc),
347 /* Returns an array of pointers to TRP_RENTRY, length of array in n_out.
348 * Caller must free the array (in the talloc NULL context), but must
349 * not free its contents. */
350 TRP_RENTRY **trp_rtable_get_entries(TRP_RTABLE *rtbl, size_t *n_out)
352 TRP_RENTRY **ret=NULL;
355 TRP_RENTRY **apc_entries=NULL;
359 *n_out=trp_rtable_size(rtbl);
363 ret=talloc_array(NULL, TRP_RENTRY *, *n_out);
365 tr_crit("trp_rtable_get_entries: unable to allocate return array.");
370 ii_ret=0; /* counts output entries */
371 apc=trp_rtable_get_apcs(rtbl, &n_apc);
373 apc_entries=trp_rtable_get_apc_entries(rtbl, apc[n_apc], &n_entries);
375 ret[ii_ret++]=apc_entries[n_entries];
376 talloc_free(apc_entries);
380 if (ii_ret!=*n_out) {
381 tr_crit("trp_rtable_get_entries: found incorrect number of entries.");
389 /* Returns an array of pointers to TR_NAME, length of array in n_out.
390 * Caller must free the array (in the talloc NULL context). */
391 TR_NAME **trp_rtable_get_apcs(TRP_RTABLE *rtbl, size_t *n_out)
393 size_t len=g_hash_table_size(rtbl); /* known apcs are keys in top level hash table */
404 ret=talloc_array(NULL, TR_NAME *, len);
406 tr_crit("trp_rtable_get_apcs: unable to allocate return array.");
410 apcs=g_hash_table_get_keys(rtbl);
411 for (ii=0,p=apcs; p!=NULL; ii++,p=g_list_next(p))
412 ret[ii]=(TR_NAME *)p->data;
420 /* Returns an array of pointers to TR_NAME, length of array in n_out.
421 * Caller must free the array (in the talloc NULL context). */
422 TR_NAME **trp_rtable_get_apc_realms(TRP_RTABLE *rtbl, TR_NAME *apc, size_t *n_out)
425 TRP_RTABLE *apc_tbl=g_hash_table_lookup(rtbl, apc);;
434 *n_out=g_hash_table_size(apc_tbl); /* set output length */
435 ret=talloc_array(NULL, TR_NAME *, *n_out);
436 entries=g_hash_table_get_keys(apc_tbl);
437 for (ii=0,p=entries; p!=NULL; ii++,p=g_list_next(p))
438 ret[ii]=(TR_NAME *)p->data;
440 g_list_free(entries);
444 /* Get all entries in an apc. Returns an array of pointers in NULL talloc context.
445 * Caller must free this list with talloc_free, but must not free the entries in the
447 TRP_RENTRY **trp_rtable_get_apc_entries(TRP_RTABLE *rtbl, TR_NAME *apc, size_t *n_out)
450 TR_NAME **realm=NULL;
452 TRP_RENTRY **realm_entries=NULL;
454 TRP_RENTRY **ret=NULL;
457 *n_out=trp_rtable_apc_size(rtbl, apc);
461 ret=talloc_array(NULL, TRP_RENTRY *, *n_out);
463 tr_crit("trp_rtable_get_apc_entries: could not allocate return array.");
468 ii_ret=0; /* counts entries in the output array */
469 realm=trp_rtable_get_apc_realms(rtbl, apc, &n_realms);
470 for (ii=0; ii<n_realms; ii++) {
471 realm_entries=trp_rtable_get_realm_entries(rtbl, apc, realm[ii], &n_entries);
472 for (jj=0; jj<n_entries; jj++)
473 ret[ii_ret++]=realm_entries[jj];
474 talloc_free(realm_entries);
478 if (ii_ret!=*n_out) {
479 tr_crit("trp_rtable_get_apc_entries: found incorrect number of entries.");
488 /* Get all entries in an apc/realm. Returns an array of pointers in NULL talloc context.
489 * Caller must free this list with talloc_free, but must not free the entries in the
491 TRP_RENTRY **trp_rtable_get_realm_entries(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, size_t *n_out)
494 TRP_RENTRY **ret=NULL;
497 peer=trp_rtable_get_apc_realm_peers(rtbl, apc, realm, n_out);
498 ret=talloc_array(NULL, TRP_RENTRY *, *n_out);
500 tr_crit("trp_rtable_get_realm_entries: could not allocate return array.");
505 for (ii=0; ii<*n_out; ii++)
506 ret[ii]=trp_rtable_get_entry(rtbl, apc, realm, peer[ii]);
511 TR_NAME **trp_rtable_get_apc_realm_peers(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, size_t *n_out)
514 GHashTable *realm_tbl=NULL;
519 *n_out=trp_rtable_realm_size(rtbl, apc, realm);
522 realm_tbl=trp_rtable_get_realm_table(rtbl, apc, realm);
523 ret=talloc_array(NULL, TR_NAME *, *n_out);
525 tr_crit("trp_rtable_get_apc_realm_peers: could not allocate return array.");
529 keys=g_hash_table_get_keys(realm_tbl);
530 for (ii=0,p=keys; p!=NULL; ii++,p=g_list_next(p))
531 ret[ii]=(TR_NAME *)p->data;
536 /* Gets a single entry. Do not free it. */
537 TRP_RENTRY *trp_rtable_get_entry(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, TR_NAME *peer)
539 GHashTable *realm_tbl=NULL;
541 realm_tbl=trp_rtable_get_realm_table(rtbl, apc, realm);
544 return g_hash_table_lookup(realm_tbl, peer); /* does not copy or increment ref count */
547 static char *timespec_to_str(struct timespec *ts)
552 if (localtime_r(&(ts->tv_sec), &tm)==NULL)
555 s=malloc(40); /* long enough to contain strftime result */
559 if (strftime(s, 40, "%F %T", &tm)==0) {
566 TRP_RENTRY *trp_rtable_get_selected_entry(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm)
569 TRP_RENTRY **entry=trp_rtable_get_realm_entries(rtbl, apc, realm, &n);
570 TRP_RENTRY *selected=NULL;
575 while(n-- && !trp_rentry_get_selected(entry[n])) { }
581 /* Pretty print a route table entry to a newly allocated string. If sep is NULL,
582 * returns comma+space separated string. */
583 char *trp_rentry_to_str(TALLOC_CTX *mem_ctx, TRP_RENTRY *entry, const char *sep)
585 char *apc=tr_name_strdup(entry->apc);
586 char *realm=tr_name_strdup(entry->realm);
587 char *peer=tr_name_strdup(entry->peer);
588 char *trust_router=tr_name_strdup(entry->trust_router);
589 char *next_hop=tr_name_strdup(entry->next_hop);
590 char *expiry=timespec_to_str(entry->expiry);
596 result=talloc_asprintf(mem_ctx,
597 "%s%s%s%s%s%s%u%s%s%s%s%s%d%s%s",
604 entry->selected, sep,
615 static int sort_tr_names_cmp(const void *a, const void *b)
617 TR_NAME **n1=(TR_NAME **)a;
618 TR_NAME **n2=(TR_NAME **)b;
619 return tr_name_cmp(*n1, *n2);
622 static void sort_tr_names(TR_NAME **names, size_t n_names)
624 qsort(names, n_names, sizeof(TR_NAME *), sort_tr_names_cmp);
627 char *trp_rtable_to_str(TALLOC_CTX *mem_ctx, TRP_RTABLE *rtbl, const char *sep, const char *lineterm)
629 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
632 TR_NAME **realms=NULL;
634 TRP_RENTRY **entries=NULL;
636 char **tbl_strings=NULL;
637 size_t ii_tbl=0; /* counts tbl_strings */
640 size_t ii=0, jj=0, kk=0;
647 tbl_size=trp_rtable_size(rtbl);
649 result=talloc_strdup(mem_ctx, lineterm);
653 tbl_strings=talloc_array(tmp_ctx, char *, tbl_size);
654 if (tbl_strings==NULL) {
655 result=talloc_strdup(mem_ctx, "error");
659 apcs=trp_rtable_get_apcs(rtbl, &n_apcs);
660 talloc_steal(tmp_ctx, apcs);
661 sort_tr_names(apcs, n_apcs);
664 for (ii=0; ii<n_apcs; ii++) {
665 realms=trp_rtable_get_apc_realms(rtbl, apcs[ii], &n_realms);
666 talloc_steal(tmp_ctx, realms);
667 sort_tr_names(realms, n_realms);
668 for (jj=0; jj<n_realms; jj++) {
669 entries=trp_rtable_get_realm_entries(rtbl, apcs[ii], realms[jj], &n_entries);
670 talloc_steal(tmp_ctx, entries);
671 for (kk=0; kk<n_entries; kk++) {
672 tbl_strings[ii_tbl]=trp_rentry_to_str(tmp_ctx, entries[kk], sep);
673 len+=strlen(tbl_strings[ii_tbl]);
676 talloc_free(entries);
682 /* now combine all the strings */
683 len += tbl_size*strlen(lineterm); /* space for line terminations*/
684 len += 1; /* nul terminator */
685 result=(char *)talloc_size(tmp_ctx, len);
686 for (p=result,ii=0; ii < tbl_size; ii++) {
687 p+=sprintf(p, "%s%s", tbl_strings[ii], lineterm);
689 talloc_steal(mem_ctx, result);
692 talloc_free(tmp_ctx);