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

📄 protocol.c

📁 利用51单片机对RTL8109进行控制
💻 C
📖 第 1 页 / 共 3 页
字号:
#ifndef _protocol_c
#define _protocol_c
#endif

#include "net.h"
#include "struct.h"
#include "fundef.h"
#include "variable.h"

unsigned int createtcpcrc(void);
void create_ip_packet(union ip_address_type ip_add,uint length,uchar protocal);
void create_tcp_packet(unsigned char length);
void telnet_request(ulong ip_address,uint portnumber);
void copy_to_retransmit_buffer(void);
void protect_multi(void);
unsigned char check_multi(void);
void create_udp_packet(unsigned char length);

unsigned int checksum(unsigned int xdata *check,unsigned int length)
{
//计算校验和
	unsigned long sum=0;
	unsigned int i;
	for (i=0;i<(length)/2;i++)
	{
		sum+=*check++;
	}
	if(length&0x01)//表示长度为单数
	{
		sum=sum+((*check)&0xff00);
	}
	sum=(sum&0xffff)+((sum>>16)&0xffff);//高16位和低16位相加
	if(sum&0xffff0000)
	{//表示有进位
		sum++;
	}
	return ( (unsigned int)(~((sum)&0xffff))  );
}



void arp_answer()
{
	unsigned char i;

   	if (rxdnet.arpframe.destip[0]==my_ip_address.words[0])
   	if (rxdnet.arpframe.destip[1]==my_ip_address.words[1])
   	{
	   	//表示是向我这个ip地址的请求

   		for(i=16;i<64;i++)
   		{
	   		//复制arp到发送缓冲区
   			txdnet.bytes.bytebuf[i]=rxdnet.bytes.bytebuf[i];

   		}

   		for(i=0;i<3;i++)
   		{
	   		//复制对方网卡地址或网关地址
    			txdnet.etherframe.destnodeid[i]=rxdnet.etherframe.sourcenodeid[i];
    		//	ping_ethernet_address.words[i]=rxdnet.etherframe.sourcenodeid[i];

        		txdnet.arpframe.sourcenodeid[i]=my_ethernet_address.words[i];//txdnet.etherframe.sourcenodeid[i];
        		txdnet.arpframe.destnodeid[i]=rxdnet.arpframe.sourcenodeid[i];
    		}
        	for(i=0;i<2;i++)
        	{
	    		txdnet.arpframe.destip[i]=rxdnet.arpframe.sourceip[i];
	    	//	ping_ip_address.words[i]=rxdnet.arpframe.sourceip[i];
	    		txdnet.arpframe.sourceip[i]=rxdnet.arpframe.destip[i];
        	}

        	txdnet.arpframe.operation=0x0002;//应答代码
        	txdnet.etherframe.protocal=0x0806;//arp protocal

        	send_packet(60);
   	}
}


void arp_request(unsigned long ip_address)
{
   	unsigned char i;

    	txdnet.etherframe.protocal=0x0806;//arp protocal
   	for(i=0;i<3;i++)
   	{
   		//复制对方网卡地址或网关地址
    		txdnet.etherframe.destnodeid[i]=0xffff;
        	txdnet.arpframe.sourcenodeid[i]=txdnet.etherframe.sourcenodeid[i];
        	txdnet.arpframe.destnodeid[i]=0x0000;
    	}
        for(i=0;i<2;i++)
        {
        	txdnet.arpframe.sourceip[i]=my_ip_address.words[i];
        }
    	txdnet.arpframe.destip[0]=ip_address>>16;
    	txdnet.arpframe.destip[1]=ip_address&0xffff;
        txdnet.arpframe.harewaretype=0x0001;
        txdnet.arpframe.protocaltype=0x0800;
        txdnet.arpframe.halength=0x06;
        txdnet.arpframe.palength=0x04;
        txdnet.arpframe.operation=0x0001;//arp request
        for(i=0x2e;i<(0x2e+18);i++)
        {
    		txdnet.bytes.bytebuf[i]=0x00;
        }

        send_packet(60);
}

void gateway_arp_request()
{
	if((sec<3) && (gateway_ip_address_ttl<5))
	{
		arp_request(gateway_ip_address.dwords);
	}
}

void arp_process()//收到arp返回包
{
	uchar i;

	if(rxdnet.arpframe.sourceip[0]==gateway_ip_address.words[0])
	if(rxdnet.arpframe.sourceip[1]==gateway_ip_address.words[1])
 	{
 		//表示是网关的ip地址的回答.
   		for (i=0;i<3;i++)
   		{
   			gateway_ethernet_address.words[i]=rxdnet.arpframe.sourcenodeid[i];
   		}

   		gateway_ip_address_ttl=10;
   		return;

	}

	if(rxdnet.arpframe.sourceip[0]==dvr_ip_address.words[0])
	if(rxdnet.arpframe.sourceip[1]==dvr_ip_address.words[1])
	{
		if(c_type!=2)
		{
   			for (i=0;i<3;i++)
   			{
   				dvr_ethernet_address.words[i]=rxdnet.arpframe.sourcenodeid[i];
   			//send_char(rxdnet.arpframe.sourcenodeid[i]);

   			}
   		}
   		ping_ip_address_ttl=10;
   		return;
	}
	
	if(rxdnet.arpframe.sourceip[0]==ping_ip_address.words[0])
	if(rxdnet.arpframe.sourceip[1]==ping_ip_address.words[1])
	{
		if(c_type!=2)
		{
   			for (i=0;i<3;i++)
   			{
   				ping_ethernet_address.words[i]=rxdnet.arpframe.sourcenodeid[i];
   			//send_char(rxdnet.arpframe.sourcenodeid[i]);

   			}
   		}
   		ping_ip_address_ttl=10;
	}
}


