Fixed some bugs in ARP and TCP Code
[tpg/acess2.git] / Modules / IPStack / arp.c
index 3f91939..cedc0b3 100644 (file)
@@ -16,7 +16,7 @@ extern tInterface     *IPv6_GetInterface(tAdapter *Adapter, tIPv6 Address, int Broad
 
 // === PROTOTYPES ===
  int   ARP_Initialise();
- int   ARP_int_Resolve4(tInterface *Interface, tIPv4 Address);
+tMacAddr       ARP_Resolve4(tInterface *Interface, tIPv4 Address);
 void   ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffer);
 
 // === GLOBALS ===
@@ -64,6 +64,7 @@ tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
 {
         int    lastID;
         int    i;
+       struct sArpRequest4     req;
        
        ENTER("pInterface xAddress", Interface, Address);
        
@@ -87,7 +88,25 @@ tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
        RELEASE( &glARP_Cache4 );
        
        lastID = giARP_LastUpdateID;
-       ARP_int_Resolve4(Interface, Address);
+       
+       // Create request
+       Log_Log("ARP4", "Asking for address %i.%i.%i.%i",
+               Address.B[0], Address.B[1], Address.B[2], Address.B[3]
+               );
+       req.HWType = htons(0x0001);     // Ethernet
+       req.Type   = htons(0x0800);
+       req.HWSize = 6;
+       req.SWSize = 4;
+       req.Request = htons(1);
+       req.SourceMac = Interface->Adapter->MacAddr;
+       req.SourceIP = Interface->IP4.Address;
+       req.DestMac = cMAC_BROADCAST;
+       req.DestIP = Address;
+       
+       // Send Request
+       Link_SendPacket(Interface->Adapter, 0x0806, req.DestMac, sizeof(struct sArpRequest4), &req);
+       
+       // Wait for a reply
        for(;;)
        {
                while(lastID == giARP_LastUpdateID)     Threads_Yield();
@@ -105,33 +124,6 @@ tMacAddr ARP_Resolve4(tInterface *Interface, tIPv4 Address)
        }
 }
 
-/**
- * \fn int ARP_int_Resolve4(tInterface *Interface, tIPv4 Address)
- * \brief Request the network to resolve an IPv4 Address
- * \return Boolean Success
- */
-int ARP_int_Resolve4(tInterface *Interface, tIPv4 Address)
-{
-       struct sArpRequest4     req;
-       
-       Log("[ARP4 ] Asking for address %i.%i.%i.%i",
-               Address.B[0], Address.B[1], Address.B[2], Address.B[3]
-               );
-       req.HWType = htons(0x100);      // Ethernet
-       req.Type   = htons(0x0800);
-       req.HWSize = 6;
-       req.SWSize = 4;
-       req.Request = htons(1);
-       req.SourceMac = Interface->Adapter->MacAddr;
-       req.SourceIP = Interface->IP4.Address;
-       req.DestMac = cMAC_BROADCAST;
-       req.DestIP = Address;
-       
-       Link_SendPacket(Interface->Adapter, 0x0806, req.DestMac, sizeof(struct sArpRequest4), &req);
-       
-       return 0;
-}
-
 /**
  * \brief Updates the ARP Cache entry for an IPv4 Address
  */
@@ -160,8 +152,15 @@ void ARP_UpdateCache4(tIPv4 SWAddr, tMacAddr HWAddr)
                gaARP_Cache4[i].IP = SWAddr;
        }
        
+       Log_Log("ARP4", "Caching %i.%i.%i.%i (%02x:%02x:%02x:%02x:%02x:%02x) in %i",
+               SWAddr.B[0], SWAddr.B[1], SWAddr.B[2], SWAddr.B[3],
+               HWAddr.B[0], HWAddr.B[1], HWAddr.B[2], HWAddr.B[3], HWAddr.B[4], HWAddr.B[5],
+               i
+               );
+               
        gaARP_Cache4[i].MAC = HWAddr;
        gaARP_Cache4[i].LastUpdate = now();
+       giARP_LastUpdateID ++;
        RELEASE(&glARP_Cache4);
 }
 
@@ -195,6 +194,7 @@ void ARP_UpdateCache6(tIPv6 SWAddr, tMacAddr HWAddr)
        
        gaARP_Cache6[i].IP = SWAddr;
        gaARP_Cache6[i].LastUpdate = now();
+       giARP_LastUpdateID ++;
        RELEASE(&glARP_Cache6);
 }
 
@@ -210,19 +210,25 @@ void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffe
        
        // Sanity Check Packet
        if( Length < sizeof(tArpRequest4) ) {
-               Log("[ARP  ] Recieved undersized packet");
+               Log_Log("ARP", "Recieved undersized packet");
                return ;
        }
        if( ntohs(req4->Type) != 0x0800 ) {
-               Log("[ARP  ] Recieved a packet with a bad type 0x%x", ntohs(req4->Type));
+               Log_Log("ARP", "Recieved a packet with a bad type 0x%x", ntohs(req4->Type));
                return ;
        }
        if( req4->HWSize != 6 ) {
-               Log("[ARP  ] Recieved a packet with HWSize != 6 (%i)", req4->HWSize);
+               Log_Log("ARP", "Recieved a packet with HWSize != 6 (%i)", req4->HWSize);
                return;
        }
        if( !MAC_EQU(req4->SourceMac, From) ) {
-               Log("[ARP  ] ARP spoofing detected", req4->HWSize);
+               Log_Log("ARP", "ARP spoofing detected "
+                       "(%02x%02x:%02x%02x:%02x%02x != %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],
+                       From.B[0], From.B[1], From.B[2],
+                       From.B[3], From.B[4], From.B[5]
+                       );
                return;
        }
        
@@ -312,5 +318,9 @@ void ARP_int_GetPacket(tAdapter *Adapter, tMacAddr From, int Length, void *Buffe
                }
                
                break;
+       
+       default:
+               Warning("[ARP  ] Unknown Request ID %i", ntohs(req4->Request));
+               break;
        }
 }

UCC git Repository :: git.ucc.asn.au