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:
47:73af5c0b0dc2
Parent:
46:40d33e9037e4
Child:
48:952dddb74b8b
--- a/ip6/ip6.cpp	Tue Oct 24 07:01:35 2017 +0000
+++ b/ip6/ip6.cpp	Thu Oct 26 14:50:24 2017 +0000
@@ -14,6 +14,7 @@
 #include       "io.h"
 #include      "ntp.h"
 #include      "mac.h"
+#include "http-reply.h"
 
 #define SHOW_FILTERED true
 
@@ -61,6 +62,74 @@
     *p = 0;
     return p - pText;
 }
+static void logHexNibble(bool* pAdded, int number, int index)
+{
+    int nibble = number;
+    if (index) nibble >>= 4;
+    nibble &= 0xF;
+    
+    if (nibble || *pAdded)
+    {
+        LogPush(nibble < 10 ? nibble + '0' : nibble - 10 + 'a');
+        *pAdded = true;
+    }
+}
+int Ip6AddressLog(char* pIp)
+{
+    int count = 0;
+    char* pIpE = pIp + 16;
+    while (true)
+    {
+        bool added = false;
+        if (*pIp || *(pIp + 1))
+        {
+            logHexNibble(&added, *(pIp + 0), 1); if (added) count++;
+            logHexNibble(&added, *(pIp + 0), 0); if (added) count++;
+            logHexNibble(&added, *(pIp + 1), 1); if (added) count++;
+            logHexNibble(&added, *(pIp + 1), 0); if (added) count++;
+        }
+        
+        pIp += 2;
+        if (pIp >= pIpE) break;
+        
+        LogPush(':'); count++;
+    }
+    return count;
+}
+static void httpHexNibble(bool* pAdded, int number, int index)
+{
+    int nibble = number;
+    if (index) nibble >>= 4;
+    nibble &= 0xF;
+    
+    if (nibble || *pAdded)
+    {
+        HttpReplyAddChar(nibble < 10 ? nibble + '0' : nibble - 10 + 'a');
+        *pAdded = true;
+    }
+}
+int Ip6AddressHttp(char* pIp)
+{
+    int count = 0;
+    char* pIpE = pIp + 16;
+    while (true)
+    {
+        bool added = false;
+        if (*pIp || *(pIp + 1))
+        {
+            httpHexNibble(&added, *(pIp + 0), 1); if (added) count++;
+            httpHexNibble(&added, *(pIp + 0), 0); if (added) count++;
+            httpHexNibble(&added, *(pIp + 1), 1); if (added) count++;
+            httpHexNibble(&added, *(pIp + 1), 0); if (added) count++;
+        }
+        
+        pIp += 2;
+        if (pIp >= pIpE) break;
+        
+        HttpReplyAddChar(':'); count++;
+    }
+    return count;
+}
 bool Ip6IsSame(char* ipA, char* ipB)
 {
     return memcmp(ipA, ipB, 16) == 0;
@@ -139,31 +208,24 @@
 
 static void logHeader()
 {
-    char text[100];
     if (NetTraceVerbose)
     {
         Log("IP6 header\r\n");
         LogF("  Version           %d\r\n", version);
         LogF("  Payload length    %d\r\n", dataLength);
         LogF("  Hop limit         %d\r\n", hoplimit);
-        IpProtocolToString(protocol, sizeof(text), text);
-        LogF("  Protocol          %s\r\n", text);
-        Ip6AddressToString(srcIp, sizeof(text), text);
-        LogF("  Source IP         %s\r\n", text);
-        Ip6AddressToString(dstIp, sizeof(text), text);
-        LogF("  Destination IP    %s\r\n", text);
+        LogF("  Protocol          "); IpProtocolLog(protocol); Log("\r\n");
+        Log ("  Source IP         "); Ip6AddressLog(srcIp);    Log("\r\n");
+        Log ("  Destination IP    "); Ip6AddressLog(dstIp);    Log("\r\n");
     }
     else
     {
         Log("IP6   header ");
-        IpProtocolToString(protocol, sizeof(text), text);
-        Log(text);
+        IpProtocolLog(protocol);
         Log(" ");
-        Ip6AddressToString(srcIp, sizeof(text), text);
-        Log(text);
+        Ip6AddressLog(srcIp);
         Log(" >>> ");
-        Ip6AddressToString(dstIp, sizeof(text), text);
-        Log(text);
+        Ip6AddressLog(dstIp);
         Log("\r\n");
     }
 }
@@ -224,11 +286,11 @@
     {
         if (SHOW_FILTERED)
         {
-            char text[100];
-            Ip6AddressToString(dstIp, sizeof(text), text);
-            LogTimeF("IP6 filtered out ip %s ", text);
-            Ip6AddressToString(srcIp, sizeof(text), text);
-            LogF("from %s \r\n", text);
+            LogTime("IP6 filtered out ip ");
+            Ip6AddressLog(dstIp);
+            LogF(" from ");
+            Ip6AddressLog(srcIp);
+            Log("\r\n");
         }
         return DO_NOTHING;
     }
@@ -268,13 +330,13 @@
     hoplimit   = 255;
     
     int action = DO_NOTHING;
-    if (action == DO_NOTHING)
+    if (!action)
     {
         action = Icmp6PollForPacketToSend(pData, &dataLength, srcIp, dstIp);
         protocol = ICMP6;
     }
     
-    if (action == DO_NOTHING)
+    if (!action)
     {
         action = Udp6PollForPacketToSend(pData, &dataLength, srcIp, dstIp);
         protocol = UDP;