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

📄 zebra_vty.c

📁 zebra测试源代码用于 SOCKET 通信
💻 C
📖 第 1 页 / 共 3 页
字号:
       "IP routing table\n"       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\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;  ret = str2prefix (argv[0], &p);  if (! ret)    {      vty_out (vty, "%% Malformed Prefix%s", VTY_NEWLINE);      return CMD_WARNING;    }    table = vrf_table (AFI_IP, SAFI_UNICAST, 0);  if (! table)    return CMD_SUCCESS;  /* Show matched type IPv4 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_V4_HEADER, VTY_NEWLINE,		       VTY_NEWLINE, VTY_NEWLINE);	      first = 0;	    }	  vty_show_ip_route (vty, rn, rib);	}  return CMD_SUCCESS;}DEFUN (show_ip_route_supernets,       show_ip_route_supernets_cmd,       "show ip route supernets-only",       SHOW_STR       IP_STR       "IP routing table\n"       "Show supernet entries only\n"){  struct route_table *table;  struct route_node *rn;  struct rib *rib;  u_int32_t addr;   int first = 1;  table = vrf_table (AFI_IP, SAFI_UNICAST, 0);  if (! table)    return CMD_SUCCESS;  /* Show matched type IPv4 routes. */  for (rn = route_top (table); rn; rn = route_next (rn))    for (rib = rn->info; rib; rib = rib->next)      {	addr = ntohl (rn->p.u.prefix4.s_addr);	if ((IN_CLASSC (addr) && rn->p.prefixlen < 24)	   || (IN_CLASSB (addr) && rn->p.prefixlen < 16)	   || (IN_CLASSA (addr) && rn->p.prefixlen < 8)) 	  {	    if (first)	      {		vty_out (vty, SHOW_ROUTE_V4_HEADER, VTY_NEWLINE,			 VTY_NEWLINE, VTY_NEWLINE);		first = 0;	      }	    vty_show_ip_route (vty, rn, rib);	  }      }  return CMD_SUCCESS;}DEFUN (show_ip_route_protocol,       show_ip_route_protocol_cmd,       "show ip route (bgp|connected|kernel|ospf|rip|static)",       SHOW_STR       IP_STR       "IP routing table\n"       "Border Gateway Protocol (BGP)\n"       "Connected\n"       "Kernel\n"       "Open Shortest Path First (OSPF)\n"       "Routing Information Protocol (RIP)\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_OSPF;  else if (strncmp (argv[0], "r", 1) == 0)    type = ZEBRA_ROUTE_RIP;  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_IP, SAFI_UNICAST, 0);  if (! table)    return CMD_SUCCESS;  /* Show matched type IPv4 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_V4_HEADER,		       VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);	      first = 0;	    }	  vty_show_ip_route (vty, rn, rib);	}  return CMD_SUCCESS;}DEFUN (show_ip_route_addr,       show_ip_route_addr_cmd,       "show ip route A.B.C.D",       SHOW_STR       IP_STR       "IP routing table\n"       "Network in the IP routing table to display\n"){  int ret;  struct prefix_ipv4 p;  struct route_table *table;  struct route_node *rn;  ret = str2prefix_ipv4 (argv[0], &p);  if (ret <= 0)    {      vty_out (vty, "%% Malformed IPv4 address%s", VTY_NEWLINE);      return CMD_WARNING;    }  table = vrf_table (AFI_IP, 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_ip_route_detail (vty, rn);  route_unlock_node (rn);  return CMD_SUCCESS;}DEFUN (show_ip_route_prefix,       show_ip_route_prefix_cmd,       "show ip route A.B.C.D/M",       SHOW_STR       IP_STR       "IP routing table\n"       "IP prefix <network>/<length>, e.g., 35.0.0.0/8\n"){  int ret;  struct prefix_ipv4 p;  struct route_table *table;  struct route_node *rn;  ret = str2prefix_ipv4 (argv[0], &p);  if (ret <= 0)    {      vty_out (vty, "%% Malformed IPv4 address%s", VTY_NEWLINE);      return CMD_WARNING;    }  table = vrf_table (AFI_IP, 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_ip_route_detail (vty, rn);  route_unlock_node (rn);  return CMD_SUCCESS;}voidzebra_show_ip_route (struct vty *vty, struct vrf *vrf){  vty_out (vty, "IP routing table name is %s(%d)%s",	   vrf->name ? vrf->name : "", vrf->id, VTY_NEWLINE);  vty_out (vty, "Route Source    Networks%s", VTY_NEWLINE);  vty_out (vty, "connected       %d%s", 0, VTY_NEWLINE);  vty_out (vty, "static          %d%s", 0, VTY_NEWLINE);  vty_out (vty, "rip             %d%s", 0, VTY_NEWLINE);  vty_out (vty, "bgp             %d%s", 0, VTY_NEWLINE);  vty_out (vty, " External: %d Internal: %d Local: %d%s",	   0, 0, 0, VTY_NEWLINE);  vty_out (vty, "ospf            %d%s", 0, VTY_NEWLINE);  vty_out (vty,	   "  Intra-area: %d Inter-area: %d External-1: %d External-2: %d%s",	   0, 0, 0, 0, VTY_NEWLINE);  vty_out (vty, "  NSSA External-1: %d NSSA External-2: %d%s",	   0, 0, VTY_NEWLINE);  vty_out (vty, "internal        %d%s", 0, VTY_NEWLINE);  vty_out (vty, "Total           %d%s", 0, VTY_NEWLINE);}/* Show route summary.  */DEFUN (show_ip_route_summary,       show_ip_route_summary_cmd,       "show ip route summary",       SHOW_STR       IP_STR       "IP routing table\n"       "Summary of all routes\n"){  struct vrf *vrf;  /* Default table id is zero.  */  vrf = vrf_lookup (0);  if (! vrf)    {      vty_out (vty, "%% No Default-IP-Routing-Table%s", VTY_NEWLINE);      return CMD_WARNING;    }  zebra_show_ip_route (vty, vrf);  return CMD_SUCCESS;}/* Write IPv4 static route configuration. */intstatic_config_ipv4 (struct vty *vty){  struct route_node *rn;  struct static_ipv4 *si;    struct route_table *stable;  int write;  write = 0;  /* Lookup table.  */  stable = vrf_static_table (AFI_IP, 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, "ip route %s/%d", inet_ntoa (rn->p.u.prefix4),		 rn->p.prefixlen);	switch (si->type)	  {	  case STATIC_IPV4_GATEWAY:	    vty_out (vty, " %s", inet_ntoa (si->gate.ipv4));	    break;	  case STATIC_IPV4_IFNAME:	    vty_out (vty, " %s", si->gate.ifname);	    break;	  case STATIC_IPV4_BLACKHOLE:	    vty_out (vty, " Null0");	    break;	  }	if (si->distance != ZEBRA_STATIC_DISTANCE_DEFAULT)	  vty_out (vty, " %d", si->distance);	vty_out (vty, "%s", VTY_NEWLINE);	write = 1;      }  return write;}#ifdef HAVE_IPV6/* General fucntion for IPv6 static route. */intstatic_ipv6_func (struct vty *vty, int add_cmd, char *dest_str,		  char *gate_str, char *ifname, char *distance_str){  int ret;  u_char distance;  struct prefix p;  struct in6_addr *gate = NULL;  struct in6_addr gate_addr;  u_char type = 0;  int table = 0;    ret = str2prefix (dest_str, &p);  if (ret <= 0)    {      vty_out (vty, "%% Malformed address%s", VTY_NEWLINE);      return CMD_WARNING;    }  /* Apply mask for given prefix. */  apply_mask (&p);  /* Administrative distance. */  if (distance_str)    distance = atoi (distance_str);  else    distance = ZEBRA_STATIC_DISTANCE_DEFAULT;  /* When gateway is valid IPv6 addrees, then gate is treated as     nexthop address other case gate is treated as interface name. */  ret = inet_pton (AF_INET6, gate_str, &gate_addr);  if (ifname)    {      /* When ifname is specified.  It must be come with gateway         address. */      if (ret != 1)	{	  vty_out (vty, "%% Malformed address%s", VTY_NEWLINE);	  return CMD_WARNING;	}      type = STATIC_IPV6_GATEWAY_IFNAME;      gate = &gate_addr;    }  else    {      if (ret == 1)	{	  type = STATIC_IPV6_GATEWAY;	  gate = &gate_addr;	}      else	{	  type = STATIC_IPV6_IFNAME;	  ifname = gate_str;	}    }  if (add_cmd)    static_add_ipv6 (&p, type, gate, ifname, distance, table);  else    static_delete_ipv6 (&p, type, gate, ifname, distance, table);  return CMD_SUCCESS;}DEFUN (ipv6_route,       ipv6_route_cmd,       "ipv6 route X:X::X:X/M (X:X::X:X|INTERFACE)",       IP_STR       "Establish static routes\n"       "IPv6 destination prefix (e.g. 3ffe:506::/32)\n"       "IPv6 gateway address\n"       "IPv6 gateway interface name\n"){  return static_ipv6_func (vty, 1, argv[0], argv[1], NULL, NULL);}DEFUN (ipv6_route_ifname,       ipv6_route_ifname_cmd,       "ipv6 route X:X::X:X/M X:X::X:X INTERFACE",       IP_STR       "Establish static routes\n"       "IPv6 destination prefix (e.g. 3ffe:506::/32)\n"       "IPv6 gateway address\n"       "IPv6 gateway interface name\n"){  return static_ipv6_func (vty, 1, argv[0], argv[1], argv[2], NULL);}DEFUN (ipv6_route_pref,       ipv6_route_pref_cmd,       "ipv6 route X:X::X:X/M (X:X::X:X|INTERFACE) <1-255>",       IP_STR       "Establish static routes\n"       "IPv6 destination prefix (e.g. 3ffe:506::/32)\n"       "IPv6 gateway address\n"       "IPv6 gateway interface name\n"       "Distance value for this prefix\n"){  return static_ipv6_func (vty, 1, argv[0], argv[1], NULL, argv[2]);}DEFUN (ipv6_route_ifname_pref,       ipv6_route_ifname_pref_cmd,       "ipv6 route X:X::X:X/M X:X::X:X INTERFACE <1-255>",       IP_STR       "Establish static routes\n"       "IPv6 destination prefix (e.g. 3ffe:506::/32)\n"       "IPv6 gateway address\n"       "IPv6 gateway interface name\n"       "Distance value for this prefix\n"){  return static_ipv6_func (vty, 1, argv[0], argv[1], argv[2], argv[3]);}DEFUN (no_ipv6_route,       no_ipv6_route_cmd,       "no ipv6 route X:X::X:X/M (X:X::X:X|INTERFACE)",       NO_STR       IP_STR       "Establish static routes\n"       "IPv6 destination prefix (e.g. 3ffe:506::/32)\n"       "IPv6 gateway address\n"       "IPv6 gateway interface name\n"){  return static_ipv6_func (vty, 0, argv[0], argv[1], NULL, NULL);}DEFUN (no_ipv6_route_ifname,       no_ipv6_route_ifname_cmd,       "no ipv6 route X:X::X:X/M X:X::X:X INTERFACE",       NO_STR       IP_STR       "Establish static routes\n"       "IPv6 destination prefix (e.g. 3ffe:506::/32)\n"       "IPv6 gateway address\n"       "IPv6 gateway interface name\n"){  return static_ipv6_func (vty, 0, argv[0], argv[1], argv[2], NULL);}DEFUN (no_ipv6_route_pref,       no_ipv6_route_pref_cmd,       "no ipv6 route X:X::X:X/M (X:X::X:X|INTERFACE) <1-255>",       NO_STR       IP_STR       "Establish static routes\n"       "IPv6 destination prefix (e.g. 3ffe:506::/32)\n"       "IPv6 gateway address\n"       "IPv6 gateway interface name\n"       "Distance value for this prefix\n"){  return static_ipv6_func (vty, 0, argv[0], argv[1], NULL, argv[2]);}DEFUN (no_ipv6_route_ifname_pref,       no_ipv6_route_ifname_pref_cmd,       "no ipv6 route X:X::X:X/M X:X::X:X INTERFACE <1-255>",       NO_STR       IP_STR       "Establish static routes\n"       "IPv6 destination prefix (e.g. 3ffe:506::/32)\n"       "IPv6 gateway address\n"       "IPv6 gateway interface name\n"       "Distance value for this prefix\n"){  return static_ipv6_func (vty, 0, argv[0], argv[1], argv[2], argv[3]);}/* New RIB.  Detailed information for IPv4 route. */voidvty_show_ipv6_route_detail (struct vty *vty, struct route_node *rn){  struct rib *rib;  struct nexthop *nexthop;  char buf[BUFSIZ];  for (rib = rn->info; rib; rib = rib->next)    {      vty_out (vty, "Routing entry for %s/%d%s", 	       inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ),	       rn->p.prefixlen,	       VTY_NEWLINE);      vty_out (vty, "  Known via \"%s\"", route_type_str (rib->type));      vty_out (vty, ", distance %d, metric %d", rib->distance, rib->metric);      if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))	vty_out (vty, ", best");      if (rib->refcnt)	vty_out (vty, ", refcnt %ld", rib->refcnt);      vty_out (vty, "%s", VTY_NEWLINE);#define ONE_DAY_SECOND 60*60*24#define ONE_WEEK_SECOND 60*60*24*7      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);	  vty_out (vty, "  Last update ");	  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", 

⌨️ 快捷键说明

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