From 1d5b375cc3c68533f89a524abb00dcc316df97db Mon Sep 17 00:00:00 2001 From: Chris Caputo Date: Mon, 9 Mar 2020 02:08:36 +0000 Subject: [PATCH] Enable aggregate support with '-F' (user-defined). --- README.md | 1 + bgpq4.8 | 1 + bgpq4.c | 12 ------------ bgpq4_printer.c | 28 ++++++++++++++++++++++++---- sx_prefix.c | 9 ++++++++- sx_prefix.h | 3 ++- 6 files changed, 36 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index b00b54e..ef8f6fa 100644 --- a/README.md +++ b/README.md @@ -290,6 +290,7 @@ example below: ipfw add pass all from 193.193.192.0/19 to any Recognized format characters: '%n' - network, '%l' - mask length, +'%a' - aggregate low mask length, '%A' - aggregate high mask length, '%N' - object name, '%m' - object mask and '%i' - inversed mask. Recognized escape characters: '\n' - new line, '\t' - tabulation. Please note that no new lines inserted automatically after each sentence, diff --git a/bgpq4.8 b/bgpq4.8 index 2ef891b..801bc86 100644 --- a/bgpq4.8 +++ b/bgpq4.8 @@ -251,6 +251,7 @@ ipfw add pass all from 193.193.192.0/19 to any .fi .Pp Recognized format characters: %n - network, %l - mask length, +%a - aggregate low mask length, %A - aggregate high mask length, %N - object name, %m - object mask and %i - inversed mask. Recognized escape characters: \\n - new line, \\t - tabulation. Please note that no new lines inserted automatically after each sentence, diff --git a/bgpq4.c b/bgpq4.c index 7a3605b..a1bfdeb 100644 --- a/bgpq4.c +++ b/bgpq4.c @@ -459,12 +459,6 @@ main(int argc, char* argv[]) sx_report(SX_FATAL, "Sorry, only prefix-lists supported in formatted " "output\n"); - if (expander.vendor == V_FORMAT && (refine || refineLow)) { - sx_report(SX_FATAL, "Sorry, formatted output (-F ) in not " - "compatible with -R/-r options\n"); - exit(1); - } - if (expander.vendor == V_HUAWEI && expander.generation != T_ASPATH && expander.generation != T_OASPATH && expander.generation != T_PREFIXLIST) sx_report(SX_FATAL, "Sorry, only as-paths and prefix-lists supported " @@ -486,12 +480,6 @@ main(int argc, char* argv[]) exit(1); } - if(aggregate && expander.vendor == V_FORMAT) { - sx_report(SX_FATAL, "Sorry, aggregation (-A) is not compatible with " - "formatted output (-F )\n"); - exit(1); - } - if (aggregate && (expander.vendor == V_NOKIA_MD || expander.vendor == V_NOKIA) && expander.generation != T_PREFIXLIST) { sx_report(SX_FATAL, "Sorry, aggregation (-A) is not supported with " diff --git a/bgpq4_printer.c b/bgpq4_printer.c index 8c8165c..030275f 100644 --- a/bgpq4_printer.c +++ b/bgpq4_printer.c @@ -1638,14 +1638,34 @@ bgpq4_print_format_prefix(struct sx_radix_node* n, void* ff) struct bgpq_expander* b = fpc->b; if (n->isGlue) - return; + goto checkSon; if (!f) f = stdout; - sx_prefix_snprintf_fmt(n->prefix, f, - b->name ? b->name : "NN", - b->format); + if (!n->isAggregate) { + sx_prefix_snprintf_fmt(n->prefix, f, + b->name ? b->name : "NN", + b->format, + n->prefix->masklen, + n->prefix->masklen); + } else if (n->aggregateLow > n->prefix->masklen) { + sx_prefix_snprintf_fmt(n->prefix, f, + b->name ? b->name : "NN", + b->format, + n->aggregateLow, + n->aggregateHi); + } else { + sx_prefix_snprintf_fmt(n->prefix, f, + b->name ? b->name : "NN", + b->format, + n->prefix->masklen, + n->aggregateHi); + } + +checkSon: + if (n->son) + bgpq4_print_format_prefix(n->son, ff); } diff --git a/sx_prefix.c b/sx_prefix.c index 6a41aec..3f09b35 100644 --- a/sx_prefix.c +++ b/sx_prefix.c @@ -401,7 +401,8 @@ sx_prefix_snprintf(struct sx_prefix* p, char* rbuffer, int srb) void sx_prefix_snprintf_fmt(struct sx_prefix* p, FILE* f, - const char* name, const char* format) + const char* name, const char* format, + unsigned int aggregateLow, unsigned int aggregateHi) { unsigned off = 0; const char* c = format; @@ -423,6 +424,12 @@ sx_prefix_snprintf_fmt(struct sx_prefix* p, FILE* f, case 'l': fprintf(f, "%i", p->masklen); break; + case 'a': + fprintf(f, "%u", aggregateLow); + break; + case 'A': + fprintf(f, "%u", aggregateHi); + break; case '%': fprintf(f, "%%"); break; diff --git a/sx_prefix.h b/sx_prefix.h index c211996..507317d 100644 --- a/sx_prefix.h +++ b/sx_prefix.h @@ -52,7 +52,8 @@ int sx_prefix_fprint(FILE* f, struct sx_prefix* p); int sx_prefix_snprintf(struct sx_prefix* p, char* rbuffer, int srb); int sx_prefix_snprintf_sep(struct sx_prefix* p, char* rbuffer, int srb, char*); void sx_prefix_snprintf_fmt(struct sx_prefix* p, FILE* f, - const char* name, const char* fmt); + const char* name, const char* fmt, + unsigned int aggregateLow, unsigned int aggregateHi); int sx_prefix_jsnprintf(struct sx_prefix* p, char* rbuffer, int srb); struct sx_radix_tree* sx_radix_tree_new(int af); struct sx_radix_node* sx_radix_node_new(struct sx_prefix* prefix);