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 void trp_rentry_set_realm(TRP_RENTRY *entry, TR_NAME *realm)
72 TR_NAME *trp_rentry_get_realm(TRP_RENTRY *entry)
77 void trp_rentry_set_trust_router(TRP_RENTRY *entry, TR_NAME *tr)
79 entry->trust_router=tr;
82 TR_NAME *trp_rentry_get_trust_router(TRP_RENTRY *entry)
84 return entry->trust_router;
87 void trp_rentry_set_peer(TRP_RENTRY *entry, TR_NAME *peer)
92 TR_NAME *trp_rentry_get_peer(TRP_RENTRY *entry)
97 void trp_rentry_set_metric(TRP_RENTRY *entry, unsigned int metric)
102 unsigned int trp_rentry_get_metric(TRP_RENTRY *entry)
104 return entry->metric;
107 void trp_rentry_set_next_hop(TRP_RENTRY *entry, TR_NAME *next_hop)
109 entry->next_hop=next_hop;
112 TR_NAME *trp_rentry_get_next_hop(TRP_RENTRY *entry)
114 return entry->next_hop;
117 void trp_rentry_set_selected(TRP_RENTRY *entry, int sel)
122 int trp_rentry_get_selected(TRP_RENTRY *entry)
124 return entry->selected;
127 void trp_rentry_set_interval(TRP_RENTRY *entry, int interval)
129 entry->interval=interval;
132 int trp_rentry_get_interval(TRP_RENTRY *entry)
134 return entry->interval;
137 /* copies incoming value, does not assume responsibility for freeing */
138 void trp_rentry_set_expiry(TRP_RENTRY *entry, struct timespec *exp)
140 entry->expiry->tv_sec=exp->tv_sec;
141 entry->expiry->tv_nsec=exp->tv_nsec;
144 struct timespec *trp_rentry_get_expiry(TRP_RENTRY *entry)
146 return entry->expiry;
150 /* result must be freed with g_free */
151 static gchar *tr_name_to_g_str(const TR_NAME *n)
153 gchar *s=g_strndup(n->buf, n->len);
157 /* hash function for TR_NAME keys */
158 static guint trp_tr_name_hash(gconstpointer key)
160 const TR_NAME *name=(TR_NAME *)key;
161 gchar *s=tr_name_to_g_str(name);
162 guint hash=g_str_hash(s);
167 /* hash equality function for TR_NAME keys */
168 static gboolean trp_tr_name_equal(gconstpointer key1, gconstpointer key2)
170 const TR_NAME *n1=(TR_NAME *)key1;
171 const TR_NAME *n2=(TR_NAME *)key2;
172 gchar *s1=tr_name_to_g_str(n1);
173 gchar *s2=tr_name_to_g_str(n2);
174 gboolean equal=g_str_equal(s1, s2);
180 /* free a value to the top level rtable (a hash of all entries in the apc) */
181 static void trp_rtable_destroy_table(gpointer data)
183 g_hash_table_destroy(data);
186 static void trp_rtable_destroy_rentry(gpointer data)
188 trp_rentry_free(data);
191 static void trp_rtable_destroy_tr_name(gpointer data)
196 TRP_RTABLE *trp_rtable_new(void)
198 GHashTable *new=g_hash_table_new_full(trp_tr_name_hash,
200 trp_rtable_destroy_tr_name,
201 trp_rtable_destroy_table);
205 void trp_rtable_free(TRP_RTABLE *rtbl)
207 g_hash_table_destroy(rtbl);
210 static GHashTable *trp_rtbl_get_or_add_table(GHashTable *tbl, TR_NAME *key, GDestroyNotify destroy)
212 GHashTable *val_tbl=NULL;
214 val_tbl=g_hash_table_lookup(tbl, key);
216 val_tbl=g_hash_table_new_full(trp_tr_name_hash,
218 trp_rtable_destroy_tr_name,
220 g_hash_table_insert(tbl, tr_dup_name(key), val_tbl);
225 void trp_rtable_add(TRP_RTABLE *rtbl, TRP_RENTRY *entry)
227 GHashTable *apc_tbl=NULL;
228 GHashTable *realm_tbl=NULL;
230 apc_tbl=trp_rtbl_get_or_add_table(rtbl, entry->apc, trp_rtable_destroy_table);
231 realm_tbl=trp_rtbl_get_or_add_table(apc_tbl, entry->realm, trp_rtable_destroy_rentry);
232 g_hash_table_insert(realm_tbl, tr_dup_name(entry->peer), entry); /* destroys and replaces a duplicate */
235 /* note: the entry pointer passed in is invalid after calling this because the entry is freed */
236 void trp_rtable_remove(TRP_RTABLE *rtbl, TRP_RENTRY *entry)
238 GHashTable *apc_tbl=NULL;
239 GHashTable *realm_tbl=NULL;
241 apc_tbl=g_hash_table_lookup(rtbl, entry->apc);
245 realm_tbl=g_hash_table_lookup(apc_tbl, entry->realm);
249 /* remove the element */
250 g_hash_table_remove(realm_tbl, entry->peer);
251 /* if that was the last entry in the realm, remove the realm table */
252 if (g_hash_table_size(realm_tbl)==0)
253 g_hash_table_remove(apc_tbl, entry->realm);
254 /* if that was the last realm in the apc, remove the apc table */
255 if (g_hash_table_size(apc_tbl)==0)
256 g_hash_table_remove(rtbl, entry->apc);
259 /* gets the actual hash table, for internal use only */
260 static GHashTable *trp_rtable_get_apc_table(TRP_RTABLE *rtbl, TR_NAME *apc)
262 return g_hash_table_lookup(rtbl, apc);
265 /* gets the actual hash table, for internal use only */
266 static GHashTable *trp_rtable_get_realm_table(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm)
268 GHashTable *apc_tbl=trp_rtable_get_apc_table(rtbl, apc);
272 return g_hash_table_lookup(apc_tbl, realm);
275 struct table_size_cookie {
279 static void trp_rtable_size_helper(gpointer key, gpointer value, gpointer user_data)
281 struct table_size_cookie *data=(struct table_size_cookie *)user_data;
282 data->size += trp_rtable_apc_size(data->rtbl, (TR_NAME *)key);
284 size_t trp_rtable_size(TRP_RTABLE *rtbl)
286 struct table_size_cookie data={rtbl, 0};
287 g_hash_table_foreach(rtbl, trp_rtable_size_helper, &data);
291 struct table_apc_size_cookie {
296 static void table_apc_size_helper(gpointer key, gpointer value, gpointer user_data)
298 struct table_apc_size_cookie *data=(struct table_apc_size_cookie *)user_data;
299 data->size += trp_rtable_realm_size(data->rtbl, data->apc, (TR_NAME *)key);
301 size_t trp_rtable_apc_size(TRP_RTABLE *rtbl, TR_NAME *apc)
303 struct table_apc_size_cookie data={apc, rtbl, 0};
304 GHashTable *apc_tbl=trp_rtable_get_apc_table(rtbl, apc);
307 g_hash_table_foreach(apc_tbl, table_apc_size_helper, &data);
311 size_t trp_rtable_realm_size(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm)
313 GHashTable *realm_tbl=trp_rtable_get_realm_table(rtbl, apc, realm);
317 return g_hash_table_size(g_hash_table_lookup(
318 g_hash_table_lookup(rtbl, apc),
322 /* Returns an array of pointers to TRP_RENTRY, length of array in n_out.
323 * Caller must free the array (in the talloc NULL context), but must
324 * not free its contents. */
325 TRP_RENTRY **trp_rtable_get_entries(TRP_RTABLE *rtbl, size_t *n_out)
327 TRP_RENTRY **ret=NULL;
330 TRP_RENTRY **apc_entries=NULL;
334 *n_out=trp_rtable_size(rtbl);
338 ret=talloc_array(NULL, TRP_RENTRY *, *n_out);
340 tr_crit("trp_rtable_get_entries: unable to allocate return array.");
345 ii_ret=0; /* counts output entries */
346 apc=trp_rtable_get_apcs(rtbl, &n_apc);
348 apc_entries=trp_rtable_get_apc_entries(rtbl, apc[n_apc], &n_entries);
350 ret[ii_ret++]=apc_entries[n_entries];
351 talloc_free(apc_entries);
355 if (ii_ret!=*n_out) {
356 tr_crit("trp_rtable_get_entries: found incorrect number of entries.");
364 /* Returns an array of pointers to TR_NAME, length of array in n_out.
365 * Caller must free the array (in the talloc NULL context). */
366 TR_NAME **trp_rtable_get_apcs(TRP_RTABLE *rtbl, size_t *n_out)
368 size_t len=g_hash_table_size(rtbl); /* known apcs are keys in top level hash table */
379 ret=talloc_array(NULL, TR_NAME *, len);
381 tr_crit("trp_rtable_get_apcs: unable to allocate return array.");
385 apcs=g_hash_table_get_keys(rtbl);
386 for (ii=0,p=apcs; p!=NULL; ii++,p=g_list_next(p))
387 ret[ii]=(TR_NAME *)p->data;
395 /* Returns an array of pointers to TR_NAME, length of array in n_out.
396 * Caller must free the array (in the talloc NULL context). */
397 TR_NAME **trp_rtable_get_apc_realms(TRP_RTABLE *rtbl, TR_NAME *apc, size_t *n_out)
400 TRP_RTABLE *apc_tbl=g_hash_table_lookup(rtbl, apc);;
409 *n_out=g_hash_table_size(apc_tbl); /* set output length */
410 ret=talloc_array(NULL, TR_NAME *, *n_out);
411 entries=g_hash_table_get_keys(apc_tbl);
412 for (ii=0,p=entries; p!=NULL; ii++,p=g_list_next(p))
413 ret[ii]=(TR_NAME *)p->data;
415 g_list_free(entries);
419 /* Get all entries in an apc. Returns an array of pointers in NULL talloc context.
420 * Caller must free this list with talloc_free, but must not free the entries in the
422 TRP_RENTRY **trp_rtable_get_apc_entries(TRP_RTABLE *rtbl, TR_NAME *apc, size_t *n_out)
425 TR_NAME **realm=NULL;
427 TRP_RENTRY **realm_entries=NULL;
429 TRP_RENTRY **ret=NULL;
432 *n_out=trp_rtable_apc_size(rtbl, apc);
436 ret=talloc_array(NULL, TRP_RENTRY *, *n_out);
438 tr_crit("trp_rtable_get_apc_entries: could not allocate return array.");
443 ii_ret=0; /* counts entries in the output array */
444 realm=trp_rtable_get_apc_realms(rtbl, apc, &n_realms);
445 for (ii=0; ii<n_realms; ii++) {
446 realm_entries=trp_rtable_get_realm_entries(rtbl, apc, realm[ii], &n_entries);
447 for (jj=0; jj<n_entries; jj++)
448 ret[ii_ret++]=realm_entries[jj];
449 talloc_free(realm_entries);
453 if (ii_ret!=*n_out) {
454 tr_crit("trp_rtable_get_apc_entries: found incorrect number of entries.");
463 /* Get all entries in an apc/realm. Returns an array of pointers in NULL talloc context.
464 * Caller must free this list with talloc_free, but must not free the entries in the
466 TRP_RENTRY **trp_rtable_get_realm_entries(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, size_t *n_out)
469 TRP_RENTRY **ret=NULL;
472 peer=trp_rtable_get_apc_realm_peers(rtbl, apc, realm, n_out);
473 ret=talloc_array(NULL, TRP_RENTRY *, *n_out);
475 tr_crit("trp_rtable_get_realm_entries: could not allocate return array.");
480 for (ii=0; ii<*n_out; ii++)
481 ret[ii]=trp_rtable_get_entry(rtbl, apc, realm, peer[ii]);
486 TR_NAME **trp_rtable_get_apc_realm_peers(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, size_t *n_out)
489 GHashTable *realm_tbl=NULL;
494 *n_out=trp_rtable_realm_size(rtbl, apc, realm);
497 realm_tbl=trp_rtable_get_realm_table(rtbl, apc, realm);
498 ret=talloc_array(NULL, TR_NAME *, *n_out);
500 tr_crit("trp_rtable_get_apc_realm_peers: could not allocate return array.");
504 keys=g_hash_table_get_keys(realm_tbl);
505 for (ii=0,p=keys; p!=NULL; ii++,p=g_list_next(p))
506 ret[ii]=(TR_NAME *)p->data;
511 /* Gets a single entry. Do not free it. */
512 TRP_RENTRY *trp_rtable_get_entry(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, TR_NAME *peer)
514 GHashTable *realm_tbl=NULL;
516 realm_tbl=trp_rtable_get_realm_table(rtbl, apc, realm);
519 return g_hash_table_lookup(realm_tbl, peer); /* does not copy or increment ref count */
522 static char *timespec_to_str(struct timespec *ts)
527 if (localtime_r(&(ts->tv_sec), &tm)==NULL)
530 s=malloc(40); /* long enough to contain strftime result */
534 if (strftime(s, 40, "%F %T", &tm)==0) {
541 TRP_RENTRY *trp_rtable_get_selected_entry(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm)
544 TRP_RENTRY **entry=trp_rtable_get_realm_entries(rtbl, apc, realm, &n);
545 TRP_RENTRY *selected=NULL;
550 while(n-- && !trp_rentry_get_selected(entry[n])) { }
556 /* Pretty print a route table entry to a newly allocated string. If sep is NULL,
557 * returns comma+space separated string. */
558 char *trp_rentry_to_str(TALLOC_CTX *mem_ctx, TRP_RENTRY *entry, const char *sep)
560 char *apc=tr_name_strdup(entry->apc);
561 char *realm=tr_name_strdup(entry->realm);
562 char *peer=tr_name_strdup(entry->peer);
563 char *trust_router=tr_name_strdup(entry->trust_router);
564 char *next_hop=tr_name_strdup(entry->next_hop);
565 char *expiry=timespec_to_str(entry->expiry);
571 result=talloc_asprintf(mem_ctx,
572 "%s%s%s%s%s%s%u%s%s%s%s%s%d%s%s",
579 entry->selected, sep,
590 static int sort_tr_names_cmp(const void *a, const void *b)
592 TR_NAME **n1=(TR_NAME **)a;
593 TR_NAME **n2=(TR_NAME **)b;
594 return tr_name_cmp(*n1, *n2);
597 static void sort_tr_names(TR_NAME **names, size_t n_names)
599 qsort(names, n_names, sizeof(TR_NAME *), sort_tr_names_cmp);
602 char *trp_rtable_to_str(TALLOC_CTX *mem_ctx, TRP_RTABLE *rtbl, const char *sep, const char *lineterm)
604 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
607 TR_NAME **realms=NULL;
609 TRP_RENTRY **entries=NULL;
611 char **tbl_strings=NULL;
612 size_t ii_tbl=0; /* counts tbl_strings */
615 size_t ii=0, jj=0, kk=0;
622 tbl_size=trp_rtable_size(rtbl);
624 result=talloc_strdup(mem_ctx, lineterm);
628 tbl_strings=talloc_array(tmp_ctx, char *, tbl_size);
629 if (tbl_strings==NULL) {
630 result=talloc_strdup(mem_ctx, "error");
634 apcs=trp_rtable_get_apcs(rtbl, &n_apcs);
635 talloc_steal(tmp_ctx, apcs);
636 sort_tr_names(apcs, n_apcs);
639 for (ii=0; ii<n_apcs; ii++) {
640 realms=trp_rtable_get_apc_realms(rtbl, apcs[ii], &n_realms);
641 talloc_steal(tmp_ctx, realms);
642 sort_tr_names(realms, n_realms);
643 for (jj=0; jj<n_realms; jj++) {
644 entries=trp_rtable_get_realm_entries(rtbl, apcs[ii], realms[jj], &n_entries);
645 talloc_steal(tmp_ctx, entries);
646 for (kk=0; kk<n_entries; kk++) {
647 tbl_strings[ii_tbl]=trp_rentry_to_str(tmp_ctx, entries[kk], sep);
648 len+=strlen(tbl_strings[ii_tbl]);
651 talloc_free(entries);
657 /* now combine all the strings */
658 len += tbl_size*strlen(lineterm); /* space for line terminations*/
659 len += 1; /* nul terminator */
660 result=(char *)talloc_size(tmp_ctx, len);
661 for (p=result,ii=0; ii < tbl_size; ii++) {
662 p+=sprintf(p, "%s%s", tbl_strings[ii], lineterm);
664 talloc_steal(mem_ctx, result);
667 talloc_free(tmp_ctx);