Andrew Boyson / net

Dependents:   oldheating gps motorhome heating

Revision:
22:914b970356f0
Parent:
14:e75a59c1123d
Child:
30:e34173b7585c
diff -r 02c82594c8c0 -r 914b970356f0 eth/arp.cpp
--- a/eth/arp.cpp	Thu Jun 29 19:42:28 2017 +0000
+++ b/eth/arp.cpp	Mon Jul 03 14:29:07 2017 +0000
@@ -4,10 +4,15 @@
 #include  "eth.h"
 #include  "mac.h"
 #include "dhcp.h"
+#include   "ar.h"
+#include   "io.h"
 
 #define REQUEST   1
 #define REPLY     2
 
+uint32_t ArpAddressToResolve;
+bool     ArpResolveRequestFlag = false;
+
 __packed struct header
 {
     int16_t  hardwareType;             //16.bit: (ar$hrd) Hardware address space (e.g., Ethernet, Packet Radio Net). Always 1.
@@ -34,16 +39,44 @@
     if (protocolType          != IPV4         ) return DO_NOTHING; //This is not IPv4
     if (hardwareLength        != 6            ) return DO_NOTHING; //This is not a MAC hardware address
     if (protocolLength        != 4            ) return DO_NOTHING; //This is not an IPv4 IP address
-    if (opCode                != REQUEST      ) return DO_NOTHING; //This is not a request
     if (targetProtocolAddress != DhcpLocalIp  ) return DO_NOTHING; //This packet was not addressed to us
     
-    memcpy(pHeader->targetHardwareAddress,  pHeader->senderHardwareAddress, 6);
-           pHeader->targetProtocolAddress = pHeader->senderProtocolAddress;
+    switch (opCode)
+    {
+        case REQUEST:
+            memcpy(pHeader->targetHardwareAddress,  pHeader->senderHardwareAddress, 6);
+                   pHeader->targetProtocolAddress = pHeader->senderProtocolAddress;
+            memcpy(pHeader->senderHardwareAddress,  MacLocal,6);
+                   pHeader->senderProtocolAddress = DhcpLocalIp;
+                   pHeader->opCode                = NetToHost16(REPLY);
+            memcpy(pDstMac, pSrcMac, 6);
+            return UNICAST;
+        case REPLY:
+            ArAdd4(pHeader->senderHardwareAddress, pHeader->senderProtocolAddress);
+            return DO_NOTHING;
+        default:
+            return DO_NOTHING;
+    }
+}
+int ArpPollForPacketToSend(void* pPacket, int* pSize)
+{
+    if (!ArpResolveRequestFlag) return DO_NOTHING;
+    ArpResolveRequestFlag = false;
+    
+    struct header* pHeader = (header*)pPacket;
+    
+    pHeader->hardwareType   = NetToHost16(ETHERNET);
+    pHeader->protocolType   = NetToHost16(IPV4);
+    pHeader->hardwareLength = 6;
+    pHeader->protocolLength = 4;
+    pHeader->opCode         = NetToHost16(REQUEST);
+    
+    memset(pHeader->targetHardwareAddress,  0, 6);
+           pHeader->targetProtocolAddress = ArpAddressToResolve;
     memcpy(pHeader->senderHardwareAddress,  MacLocal,6);
            pHeader->senderProtocolAddress = DhcpLocalIp;
-           pHeader->opCode                = NetToHost16(REPLY);
+           
+    *pSize = sizeof(header);
     
-    memcpy(pDstMac, pSrcMac, 6);
-    
-    return UNICAST;
-}
+    return BROADCAST;
+}
\ No newline at end of file