From: reschke Date: Wed, 10 Feb 2021 18:01:44 +0000 (+0100) Subject: support ROA export in JSON formatwq X-Git-Tag: v1.5.1~31^2~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=512bebdbd5a674a08d3bcc5bebf38e3934227d5a;p=thirdparty%2FFORT-validator.git support ROA export in JSON formatwq --- diff --git a/src/config.c b/src/config.c index 2d03935e..43cc8ca3 100644 --- a/src/config.c +++ b/src/config.c @@ -186,6 +186,8 @@ struct rpki_config { char *roa; /** File where the validated BGPsec certs will be stored */ char *bgpsec; + /** File where the validated ROAs will be stored in JSON format */ + char *roa_json; } output; /* ASN1 decoder max stack size allowed */ @@ -697,6 +699,13 @@ static const struct option_field options[] = { .offset = offsetof(struct rpki_config, output.bgpsec), .doc = "File where BGPsec Router Keys will be stored in CSV format, use '-' to print at console", .arg_doc = "", + }, { + .id = 6002, + .name = "output.roa.json", + .type = >_string, + .offset = offsetof(struct rpki_config, output.roa_json), + .doc = "File where ROAs will be stored in JSON format, use '-' to print at console", + .arg_doc = "", }, { @@ -976,6 +985,7 @@ set_default_values(void) } rpki_config.output.roa = NULL; + rpki_config.output.roa_json = NULL; rpki_config.output.bgpsec = NULL; rpki_config.asn1_decode_max_stack = 4096; /* 4kB */ @@ -1437,6 +1447,12 @@ config_get_output_roa(void) return rpki_config.output.roa; } +char const * +config_get_output_roa_json(void) +{ + return rpki_config.output.roa_json; +} + char const * config_get_output_bgpsec(void) { diff --git a/src/config.h b/src/config.h index 0a8c8d64..e7772167 100644 --- a/src/config.h +++ b/src/config.h @@ -48,6 +48,7 @@ unsigned int config_get_http_priority(void); unsigned int config_get_http_retry_count(void); unsigned int config_get_http_retry_interval(void); char const *config_get_output_roa(void); +char const *config_get_output_roa_json(void); char const *config_get_output_bgpsec(void); unsigned int config_get_asn1_decode_max_stack(void); unsigned int config_get_stale_repository_period(void); diff --git a/src/output_printer.c b/src/output_printer.c index 8dfd41fa..18c98b41 100644 --- a/src/output_printer.c +++ b/src/output_printer.c @@ -62,6 +62,37 @@ print_roa(struct vrp const *vrp, void *arg) return 0; } +typedef struct json_out { FILE *file; int first; } JSON_OUT; + +static int +print_roa_json(struct vrp const *vrp, void *arg) +{ + JSON_OUT *json_out = arg; + FILE *out = json_out->file; + + if (!json_out->first) + fprintf(out, ","); + + + switch (vrp->addr_fam) { + case AF_INET: + fprintf(out, "\n { \"asn\" : \"AS%u\", \"prefix\" : \"%s/%u\", \"maxLength\" : %u }", vrp->asn, + addr2str4(&vrp->prefix.v4, addr_buf), vrp->prefix_length, + vrp->max_prefix_length); + break; + case AF_INET6: + fprintf(out, "\n { \"asn\" : \"AS%u\", \"prefix\" : \"%s/%u\", \"maxLength\" : %u }", vrp->asn, + addr2str6(&vrp->prefix.v6, addr_buf), vrp->prefix_length, + vrp->max_prefix_length); + break; + default: + pr_crit("Unknown family type"); + } + + json_out->first = 0; + return 0; +} + /* Print as base64url strings without trailing pad */ static int print_router_key(struct router_key const *key, void *arg) @@ -122,6 +153,31 @@ print_roas(struct db_table *db) pr_op_err("Error printing ROAs"); } +static void +print_roas_json(struct db_table *db) +{ + FILE *out; + bool fopen; + int error; + + out = NULL; + error = open_file(config_get_output_roa_json(), &out, &fopen); + if (error) + return; + + JSON_OUT json_out; + json_out.file = out; + json_out.first = 1; + + fprintf(out, "{ \"roas\" : ["); + error = db_table_foreach_roa(db, print_roa_json, &json_out); + fprintf(out, "\n]}\n"); + if (fopen) + file_close(out); + if (error) + pr_op_err("Error printing ROAs"); +} + static void print_router_keys(struct db_table *db) { @@ -146,5 +202,6 @@ void output_print_data(struct db_table *db) { print_roas(db); + print_roas_json(db); print_router_keys(db); }