]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_btoa.c
e29f72b747fa8a5abf694e1cb8ceae77024232e4
[mirror_frr.git] / bgpd / bgp_btoa.c
1 /* BGP dump to ascii converter
2 * Copyright (C) 1999 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "zebra.h"
24 #include "stream.h"
25 #include "log.h"
26 #include "prefix.h"
27 #include "command.h"
28 #include "memory.h"
29 #include "privs.h"
30 #include "filter.h"
31
32 #include "bgpd/bgpd.h"
33 #include "bgpd/bgp_dump.h"
34 #include "bgpd/bgp_attr.h"
35 #include "bgpd/bgp_aspath.h"
36
37 /* privileges */
38 static zebra_capabilities_t _caps_p[] = {
39 ZCAP_BIND,
40 ZCAP_NET_RAW,
41 ZCAP_NET_ADMIN,
42 };
43
44 struct zebra_privs_t bgpd_privs = {
45 #if defined(FRR_USER) && defined(FRR_GROUP)
46 .user = FRR_USER,
47 .group = FRR_GROUP,
48 #endif
49 #ifdef VTY_GROUP
50 .vty_group = VTY_GROUP,
51 #endif
52 .caps_p = _caps_p,
53 .cap_num_p = array_size(_caps_p),
54 .cap_num_i = 0,
55 };
56
57 enum MRT_MSG_TYPES {
58 MSG_NULL,
59 MSG_START, /* sender is starting up */
60 MSG_DIE, /* receiver should shut down */
61 MSG_I_AM_DEAD, /* sender is shutting down */
62 MSG_PEER_DOWN, /* sender's peer is down */
63 MSG_PROTOCOL_BGP, /* msg is a BGP packet */
64 MSG_PROTOCOL_RIP, /* msg is a RIP packet */
65 MSG_PROTOCOL_IDRP, /* msg is an IDRP packet */
66 MSG_PROTOCOL_RIPNG, /* msg is a RIPNG packet */
67 MSG_PROTOCOL_BGP4PLUS, /* msg is a BGP4+ packet */
68 MSG_PROTOCOL_BGP4PLUS_01, /* msg is a BGP4+ (draft 01) packet */
69 MSG_PROTOCOL_OSPF, /* msg is an OSPF packet */
70 MSG_TABLE_DUMP /* routing table dump */
71 };
72
73 static int attr_parse(struct stream *s, u_int16_t len)
74 {
75 u_int flag;
76 u_int type;
77 u_int16_t length;
78 u_int16_t lim;
79
80 lim = s->getp + len;
81
82 printf("attr_parse s->getp %zd, len %d, lim %d\n", s->getp, len, lim);
83
84 while (s->getp < lim) {
85 flag = stream_getc(s);
86 type = stream_getc(s);
87
88 if (flag & BGP_ATTR_FLAG_EXTLEN)
89 length = stream_getw(s);
90 else
91 length = stream_getc(s);
92
93 printf("FLAG: %d\n", flag);
94 printf("TYPE: %d\n", type);
95 printf("Len: %d\n", length);
96
97 switch (type) {
98 case BGP_ATTR_ORIGIN: {
99 u_char origin;
100 origin = stream_getc(s);
101 printf("ORIGIN: %d\n", origin);
102 } break;
103 case BGP_ATTR_AS_PATH: {
104 struct aspath *aspath;
105
106 aspath = aspath_parse(s, length, 1);
107 printf("ASPATH: %s\n", aspath->str);
108 aspath_free(aspath);
109 } break;
110 case BGP_ATTR_NEXT_HOP: {
111 struct in_addr nexthop;
112 nexthop.s_addr = stream_get_ipv4(s);
113 printf("NEXTHOP: %s\n", inet_ntoa(nexthop));
114 } break;
115 default:
116 stream_getw_from(s, length);
117 break;
118 }
119 }
120
121 return 0;
122 }
123
124 int main(int argc, char **argv)
125 {
126 int ret;
127 FILE *fp;
128 struct stream *s;
129 time_t now;
130 int type;
131 int subtype;
132 size_t len;
133 int source_as;
134 int dest_as;
135 ifindex_t ifindex;
136 int family;
137 struct in_addr sip;
138 struct in_addr dip;
139 u_int16_t viewno, seq_num;
140 struct prefix_ipv4 p;
141
142 s = stream_new(10000);
143
144 if (argc != 2) {
145 fprintf(stderr, "Usage: %s FILENAME\n", argv[0]);
146 exit(1);
147 }
148 fp = fopen(argv[1], "r");
149 if (!fp) {
150 fprintf(stdout,
151 "%% Can't open configuration file %s due to '%s'.\n",
152 argv[1], safe_strerror(errno));
153 exit(1);
154 }
155
156 while (1) {
157 stream_reset(s);
158
159 ret = fread(s->data, 12, 1, fp);
160 if (!ret || feof(fp)) {
161 printf("END OF FILE\n");
162 break;
163 }
164 if (ferror(fp)) {
165 printf("ERROR OF FREAD\n");
166 break;
167 }
168
169 /* Extract header. */
170 now = stream_getl(s);
171 type = stream_getw(s);
172 subtype = stream_getw(s);
173 len = stream_getl(s);
174
175 printf("TIME: %s", ctime(&now));
176
177 /* printf ("TYPE: %d/%d\n", type, subtype); */
178
179 if (type == MSG_PROTOCOL_BGP4MP)
180 printf("TYPE: BGP4MP");
181 else if (type == MSG_PROTOCOL_BGP4MP_ET)
182 printf("TYPE: BGP4MP_ET");
183 else if (type == MSG_TABLE_DUMP)
184 printf("TYPE: MSG_TABLE_DUMP");
185 else
186 printf("TYPE: Unknown %d", type);
187
188 if (type == MSG_TABLE_DUMP)
189 switch (subtype) {
190 case AFI_IP:
191 printf("/AFI_IP\n");
192 break;
193 case AFI_IP6:
194 printf("/AFI_IP6\n");
195 break;
196 default:
197 printf("/UNKNOWN %d", subtype);
198 break;
199 }
200 else {
201 switch (subtype) {
202 case BGP4MP_STATE_CHANGE:
203 printf("/CHANGE\n");
204 break;
205 case BGP4MP_MESSAGE:
206 printf("/MESSAGE\n");
207 break;
208 case BGP4MP_ENTRY:
209 printf("/ENTRY\n");
210 break;
211 case BGP4MP_SNAPSHOT:
212 printf("/SNAPSHOT\n");
213 break;
214 default:
215 printf("/UNKNOWN %d", subtype);
216 break;
217 }
218 }
219
220 printf("len: %zd\n", len);
221
222 fread(s->data + 12, len, 1, fp);
223 if (feof(fp)) {
224 printf("ENDOF FILE 2\n");
225 break;
226 }
227 if (ferror(fp)) {
228 printf("ERROR OF FREAD 2\n");
229 break;
230 }
231
232 /* printf ("now read %d\n", len); */
233
234 if (type == MSG_TABLE_DUMP) {
235 u_char status;
236 time_t originated;
237 struct in_addr peer;
238 u_int16_t attrlen;
239
240 viewno = stream_getw(s);
241 seq_num = stream_getw(s);
242 printf("VIEW: %d\n", viewno);
243 printf("SEQUENCE: %d\n", seq_num);
244
245 /* start */
246 while (s->getp < len - 16) {
247 p.prefix.s_addr = stream_get_ipv4(s);
248 p.prefixlen = stream_getc(s);
249 printf("PREFIX: %s/%d\n", inet_ntoa(p.prefix),
250 p.prefixlen);
251
252 status = stream_getc(s);
253 originated = stream_getl(s);
254 peer.s_addr = stream_get_ipv4(s);
255 source_as = stream_getw(s);
256
257 printf("FROM: %s AS%d\n", inet_ntoa(peer),
258 source_as);
259 printf("ORIGINATED: %s", ctime(&originated));
260
261 attrlen = stream_getw(s);
262 printf("ATTRLEN: %d\n", attrlen);
263
264 attr_parse(s, attrlen);
265
266 printf("STATUS: 0x%x\n", status);
267 }
268 } else {
269 source_as = stream_getw(s);
270 dest_as = stream_getw(s);
271 printf("source_as: %d\n", source_as);
272 printf("dest_as: %d\n", dest_as);
273
274 ifindex = stream_getw(s);
275 family = stream_getw(s);
276
277 printf("ifindex: %d\n", ifindex);
278 printf("family: %d\n", family);
279
280 sip.s_addr = stream_get_ipv4(s);
281 dip.s_addr = stream_get_ipv4(s);
282
283 printf("saddr: %s\n", inet_ntoa(sip));
284 printf("daddr: %s\n", inet_ntoa(dip));
285
286 printf("\n");
287 }
288 }
289 fclose(fp);
290 return 0;
291 }