www.pudn.com > TcpIpOn8051.rar > command.C


#include  
#include "public.h" 
#include "ip.h" 
#include "tcp.h" 
#include  
 
extern TCP_SOCK xdata tsock[nsocks]; 
extern char clientcmd[30]; 
 
ip strtoip(char *arg); 
BYTE hextostr(BYTE h,char *str); 
void iptostr(ip sip,char *str); 
WORD strtoword(char *str); 
void wordtostr(WORD port,char *str); 
void strtomac(BYTE *str,WORD mac[3]); 
//BYTE strcmp(char *s1,char *s2); 
 
extern void ping(DWORD destip); 
extern void set_ip(ip i); 
extern void set_mask(ip i); 
extern void set_gate(ip i); 
extern void set_port(WORD port); 
extern void set_agent(ip i); 
extern void set_agentport(WORD port); 
extern void print(char str[]); 
extern BYTE tcp_open(TCP_SOCK *ts,DWORD destip, WORD destport); 
extern BYTE tcp_close(TCP_SOCK *ts); 
 
void command_deal(char *str) 
{ 
	register BYTE i,j; 
	char command[10]; 
	char arg[20]; 
	DWORD temp; 
	WORD tport; 
	char xdata sip[16]; 
	TCP_SOCK *ts; 
 
	i=0; 
	j=0; 
	while((str[i]>='a' && str[i]<='z')||(str[i]>='A' && str[i]<='Z')) 
 		{ 
 			command[i]=str[i]; 
 			i++; 
 		} 
	command[i]='\0'; 
	//取出命令字段。 
 
	if(str[i]!='\0') 
 	{ 
 		while(str[i]==' ') i++; 
 		while(str[i]!='\0') 
  	{ 
  		arg[j++]=str[i++]; 
  	} 
 	arg[j]='\0'; 
 	} 
//以上已经分清了命令和参数两个部分下面对命令处理 
 
	if(!strcmp(command,(char *)"ping")) 
		if((temp=strtoip(arg))!=0xffffffff) 
		{ 
 			ping(temp); 
			iptostr(temp,&sip); 
			print(&sip); 
		} 
	if(!strcmp(command,(char *)"setip")) 
		if((temp=strtoip(arg))!=0xffffffff) 
			set_ip(temp); 
	if(!strcmp(command,(char *)"setgate")) 
		if((temp=strtoip(arg))!=0xffffffff) 
			set_gate(temp); 
	if(!strcmp(command,(char *)"setmask")) 
		if((temp=strtoip(arg))!=0xffffffff) 
			set_mask(temp); 
	if(!strcmp(command,(char *)"setagent")) 
		if((temp=strtoip(arg))!=0xffffffff) 
			set_agent(temp); 
	if(!strcmp(command,(char *)"setport")) 
		if((tport=strtoword(arg))!=0) 
		{ 
			set_port(tport); 
			wordtostr(tport,&sip); 
			print(&sip); 
		} 
	if(!strcmp(command,(char *)"setaport")) 
		if((tport=strtoword(arg))!=0) 
			set_agentport(tport); 
	if(!strcmp(command,(char *)"opentcp")) 
		if((temp=strtoip(arg))!=0xffffffff) 
		{ 
			if(tsock[0].state==0) 
			{ 
				ts=&tsock[0]; 
				tcp_open(ts,temp,1025); 
				strcpy(clientcmd,str); 
			} 
			else if(tsock[1].state==0) 
			{ 
				ts=&tsock[1]; 
				tcp_open(ts,temp,1025); 
			} 
			else print("busy!please try later."); 
		} 
	if(!strcmp(command,(char *)"closetcp")) 
		if((temp=strtoip(arg))!=0xffffffff) 
			if(tsock[0].remip==temp) 
				tcp_close(&tsock[0]); 
			else if(tsock[1].remip==temp) 
				tcp_close(&tsock[1]); 
			else print("no connect"); 
 
} 
 
//ip为DWORD形式 
ip strtoip(char *str) 
{ 
	ip xdata ipaddr=0; 
	BYTE temp=0; 
	BYTE count=0; 
	bit flag=1; 
	register BYTE i; 
 
	i=0; 
	while(flag) 
 	{ 
 		count++; 
 		while(str[i]<='9' && str[i]>='0') 
  			temp=temp*10+str[i++]-'0'; 
 		if(str[i]=='.')i++; 
 		else flag=0; 
 		if(temp>255)temp=255; 
 		ipaddr=ipaddr<<8; 
		ipaddr=ipaddr+(DWORD)temp; 
 		temp=0; 
 	}  
	return (count==4)?ipaddr:0xffffffff;  
} 
 
void iptostr(ip sip,char *str) 
{ 
	BYTE temp0,temp1,temp2,temp3; 
	DWORD temp; 
	register BYTE i; 
 
	temp=sip; 
	temp=temp>>24; 
	temp0=(BYTE)temp; 
	temp=sip; 
	temp=temp>>16; 
	temp1=(BYTE)temp; 
	temp=sip; 
	temp=temp>>8; 
	temp2=(BYTE)temp; 
	temp=sip; 
	temp3=(BYTE)temp; 
	i=hextostr(temp0,str); 
	str[i++]='.'; 
	i=i+hextostr(temp1,&str[i]); 
	str[i++]='.'; 
	i=i+hextostr(temp2,&str[i]); 
	str[i++]='.'; 
	i=i+hextostr(temp3,&str[i]); 
	str[i++]='\0'; 
} 
 
