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;
174 void trp_rentry_set_triggered(TRP_RENTRY *entry, int trig)
176 entry->triggered=trig;
179 int trp_rentry_get_triggered(TRP_RENTRY *entry)
181 return entry->triggered;
185 /* result must be freed with g_free */
186 static gchar *tr_name_to_g_str(const TR_NAME *n)
188 gchar *s=g_strndup(n->buf, n->len);
192 /* hash function for TR_NAME keys */
193 static guint trp_tr_name_hash(gconstpointer key)
195 const TR_NAME *name=(TR_NAME *)key;
196 gchar *s=tr_name_to_g_str(name);
197 guint hash=g_str_hash(s);
202 /* hash equality function for TR_NAME keys */
203 static gboolean trp_tr_name_equal(gconstpointer key1, gconstpointer key2)
205 const TR_NAME *n1=(TR_NAME *)key1;
206 const TR_NAME *n2=(TR_NAME *)key2;
207 gchar *s1=tr_name_to_g_str(n1);
208 gchar *s2=tr_name_to_g_str(n2);
209 gboolean equal=g_str_equal(s1, s2);
215 /* free a value to the top level rtable (a hash of all entries in the apc) */
216 static void trp_rtable_destroy_table(gpointer data)
218 g_hash_table_destroy(data);
221 static void trp_rtable_destroy_rentry(gpointer data)
223 trp_rentry_free(data);
226 static void trp_rtable_destroy_tr_name(gpointer data)
231 TRP_RTABLE *trp_rtable_new(void)
233 GHashTable *new=g_hash_table_new_full(trp_tr_name_hash,
235 trp_rtable_destroy_tr_name,
236 trp_rtable_destroy_table);
240 void trp_rtable_free(TRP_RTABLE *rtbl)
242 g_hash_table_destroy(rtbl);
245 static GHashTable *trp_rtbl_get_or_add_table(GHashTable *tbl, TR_NAME *key, GDestroyNotify destroy)
247 GHashTable *val_tbl=NULL;
249 val_tbl=g_hash_table_lookup(tbl, key);
251 val_tbl=g_hash_table_new_full(trp_tr_name_hash,
253 trp_rtable_destroy_tr_name,
255 g_hash_table_insert(tbl, tr_dup_name(key), val_tbl);
260 void trp_rtable_add(TRP_RTABLE *rtbl, TRP_RENTRY *entry)
262 GHashTable *apc_tbl=NULL;
263 GHashTable *realm_tbl=NULL;
265 apc_tbl=trp_rtbl_get_or_add_table(rtbl, entry->apc, trp_rtable_destroy_table);
266 realm_tbl=trp_rtbl_get_or_add_table(apc_tbl, entry->realm, trp_rtable_destroy_rentry);
267 g_hash_table_insert(realm_tbl, tr_dup_name(entry->peer), entry); /* destroys and replaces a duplicate */
268 /* the route entry should not belong to any context, we will manage it ourselves */
269 talloc_steal(NULL, entry);
272 /* note: the entry pointer passed in is invalid after calling this because the entry is freed */
273 void trp_rtable_remove(TRP_RTABLE *rtbl, TRP_RENTRY *entry)
275 GHashTable *apc_tbl=NULL;
276 GHashTable *realm_tbl=NULL;
278 apc_tbl=g_hash_table_lookup(rtbl, entry->apc);
282 realm_tbl=g_hash_table_lookup(apc_tbl, entry->realm);
286 /* remove the element */
287 g_hash_table_remove(realm_tbl, entry->peer);
288 /* if that was the last entry in the realm, remove the realm table */
289 if (g_hash_table_size(realm_tbl)==0)
290 g_hash_table_remove(apc_tbl, entry->realm);
291 /* if that was the last realm in the apc, remove the apc table */
292 if (g_hash_table_size(apc_tbl)==0)
293 g_hash_table_remove(rtbl, entry->apc);
296 void trp_rtable_clear(TRP_RTABLE *rtbl)
298 g_hash_table_remove_all(rtbl); /* destructors should do all the cleanup */
301 /* gets the actual hash table, for internal use only */
302 static GHashTable *trp_rtable_get_apc_table(TRP_RTABLE *rtbl, TR_NAME *apc)
304 return g_hash_table_lookup(rtbl, apc);
307 /* gets the actual hash table, for internal use only */
308 static GHashTable *trp_rtable_get_realm_table(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm)
310 GHashTable *apc_tbl=trp_rtable_get_apc_table(rtbl, apc);
314 return g_hash_table_lookup(apc_tbl, realm);
317 struct table_size_cookie {
321 static void trp_rtable_size_helper(gpointer key, gpointer value, gpointer user_data)
323 struct table_size_cookie *data=(struct table_size_cookie *)user_data;
324 data->size += trp_rtable_apc_size(data->rtbl, (TR_NAME *)key);
326 size_t trp_rtable_size(TRP_RTABLE *rtbl)
328 struct table_size_cookie data={rtbl, 0};
329 g_hash_table_foreach(rtbl, trp_rtable_size_helper, &data);
333 struct table_apc_size_cookie {
338 static void table_apc_size_helper(gpointer key, gpointer value, gpointer user_data)
340 struct table_apc_size_cookie *data=(struct table_apc_size_cookie *)user_data;
341 data->size += trp_rtable_realm_size(data->rtbl, data->apc, (TR_NAME *)key);
343 size_t trp_rtable_apc_size(TRP_RTABLE *rtbl, TR_NAME *apc)
345 struct table_apc_size_cookie data={apc, rtbl, 0};
346 GHashTable *apc_tbl=trp_rtable_get_apc_table(rtbl, apc);
349 g_hash_table_foreach(apc_tbl, table_apc_size_helper, &data);
353 size_t trp_rtable_realm_size(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm)
355 GHashTable *realm_tbl=trp_rtable_get_realm_table(rtbl, apc, realm);
359 return g_hash_table_size(g_hash_table_lookup(
360 g_hash_table_lookup(rtbl, apc),
364 /* Returns an array of pointers to TRP_RENTRY, length of array in n_out.
365 * Caller must free the array (in the talloc NULL context), but must
366 * not free its contents. */
367 TRP_RENTRY **trp_rtable_get_entries(TRP_RTABLE *rtbl, size_t *n_out)
369 TRP_RENTRY **ret=NULL;
372 TRP_RENTRY **apc_entries=NULL;
376 *n_out=trp_rtable_size(rtbl);
380 ret=talloc_array(NULL, TRP_RENTRY *, *n_out);
382 tr_crit("trp_rtable_get_entries: unable to allocate return array.");
387 ii_ret=0; /* counts output entries */
388 apc=trp_rtable_get_apcs(rtbl, &n_apc);
390 apc_entries=trp_rtable_get_apc_entries(rtbl, apc[n_apc], &n_entries);
392 ret[ii_ret++]=apc_entries[n_entries];
393 talloc_free(apc_entries);
397 if (ii_ret!=*n_out) {
398 tr_crit("trp_rtable_get_entries: found incorrect number of entries.");
406 /* Returns an array of pointers to TR_NAME, length of array in n_out.
407 * Caller must free the array (in the talloc NULL context). */
408 TR_NAME **trp_rtable_get_apcs(TRP_RTABLE *rtbl, size_t *n_out)
410 size_t len=g_hash_table_size(rtbl); /* known apcs are keys in top level hash table */
421 ret=talloc_array(NULL, TR_NAME *, len);
423 tr_crit("trp_rtable_get_apcs: unable to allocate return array.");
427 apcs=g_hash_table_get_keys(rtbl);
428 for (ii=0,p=apcs; p!=NULL; ii++,p=g_list_next(p))
429 ret[ii]=(TR_NAME *)p->data;
437 /* Returns an array of pointers to TR_NAME, length of array in n_out.
438 * Caller must free the array (in the talloc NULL context). */
439 TR_NAME **trp_rtable_get_apc_realms(TRP_RTABLE *rtbl, TR_NAME *apc, size_t *n_out)
442 TRP_RTABLE *apc_tbl=g_hash_table_lookup(rtbl, apc);;
451 *n_out=g_hash_table_size(apc_tbl); /* set output length */
452 ret=talloc_array(NULL, TR_NAME *, *n_out);
453 entries=g_hash_table_get_keys(apc_tbl);
454 for (ii=0,p=entries; p!=NULL; ii++,p=g_list_next(p))
455 ret[ii]=(TR_NAME *)p->data;
457 g_list_free(entries);
461 /* Get all entries in an apc. Returns an array of pointers in NULL talloc context.
462 * Caller must free this list with talloc_free, but must not free the entries in the
464 TRP_RENTRY **trp_rtable_get_apc_entries(TRP_RTABLE *rtbl, TR_NAME *apc, size_t *n_out)
467 TR_NAME **realm=NULL;
469 TRP_RENTRY **realm_entries=NULL;
471 TRP_RENTRY **ret=NULL;
474 *n_out=trp_rtable_apc_size(rtbl, apc);
478 ret=talloc_array(NULL, TRP_RENTRY *, *n_out);
480 tr_crit("trp_rtable_get_apc_entries: could not allocate return array.");
485 ii_ret=0; /* counts entries in the output array */
486 realm=trp_rtable_get_apc_realms(rtbl, apc, &n_realms);
487 for (ii=0; ii<n_realms; ii++) {
488 realm_entries=trp_rtable_get_realm_entries(rtbl, apc, realm[ii], &n_entries);
489 for (jj=0; jj<n_entries; jj++)
490 ret[ii_ret++]=realm_entries[jj];
491 talloc_free(realm_entries);
495 if (ii_ret!=*n_out) {
496 tr_crit("trp_rtable_get_apc_entries: found incorrect number of entries.");
505 /* Get all entries in an apc/realm. Returns an array of pointers in NULL talloc context.
506 * Caller must free this list with talloc_free, but must not free the entries in the
508 TRP_RENTRY **trp_rtable_get_realm_entries(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, size_t *n_out)
511 TRP_RENTRY **ret=NULL;
514 peer=trp_rtable_get_apc_realm_peers(rtbl, apc, realm, n_out);
515 ret=talloc_array(NULL, TRP_RENTRY *, *n_out);
517 tr_crit("trp_rtable_get_realm_entries: could not allocate return array.");
522 for (ii=0; ii<*n_out; ii++)
523 ret[ii]=trp_rtable_get_entry(rtbl, apc, realm, peer[ii]);
528 TR_NAME **trp_rtable_get_apc_realm_peers(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, size_t *n_out)
531 GHashTable *realm_tbl=NULL;
536 *n_out=trp_rtable_realm_size(rtbl, apc, realm);
539 realm_tbl=trp_rtable_get_realm_table(rtbl, apc, realm);
540 ret=talloc_array(NULL, TR_NAME *, *n_out);
542 tr_crit("trp_rtable_get_apc_realm_peers: could not allocate return array.");
546 keys=g_hash_table_get_keys(realm_tbl);
547 for (ii=0,p=keys; p!=NULL; ii++,p=g_list_next(p))
548 ret[ii]=(TR_NAME *)p->data;
553 /* Gets a single entry. Do not free it. */
554 TRP_RENTRY *trp_rtable_get_entry(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, TR_NAME *peer)
556 GHashTable *realm_tbl=NULL;
558 realm_tbl=trp_rtable_get_realm_table(rtbl, apc, realm);
562 return g_hash_table_lookup(realm_tbl, peer); /* does not copy or increment ref count */
565 static char *timespec_to_str(struct timespec *ts)
570 if (localtime_r(&(ts->tv_sec), &tm)==NULL)
573 s=malloc(40); /* long enough to contain strftime result */
577 if (strftime(s, 40, "%F %T", &tm)==0) {
584 TRP_RENTRY *trp_rtable_get_selected_entry(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm)
587 TRP_RENTRY **entry=trp_rtable_get_realm_entries(rtbl, apc, realm, &n);
588 TRP_RENTRY *selected=NULL;
593 while(n-- && !trp_rentry_get_selected(entry[n])) { }
599 /* Pretty print a route table entry to a newly allocated string. If sep is NULL,
600 * returns comma+space separated string. */
601 char *trp_rentry_to_str(TALLOC_CTX *mem_ctx, TRP_RENTRY *entry, const char *sep)
603 char *apc=tr_name_strdup(entry->apc);
604 char *realm=tr_name_strdup(entry->realm);
605 char *peer=tr_name_strdup(entry->peer);
606 char *trust_router=tr_name_strdup(entry->trust_router);
607 char *next_hop=tr_name_strdup(entry->next_hop);
608 char *expiry=timespec_to_str(entry->expiry);
614 result=talloc_asprintf(mem_ctx,
615 "%s%s%s%s%s%s%u%s%s%s%s%s%d%s%s",
622 entry->selected, sep,
633 void trp_rtable_clear_triggered(TRP_RTABLE *rtbl)
636 TRP_RENTRY **entries=trp_rtable_get_entries(rtbl, &n_entries);
640 for (ii=0; ii<n_entries; ii++)
641 trp_rentry_set_triggered(entries[ii], 0);
642 talloc_free(entries);
646 static int sort_tr_names_cmp(const void *a, const void *b)
648 TR_NAME **n1=(TR_NAME **)a;
649 TR_NAME **n2=(TR_NAME **)b;
650 return tr_name_cmp(*n1, *n2);
653 static void sort_tr_names(TR_NAME **names, size_t n_names)
655 qsort(names, n_names, sizeof(TR_NAME *), sort_tr_names_cmp);
658 char *trp_rtable_to_str(TALLOC_CTX *mem_ctx, TRP_RTABLE *rtbl, const char *sep, const char *lineterm)
660 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
663 TR_NAME **realms=NULL;
665 TRP_RENTRY **entries=NULL;
667 char **tbl_strings=NULL;
668 size_t ii_tbl=0; /* counts tbl_strings */
671 size_t ii=0, jj=0, kk=0;
678 tbl_size=trp_rtable_size(rtbl);
680 result=talloc_strdup(mem_ctx, lineterm);
684 tbl_strings=talloc_array(tmp_ctx, char *, tbl_size);
685 if (tbl_strings==NULL) {
686 result=talloc_strdup(mem_ctx, "error");
690 apcs=trp_rtable_get_apcs(rtbl, &n_apcs);
691 talloc_steal(tmp_ctx, apcs);
692 sort_tr_names(apcs, n_apcs);
695 for (ii=0; ii<n_apcs; ii++) {
696 realms=trp_rtable_get_apc_realms(rtbl, apcs[ii], &n_realms);
697 talloc_steal(tmp_ctx, realms);
698 sort_tr_names(realms, n_realms);
699 for (jj=0; jj<n_realms; jj++) {
700 entries=trp_rtable_get_realm_entries(rtbl, apcs[ii], realms[jj], &n_entries);
701 talloc_steal(tmp_ctx, entries);
702 for (kk=0; kk<n_entries; kk++) {
703 tbl_strings[ii_tbl]=trp_rentry_to_str(tmp_ctx, entries[kk], sep);
704 len+=strlen(tbl_strings[ii_tbl]);
707 talloc_free(entries);
713 /* now combine all the strings */
714 len += tbl_size*strlen(lineterm); /* space for line terminations*/
715 len += 1; /* nul terminator */
716 result=(char *)talloc_size(tmp_ctx, len);
717 for (p=result,ii=0; ii < tbl_size; ii++) {
718 p+=sprintf(p, "%s%s", tbl_strings[ii], lineterm);
720 talloc_steal(mem_ctx, result);
723 talloc_free(tmp_ctx);