vtysh.c

来自「zebra测试源代码用于 SOCKET 通信」· C语言 代码 · 共 2,119 行 · 第 1/4 页

C
2,119
字号
#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;}#if 1/*----------------sunl--------------------------*/int configurePortAuto(u_int8_t phyAddr){	u_int16_t data;	int ret = 0;	/*Init Pin*/	InitSerialInterface();			data= SMIRead(phyAddr, 0);	/*read data from 8305*/printf("before config, the data is %d\n", data);	SetBit(data, AUTO);			/*auto negotiation enable=1*/			/*write the data to 8305*/	if((ret = SMIWrite(phyAddr, 0, data)) == -1)	{		printf("Writing is error!!!\n");		return -1;	}	return 0;}int configureEthernetPort(u_int8_t phyAddr, u_int16_t speed, u_int16_t duplex){	u_int16_t data;	int ret = 0;		/*Init Pin*/	InitSerialInterface();	data= SMIRead(phyAddr, 0);	/*read data from 8305*/printf("before config, the data is %d\n", data);	ClrBit(data, AUTO);			/*auto negotiation disable=0*/			/*dulplex mode*/	if(duplex==1)	{		SetBit(data, DULPLEX);		/*full dulplex*/	}	else if(duplex==0)	{		ClrBit(data, DULPLEX);		/*half dulplex*/	}	/*speed select*/	if(speed==1)	{		SetBit(data, SPEED);		/*speed=100M*/	}	else if(speed==0)	{		ClrBit(data, SPEED);		/*speed=10M*/	}			/*write the data to 8305*/	if((ret = SMIWrite(phyAddr, 0, data)) == -1)	{		printf("Writing is error!!!\n");		return -1;	}	return 0;}/*Configure the ethernet ports on KT505*/DEFUN (config_lan_negotiation,       config_lan_negotiation_cmd,       "lan negotiation (10half|10full|100half|100full|auto)",       "Configure the ethernet ports\n"){	u_int16_t data_read;	int i, flag;	if( strcmp(argv[0], "100full") == 0 )	{		flag=1;		for(i=0; i<4; i++)		{			if( configureEthernetPort(i, 1, 1) == 0 )			{				strcpy(lanneg[i].mode, argv[0]);			}			else			{				flag=0;				break;			}		}		if(flag==1)		{			vty_out (vty, "negotiation is 100full%s", VTY_NEWLINE);		}		else		{			vty_out (vty, "negotiation is not set well%s", VTY_NEWLINE);		}	}	else if(strcmp(argv[0], "100half") == 0)	{		flag=1;		for(i=0; i<4; i++)		{			if( configureEthernetPort(i, 1, 0) == 0 )			{				strcpy(lanneg[i].mode, argv[0]);			}			else			{				flag=0;				break;			}		}		if(flag==1)		{			vty_out (vty, "negotiation is 100half%s", VTY_NEWLINE);		}		else		{			vty_out (vty, "negotiation is not set well%s", VTY_NEWLINE);		}		}	else if(strcmp(argv[0], "10full") == 0)	{		flag=1;		for(i=0; i<4; i++)		{			if( configureEthernetPort(i, 0, 1) == 0 )			{				strcpy(lanneg[i].mode, argv[0]);			}			else			{				flag=0;				break;			}		}		if(flag==1)		{			vty_out (vty, "negotiation is 10full%s", VTY_NEWLINE);		}		else		{			vty_out (vty, "negotiation is not set well%s", VTY_NEWLINE);		}		}	else if(strcmp(argv[0], "10half") == 0)	{		flag=1;		for(i=0; i<4; i++)		{			if( configureEthernetPort(i, 0, 0) == 0 )			{				strcpy(lanneg[i].mode, argv[0]);			}			else			{				flag=0;				break;			}		}		if(flag==1)		{			vty_out (vty, "negotiation is 10half%s", VTY_NEWLINE);		}		else		{			vty_out (vty, "negotiation is not set well%s", VTY_NEWLINE);		}		}	else if(strcmp(argv[0], "auto") == 0)	{		flag=1;		for(i=0; i<4; i++)		{			if( configurePortAuto(i) == 0 )			{				strcpy(lanneg[i].mode, argv[0]);			}			else			{				flag=0;				break;			}		}		if(flag==1)		{			vty_out (vty, "negotiation is auto%s", VTY_NEWLINE);		}		else		{			vty_out (vty, "negotiation is not set well%s", VTY_NEWLINE);		}	}	  return CMD_SUCCESS;}#endifDEFUN (bridge_addbr_delbr,       bridge_addbr_delbr_cmd,       "bridge (addbr|delbr) WORD",       "bridge (Spanning Tree protocol ) configuration\n"       "add a bridge\n"       "delete a bridge\n"       "bridge name\n"){	pid_t pid;	if((pid = vfork()) < 0)		printf("error in fork!");	else if (pid == 0)	{		execl("/bin/brctl", "brctl", argv[0], argv[1], 0);	}			return CMD_SUCCESS;}DEFUN (bridge_addif_delif,       bridge_addif_delif_cmd,       "bridge (addif|delif) WORD WORD",       "bridge (Spanning Tree protocol ) configuration\n"       "add interface to bridge\n"       "delete interface from bridge\n"       "bridge name\n"       "interface name\n"){	pid_t pid;	if((pid = vfork()) < 0)		printf("error in fork!");	else if (pid == 0)	{		execl("/bin/brctl", "brctl", argv[0], argv[1], argv[2], 0);	}		return CMD_SUCCESS;}void int505(){	strcpy(lanneg[0].port, "0");	strcpy(lanneg[1].port, "1");	strcpy(lanneg[2].port, "2");	strcpy(lanneg[3].port, "3");	strcpy(lanneg[0].mode, "auto");	strcpy(lanneg[1].mode, "auto");	strcpy(lanneg[2].mode, "auto");	strcpy(lanneg[3].mode, "auto");}/*-----------------------------------*/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); #if 1  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); #endif  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); #if 1  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); #endif  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);#if 1  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); #endif/*------------------sunl-start ----------------*///	br_init();    {    	int505();    	install_element(CONFIG_NODE, &config_lan_negotiation_cmd);	install_element(CONFIG_NODE, &bridge_addbr_delbr_cmd);	install_element(CONFIG_NODE, &bridge_addif_delif_cmd);    }/*------------------sunl-end------------------*/ }

⌨️ 快捷键说明

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