e2ea5bbdb80ab7b9cc628fa8c12e55ef723bdc0f
[trust_router.git] / trp / trp_rtable.c
1 /*
2  * Copyright (c) 2016, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include <stdlib.h>
36
37 #include <glib.h>
38 #include <talloc.h>
39 #include <time.h>
40
41 #include <tr_name_internal.h>
42 #include <trp_internal.h>
43 #include <trp_rtable.h>
44 #include <tr_debug.h>
45 #include <trust_router/trp.h>
46 #include <trust_router/tid.h>
47
48 /* Note: be careful mixing talloc with glib. */
49
50 static int trp_route_destructor(void *obj)
51 {
52   TRP_ROUTE *entry=talloc_get_type_abort(obj, TRP_ROUTE);
53   if (entry->comm!=NULL)
54     tr_free_name(entry->comm);
55   if (entry->realm!=NULL)
56     tr_free_name(entry->realm);
57   if (entry->trust_router!=NULL)
58     tr_free_name(entry->trust_router);
59   if (entry->peer!=NULL)
60     tr_free_name(entry->peer);
61   if (entry->next_hop!=NULL)
62     tr_free_name(entry->next_hop);
63   return 0;
64 }
65
66 TRP_ROUTE *trp_route_new(TALLOC_CTX *mem_ctx)
67 {
68   TRP_ROUTE *entry=talloc(mem_ctx, TRP_ROUTE);
69   if (entry!=NULL) {
70     entry->comm=NULL;
71     entry->realm=NULL;
72     entry->trust_router=NULL;
73     entry->trp_port=TRP_PORT;
74     entry->tid_port=TID_PORT;
75     entry->peer=NULL;
76     entry->next_hop=NULL;
77     entry->selected=0;
78     entry->interval=0;
79     entry->expiry=talloc(entry, struct timespec);
80     if (entry->expiry==NULL) {
81       talloc_free(entry);
82       return NULL;
83     }
84     *(entry->expiry)=(struct timespec){0,0};
85     entry->local=0;
86     entry->triggered=0;
87     talloc_set_destructor((void *)entry, trp_route_destructor);
88   }
89   return entry;
90 }
91
92 void trp_route_free(TRP_ROUTE *entry)
93 {
94   if (entry!=NULL)
95     talloc_free(entry);
96 }
97
98 void trp_route_set_comm(TRP_ROUTE *entry, TR_NAME *comm)
99 {
100   if (entry->comm!=NULL)
101     tr_free_name(entry->comm);
102   entry->comm=comm;
103 }
104
105 TR_NAME *trp_route_get_comm(TRP_ROUTE *entry)
106 {
107   return entry->comm;
108 }
109
110 TR_NAME *trp_route_dup_comm(TRP_ROUTE *entry)
111 {
112   return tr_dup_name(trp_route_get_comm(entry));
113 }
114
115 void trp_route_set_realm(TRP_ROUTE *entry, TR_NAME *realm)
116 {
117   if (entry->realm!=NULL)
118     tr_free_name(entry->realm);
119   entry->realm=realm;
120 }
121
122 TR_NAME *trp_route_get_realm(TRP_ROUTE *entry)
123 {
124   return entry->realm;
125 }
126
127 TR_NAME *trp_route_dup_realm(TRP_ROUTE *entry)
128 {
129   return tr_dup_name(trp_route_get_realm(entry));
130 }
131
132 void trp_route_set_trust_router(TRP_ROUTE *entry, TR_NAME *tr)
133 {
134   if (entry->trust_router!=NULL)
135     tr_free_name(entry->trust_router);
136   entry->trust_router=tr;
137 }
138
139 TR_NAME *trp_route_get_trust_router(TRP_ROUTE *entry)
140 {
141   return entry->trust_router;
142 }
143
144 TR_NAME *trp_route_dup_trust_router(TRP_ROUTE *entry)
145 {
146   return tr_dup_name(trp_route_get_trust_router(entry));
147 }
148
149 void trp_route_set_peer(TRP_ROUTE *entry, TR_NAME *peer)
150 {
151   if (entry->peer!=NULL)
152     tr_free_name(entry->peer);
153   entry->peer=peer;
154 }
155
156 TR_NAME *trp_route_get_peer(TRP_ROUTE *entry)
157 {
158   return entry->peer;
159 }
160
161 TR_NAME *trp_route_dup_peer(TRP_ROUTE *entry)
162 {
163   return tr_dup_name(trp_route_get_peer(entry));
164 }
165
166 void trp_route_set_metric(TRP_ROUTE *entry, unsigned int metric)
167 {
168   entry->metric=metric;
169 }
170
171 unsigned int trp_route_get_metric(TRP_ROUTE *entry)
172 {
173   return entry->metric;
174 }
175
176 /* TODO: set the hostname and port for the next hop. Currently assume default TID port. --jlr */
177 void trp_route_set_next_hop(TRP_ROUTE *entry, TR_NAME *next_hop)
178 {
179   if (entry->next_hop!=NULL)
180     tr_free_name(entry->next_hop);
181   entry->next_hop=next_hop;
182 }
183
184 TR_NAME *trp_route_get_next_hop(TRP_ROUTE *entry)
185 {
186   return entry->next_hop;
187 }
188
189 TR_NAME *trp_route_dup_next_hop(TRP_ROUTE *entry)
190 {
191   return tr_dup_name(trp_route_get_next_hop(entry));
192 }
193
194 void trp_route_set_selected(TRP_ROUTE *entry, int sel)
195 {
196   entry->selected=sel;
197 }
198
199 int trp_route_is_selected(TRP_ROUTE *entry)
200 {
201   return entry->selected;
202 }
203
204 void trp_route_set_interval(TRP_ROUTE *entry, int interval)
205 {
206   entry->interval=interval;
207 }
208
209 int trp_route_get_interval(TRP_ROUTE *entry)
210 {
211   return entry->interval;
212 }
213
214 /* copies incoming value, does not assume responsibility for freeing */
215 void trp_route_set_expiry(TRP_ROUTE *entry, struct timespec *exp)
216 {
217   entry->expiry->tv_sec=exp->tv_sec;
218   entry->expiry->tv_nsec=exp->tv_nsec;
219 }
220
221 struct timespec *trp_route_get_expiry(TRP_ROUTE *entry)
222 {
223   return entry->expiry;
224 }
225
226 void trp_route_set_local(TRP_ROUTE *entry, int local)
227 {
228   entry->local=local;
229 }
230
231 int trp_route_is_local(TRP_ROUTE *entry)
232 {
233   return entry->local;
234 }
235
236 void trp_route_set_triggered(TRP_ROUTE *entry, int trig)
237 {
238   tr_debug("trp_route_set_triggered: setting route to %.*s/%.*s through %.*s to %s",
239            entry->comm->len, entry->comm->buf,
240            entry->realm->len, entry->realm->buf,
241            entry->peer->len, entry->peer->buf,
242            trig ? "triggered" : "not triggered");
243   entry->triggered=trig;
244 }
245
246 int trp_route_is_triggered(TRP_ROUTE *entry)
247 {
248   return entry->triggered;
249 }
250
251
252 /* result must be freed with g_free */
253 static gchar *tr_name_to_g_str(const TR_NAME *n)
254 {
255   gchar *s=g_strndup(n->buf, n->len);
256   if (s==NULL)
257     tr_debug("tr_name_to_g_str: allocation failure.");
258   return s;
259 }
260
261 /* hash function for TR_NAME keys */
262 static guint trp_tr_name_hash(gconstpointer key)
263 {
264   const TR_NAME *name=(TR_NAME *)key;
265   gchar *s=tr_name_to_g_str(name);
266   guint hash=g_str_hash(s);
267   g_free(s);
268   return hash;
269 }
270
271 /* hash equality function for TR_NAME keys */
272 static gboolean trp_tr_name_equal(gconstpointer key1, gconstpointer key2)
273 {
274   const TR_NAME *n1=(TR_NAME *)key1;
275   const TR_NAME *n2=(TR_NAME *)key2;
276   gchar *s1=tr_name_to_g_str(n1);
277   gchar *s2=tr_name_to_g_str(n2);
278   gboolean equal=g_str_equal(s1, s2);
279   g_free(s1);
280   g_free(s2);
281   return equal;
282 }
283
284 /* free a value to the top level rtable (a hash of all entries in the comm) */
285 static void trp_rtable_destroy_table(gpointer data)
286 {
287   g_hash_table_destroy(data);
288 }
289
290 static void trp_rtable_destroy_rentry(gpointer data)
291 {
292   trp_route_free(data);
293 }
294
295 static void trp_rtable_destroy_tr_name(gpointer data)
296 {
297   tr_free_name(data);
298 }
299
300 TRP_RTABLE *trp_rtable_new(void)
301 {
302   GHashTable *new=g_hash_table_new_full(trp_tr_name_hash,
303                                         trp_tr_name_equal,
304                                         trp_rtable_destroy_tr_name,
305                                         trp_rtable_destroy_table);
306   return new;
307 }
308
309 void trp_rtable_free(TRP_RTABLE *rtbl)
310 {
311   g_hash_table_destroy(rtbl);
312 }
313
314 static GHashTable *trp_rtbl_get_or_add_table(GHashTable *tbl, TR_NAME *key, GDestroyNotify destroy)
315 {
316   GHashTable *val_tbl=NULL;
317
318   val_tbl=g_hash_table_lookup(tbl, key);
319   if (val_tbl==NULL) {
320     val_tbl=g_hash_table_new_full(trp_tr_name_hash,
321                                   trp_tr_name_equal,
322                                   trp_rtable_destroy_tr_name,
323                                   destroy);
324     g_hash_table_insert(tbl, tr_dup_name(key), val_tbl);
325   }
326   return val_tbl;
327 }
328
329 void trp_rtable_add(TRP_RTABLE *rtbl, TRP_ROUTE *entry)
330 {
331   GHashTable *comm_tbl=NULL;
332   GHashTable *realm_tbl=NULL;
333
334   comm_tbl=trp_rtbl_get_or_add_table(rtbl, entry->comm, trp_rtable_destroy_table);
335   realm_tbl=trp_rtbl_get_or_add_table(comm_tbl, entry->realm, trp_rtable_destroy_rentry);
336   g_hash_table_insert(realm_tbl, tr_dup_name(entry->peer), entry); /* destroys and replaces a duplicate */
337   /* the route entry should not belong to any context, we will manage it ourselves */
338   talloc_steal(NULL, entry);
339 }
340
341 /* note: the entry pointer passed in is invalid after calling this because the entry is freed */
342 void trp_rtable_remove(TRP_RTABLE *rtbl, TRP_ROUTE *entry)
343 {
344   GHashTable *comm_tbl=NULL;
345   GHashTable *realm_tbl=NULL;
346
347   comm_tbl=g_hash_table_lookup(rtbl, entry->comm);
348   if (comm_tbl==NULL)
349     return;
350
351   realm_tbl=g_hash_table_lookup(comm_tbl, entry->realm);
352   if (realm_tbl==NULL)
353     return;
354
355   /* remove the element */
356   g_hash_table_remove(realm_tbl, entry->peer);
357   /* if that was the last entry in the realm, remove the realm table */
358   if (g_hash_table_size(realm_tbl)==0)
359     g_hash_table_remove(comm_tbl, entry->realm);
360   /* if that was the last realm in the comm, remove the comm table */
361   if (g_hash_table_size(comm_tbl)==0)
362     g_hash_table_remove(rtbl, entry->comm);
363 }
364
365 void trp_rtable_clear(TRP_RTABLE *rtbl)
366 {
367   g_hash_table_remove_all(rtbl); /* destructors should do all the cleanup */
368 }
369
370 /* gets the actual hash table, for internal use only */
371 static GHashTable *trp_rtable_get_comm_table(TRP_RTABLE *rtbl, TR_NAME *comm)
372 {
373   return g_hash_table_lookup(rtbl, comm);
374 }
375
376 /* gets the actual hash table, for internal use only */
377 static GHashTable *trp_rtable_get_realm_table(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
378 {
379   GHashTable *comm_tbl=trp_rtable_get_comm_table(rtbl, comm);
380   if (comm_tbl==NULL)
381     return NULL;
382   else
383     return g_hash_table_lookup(comm_tbl, realm);
384 }
385
386 struct table_size_cookie {
387   TRP_RTABLE *rtbl;
388   size_t size;
389 };
390 static void trp_rtable_size_helper(gpointer key, gpointer value, gpointer user_data)
391 {
392   struct table_size_cookie *data=(struct table_size_cookie *)user_data;
393   data->size += trp_rtable_comm_size(data->rtbl, (TR_NAME *)key);
394 };
395 size_t trp_rtable_size(TRP_RTABLE *rtbl)
396 {
397   struct table_size_cookie data={rtbl, 0};
398   g_hash_table_foreach(rtbl, trp_rtable_size_helper, &data);
399   return data.size;
400 }
401
402 struct table_comm_size_cookie {
403   TR_NAME *comm;
404   TRP_RTABLE *rtbl;
405   size_t size;
406 };
407 static void table_comm_size_helper(gpointer key, gpointer value, gpointer user_data)
408 {
409   struct table_comm_size_cookie *data=(struct table_comm_size_cookie *)user_data;
410   data->size += trp_rtable_realm_size(data->rtbl, data->comm, (TR_NAME *)key);
411 }
412 size_t trp_rtable_comm_size(TRP_RTABLE *rtbl, TR_NAME *comm)
413 {
414   struct table_comm_size_cookie data={comm, rtbl, 0};
415   GHashTable *comm_tbl=trp_rtable_get_comm_table(rtbl, comm);
416   if (comm_tbl==NULL)
417     return 0;;
418   g_hash_table_foreach(comm_tbl, table_comm_size_helper, &data);
419   return data.size;
420 }
421
422 size_t trp_rtable_realm_size(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
423 {
424   GHashTable *realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
425   if (realm_tbl==NULL)
426     return 0;
427   else
428     return g_hash_table_size(g_hash_table_lookup(
429                                g_hash_table_lookup(rtbl, comm),
430                                realm));
431 }
432
433 /* Returns an array of pointers to TRP_ROUTE, length of array in n_out.
434  * Caller must free the array (in the talloc NULL context), but must
435  * not free its contents. */
436 TRP_ROUTE **trp_rtable_get_entries(TRP_RTABLE *rtbl, size_t *n_out)
437 {
438   TRP_ROUTE **ret=NULL;
439   TR_NAME **comm=NULL;
440   size_t n_comm=0;
441   TRP_ROUTE **comm_entries=NULL;
442   size_t n_entries=0;
443   size_t ii_ret=0;
444
445   *n_out=trp_rtable_size(rtbl);
446   if (*n_out==0)
447     return NULL;
448
449   ret=talloc_array(NULL, TRP_ROUTE *, *n_out);
450   if (ret==NULL) {
451     tr_crit("trp_rtable_get_entries: unable to allocate return array.");
452     *n_out=0;
453     return NULL;
454   }
455
456   ii_ret=0; /* counts output entries */
457   comm=trp_rtable_get_comms(rtbl, &n_comm);
458   while(n_comm--) {
459     comm_entries=trp_rtable_get_comm_entries(rtbl, comm[n_comm], &n_entries);
460     while (n_entries--)
461       ret[ii_ret++]=comm_entries[n_entries];
462     talloc_free(comm_entries);
463   }
464   talloc_free(comm);
465
466   if (ii_ret!=*n_out) {
467     tr_crit("trp_rtable_get_entries: found incorrect number of entries.");
468     talloc_free(ret);
469     *n_out=0;
470     return NULL;
471   }
472   return ret;
473 }
474
475 /* Returns an array of pointers to TR_NAME, length of array in n_out.
476  * Caller must free the array (in the talloc NULL context). */
477 TR_NAME **trp_rtable_get_comms(TRP_RTABLE *rtbl, size_t *n_out)
478 {
479   size_t len=g_hash_table_size(rtbl); /* known comms are keys in top level hash table */
480   size_t ii=0;
481   GList *comms=NULL;;
482   GList *p=NULL;
483   TR_NAME **ret=NULL;
484
485   if (len==0) {
486     *n_out=0;
487     return NULL;
488   }
489     
490   ret=talloc_array(NULL, TR_NAME *, len);
491   if (ret==NULL) {
492     tr_crit("trp_rtable_get_comms: unable to allocate return array.");
493     *n_out=0;
494     return NULL;
495   }
496   comms=g_hash_table_get_keys(rtbl);
497   for (ii=0,p=comms; p!=NULL; ii++,p=g_list_next(p))
498     ret[ii]=(TR_NAME *)p->data;
499
500   g_list_free(comms);
501
502   *n_out=len;
503   return ret;
504 }
505
506 /* Returns an array of pointers to TR_NAME, length of array in n_out.
507  * Caller must free the array (in the talloc NULL context). */
508 TR_NAME **trp_rtable_get_comm_realms(TRP_RTABLE *rtbl, TR_NAME *comm, size_t *n_out)
509 {
510   size_t ii=0;
511   TRP_RTABLE *comm_tbl=g_hash_table_lookup(rtbl, comm);;
512   GList *entries=NULL;
513   GList *p=NULL;
514   TR_NAME **ret=NULL;
515
516   if (comm_tbl==NULL) {
517     *n_out=0;
518     return NULL;
519   }
520   *n_out=g_hash_table_size(comm_tbl); /* set output length */
521   ret=talloc_array(NULL, TR_NAME *, *n_out);
522   entries=g_hash_table_get_keys(comm_tbl);
523   for (ii=0,p=entries; p!=NULL; ii++,p=g_list_next(p))
524     ret[ii]=(TR_NAME *)p->data;
525
526   g_list_free(entries);
527   return ret;
528 }
529
530 /* Get all entries in an comm. Returns an array of pointers in NULL talloc context.
531  * Caller must free this list with talloc_free, but must not free the entries in the
532  * list.. */
533 TRP_ROUTE **trp_rtable_get_comm_entries(TRP_RTABLE *rtbl, TR_NAME *comm, size_t *n_out)
534 {
535   size_t ii=0, jj=0;
536   TR_NAME **realm=NULL;
537   size_t n_realms=0;
538   TRP_ROUTE **realm_entries=NULL;
539   size_t n_entries=0;
540   TRP_ROUTE **ret=NULL;
541   size_t ii_ret=0;
542
543   *n_out=trp_rtable_comm_size(rtbl, comm);
544   if (*n_out==0)
545     return NULL;
546
547   ret=talloc_array(NULL, TRP_ROUTE *, *n_out);
548   if (ret==NULL) {
549     tr_crit("trp_rtable_get_comm_entries: could not allocate return array.");
550     *n_out=0;
551     return NULL;
552   }
553   
554   ii_ret=0; /* counts entries in the output array */
555   realm=trp_rtable_get_comm_realms(rtbl, comm, &n_realms);
556   for (ii=0; ii<n_realms; ii++) {
557     realm_entries=trp_rtable_get_realm_entries(rtbl, comm, realm[ii], &n_entries);
558     for (jj=0; jj<n_entries; jj++)
559       ret[ii_ret++]=realm_entries[jj];
560     talloc_free(realm_entries);
561   }
562   talloc_free(realm);
563
564   if (ii_ret!=*n_out) {
565     tr_crit("trp_rtable_get_comm_entries: found incorrect number of entries.");
566     talloc_free(ret);
567     *n_out=0;
568     return NULL;
569   }
570
571   return ret;
572 }
573
574 /* Get all entries in an comm/realm. Returns an array of pointers in NULL talloc context.
575  * Caller must free this list with talloc_free, but must not free the entries in the
576  * list.. */
577 TRP_ROUTE **trp_rtable_get_realm_entries(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, size_t *n_out)
578 {
579   size_t ii=0;
580   TRP_ROUTE **ret=NULL;
581   TR_NAME **peer=NULL;
582
583   tr_debug("trp_rtable_get_realm_entries: entered.");
584   peer=trp_rtable_get_comm_realm_peers(rtbl, comm, realm, n_out);
585   ret=talloc_array(NULL, TRP_ROUTE *, *n_out);
586   if (ret==NULL) {
587     tr_crit("trp_rtable_get_realm_entries: could not allocate return array.");
588     talloc_free(peer);
589     n_out=0;
590     return NULL;
591   }
592   for (ii=0; ii<*n_out; ii++)
593     ret[ii]=trp_rtable_get_entry(rtbl, comm, realm, peer[ii]);
594   talloc_free(peer);
595   return ret;
596 }
597
598 TR_NAME **trp_rtable_get_comm_realm_peers(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, size_t *n_out)
599 {
600   TR_NAME **ret=NULL;
601   GHashTable *realm_tbl=NULL;
602   GList *keys=NULL;
603   GList *p=NULL;
604   size_t ii=0;
605
606   *n_out=trp_rtable_realm_size(rtbl, comm, realm);
607   if (*n_out==0)
608     return NULL;
609   realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
610   ret=talloc_array(NULL, TR_NAME *, *n_out);
611   if (ret==NULL) {
612     tr_crit("trp_rtable_get_comm_realm_peers: could not allocate return array.");
613     *n_out=0;
614     return NULL;
615   }
616   keys=g_hash_table_get_keys(realm_tbl);
617   for (ii=0,p=keys; p!=NULL; ii++,p=g_list_next(p))
618     ret[ii]=(TR_NAME *)p->data;
619   g_list_free(keys);
620   return ret;
621 }
622
623 /* Gets a single entry. Do not free it. */
624 TRP_ROUTE *trp_rtable_get_entry(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm, TR_NAME *peer)
625 {
626   GHashTable *realm_tbl=NULL;
627
628   realm_tbl=trp_rtable_get_realm_table(rtbl, comm, realm);
629   if (realm_tbl==NULL)
630     return NULL;
631
632   return g_hash_table_lookup(realm_tbl, peer); /* does not copy or increment ref count */
633 }
634
635 static char *timespec_to_str(struct timespec *ts)
636 {
637   struct tm tm;
638   char *s=NULL;
639
640   if (localtime_r(&(ts->tv_sec), &tm)==NULL)
641     return NULL;
642
643   s=malloc(40); /* long enough to contain strftime result */
644   if (s==NULL)
645     return NULL;
646
647   if (strftime(s, 40, "%F %T", &tm)==0) {
648     free(s);
649     return NULL;
650   }
651   return s;
652 }
653
654 TRP_ROUTE *trp_rtable_get_selected_entry(TRP_RTABLE *rtbl, TR_NAME *comm, TR_NAME *realm)
655 {
656   size_t n=0;
657   int ii=0;
658   TRP_ROUTE **entry=trp_rtable_get_realm_entries(rtbl, comm, realm, &n);
659   TRP_ROUTE *selected=NULL;
660
661   if (n==0)
662     return NULL;
663
664   tr_debug("trp_rtable_get_selected_entry: looking through route table entries for realm %.*s.",
665            realm->len, realm->buf);
666   for(ii=0; ii<n; ii++) {
667     if (trp_route_is_selected(entry[ii])) {
668       selected=entry[ii];
669       break;
670     }
671   }
672   tr_debug("trp_rtable_get_selected_entry: ii=%d.", ii);
673
674   talloc_free(entry);
675   return selected;
676 }
677
678 /* Pretty print a route table entry to a newly allocated string. If sep is NULL,
679  * returns comma+space separated string. */
680 char *trp_route_to_str(TALLOC_CTX *mem_ctx, TRP_ROUTE *entry, const char *sep)
681 {
682   char *comm=tr_name_strdup(entry->comm);
683   char *realm=tr_name_strdup(entry->realm);
684   char *peer=tr_name_strdup(entry->peer);
685   char *trust_router=tr_name_strdup(entry->trust_router);
686   char *next_hop=tr_name_strdup(entry->next_hop);
687   char *expiry=timespec_to_str(entry->expiry);
688   char *result=NULL;
689
690   if (sep==NULL)
691     sep=", ";
692
693   result=talloc_asprintf(mem_ctx,
694                          "%s%s%s%s%s%s%u%s%s%s%s%s%u%s%u%s%s%s%u",
695                          comm, sep,
696                          realm, sep,
697                          peer, sep,
698                          entry->metric, sep,
699                          trust_router, sep,
700                          next_hop, sep,
701                          entry->selected, sep,
702                          entry->local, sep,
703                          expiry, sep,
704                          entry->triggered);
705   free(comm);
706   free(realm);
707   free(peer);
708   free(trust_router);
709   free(next_hop);
710   free(expiry);
711   return result;
712 }
713
714 void trp_rtable_clear_triggered(TRP_RTABLE *rtbl)
715 {
716   size_t n_entries=0;
717   TRP_ROUTE **entries=trp_rtable_get_entries(rtbl, &n_entries);
718   size_t ii=0;
719
720   if (entries!=NULL) {
721     for (ii=0; ii<n_entries; ii++)
722       trp_route_set_triggered(entries[ii], 0);
723     talloc_free(entries);
724   }
725 }
726
727 static int sort_tr_names_cmp(const void *a, const void *b)
728 {
729   TR_NAME **n1=(TR_NAME **)a;
730   TR_NAME **n2=(TR_NAME **)b;
731   return tr_name_cmp(*n1, *n2);
732 }
733
734 static void sort_tr_names(TR_NAME **names, size_t n_names)
735 {
736   qsort(names, n_names, sizeof(TR_NAME *), sort_tr_names_cmp);
737 }
738
739 char *trp_rtable_to_str(TALLOC_CTX *mem_ctx, TRP_RTABLE *rtbl, const char *sep, const char *lineterm)
740 {
741   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
742   TR_NAME **comms=NULL;
743   size_t n_comms=0;
744   TR_NAME **realms=NULL;
745   size_t n_realms=0;
746   TRP_ROUTE **entries=NULL;
747   size_t n_entries=0;
748   char **tbl_strings=NULL;
749   size_t ii_tbl=0; /* counts tbl_strings */
750   size_t tbl_size=0;
751   size_t len=0;
752   size_t ii=0, jj=0, kk=0;
753   char *p=NULL;
754   char *result=NULL;
755
756   if (lineterm==NULL)
757     lineterm="\n";
758
759   tbl_size=trp_rtable_size(rtbl);
760   if (tbl_size==0) {
761     result=talloc_strdup(mem_ctx, lineterm);
762     goto cleanup;
763   }
764
765   tbl_strings=talloc_array(tmp_ctx, char *, tbl_size);
766   if (tbl_strings==NULL) {
767     result=talloc_strdup(mem_ctx, "error");
768     goto cleanup;
769   }
770   
771   comms=trp_rtable_get_comms(rtbl, &n_comms);
772   talloc_steal(tmp_ctx, comms);
773   sort_tr_names(comms, n_comms);
774   ii_tbl=0;
775   len=0;
776   for (ii=0; ii<n_comms; ii++) {
777     realms=trp_rtable_get_comm_realms(rtbl, comms[ii], &n_realms);
778     talloc_steal(tmp_ctx, realms);
779     sort_tr_names(realms, n_realms);
780     for (jj=0; jj<n_realms; jj++) {
781       entries=trp_rtable_get_realm_entries(rtbl, comms[ii], realms[jj], &n_entries);
782       talloc_steal(tmp_ctx, entries);
783       for (kk=0; kk<n_entries; kk++) {
784         tbl_strings[ii_tbl]=trp_route_to_str(tmp_ctx, entries[kk], sep);
785         len+=strlen(tbl_strings[ii_tbl]);
786         ii_tbl++;
787       }
788       talloc_free(entries);
789     }
790     talloc_free(realms);
791   }
792   talloc_free(comms);
793
794   /* now combine all the strings */
795   len += tbl_size*strlen(lineterm); /* space for line terminations*/
796   len += 1; /* nul terminator */
797   result=(char *)talloc_size(tmp_ctx, len);
798   for (p=result,ii=0; ii < tbl_size; ii++) {
799     p+=sprintf(p, "%s%s", tbl_strings[ii], lineterm);
800   }
801   talloc_steal(mem_ctx, result);
802   
803 cleanup:
804   talloc_free(tmp_ctx);
805   return result;
806 }