⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bgp_dump.c

📁 大名鼎鼎的路由器源码。程序分ZEBRA、OSPFRIP等3个包。程序框架采用一个路由协议一个进程的方式
💻 C
📖 第 1 页 / 共 2 页
字号:
  bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_STATE_CHANGE);  bgp_dump_common (obuf, peer);  stream_putw (obuf, status_old);  stream_putw (obuf, status_new);  /* Set length. */  bgp_dump_set_size (obuf, MSG_PROTOCOL_BGP4MP);  /* Write to the stream. */  fwrite (STREAM_DATA (obuf), stream_get_putp (obuf), 1, bgp_dump_all.fp);  fflush (bgp_dump_all.fp);}voidbgp_dump_packet_func (struct bgp_dump *bgp_dump, struct peer *peer,		      struct stream *packet){  struct stream *obuf;  /* If dump file pointer is disabled return immediately. */  if (bgp_dump->fp == NULL)    return;  /* Make dump stream. */  obuf = bgp_dump_obuf;  stream_reset (obuf);  /* Dump header and common part. */  bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_MESSAGE);  bgp_dump_common (obuf, peer);  /* Packet contents. */  stream_put (obuf, STREAM_DATA (packet), stream_get_endp (packet));    /* Set length. */  bgp_dump_set_size (obuf, MSG_PROTOCOL_BGP4MP);  /* Write to the stream. */  fwrite (STREAM_DATA (obuf), stream_get_putp (obuf), 1, bgp_dump->fp);  fflush (bgp_dump->fp);}/* Called from bgp_packet.c when BGP packet is received. */voidbgp_dump_packet (struct peer *peer, int type, struct stream *packet){  /* bgp_dump_all. */  bgp_dump_packet_func (&bgp_dump_all, peer, packet);  /* bgp_dump_updates. */  if (type == BGP_MSG_UPDATE)    bgp_dump_packet_func (&bgp_dump_updates, peer, packet);}unsigned intbgp_dump_parse_time (char *str){  int i;  int len;  int seen_h;  int seen_m;  int time;  unsigned int total;  time = 0;  total = 0;  seen_h = 0;  seen_m = 0;  len = strlen (str);  for (i = 0; i < len; i++)    {      if (isdigit ((int) str[i]))	{	  time *= 10;	  time += str[i] - '0';	}      else if (str[i] == 'H' || str[i] == 'h')	{	  if (seen_h)	    return 0;	  if (seen_m)	    return 0;	  total += time * 60 *60;	  time = 0;	  seen_h = 1;	}      else if (str[i] == 'M' || str[i] == 'm')	{	  if (seen_m)	    return 0;	  total += time * 60;	  time = 0;	  seen_h = 1;	}      else	return 0;    }  return total + time;}intbgp_dump_set (struct vty *vty, struct bgp_dump *bgp_dump, int type,	      char *path, char *interval_str){  if (interval_str)    {      unsigned int interval;      /* Check interval string. */      interval = bgp_dump_parse_time (interval_str);      if (interval == 0)	{	  vty_out (vty, "Malformed interval string%s", VTY_NEWLINE);	  return CMD_WARNING;	}      /* Set interval. */      bgp_dump->interval = interval;      if (bgp_dump->interval_str)	free (bgp_dump->interval_str);      bgp_dump->interval_str = strdup (interval_str);      /* Create interval thread. */      bgp_dump_interval_add (bgp_dump, interval);    }  /* Set type. */  bgp_dump->type = type;  /* Set file name. */  if (bgp_dump->filename)    free (bgp_dump->filename);  bgp_dump->filename = strdup (path);  /* This should be called when interval is expired. */  bgp_dump_open_file (bgp_dump);  return CMD_SUCCESS;}intbgp_dump_unset (struct vty *vty, struct bgp_dump *bgp_dump){  /* Set file name. */  if (bgp_dump->filename)    {      free (bgp_dump->filename);      bgp_dump->filename = NULL;    }  /* This should be called when interval is expired. */  if (bgp_dump->fp)    {      fclose (bgp_dump->fp);      bgp_dump->fp = NULL;    }  /* Create interval thread. */  if (bgp_dump->t_interval)    {      thread_cancel (bgp_dump->t_interval);      bgp_dump->t_interval = NULL;    }  bgp_dump->interval = 0;  if (bgp_dump->interval_str)    {      free (bgp_dump->interval_str);      bgp_dump->interval_str = NULL;    }    return CMD_SUCCESS;}DEFUN (dump_bgp_all,       dump_bgp_all_cmd,       "dump bgp all PATH",       "Dump packet\n"       "BGP packet dump\n"       "Dump all BGP packets\n"       "Output filename\n"){  return bgp_dump_set (vty, &bgp_dump_all, BGP_DUMP_ALL, argv[0], NULL);}DEFUN (dump_bgp_all_interval,       dump_bgp_all_interval_cmd,       "dump bgp all PATH INTERVAL",       "Dump packet\n"       "BGP packet dump\n"       "Dump all BGP packets\n"       "Output filename\n"       "Interval of output\n"){  return bgp_dump_set (vty, &bgp_dump_all, BGP_DUMP_ALL, argv[0], argv[1]);}DEFUN (no_dump_bgp_all,       no_dump_bgp_all_cmd,       "no dump bgp all [PATH] [INTERVAL]",       NO_STR       "Dump packet\n"       "BGP packet dump\n"       "Dump all BGP packets\n"){  return bgp_dump_unset (vty, &bgp_dump_all);}DEFUN (dump_bgp_updates,       dump_bgp_updates_cmd,       "dump bgp updates PATH",       "Dump packet\n"       "BGP packet dump\n"       "Dump BGP updates only\n"       "Output filename\n"){  return bgp_dump_set (vty, &bgp_dump_updates, BGP_DUMP_UPDATES, argv[0], NULL);}DEFUN (dump_bgp_updates_interval,       dump_bgp_updates_interval_cmd,       "dump bgp updates PATH INTERVAL",       "Dump packet\n"       "BGP packet dump\n"       "Dump BGP updates only\n"       "Output filename\n"       "Interval of output\n"){  return bgp_dump_set (vty, &bgp_dump_updates, BGP_DUMP_UPDATES, argv[0], argv[1]);}DEFUN (no_dump_bgp_updates,       no_dump_bgp_updates_cmd,       "no dump bgp updates [PATH] [INTERVAL]",       NO_STR       "Dump packet\n"       "BGP packet dump\n"       "Dump BGP updates only\n"){  return bgp_dump_unset (vty, &bgp_dump_updates);}DEFUN (dump_bgp_routes,       dump_bgp_routes_cmd,       "dump bgp routes-mrt PATH",       "Dump packet\n"       "BGP packet dump\n"       "Dump whole BGP routing table\n"       "Output filename\n"){  return bgp_dump_set (vty, &bgp_dump_routes, BGP_DUMP_ROUTES, argv[0], NULL);}DEFUN (dump_bgp_routes_interval,       dump_bgp_routes_interval_cmd,       "dump bgp routes-mrt PATH INTERVAL",       "Dump packet\n"       "BGP packet dump\n"       "Dump whole BGP routing table\n"       "Output filename\n"       "Interval of output\n"){  return bgp_dump_set (vty, &bgp_dump_routes, BGP_DUMP_ROUTES, argv[0], argv[1]);}DEFUN (no_dump_bgp_routes,       no_dump_bgp_routes_cmd,       "no dump bgp routes-mrt [PATH] [INTERVAL]",       NO_STR       "Dump packet\n"       "BGP packet dump\n"       "Dump whole BGP routing table\n"){  return bgp_dump_unset (vty, &bgp_dump_routes);}/* BGP node structure. */struct cmd_node bgp_dump_node ={  DUMP_NODE,  "",};#if 0char *config_time2str (unsigned int interval){  static char buf[BUFSIZ];  buf[0] = '\0';  if (interval / 3600)    {      sprintf (buf, "%dh", interval / 3600);      interval %= 3600;    }  if (interval / 60)    {      sprintf (buf + strlen (buf), "%dm", interval /60);      interval %= 60;    }  if (interval)    {      sprintf (buf + strlen (buf), "%d", interval);    }  return buf;}#endifintconfig_write_bgp_dump (struct vty *vty){  if (bgp_dump_all.filename)    {      if (bgp_dump_all.interval_str)	vty_out (vty, "dump bgp all %s %s%s", 		 bgp_dump_all.filename, bgp_dump_all.interval_str,		 VTY_NEWLINE);      else	vty_out (vty, "dump bgp all %s%s", 		 bgp_dump_all.filename, VTY_NEWLINE);    }  if (bgp_dump_updates.filename)    {      if (bgp_dump_updates.interval_str)	vty_out (vty, "dump bgp updates %s %s%s", 		 bgp_dump_updates.filename, bgp_dump_updates.interval_str,		 VTY_NEWLINE);      else	vty_out (vty, "dump bgp updates %s%s", 		 bgp_dump_updates.filename, VTY_NEWLINE);    }  if (bgp_dump_routes.filename)    {      if (bgp_dump_routes.interval_str)	vty_out (vty, "dump bgp routes-mrt %s %s%s", 		 bgp_dump_routes.filename, bgp_dump_routes.interval_str,		 VTY_NEWLINE);      else	vty_out (vty, "dump bgp routes-mrt %s%s", 		 bgp_dump_routes.filename, VTY_NEWLINE);    }  return 0;}/* Initialize BGP packet dump functionality. */voidbgp_dump_init (){  memset (&bgp_dump_all, 0, sizeof (struct bgp_dump));  memset (&bgp_dump_updates, 0, sizeof (struct bgp_dump));  memset (&bgp_dump_routes, 0, sizeof (struct bgp_dump));  bgp_dump_obuf = stream_new (BGP_MAX_PACKET_SIZE + BGP_DUMP_HEADER_SIZE);  install_node (&bgp_dump_node, config_write_bgp_dump);  install_element (CONFIG_NODE, &dump_bgp_all_cmd);  install_element (CONFIG_NODE, &dump_bgp_all_interval_cmd);  install_element (CONFIG_NODE, &no_dump_bgp_all_cmd);  install_element (CONFIG_NODE, &dump_bgp_updates_cmd);  install_element (CONFIG_NODE, &dump_bgp_updates_interval_cmd);  install_element (CONFIG_NODE, &no_dump_bgp_updates_cmd);  install_element (CONFIG_NODE, &dump_bgp_routes_cmd);  install_element (CONFIG_NODE, &dump_bgp_routes_interval_cmd);  install_element (CONFIG_NODE, &no_dump_bgp_routes_cmd);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -