#include "crypto/base64.h"
#include "types/vrp.h"
-static char addr_buf[INET6_ADDRSTRLEN];
-
typedef struct json_out {
FILE *file;
bool first;
} JSON_OUT;
-static int
-load_output_file(char const *output, FILE **result, bool *fopen)
+static FILE *
+load_output_file(char const *filename)
{
- FILE *tmp;
- int error;
-
- if (output == NULL) {
- *result = NULL;
- return 0;
- }
+ FILE *out;
- *fopen = false;
- if (strcmp(output, "-") == 0) {
- *result = stdout;
- return 0;
- }
+ if (filename == NULL)
+ return NULL;
- error = file_write(output, "w", &tmp);
- if (error) {
- *result = NULL;
- return error;
- }
+ if (strcmp(filename, "-") == 0)
+ return stdout;
- *fopen = true;
- *result = tmp;
- return 0;
+ return (file_write(filename, "w", &out) == 0) ? out : NULL;
}
static int
print_roa_csv(struct vrp const *vrp, void *arg)
{
- FILE *out = arg;
+ char addr_buf[INET6_ADDRSTRLEN];
if (vrp->addr_fam != AF_INET && vrp->addr_fam != AF_INET6)
pr_crit("Unknown family type");
- fprintf(out, "AS%u,%s/%u,%u\n", vrp->asn,
+ fprintf(arg, "AS%u,%s/%u,%u\n", vrp->asn,
inet_ntop(vrp->addr_fam, &vrp->prefix, addr_buf, INET6_ADDRSTRLEN),
vrp->prefix_length, vrp->max_prefix_length);
{
JSON_OUT *json_out = arg;
FILE *out;
+ char addr_buf[INET6_ADDRSTRLEN];
out = json_out->file;
if (!json_out->first)
pr_crit("Unknown family type");
fprintf(out,
- "\n { \"asn\" : \"AS%u\", \"prefix\" : \"%s/%u\", \"maxLength\" : %u }",
+ "\n { \"asn\": \"AS%u\", \"prefix\": \"%s/%u\", \"maxLength\": %u }",
vrp->asn,
inet_ntop(vrp->addr_fam, &vrp->prefix, addr_buf, INET6_ADDRSTRLEN),
vrp->prefix_length,
static int
print_router_key_csv(struct router_key const *key, void *arg)
{
- FILE *out = arg;
char *buf1, *buf2;
if (!base64url_encode(key->ski, RK_SKI_LEN, &buf1)) {
return 0; /* Skip it, I guess */
}
- fprintf(out, "AS%u,%s,%s\n", key->as, buf1, buf2);
+ fprintf(arg, "AS%u,%s,%s\n", key->as, buf1, buf2);
free(buf2);
free(buf1);
fprintf(out, ",");
fprintf(out,
- "\n { \"asn\" : \"AS%u\", \"ski\" : \"%s\", \"spki\" : \"%s\" }",
+ "\n { \"asn\": \"AS%u\", \"ski\": \"%s\", \"spki\": \"%s\" }",
key->as,
buf1,
buf2);
return 0;
}
-static int
-open_file(char const *loc, FILE **out, bool *fopen)
-{
- int error;
-
- error = load_output_file(loc, out, fopen);
- if (error)
- return pr_op_err("Error getting file '%s'", loc);
-
- /* No output configured */
- if (*out == NULL)
- return -ENOENT;
-
- return 0;
-}
-
static void
print_roas(struct db_table const *db)
{
FILE *out;
JSON_OUT json_out;
- bool fopen;
int error;
- out = NULL;
- error = open_file(config_get_output_roa(), &out, &fopen);
- if (error)
+ out = load_output_file(config_get_output_roa());
+ if (out == NULL)
return;
if (config_get_output_format() == OFM_CSV) {
fprintf(out, "ASN,Prefix,Max prefix length\n");
error = db_table_foreach_roa(db, print_roa_csv, out);
+
} else {
json_out.file = out;
json_out.first = true;
fprintf(out, "\n]}\n");
}
- if (fopen)
- file_close(out);
if (error)
- pr_op_err("Error printing ROAs");
+ pr_op_err("Error printing ROAs: %s", strerror(error));
+ if (out != stdout)
+ file_close(out);
}
static void
{
FILE *out;
JSON_OUT json_out;
- bool fopen;
int error;
- out = NULL;
- error = open_file(config_get_output_bgpsec(), &out, &fopen);
- if (error)
+ out = load_output_file(config_get_output_bgpsec());
+ if (out == NULL)
return;
if (config_get_output_format() == OFM_CSV) {
- fprintf(out,
- "ASN,Subject Key Identifier,Subject Public Key Info\n");
- error = db_table_foreach_router_key(db, print_router_key_csv,
- out);
+ fprintf(out, "ASN,Subject Key Identifier,Subject Public Key Info\n");
+ error = db_table_foreach_router_key(db, print_router_key_csv, out);
+
} else {
json_out.file = out;
json_out.first = true;
fprintf(out, "{ \"router-keys\" : [");
- error = db_table_foreach_router_key(db, print_router_key_json,
- &json_out);
+ error = db_table_foreach_router_key(db, print_router_key_json, &json_out);
fprintf(out, "\n]}\n");
}
- if (fopen)
- file_close(out);
if (error)
- pr_op_err("Error printing Router Keys");
+ pr_op_err("Error printing Router Keys: %s", strerror(error));
+ if (out != stdout)
+ file_close(out);
}
void