📄 arp.c
字号:
//-----------------------------------------------------------------------------
// Net ARP.C
//
// This module handles ARP messages and ARP resolution and manages
// the ARP cache. Refer to RFC 826 and RFC 1122
//-----------------------------------------------------------------------------
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "rtl8019.h"
#include "cksum.h"
#include "net.h"
#include "ip.h"
#include "arp.h"
extern WAIT wait;
extern unsigned char my_hwaddr[];
extern unsigned char broadcast_hwaddr[];
extern unsigned int my_ipaddr;
extern unsigned int my_subnet;
extern unsigned int gateway_ipaddr;
ARP_CACHE arp_cache[CACHESIZE];
unsigned char waiting_for_arp;
//unsigned short int get__int16(unsigned char* inbuf);
//void set__int16(unsigned char* inbuf,unsigned short int set);
//GET ARP HADER
void arp_getHader( void *inbuf,void *h )
{
unsigned char *pinbuf = (unsigned char*)inbuf;
unsigned char *pBuf;
ARP_HEADER *ph = (ARP_HEADER*)h;
//=========================================
//pBuf = (unsigned char *)&ph->hardware_type ;
//pBuf[1] = pinbuf[14];
//pBuf[0] = pinbuf[15];
ph->hardware_type = get__int16( pinbuf + 14 ); //以太网帧的报头为14字节 // \TCPIP\CKsum.c
//=========================================
//pBuf = (unsigned char *)&ph->protocol_type ;
//pBuf[1] = pinbuf[16];
//pBuf[0] = pinbuf[17];
ph->protocol_type = get__int16( pinbuf + 16 );
//=========================================
ph->hwaddr_len = pinbuf[18];
ph->ipaddr_len = pinbuf[19];
//=========================================
//pBuf = (unsigned char *)&ph->message_type ;
//pBuf[1] = pinbuf[20];
//pBuf[0] = pinbuf[21];
ph->message_type = get__int16( pinbuf + 20 );
//=========================================
memcpy( (unsigned char*)(ph->source_hwaddr) , (unsigned char*)(pinbuf + 22) ,6 );
//=========================================
ph->source_ipaddr = get__int32(pinbuf+28);
//=========================================
memcpy( (unsigned char*)(ph->dest_hwaddr) , (unsigned char*)(pinbuf + 32) ,6 );
//=========================================
ph->dest_ipaddr = get__int32(pinbuf+38);
}
void arp_setHader( void *inbuf,void *h )
{
unsigned char *pinbuf = (unsigned char*)inbuf;
unsigned char *pBuf;
ARP_HEADER *ph = (ARP_HEADER*)h;
//=========================================
//pBuf = (unsigned char *)&ph->hardware_type ;
//pinbuf[14] = pBuf[1];
//pinbuf[15] = pBuf[0];
set__int16(pinbuf + 14 , ph->hardware_type );
//=========================================
//pBuf = (unsigned char *)&ph->protocol_type ;
//pinbuf[16] = pBuf[1];
//pinbuf[17] = pBuf[0];
set__int16(pinbuf + 16 , ph->protocol_type );
//=========================================
pinbuf[18] = ph->hwaddr_len;
pinbuf[19] = ph->ipaddr_len;
//=========================================
//pBuf = (unsigned char *)&ph->message_type ;
//pinbuf[20] = pBuf[1];
//pinbuf[21] = pBuf[0];
set__int16(pinbuf + 20 , ph->message_type );
//=========================================
memcpy( (unsigned char*)(pinbuf + 22) , (unsigned char*)(ph->source_hwaddr) , 6 );
//=========================================
set__int32(pinbuf + 28 , ph->source_ipaddr );
//=========================================
memcpy( (unsigned char*)(pinbuf + 32) , (unsigned char*)(ph->dest_hwaddr) , 6 );
//=========================================
set__int32(pinbuf + 38 , ph->dest_ipaddr );
}
//=============================================
void init_arp()
{
memset(arp_cache, 0, sizeof(arp_cache));
memset(&wait, 0, sizeof(wait));
waiting_for_arp = FALSE;
}
//------------------------------------------------------------------------
// This is called every 60 seconds to age the ARP cache
// If an entry times out then it is deleted from the cache
// See "TCP/IP Illustrated, Volume 1" Sect 4.3
//------------------------------------------------------------------------
void age_arp_cache()
{
unsigned char i;
for (i=0; i < CACHESIZE; i++)
{
if ((arp_cache[i].ipaddr != 0) && (arp_cache[i].timer))
{
arp_cache[i].timer--;
if (arp_cache[i].timer == 0)
{
// Timed out so clear out cache entry
// Do not need to zero hwaddr
arp_cache[i].ipaddr = 0;
}
}
}
}
//------------------------------------------------------------------------
// This allocates memory for the entire outgoing message,
// including eth and ip headers, then builds an outgoing
// ARP response message
// See "TCP/IP Illustrated, Volume 1" Sect 4.4
//------------------------------------------------------------------------
void arp_send(unsigned char *hwaddr, unsigned int ipaddr, unsigned short int msg_type)
{
unsigned char outbuf[42];//arp头(28bytes)+nic头(14bytes)=42bytes
ARP_HEADER *arp,arpbuf;
arp = &arpbuf;
// Allow 14 bytes for the ethernet header
arp->hardware_type = DIX_ETHERNET;
arp->protocol_type = IP_PACKET;
arp->hwaddr_len = 6;
arp->ipaddr_len = 4;
arp->message_type = msg_type;
//My hardware address and IP addresses
memcpy(arp->source_hwaddr, my_hwaddr, 6);
arp->source_ipaddr = my_ipaddr;
// Destination hwaddr and dest IP addr
if (msg_type == ARP_REQUEST)//如果是arp请求则目标物理地址为0
{
memset(arp->dest_hwaddr, 0, 6);
}else
{
memcpy(arp->dest_hwaddr, hwaddr, 6);
}
arp->dest_ipaddr = ipaddr;
// If request then the message is a brodcast, if a response then
// send to specified hwaddr
// ARP payload size is always 28 bytes
arp_setHader( outbuf, arp );//封装以太帧报头(nic报头+arp报头)
if (msg_type == ARP_REQUEST)
{
ETH_Send(outbuf, broadcast_hwaddr, ARP_PACKET, 28);//发送arp请求
}else
{
ETH_Send(outbuf, hwaddr, ARP_PACKET, 28);//应该是arp应答
}
}
//------------------------------------------------------------------------
// This re-sends an ARP request if there was no response to
// the first one. It is called every 0.5 seconds. If there
// is no response after 2 re-tries, the datagram that IP was
// trying to send is deleted
//-----------------------------------------------------------------------
void arp_retransmit()
{
static unsigned char retries = 0;
if ((waiting_for_arp) && (wait.timer))
{
wait.timer--;
if (wait.timer == 0)
{
retries++;
if (retries <= 2)
{
//if (debug) serial_send("ARP: Re-sending ARP broadcast\r");
arp_send(NULL, wait.ipaddr, ARP_REQUEST);
wait.timer = ARP_TIMEOUT;
}
else
{
//if (debug) serial_send("ARP: Gave up waiting for response\r");
wait.timer = 0;
waiting_for_arp = 0;
//free(wait.buf);
}
}
}
}
//------------------------------------------------------------------------
// Find the ethernet hardware address for the given ip address
// If destination IP is on my subnet then we want the eth
// address of destination, otherwise we want eth addr of gateway.
// Look in ARP cache first. If not found there, send ARP request.
// Return pointer to the hardware address or NULL if not found
// See "TCP/IP Illustrated, Volume 1" Sect 4.5
//------------------------------------------------------------------------
unsigned char *arp_resolve(unsigned int dest_ipaddr)
{
unsigned short int i;
// If destination IP is not on my subnet then we really want eth addr
// of gateway, not destination IP
if ((dest_ipaddr ^ my_ipaddr) & my_subnet)
{
if (gateway_ipaddr == 0)
{
//if (debug) serial_send("ARP: Error, gateway addr not set\r");
return (NULL);
}
else dest_ipaddr = gateway_ipaddr;
}
// See if IP addr of interest is in ARP cache
for (i=0; i < CACHESIZE; i++)
{
if (arp_cache[i].ipaddr == dest_ipaddr)
return (&arp_cache[i].hwaddr[0]);
}
// Not in cache so broadcast ARP request
arp_send(NULL, dest_ipaddr, ARP_REQUEST);
// Set a flag to indicate that an IP datagram is waiting
// to be sent
waiting_for_arp = TRUE;
// Null means that we have sent an ARP request
return (NULL);
}
//------------------------------------------------------------------------
// This handles incoming ARP messages
// See "TCP/IP Illustrated, Volume 1" Sect 4.4
// Todo: Resolve problem of trying to add to a full cache
//------------------------------------------------------------------------
void arp_rcve(unsigned char *inbuf)//inbuf 指向收到的以太网帧
{
unsigned char i, cached, oldest;
unsigned short minimum;
ARP_HEADER *arp,p;
arp = &p;
arp_getHader(inbuf,arp);//从以太帧抽取ARP报头,放于局部变量(arp->p)
cached = FALSE;
// Validate incoming frame
if ((arp->hardware_type != DIX_ETHERNET) ||
(arp->protocol_type != IP_PACKET)) return;
// Search ARP cache for senders IP address
// If found, update entry and restart timer
for (i=0; i < CACHESIZE; i++)
{
if (arp_cache[i].ipaddr == arp->source_ipaddr)
{
memcpy(&arp_cache[i].hwaddr[0], &arp->source_hwaddr[0], 6);
arp_cache[i].timer = CACHETIME;
cached = TRUE;
break;
}
}
if (arp->dest_ipaddr != my_ipaddr) return;
// At this point we know the the frame is addressed to me
// If not already in cache then add entry and start timer
if (cached == FALSE)//在arp_cache内找不到源主机的IP地址
{
// Find first blank space and add entry
// Blank entries are indicated by ip addr = 0
for (i=0; i < CACHESIZE; i++)
{
if (arp_cache[i].ipaddr == 0) //有新的主机请求,将其IP和物理地址存入arp_cache
{
arp_cache[i].ipaddr = arp->source_ipaddr;
memcpy(&arp_cache[i].hwaddr[0], &arp->source_hwaddr[0], 6);
arp_cache[i].timer = CACHETIME;
break;
}
}
// If no blank entries in arp cache then sort cache
// to find oldest entry and replace it
if (i == CACHESIZE) //arp_cache已经满座!存入最老的位置
{
// Oldest entry is the one with lowest timer value
minimum = 0xFFFF;
for (i=0; i < CACHESIZE; i++)
{
if (arp_cache[i].timer < minimum)
{
minimum = arp_cache[i].timer;
oldest = i;
}
}
// "oldest" is now index of oldest entry, so replace it
arp_cache[oldest].ipaddr = arp->source_ipaddr;
memcpy(&arp_cache[oldest].hwaddr[0], &arp->source_hwaddr[0], 6);
arp_cache[oldest].timer = CACHETIME;
}
}
// If we are waiting for an arp response and the arp response
// that just came in is addressed to me and is from the host
// we are waiting for, then send the message-in-waiting
if (arp->message_type == ARP_RESPONSE)//arp报文类型为arp响应;
{
if ((waiting_for_arp) && (wait.ipaddr == arp->source_ipaddr))
{
waiting_for_arp = FALSE;
/* for debug *///===================是本地所等待的arp请求响应
ip_send(wait.buf, wait.ipaddr, wait.proto_id, wait.len);
}
}
else if (arp->message_type == ARP_REQUEST)//arp报文类型为arp请求则发送ARP响应。
{
// Send ARP response
arp_send(arp->source_hwaddr, arp->source_ipaddr, ARP_RESPONSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -