📄 arp.c
字号:
#include "p_tcpip.h"
static XCHAR iTimer; //55
static XCHAR iRetries; //56
static PXCHAR Rcv_pPeerMacAddress; //81
static PXCHAR Rcv_pPeerIPAddress; //83
static PXCHAR Snd_DstMACAddress; //85
static PXCHAR Snd_DstIPAddress; //0x6b87
struct arp_packet arp;
void ArpInitialize()
{
struct struct ar *parp=(struct arp *)Adapter_pRcvPacket;
Rcv_pPeerIPAddress=parp->arp_spa;
Rcv_pPeerMacAddress=parp->arp_sha;
Snd_DstIPAddress=arp.arp_tpa;
Snd_DstMACAddress=arp.arp_tha;
p_memcpy_x2x(arp.ep_src,Local_pMacAddress,EP_ALEN); //en_src source address
arp.ep_type[0]=EPT_ARP_HIGH;
arp.ep_type[1]=EPT_ARP_LOW; //arp type
arp.hw_type[0]=0;
arp.hw_type[1]=0x01; //hard type
arp.proto_type[0]=EPT_IP_HIGH ;
arp.proto_type[1]=EPT_IP_LOW; //protocol type
arp.hw_addr_len=EP_ALEN; //hardware address length
arp.proto_len=IP_ALEN; //protocol address length
arp.arp_op=0; //Arp op
p_memcpy_x2x(&(arp.arp_sha),Local_pMacAddress,EP_ALEN);
p_memcpy_x2x(&(arp.arp_spa),Local_pIPAddress,IP_ALEN);
p_memset_x(&(arp.arp_data[ArpBufLength],0,3); //iState=0;
//iState=0;
}
void Arpqsend(UCHAR i)
{
p_memcpy_x2x(pPeerMacAddress,arp.arp_data[i*EP_ALEN],EP_ALEN);
p_memcpy_x2x(pPeerIpAddress,arp.arp_data[ARP_QSIZE*EP_ALEN+i*IP_ALEN],IP_ALEN);
DebugIP(pPeerIpAddress,0x101a);
DEBUGVALS(pPeerMacAddress,EP_ALEN,0x1010);
TaskAfterArpResolved();
}
void ArpRun()//输入模块
{
DCHAR i,j;
bdata DCHAR flag;
struct struct ar *parp=(struct arp *)Adapter_pRcvPacket;
i=parp->arp_op;
switch(i)
{
case ARP_REQUEST : //input arp_Request
if(!p_memcmp_x2x(Local_pIPAddress,parp->arp_tpa ,IP_ALEN))
{
arp.arp_op=ARP_REPLY; //ar_reply
p_memcpy_x2x(arp.ep_dst,Rcv_pPeerMacAddress,EP_ALEN);
p_memcpy_x2x(Snd_DstMACAddress,Rcv_pPeerMacAddress,EP_ALEN+IP_ALEN);
Ne2000Send(&arp.ep_dst,0x2a); //6b57发送ARP应答
}
j=0;
do
{
flag=arp.arp_data[ArpBufLength+j];
if (flag==ARP_FREE) // as_free
{ //在高速缓存中增加发送方地址
arp.arp_data[ArpBufLength+j]=ARP_RESOLVED; //28+8+3*6+3*4 //as_Resolved
p_memcpy_x2x(&arp.arp_data[j*EP_ALEN],Rcv_pPeerMacAddress,EP_ALEN);
p_memcpy_x2x(&arp.arp_data[ARP_QSIZE*EP_ALEN+j*IP_ALEN],Rcv_pPeerIPAddress,IP_ALEN);
return;
}
j++;
}while(i<ARP_QSIZE);//save 3 target mac+ip address
return;
case ARP_REPLY: //ar_Reply
if(p_memcmp_x2x(Rcv_pPeerIPAddress,Snd_DstIPAddress,IP_ALEN))
return;
j=0;
do{
flag=arp.arp_data[ArpBufLength+j];
if (flag==ARP_PENDING) //as_pending
{
arp.arp_data[ArpBufLength+j]=ARP_RESOLVED;//0x6ba7 as_Resolved
p_memcpy_x2x(&arp.arp_data[j*EP_ALEN],Rcv_pPeerMacAddress,EP_ALEN);
Arpqsend(j); //把等待ARP队列的分组发送出去
return;
}
j++;
}while(j<ARP_QSIZE);
return;
default :
return;
}
}
BOOLEAN NetMatch(PXCHAR pPeerIP,PXCHAR pLocalIP)
{
DCHAR i;
for (i=0;i++;i<IP_ALEN)
{
if ((pPeerIP[i] &Local_pSubnetMask[i])!=(pLocalIP[i] &Local_pSubnetMask[i]))
return FALSE;
}
return TRUE;
}
void ArpSend(PXCHAR pPeerIP)
{
DCHAR L,i,j;
L=0;
i=0;
while (i<ARP_QSIZE)
{
j=arp.arp_data[ArpBufLength+i];
if (j>=ARP_RESOLVED)
if(!p_memcmp_x2x(pPeerIP,arp.arp_data[ARP_QSIZE*EP_ALEN+i*IP_ALEN],IP_ALEN))
{
Arpqsend(i);//地址已解答发送分组
return;
}
if(j==ARP_FREE) L=i;
i++;
}
arp.arp_data[ArpBufLength+L]=ARP_PENDING; //状态地址未得到解答
p_memcpy_x2x(arp.arp_data[ARP_QSIZE*EP_ALEN+L*IP_ALEN],pPeerIP,IP_ALEN);
arp.arp_op=ARP_REQUEST; //ar_/request // iState=1;
p_memset_x(arp.ep_dst,0xff,EP_ALEN); //6b57
p_memset_x(Snd_DstMACAddress,0,EP_ALEN);
if(NetMatch(pPeerIP,Local_pIPAddress)) //0x6b49
p_memcpy_x2x(Snd_DstIPAddress,pPeerIP,IP_ALEN);
else
p_memcpy_x2x(Snd_DstIPAddress,Local_pRouteIP,IP_ALEN);
iTimer=0;
iRetries=0;//初始化
Ne2000Send(&arp.ep_dst,0x2a);//发出ARP 请求广播包
}
void ArpHandleTimer()
{
DCHAR i,j;
i=0;
while (i<ARP_QSIZE)
{
j=arp.arp_data[ArpBufLength+i];; //iState
if (j>=ARP_RESOLVED)
{
j++;
goto QD435;
}
if (j==ARP_PENDING) //as_Pending
{
iTimer=iTimer+1; //ae_ttl
if (iTimer>=ARP_TIMEOUT)
{
iRetries=iRetries+1;//ae_attempts
iTimer=0;
if (iRetries>=ARP_MAX_RETRY)
{
j=0; //as_free
DebugVal(0x1019);
TASKARPFAILED(); //q5c8d 释放等待分组
}
else
{
DebugIP(arp.arp_data[ARP_QSIZE*EP_ALEN+i*IP_ALEN],0x101b);
Ne2000WaitPacket();
Ne2000Send(&arp.ep_dst,0x2a);//重新广播请求
}
}// if (ACC>=3)
}// if (j==1)
QD435: arp.arp_data[ArpBufLength+i]=j;
i++;
}//while
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -