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

📄 zebra_vty.c

📁 zebra测试源代码用于 SOCKET 通信
💻 C
📖 第 1 页 / 共 3 页
字号:
		     tm->tm_yday, tm->tm_hour, tm->tm_min);	  else	    vty_out (vty, "%02dw%dd%02dh", 		     tm->tm_yday/7,		     tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour);	  vty_out (vty, " ago%s", VTY_NEWLINE);	}      for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)	{	  vty_out (vty, "  %c",		   CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ? '*' : ' ');	  switch (nexthop->type)	    {	    case NEXTHOP_TYPE_IPV6:	    case NEXTHOP_TYPE_IPV6_IFINDEX:	    case NEXTHOP_TYPE_IPV6_IFNAME:	      vty_out (vty, " %s",		       inet_ntop (AF_INET6, &nexthop->gate.ipv6, buf, BUFSIZ));	      if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME)		vty_out (vty, ", %s", nexthop->ifname);	      else if (nexthop->ifindex)		vty_out (vty, ", via %s", ifindex2ifname (nexthop->ifindex));	      break;	    case NEXTHOP_TYPE_IFINDEX:	      vty_out (vty, " directly connected, %s",		       ifindex2ifname (nexthop->ifindex));	      break;	    case NEXTHOP_TYPE_IFNAME:	      vty_out (vty, " directly connected, %s",		       nexthop->ifname);	      break;	    default:	      break;	    }	  if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))	    vty_out (vty, " inactive");	  if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))	    {	      vty_out (vty, " (recursive");			      switch (nexthop->rtype)		{		case NEXTHOP_TYPE_IPV6:		case NEXTHOP_TYPE_IPV6_IFINDEX:		case NEXTHOP_TYPE_IPV6_IFNAME:		  vty_out (vty, " via %s)",			   inet_ntop (AF_INET6, &nexthop->rgate.ipv6,				      buf, BUFSIZ));		  if (nexthop->rifindex)		    vty_out (vty, ", %s", ifindex2ifname (nexthop->rifindex));		  break;		case NEXTHOP_TYPE_IFINDEX:		case NEXTHOP_TYPE_IFNAME:		  vty_out (vty, " is directly connected, %s)",			   ifindex2ifname (nexthop->rifindex));		  break;		default:		  break;		}	    }	  vty_out (vty, "%s", VTY_NEWLINE);	}      vty_out (vty, "%s", VTY_NEWLINE);    }}voidvty_show_ipv6_route (struct vty *vty, struct route_node *rn,		     struct rib *rib){  struct nexthop *nexthop;  int len = 0;  char buf[BUFSIZ];  /* Nexthop information. */  for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)    {      if (nexthop == rib->nexthop)	{	  /* Prefix information. */	  len = vty_out (vty, "%c%c%c %s/%d",			 route_type_char (rib->type),			 CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)			 ? '>' : ' ',			 CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)			 ? '*' : ' ',			 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),			 rn->p.prefixlen);	  /* Distance and metric display. */	  if (rib->type != ZEBRA_ROUTE_CONNECT 	      && rib->type != ZEBRA_ROUTE_KERNEL)	    len += vty_out (vty, " [%d/%d]", rib->distance,			    rib->metric);	}      else	vty_out (vty, "  %c%*c",		 CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)		 ? '*' : ' ',		 len - 3, ' ');      switch (nexthop->type)	{	case NEXTHOP_TYPE_IPV6:	case NEXTHOP_TYPE_IPV6_IFINDEX:	case NEXTHOP_TYPE_IPV6_IFNAME:	  vty_out (vty, " via %s",		   inet_ntop (AF_INET6, &nexthop->gate.ipv6, buf, BUFSIZ));	  if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME)	    vty_out (vty, ", %s", nexthop->ifname);	  else if (nexthop->ifindex)	    vty_out (vty, ", %s", ifindex2ifname (nexthop->ifindex));	  break;	case NEXTHOP_TYPE_IFINDEX:	  vty_out (vty, " is directly connected, %s",		   ifindex2ifname (nexthop->ifindex));	  break;	case NEXTHOP_TYPE_IFNAME:	  vty_out (vty, " is directly connected, %s",		   nexthop->ifname);	  break;	default:	  break;	}      if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))	vty_out (vty, " inactive");      if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))	{	  vty_out (vty, " (recursive");			  switch (nexthop->rtype)	    {	    case NEXTHOP_TYPE_IPV6:	    case NEXTHOP_TYPE_IPV6_IFINDEX:	    case NEXTHOP_TYPE_IPV6_IFNAME:	      vty_out (vty, " via %s)",		       inet_ntop (AF_INET6, &nexthop->rgate.ipv6,				  buf, BUFSIZ));	      if (nexthop->rifindex)		vty_out (vty, ", %s", ifindex2ifname (nexthop->rifindex));	      break;	    case NEXTHOP_TYPE_IFINDEX:	    case NEXTHOP_TYPE_IFNAME:	      vty_out (vty, " is directly connected, %s)",		       ifindex2ifname (nexthop->rifindex));	      break;	    default:	      break;	    }	}      if (rib->type == ZEBRA_ROUTE_RIPNG	  || rib->type == ZEBRA_ROUTE_OSPF6	  || rib->type == ZEBRA_ROUTE_BGP)	{	  time_t uptime;	  struct tm *tm;	  uptime = time (NULL);	  uptime -= rib->uptime;	  tm = gmtime (&uptime);#define ONE_DAY_SECOND 60*60*24#define ONE_WEEK_SECOND 60*60*24*7	  if (uptime < ONE_DAY_SECOND)	    vty_out (vty,  ", %02d:%02d:%02d", 		     tm->tm_hour, tm->tm_min, tm->tm_sec);	  else if (uptime < ONE_WEEK_SECOND)	    vty_out (vty, ", %dd%02dh%02dm", 		     tm->tm_yday, tm->tm_hour, tm->tm_min);	  else	    vty_out (vty, ", %02dw%dd%02dh", 		     tm->tm_yday/7,		     tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour);	}      vty_out (vty, "%s", VTY_NEWLINE);    }}#define SHOW_ROUTE_V6_HEADER "Codes: K - kernel route, C - connected, S - static, R - RIPng, O - OSPFv3,%s       B - BGP, * - FIB route.%s%s"DEFUN (show_ipv6_route,       show_ipv6_route_cmd,       "show ipv6 route",       SHOW_STR       IP_STR       "IPv6 routing table\n"){  struct route_table *table;  struct route_node *rn;  struct rib *rib;  int first = 1;  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);  if (! table)    return CMD_SUCCESS;  /* Show all IPv6 route. */  for (rn = route_top (table); rn; rn = route_next (rn))    for (rib = rn->info; rib; rib = rib->next)      {	if (first)	  {	    vty_out (vty, SHOW_ROUTE_V6_HEADER, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);	    first = 0;	  }	vty_show_ipv6_route (vty, rn, rib);      }  return CMD_SUCCESS;}DEFUN (show_ipv6_route_prefix_longer,       show_ipv6_route_prefix_longer_cmd,       "show ipv6 route X:X::X:X/M longer-prefixes",       SHOW_STR       IP_STR       "IPv6 routing table\n"       "IPv6 prefix\n"       "Show route matching the specified Network/Mask pair only\n"){  struct route_table *table;  struct route_node *rn;  struct rib *rib;  struct prefix p;  int ret;  int first = 1;  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);  if (! table)    return CMD_SUCCESS;  ret = str2prefix (argv[0], &p);  if (! ret)    {      vty_out (vty, "%% Malformed Prefix%s", VTY_NEWLINE);      return CMD_WARNING;    }  /* Show matched type IPv6 routes. */  for (rn = route_top (table); rn; rn = route_next (rn))    for (rib = rn->info; rib; rib = rib->next)      if (prefix_match (&p, &rn->p))	{	  if (first)	    {	      vty_out (vty, SHOW_ROUTE_V6_HEADER, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);	      first = 0;	    }	  vty_show_ipv6_route (vty, rn, rib);	}  return CMD_SUCCESS;}DEFUN (show_ipv6_route_protocol,       show_ipv6_route_protocol_cmd,       "show ipv6 route (bgp|connected|kernel|ospf6|ripng|static)",       SHOW_STR       IP_STR       "IP routing table\n"       "Border Gateway Protocol (BGP)\n"       "Connected\n"       "Kernel\n"       "Open Shortest Path First (OSPFv3)\n"       "Routing Information Protocol (RIPng)\n"       "Static routes\n"){  int type;  struct route_table *table;  struct route_node *rn;  struct rib *rib;  int first = 1;  if (strncmp (argv[0], "b", 1) == 0)    type = ZEBRA_ROUTE_BGP;  else if (strncmp (argv[0], "c", 1) == 0)    type = ZEBRA_ROUTE_CONNECT;  else if (strncmp (argv[0], "k", 1) ==0)    type = ZEBRA_ROUTE_KERNEL;  else if (strncmp (argv[0], "o", 1) == 0)    type = ZEBRA_ROUTE_OSPF6;  else if (strncmp (argv[0], "r", 1) == 0)    type = ZEBRA_ROUTE_RIPNG;  else if (strncmp (argv[0], "s", 1) == 0)    type = ZEBRA_ROUTE_STATIC;  else     {      vty_out (vty, "Unknown route type%s", VTY_NEWLINE);      return CMD_WARNING;    }    table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);  if (! table)    return CMD_SUCCESS;  /* Show matched type IPv6 routes. */  for (rn = route_top (table); rn; rn = route_next (rn))    for (rib = rn->info; rib; rib = rib->next)      if (rib->type == type)	{	  if (first)	    {	      vty_out (vty, SHOW_ROUTE_V6_HEADER, VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);	      first = 0;	    }	  vty_show_ipv6_route (vty, rn, rib);	}  return CMD_SUCCESS;}DEFUN (show_ipv6_route_addr,       show_ipv6_route_addr_cmd,       "show ipv6 route X:X::X:X",       SHOW_STR       IP_STR       "IPv6 routing table\n"       "IPv6 Address\n"){  int ret;  struct prefix_ipv6 p;  struct route_table *table;  struct route_node *rn;  ret = str2prefix_ipv6 (argv[0], &p);  if (ret <= 0)    {      vty_out (vty, "Malformed IPv6 address%s", VTY_NEWLINE);      return CMD_WARNING;    }  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);  if (! table)    return CMD_SUCCESS;  rn = route_node_match (table, (struct prefix *) &p);  if (! rn)    {      vty_out (vty, "%% Network not in table%s", VTY_NEWLINE);      return CMD_WARNING;    }  vty_show_ipv6_route_detail (vty, rn);  route_unlock_node (rn);  return CMD_SUCCESS;}DEFUN (show_ipv6_route_prefix,       show_ipv6_route_prefix_cmd,       "show ipv6 route X:X::X:X/M",       SHOW_STR       IP_STR       "IPv6 routing table\n"       "IPv6 prefix\n"){  int ret;  struct prefix_ipv6 p;  struct route_table *table;  struct route_node *rn;  ret = str2prefix_ipv6 (argv[0], &p);  if (ret <= 0)    {      vty_out (vty, "Malformed IPv6 prefix%s", VTY_NEWLINE);      return CMD_WARNING;    }  table = vrf_table (AFI_IP6, SAFI_UNICAST, 0);  if (! table)    return CMD_SUCCESS;  rn = route_node_match (table, (struct prefix *) &p);  if (! rn || rn->p.prefixlen != p.prefixlen)    {      vty_out (vty, "%% Network not in table%s", VTY_NEWLINE);      return CMD_WARNING;    }  vty_show_ipv6_route_detail (vty, rn);  route_unlock_node (rn);  return CMD_SUCCESS;}/* Write IPv6 static route configuration. */intstatic_config_ipv6 (struct vty *vty){  struct route_node *rn;  struct static_ipv6 *si;    int write;  char buf[BUFSIZ];  struct route_table *stable;  write = 0;  /* Lookup table.  */  stable = vrf_static_table (AFI_IP6, SAFI_UNICAST, 0);  if (! stable)    return -1;  for (rn = route_top (stable); rn; rn = route_next (rn))    for (si = rn->info; si; si = si->next)      {	vty_out (vty, "ipv6 route %s/%d",		 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),		 rn->p.prefixlen);	switch (si->type)	  {	  case STATIC_IPV6_GATEWAY:	    vty_out (vty, " %s", inet_ntop (AF_INET6, &si->ipv6, buf, BUFSIZ));	    break;	  case STATIC_IPV6_IFNAME:	    vty_out (vty, " %s", si->ifname);	    break;	  case STATIC_IPV6_GATEWAY_IFNAME:	    vty_out (vty, " %s %s",		     inet_ntop (AF_INET6, &si->ipv6, buf, BUFSIZ), si->ifname);	    break;	  }	if (si->distance != ZEBRA_STATIC_DISTANCE_DEFAULT)	  vty_out (vty, " %d", si->distance);	vty_out (vty, "%s", VTY_NEWLINE);	write = 1;      }  return write;}#endif /* HAVE_IPV6 *//* Static ip route configuration write function. */intzebra_ip_config (struct vty *vty){  int write = 0;  write += static_config_ipv4 (vty);#ifdef HAVE_IPV6  write += static_config_ipv6 (vty);#endif /* HAVE_IPV6 */  return write;}/* IP node for static routes. */struct cmd_node ip_node = { IP_NODE,  "",  1 };/* Route VTY.  */voidzebra_vty_route_init (){  install_node (&ip_node, zebra_ip_config);  install_element (CONFIG_NODE, &ip_route_cmd);  install_element (CONFIG_NODE, &ip_route_mask_cmd);  install_element (CONFIG_NODE, &no_ip_route_cmd);  install_element (CONFIG_NODE, &no_ip_route_mask_cmd);  install_element (CONFIG_NODE, &ip_route_distance_cmd);  install_element (CONFIG_NODE, &ip_route_mask_distance_cmd);  install_element (CONFIG_NODE, &no_ip_route_distance_cmd);  install_element (CONFIG_NODE, &no_ip_route_mask_distance_cmd);  install_element (VIEW_NODE, &show_ip_route_cmd);  install_element (VIEW_NODE, &show_ip_route_addr_cmd);  install_element (VIEW_NODE, &show_ip_route_prefix_cmd);  install_element (VIEW_NODE, &show_ip_route_prefix_longer_cmd);  install_element (VIEW_NODE, &show_ip_route_protocol_cmd);  install_element (VIEW_NODE, &show_ip_route_supernets_cmd);  install_element (ENABLE_NODE, &show_ip_route_cmd);  install_element (ENABLE_NODE, &show_ip_route_addr_cmd);  install_element (ENABLE_NODE, &show_ip_route_prefix_cmd);  install_element (ENABLE_NODE, &show_ip_route_prefix_longer_cmd);  install_element (ENABLE_NODE, &show_ip_route_protocol_cmd);  install_element (ENABLE_NODE, &show_ip_route_supernets_cmd);#if 0  install_element (VIEW_NODE, &show_ip_route_summary_cmd);  install_element (ENABLE_NODE, &show_ip_route_summary_cmd);#endif /* 0 */#ifdef HAVE_IPV6  install_element (CONFIG_NODE, &ipv6_route_cmd);  install_element (CONFIG_NODE, &ipv6_route_ifname_cmd);  install_element (CONFIG_NODE, &no_ipv6_route_cmd);  install_element (CONFIG_NODE, &no_ipv6_route_ifname_cmd);  install_element (CONFIG_NODE, &ipv6_route_pref_cmd);  install_element (CONFIG_NODE, &ipv6_route_ifname_pref_cmd);  install_element (CONFIG_NODE, &no_ipv6_route_pref_cmd);  install_element (CONFIG_NODE, &no_ipv6_route_ifname_pref_cmd);  install_element (VIEW_NODE, &show_ipv6_route_cmd);  install_element (VIEW_NODE, &show_ipv6_route_protocol_cmd);  install_element (VIEW_NODE, &show_ipv6_route_addr_cmd);  install_element (VIEW_NODE, &show_ipv6_route_prefix_cmd);  install_element (VIEW_NODE, &show_ipv6_route_prefix_longer_cmd);  install_element (ENABLE_NODE, &show_ipv6_route_cmd);  install_element (ENABLE_NODE, &show_ipv6_route_protocol_cmd);  install_element (ENABLE_NODE, &show_ipv6_route_addr_cmd);  install_element (ENABLE_NODE, &show_ipv6_route_prefix_cmd);  install_element (ENABLE_NODE, &show_ipv6_route_prefix_longer_cmd);#endif /* HAVE_IPV6 */}voidzebra_vty_init (){  zebra_vty_route_init ();}

⌨️ 快捷键说明

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