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

📄 dhcp.c

📁 dhcp snooping for vxworks
💻 C
📖 第 1 页 / 共 2 页
字号:
	*p = 0;
	p++;
	*p = 1;
	p++;
	*p = 2;
	p++;
	*p = 3;
	p++;
	*p = 4;
	p++;
	*p = 5;
	p++;
	*(short *)p = 4088;
	p = p + 2;
	*(unsigned int *)p = 3000;
	p = p + 4;
	*(unsigned int *)p = 1000;
	p = p + 4;
	*p = 127;
	p = p + 5;

	receive_dhcp_command_from_ec2(buf, 100);
}

 DEFUN (set_dhcp_func, set_dhcp_cmd,
	"set dhcp [snooping|option82] [enable|disable]",
	/*"set dhcp [snoop] [enable|disable]",*/
	"Configure system services.\n" "Configure dhcp services\n"
	"Configure dhcp snooping services.\n" "Configure dhcp option82 services.\n"
	"Set enable.\n" "Set disable.\n")
{
	char en = 0;
	char buf[10];
	char slot;
	char *p;

	if (strcmp (argv[1], "enable") == 0)
	{
		en = 1;
		dhcp_2_cpu();
	}
	else
	{
		en = 0;
	}

	if (strcmp (argv[0], "snooping") == 0)
	flag_dhcp_snooping = en;
	else
	flag_dhcp_option82 = en;

	if (en == 0 && 0 == (flag_dhcp_option82+flag_dhcp_snooping))
	{
		dhcp_del();
	}

	memset (buf, 0, sizeof(buf));

	for (slot = 1; slot <= MAX_DOWN_SLOT; slot++)
	send_dhcp_snoop_cfg_2slot(slot);
	
	return 0;
}


 DEFUN (set_dhcp_trust_func, set_dhcp_trust_cmd,
	"set dhcp snoop trusted_port <portlist>",
	"Configure system services.\n" "Configure dhcp services\n" "Configure dhcp snoop services\n"
	"Configure dhcp snooping trusted ports.\n" "Please input portlist.\n")
{
	char *portlist;
	int portno;
	char port[30];
	char slot;
	int ret = 0, i;

	portlist = argv[0];
	ret = get_uplinkport_by_list(portlist, port);
	if (ret<0)
	{
		vty_out(vty,"Invalid Port List <%s> \r\n",portlist);
		return -1;
	}
	
	memset (dhcp_trust_port,0, sizeof(dhcp_trust_port));
	for (i = MIN_UPLINK_PORT; i <= MAX_UPLINK_PORT; i++)
	{
		if (port[i])
		dhcp_trust_port[i] = 1;
	}
	return 0;
}

 DEFUN (set_dhcp_optionGsw_func, set_dhcp_optionGsw_cmd,
	"set dhcp option82  accessNodeIdentifier <identifier> ani_rack <0-15> ani_frame <0-31>",
	"Configure system services.\n" "Configure dhcp services\n" "Configure dhcp option services\n"
	"Set access identifier.\n" "Please input identifier.\n" "Set ANI rack.\n" "Please input rack.\n" 
	"Set ANI frame.\n" "Please input ANI frame.\n")
{
	if (argv[0])
	strncpy ((char *)option82_value_gsw.AccessNodeIdentifier, argv[0], 50);

	option82_value_gsw.ANI_frame = atoi(argv[2]);
	option82_value_gsw.ANI_rack = atoi (argv[1]);
	return 0;
 }

  DEFUN (show_dhcp_optionGsw_func, show_dhcp_optionGsw_cmd,
	"show dhcp option82 value",
	"Show system services.\n" "Show dhcp services\n"
	"Show dhcp option82 services.\n" "Show dhcp option82 values.\n" 
	)
{
	char ret = 0;
	char onu, slot;

	vty_out (vty, "AccessNodeIdentifier : %s\r\n", option82_value_gsw.AccessNodeIdentifier);
	vty_out (vty, "ANI rack             : %d\r\n", option82_value_gsw.ANI_rack);
	vty_out (vty, "ANI frame            : %d\r\n", option82_value_gsw.ANI_frame);
	return ret;
}

 DEFUN (show_dhcpbind_func, show_dhcpbind_cmd,
	"show dhcp snoop binding slot <slot> ",
	"Show system services.\n" "Show dhcp services\n"
	"Show dhcp snooping services.\n" "Show dhcp snooping binding table.\n" 
	"Show slot dhcp snooping binding table.\n" "slot number.\n")
{
	char ret = 0;
	int onu, slot;

	slot = atoslot (argv[0]);
	if (slot <0)
	{
		vty_out (vty, "Invalid slot input\r\n");
		return -1;
	}
	
	slot = translateslot_in (atoi(argv[0]));
	ret = show_dhcp_snooping_bind (slot, vty);
	return ret;
}

 DEFUN (show_dhcptrust_func, show_dhcptrust_cmd,
	"show dhcp snoop trusted_port ",
	"Show system services.\n" "Show dhcp services\n"
	"Show dhcp snooping services.\n" "Show dhcp snooping trusted ports.\n" 
	)
{
	int i;
	char uplink[10];
	
	vty_out (vty, "DHCP snooping trusted ports:\r\n");
	for (i = MIN_UPLINK_PORT; i<=MAX_UPLINK_PORT; i++)
	{
#ifdef _AN5116_06A_
		if (upLink_HS_get()&& (i == 17||( i >21 && i <=24)))
		continue;
#endif		
		if (dhcp_trust_port[i])
		{
			translate_uplinkport_out(i, uplink);
			vty_out (vty, "%s ", uplink);
		}
	}
	vty_out (vty, "\r\n");
	return 0;
}
 

DEFUN (show_dhcp_state_func, show_dhcp_state_cmd,
	"show dhcp state",
	"Show system services.\n" "Show dhcp services\n"
	"Show dhcp state.\n")
{
	if (flag_dhcp_option82)
	vty_out (vty, "DHCP option82  : enable\r\n");
	else
	vty_out (vty, "DHCP option82  : disable\r\n");

	if (flag_dhcp_snooping)
	vty_out (vty, "DHCP snooping  : enable\r\n");
	else
	vty_out (vty, "DHCP snooping  : disable\r\n");

	return 0;
}

DEFUN (set_dhcp_debug_func, set_dhcp_debug_cmd,
	"debug dhcp {[option82|snooping|off]}*1 {[discard] <0-1>}*1",
	"debug.\n" "debug.\n" "debug.\n" "debug.\n")
{
	if (argc == 0)
	{
		dhcp_snoop_debug =1;
		dhcp_option82_debug = 1;
	}
	else if (strcmp (argv[0], "option82") == 0)
	{
		dhcp_option82_debug = 1;
	}
	else if (strcmp (argv[0], "snooping") == 0)
	{
		dhcp_snoop_debug = 1;
	}
	else if (strcmp (argv[0], "discard") == 0)
	{
		discard_no_82 = atoi (argv[1]);
	}
	else
	{
		dhcp_snoop_debug =0;
		dhcp_option82_debug = 0;
	}

	return 0;
}

void showrun_dhcp(struct vty *vty)
{
	int i;
	int count= 0;
	char first = 0;
	char uplink[10];
	
	vty_out (vty, "!dhcp config---------------------------------\r\n");	

	/*state*/
	if (flag_dhcp_option82)
	vty_out (vty, "set dhcp option82 enable\r\n");
	if (flag_dhcp_snooping)
	vty_out (vty, "set dhcp snooping enable\r\n");

	/*trusted port*/
	for (i = 1; i<=7; i++)
	{
		if (dhcp_trust_port[i])
		count++;
	}
	/*all are trusnted ports is default set*/
	if (count != 7)
	{
		vty_out (vty, "set dhcp turst_port ");
		first = 1;
		for (i = 0; i<=7; i++)
		{
			if (dhcp_trust_port[i])
			{
				translate_uplinkport_out(17+i, uplink);
				if (first)
				{
					vty_out (vty, "%s", uplink);
					first = 0;
				}
				else
				vty_out (vty, ",%s", uplink);
			}
		}
		vty_out (vty, "\r\n");
	}

	/*dhcp option82 value*/
	if (strcmp (option82_value_gsw.AccessNodeIdentifier, DEFAULT_AcessNodeId) != 0 || option82_value_gsw.ANI_frame || option82_value_gsw.ANI_rack)
	{
		vty_out (vty, "set dhcp option82  accessNodeIdentifier %s ani_rack %d ani_frame %d\r\n", option82_value_gsw.AccessNodeIdentifier, option82_value_gsw.ANI_rack, option82_value_gsw.ANI_frame);
	}
	
	vty_out (vty, "!end dhcp config---------------------------------\r\n");	
}

/*slot is inter slot, 插上时plug=1,拔盘plug=0*/
void hotplug_dhcp (char slot, char plug)
{
	char buf[10];
	char *p;

	memset (buf, 0, sizeof (buf));
	/*plug on*/
	if (plug)
	{
		/*enable dhcp snoop*/
		if (flag_dhcp_snooping)
		{
			send_dhcp_snoop_cfg_2slot(slot);
		}
	}
	return;
}

void dhcp_cli_install(void)
{

	install_element (CONFIG_SERVICE_NODE, &set_dhcp_cmd);
	install_element (CONFIG_SERVICE_NODE, &set_dhcp_trust_cmd);
	install_element (CONFIG_SERVICE_NODE, &show_dhcpbind_cmd);
	install_element (CONFIG_SERVICE_NODE, &show_dhcp_state_cmd);
	install_element (CONFIG_SERVICE_NODE, &show_dhcptrust_cmd);
	install_element (CONFIG_SERVICE_NODE, &set_dhcp_optionGsw_cmd);
	install_element (CONFIG_SERVICE_NODE, &show_dhcp_optionGsw_cmd);
	install_element (DEBUG_HIDDEN_NODE, &set_dhcp_debug_cmd);
}


int cl_dhcp_module_init (void)
 {
	  struct cl_cmd_module *cl_mod_dhcp;

	  cl_mod_dhcp = (struct cl_cmd_module *) malloc (sizeof (struct cl_cmd_module));
	  if (!cl_mod_dhcp)
	  {
	   return 0;
	  }
	  memset (cl_mod_dhcp, 0, sizeof (struct cl_cmd_module));

	  cl_mod_dhcp->module_name = (char *) malloc (20);
	  if (cl_mod_dhcp->module_name == NULL) /* modified by aibin  2007/12/13 */
	  {
	   free (cl_mod_dhcp);
	   return 0;
	  }
	  memset (cl_mod_dhcp->module_name, 0, 20);
	  memcpy (cl_mod_dhcp->module_name, "dhcp", 13);

	  cl_mod_dhcp->init_func = dhcp_cli_install;

	  cl_mod_dhcp->showrun_func = showrun_dhcp;
	  /*
	     cl_mod_um->showrun_func = NULL;
	   */
	  cl_mod_dhcp->next = NULL;
	  cl_mod_dhcp->prev = NULL;
	  cl_install_module (cl_mod_dhcp);
	  return 1;
 }

int snmp_dhcp_snoop_state_set (unsigned char enable)
{
	int i;
	
	if(enable > 1)
	return -1;

	if (enable == flag_dhcp_snooping)
	return 0;

	flag_dhcp_snooping = enable;
	for (i = 1; i <= MAX_DOWN_SLOT; i++)
	send_dhcp_snoop_cfg_2slot(i); 

	if (enable == 0 && 0 == (flag_dhcp_option82+flag_dhcp_snooping))
	{
		dhcp_del();
	}

	if (enable == 1)
	{
		dhcp_2_cpu();
	}
	return 0; 
}

int snmp_dhcp_snoop_state_get (unsigned char *enable)
{
	*enable = flag_dhcp_snooping;
	return 0;
}

int snmp_dhcp_snoop_trust_port_set (char *buf, short len)
{
	char *p;
	char port;
	int count = 0;
	
	if (typepacket)
	typePkt (buf, len);

	memset (dhcp_trust_port, 0, sizeof(dhcp_trust_port));
	p = buf;
	count = *p;
	p++;
	while (count)
	{
#ifdef _ AN5116_06A_
		if (*p == 8)
		dhcp_trust_port[0] = 1;
		else		
		dhcp_trust_port[*p] = 1;
#else
		dhcp_trust_port[*p] = 1;
#endif
		p++;
		count--;
	}
	return 0;
}

void test_snmp_dhcp_snoop_trust_port_set(void)
{
	char buf[20];
	char *p;

	memset (buf, 0, sizeof (buf));
	p = buf;
	*p = 3;
	p++;
	*p = 2;
	p++;
	*p = 3;
	p++;
	*p = 4;
	p++;

	snmp_dhcp_snoop_trust_port_set (buf, p-buf);
}
	
int snmp_dhcp_snoop_trust_port_get (char *buf, short *len)
{
	char *p;
	int count = 0;
	int i;

	p = buf;
	p++;
	for (i = 0; i <= 10; i++)
	{
		if (dhcp_trust_port[i])
		{
#ifdef _AN5116_06A_
			if (i == 0)
			*p = 8;
			if (i == 8)
			continue;
#else
			if (i==0 || i >= 8)
			continue;
#endif
			else
			*p = i;
			count++;
			p++;
		}
	}
	*buf = count;
	*len = count+ 1;
}

void test_snmp_dhcp_snoop_trust_port_get (void)
{
	char buf[20];
	char *p;
	short len;

	memset (buf, 0, sizeof (buf));
	snmp_dhcp_snoop_trust_port_get (buf, &len);
	typePkt (buf, len);
}

int ec2_dhcp_binding_tbl_index_num_get(void)
{
	return 1;              /*FTTH不会分包*/
}

/*slot is out slot*/
int ec2_dhcp_binding_tbl_get(char slot, char index, char *buf, unsigned short *len)
{
	int count, i;
	char *p;
	char out_slot;
	char in_slot;
	short item;

	*len = 0;
	out_slot = slot;
	in_slot = translateslot_in(slot);
/*	if (SlotType(slot) == -1)
	{
		vty_out(vty, "slot %d is unconnected\r\n", slot);
		return -1;
	}
*/
	dhcp_command_respons = 0;
	p_dhcp_bind = (char *)malloc (MAX_DHCP_SLOT_BIND_BUF);
	if (p_dhcp_bind == NULL)
	{
		PRINTFSNOOP("\r\nmalloc memory failed");
		return -1;
	}
	memset (p_dhcp_bind, 0, sizeof (MAX_DHCP_SLOT_BIND_BUF));

	tell_slot_dhcp_bind(in_slot);
	
	count = dhcp_wait_time* (sysClkRateGet () / 10);
	for (i = 0; i < count; i++)
	{
		taskDelay (10);
		/*收到响应*/
		if (dhcp_command_respons == 1)
		{
			break;	
		}
	}

	/*only for test*/
	/*test_response (slot);*/

	if (dhcp_command_respons)
	{
		if (typepacket)
		typePkt (p_dhcp_bind, 100);
		
		p = p_dhcp_bind;
		
		if (*p != out_slot)
		{
			printf("response table not query slot %d\r\n", out_slot);
		}
		p++;
		count = *(short *)p;
		*len = 27*count +3;
		memcpy (buf, p_dhcp_bind, *len);
	}
	else
	{
		return -1;
	}
	free (p_dhcp_bind);
	p_dhcp_bind = NULL;
	dhcp_command_respons = 0;
	return 0;
}

void test_snmp_dhcp_snoop_bind_get (char slot)
{
	char buf[100];
	char *p;
	short len;

	memset (buf, 0, sizeof (buf));
	ec2_dhcp_binding_tbl_get (slot,1, buf, &len);
	typePkt (buf, len);


}

int dhcp_option82_value_set (char *value, short len, char *err, unsigned short *errlen)
{
	char *p;
	char stat = 0;

	if (typepacket)
	typePkt (value, len);

	p = value;
	stat = *p;
	p++;
	strncpy (option82_value_gsw.AccessNodeIdentifier, p, 50);
	p = p + 50;
	option82_value_gsw.ANI_rack = *p;
	p++;
	option82_value_gsw.ANI_frame = *p;
	dhcp_option82_state_set (stat);

	p = err;
	*(int *)p = 1;
	p = p+4;
	*(short *)p = 0;
	*errlen = 6;
	return 0;
}

void test_dhcp_option82_value_set (char state)
{
	char buf[100], err[10];
	short len, errlen;
	char *p;

	p = buf;
	*p = state;
	p++;
	strncpy (p,option82_value_gsw.AccessNodeIdentifier, 50);
	p = p + 50;
	*p = option82_value_gsw.ANI_rack;
	p++;
	*p = option82_value_gsw.ANI_frame;
	p++;

	dhcp_option82_value_set(buf, p-buf, err, &errlen);
}

int dhcp_option82_value_get (char *value, unsigned short *len)
{
	char *p;

	p = value;
	memset (p, 0,4);
	*(int *)p = 1;
	p = p+4;
	*p = flag_dhcp_option82;
	p++;
	strncpy (p, option82_value_gsw.AccessNodeIdentifier, 50);
	p = p + 50;
	*p = option82_value_gsw.ANI_rack;
	p++;
	*p = option82_value_gsw.ANI_frame;
	p++;

	*len = p-value;
	if (typepacket)
	typePkt (value, *len);

	
	return 0;
}

int dhcp_option82_state_set (char state)
{
	int en = 0;
	char slot;
	
	en = state;
	
	flag_dhcp_option82 = en;

	if (en == 0 && 0 == (flag_dhcp_option82+flag_dhcp_snooping))
	{
		dhcp_del();
	}

	for (slot = 1; slot <= 16; slot++);

	return 0;
}

int dhcp_option82_state_get (char *state)
{
	*state = flag_dhcp_option82;
	return 0;
}



/*int remote_onu_devicetype_change(){}*/

#endif /*end of define _DHCP_*/

⌨️ 快捷键说明

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