unsigned int  createipheadcrc()
{
	unsigned char i;
 	crctemp.dwords=0;
 	for(i=9;i<19;i++)
	{
 		crctemp.dwords=crctemp.dwords+txdnet.words.wordbuf[i];
	}
	while(crctemp.words.high>0)
	{
                crctemp.dwords=(ulong)(crctemp.words.high+crctemp.words.low);
        }
	crctemp.words.low=0xffff-crctemp.words.low;
	return(crctemp.words.low);
}

/*
void FunIntToASCII(unsigned char temp)
{
	ASCII[0]=temp/100+0x30;
	temp=temp %100;

	ASCII[1]=temp/10+0x30;
	temp=temp %10;

	ASCII[2]=temp+0x30;
}

void FunSendRemoteAddr(void)
{
	FunIntToASCII(rxdnet.ipframe.sourceip[1]&0xff);
	send_char(ASCII[0]);
	send_char(ASCII[1]);
	send_char(ASCII[2]);
        send_char(0x1f);
        send_char('a');
}
*/
void tcp_syn_sent()
{
	unsigned char i;
     //   tcp1024.irs=rxdnet.tcpframe.seqnumber;//对方的初始化顺序号
        tcp1024.rcv_nxt=rxdnet.tcpframe.seqnumber+1;//对方的顺序号,用于确认

        tcp1024.snd_nxt=rxdnet.tcpframe.acknumber;

     //   tcp1024.dest_max_segment_size=560;//默认为560

        tcp1024.state=tcp_state_established;

        txdnet.tcpframe.sourceport=tcp1024.my_port;
        txdnet.tcpframe.destport=tcp1024.dest_port;

        txdnet.tcpframe.seqnumber=tcp1024.snd_nxt;
        txdnet.tcpframe.acknumber=tcp1024.rcv_nxt;
        txdnet.tcpframe.offset=0x50;
        txdnet.tcpframe.control=tcp_ack; //syn+ack
        txdnet.tcpframe.window=tcp1024.snd_wnd;
        txdnet.tcpframe.urg=0;
        txdnet.tcpframe.crc=0;
        txdnet.ipframe.destip[0]=tcp1024.dest_ip[0];
        txdnet.ipframe.destip[1]=tcp1024.dest_ip[1];
        txdnet.ipframe.sourceip[0]=my_ip_address.words[0];
        txdnet.ipframe.sourceip[1]=my_ip_address.words[1];
        txdnet.ipframe.ttl=0;
        txdnet.ipframe.protocal=6;//tcp
        txdnet.ipframe.crc=20;
        txdnet.tcpframe.crc=createtcpcrc();
        b_dvrcommand=1;
        
        for(i=0;i<3;i++)
        {
        	txdnet.etherframe.destnodeid[i]=tcp1024.dest_node_id[i];//目的网卡地址
        }
        
        create_ip_packet(dvr_ip_address,20,6);
	tcpconnect=1;
}

void send_tcpdata(unsigned char data_length)
{
	unsigned char i;

        txdnet.tcpframe.sourceport=tcp1024.my_port;
        txdnet.tcpframe.destport=tcp1024.dest_port;
        txdnet.tcpframe.seqnumber=tcp1024.snd_nxt;
        txdnet.tcpframe.acknumber=tcp1024.rcv_nxt;
        txdnet.tcpframe.offset=0x50;
        txdnet.tcpframe.control=0x18;//tcp_ack; //syn+ack
        txdnet.tcpframe.window=tcp1024.snd_wnd;
        txdnet.tcpframe.urg=0;
        txdnet.tcpframe.crc=0;
        txdnet.ipframe.destip[0]=tcp1024.dest_ip[0];
        txdnet.ipframe.destip[1]=tcp1024.dest_ip[1];
        txdnet.ipframe.sourceip[0]=my_ip_address.words[0];
        txdnet.ipframe.sourceip[1]=my_ip_address.words[1];
        txdnet.ipframe.ttl=0;
        txdnet.ipframe.protocal=6;//tcp
        for(i=0;i<data_length;i++)
        {
		txdnet.tcpframe.tcpdata[i]=tcpdata_buf[i];
	}
	
	for(i=0;i<3;i++)
        {
        	txdnet.etherframe.destnodeid[i]=tcp1024.dest_node_id[i];//目的网卡地址
        }
        
        txdnet.ipframe.crc=data_length+20;

	txdnet.tcpframe.crc=createtcpcrc();
	
	b_dvrcommand=1;

        create_ip_packet(dvr_ip_address,data_length+20,6);
}

