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:
49:1a6336f2b3f9
Parent:
48:952dddb74b8b
Child:
50:492f2d2954e4
--- a/ip6/ip6.cpp	Tue Oct 31 21:25:09 2017 +0000
+++ b/ip6/ip6.cpp	Thu Nov 02 08:10:55 2017 +0000
@@ -1,171 +1,23 @@
 #include     "mbed.h"
 #include      "log.h"
-#include      "net.h"
-#include   "action.h"
-#include    "icmp6.h"
-#include  "udptcp6.h"
-#include      "ar6.h"
-#include       "nr.h"
-#include    "slaac.h"
-#include      "eth.h"
-#include       "ip.h"
-#include      "ip6.h"
-#include      "ndp.h"
-#include       "io.h"
-#include      "ntp.h"
-#include      "mac.h"
-#include "http-reply.h"
+#include     "net.h"
+#include  "action.h"
+#include   "icmp6.h"
+#include "udptcp6.h"
+#include     "ar6.h"
+#include      "nr.h"
+#include   "slaac.h"
+#include     "eth.h"
+#include      "ip.h"
+#include "ip6addr.h"
+#include     "ndp.h"
+#include      "io.h"
+#include     "ntp.h"
+#include     "mac.h"
+#include    "http.h"
 
 bool Ip6Trace = true;
 
-void Ip6Clear(char* ip)
-{
-    *ip = 0;
-}
-bool Ip6IsEmpty(char* ip)
-{
-    return !*ip; //Just check for the first byte being non zero
-}
-static void addHexNibble(bool* pAdded, int number, int index, char** pp)
-{
-    int nibble = number;
-    if (index) nibble >>= 4;
-    nibble &= 0xF;
-    
-    if (nibble || *pAdded)
-    {
-        **pp = nibble < 10 ? nibble + '0' : nibble - 10 + 'a';
-        *pp += 1;
-        *pAdded = true;
-    }
-}
-int Ip6AddressToString(char* pIp, int size, char* pText)
-{
-    char* pIpE = pIp + 16;
-    char* p = pText;
-    while (true)
-    {
-        bool added = false;
-        if (*pIp || *(pIp + 1))
-        {
-            if (p > pText + size - 2) break;  addHexNibble(&added, *(pIp + 0), 1, &p);
-            if (p > pText + size - 2) break;  addHexNibble(&added, *(pIp + 0), 0, &p);
-            if (p > pText + size - 2) break;  addHexNibble(&added, *(pIp + 1), 1, &p);
-            if (p > pText + size - 2) break;  addHexNibble(&added, *(pIp + 1), 0, &p);
-        }
-        
-        pIp += 2;
-        if (pIp >= pIpE) break;
-        
-        if (p > pText + size - 2) break; *p++ = ':';
-    }
-    *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;
-}
-void Ip6Copy(char* ipTo, char* ipFrom)
-{
-    memcpy(ipTo, ipFrom, 16);
-}
-
-char Ip6AllNodes  [] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
-char Ip6AllRouters[] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02};
-char Ip6Mdns      [] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb};
-char Ip6Llmnr     [] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03};
-
-void Ip6DstIpFromDest(int dest, char* pDstIp)
-{
-    switch (dest)
-    {
-        case   UNICAST:                                        break;
-        case   UNICAST_DNS:    Ip6Copy(pDstIp, NdpDnsServer ); break;
-        case   UNICAST_NTP:    Ip6Copy(pDstIp, NtpServerIp6 ); break;
-        case MULTICAST_NODE:   Ip6Copy(pDstIp, Ip6AllNodes  ); break;
-        case MULTICAST_ROUTER: Ip6Copy(pDstIp, Ip6AllRouters); break;
-        case MULTICAST_MDNS:   Ip6Copy(pDstIp, Ip6Mdns      ); break;
-        case MULTICAST_LLMNR:  Ip6Copy(pDstIp, Ip6Llmnr     ); break;
-        default:
-            LogTimeF("Ip6DestIpFromDest unknown destination %d\r\n", dest);
-            break;           
-    }
-}
-void Ip6SrcIpFromScope(int scope, char* pSrcIp)
-{
-    if (scope == SCOPE_GLOBAL) Ip6Copy(pSrcIp, SlaacGlobalIp   );
-    else                       Ip6Copy(pSrcIp, SlaacLinkLocalIp);
-    //Note that scope could be SCOPE_NONE if source was multicast in which case should return the link local ip.
-}
 
 #define HEADER_LENGTH 40
 __packed struct header
@@ -192,18 +44,18 @@
     dataLength = NetToHost16(pHeader->dataLength);
     protocol   =             pHeader->protocol;
     hoplimit   =             pHeader->hoplimit;
-              Ip6Copy(srcIp, pHeader->src);
-              Ip6Copy(dstIp, pHeader->dst);
+       Ip6AddressCopy(srcIp, pHeader->src);
+       Ip6AddressCopy(dstIp, pHeader->dst);
     pData      = (char*)pHeader + HEADER_LENGTH;
 }
 static void writeHeader(struct header * pHeader)
 {
-            pHeader->versionTrafficFlow  = version << 4;
-            pHeader->protocol            = protocol;
-            pHeader->hoplimit            = hoplimit;
-    Ip6Copy(pHeader->dst, dstIp);
-    Ip6Copy(pHeader->src, srcIp);
-            pHeader->dataLength          = NetToHost16(dataLength);
+                   pHeader->versionTrafficFlow  = version << 4;
+                   pHeader->protocol            = protocol;
+                   pHeader->hoplimit            = hoplimit;
+    Ip6AddressCopy(pHeader->dst, dstIp);
+    Ip6AddressCopy(pHeader->src, srcIp);
+                   pHeader->dataLength          = NetToHost16(dataLength);
 }
 
 static void logHeader()