A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.

Dependents:   oldheating gps motorhome heating

Revision:
14:e75a59c1123d
Parent:
11:c051adb70c5a
Child:
15:6ca6778168b1
--- a/ip4/ip4.cpp	Mon May 01 18:20:55 2017 +0000
+++ b/ip4/ip4.cpp	Fri May 05 17:44:16 2017 +0000
@@ -6,6 +6,7 @@
 #include   "ar.h"
 #include "dhcp.h"
 #include  "eth.h"
+#include   "ip.h"
 #include  "ip4.h"
 
 #define DEBUG false
@@ -16,6 +17,15 @@
 #define IP4_MULTICAST_DNS_ADDRESS   0xFB0000E0
 #define IP4_MULTICAST_LLMNR_ADDRESS 0xFC0000E0
 
+int Ip4AddressToString(uint32_t ip, int size, char* text)
+{
+    int a0 = (ip & 0xFF000000) >> 24;
+    int a1 = (ip & 0x00FF0000) >> 16;
+    int a2 = (ip & 0x0000FF00) >>  8;
+    int a3 = (ip & 0x000000FF);
+    return snprintf(text, size, "%d.%d.%d.%d", a3, a2, a1, a0); 
+}
+
 void Ip4DestIpFromAction(int action, uint32_t* pDstIp)
 {
     switch (action)
@@ -62,8 +72,8 @@
 static uint8_t     protocol;
 static uint16_t    checksum;
 static uint16_t     calcsum;
-static uint32_t       srcIp;
-static uint32_t       dstIp;
+uint32_t       Ip4Src;
+uint32_t       Ip4Dst;
 static void*          pData;
 static int       dataLength;
 
@@ -83,8 +93,8 @@
              protocol      =             pHeader->protocol;
              checksum      = NetToHost16(pHeader->checksum);
              calcsum       = NetCheckSum(headerLength, pHeader);
-             srcIp         =             pHeader->src;
-             dstIp         =             pHeader->dst;
+             Ip4Src        =             pHeader->src;
+             Ip4Dst        =             pHeader->dst;
              pData         =      (char*)pHeader + headerLength;
              dataLength    =         totalLength - headerLength;
 }
@@ -102,8 +112,8 @@
     pHeader->ttl         = ttl;
     pHeader->protocol    = protocol;
     
-    pHeader->dst         = dstIp;
-    pHeader->src         = srcIp;
+    pHeader->dst         = Ip4Dst;
+    pHeader->src         = Ip4Src;
     pHeader->length      = NetToHost16(headerLength + dataLength);
     pHeader->checksum    = 0;
     pHeader->checksum    = NetCheckSum(headerLength, pHeader);
@@ -125,13 +135,13 @@
     else               LogF("  No more fragments\r\n");
     LogF("  Offset            %d\r\n", offset);
     LogF("  Time to live      %d\r\n", ttl);
-    NetProtocolToString(protocol, sizeof(text), text);
+    IpProtocolToString(protocol, sizeof(text), text);
     LogF("  Protocol          %s\r\n", text);
     LogF("  Checksum (hex)    %04hX\r\n", checksum);
     LogF("  Calculated (hex)  %04hX\r\n",  calcsum);
-    NetIp4AddressToString(srcIp, sizeof(text), text);
+    Ip4AddressToString(Ip4Src, sizeof(text), text);
     LogF("  Source IP         %s\r\n", text);
-    NetIp4AddressToString(dstIp, sizeof(text), text);
+    Ip4AddressToString(Ip4Dst, sizeof(text), text);
     LogF("  Destination IP    %s\r\n", text);
 }
 int Ip4HandleReceivedPacket(char* pSrcMac, void* pPacket, int* pSize, char* pDstMac)
@@ -139,23 +149,23 @@
     struct header * pHeader = (header*)pPacket;
     readHeader(pHeader);
     
-    bool isMe        = dstIp == DhcpLocalIp;
-    bool isBroadcast = dstIp == IP4_BROADCAST_ADDRESS;
-    bool isMulticast = (dstIp & 0xE0) == 0xE0; //224.x.x.x == 1110 0000 == E0.xx.xx.xx == xx.xx.xx.E0 in little endian
+    bool isMe        = Ip4Dst == DhcpLocalIp;
+    bool isBroadcast = Ip4Dst == IP4_BROADCAST_ADDRESS;
+    bool isMulticast = (Ip4Dst & 0xE0) == 0xE0; //224.x.x.x == 1110 0000 == E0.xx.xx.xx == xx.xx.xx.E0 in little endian
     
     if (!isMe && !isBroadcast && !isMulticast) return DO_NOTHING;
     
-    ArAdd4(pSrcMac, srcIp);
+    ArAdd4(pSrcMac, Ip4Src);
     
     if (DEBUG) logHeader("IP4 packet received");
 
     int action = DO_NOTHING;
     switch (protocol)
     {
-        case ICMP: action = IcmpHandleReceivedPacket(&srcIp, &dstIp, &dataLength, pData); break;
+        case ICMP: action = IcmpHandleReceivedPacket(&Ip4Src, &Ip4Dst, &dataLength, pData); break;
         case IGMP: return DO_NOTHING;
-        case UDP:  action = Udp4HandleReceivedPacket(&srcIp, &dstIp, &dataLength, pData); break;
-        case TCP:  action = Tcp4HandleReceivedPacket(&srcIp, &dstIp, &dataLength, pData); break;        
+        case UDP:  action = Udp4HandleReceivedPacket(&Ip4Src, &Ip4Dst, &dataLength, pData); break;
+        case TCP:  action = Tcp4HandleReceivedPacket(&Ip4Src, &Ip4Dst, &dataLength, pData); break;        
         default:
             logHeader("IP4 packet unhandled");
             return DO_NOTHING;
@@ -187,14 +197,14 @@
     protocol      = UDP;
     
     int action  = DO_NOTHING;
-    if (!action) action = Udp4PollForPacketToSend(pData, &dataLength, &srcIp, &dstIp);
+    if (!action) action = Udp4PollForPacketToSend(pData, &dataLength, &Ip4Src, &Ip4Dst);
     if (!action) return DO_NOTHING;
     switch (action)
     {
         case UNICAST:
         case UNICAST_DNS:
         case UNICAST_DHCP:
-            ArRev4(dstIp, pDstMac);             //Make the remote MAC from ARP
+            ArRev4(Ip4Dst, pDstMac);             //Make the remote MAC from ARP
             break;
     }