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

📄 arp.c

📁 CP2201和51单片机实现ARP ICMP IP UDP协议
💻 C
字号:
/*==============================================*/
//	ARP协议程序

/*==============================================*/

#include   "ne2000.h"
#include  "eth.h"
#include "ip.h"
#include "pubilc.h"
#include <stdio.h>
static unsigned char	arpindex = 0; //ARP table 循环加入点
extern union NetNode xdata myNode;		
/*==============================================*/
//	函数名称:	arp动态缓冲区初始化
/*==============================================*/
void arptab_init(void)
{
	INT8U i,j;
	for(i = 0; i < MaxLenARPtable; i++)
	{
		for(j = 0; j < 12; j++)
		{
			arp_tab[i].bytes[j] = 0;
		}
	}
	waiting_for_arp=0;
}

/*==============================================*/
//	函数名称:	arp请求
/*==============================================*/
void arp_request(union ip_address_type ip_address)
{
	INT8U  i;
	INT8U xdata arp_req[60];
    union ethernet_address_type xdata castaddress;
  	arp_head xdata *arp;
	arp=(arp_head xdata *)(arp_req+14);
	for(i = 0; i < 6; i++)
	{
		castaddress.bytes[i] = 0xFF;
		arp->sourcenodeid[i] = myNode.node.mac[i];
	}

	for(i = 0; i < 4; i++)
	{
		arp->sourceip[i] = myNode.nodebytes.ipbytes[i];
		arp->destip[i] = ip_address.bytes[i];
	}
	arp->harewaretype=0x0001;  //     0x0100;
	arp->protocaltype=0x0800;  //0x0008;
	arp->halength=0x06;
	arp->palength=0x04;
 	arp->operation=0x0001;   //0x0100;//应答代码
 	for(i=42;i<60;i++)
	arp_req[i]=0x00;
	eth_send(arp_req,castaddress, ARP_PACKET, 46);
	waiting_for_arp=1;
}

/*==============================================*/
//	函数名称:	arp应答
/*==============================================*/
void arp_answer(INT8U xdata *inbuf)
{       
	INT16U i; 
	union ethernet_address_type answeraddress;
	unsigned char xdata arp_outbuf[60];
  	arp_head xdata *arp_in,*arp_out;
  	arp_in=(arp_head xdata *)(inbuf+14);
  	arp_out=(arp_head xdata *)(arp_outbuf+14);
   // union ethernet_address_type answeraddress;
	if( (arp_in->destip[0] == myNode.nodebytes.ipbytes[0])
		&& (arp_in->destip[1] == myNode.nodebytes.ipbytes[1])
		&& (arp_in->destip[2] == myNode.nodebytes.ipbytes[2])
		&& (arp_in->destip[3] == myNode.nodebytes.ipbytes[3]))
	 {								//表示是向我这个ip地址的请求
	    	for(i = 12; i < 60; i++)
		{							//复制arp到发送缓冲区
	    		arp_outbuf[i]=inbuf[i];
	    	}
	    	for(i = 0; i < 6; i++)
		{							//复制对方网卡地址或网关地址   
			answeraddress.bytes[i]=arp_in->sourcenodeid[i];
			arp_out->sourcenodeid[i]=myNode.node.mac[i];
			arp_out->destnodeid[i]=arp_in->sourcenodeid[i];
	    	}
		for(i = 0; i < 4; i++)
		{
			arp_out->destip[i]=arp_in->sourceip[i];
			arp_out->sourceip[i]=arp_in->destip[i];
		}
		arp_out->operation=0x0002;     //0x0200;//响应代码
        eth_send (arp_outbuf,answeraddress,ARP_PACKET, 46);
	}
	else
	{
	  return;
	}
}

