📄 arp.c
字号:
* INPUTS
*
*
* *ip_dest - Pointer to the IP destination.
* *mac_dest - Pointer to the hardware address.
* This will be filled in if found.
* *buf_ptr - Pointer to a buffer containing an IP packet.
* buf_len - the ip packet's length
* OUTPUTS
*
* 1 - Indicates successful operation.
* 0 - Indicates the address is unresolved.
* The IP is queued pending resolution.
*
*************************************************************************/
UINT8 ARP_Resolve(UINT8*ip_dest,
UINT8 *mac_dest, UINT8 *buf_ptr,UINT16 buf_len)
{
UINT16 timer;
UINT8 found;
UINT8 i;
UINT8 *ip_ptr;
//如果目的IP地址是一个子网内部的则继续向下,否则,将IP地址修改为GATEWAY的IP,继续向下,
//这样做的目的是为了转发数据报
ip_ptr = ip_dest;
i = *ip_dest;
i &= 0xe0;
if ((i & 0xe0) == 0xe0) //d类 28位多播组号
{
}
else if((i & 0xc0) == 0xc0) //c 类21位网络号8位主机号
{
for(i=0;i<4;i++)
{
if((MY_IP[i] & NET_MASK[i]) != (*(ip_dest+i) &NET_MASK[i]))
{
ip_ptr = &GATEWAY[0]; //查网关的IP地址
break;
}
}
}
else if((i & 0x80) == 0x80) // B类14位网络号16位主机号
{
for(i=0;i<4;i++)
{
if ((MY_IP[i] & NET_MASK[i]) != (*(ip_dest+i) &NET_MASK[i]))
{
ip_ptr = GATEWAY; //查网关的IP地址
break;
}
}
}
else //A类7位网络号24位主机号
{
for(i=0;i<4;i++)
{
if ((MY_IP[i] & NET_MASK[i]) != (*(ip_dest+i) &NET_MASK[i]))
{
ip_ptr = GATEWAY; //查网关的IP地址
break;
}
}
}
/* If this is a broadcast packet then simply return the ethernet broadcast
address. */
/*
if(memcmp(ip_dest, NET_BCAST,4) == 0)
{
memcp(mac_dest,NET_Ether_Broadaddr,6);
return 1;
}
*/
/* Check the ARP cache for the destination. If found, return. */
found = ARP_Find_Entry(ip_ptr);
if ( found != 0xff)
{
memcp(mac_dest, ARP_Cache[found].arp_mac_addr, DADDLEN);
return (1);
}
ARP_Request(ip_ptr,NET_Ether_Broadaddr); //broadcast the IP addr.
// if not found, save it to the Wait_IP_buffer
for(i=0;i<wait_buffer_num;i++)
{
if(Wait_IP_buffer[i].flag == 0)
{
memcp(Wait_IP_buffer[i].send_packet,buf_ptr,buf_len);
memcp(Wait_IP_buffer[i].tipnum,ip_ptr,4);
Wait_IP_buffer[i].flag = 1;
Wait_IP_buffer[i].send_packet_len = buf_len;
Wait_IP_buffer[i].time = 0xffff;
return (0);
}
}
// the oldest is discarded.
timer = Wait_IP_buffer[0].time;
for (i=1;i<wait_buffer_num;i++)
{
if (Wait_IP_buffer[i].time < timer)
{
timer = Wait_IP_buffer[i].time;
found = i;
}
}
memcp (Wait_IP_buffer[found].send_packet,buf_ptr,buf_len);
return(0);
} /* end ARP_Resolve */
/*************************************************************************
*
* FUNCTION
*
* ARP_Find_Entry
*
* DESCRIPTION
*
* This function searches the ARP cache for a matching entry.
*
* INPUTS
*
* *dest - IP address for which an ethernet address is desired.
*
* OUTPUTS
*
* *ARP_ENTRY - A pointer to an entry in the ARP cache.
* NU_NULL - If failure.
*
*************************************************************************/
UINT8 ARP_Find_Entry( UINT8* destip)
{
UINT8 i;
char j;
/* Search the cache for the target IP number. */
for (i = 0; i < ARP_CACHE_LENGTH; i++)
{
j = memcmp(destip,ARP_Cache[i].arp_ip_addr,4);
if ( j == 0)
{
// We found the entry.
//a_entry = &ARP_Cache[i];
if(ARP_Cache[i] .arp_time !=0)
{
return(i);
}
}
}
return (0xff);
} /* ARP_Find_Entry */
/*************************************************************************
*
* FUNCTION
*
* ARP_Request
*
* DESCRIPTION
*
* Send an ARP request.
*
* INPUTS
*
* *tip - The target IP number
* *thardware - The target hardware address.
*
* OUTPUTS
*
* NU_SUCCESS - If success.
* -1 - If error.
*
*************************************************************************/
void ARP_Request(UINT8 *tip, UINT8 *thardware)
{
ARP_LAYER arp_packet;
ARP_Build_Pkt(tip, &arp_packet,thardware, MY_IP, ARPREQ);//RARPQ);modified bu yugz
Ether_output(thardware,sizeof(arp_packet),(UINT8 *)&arp_packet,EARP);
return ;
} /* ARP_Request */
/*************************************************************************
*
* FUNCTION
*
* ARP_Cache_Update
*
* DESCRIPTION
*
* Add an entry to the ARP cache.
*
* INPUTS
*
* *ipn - The IP address
* *hrdn - The pointer to the hardware address
* flags - The arp flags
*
* OUTPUTS
*
* INT - Index of entry made in ARP cache.
* -1 - Entry was not found.
*
*************************************************************************/
INT16 ARP_Cache_Update(UINT8 *ipn, UINT8 *hrdn, UINT8 flags)
{
INT16 i, found = -1 ;
UINT32 timer;
UINT8 j;
for (i=0; i < ARP_CACHE_LENGTH; i++)
{
j = memcmp(ipn, ARP_Cache[i].arp_ip_addr,4) ;
if (j == 0)
{
found = i;
break;
}
}
/*
* if that IP number is not already here, take the oldest
* entry.
* If it is already here, update the info and reset the timer.
* These were pre-initialized to 0, so if any are blank, they
* will be
* taken first because they are faked to be oldest.
*/
if (found<0)
{
found = 0;
timer = ARP_Cache[found].arp_time;
for (i=1; i < ARP_CACHE_LENGTH; i++)
{
if ( (ARP_Cache[i].arp_time < timer) &&
( (ARP_Cache[i].arp_flags & ARP_PERMANENT) == 0) )
{
found = i;
timer = ARP_Cache[i].arp_time;
}
}
}
/*
* do the update to the cache
*/
memcp (ARP_Cache[found].arp_mac_addr, hrdn, DADDLEN);
memcp (ARP_Cache[found].arp_ip_addr,ipn,4);
ARP_Cache[found].arp_time = 2000; //time_20min; // here is the 20 * tick/min
ARP_Cache[found].arp_flags = (ARP_UP | flags);
return (found);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -