1 /* BGP dump to ascii converter
2 * Copyright (C) 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
32 #include "bgpd/bgpd.h"
33 #include "bgpd/bgp_dump.h"
34 #include "bgpd/bgp_attr.h"
35 #include "bgpd/bgp_aspath.h"
38 static zebra_capabilities_t _caps_p
[] = {
39 ZCAP_BIND
, ZCAP_NET_RAW
, ZCAP_NET_ADMIN
,
42 struct zebra_privs_t bgpd_privs
= {
43 #if defined(FRR_USER) && defined(FRR_GROUP)
48 .vty_group
= VTY_GROUP
,
51 .cap_num_p
= array_size(_caps_p
),
57 MSG_START
, /* sender is starting up */
58 MSG_DIE
, /* receiver should shut down */
59 MSG_I_AM_DEAD
, /* sender is shutting down */
60 MSG_PEER_DOWN
, /* sender's peer is down */
61 MSG_PROTOCOL_BGP
, /* msg is a BGP packet */
62 MSG_PROTOCOL_RIP
, /* msg is a RIP packet */
63 MSG_PROTOCOL_IDRP
, /* msg is an IDRP packet */
64 MSG_PROTOCOL_RIPNG
, /* msg is a RIPNG packet */
65 MSG_PROTOCOL_BGP4PLUS
, /* msg is a BGP4+ packet */
66 MSG_PROTOCOL_BGP4PLUS_01
, /* msg is a BGP4+ (draft 01) packet */
67 MSG_PROTOCOL_OSPF
, /* msg is an OSPF packet */
68 MSG_TABLE_DUMP
/* routing table dump */
71 static void attr_parse(struct stream
*s
, uint16_t len
)
80 printf("attr_parse s->getp %zd, len %d, lim %d\n", s
->getp
, len
, lim
);
82 while (s
->getp
< lim
) {
83 flag
= stream_getc(s
);
84 type
= stream_getc(s
);
86 if (flag
& BGP_ATTR_FLAG_EXTLEN
)
87 length
= stream_getw(s
);
89 length
= stream_getc(s
);
91 printf("FLAG: %d\n", flag
);
92 printf("TYPE: %d\n", type
);
93 printf("Len: %d\n", length
);
96 case BGP_ATTR_ORIGIN
: {
98 origin
= stream_getc(s
);
99 printf("ORIGIN: %d\n", origin
);
101 case BGP_ATTR_AS_PATH
: {
102 struct aspath
*aspath
;
104 aspath
= aspath_parse(s
, length
, 1);
105 printf("ASPATH: %s\n", aspath
->str
);
108 case BGP_ATTR_NEXT_HOP
: {
109 struct in_addr nexthop
;
110 nexthop
.s_addr
= stream_get_ipv4(s
);
111 printf("NEXTHOP: %s\n", inet_ntoa(nexthop
));
114 stream_getw_from(s
, length
);
120 int main(int argc
, char **argv
)
135 uint16_t viewno
, seq_num
;
136 struct prefix_ipv4 p
;
138 s
= stream_new(10000);
141 fprintf(stderr
, "Usage: %s FILENAME\n", argv
[0]);
144 fd
= open(argv
[1], O_RDONLY
);
147 "%% Can't open configuration file %s due to '%s'.\n",
148 argv
[1], safe_strerror(errno
));
155 ret
= stream_read(s
, fd
, 12);
158 printf("END OF FILE\n");
160 printf("ERROR OF READ\n");
162 printf("UNDERFLOW\n");
166 /* Extract header. */
167 now
= stream_getl(s
);
168 type
= stream_getw(s
);
169 subtype
= stream_getw(s
);
170 len
= stream_getl(s
);
172 printf("TIME: %s", ctime(&now
));
174 /* printf ("TYPE: %d/%d\n", type, subtype); */
176 if (type
== MSG_PROTOCOL_BGP4MP
)
177 printf("TYPE: BGP4MP");
178 else if (type
== MSG_PROTOCOL_BGP4MP_ET
)
179 printf("TYPE: BGP4MP_ET");
180 else if (type
== MSG_TABLE_DUMP
)
181 printf("TYPE: MSG_TABLE_DUMP");
183 printf("TYPE: Unknown %d", type
);
185 if (type
== MSG_TABLE_DUMP
)
191 printf("/AFI_IP6\n");
194 printf("/UNKNOWN %d", subtype
);
199 case BGP4MP_STATE_CHANGE
:
203 printf("/MESSAGE\n");
208 case BGP4MP_SNAPSHOT
:
209 printf("/SNAPSHOT\n");
212 printf("/UNKNOWN %d", subtype
);
217 printf("len: %zd\n", len
);
219 ret
= stream_read(s
, fd
, len
);
220 if (ret
!= (int)len
) {
222 printf("END OF FILE 2\n");
224 printf("ERROR OF READ 2\n");
226 printf("UNDERFLOW 2\n");
230 /* printf ("now read %d\n", len); */
232 if (type
== MSG_TABLE_DUMP
) {
238 viewno
= stream_getw(s
);
239 seq_num
= stream_getw(s
);
240 printf("VIEW: %d\n", viewno
);
241 printf("SEQUENCE: %d\n", seq_num
);
244 while (s
->getp
< len
- 16) {
245 p
.prefix
.s_addr
= stream_get_ipv4(s
);
246 p
.prefixlen
= stream_getc(s
);
247 printf("PREFIX: %s/%d\n", inet_ntoa(p
.prefix
),
250 status
= stream_getc(s
);
251 originated
= stream_getl(s
);
252 peer
.s_addr
= stream_get_ipv4(s
);
253 source_as
= stream_getw(s
);
255 printf("FROM: %s AS%d\n", inet_ntoa(peer
),
257 printf("ORIGINATED: %s", ctime(&originated
));
259 attrlen
= stream_getw(s
);
260 printf("ATTRLEN: %d\n", attrlen
);
262 attr_parse(s
, attrlen
);
264 printf("STATUS: 0x%x\n", status
);
267 source_as
= stream_getw(s
);
268 dest_as
= stream_getw(s
);
269 printf("source_as: %d\n", source_as
);
270 printf("dest_as: %d\n", dest_as
);
272 ifindex
= stream_getw(s
);
273 family
= stream_getw(s
);
275 printf("ifindex: %d\n", ifindex
);
276 printf("family: %d\n", family
);
278 sip
.s_addr
= stream_get_ipv4(s
);
279 dip
.s_addr
= stream_get_ipv4(s
);
281 printf("saddr: %s\n", inet_ntoa(sip
));
282 printf("daddr: %s\n", inet_ntoa(dip
));