/*==============================================*/
//	函数名称:	arp应答处理
/*==============================================*/
void arp_process(INT8U xdata *inbuf)
{
    INT8U i,j;
	union ip_address_type xdata rx_ip;
	union ethernet_address_type xdata rx_hwaddr;
  	arp_head xdata *arp;
  	arp=(arp_head xdata *)(inbuf+14);
        for(i=0;i<4;i++)
        rx_ip.bytes[i]=arp->sourceip[i];
        for(i=0;i<6;i++)
        rx_hwaddr.bytes[i]=arp->sourcenodeid[i];
	if ((waiting_for_arp) && (wait.ipaddr == rx_ip.dwords))
   		{
   		waiting_for_arp = 0;
		eth_send(wait.bytebuf,rx_hwaddr,wait.proto_id ,wait.len);
		}
        for(i = 0; i < MaxLenARPtable; i++)
	{
		{
			if((arp_tab[i].arp.ip_address.bytes[0] == arp->sourceip[0])
				&& (arp_tab[i].arp.ip_address.bytes[1] == arp->sourceip[1])
				&& (arp_tab[i].arp.ip_address.bytes[2] == arp->sourceip[2])
				&& (arp_tab[i].arp.ip_address.bytes[3] == arp->sourceip[3]))
			{
				arp_tab[i].arp.ttl=0x80;
				arp_tab[i].arp.status = 1;
				for(j = 0; j < 4; j++)
					arp_tab[i].arp.ip_address.bytes[j]=arp->sourceip[j];
				for(j = 0; j < 6; j++)
					arp_tab[i].arp.ethernet_address.bytes[j]=arp->sourcenodeid[j];
				return;
			}
		}
		
		
	}
	arp_tab[arpindex].arp.status = 1;		//write arp package to some location.
	arp_tab[arpindex].arp.ttl = 0x80;
	for(j = 0; j < 4; j++)
		arp_tab[arpindex].arp.ip_address.bytes[j] = arp->sourceip[j];
	for(j = 0; j < 6; j++)
		arp_tab[arpindex].arp.ethernet_address.bytes[j] = arp->sourcenodeid[j];
	arpindex++;
	if(arpindex == MaxLenARPtable)
		arpindex = 0;
}

/*==============================================*/
//	函数名称:	更新ARP缓存
/*==============================================*/
void updatearptab(void)
{
	INT8U i;

	for(i = 0; i < MaxLenARPtable; i++)
	{
		if(arp_tab[i].arp.status == 1)
		{
			if(arp_tab[i].arp.ttl == 0)
			{
				arp_tab[i].arp.status=0;	
			}
			else
			{
				arp_tab[i].arp.ttl--;
			}
		}
	}
}

/*==============================================*/
//	函数名称:	在ARP缓存中查找指定IP/MAC映射对
/*==============================================*/
unsigned char arp_find_mac(union ip_address_type ip,union ethernet_address_type xdata *macadr)
{
	INT8U i,j;

	for(i = 0; i < MaxLenARPtable; i++)
	{
		if(arp_tab[i].arp.status == 1)
		      {
		      	if((arp_tab[i].arp.ip_address.bytes[0] == ip.bytes[0])
				&& (arp_tab[i].arp.ip_address.bytes[1] == ip.bytes[1])
				&& (arp_tab[i].arp.ip_address.bytes[2] == ip.bytes[2])
				&& (arp_tab[i].arp.ip_address.bytes[3] == ip.bytes[3]))
				{
				for(j = 0; j < 6; j++)
					macadr->bytes[j] = arp_tab[i].arp.ethernet_address.bytes[j];
				return 1;
		  		}
		  	}
	}
	return 0;
}

/*==============================================*/
//	函数名称:	在ARP缓存中直接加入IP/MAC映射对
/*==============================================*/
void arp_ip_mac(INT8U xdata *inbuf, union ethernet_address_type hard_addr)
{
	INT8U i,j;
  	 ip_head *ip;
  	 ip=(ip_head *)(inbuf+14);
	for(i = 0; i < MaxLenARPtable; i++)
	{
		{
			if((arp_tab[i].arp.ip_address.bytes[0] == ip->sourceip[0])
				&& (arp_tab[i].arp.ip_address.bytes[1] == ip->sourceip[1])
				&& (arp_tab[i].arp.ip_address.bytes[2] == ip->sourceip[2])
				&& (arp_tab[i].arp.ip_address.bytes[3] == ip->sourceip[3]))
			{
				arp_tab[i].arp.ttl=0x80;
				arp_tab[i].arp.status = 1;
				for(j = 0; j < 4; j++)
					arp_tab[i].arp.ip_address.bytes[j] = ip->sourceip[j];
				for(j = 0; j < 6; j++)
					arp_tab[i].arp.ethernet_address.bytes[j] = hard_addr.bytes[j];
				return;
			}
		}
		
	}
	arp_tab[arpindex].arp.status = 1;		//write arp package to some location.
	arp_tab[arpindex].arp.ttl = 0x80;
	for(j = 0; j < 4; j++)
		arp_tab[arpindex].arp.ip_address.bytes[j] = ip->sourceip[j];
	for(j = 0; j < 6; j++)
		arp_tab[arpindex].arp.ethernet_address.bytes[j] = hard_addr.bytes[j];
	arpindex++;
	if(arpindex == MaxLenARPtable)
		arpindex = 0;
}
void arp_rcve(const INT8U xdata *inbuf)
  {
  	
  	arp_head xdata *arp;
  	arp=(arp_head xdata *)(inbuf+14);
  	switch(arp->operation)	
  	{
         case 0x0001:
  	 arp_answer(inbuf);  
  	 break;
  	 case 0x0002:
  	 arp_process(inbuf);
  	 break;
  	default:
        break;
    }
  }

⌨️ 快捷键说明

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