]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_btoa.c
Merge branch 'stable/3.0' into tmp-3.0-master-merge
[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, ZCAP_NET_RAW, ZCAP_NET_ADMIN,
40 };
41
42 struct zebra_privs_t bgpd_privs = {
43 #if defined(FRR_USER) && defined(FRR_GROUP)
44 .user = FRR_USER,
45 .group = FRR_GROUP,
46 #endif
47 #ifdef VTY_GROUP
48 .vty_group = VTY_GROUP,
49 #endif
50 .caps_p = _caps_p,
51 .cap_num_p = array_size(_caps_p),
52 .cap_num_i = 0,
53 };
54
55 enum MRT_MSG_TYPES {
56 MSG_NULL,
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 */
69 };
70
71 static int attr_parse(struct stream *s, u_int16_t len)
72 {
73 u_int flag;
74 u_int type;
75 u_int16_t length;
76 u_int16_t lim;
77
78 lim = s->getp + len;
79
80 printf("attr_parse s->getp %zd, len %d, lim %d\n", s->getp, len, lim);
81
82 while (s->getp < lim) {
83 flag = stream_getc(s);
84 type = stream_getc(s);
85
86 if (flag & BGP_ATTR_FLAG_EXTLEN)
87 length = stream_getw(s);
88 else
89 length = stream_getc(s);
90
91 printf("FLAG: %d\n", flag);
92 printf("TYPE: %d\n", type);
93 printf("Len: %d\n", length);
94
95 switch (type) {
96 case BGP_ATTR_ORIGIN: {
97 u_char origin;
98 origin = stream_getc(s);
99 printf("ORIGIN: %d\n", origin);
100 } break;
101 case BGP_ATTR_AS_PATH: {
102 struct aspath *aspath;
103
104 aspath = aspath_parse(s, length, 1);
105 printf("ASPATH: %s\n", aspath->str);
106 aspath_free(aspath);
107 } break;
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));
112 } break;
113 default:
114 stream_getw_from(s, length);
115 break;
116 }
117 }
118
119 return 0;
120 }
121
122 int main(int argc, char **argv)
123 {
124 int ret;
125 FILE *fp;
126 struct stream *s;
127 time_t now;
128 int type;
129 int subtype;
130 size_t len;
131 int source_as;
132 int dest_as;
133 ifindex_t ifindex;
134 int family;
135 struct in_addr sip;
136 struct in_addr dip;
137 u_int16_t viewno, seq_num;
138 struct prefix_ipv4 p;
139
140 s = stream_new(10000);
141
142 if (argc != 2) {
143 fprintf(stderr, "Usage: %s FILENAME\n", argv[0]);
144 exit(1);
145 }
146 fp = fopen(argv[1], "r");
147 if (!fp) {
148 fprintf(stdout,
149 "%% Can't open configuration file %s due to '%s'.\n",
150 argv[1], safe_strerror(errno));
151 exit(1);
152 }
153
154 while (1) {
155 stream_reset(s);
156
157 ret = fread(s->data, 12, 1, fp);
158 if (!ret || feof(fp)) {
159 printf("END OF FILE\n");
160 break;
161 }
162 if (ferror(fp)) {
163 printf("ERROR OF FREAD\n");
164 break;
165 }
166
167 /* Extract header. */
168 now = stream_getl(s);
169 type = stream_getw(s);
170 subtype = stream_getw(s);
171 len = stream_getl(s);
172
173 printf("TIME: %s", ctime(&now));
174
175 /* printf ("TYPE: %d/%d\n", type, subtype); */
176
177 if (type == MSG_PROTOCOL_BGP4MP)
178 printf("TYPE: BGP4MP");
179 else if (type == MSG_PROTOCOL_BGP4MP_ET)
180 printf("TYPE: BGP4MP_ET");
181 else if (type == MSG_TABLE_DUMP)
182 printf("TYPE: MSG_TABLE_DUMP");
183 else
184 printf("TYPE: Unknown %d", type);
185
186 if (type == MSG_TABLE_DUMP)
187 switch (subtype) {
188 case AFI_IP:
189 printf("/AFI_IP\n");
190 break;
191 case AFI_IP6:
192 printf("/AFI_IP6\n");
193 break;
194 default:
195 printf("/UNKNOWN %d", subtype);
196 break;
197 }
198 else {
199 switch (subtype) {
200 case BGP4MP_STATE_CHANGE:
201 printf("/CHANGE\n");
202 break;
203 case BGP4MP_MESSAGE:
204 printf("/MESSAGE\n");
205 break;
206 case BGP4MP_ENTRY:
207 printf("/ENTRY\n");
208 break;
209 case BGP4MP_SNAPSHOT:
210 printf("/SNAPSHOT\n");
211 break;
212 default:
213 printf("/UNKNOWN %d", subtype);
214 break;
215 }
216 }
217
218 printf("len: %zd\n", len);
219
220 fread(s->data + 12, len, 1, fp);
221 if (feof(fp)) {
222 printf("ENDOF FILE 2\n");
223 break;
224 }
225 if (ferror(fp)) {
226 printf("ERROR OF FREAD 2\n");
227 break;
228 }
229
230 /* printf ("now read %d\n", len); */
231
232 if (type == MSG_TABLE_DUMP) {
233 u_char status;
234 time_t originated;
235 struct in_addr peer;
236 u_int16_t attrlen;
237
238 viewno = stream_getw(s);
239 seq_num = stream_getw(s);
240 printf("VIEW: %d\n", viewno);
241 printf("SEQUENCE: %d\n", seq_num);
242
243 /* start */
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),
248 p.prefixlen);
249
250 status = stream_getc(s);
251 originated = stream_getl(s);
252 peer.s_addr = stream_get_ipv4(s);
253 source_as = stream_getw(s);
254
255 printf("FROM: %s AS%d\n", inet_ntoa(peer),
256 source_as);
257 printf("ORIGINATED: %s", ctime(&originated));
258
259 attrlen = stream_getw(s);
260 printf("ATTRLEN: %d\n", attrlen);
261
262 attr_parse(s, attrlen);
263
264 printf("STATUS: 0x%x\n", status);
265 }
266 } else {
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);
271
272 ifindex = stream_getw(s);
273 family = stream_getw(s);
274
275 printf("ifindex: %d\n", ifindex);
276 printf("family: %d\n", family);
277
278 sip.s_addr = stream_get_ipv4(s);
279 dip.s_addr = stream_get_ipv4(s);
280
281 printf("saddr: %s\n", inet_ntoa(sip));
282 printf("daddr: %s\n", inet_ntoa(dip));
283
284 printf("\n");
285 }
286 }
287 fclose(fp);
288 return 0;
289 }