[bgpd] Pass NOSUB to regexec
[jleu-quagga.git] / bgpd / bgp_btoa.c
blob7c708814514e80cea698e6aec9d3097ccd1327f7
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
9 later version.
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
17 along with GNU Zebra; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
21 #include <zebra.h>
23 #include "zebra.h"
24 #include "stream.h"
25 #include "log.h"
26 #include "prefix.h"
27 #include "command.h"
29 #include "bgpd/bgpd.h"
30 #include "bgpd/bgp_dump.h"
31 #include "bgpd/bgp_attr.h"
32 #include "bgpd/bgp_aspath.h"
34 enum MRT_MSG_TYPES {
35 MSG_NULL,
36 MSG_START, /* sender is starting up */
37 MSG_DIE, /* receiver should shut down */
38 MSG_I_AM_DEAD, /* sender is shutting down */
39 MSG_PEER_DOWN, /* sender's peer is down */
40 MSG_PROTOCOL_BGP, /* msg is a BGP packet */
41 MSG_PROTOCOL_RIP, /* msg is a RIP packet */
42 MSG_PROTOCOL_IDRP, /* msg is an IDRP packet */
43 MSG_PROTOCOL_RIPNG, /* msg is a RIPNG packet */
44 MSG_PROTOCOL_BGP4PLUS, /* msg is a BGP4+ packet */
45 MSG_PROTOCOL_BGP4PLUS_01, /* msg is a BGP4+ (draft 01) packet */
46 MSG_PROTOCOL_OSPF, /* msg is an OSPF packet */
47 MSG_TABLE_DUMP /* routing table dump */
50 int
51 attr_parse (struct stream *s, u_int16_t len)
53 u_int flag;
54 u_int type;
55 u_int16_t length;
56 u_int16_t lim;
58 lim = s->getp + len;
60 printf ("attr_parse s->getp %d, len %d, lim %d\n", s->getp, len, lim);
62 while (s->getp < lim)
64 flag = stream_getc (s);
65 type = stream_getc (s);
67 if (flag & ATTR_FLAG_EXTLEN)
68 length = stream_getw (s);
69 else
70 length = stream_getc (s);
72 printf ("FLAG: %d\n", flag);
73 printf ("TYPE: %d\n", type);
74 printf ("Len: %d\n", length);
76 switch (type)
78 case BGP_ATTR_ORIGIN:
80 u_char origin;
81 origin = stream_getc (s);
82 printf ("ORIGIN: %d\n", origin);
84 break;
85 case BGP_ATTR_AS_PATH:
87 struct aspath aspath;
89 aspath.data = (s->data + s->getp);
90 aspath.length = length;
91 aspath.str = aspath_make_str_count (&aspath);
92 printf ("ASPATH: %s\n", aspath.str);
93 free (aspath.str);
95 stream_forward (s, length);
97 break;
98 case BGP_ATTR_NEXT_HOP:
100 struct in_addr nexthop;
101 nexthop.s_addr = stream_get_ipv4 (s);
102 printf ("NEXTHOP: %s\n", inet_ntoa (nexthop));
103 /* stream_forward (s, length); */
105 break;
106 default:
107 stream_forward (s, length);
108 break;
112 return 0;
116 main (int argc, char **argv)
118 int ret;
119 FILE *fp;
120 struct stream *s;
121 time_t now;
122 int type;
123 int subtype;
124 int len;
125 int source_as;
126 int dest_as;
127 int ifindex;
128 int family;
129 struct in_addr sip;
130 struct in_addr dip;
131 u_int16_t viewno, seq_num;
132 struct prefix_ipv4 p;
134 s = stream_new (10000);
136 if (argc != 2)
138 fprintf (stderr, "Usage: %s FILENAME\n", argv[0]);
139 exit (1);
141 fp = fopen (argv[1], "r");
142 if (!fp)
144 perror ("fopen");
145 exit (1);
148 while (1)
150 stream_reset (s);
152 ret = fread (s->data, 12, 1, fp);
153 if (feof (fp))
155 printf ("END OF FILE\n");
156 break;
158 if (ferror (fp))
160 printf ("ERROR OF FREAD\n");
161 break;
164 /* Extract header. */
165 now = stream_getl (s);
166 type = stream_getw (s);
167 subtype = stream_getw (s);
168 len = stream_getl (s);
170 printf ("TIME: %s", ctime (&now));
172 /* printf ("TYPE: %d/%d\n", type, subtype); */
174 if (type == MSG_PROTOCOL_BGP4MP)
175 printf ("TYPE: BGP4MP");
176 else if (type == MSG_TABLE_DUMP)
177 printf ("TYPE: MSG_TABLE_DUMP");
178 else
179 printf ("TYPE: Unknown %d", type);
181 if (type == MSG_TABLE_DUMP)
182 switch (subtype)
184 case AFI_IP:
185 printf ("/AFI_IP\n");
186 break;
187 case AFI_IP6:
188 printf ("/AFI_IP6\n");
189 break;
190 default:
191 printf ("/UNKNOWN %d", subtype);
192 break;
194 else
196 switch (subtype)
198 case BGP4MP_STATE_CHANGE:
199 printf ("/CHANGE\n");
200 break;
201 case BGP4MP_MESSAGE:
202 printf ("/MESSAGE\n");
203 break;
204 case BGP4MP_ENTRY:
205 printf ("/ENTRY\n");
206 break;
207 case BGP4MP_SNAPSHOT:
208 printf ("/SNAPSHOT\n");
209 break;
210 default:
211 printf ("/UNKNOWN %d", subtype);
212 break;
216 printf ("len: %d\n", len);
218 ret = fread (s->data + 12, len, 1, fp);
219 if (feof (fp))
221 printf ("ENDOF FILE 2\n");
222 break;
224 if (ferror (fp))
226 printf ("ERROR OF FREAD 2\n");
227 break;
230 /* printf ("now read %d\n", len); */
232 if (type == MSG_TABLE_DUMP)
234 u_char status;
235 time_t originated;
236 struct in_addr peer;
237 u_int16_t attrlen;
239 viewno = stream_getw (s);
240 seq_num = stream_getw (s);
241 printf ("VIEW: %d\n", viewno);
242 printf ("SEQUENCE: %d\n", seq_num);
244 /* start */
245 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), p.prefixlen);
251 status = stream_getc (s);
252 originated = stream_getl (s);
253 peer.s_addr = stream_get_ipv4 (s);
254 source_as = stream_getw(s);
256 printf ("FROM: %s AS%d\n", inet_ntoa (peer), source_as);
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 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);
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));
286 printf ("\n");
289 fclose (fp);
290 return 0;