paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 |
| 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. */ |
| 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 | |
| 29 | #include "bgpd/bgpd.h" |
| 30 | #include "bgpd/bgp_dump.h" |
| 31 | #include "bgpd/bgp_attr.h" |
| 32 | #include "bgpd/bgp_aspath.h" |
| 33 | |
| 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 */ |
| 48 | }; |
| 49 | |
| 50 | int |
| 51 | attr_parse (struct stream *s, u_int16_t len) |
| 52 | { |
| 53 | u_int flag; |
| 54 | u_int type; |
| 55 | u_int16_t length; |
| 56 | u_int16_t lim; |
| 57 | |
| 58 | lim = s->getp + len; |
| 59 | |
| 60 | printf ("attr_parse s->getp %d, len %d, lim %d\n", s->getp, len, lim); |
| 61 | |
| 62 | while (s->getp < lim) |
| 63 | { |
| 64 | flag = stream_getc (s); |
| 65 | type = stream_getc (s); |
| 66 | |
| 67 | if (flag & ATTR_FLAG_EXTLEN) |
| 68 | length = stream_getw (s); |
| 69 | else |
| 70 | length = stream_getc (s); |
| 71 | |
| 72 | printf ("FLAG: %d\n", flag); |
| 73 | printf ("TYPE: %d\n", type); |
| 74 | printf ("Len: %d\n", length); |
| 75 | |
| 76 | switch (type) |
| 77 | { |
| 78 | case BGP_ATTR_ORIGIN: |
| 79 | { |
| 80 | u_char origin; |
| 81 | origin = stream_getc (s); |
| 82 | printf ("ORIGIN: %d\n", origin); |
| 83 | } |
| 84 | break; |
| 85 | case BGP_ATTR_AS_PATH: |
| 86 | { |
| 87 | struct aspath aspath; |
| 88 | |
| 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); |
| 94 | |
| 95 | stream_forward (s, length); |
| 96 | } |
| 97 | break; |
| 98 | case BGP_ATTR_NEXT_HOP: |
| 99 | { |
| 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); */ |
| 104 | } |
| 105 | break; |
| 106 | default: |
| 107 | stream_forward (s, length); |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | int |
| 116 | main (int argc, char **argv) |
| 117 | { |
| 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; |
| 133 | |
| 134 | s = stream_new (10000); |
| 135 | |
| 136 | if (argc != 2) |
| 137 | { |
| 138 | fprintf (stderr, "Usage: %s FILENAME\n", argv[0]); |
| 139 | exit (1); |
| 140 | } |
| 141 | fp = fopen (argv[1], "r"); |
| 142 | if (!fp) |
| 143 | { |
| 144 | perror ("fopen"); |
| 145 | exit (1); |
| 146 | } |
| 147 | |
| 148 | while (1) |
| 149 | { |
| 150 | stream_reset (s); |
| 151 | |
| 152 | ret = fread (s->data, 12, 1, fp); |
| 153 | if (feof (fp)) |
| 154 | { |
| 155 | printf ("END OF FILE\n"); |
| 156 | break; |
| 157 | } |
| 158 | if (ferror (fp)) |
| 159 | { |
| 160 | printf ("ERROR OF FREAD\n"); |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | /* Extract header. */ |
| 165 | now = stream_getl (s); |
| 166 | type = stream_getw (s); |
| 167 | subtype = stream_getw (s); |
| 168 | len = stream_getl (s); |
| 169 | |
| 170 | printf ("TIME: %s", ctime (&now)); |
| 171 | |
| 172 | /* printf ("TYPE: %d/%d\n", type, subtype); */ |
| 173 | |
| 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); |
| 180 | |
| 181 | if (type == MSG_TABLE_DUMP) |
| 182 | switch (subtype) |
| 183 | { |
| 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; |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | switch (subtype) |
| 197 | { |
| 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; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | printf ("len: %d\n", len); |
| 217 | |
| 218 | ret = fread (s->data + 12, len, 1, fp); |
| 219 | if (feof (fp)) |
| 220 | { |
| 221 | printf ("ENDOF FILE 2\n"); |
| 222 | break; |
| 223 | } |
| 224 | if (ferror (fp)) |
| 225 | { |
| 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 | { |
| 234 | u_char status; |
| 235 | time_t originated; |
| 236 | struct in_addr peer; |
| 237 | u_int16_t attrlen; |
| 238 | |
| 239 | viewno = stream_getw (s); |
| 240 | seq_num = stream_getw (s); |
| 241 | printf ("VIEW: %d\n", viewno); |
| 242 | printf ("SEQUENCE: %d\n", seq_num); |
| 243 | |
| 244 | /* start */ |
| 245 | while (s->getp < len - 16) |
| 246 | { |
| 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); |
| 250 | |
| 251 | status = stream_getc (s); |
| 252 | originated = stream_getl (s); |
| 253 | peer.s_addr = stream_get_ipv4 (s); |
| 254 | source_as = stream_getw(s); |
| 255 | |
| 256 | printf ("FROM: %s AS%d\n", inet_ntoa (peer), 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 | } |
| 267 | else |
| 268 | { |
| 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 | } |