bgp_btoa.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 "bgpd/bgpd.h"
  25. #include "bgpd/bgp_dump.h"
  26. #include "bgpd/bgp_attr.h"
  27. #include "bgpd/bgp_aspath.h"
  28. /* privileges */
  29. static zebra_capabilities_t _caps_p [] =
  30. {
  31. ZCAP_BIND,
  32. ZCAP_NET_RAW,
  33. ZCAP_NET_ADMIN,
  34. };
  35. struct zebra_privs_t bgpd_privs =
  36. {
  37. #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
  38. .user = QUAGGA_USER,
  39. .group = QUAGGA_GROUP,
  40. #endif
  41. #ifdef VTY_GROUP
  42. .vty_group = VTY_GROUP,
  43. #endif
  44. .caps_p = _caps_p,
  45. .cap_num_p = array_size(_caps_p),
  46. .cap_num_i = 0,
  47. };
  48. enum MRT_MSG_TYPES {
  49. MSG_NULL,
  50. MSG_START, /* sender is starting up */
  51. MSG_DIE, /* receiver should shut down */
  52. MSG_I_AM_DEAD, /* sender is shutting down */
  53. MSG_PEER_DOWN, /* sender's peer is down */
  54. MSG_PROTOCOL_BGP, /* msg is a BGP packet */
  55. MSG_PROTOCOL_RIP, /* msg is a RIP packet */
  56. MSG_PROTOCOL_IDRP, /* msg is an IDRP packet */
  57. MSG_PROTOCOL_RIPNG, /* msg is a RIPNG packet */
  58. MSG_PROTOCOL_BGP4PLUS, /* msg is a BGP4+ packet */
  59. MSG_PROTOCOL_BGP4PLUS_01, /* msg is a BGP4+ (draft 01) packet */
  60. MSG_PROTOCOL_OSPF, /* msg is an OSPF packet */
  61. MSG_TABLE_DUMP /* routing table dump */
  62. };
  63. static int
  64. attr_parse (struct stream *s, u_int16_t len)
  65. {
  66. u_int flag;
  67. u_int type;
  68. u_int16_t length;
  69. u_int16_t lim;
  70. lim = s->getp + len;
  71. printf ("attr_parse s->getp %zd, len %d, lim %d\n", s->getp, len, lim);
  72. while (s->getp < lim)
  73. {
  74. flag = stream_getc (s);
  75. type = stream_getc (s);
  76. if (flag & BGP_ATTR_FLAG_EXTLEN)
  77. length = stream_getw (s);
  78. else
  79. length = stream_getc (s);
  80. printf ("FLAG: %d\n", flag);
  81. printf ("TYPE: %d\n", type);
  82. printf ("Len: %d\n", length);
  83. switch (type)
  84. {
  85. case BGP_ATTR_ORIGIN:
  86. {
  87. u_char origin;
  88. origin = stream_getc (s);
  89. printf ("ORIGIN: %d\n", origin);
  90. }
  91. break;
  92. case BGP_ATTR_AS_PATH:
  93. {
  94. struct aspath *aspath;
  95. aspath = aspath_parse (s, length, 1);
  96. printf ("ASPATH: %s\n", aspath->str);
  97. aspath_free(aspath);
  98. }
  99. break;
  100. case BGP_ATTR_NEXT_HOP:
  101. {
  102. struct in_addr nexthop;
  103. nexthop.s_addr = stream_get_ipv4 (s);
  104. printf ("NEXTHOP: %s\n", inet_ntoa (nexthop));
  105. }
  106. break;
  107. default:
  108. stream_getw_from (s, length);
  109. break;
  110. }
  111. }
  112. return 0;
  113. }
  114. int
  115. main (int argc, char **argv)
  116. {
  117. int ret;
  118. FILE *fp;
  119. struct stream *s;
  120. time_t now;
  121. int type;
  122. int subtype;
  123. size_t len;
  124. int source_as;
  125. int dest_as;
  126. int ifindex;
  127. int family;
  128. struct in_addr sip;
  129. struct in_addr dip;
  130. u_int16_t viewno, seq_num;
  131. struct prefix_ipv4 p;
  132. s = stream_new (10000);
  133. if (argc != 2)
  134. {
  135. fprintf (stderr, "Usage: %s FILENAME\n", argv[0]);
  136. exit (1);
  137. }
  138. fp = fopen (argv[1], "r");
  139. if (!fp)
  140. {
  141. perror ("fopen");
  142. exit (1);
  143. }
  144. while (1)
  145. {
  146. stream_reset (s);
  147. ret = fread (s->data, 12, 1, fp);
  148. if (!ret || feof (fp))
  149. {
  150. printf ("END OF FILE\n");
  151. break;
  152. }
  153. if (ferror (fp))
  154. {
  155. printf ("ERROR OF FREAD\n");
  156. break;
  157. }
  158. /* Extract header. */
  159. now = stream_getl (s);
  160. type = stream_getw (s);
  161. subtype = stream_getw (s);
  162. len = stream_getl (s);
  163. printf ("TIME: %s", ctime (&now));
  164. /* printf ("TYPE: %d/%d\n", type, subtype); */
  165. if (type == MSG_PROTOCOL_BGP4MP)
  166. printf ("TYPE: BGP4MP");
  167. else if (type == MSG_PROTOCOL_BGP4MP_ET)
  168. printf ("TYPE: BGP4MP_ET");
  169. else if (type == MSG_TABLE_DUMP)
  170. printf ("TYPE: MSG_TABLE_DUMP");
  171. else
  172. printf ("TYPE: Unknown %d", type);
  173. if (type == MSG_TABLE_DUMP)
  174. switch (subtype)
  175. {
  176. case AFI_IP:
  177. printf ("/AFI_IP\n");
  178. break;
  179. case AFI_IP6:
  180. printf ("/AFI_IP6\n");
  181. break;
  182. default:
  183. printf ("/UNKNOWN %d", subtype);
  184. break;
  185. }
  186. else
  187. {
  188. switch (subtype)
  189. {
  190. case BGP4MP_STATE_CHANGE:
  191. printf ("/CHANGE\n");
  192. break;
  193. case BGP4MP_MESSAGE:
  194. printf ("/MESSAGE\n");
  195. break;
  196. case BGP4MP_ENTRY:
  197. printf ("/ENTRY\n");
  198. break;
  199. case BGP4MP_SNAPSHOT:
  200. printf ("/SNAPSHOT\n");
  201. break;
  202. default:
  203. printf ("/UNKNOWN %d", subtype);
  204. break;
  205. }
  206. }
  207. printf ("len: %zd\n", len);
  208. ret = fread (s->data + 12, len, 1, fp);
  209. if (feof (fp))
  210. {
  211. printf ("ENDOF FILE 2\n");
  212. break;
  213. }
  214. if (ferror (fp))
  215. {
  216. printf ("ERROR OF FREAD 2\n");
  217. break;
  218. }
  219. /* printf ("now read %d\n", len); */
  220. if (type == MSG_TABLE_DUMP)
  221. {
  222. u_char status;
  223. time_t originated;
  224. struct in_addr peer;
  225. u_int16_t attrlen;
  226. viewno = stream_getw (s);
  227. seq_num = stream_getw (s);
  228. printf ("VIEW: %d\n", viewno);
  229. printf ("SEQUENCE: %d\n", seq_num);
  230. /* start */
  231. while (s->getp < len - 16)
  232. {
  233. p.prefix.s_addr = stream_get_ipv4 (s);
  234. p.prefixlen = stream_getc (s);
  235. printf ("PREFIX: %s/%d\n", inet_ntoa (p.prefix), p.prefixlen);
  236. status = stream_getc (s);
  237. originated = stream_getl (s);
  238. peer.s_addr = stream_get_ipv4 (s);
  239. source_as = stream_getw(s);
  240. printf ("FROM: %s AS%d\n", inet_ntoa (peer), source_as);
  241. printf ("ORIGINATED: %s", ctime (&originated));
  242. attrlen = stream_getw (s);
  243. printf ("ATTRLEN: %d\n", attrlen);
  244. attr_parse (s, attrlen);
  245. printf ("STATUS: 0x%x\n", status);
  246. }
  247. }
  248. else
  249. {
  250. source_as = stream_getw (s);
  251. dest_as = stream_getw (s);
  252. printf ("source_as: %d\n", source_as);
  253. printf ("dest_as: %d\n", dest_as);
  254. ifindex = stream_getw (s);
  255. family = stream_getw (s);
  256. printf ("ifindex: %d\n", ifindex);
  257. printf ("family: %d\n", family);
  258. sip.s_addr = stream_get_ipv4 (s);
  259. dip.s_addr = stream_get_ipv4 (s);
  260. printf ("saddr: %s\n", inet_ntoa (sip));
  261. printf ("daddr: %s\n", inet_ntoa (dip));
  262. printf ("\n");
  263. }
  264. }
  265. fclose (fp);
  266. return 0;
  267. }