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

📄 interface.c

📁 大名鼎鼎的路由器源码。程序分ZEBRA、OSPFRIP等3个包。程序框架采用一个路由协议一个进程的方式
💻 C
📖 第 1 页 / 共 3 页
字号:
      listnode_add (ifp->connected, ifc);    }  /* This address is configured from zebra. */  if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))    SET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);  /* In case of this route need to install kernel. */  if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)      && CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))    {      /* Some system need to up the interface to set IP address. */      if (! if_is_up (ifp))	{	  if_set_flags (ifp, IFF_UP | IFF_RUNNING);	  if_refresh (ifp);	}      ret = if_set_prefix (ifp, ifc);      if (ret < 0)	{	  vty_out (vty, "%% Can't set interface IP address: %s.%s", 		   strerror(errno), VTY_NEWLINE);	  return CMD_WARNING;	}      /* IP address propery set. */      SET_FLAG (ifc->conf, ZEBRA_IFC_REAL);      /* Update interface address information to protocol daemon. */      zebra_interface_address_add_update (ifp, ifc);      /* If interface is up register connected route. */      if (if_is_up(ifp))	connected_up_ipv4 (ifp, ifc);    }  return CMD_SUCCESS;}intip_address_uninstall (struct vty *vty, struct interface *ifp, char *addr_str,		      char *peer_str, char *label, int secondry){  struct prefix_ipv4 cp;  struct connected *ifc;  int ret;  /* Convert to prefix structure. */  ret = str2prefix_ipv4 (addr_str, &cp);  if (ret <= 0)    {      vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);      return CMD_WARNING;    }  /* Check current interface address. */  ifc = connected_check_ipv4 (ifp, (struct prefix *) &cp);  if (! ifc)    {      vty_out (vty, "%% Can't find address%s", VTY_NEWLINE);      return CMD_WARNING;    }  /* This is not configured address. */  if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))    return CMD_WARNING;  /* This is not real address or interface is not active. */  if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)      || ! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))    {      listnode_delete (ifp->connected, ifc);      connected_free (ifc);      return CMD_WARNING;    }  /* This is real route. */  ret = if_unset_prefix (ifp, ifc);  if (ret < 0)    {      vty_out (vty, "%% Can't unset interface IP address: %s.%s", 	       strerror(errno), VTY_NEWLINE);      return CMD_WARNING;    }  /* Redistribute this information. */  zebra_interface_address_delete_update (ifp, ifc);  /* Remove connected route. */  connected_down_ipv4 (ifp, ifc);  /* Free address information. */  listnode_delete (ifp->connected, ifc);  connected_free (ifc);  return CMD_SUCCESS;}DEFUN (ip_address,       ip_address_cmd,       "ip address A.B.C.D/M",       "Interface Internet Protocol config commands\n"       "Set the IP address of an interface\n"       "IP address (e.g. 10.0.0.1/8)\n"){  return ip_address_install (vty, vty->index, argv[0], NULL, NULL, 0);}DEFUN (no_ip_address,       no_ip_address_cmd,       "no ip address A.B.C.D/M",       NO_STR       "Interface Internet Protocol config commands\n"       "Set the IP address of an interface\n"       "IP Address (e.g. 10.0.0.1/8)"){  return ip_address_uninstall (vty, vty->index, argv[0], NULL, NULL, 0);}#ifdef HAVE_NETLINKDEFUN (ip_address_secondary,       ip_address_secondary_cmd,       "ip address A.B.C.D/M secondary",       "Interface Internet Protocol config commands\n"       "Set the IP address of an interface\n"       "IP address (e.g. 10.0.0.1/8)\n"       "Secondary IP address\n"){  return ip_address_install (vty, vty->index, argv[0], NULL, NULL, 1);}DEFUN (ip_address_label,       ip_address_label_cmd,       "ip address A.B.C.D/M label LINE",       "Interface Internet Protocol config commands\n"       "Set the IP address of an interface\n"       "IP address (e.g. 10.0.0.1/8)\n"       "Label of this address\n"       "Label\n"){  return ip_address_install (vty, vty->index, argv[0], NULL, argv[1], 1);}DEFUN (no_ip_address_secondary,       no_ip_address_secondary_cmd,       "no ip address A.B.C.D/M secondary",       NO_STR       "Interface Internet Protocol config commands\n"       "Set the IP address of an interface\n"       "IP address (e.g. 10.0.0.1/8)\n"       "Secondary IP address\n"){  return ip_address_uninstall (vty, vty->index, argv[0], NULL, NULL, 1);}DEFUN (no_ip_address_label,       no_ip_address_label_cmd,       "no ip address A.B.C.D/M label LINE",       NO_STR       "Interface Internet Protocol config commands\n"       "Set the IP address of an interface\n"       "IP address (e.g. 10.0.0.1/8)\n"       "Label of this address\n"       "Label\n"){  return ip_address_uninstall (vty, vty->index, argv[0], NULL, argv[1], 1);}#endif /* HAVE_NETLINK */#ifdef HAVE_IPV6intipv6_address_install (struct vty *vty, struct interface *ifp, char *addr_str,		      char *peer_str, char *label, int secondary){  struct prefix_ipv6 cp;  struct connected *ifc;  struct prefix_ipv6 *p;  int ret;  ret = str2prefix_ipv6 (addr_str, &cp);  if (ret <= 0)    {      vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);      return CMD_WARNING;    }  ifc = connected_check_ipv6 (ifp, (struct prefix *) &cp);  if (! ifc)    {      ifc = connected_new ();      ifc->ifp = ifp;      /* Address. */      p = prefix_ipv6_new ();      *p = cp;      ifc->address = (struct prefix *) p;      /* Secondary. */      if (secondary)	SET_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY);      /* Label. */      if (label)	ifc->label = strdup (label);      /* Add to linked list. */      listnode_add (ifp->connected, ifc);    }  /* This address is configured from zebra. */  if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))    SET_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED);  /* In case of this route need to install kernel. */  if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)      && CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))    {      /* Some system need to up the interface to set IP address. */      if (! if_is_up (ifp))	{	  if_set_flags (ifp, IFF_UP | IFF_RUNNING);	  if_refresh (ifp);	}      ret = if_prefix_add_ipv6 (ifp, ifc);      if (ret < 0)	{	  vty_out (vty, "%% Can't set interface IP address: %s.%s", 		   strerror(errno), VTY_NEWLINE);	  return CMD_WARNING;	}      /* IP address propery set. */      SET_FLAG (ifc->conf, ZEBRA_IFC_REAL);      /* Update interface address information to protocol daemon. */      zebra_interface_address_add_update (ifp, ifc);      /* If interface is up register connected route. */      if (if_is_up(ifp))	connected_up_ipv6 (ifp, ifc);    }  return CMD_SUCCESS;}intipv6_address_uninstall (struct vty *vty, struct interface *ifp, char *addr_str,			char *peer_str, char *label, int secondry){  struct prefix_ipv6 cp;  struct connected *ifc;  int ret;  /* Convert to prefix structure. */  ret = str2prefix_ipv6 (addr_str, &cp);  if (ret <= 0)    {      vty_out (vty, "%% Malformed address %s", VTY_NEWLINE);      return CMD_WARNING;    }  /* Check current interface address. */  ifc = connected_check_ipv6 (ifp, (struct prefix *) &cp);  if (! ifc)    {      vty_out (vty, "%% Can't find address%s", VTY_NEWLINE);      return CMD_WARNING;    }  /* This is not configured address. */  if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))    return CMD_WARNING;  /* This is not real address or interface is not active. */  if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL)      || ! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))    {      listnode_delete (ifp->connected, ifc);      connected_free (ifc);      return CMD_WARNING;    }  /* This is real route. */  ret = if_prefix_delete_ipv6 (ifp, ifc);  if (ret < 0)    {      vty_out (vty, "%% Can't unset interface IP address: %s.%s", 	       strerror(errno), VTY_NEWLINE);      return CMD_WARNING;    }  /* Redistribute this information. */  zebra_interface_address_delete_update (ifp, ifc);  /* Remove connected route. */  connected_down_ipv6 (ifp, ifc);  /* Free address information. */  listnode_delete (ifp->connected, ifc);  connected_free (ifc);  return CMD_SUCCESS;}DEFUN (ipv6_address,       ipv6_address_cmd,       "ipv6 address X:X::X:X/M",       "Interface Internet Protocol config commands\n"       "Set the IP address of an interface\n"       "IPv6 address (e.g. 3ffe:506::1/48)\n"){  return ipv6_address_install (vty, vty->index, argv[0], NULL, NULL, 0);}DEFUN (no_ipv6_address,       no_ipv6_address_cmd,       "no ipv6 address X:X::X:X/M",       NO_STR       "Interface Internet Protocol config commands\n"       "Set the IP address of an interface\n"       "IPv6 address (e.g. 3ffe:506::1/48)\n"){  return ipv6_address_uninstall (vty, vty->index, argv[0], NULL, NULL, 0);}#endif /* HAVE_IPV6 */#ifdef KAMEDEFUN (ip_tunnel,       ip_tunnel_cmd,       "ip tunnel IP_address IP_address",       "KAME ip tunneling configuration commands\n"       "Set FROM IP address and TO IP address\n"){  return CMD_SUCCESS;}DEFUN (no_ip_tunnel, no_ip_tunnel_cmd,       "no ip tunnel",       NO_STR       "Set FROM IP address and TO IP address\n"){  return CMD_SUCCESS;}#endif /* KAME */intif_config_write (struct vty *vty){  listnode node;  struct interface *ifp;  char buf[BUFSIZ];  for (node = listhead (iflist); node; nextnode (node))    {      struct zebra_if *if_data;      listnode addrnode;      struct connected *ifc;      struct prefix *p;      ifp = getdata (node);      if_data = ifp->info;            vty_out (vty, "interface %s%s", ifp->name,	       VTY_NEWLINE);      if (ifp->desc)	vty_out (vty, " description %s%s", ifp->desc,		 VTY_NEWLINE);      /* Assign bandwidth here to avoid unnecessary interface flap	 while processing config script */      if (ifp->bandwidth != 0)	vty_out(vty, " bandwidth %u%s", ifp->bandwidth, VTY_NEWLINE);       for (addrnode = listhead (ifp->connected); addrnode; nextnode (addrnode))	  {	    ifc = getdata (addrnode);	    if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))	      {		p = ifc->address;		vty_out (vty, " ip%s address %s/%d",			 p->family == AF_INET ? "" : "v6",			 inet_ntop (p->family, &p->u.prefix, buf, BUFSIZ),			 p->prefixlen);		if (CHECK_FLAG (ifc->flags, ZEBRA_IFA_SECONDARY))		  vty_out (vty, " secondary");		  		if (ifc->label)		  vty_out (vty, " label %s", ifc->label);		vty_out (vty, "%s", VTY_NEWLINE);	      }	  }      if (if_data)	{	  if (if_data->shutdown == IF_ZEBRA_SHUTDOWN_ON)	    vty_out (vty, " shutdown%s", VTY_NEWLINE);	  if (if_data->multicast != IF_ZEBRA_MULTICAST_UNSPEC)	    vty_out (vty, " %smulticast%s",		     if_data->multicast == IF_ZEBRA_MULTICAST_ON ? "" : "no ",		     VTY_NEWLINE);	}#ifdef RTADV      rtadv_config_write (vty, ifp);#endif /* RTADV */      vty_out (vty, "!%s", VTY_NEWLINE);    }  return 0;}/* Allocate and initialize interface vector. */voidzebra_if_init (){  /* Initialize interface and new hook. */  if_init ();  if_add_hook (IF_NEW_HOOK, if_zebra_new_hook);  if_add_hook (IF_DELETE_HOOK, if_zebra_delete_hook);    /* Install configuration write function. */  install_node (&interface_node, if_config_write);  install_element (VIEW_NODE, &show_interface_cmd);  install_element (ENABLE_NODE, &show_interface_cmd);  install_element (CONFIG_NODE, &zebra_interface_cmd);  install_element (CONFIG_NODE, &no_zebra_interface_cmd);  install_default (INTERFACE_NODE);  install_element (INTERFACE_NODE, &interface_desc_cmd);  install_element (INTERFACE_NODE, &no_interface_desc_cmd);  install_element (INTERFACE_NODE, &multicast_cmd);  install_element (INTERFACE_NODE, &no_multicast_cmd);  install_element (INTERFACE_NODE, &shutdown_if_cmd);  install_element (INTERFACE_NODE, &no_shutdown_if_cmd);  install_element (INTERFACE_NODE, &bandwidth_if_cmd);  install_element (INTERFACE_NODE, &no_bandwidth_if_cmd);  install_element (INTERFACE_NODE, &no_bandwidth_if_val_cmd);  install_element (INTERFACE_NODE, &ip_address_cmd);  install_element (INTERFACE_NODE, &no_ip_address_cmd);#ifdef HAVE_IPV6  install_element (INTERFACE_NODE, &ipv6_address_cmd);  install_element (INTERFACE_NODE, &no_ipv6_address_cmd);#endif /* HAVE_IPV6 */#ifdef KAME  install_element (INTERFACE_NODE, &ip_tunnel_cmd);  install_element (INTERFACE_NODE, &no_ip_tunnel_cmd);#endif /* KAME */#ifdef HAVE_NETLINK  install_element (INTERFACE_NODE, &ip_address_secondary_cmd);  install_element (INTERFACE_NODE, &ip_address_label_cmd);  install_element (INTERFACE_NODE, &no_ip_address_secondary_cmd);  install_element (INTERFACE_NODE, &no_ip_address_label_cmd);#endif /* HAVE_NETLINK */}

⌨️ 快捷键说明

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