1 /*	$OpenBSD: output-bgpd.c,v 1.31 2024/04/08 14:02:13 tb Exp $ */
2 /*
3  * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
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 
20 #include "extern.h"
21 
22 int
23 output_bgpd(FILE *out, struct vrp_tree *vrps, struct brk_tree *brks,
24     struct vap_tree *vaps, struct vsp_tree *vsps, struct stats *st)
25 {
26 	struct vrp	*vrp;
27 	struct vap	*vap;
28 	size_t		 i;
29 
30 	if (outputheader(out, st) < 0)
31 		return -1;
32 
33 	if (fprintf(out, "roa-set {\n") < 0)
34 		return -1;
35 
36 	RB_FOREACH(vrp, vrp_tree, vrps) {
37 		char ipbuf[64], maxlenbuf[100];
38 
39 		ip_addr_print(&vrp->addr, vrp->afi, ipbuf, sizeof(ipbuf));
40 		if (vrp->maxlength > vrp->addr.prefixlen) {
41 			int ret = snprintf(maxlenbuf, sizeof(maxlenbuf),
42 			    "maxlen %u ", vrp->maxlength);
43 			if (ret < 0 || (size_t)ret > sizeof(maxlenbuf))
44 				return -1;
45 		} else
46 			maxlenbuf[0] = '\0';
47 		if (fprintf(out, "\t%s %ssource-as %u expires %lld\n",
48 		    ipbuf, maxlenbuf, vrp->asid, (long long)vrp->expires) < 0)
49 			return -1;
50 	}
51 
52 	if (fprintf(out, "}\n") < 0)
53 		return -1;
54 
55 	if (excludeaspa)
56 		return 0;
57 
58 	if (fprintf(out, "\naspa-set {\n") < 0)
59 		return -1;
60 	RB_FOREACH(vap, vap_tree, vaps) {
61 		if (vap->overflowed)
62 			continue;
63 		if (fprintf(out, "\tcustomer-as %d expires %lld "
64 		    "provider-as { ", vap->custasid,
65 		    (long long)vap->expires) < 0)
66 			return -1;
67 		for (i = 0; i < vap->providersz; i++) {
68 			if (fprintf(out, "%u", vap->providers[i]) < 0)
69 				return -1;
70 			if (i + 1 < vap->providersz)
71 				if (fprintf(out, ", ") < 0)
72 					return -1;
73 		}
74 
75 		if (fprintf(out, " }\n") < 0)
76 			return -1;
77 	}
78 	if (fprintf(out, "}\n") < 0)
79 		return -1;
80 
81 	return 0;
82 }
83