void tcp_recievedata(void)
{
	unsigned char temp;

	temp=rxdnet.tcpframe.offset>>4;
	temp=temp*4+20;//20bytes ip head
	temp=rxdnet.ipframe.totallength-temp;

        tcp1024.snd_nxt=rxdnet.tcpframe.acknumber;
        tcp1024.rcv_nxt=rxdnet.tcpframe.seqnumber+temp;
}

void tcp_sendack(void)
{
	unsigned char i;
	
        txdnet.tcpframe.sourceport=tcp1024.my_port;
        txdnet.tcpframe.destport=tcp1024.dest_port;

        txdnet.tcpframe.seqnumber=tcp1024.snd_nxt;
        txdnet.tcpframe.acknumber=tcp1024.rcv_nxt;
        txdnet.tcpframe.offset=0x50;
        txdnet.tcpframe.control=0x10; //syn+ack
        txdnet.tcpframe.window=tcp1024.snd_wnd;
        txdnet.tcpframe.urg=0;
        txdnet.tcpframe.crc=0;
        txdnet.ipframe.destip[0]=tcp1024.dest_ip[0];
        txdnet.ipframe.destip[1]=tcp1024.dest_ip[1];
        txdnet.ipframe.sourceip[0]=my_ip_address.words[0];
        txdnet.ipframe.sourceip[1]=my_ip_address.words[1];
        txdnet.ipframe.ttl=0;
        txdnet.ipframe.protocal=6;//tcp
        txdnet.ipframe.crc=20;
        txdnet.tcpframe.crc=createtcpcrc();
        b_dvrcommand=1;
        
	for(i=0;i<3;i++)
        {
        	txdnet.etherframe.destnodeid[i]=tcp1024.dest_node_id[i];//目的网卡地址
        }
        
        create_ip_packet(dvr_ip_address,20,6);
        tcpconnect=0;
}

void tcp_close(unsigned char state)
{
	unsigned char i;
	
        txdnet.tcpframe.sourceport=tcp1024.my_port;
        txdnet.tcpframe.destport=tcp1024.dest_port;

        txdnet.tcpframe.seqnumber=rxdnet.tcpframe.acknumber;
        txdnet.tcpframe.acknumber=rxdnet.tcpframe.seqnumber+1;
        txdnet.tcpframe.offset=0x50;
        txdnet.tcpframe.control=state; //syn+ack
        txdnet.tcpframe.window=tcp1024.snd_wnd;
        txdnet.tcpframe.urg=0;
        txdnet.tcpframe.crc=0;
        txdnet.ipframe.destip[0]=tcp1024.dest_ip[0];
        txdnet.ipframe.destip[1]=tcp1024.dest_ip[1];
        txdnet.ipframe.sourceip[0]=my_ip_address.words[0];
        txdnet.ipframe.sourceip[1]=my_ip_address.words[1];
        txdnet.ipframe.ttl=0;
        txdnet.ipframe.protocal=6;//tcp
        txdnet.ipframe.crc=20;
        txdnet.tcpframe.crc=createtcpcrc();
        b_dvrcommand=1;
	
	for(i=0;i<3;i++)
        {
        	txdnet.etherframe.destnodeid[i]=tcp1024.dest_node_id[i];//目的网卡地址
        }
        
        create_ip_packet(dvr_ip_address,20,6);
        tcpconnect=0;
}

void FunRstTcp(void)
{
	unsigned char i;
	
        txdnet.tcpframe.sourceport=tcp1024.my_port;
        txdnet.tcpframe.destport=tcp1024.dest_port;
        txdnet.tcpframe.seqnumber=tcp1024.snd_nxt;
        txdnet.tcpframe.acknumber=tcp1024.rcv_nxt;
        txdnet.tcpframe.offset=0x50;
        txdnet.tcpframe.control=0x14;//ack+rst
        txdnet.tcpframe.window=tcp1024.snd_wnd;
        txdnet.tcpframe.urg=0;
        txdnet.tcpframe.crc=0;
        txdnet.ipframe.destip[0]=tcp1024.dest_ip[0];
        txdnet.ipframe.destip[1]=tcp1024.dest_ip[1];
        txdnet.ipframe.sourceip[0]=my_ip_address.words[0];
        txdnet.ipframe.sourceip[1]=my_ip_address.words[1];
        txdnet.ipframe.ttl=0;
        txdnet.ipframe.protocal=6;//tcp
      
        txdnet.ipframe.crc=20;

	txdnet.tcpframe.crc=createtcpcrc();
	
	b_dvrcommand=1;
	
	for(i=0;i<3;i++)
        {
        	txdnet.etherframe.destnodeid[i]=tcp1024.dest_node_id[i];//目的网卡地址
        }	

        create_ip_packet(dvr_ip_address,20,6);	
}
void FunSendTcpAck(void)
{

⌨️ 快捷键说明

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