xref: /openbsd-src/usr.sbin/rpki-client/output-json.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /*	$OpenBSD: output-json.c,v 1.6 2019/12/04 23:03:05 benno Exp $ */
2 /*
3  * Copyright (c) 2019 Claudio Jeker <claudio@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <stdlib.h>
19 #include <openssl/ssl.h>
20 
21 #include "extern.h"
22 
23 int
24 output_json(FILE *out, struct vrp_tree *vrps)
25 {
26 	char		 buf[64];
27 	struct vrp	*v;
28 	int		 first = 1;
29 
30 	if (fprintf(out, "{\n\t\"roas\": [\n") < 0)
31 		return -1;
32 
33 	RB_FOREACH(v, vrp_tree, vrps) {
34 		if (first)
35 			first = 0;
36 		else {
37 			if (fprintf(out, ",\n") < 0)
38 				return -1;
39 		}
40 
41 		ip_addr_print(&v->addr, v->afi, buf, sizeof(buf));
42 
43 		if (fprintf(out, "\t\t{ \"asn\": \"AS%u\", \"prefix\": \"%s\", "
44 		    "\"maxLength\": %u, \"ta\": \"%s\" }",
45 		    v->asid, buf, v->maxlength, v->tal) < 0)
46 			return -1;
47 	}
48 
49 	if (fprintf(out, "\n\t]\n}\n") < 0)
50 		return -1;
51 	return 0;
52 }
53