bgp_btoa.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* BGP dump to ascii converter
  2. Copyright (C) 1999 Kunihiro Ishiguro
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA. */
  16. #include <zebra.h>
  17. #include "zebra.h"
  18. #include "stream.h"
  19. #include "log.h"
  20. #include "prefix.h"
  21. #include "command.h"
  22. #include "memory.h"
  23. #include "privs.h"
  24. #include "filter.h"
  25. #include "bgpd/bgpd.h"
  26. #include "bgpd/bgp_dump.h"
  27. #include "bgpd/bgp_attr.h"
  28. #include "bgpd/bgp_aspath.h"
  29. /* privileges */
  30. static zebra_capabilities_t _caps_p [] =
  31. {
  32. ZCAP_BIND,
  33. ZCAP_NET_RAW,
  34. ZCAP_NET_ADMIN,
  35. };
  36. struct zebra_privs_t bgpd_privs =
  37. {
  38. #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
  39. .user = QUAGGA_USER,
  40. .group = QUAGGA_GROUP,
  41. #endif
  42. #ifdef VTY_GROUP
  43. .vty_group = VTY_GROUP,
  44. #endif
  45. .caps_p = _caps_p,
  46. .cap_num_p = array_size(_caps_p),
  47. .cap_num_i = 0,
  48. };
  49. enum MRT_MSG_TYPES {
  50. MSG_NULL,
  51. MSG_START, /* sender is starting up */
  52. MSG_DIE, /* receiver should shut down */
  53. MSG_I_AM_DEAD, /* sender is shutting down */
  54. MSG_PEER_DOWN, /* sender's peer is down */
  55. MSG_PROTOCOL_BGP, /* msg is a BGP packet */
  56. MSG_PROTOCOL_RIP, /* msg is a RIP packet */
  57. MSG_PROTOCOL_IDRP, /* msg is an IDRP packet */
  58. MSG_PROTOCOL_RIPNG, /* msg is a RIPNG packet */
  59. MSG_PROTOCOL_BGP4PLUS, /* msg is a BGP4+ packet */
  60. MSG_PROTOCOL_BGP4PLUS_01, /* msg is a BGP4+ (draft 01) packet */
  61. MSG_PROTOCOL_OSPF, /* msg is an OSPF packet */
  62. MSG_TABLE_DUMP /* routing table dump */
  63. };
  64. static int
  65. attr_parse (struct stream *s, u_int16_t len)
  66. {
  67. u_int flag;
  68. u_int type;
  69. u_int16_t length;
  70. u_int16_t lim;
  71. lim = s->getp + len;
  72. printf ("attr_parse s->getp %zd, len %d, lim %d\n", s->getp, len, lim);
  73. while (s->getp < lim)
  74. {
  75. flag = stream_getc (s);
  76. type = stream_getc (s);
  77. if (flag & BGP_ATTR_FLAG_EXTLEN)
  78. length = stream_getw (s);
  79. else
  80. length = stream_getc (s);
  81. printf ("FLAG: %d\n", flag);
  82. printf ("TYPE: %d\n", type);
  83. printf ("Len: %d\n", length);
  84. switch (type)
  85. {
  86. case BGP_ATTR_ORIGIN:
  87. {
  88. u_char origin;
  89. origin = stream_getc (s);
  90. printf ("ORIGIN: %d\n", origin);
  91. }
  92. break;
  93. case BGP_ATTR_AS_PATH:
  94. {
  95. struct aspath *aspath;
  96. aspath = aspath_parse (s, length, 1);
  97. printf ("ASPATH: %s\n", aspath->str);
  98. aspath_free(aspath);
  99. }
  100. break;
  101. case BGP_ATTR_NEXT_HOP:
  102. {
  103. struct in_addr nexthop;
  104. nexthop.s_addr = stream_get_ipv4 (s);
  105. printf ("NEXTHOP: %s\n", inet_ntoa (nexthop));
  106. }
  107. break;
  108. default:
  109. stream_getw_from (s, length);
  110. break;
  111. }
  112. }
  113. return 0;
  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. size_t len;
  125. int source_as;
  126. int dest_as;
  127. ifindex_t 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. s = stream_new (10000);
  134. if (argc != 2)
  135. {
  136. fprintf (stderr, "Usage: %s FILENAME\n", argv[0]);
  137. exit (1);
  138. }
  139. fp = fopen (argv[1], "r");
  140. if (!fp)
  141. {
  142. perror ("fopen");
  143. exit (1);
  144. }
  145. while (1)
  146. {
  147. stream_reset (s);
  148. ret = fread (s->data, 12, 1, fp);
  149. if (!ret || feof (fp))
  150. {
  151. printf ("END OF FILE\n");
  152. break;
  153. }
  154. if (ferror (fp))
  155. {
  156. printf ("ERROR OF FREAD\n");
  157. break;
  158. }
  159. /* Extract header. */
  160. now = stream_getl (s);
  161. type = stream_getw (s);
  162. subtype = stream_getw (s);
  163. len = stream_getl (s);
  164. printf ("TIME: %s", ctime (&now));
  165. /* printf ("TYPE: %d/%d\n", type, subtype); */
  166. if (type == MSG_PROTOCOL_BGP4MP)
  167. printf ("TYPE: BGP4MP");
  168. else if (type == MSG_PROTOCOL_BGP4MP_ET)
  169. printf ("TYPE: BGP4MP_ET");
  170. else if (type == MSG_TABLE_DUMP)
  171. printf ("TYPE: MSG_TABLE_DUMP");
  172. else
  173. printf ("TYPE: Unknown %d", type);
  174. if (type == MSG_TABLE_DUMP)
  175. switch (subtype)
  176. {
  177. case AFI_IP:
  178. printf ("/AFI_IP\n");
  179. break;
  180. case AFI_IP6:
  181. printf ("/AFI_IP6\n");
  182. break;
  183. default:
  184. printf ("/UNKNOWN %d", subtype);
  185. break;
  186. }
  187. else
  188. {
  189. switch (subtype)
  190. {
  191. case BGP4MP_STATE_CHANGE:
  192. printf ("/CHANGE\n");
  193. break;
  194. case BGP4MP_MESSAGE:
  195. printf ("/MESSAGE\n");
  196. break;
  197. case BGP4MP_ENTRY:
  198. printf ("/ENTRY\n");
  199. break;
  200. case BGP4MP_SNAPSHOT:
  201. printf ("/SNAPSHOT\n");
  202. break;
  203. default:
  204. printf ("/UNKNOWN %d", subtype);
  205. break;
  206. }
  207. }
  208. printf ("len: %zd\n", len);
  209. ret = fread (s->data + 12, len, 1, fp);
  210. if (feof (fp))
  211. {
  212. printf ("ENDOF FILE 2\n");
  213. break;
  214. }
  215. if (ferror (fp))
  216. {
  217. printf ("ERROR OF FREAD 2\n");
  218. break;
  219. }
  220. /* printf ("now read %d\n", len); */
  221. if (type == MSG_TABLE_DUMP)
  222. {
  223. u_char status;
  224. time_t originated;
  225. struct in_addr peer;
  226. u_int16_t attrlen;
  227. viewno = stream_getw (s);
  228. seq_num = stream_getw (s);
  229. printf ("VIEW: %d\n", viewno);
  230. printf ("SEQUENCE: %d\n", seq_num);
  231. /* start */
  232. while (s->getp < len - 16)
  233. {
  234. p.prefix.s_addr = stream_get_ipv4 (s);
  235. p.prefixlen = stream_getc (s);
  236. printf ("PREFIX: %s/%d\n", inet_ntoa (p.prefix), p.prefixlen);
  237. status = stream_getc (s);
  238. originated = stream_getl (s);
  239. peer.s_addr = stream_get_ipv4 (s);
  240. source_as = stream_getw(s);
  241. printf ("FROM: %s AS%d\n", inet_ntoa (peer), source_as);
  242. printf ("ORIGINATED: %s", ctime (&originated));
  243. attrlen = stream_getw (s);
  244. printf ("ATTRLEN: %d\n", attrlen);
  245. attr_parse (s, attrlen);
  246. printf ("STATUS: 0x%x\n", status);
  247. }
  248. }
  249. else
  250. {
  251. source_as = stream_getw (s);
  252. dest_as = stream_getw (s);
  253. printf ("source_as: %d\n", source_as);
  254. printf ("dest_as: %d\n", dest_as);
  255. ifindex = stream_getw (s);
  256. family = stream_getw (s);
  257. printf ("ifindex: %d\n", ifindex);
  258. printf ("family: %d\n", family);
  259. sip.s_addr = stream_get_ipv4 (s);
  260. dip.s_addr = stream_get_ipv4 (s);
  261. printf ("saddr: %s\n", inet_ntoa (sip));
  262. printf ("daddr: %s\n", inet_ntoa (dip));
  263. printf ("\n");
  264. }
  265. }
  266. fclose (fp);
  267. return 0;
  268. }