From f6305dcb192628c5ae4b39362bec486184170ebf Mon Sep 17 00:00:00 2001 From: Jennifer Richards Date: Wed, 29 Jun 2016 21:47:07 -0400 Subject: [PATCH] Improved, sorted printing, provide _to_str methods. --- include/trp_rtable.h | 6 ++- trp/test/rtbl_test.c | 28 ++--------- trp/trp_rtable.c | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+), 27 deletions(-) diff --git a/include/trp_rtable.h b/include/trp_rtable.h index 51ed2f8..ff94414 100644 --- a/include/trp_rtable.h +++ b/include/trp_rtable.h @@ -10,9 +10,9 @@ typedef struct trp_rentry { TR_NAME *apc; TR_NAME *realm; - TR_NAME *trust_router; - unsigned int metric; TR_NAME *peer; + unsigned int metric; + TR_NAME *trust_router; TR_NAME *next_hop; int selected; struct timespec *expiry; @@ -34,6 +34,7 @@ TR_NAME **trp_rtable_get_apc_realms(TRP_RTABLE *rtbl, TR_NAME *apc, size_t *n_ou TRP_RENTRY **trp_rtable_get_realm_entries(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, size_t *n_out); TR_NAME **trp_rtable_get_apc_realm_peers(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, size_t *n_out); TRP_RENTRY *trp_rtable_get_entry(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, TR_NAME *peer); +char *trp_rtable_to_str(TALLOC_CTX *mem_ctx, TRP_RTABLE *rtbl, const char *sep, const char *lineterm); TRP_RENTRY *trp_rentry_new(TALLOC_CTX *mem_ctx); void trp_rentry_free(TRP_RENTRY *entry); @@ -53,5 +54,6 @@ void trp_rentry_set_selected(TRP_RENTRY *entry, int sel); int trp_rentry_get_selected(TRP_RENTRY *entry); void trp_rentry_set_expiry(TRP_RENTRY *entry, struct timespec *exp); struct timespec *trp_rentry_get_expiry(TRP_RENTRY *entry); +char *trp_rentry_to_str(TALLOC_CTX *mem_ctx, TRP_RENTRY *entry, const char *sep); #endif /* _TRP_RTABLE_H_ */ diff --git a/trp/test/rtbl_test.c b/trp/test/rtbl_test.c index 2c49f39..d2e3826 100644 --- a/trp/test/rtbl_test.c +++ b/trp/test/rtbl_test.c @@ -245,34 +245,12 @@ static void remove_entries(TRP_RTABLE *table) } } -static void print_rentry(TRP_RENTRY *entry) -{ - printf("{%s, %s, %s} = %s, %s, %d\n", - trp_rentry_get_apc(entry)->buf, - trp_rentry_get_realm(entry)->buf, - trp_rentry_get_peer(entry)->buf, - trp_rentry_get_trust_router(entry)->buf, - trp_rentry_get_next_hop(entry)->buf, - trp_rentry_get_metric(entry)); -} static void print_rtable(TRP_RTABLE *table) { - size_t ii=0, jj=0; - TRP_RENTRY **apc_entries=NULL; - size_t len=0; - TR_NAME *n=0; - - printf("Route Table\n"); - printf("{APC, REALM, peer } = TR_RTR, next, metric\n"); - for (ii=0; ii + #include #include +#include #include #include @@ -508,3 +511,137 @@ TRP_RENTRY *trp_rtable_get_entry(TRP_RTABLE *rtbl, TR_NAME *apc, TR_NAME *realm, return NULL; return g_hash_table_lookup(realm_tbl, peer); /* does not copy or increment ref count */ } + +static char *timespec_to_str(struct timespec *ts) +{ + struct tm tm; + char *s=NULL; + + if (localtime_r(&(ts->tv_sec), &tm)==NULL) + return NULL; + + s=malloc(40); /* long enough to contain strftime result */ + if (s==NULL) + return NULL; + + if (strftime(s, 40, "%F %T", &tm)==0) { + free(s); + return NULL; + } + return s; +} + +/* Pretty print a route table entry to a newly allocated string. If sep is NULL, + * returns comma+space separated string. */ +char *trp_rentry_to_str(TALLOC_CTX *mem_ctx, TRP_RENTRY *entry, const char *sep) +{ + char *apc=tr_name_strdup(entry->apc); + char *realm=tr_name_strdup(entry->realm); + char *peer=tr_name_strdup(entry->peer); + char *trust_router=tr_name_strdup(entry->trust_router); + char *next_hop=tr_name_strdup(entry->next_hop); + char *expiry=timespec_to_str(entry->expiry); + char *result=NULL; + + if (sep==NULL) + sep=", "; + + result=talloc_asprintf(mem_ctx, + "%s%s%s%s%s%s%u%s%s%s%s%s%d%s%s", + apc, sep, + realm, sep, + peer, sep, + entry->metric, sep, + trust_router, sep, + next_hop, sep, + entry->selected, sep, + expiry); + free(apc); + free(realm); + free(peer); + free(trust_router); + free(next_hop); + free(expiry); + return result; +} + +static int sort_tr_names_cmp(const void *a, const void *b) +{ + TR_NAME **n1=(TR_NAME **)a; + TR_NAME **n2=(TR_NAME **)b; + return tr_name_cmp(*n1, *n2); +} + +static void sort_tr_names(TR_NAME **names, size_t n_names) +{ + qsort(names, n_names, sizeof(TR_NAME *), sort_tr_names_cmp); +} + +char *trp_rtable_to_str(TALLOC_CTX *mem_ctx, TRP_RTABLE *rtbl, const char *sep, const char *lineterm) +{ + TALLOC_CTX *tmp_ctx=talloc_new(NULL); + TR_NAME **apcs=NULL; + size_t n_apcs=0; + TR_NAME **realms=NULL; + size_t n_realms=0; + TRP_RENTRY **entries=NULL; + size_t n_entries=0; + char **tbl_strings=NULL; + size_t ii_tbl=0; /* counts tbl_strings */ + size_t tbl_size=0; + size_t len=0; + size_t ii=0, jj=0, kk=0; + char *p=NULL; + char *result=NULL; + + if (lineterm==NULL) + lineterm="\n"; + + tbl_size=trp_rtable_size(rtbl); + if (tbl_size==0) { + result=talloc_strdup(mem_ctx, lineterm); + goto cleanup; + } + + tbl_strings=talloc_array(tmp_ctx, char *, tbl_size); + if (tbl_strings==NULL) { + result=talloc_strdup(mem_ctx, "error"); + goto cleanup; + } + + apcs=trp_rtable_get_apcs(rtbl, &n_apcs); + talloc_steal(tmp_ctx, apcs); + sort_tr_names(apcs, n_apcs); + ii_tbl=0; + len=0; + for (ii=0; ii