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

📄 vtysh.c

📁 大名鼎鼎的路由器源码。程序分ZEBRA、OSPFRIP等3个包。程序框架采用一个路由协议一个进程的方式
💻 C
📖 第 1 页 / 共 3 页
字号:
  vtysh_config_dump (fp);  if (vtysh_pager_name && fp)    {      fflush (fp);      if (pclose (fp) == -1)	{	  perror ("pclose");	  exit (1);	}      fp = NULL;    }  return CMD_SUCCESS;}DEFUN (vtysh_write_memory,       vtysh_write_memory_cmd,       "write memory",       "Write running configuration to memory, network, or terminal\n"       "Write configuration to the file (same as write file)\n"){  int ret;  mode_t old_umask;  char line[] = "write terminal\n";  FILE *fp;  char *integrate_sav = NULL;  /* config files have 0600 perms... */   old_umask = umask (0077);  integrate_sav = malloc (strlen (integrate_default) 			    + strlen (CONF_BACKUP_EXT) + 1);  strcpy (integrate_sav, integrate_default);  strcat (integrate_sav, CONF_BACKUP_EXT);  printf ("Building Configuration...\n");  /* Move current configuration file to backup config file */  unlink (integrate_sav);  rename (integrate_default, integrate_sav);  fp = fopen (integrate_default, "w");  if (fp == NULL)    {      printf ("%% Can't open configuration file %s.\n", integrate_default);      umask (old_umask);      return CMD_SUCCESS;    }  else    printf ("[OK]\n");	    vtysh_config_write (fp);  ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_ZEBRA], line);  ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIP], line);  ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_RIPNG], line);  ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF], line);  ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_OSPF6], line);  ret = vtysh_client_config (&vtysh_client[VTYSH_INDEX_BGP], line);  vtysh_config_dump (fp);  fclose (fp);  umask (old_umask);  return CMD_SUCCESS;}ALIAS (vtysh_write_memory,       vtysh_copy_runningconfig_startupconfig_cmd,       "copy running-config startup-config",         "Copy from one file to another\n"       "Copy from current system configuration\n"       "Copy to startup configuration\n");ALIAS (vtysh_write_memory,       vtysh_write_file_cmd,       "write file",       "Write running configuration to memory, network, or terminal\n"       "Write configuration to the file (same as write memory)\n");ALIAS (vtysh_write_terminal,       vtysh_show_running_config_cmd,       "show running-config",       SHOW_STR       "Current operating configuration\n");/* Execute command in child process. */intexecute_command (char *command, int argc, char *arg1, char *arg2){  int ret;  pid_t pid;  int status;  /* Call fork(). */  pid = fork ();  if (pid < 0)    {      /* Failure of fork(). */      fprintf (stderr, "Can't fork: %s\n", strerror (errno));      exit (1);    }  else if (pid == 0)    {      /* This is child process. */      switch (argc)	{	case 0:	  ret = execlp (command, command, NULL);	  break;	case 1:	  ret = execlp (command, command, arg1, NULL);	  break;	case 2:	  ret = execlp (command, command, arg1, arg2, NULL);	  break;	}      /* When execlp suceed, this part is not executed. */      fprintf (stderr, "Can't execute %s: %s\n", command, strerror (errno));      exit (1);    }  else    {      /* This is parent. */      execute_flag = 1;      ret = wait4 (pid, &status, 0, NULL);      execute_flag = 0;    }  return 0;}DEFUN (vtysh_ping,       vtysh_ping_cmd,       "ping WORD",       "send echo messages\n"       "Ping destination address or hostname\n"){  execute_command ("ping", 1, argv[0], NULL);  return CMD_SUCCESS;}DEFUN (vtysh_traceroute,       vtysh_traceroute_cmd,       "traceroute WORD",       "Trace route to destination\n"       "Trace route to destination address or hostname\n"){  execute_command ("traceroute", 1, argv[0], NULL);  return CMD_SUCCESS;}DEFUN (vtysh_telnet,       vtysh_telnet_cmd,       "telnet WORD",       "Open a telnet connection\n"       "IP address or hostname of a remote system\n"){  execute_command ("telnet", 1, argv[0], NULL);  return CMD_SUCCESS;}DEFUN (vtysh_telnet_port,       vtysh_telnet_port_cmd,       "telnet WORD PORT",       "Open a telnet connection\n"       "IP address or hostname of a remote system\n"       "TCP Port number\n"){  execute_command ("telnet", 2, argv[0], argv[1]);  return CMD_SUCCESS;}DEFUN (vtysh_start_shell,       vtysh_start_shell_cmd,       "start-shell",       "Start UNIX shell\n"){  execute_command ("sh", 0, NULL, NULL);  return CMD_SUCCESS;}DEFUN (vtysh_start_bash,       vtysh_start_bash_cmd,       "start-shell bash",       "Start UNIX shell\n"       "Start bash\n"){  execute_command ("bash", 0, NULL, NULL);  return CMD_SUCCESS;}DEFUN (vtysh_start_zsh,       vtysh_start_zsh_cmd,       "start-shell zsh",       "Start UNIX shell\n"       "Start Z shell\n"){  execute_command ("zsh", 0, NULL, NULL);  return CMD_SUCCESS;}/* Route map node structure. */struct cmd_node rmap_node ={  RMAP_NODE,  "%s(config-route-map)# "};/* Zebra node structure. */struct cmd_node zebra_node ={  ZEBRA_NODE,  "%s(config-router)# "};struct cmd_node bgp_vpnv4_node ={  BGP_VPNV4_NODE,  "%s(config-router-af)# "};struct cmd_node bgp_ipv4_node ={  BGP_IPV4_NODE,  "%s(config-router-af)# "};struct cmd_node bgp_ipv4m_node ={  BGP_IPV4M_NODE,  "%s(config-router-af)# "};struct cmd_node bgp_ipv6_node ={  BGP_IPV6_NODE,  "%s(config-router-af)# "};struct cmd_node ospf_node ={  OSPF_NODE,  "%s(config-router)# "};/* RIPng node structure. */struct cmd_node ripng_node ={  RIPNG_NODE,  "%s(config-router)# "};/* OSPF6 node structure. */struct cmd_node ospf6_node ={  OSPF6_NODE,  "%s(config-ospf6)# "};struct cmd_node keychain_node ={  KEYCHAIN_NODE,  "%s(config-keychain)# "};struct cmd_node keychain_key_node ={  KEYCHAIN_KEY_NODE,  "%s(config-keychain-key)# "};voidvtysh_install_default (enum node_type node){  install_element (node, &config_list_cmd);}/* Making connection to protocol daemon. */intvtysh_connect (struct vtysh_client *vclient, char *path){  int ret;  int sock, len;  struct sockaddr_un addr;  struct stat s_stat;  uid_t euid;  gid_t egid;  memset (vclient, 0, sizeof (struct vtysh_client));  vclient->fd = -1;  /* Stat socket to see if we have permission to access it. */  euid = geteuid();  egid = getegid();  ret = stat (path, &s_stat);  if (ret < 0 && errno != ENOENT)    {      fprintf  (stderr, "vtysh_connect(%s): stat = %s\n", 		path, strerror(errno));       exit(1);    }    if (ret >= 0)    {      if (! S_ISSOCK(s_stat.st_mode))	{	  fprintf (stderr, "vtysh_connect(%s): Not a socket\n",		   path);	  exit (1);	}            if (euid != s_stat.st_uid 	  || !(s_stat.st_mode & S_IWUSR)	  || !(s_stat.st_mode & S_IRUSR))	{	  fprintf (stderr, "vtysh_connect(%s): No permission to access socket\n",		   path);	  exit (1);	}    }  sock = socket (AF_UNIX, SOCK_STREAM, 0);  if (sock < 0)    {#ifdef DEBUG      fprintf(stderr, "vtysh_connect(%s): socket = %s\n", path, strerror(errno));#endif /* DEBUG */      return -1;    }  memset (&addr, 0, sizeof (struct sockaddr_un));  addr.sun_family = AF_UNIX;  strncpy (addr.sun_path, path, strlen (path));#ifdef HAVE_SUN_LEN  len = addr.sun_len = SUN_LEN(&addr);#else  len = sizeof (addr.sun_family) + strlen (addr.sun_path);#endif /* HAVE_SUN_LEN */  ret = connect (sock, (struct sockaddr *) &addr, len);  if (ret < 0)    {#ifdef DEBUG      fprintf(stderr, "vtysh_connect(%s): connect = %s\n", path, strerror(errno));#endif /* DEBUG */      close (sock);      return -1;    }  vclient->fd = sock;  return 0;}voidvtysh_connect_all(){  /* Clear each daemons client structure. */  vtysh_connect (&vtysh_client[VTYSH_INDEX_ZEBRA], ZEBRA_PATH);  vtysh_connect (&vtysh_client[VTYSH_INDEX_RIP], RIP_PATH);  vtysh_connect (&vtysh_client[VTYSH_INDEX_RIPNG], RIPNG_PATH);  vtysh_connect (&vtysh_client[VTYSH_INDEX_OSPF], OSPF_PATH);  vtysh_connect (&vtysh_client[VTYSH_INDEX_OSPF6], OSPF6_PATH);  vtysh_connect (&vtysh_client[VTYSH_INDEX_BGP], BGP_PATH);}/* To disable readline's filename completion */intvtysh_completion_entry_function (int ignore, int invoking_key){  return 0;}voidvtysh_readline_init (){  /* readline related settings. */  rl_bind_key ('?', vtysh_rl_describe);  rl_completion_entry_function = vtysh_completion_entry_function;  rl_attempted_completion_function = (CPPFunction *)new_completion;  /* do not append space after completion. It will be appended     in new_completion() function explicitly */  rl_completion_append_character = '\0';}char *vtysh_prompt (){  struct utsname names;  static char buf[100];  const char*hostname;  extern struct host host;  hostname = host.name;  if (!hostname)    {      uname (&names);      hostname = names.nodename;    }  snprintf (buf, sizeof buf, cmd_prompt (vty->node), hostname);  return buf;}voidvtysh_init_vty (){  /* Make vty structure. */  vty = vty_new ();  vty->type = VTY_SHELL;  vty->node = VIEW_NODE;  /* Initialize commands. */  cmd_init (0);  /* Install nodes. */  install_node (&bgp_node, NULL);  install_node (&rip_node, NULL);  install_node (&interface_node, NULL);  install_node (&rmap_node, NULL);  install_node (&zebra_node, NULL);  install_node (&bgp_vpnv4_node, NULL);  install_node (&bgp_ipv4_node, NULL);  install_node (&bgp_ipv4m_node, NULL);/* #ifdef HAVE_IPV6 */  install_node (&bgp_ipv6_node, NULL);/* #endif */  install_node (&ospf_node, NULL);/* #ifdef HAVE_IPV6 */  install_node (&ripng_node, NULL);  install_node (&ospf6_node, NULL);/* #endif */  install_node (&keychain_node, NULL);  install_node (&keychain_key_node, NULL);  vtysh_install_default (VIEW_NODE);  vtysh_install_default (ENABLE_NODE);  vtysh_install_default (CONFIG_NODE);  vtysh_install_default (BGP_NODE);  vtysh_install_default (RIP_NODE);  vtysh_install_default (INTERFACE_NODE);  vtysh_install_default (RMAP_NODE);  vtysh_install_default (ZEBRA_NODE);  vtysh_install_default (BGP_VPNV4_NODE);  vtysh_install_default (BGP_IPV4_NODE);  vtysh_install_default (BGP_IPV4M_NODE);  vtysh_install_default (BGP_IPV6_NODE);  vtysh_install_default (OSPF_NODE);  vtysh_install_default (RIPNG_NODE);  vtysh_install_default (OSPF6_NODE);  vtysh_install_default (KEYCHAIN_NODE);  vtysh_install_default (KEYCHAIN_KEY_NODE);  install_element (VIEW_NODE, &vtysh_enable_cmd);  install_element (ENABLE_NODE, &vtysh_config_terminal_cmd);  install_element (ENABLE_NODE, &vtysh_disable_cmd);  /* "exit" command. */  install_element (VIEW_NODE, &vtysh_exit_all_cmd);  install_element (VIEW_NODE, &vtysh_quit_all_cmd);  install_element (CONFIG_NODE, &vtysh_exit_all_cmd);  /* install_element (CONFIG_NODE, &vtysh_quit_all_cmd); */  install_element (ENABLE_NODE, &vtysh_exit_all_cmd);  install_element (ENABLE_NODE, &vtysh_quit_all_cmd);  install_element (RIP_NODE, &vtysh_exit_ripd_cmd);  install_element (RIP_NODE, &vtysh_quit_ripd_cmd);  install_element (RIPNG_NODE, &vtysh_exit_ripngd_cmd);  install_element (RIPNG_NODE, &vtysh_quit_ripngd_cmd);  install_element (OSPF_NODE, &vtysh_exit_ospfd_cmd);  install_element (OSPF_NODE, &vtysh_quit_ospfd_cmd);  install_element (OSPF6_NODE, &vtysh_exit_ospf6d_cmd);  install_element (OSPF6_NODE, &vtysh_quit_ospf6d_cmd);  install_element (BGP_NODE, &vtysh_exit_bgpd_cmd);  install_element (BGP_NODE, &vtysh_quit_bgpd_cmd);  install_element (BGP_VPNV4_NODE, &vtysh_exit_bgpd_cmd);  install_element (BGP_VPNV4_NODE, &vtysh_quit_bgpd_cmd);  install_element (BGP_IPV4_NODE, &vtysh_exit_bgpd_cmd);  install_element (BGP_IPV4_NODE, &vtysh_quit_bgpd_cmd);  install_element (BGP_IPV4M_NODE, &vtysh_exit_bgpd_cmd);  install_element (BGP_IPV4M_NODE, &vtysh_quit_bgpd_cmd);  install_element (BGP_IPV6_NODE, &vtysh_exit_bgpd_cmd);  install_element (BGP_IPV6_NODE, &vtysh_quit_bgpd_cmd);  install_element (KEYCHAIN_NODE, &vtysh_exit_ripd_cmd);  install_element (KEYCHAIN_NODE, &vtysh_quit_ripd_cmd);  install_element (KEYCHAIN_KEY_NODE, &vtysh_exit_ripd_cmd);  install_element (KEYCHAIN_KEY_NODE, &vtysh_quit_ripd_cmd);  install_element (RMAP_NODE, &vtysh_exit_rmap_cmd);  install_element (RMAP_NODE, &vtysh_quit_rmap_cmd);  /* "end" command. */  install_element (CONFIG_NODE, &vtysh_end_all_cmd);  install_element (ENABLE_NODE, &vtysh_end_all_cmd);  install_element (RIP_NODE, &vtysh_end_all_cmd);  install_element (RIPNG_NODE, &vtysh_end_all_cmd);  install_element (OSPF_NODE, &vtysh_end_all_cmd);  install_element (OSPF6_NODE, &vtysh_end_all_cmd);  install_element (BGP_NODE, &vtysh_end_all_cmd);  install_element (BGP_IPV4_NODE, &vtysh_end_all_cmd);  install_element (BGP_IPV4M_NODE, &vtysh_end_all_cmd);  install_element (BGP_VPNV4_NODE, &vtysh_end_all_cmd);  install_element (BGP_IPV6_NODE, &vtysh_end_all_cmd);  install_element (KEYCHAIN_NODE, &vtysh_end_all_cmd);  install_element (KEYCHAIN_KEY_NODE, &vtysh_end_all_cmd);  install_element (RMAP_NODE, &vtysh_end_all_cmd);  install_element (INTERFACE_NODE, &vtysh_end_all_cmd);  install_element (INTERFACE_NODE, &vtysh_exit_interface_cmd);  install_element (INTERFACE_NODE, &vtysh_quit_interface_cmd);  install_element (CONFIG_NODE, &router_rip_cmd);#ifdef HAVE_IPV6  install_element (CONFIG_NODE, &router_ripng_cmd);#endif  install_element (CONFIG_NODE, &router_ospf_cmd);#ifdef HAVE_IPV6  install_element (CONFIG_NODE, &router_ospf6_cmd);#endif  install_element (CONFIG_NODE, &router_bgp_cmd);  install_element (BGP_NODE, &address_family_vpnv4_cmd);  install_element (BGP_NODE, &address_family_vpnv4_unicast_cmd);  install_element (BGP_NODE, &address_family_ipv4_unicast_cmd);  install_element (BGP_NODE, &address_family_ipv4_multicast_cmd);#ifdef HAVE_IPV6  install_element (BGP_NODE, &address_family_ipv6_cmd);  install_element (BGP_NODE, &address_family_ipv6_unicast_cmd);#endif  install_element (BGP_VPNV4_NODE, &exit_address_family_cmd);  install_element (BGP_IPV4_NODE, &exit_address_family_cmd);  install_element (BGP_IPV4M_NODE, &exit_address_family_cmd);  install_element (BGP_IPV6_NODE, &exit_address_family_cmd);  install_element (CONFIG_NODE, &key_chain_cmd);  install_element (CONFIG_NODE, &route_map_cmd);  install_element (KEYCHAIN_NODE, &key_cmd);  install_element (KEYCHAIN_NODE, &key_chain_cmd);  install_element (KEYCHAIN_KEY_NODE, &key_chain_cmd);  install_element (CONFIG_NODE, &vtysh_interface_cmd);  install_element (ENABLE_NODE, &vtysh_show_running_config_cmd);  install_element (ENABLE_NODE, &vtysh_copy_runningconfig_startupconfig_cmd);  install_element (ENABLE_NODE, &vtysh_write_file_cmd);  /* write terminal command */  install_element (ENABLE_NODE, &vtysh_write_terminal_cmd);  install_element (CONFIG_NODE, &vtysh_write_terminal_cmd);  install_element (BGP_NODE, &vtysh_write_terminal_cmd);  install_element (BGP_VPNV4_NODE, &vtysh_write_terminal_cmd);  install_element (BGP_IPV4_NODE, &vtysh_write_terminal_cmd);  install_element (BGP_IPV4M_NODE, &vtysh_write_terminal_cmd);  install_element (BGP_IPV6_NODE, &vtysh_write_terminal_cmd);  install_element (RIP_NODE, &vtysh_write_terminal_cmd);  install_element (RIPNG_NODE, &vtysh_write_terminal_cmd);  install_element (OSPF_NODE, &vtysh_write_terminal_cmd);  install_element (OSPF6_NODE, &vtysh_write_terminal_cmd);  install_element (INTERFACE_NODE, &vtysh_write_terminal_cmd);  install_element (RMAP_NODE, &vtysh_write_terminal_cmd);  install_element (KEYCHAIN_NODE, &vtysh_write_terminal_cmd);  install_element (KEYCHAIN_KEY_NODE, &vtysh_write_terminal_cmd);  /* write memory command */  install_element (ENABLE_NODE, &vtysh_write_memory_cmd);  install_element (CONFIG_NODE, &vtysh_write_memory_cmd);  install_element (BGP_NODE, &vtysh_write_memory_cmd);  install_element (BGP_VPNV4_NODE, &vtysh_write_memory_cmd);  install_element (BGP_IPV4_NODE, &vtysh_write_memory_cmd);  install_element (BGP_IPV4M_NODE, &vtysh_write_memory_cmd);  install_element (BGP_IPV6_NODE, &vtysh_write_memory_cmd);  install_element (RIP_NODE, &vtysh_write_memory_cmd);  install_element (RIPNG_NODE, &vtysh_write_memory_cmd);  install_element (OSPF_NODE, &vtysh_write_memory_cmd);  install_element (OSPF6_NODE, &vtysh_write_memory_cmd);  install_element (INTERFACE_NODE, &vtysh_write_memory_cmd);  install_element (RMAP_NODE, &vtysh_write_memory_cmd);  install_element (KEYCHAIN_NODE, &vtysh_write_memory_cmd);  install_element (KEYCHAIN_KEY_NODE, &vtysh_write_memory_cmd);  install_element (VIEW_NODE, &vtysh_ping_cmd);  install_element (VIEW_NODE, &vtysh_traceroute_cmd);  install_element (VIEW_NODE, &vtysh_telnet_cmd);  install_element (VIEW_NODE, &vtysh_telnet_port_cmd);  install_element (ENABLE_NODE, &vtysh_ping_cmd);  install_element (ENABLE_NODE, &vtysh_traceroute_cmd);  install_element (ENABLE_NODE, &vtysh_telnet_cmd);  install_element (ENABLE_NODE, &vtysh_telnet_port_cmd);  install_element (ENABLE_NODE, &vtysh_start_shell_cmd);  install_element (ENABLE_NODE, &vtysh_start_bash_cmd);  install_element (ENABLE_NODE, &vtysh_start_zsh_cmd);  install_element (CONFIG_NODE, &vtysh_log_stdout_cmd);  install_element (CONFIG_NODE, &no_vtysh_log_stdout_cmd);  install_element (CONFIG_NODE, &vtysh_log_file_cmd);  install_element (CONFIG_NODE, &no_vtysh_log_file_cmd);  install_element (CONFIG_NODE, &vtysh_log_syslog_cmd);  install_element (CONFIG_NODE, &no_vtysh_log_syslog_cmd);  install_element (CONFIG_NODE, &vtysh_log_trap_cmd);  install_element (CONFIG_NODE, &no_vtysh_log_trap_cmd);  install_element (CONFIG_NODE, &vtysh_log_record_priority_cmd);  install_element (CONFIG_NODE, &no_vtysh_log_record_priority_cmd);}

⌨️ 快捷键说明

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