📄 arp.c
字号:
#include "C:\wql\tcpipsocket\target.h"
#include "C:\wql\tcpipsocket\arp.h"
#include "C:\wql\tcpipsocket\ether.h"
#include "C:\wql\tcpipsocket\ip51_mac.h"
#include "C:\wql\tcpipsocket\func.h"
#include <stdio.h>
#include <string.h>
/* This is our ARP cache. ARP_CACHE_LENGTH in target.h*/
// there are 180 bytes
ARP_ENTRY xdata ARP_Cache[ARP_CACHE_LENGTH];
//IP_buffer Wait_IP_buffer[10];
IP_buffer Wait_IP_buffer[wait_buffer_num];
//UINT8 NET_BCAST[4] = {0xff,0xff,0xff,0xff};
UINT8 xdata NET_Ether_Broadaddr[6] = {0xff,0xff,0xff,0xff,0xff,0xff};
UINT8 xdata NET_MASK[4] = {0xff,0xff,0xff,0};
UINT8 xdata GATEWAY[4] = {172,28,24,1};
extern UINT8 MY_IP[4];
extern void Ether_output(UINT8 *SHA,UINT16 LEN,UINT8 *PTR,UINT16 FLAG);
/*************************************************************************
*
* FUNCTION
*
* ARP_Build_Pkt
*
* DESCRIPTION
*
* This function will get a free buffer and fill in as much of the
* ARP packet fields as possible. All other fields must be updated
* by the calling function.
*
* INPUTS
* arp_packet - blank packet of arp (fill it)
* * tipnum - The target IP number.
* *thardware - The target hardware address.
* pkt_type - The ARP packet type, reply or request.
* *sipnum - The source IP number
*
* OUTPUTS
*
*
*************************************************************************/
void ARP_Build_Pkt ( UINT8 * tipnum, ARP_LAYER *arp_packet,UINT8 *thardware, UINT8 *sipnum, UINT16 pkt_type)
{
UINT8 i;
arp_packet->arp_hrd = HARDWARE_TYPE;
arp_packet->arp_hln = DADDLEN;
arp_packet->arp_pro = ARPPRO;
arp_packet->arp_pln = 4;
arp_packet->arp_sha[5] = SA0;
arp_packet->arp_sha[4] = SA1;
arp_packet->arp_sha[3] = SA2;
arp_packet->arp_sha[2] = SA3;
arp_packet->arp_sha[1] = SA4;
arp_packet->arp_sha[0] = SA5;
for (i = 0;i<6; i++)
{
// arp_packet->arp_sha[i] = i;//ETHERNET_MAC_ADDR[i]; //source hardware addr
arp_packet->arp_tha[i] = *(thardware+i); // destinate hardware addr
}
for (i =0;i<4;i++)
{
arp_packet->arp_spa[i] = sipnum[i];
arp_packet->arp_tpa[i] = tipnum[i];
}
arp_packet->arp_op = pkt_type;
return;
} /* end ARP_Build_Pkt */
/*************************************************************************
*
* FUNCTION
*
* ARP_Reply
*
* DESCRIPTION
*
* This function sends an ARP reply. Called whenever an ARP request
* is received.
*
* INPUTS
*
* *thardware - The target hardware address.
* tipnum - The target IP number.
* *dev - Pointer to the device the reply will be sent from.
* sipnum - The source IP number.
*
* OUTPUTS
* null
*
*************************************************************************/
void ARP_Reply(UINT8 *thardware, UINT8 * tipnum)
{
ARP_LAYER arp_packet;
ARP_Build_Pkt( tipnum, &arp_packet,thardware,MY_IP, ARPREP);
Ether_output(thardware,sizeof(arp_packet),(UINT8 *)&arp_packet,EARP); //debug
return;
} /* end ARP_Reply */
/*************************************************************************
*
* FUNCTION
*
* ARP_Interpret
*
* DESCRIPTION
*
* Interpret ARP packets.
* Look at incoming ARP packet and make required assessment of
* usefulness, check to see if we requested this packet, clear
* all appropriate flags.
*
* INPUTS
*
* *a_pkt - Pointer to the ARP packet to process.
* *device - Pointer to the device the packet was received
* on.
*
* OUTPUTS
*
* NU_SUCCESS - Indicates successful operation.
* NU_NO_ACTION - Indicates that the packet was determined
* to be unacceptable.
*************************************************************************/
void ARP_Interpret(ARP_LAYER *a_pkt)
{
UINT8 target_ip_addr[4];
UINT8 source_ip_addr[4];
UINT8 i;
for (i=0;i<4;i++)
{
target_ip_addr[i] = a_pkt->arp_tpa[i];
source_ip_addr[i] = a_pkt->arp_spa[i];
}
/* Check to see if the packet was for me. If not then return. Most ARP packets should fall
into this category.
*/
i = memcmp(target_ip_addr,MY_IP,4);
if(i == 0 && a_pkt ->arp_op == ARPREQ)
{
/* keep the address for me */
ARP_Cache_Update (source_ip_addr, &(a_pkt->arp_sha), 0);
ARP_Reply(a_pkt->arp_sha, source_ip_addr);
return;
}
/*
* Check for a reply that I probably asked for.
*/
i = memcmp(target_ip_addr,MY_IP,4);
if(i == 0 && a_pkt ->arp_op == ARPREP)
{
ARP_Cache_Update (source_ip_addr, &(a_pkt->arp_sha), 0);
/* Check to see if there are any packets pending the resolution of
this address. */
ARP_Check_Events(a_pkt);
return ;
}
return ;
}
/**************************************************************************
*
* FUNCTION
*
* ARP_Check_Events
*
* DESCRIPTION
*
* This function checks to see if there are any packets pending the
* resolution of a hardware address that was provided in either a
* recevied ARP response or an ARP request. If there is a pending
* packet, it will be transmitted.
*
* INPUTS
*
* *a_pkt - Pointer to a received ARP packet.
*
* OUTPUTS
*
* No output, but if a pending packet is found that packet is
* transmitted.
*
**************************************************************************/
void ARP_Check_Events(ARP_LAYER *a_pkt)
{
UINT8 i,j;
for (i = 0;i<wait_buffer_num;i++)
{
if(Wait_IP_buffer[i].flag == 1)
{
j=memcmp(Wait_IP_buffer[i].tipnum, a_pkt->arp_spa,4);
if(j== 0)
{
Ether_output(a_pkt->arp_sha,Wait_IP_buffer[i].send_packet_len,(UINT8 *)Wait_IP_buffer[i].send_packet,0X0800);
// clear the valid flag.
Wait_IP_buffer[i].flag = 0;
}
}
}
} /* ARP_Check_Events */
/*************************************************************************
*
* FUNCTION
*
* ARP_Init
*
* DESCRIPTION
*
* Initialize the ARP module.
*
* INPUTS
*
* None
*
* OUTPUTS
*
* None
*
*************************************************************************/
void ARP_Init ()
{
UINT8 i,j;
/* Clear the ARP Cache */
for (i =0 ;i<ARP_CACHE_LENGTH ;i++)
{
for(j=0;j<sizeof(ARP_ENTRY);j++)
{
*((UINT8*)(&ARP_Cache[i])+j) = 0;
}
}
/* The resolve list is initially empty. */
for(i=0;i<wait_buffer_num;i++)
{
Wait_IP_buffer[i].flag = 0;
}
// ARP_Request(GATEWAY,NET_Ether_Broadaddr); //broadcast the IP addr.
} /* ARP_Init */
/*************************************************************************
*
* FUNCTION
*
* ARP_Resolve
*
* DESCRIPTION
*
* This function attempts to resolve an ethernet hardware address.
* If unable to find an entry in the ARP cache it will queue the
* packet, an IP packet, that needs to be sent. An ARP event will
* be created to send an ARP request. The IP packet is transmitted
* once the address is resolved.
*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -