/*
* Acess2 IP Stack
* - Address Resolution Protocol
+ * - Part of the IPv4 protocol
*/
#define DEBUG 0
#include "ipstack.h"
#include "arp.h"
#include "link.h"
-#define ARPv6 1
+#define ARPv6 0
#define ARP_CACHE_SIZE 64
#define ARP_MAX_AGE (60*60*1000) // 1Hr
int giARP_Cache6Space;
tMutex glARP_Cache6;
#endif
- int giARP_LastUpdateID = 0;
+volatile int giARP_LastUpdateID = 0;
// === CODE ===
/**
int lastID;
int i;
struct sArpRequest4 req;
+ Sint64 timeout;
ENTER("pInterface xAddress", Interface, Address);
+ // Check routing tables if not on this subnet
+ if( IPStack_CompareAddress(4, &Address, Interface->Address, Interface->SubnetBits) == 0 )
+ {
+ tRoute *route = IPStack_FindRoute(4, Interface, &Address);
+ // If the next hop is defined, use it
+ // - 0.0.0.0 as the next hop means "no next hop / direct"
+ if( route && ((tIPv4*)route->NextHop)->L != 0 )
+ {
+ // Recursion: see /Recursion/
+ return ARP_Resolve4(Interface, *(tIPv4*)route->NextHop);
+ }
+ }
+
+ // Check ARP Cache
Mutex_Acquire( &glARP_Cache4 );
for( i = 0; i < giARP_Cache4Space; i++ )
{
req.SWSize = 4;
req.Request = htons(1);
req.SourceMac = Interface->Adapter->MacAddr;
- req.SourceIP = Interface->IP4.Address;
+ req.SourceIP = *(tIPv4*)Interface->Address;
req.DestMac = cMAC_BROADCAST;
req.DestIP = Address;
// Send Request
Link_SendPacket(Interface->Adapter, 0x0806, req.DestMac, sizeof(struct sArpRequest4), &req);
+ timeout = now() + Interface->TimeoutDelay;
+
// Wait for a reply
for(;;)
{
- while(lastID == giARP_LastUpdateID) Threads_Yield();
+ while(lastID == giARP_LastUpdateID && now() < timeout) {
+ Threads_Yield();
+ }
+
+ if( now() >= timeout ) {
+ Log_Log("ARP4", "Timeout");
+ break; // Timeout
+ }
+
lastID = giARP_LastUpdateID;
Mutex_Acquire( &glARP_Cache4 );
if(gaARP_Cache4[i].IP.L != Address.L) continue;
Mutex_Release( &glARP_Cache4 );
+ Log_Debug("ARP4", "Return %02x:%02x:%02x:%02x:%02x:%02x",
+ gaARP_Cache4[i].MAC.B[0], gaARP_Cache4[i].MAC.B[1],
+ gaARP_Cache4[i].MAC.B[2], gaARP_Cache4[i].MAC.B[3],
+ gaARP_Cache4[i].MAC.B[4], gaARP_Cache4[i].MAC.B[5]);
return gaARP_Cache4[i].MAC;
}
Mutex_Release( &glARP_Cache4 );
}
+ {
+ tMacAddr ret = {{0,0,0,0,0,0}};
+ return ret;
+ }
}
/**
i = free;
else
i = oldest;
- gaARP_Cache4[i].IP = SWAddr;
}
Log_Log("ARP4", "Caching %i.%i.%i.%i (%02x:%02x:%02x:%02x:%02x:%02x) in %i",
i
);
+ gaARP_Cache4[i].IP = SWAddr;
gaARP_Cache4[i].MAC = HWAddr;
gaARP_Cache4[i].LastUpdate = now();
giARP_LastUpdateID ++;
}
#endif
- Log_Debug("ARP", "Request ID %i", ntohs(req4->Request));
-
switch( ntohs(req4->Request) )
{
case 1: // You want my IP?
- Log_Debug("ARP", "ARP Request Address class %i", req4->SWSize);
// Check what type of IP it is
switch( req4->SWSize )
{
case 4:
- Log_Debug("ARP", "From MAC %02x:%02x:%02x:%02x:%02x:%02x",
- req4->SourceMac.B[0], req4->SourceMac.B[1],
- req4->SourceMac.B[2], req4->SourceMac.B[3],
- req4->SourceMac.B[4], req4->SourceMac.B[5]);
- //Log_Debug("ARP", "to MAC %02x:%02x:%02x:%02x:%02x:%02x",
- // req4->DestMac.B[0], req4->DestMac.B[1],
- // req4->DestMac.B[2], req4->DestMac.B[3],
- // req4->DestMac.B[4], req4->DestMac.B[5]);
Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i from %i.%i.%i.%i",
req4->DestIP.B[0], req4->DestIP.B[1], req4->DestIP.B[2],
req4->DestIP.B[3],
req4->SourceIP.B[0], req4->SourceIP.B[1],
req4->SourceIP.B[2], req4->SourceIP.B[3]);
+ Log_Debug("ARP", " from MAC %02x:%02x:%02x:%02x:%02x:%02x",
+ req4->SourceMac.B[0], req4->SourceMac.B[1],
+ req4->SourceMac.B[2], req4->SourceMac.B[3],
+ req4->SourceMac.B[4], req4->SourceMac.B[5]);
iface = IPv4_GetInterface(Adapter, req4->DestIP, 0);
if( iface )
{
- Log_Debug("ARP", "Caching sender's IP Address");
ARP_UpdateCache4(req4->SourceIP, req4->SourceMac);
req4->DestIP = req4->SourceIP;
req4->DestMac = req4->SourceMac;
- req4->SourceIP = iface->IP4.Address;
+ req4->SourceIP = *(tIPv4*)iface->Address;;
req4->SourceMac = Adapter->MacAddr;
req4->Request = htons(2);
Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",
{
req6->DestIP = req6->SourceIP;
req6->DestMac = req6->SourceMac;
- req6->SourceIP = iface->IP6.Address;
+ req6->SourceIP = *(tIPv6*)iface->Address;
req6->SourceMac = Adapter->MacAddr;
req6->Request = htons(2);
Log_Debug("ARP", "Sending back us (%02x:%02x:%02x:%02x:%02x:%02x)",