//返回字符串后一个位置 
BYTE hextostr(BYTE h,char *str) 
{ 
	BYTE temp0,temp1; 
	register BYTE i=0; 
 
	temp0=h/100; 
	h=h%100; 
	temp1=h/10; 
	h=h%10; 
	if(temp0==0) 
  		if(temp1==0)  
			str[i++]=h+0x30; 
  		else  
		{	str[i++]=temp1+0x30; 
			str[i++]=h+0x30; 
		} 
	else  
	{ 
		str[i++]=temp0+0x30; 
		str[i++]=temp1+0x30; 
		str[i++]=h+0x30; 
	} 
	return i; 
} 
/*------------------------------------------------ 
MAC地址转换为数组 
-----------------------------------------------------*/ 
void strtomac(BYTE *mac_str,WORD *result) 
//MAC地址转换为数组 
{ 
	int i, j, k; 
	int tmp, turn, one_digit; 
 
	for (i = 2; i >= 0; i--) 
	{ 
		tmp = 0; 
		turn = 0; 
		for (j = (i + 1) * 4 - 1; j >= i * 4; j--) 
		{ 
			if (isdigit(mac_str[j])) 
				one_digit = mac_str[j] - '0'; 
			else if (islower(mac_str[j])) 
				one_digit = mac_str[j] - 'a' + 10; 
			else 
				one_digit = mac_str[j] - 'A' + 10; 
			for (k = 0; k < turn; k++) 
				one_digit = one_digit * 16; 
			tmp += one_digit; 
			turn++; 
		} 
		result[i] = tmp; 
	} 
} 
 
WORD check_str(BYTE *mac_str) 
//检测输入的MAC地址是否符合要求:MAC_LENGTH位16进制字母 
//符合要求输出1,否则输出0 
{ 
	int result = 1; 
	int i = 0; 
 
	if ((int)(strlen(mac_str)) != 12) 
		return 0; 
 
	while (i < 12 && result == 1) 
	{ 
		if (!isxdigit(mac_str[i++])) 
			result = 0; 
	} 
 
	return result; 
} 
 
int check_ip(BYTE *ip_str) 
//检查IP地址是否符合要求(四段点分十进制数,各段数值在0~255之间) 
//符合要求返回1,负责返回-1 
{ 
	int result = 1; 
	int str_len = 0; 
	int i; 
	int ip_value = 0;		//记录一段地址ip地址值 
	int dot_cnt = 0;		//记录'.'的个数 
	int num_cnt = 0;		//记录地址段的个数 
	int has_num = 0;		//已经输入数字该值为1,否则为0 
	char ch; 
	char state = 'n';		//读数阶段state为'n', 读符号阶段state为'.' 
	 
	str_len = strlen(ip_str); 
	if (str_len == 0 || str_len > 15) 
		return 0; 
 
	i = 0; 
	while (i < str_len && result == 1 && dot_cnt < 4 && num_cnt < 4) 
	{ 
		ch = ip_str[i++]; 
		if (isdigit(ch)) 
		{ 
			has_num = 1; 
			ip_value = ip_value * 10 + ch - '0'; 
		} 
		else if (ch == '.') 
		{ 
			if (has_num == 0) 
				result = 0; 
			else if (ip_value >= 0 && ip_value <= 255) 
			{ 
				num_cnt++; 
				dot_cnt++; 
				ip_value = 0; 
			} 
			else 
				result = 0; 
		} 
		else 
			result = 0; 
	} 
 
	if (has_num == 1) 
		num_cnt++; 
 
	if (ip_value < 0 || ip_value > 255) 
		return 0; 
 
	if (result == 1 && dot_cnt == 3 && num_cnt == 4) 
		return 1; 
	else 
		return 0; 
} 
 
WORD strtoword(char *str) 
{ 
 
	register BYTE i;  
	WORD temp; 
	temp=0; 
	i=0; 
	while(str[i]<='9' && str[i]>='0') 
  		temp=temp*10+str[i++]-'0'; 
	return temp; 
} 
 
void wordtostr(WORD port,char *str) 
{ 
	char str1[5]; 
	register BYTE i,j; 
 
	for(i=0;i<5;i++) 
 	{ 
 		str1[i]=port%10+0x30; 
 		port=port/10; 
 	} 
 
	i=4; 
	while(str1[i]=='0') 
		i--; 
	for(j=0;j<=i;j++) 
 		str[j]=str1[i-j]; 
	str[i+1]='\0'; 
} 
/*---------------------------------------- 
=:0 
<>:1 
----------------------------------------*/ 
/* 
BYTE strcmp(char *s1,char *s2) 
{ 
	register BYTE i=0; 
	while(s1[i]!='\0' && s2[i]!='\0' && s1[i]==s2[i]) 
		i++; 
	return (s1[i]=='\0' && s2[i]=='\0' )?0:1; 
} 
*/