📄 main.c
字号:
}
//=================以下处理telnet
if( string_compare("telnet",6)!=0 )
{
#ifdef debug
send_string("\r\nCommand: telnet");
#endif
ip=0;
for(i=6,j=0;i<command_length;i++)
{//取得ip地址
if(command_buffer[i]!='.')
{ command_buffer[i]=command_buffer[i]&0x0f;
ip=ip*10;
ip=ip+command_buffer[i];
}
else{
if(j<4)ping_ip_address.bytes[j]=ip;
if(j==4)port.bytes.high=ip;
j++;
// if(j==4)break;//确保不存储ping_ip_address.bytes[4];
ip=0;
}
}
port.bytes.low=ip;
if(j==5){
// 表示命令正确
send_string("\r\ntelnet IP=") ;
send_word(ping_ip_address.words[0]);
send_word(ping_ip_address.words[1]);
send_string(" Port=");
send_word(port.word);
tcp_count=20;//20second
if((ping_ip_address.dwords&mask_ip_address.dwords)==(my_ip_address.dwords&mask_ip_address.dwords))
{
//表示位于同一子网.
arp_request(ping_ip_address.dwords);
}
else{//表示属于不同的子网,需要通过网关.
send_string("\r\nIn other Subnet");
ping_ip_address_ttl=10;
for(j=0;j<6;j++)
{
ping_ethernet_address.bytes[j]=gateway_ethernet_address.bytes[j];
}
if(gateway_ip_address_ttl==0)
{
send_string("\r\nGateWay Not Found!\r\nC:>");
tcp_count=0;
}
}
}
else{
send_string("\r\ntelnet Command Error\r\nC:>");
}
}
//============================
if(string_compare("showall",7)!=0 )
{ send_string("\r\nMAC address=");
for (i=0;i<3;i=i++)
{
send_word(my_ethernet_address.words[i]);
}
send_string("\r\nIP ADDRESS=");
send_word(my_ip_address.words[0]);
send_word(my_ip_address.words[1]);
send_string("\r\nGateWay IP ADDRESS=");
send_long(gateway_ip_address.dwords);
send_string("\r\nNet_mask=");
send_long(mask_ip_address.dwords);
send_string("\r\nTcp port=");
send_word(tcp_port.word);
send_string("\r\nUdp port=");
send_word(udp_port.word);
} //============================
if(string_compare("ip",2)!=0 )
//========================以下为初始化
{
k=28;
for(i=2;i<command_length;i++)
{
if(command_buffer[i]!='.')
{ command_buffer[i]=command_buffer[i]&0x0f;
ip=ip*10;
ip=ip+command_buffer[i];
}
else{ vue=ip*256+ip;
write_93c46(k,vue);
delay_ms(5);
k++;
ip=0;
}
}
send_string("\r\nok");
//read_port();
read_myip();
read_gateip();
read_maskip();
serial_init();
}
else{
send_string("\r\nerror");
}
}
if(string_compare("port",4)!=0 )
{
k=42;
for(i=4,j=0;i<command_length;i++)
{
if(command_buffer[i]!='.')
{ command_buffer[i]=command_buffer[i]&0x0f;
ip1=ip1*10;
ip1=ip1+command_buffer[i];
}
else{ //vue=ip*256+ip;
write_93c46(k,ip1);
delay_ms(10);
k++;
ip1=0;
}
}
read_port();
send_string("\r\nok");}
else{
send_string("\r\nerror");
}
}
if(string_compare("mac",3)!=0 )
{
k=2;
for(i=3,j=0;i<command_length;i++)
{
if(command_buffer[i]!='.')
{ command_buffer[i]=command_buffer[i]&0x0f;
ip1=ip1*10;
ip1=ip1+command_buffer[i];
}
else{ //vue=ip*256+ip;
write_93c46(k,ip1);
delay_ms(10);
k++;
ip1=0;
}
}
rtl8019as_init();
send_string("\r\nok");}
else{
send_string("\r\nerror");
}
}
command_length=0;
}
}
}
//14//
void create_ip_packet(unsigned int length,unsigned int ip0,unsigned int ip1,unsigned char protocal)
//tcp包的长度,不包括ip头,ip地址高16位,ip地址低16位,ip协议段
{
//产生一个ip头
txdnet.ipframe.verandihl = 0x45;//版本和头长度
txdnet.ipframe.ttl = 128;//ttl
txdnet.ipframe.typeofserver = 0x00;//服务类型
txdnet.ipframe.crc = 0;
txdnet.ipframe.frameindex = frameindex++;
txdnet.ipframe.segment = 0x4000;//没有分段
length = length + 20;
txdnet.ipframe.totallength = length;
txdnet.ipframe.protocal = protocal;//下层协议
txdnet.ipframe.destip[0] = ip0;//对方ip
txdnet.ipframe.destip[1] = ip1;
txdnet.ipframe.sourceip[0] = my_ip_address.words[0];//本机ip
txdnet.ipframe.sourceip[1] = my_ip_address.words[1];
txdnet.ipframe.crc = createipheadcrc();
length = length + 14;//6+6+2
txdnet.etherframe.length = length;//是不是应该再加上4啊,crc校验字??
send_packet(length);
if(protocal == 6)
if(txdnet.tcpframe.control & (tcp_syn| tcp_fin| tcp_psh| tcp_urg))
{
copy_to_retransmit_buffer();
}
}
//17//
void ping_answer()
{
unsigned char i;
if(rxdnet.icmpframe.type == 0x08)
{//表示是ping请求
for (i = 16; i < rxdnet.etherframe.length; i++)
{
txdnet.bytes.bytebuf[i] = rxdnet.bytes.bytebuf[i];//将数据复制到发送缓冲区
}
txdnet.etherframe.destnodeid[0] = rxdnet.etherframe.sourcenodeid[0];
txdnet.etherframe.destnodeid[1] = rxdnet.etherframe.sourcenodeid[1];
txdnet.etherframe.destnodeid[2] = rxdnet.etherframe.sourcenodeid[2];
txdnet.ipframe.ttl = txdnet.ipframe.ttl-1;
txdnet.ipframe.crc = 0;
txdnet.ipframe.destip[0] = rxdnet.ipframe.sourceip[0];
txdnet.ipframe.destip[1] = rxdnet.ipframe.sourceip[1];
txdnet.ipframe.sourceip[0] = my_ip_address.words[0];
txdnet.ipframe.sourceip[1] = my_ip_address.words[1];
txdnet.ipframe.crc = createipheadcrc();
txdnet.icmpframe.type = 0x00;// is icmp answer;
txdnet.icmpframe.crc = 0;
for(i = 21; i < 41; i++)
{
txdnet.words.wordbuf[i] = rxdnet.words.wordbuf[i];
}
txdnet.icmpframe.crc = createicmpcrc();
send_packet(rxdnet.etherframe.length);
}
}
//18//
void ping_echo()
{
send_string(" Reply From IP=");
send_word(rxdnet.ipframe.sourceip[0]);
send_word(rxdnet.ipframe.sourceip[1]);
send_string(" TTL=");
send_hex(rxdnet.ipframe.ttl);
}
//23//
void retransmit_packet(unsigned char i)
{
unsigned int ii;
// unsigned char xdata *txd=&txdnet;
// unsigned char xdata *rt;
unsigned char *txd = &txdnet;
unsigned char *rt;
rt = &retransmit_buffer[i].bytes.bytebuf;
for(ii = 0; ii < retransmit_buffer[i].rtframe.length+4; ii++)
{
(*txd) = (*rt);
rt++;
txd++;
}
send_packet(retransmit_buffer[i].rtframe.length);
retransmit_buffer[i].rtframe.timeout = rttime;
}
//24//
void do_retransmit_buffer()
{
unsigned char i;
for(i = 0; i < rt_size; i++)
{
if(retransmit_buffer[i].rtframe.timeout > 0)
retransmit_buffer[i].rtframe.timeout--;
}
for(i = 0; i < rt_size; i++)//rt_size为重发缓冲区的个数
{
if(retransmit_buffer[i].etherframe.status!=0)//表示该重发缓冲区有数据
{
if(retransmit_buffer[i].tcpframe.seqnumber < tcp1024.snd_una)
{
retransmit_buffer[i].rtframe.status = 0;//该缓冲区无效
}
else
if(retransmit_buffer[i].rtframe.timeout == 0)
{//重发
retransmit_buffer[i].rtframe.status++;
if(retransmit_buffer[i].rtframe.status == tcp_max_rt)
delete_socket();
retransmit_packet(i);
}
}
}
}
//28//
unsigned char free_rt()
{
//如果retransmit_buffer还有空的,返回1,否则返回0
unsigned char i;
for(i = 0; i < rt_size; i++)
{
if(retransmit_buffer[i].rtframe.status == 0)
{
return(1);
}
}
return(0);
}
//29//
unsigned char get_char()
{
unsigned char temp;
temp = comrxdbuf[comrxdread];
comrxdread++;
if(comrxdread == com_rxd_buffer_size)
comrxdread = 0;
// if(temp == 'c')
// drop_count++;
// else
// drop_count = 0;
return(temp);
}
/*/30//
unsigned int copy_data()
{
//返回要发送的数据的长度
unsigned int ii;
if(free_rt() == 0)
return(0);
for(ii = 0; ii < 1024; ii++)
{
//每次最多发送1024字节的数据
if(comrxdread != comrxdwrite)
{
txdnet.tcpframe.tcpdata[ii] = get_char();
}
else
{
break;
}
}
return(ii);
}
*/
//37//
unsigned char verifyudpcrc()//对udp头进行校验,错误返回0,正确返回1
{
unsigned int crc;
rxdnet.ipframe.ttl = 0;
rxdnet.ipframe.crc = rxdnet.ipframe.totallength - (rxdnet.ipframe.verandihl & 0x0f) * 4;
crc = checksum(&rxdnet.ippacket.ippacket[4], rxdnet.ipframe.crc + 12);
if(crc==0)
{
return (1);
}
return(0);
}
//38//
void process_udp()
{
unsigned int ii;
//对udp协议处理
if(verifyudpcrc())
if(rxdnet.udpframe.destport == 1025)//仅接收1025端口的数据包
{
send_string("\r\nUDP: source_ip=");
send_word(rxdnet.ipframe.sourceip[0]);
send_word(rxdnet.ipframe.sourceip[1]);
send_string(" dest_ip=");
send_word(rxdnet.ipframe.destip[0]);
send_word(rxdnet.ipframe.destip[1]);
send_string("\r\nUDP: dest_Port=");
send_word(rxdnet.udpframe.destport);
send_string(" source_Port=");
send_word(rxdnet.udpframe.sourceport);
send_string(" length=");
send_word(rxdnet.udpframe.length);
send_string("\r\nUDP: Data=");
for(ii = 0; ii < rxdnet.udpframe.length -8 ; ii++)//8 bytes udp head
{
send_char(rxdnet.udpframe.udpdata[ii]);//udp数据包的数据
}
}
}
//39//
void udp_request(unsigned long ip_address, unsigned int portnumber)
{
//发送udp数据包
unsigned char i;
unsigned int ii;
unsigned int datalength;
for(i = 0; i < 3; i++)
{
txdnet.etherframe.destnodeid[i] = ping_ethernet_address.words[i];//目的网卡地址
txdnet.etherframe.sourcenodeid[i] =my_ethernet_address.words[i];//me
}
txdnet.etherframe.protocal = 0x0800;//协议为ip协议
txdnet.udpframe.sourceport = 1025;//默认的本机端口为1025,可以修改
txdnet.udpframe.destport = portnumber;
//////////////////////////////////////////////////////////////////////
/* datalength = command_buffer[1] * 256 + command_buffer[2];
for(ii = 0,i = command_buffer[0]; ii < datalength; ii++)
{
txdnet.udpframe.udpdata[ii] = command_buffer[ii+i];
}
*/
///////////////////////////////////////////////////////////////////////
datalength =serrxdwrite;
for(i=0,ii=0;i<datalength;i++)
{txdnet.udpframe.udpdata[i]=serrxdbuf[ii];
ii++;
}
datalength = datalength+8;//八字节UDP头
txdnet.udpframe.length = datalength;
txdnet.udpframe.crc = 0;
txdnet.udpframe.crc = createtcpcrc();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -