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 */
258 /* the route entry should not belong to any context, we will manage it ourselves */
259 talloc_steal(NULL, entry);
262 /* note: the entry pointer passed in is invalid after calling this because the entry is freed */
263 void trp_rtable_remove(TRP_RTABLE *rtbl, TRP_RENTRY *entry)
265 GHashTable *apc_tbl=NULL;
266 GHashTable *realm_tbl=NULL;
268 apc_tbl=g_hash_table_lookup(rtbl, entry->apc);
272 realm_tbl=g_hash_table_lookup(apc_tbl, entry->realm);
276 /* remove the element */
277 g_hash_table_remove(realm_tbl, entry->peer);
278 /* if that was the last entry in the realm, remove the realm table */
279 if (g_hash_table_size(realm_tbl)==0)
280 g_hash_table_remove(apc_tbl, entry->realm);
281 /* if that was the last realm in the apc, remove the apc table */
282 if (g_hash_table_size(apc_tbl)==0)
283 g_hash_table_remove(rtbl, entry->apc);
286 void trp_rtable_clear(TRP_RTABLE *rtbl)
288 g_hash_table_remove_all(rtbl); /* destructors should do all the cleanup */
291 /* gets the actual hash table, for internal use only */
292 static GHashTable *trp_rtable_get_apc_table(TRP_RTABLE *rtbl, TR_NAME *apc)
294 return g_hash_table_lookup(rtbl, apc);
297 /* gets the actual hash table, for internal use only */
298 static GHashTable *trp_rtable_get_realm_table(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm)
300 GHashTable *apc_tbl=trp_rtable_get_apc_table(rtbl, apc);
304 return g_hash_table_lookup(apc_tbl, realm);
307 struct table_size_cookie {
311 static void trp_rtable_size_helper(gpointer key, gpointer value, gpointer user_data)
313 struct table_size_cookie *data=(struct table_size_cookie *)user_data;
314 data->size += trp_rtable_apc_size(data->rtbl, (TR_NAME *)key);
316 size_t trp_rtable_size(TRP_RTABLE *rtbl)
318 struct table_size_cookie data={rtbl, 0};
319 g_hash_table_foreach(rtbl, trp_rtable_size_helper, &data);
323 struct table_apc_size_cookie {
328 static void table_apc_size_helper(gpointer key, gpointer value, gpointer user_data)
330 struct table_apc_size_cookie *data=(struct table_apc_size_cookie *)user_data;
331 data->size += trp_rtable_realm_size(data->rtbl, data->apc, (TR_NAME *)key);
333 size_t trp_rtable_apc_size(TRP_RTABLE *rtbl, TR_NAME *apc)
335 struct table_apc_size_cookie data={apc, rtbl, 0};
336 GHashTable *apc_tbl=trp_rtable_get_apc_table(rtbl, apc);
339 g_hash_table_foreach(apc_tbl, table_apc_size_helper, &data);
343 size_t trp_rtable_realm_size(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm)
345 GHashTable *realm_tbl=trp_rtable_get_realm_table(rtbl, apc, realm);
349 return g_hash_table_size(g_hash_table_lookup(
350 g_hash_table_lookup(rtbl, apc),
354 /* Returns an array of pointers to TRP_RENTRY, length of array in n_out.
355 * Caller must free the array (in the talloc NULL context), but must
356 * not free its contents. */
357 TRP_RENTRY **trp_rtable_get_entries(TRP_RTABLE *rtbl, size_t *n_out)
359 TRP_RENTRY **ret=NULL;
362 TRP_RENTRY **apc_entries=NULL;
366 *n_out=trp_rtable_size(rtbl);
370 ret=talloc_array(NULL, TRP_RENTRY *, *n_out);
372 tr_crit("trp_rtable_get_entries: unable to allocate return array.");
377 ii_ret=0; /* counts output entries */
378 apc=trp_rtable_get_apcs(rtbl, &n_apc);
380 apc_entries=trp_rtable_get_apc_entries(rtbl, apc[n_apc], &n_entries);
382 ret[ii_ret++]=apc_entries[n_entries];
383 talloc_free(apc_entries);
387 if (ii_ret!=*n_out) {
388 tr_crit("trp_rtable_get_entries: found incorrect number of entries.");
396 /* Returns an array of pointers to TR_NAME, length of array in n_out.
397 * Caller must free the array (in the talloc NULL context). */
398 TR_NAME **trp_rtable_get_apcs(TRP_RTABLE *rtbl, size_t *n_out)
400 size_t len=g_hash_table_size(rtbl); /* known apcs are keys in top level hash table */
411 ret=talloc_array(NULL, TR_NAME *, len);
413 tr_crit("trp_rtable_get_apcs: unable to allocate return array.");
417 apcs=g_hash_table_get_keys(rtbl);
418 for (ii=0,p=apcs; p!=NULL; ii++,p=g_list_next(p))
419 ret[ii]=(TR_NAME *)p->data;
427 /* Returns an array of pointers to TR_NAME, length of array in n_out.
428 * Caller must free the array (in the talloc NULL context). */
429 TR_NAME **trp_rtable_get_apc_realms(TRP_RTABLE *rtbl, TR_NAME *apc, size_t *n_out)
432 TRP_RTABLE *apc_tbl=g_hash_table_lookup(rtbl, apc);;
441 *n_out=g_hash_table_size(apc_tbl); /* set output length */
442 ret=talloc_array(NULL, TR_NAME *, *n_out);
443 entries=g_hash_table_get_keys(apc_tbl);
444 for (ii=0,p=entries; p!=NULL; ii++,p=g_list_next(p))
445 ret[ii]=(TR_NAME *)p->data;
447 g_list_free(entries);
451 /* Get all entries in an apc. Returns an array of pointers in NULL talloc context.
452 * Caller must free this list with talloc_free, but must not free the entries in the
454 TRP_RENTRY **trp_rtable_get_apc_entries(TRP_RTABLE *rtbl, TR_NAME *apc, size_t *n_out)
457 TR_NAME **realm=NULL;
459 TRP_RENTRY **realm_entries=NULL;
461 TRP_RENTRY **ret=NULL;
464 *n_out=trp_rtable_apc_size(rtbl, apc);
468 ret=talloc_array(NULL, TRP_RENTRY *, *n_out);
470 tr_crit("trp_rtable_get_apc_entries: could not allocate return array.");
475 ii_ret=0; /* counts entries in the output array */
476 realm=trp_rtable_get_apc_realms(rtbl, apc, &n_realms);
477 for (ii=0; ii<n_realms; ii++) {
478 realm_entries=trp_rtable_get_realm_entries(rtbl, apc, realm[ii], &n_entries);
479 for (jj=0; jj<n_entries; jj++)
480 ret[ii_ret++]=realm_entries[jj];
481 talloc_free(realm_entries);
485 if (ii_ret!=*n_out) {
486 tr_crit("trp_rtable_get_apc_entries: found incorrect number of entries.");
495 /* Get all entries in an apc/realm. Returns an array of pointers in NULL talloc context.
496 * Caller must free this list with talloc_free, but must not free the entries in the
498 TRP_RENTRY **trp_rtable_get_realm_entries(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, size_t *n_out)
501 TRP_RENTRY **ret=NULL;
504 peer=trp_rtable_get_apc_realm_peers(rtbl, apc, realm, n_out);
505 ret=talloc_array(NULL, TRP_RENTRY *, *n_out);
507 tr_crit("trp_rtable_get_realm_entries: could not allocate return array.");
512 for (ii=0; ii<*n_out; ii++)
513 ret[ii]=trp_rtable_get_entry(rtbl, apc, realm, peer[ii]);
518 TR_NAME **trp_rtable_get_apc_realm_peers(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, size_t *n_out)
521 GHashTable *realm_tbl=NULL;
526 *n_out=trp_rtable_realm_size(rtbl, apc, realm);
529 realm_tbl=trp_rtable_get_realm_table(rtbl, apc, realm);
530 ret=talloc_array(NULL, TR_NAME *, *n_out);
532 tr_crit("trp_rtable_get_apc_realm_peers: could not allocate return array.");
536 keys=g_hash_table_get_keys(realm_tbl);
537 for (ii=0,p=keys; p!=NULL; ii++,p=g_list_next(p))
538 ret[ii]=(TR_NAME *)p->data;
543 /* Gets a single entry. Do not free it. */
544 TRP_RENTRY *trp_rtable_get_entry(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, TR_NAME *peer)
546 GHashTable *realm_tbl=NULL;
548 realm_tbl=trp_rtable_get_realm_table(rtbl, apc, realm);
552 return g_hash_table_lookup(realm_tbl, peer); /* does not copy or increment ref count */
555 static char *timespec_to_str(struct timespec *ts)
560 if (localtime_r(&(ts->tv_sec), &tm)==NULL)
563 s=malloc(40); /* long enough to contain strftime result */
567 if (strftime(s, 40, "%F %T", &tm)==0) {
574 TRP_RENTRY *trp_rtable_get_selected_entry(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm)
577 TRP_RENTRY **entry=trp_rtable_get_realm_entries(rtbl, apc, realm, &n);
578 TRP_RENTRY *selected=NULL;
583 while(n-- && !trp_rentry_get_selected(entry[n])) { }
589 /* Pretty print a route table entry to a newly allocated string. If sep is NULL,
590 * returns comma+space separated string. */
591 char *trp_rentry_to_str(TALLOC_CTX *mem_ctx, TRP_RENTRY *entry, const char *sep)
593 char *apc=tr_name_strdup(entry->apc);
594 char *realm=tr_name_strdup(entry->realm);
595 char *peer=tr_name_strdup(entry->peer);
596 char *trust_router=tr_name_strdup(entry->trust_router);
597 char *next_hop=tr_name_strdup(entry->next_hop);
598 char *expiry=timespec_to_str(entry->expiry);
604 result=talloc_asprintf(mem_ctx,
605 "%s%s%s%s%s%s%u%s%s%s%s%s%d%s%s",
612 entry->selected, sep,
623 static int sort_tr_names_cmp(const void *a, const void *b)
625 TR_NAME **n1=(TR_NAME **)a;
626 TR_NAME **n2=(TR_NAME **)b;
627 return tr_name_cmp(*n1, *n2);
630 static void sort_tr_names(TR_NAME **names, size_t n_names)
632 qsort(names, n_names, sizeof(TR_NAME *), sort_tr_names_cmp);
635 char *trp_rtable_to_str(TALLOC_CTX *mem_ctx, TRP_RTABLE *rtbl, const char *sep, const char *lineterm)
637 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
640 TR_NAME **realms=NULL;
642 TRP_RENTRY **entries=NULL;
644 char **tbl_strings=NULL;
645 size_t ii_tbl=0; /* counts tbl_strings */
648 size_t ii=0, jj=0, kk=0;
655 tbl_size=trp_rtable_size(rtbl);
657 result=talloc_strdup(mem_ctx, lineterm);
661 tbl_strings=talloc_array(tmp_ctx, char *, tbl_size);
662 if (tbl_strings==NULL) {
663 result=talloc_strdup(mem_ctx, "error");
667 apcs=trp_rtable_get_apcs(rtbl, &n_apcs);
668 talloc_steal(tmp_ctx, apcs);
669 sort_tr_names(apcs, n_apcs);
672 for (ii=0; ii<n_apcs; ii++) {
673 realms=trp_rtable_get_apc_realms(rtbl, apcs[ii], &n_realms);
674 talloc_steal(tmp_ctx, realms);
675 sort_tr_names(realms, n_realms);
676 for (jj=0; jj<n_realms; jj++) {
677 entries=trp_rtable_get_realm_entries(rtbl, apcs[ii], realms[jj], &n_entries);
678 talloc_steal(tmp_ctx, entries);
679 for (kk=0; kk<n_entries; kk++) {
680 tbl_strings[ii_tbl]=trp_rentry_to_str(tmp_ctx, entries[kk], sep);
681 len+=strlen(tbl_strings[ii_tbl]);
684 talloc_free(entries);
690 /* now combine all the strings */
691 len += tbl_size*strlen(lineterm); /* space for line terminations*/
692 len += 1; /* nul terminator */
693 result=(char *)talloc_size(tmp_ctx, len);
694 for (p=result,ii=0; ii < tbl_size; ii++) {
695 p+=sprintf(p, "%s%s", tbl_strings[ii], lineterm);
697 talloc_steal(mem_ctx, result);
700 talloc_free(tmp_ctx);