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
[] = {
44 struct zebra_privs_t bgpd_privs
= {
45 #if defined(FRR_USER) && defined(FRR_GROUP)
50 .vty_group
= VTY_GROUP
,
53 .cap_num_p
= array_size(_caps_p
),
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 */
73 static int attr_parse(struct stream
*s
, u_int16_t len
)
82 printf("attr_parse s->getp %zd, len %d, lim %d\n", s
->getp
, len
, lim
);
84 while (s
->getp
< lim
) {
85 flag
= stream_getc(s
);
86 type
= stream_getc(s
);
88 if (flag
& BGP_ATTR_FLAG_EXTLEN
)
89 length
= stream_getw(s
);
91 length
= stream_getc(s
);
93 printf("FLAG: %d\n", flag
);
94 printf("TYPE: %d\n", type
);
95 printf("Len: %d\n", length
);
98 case BGP_ATTR_ORIGIN
: {
100 origin
= stream_getc(s
);
101 printf("ORIGIN: %d\n", origin
);
103 case BGP_ATTR_AS_PATH
: {
104 struct aspath
*aspath
;
106 aspath
= aspath_parse(s
, length
, 1);
107 printf("ASPATH: %s\n", aspath
->str
);
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
));
116 stream_getw_from(s
, length
);
124 int main(int argc
, char **argv
)
139 u_int16_t viewno
, seq_num
;
140 struct prefix_ipv4 p
;
142 s
= stream_new(10000);
145 fprintf(stderr
, "Usage: %s FILENAME\n", argv
[0]);
148 fp
= fopen(argv
[1], "r");
151 "%% Can't open configuration file %s due to '%s'.\n",
152 argv
[1], safe_strerror(errno
));
159 ret
= fread(s
->data
, 12, 1, fp
);
160 if (!ret
|| feof(fp
)) {
161 printf("END OF FILE\n");
165 printf("ERROR OF FREAD\n");
169 /* Extract header. */
170 now
= stream_getl(s
);
171 type
= stream_getw(s
);
172 subtype
= stream_getw(s
);
173 len
= stream_getl(s
);
175 printf("TIME: %s", ctime(&now
));
177 /* printf ("TYPE: %d/%d\n", type, subtype); */
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");
186 printf("TYPE: Unknown %d", type
);
188 if (type
== MSG_TABLE_DUMP
)
194 printf("/AFI_IP6\n");
197 printf("/UNKNOWN %d", subtype
);
202 case BGP4MP_STATE_CHANGE
:
206 printf("/MESSAGE\n");
211 case BGP4MP_SNAPSHOT
:
212 printf("/SNAPSHOT\n");
215 printf("/UNKNOWN %d", subtype
);
220 printf("len: %zd\n", len
);
222 fread(s
->data
+ 12, len
, 1, fp
);
224 printf("ENDOF FILE 2\n");
228 printf("ERROR OF FREAD 2\n");
232 /* printf ("now read %d\n", len); */
234 if (type
== MSG_TABLE_DUMP
) {
240 viewno
= stream_getw(s
);
241 seq_num
= stream_getw(s
);
242 printf("VIEW: %d\n", viewno
);
243 printf("SEQUENCE: %d\n", seq_num
);
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
),
252 status
= stream_getc(s
);
253 originated
= stream_getl(s
);
254 peer
.s_addr
= stream_get_ipv4(s
);
255 source_as
= stream_getw(s
);
257 printf("FROM: %s AS%d\n", inet_ntoa(peer
),
259 printf("ORIGINATED: %s", ctime(&originated
));
261 attrlen
= stream_getw(s
);
262 printf("ATTRLEN: %d\n", attrlen
);
264 attr_parse(s
, attrlen
);
266 printf("STATUS: 0x%x\n", status
);
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
);
274 ifindex
= stream_getw(s
);
275 family
= stream_getw(s
);
277 printf("ifindex: %d\n", ifindex
);
278 printf("family: %d\n", family
);
280 sip
.s_addr
= stream_get_ipv4(s
);
281 dip
.s_addr
= stream_get_ipv4(s
);
283 printf("saddr: %s\n", inet_ntoa(sip
));
284 printf("daddr: %s\n", inet_ntoa(dip
));