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:
44:83ce5ace337b
Parent:
43:bc028d5a6424
Child:
46:40d33e9037e4
--- a/ip6/ip6.cpp	Thu Oct 19 20:56:58 2017 +0000
+++ b/ip6/ip6.cpp	Sun Oct 22 17:19:17 2017 +0000
@@ -75,7 +75,7 @@
 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 Ip6DestIpFromDest(int dest, char* pDstIp)
+void Ip6DstIpFromDest(int dest, char* pDstIp)
 {
     switch (dest)
     {
@@ -91,6 +91,12 @@
             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
@@ -125,7 +131,7 @@
 {
             pHeader->versionTrafficFlow  = version << 4;
             pHeader->protocol            = protocol;
-            pHeader->hoplimit            = 255;
+            pHeader->hoplimit            = hoplimit;
     Ip6Copy(pHeader->dst, dstIp);
     Ip6Copy(pHeader->src, srcIp);
             pHeader->dataLength          = NetToHost16(dataLength);
@@ -149,7 +155,7 @@
     }
     else
     {
-        Log("IP6 header ");
+        Log("IP6   header ");
         IpProtocolToString(protocol, sizeof(text), text);
         Log(text);
         Log(" ");
@@ -207,12 +213,12 @@
     struct header * pHeader = (header*)pPacket;
     readHeader(pHeader);
     
-    bool isMe        = SlaacIsMe(dstIp);
+    int  scope       = SlaacScope(dstIp);
     bool isMulticast = dstIp[0] == 0xFF;
     bool isSolicited = getIsSolicited(dstIp);
     bool isGroup     = getIsSameGroup(dstIp, SlaacLinkLocalIp);
     
-    bool doIt = isMe || (isMulticast && !isSolicited) || (isGroup && isSolicited);
+    bool doIt = scope || (isMulticast && !isSolicited) || (isGroup && isSolicited);
     
     if (!doIt)
     {
@@ -233,17 +239,18 @@
     int action = DO_NOTHING;
     switch (protocol)
     {
-        case HOPOPT: action = DO_NOTHING;                                                         break;
-        case ICMP6:  action = Icmp6HandleReceivedPacket(trace, srcIp, dstIp, &dataLength, pData); break;
-        case UDP:    action =  Udp6HandleReceivedPacket(trace, srcIp, dstIp, &dataLength, pData); break;
-        case TCP:    action =  Tcp6HandleReceivedPacket(trace, srcIp, dstIp, &dataLength, pData); break;        
+        case HOPOPT: action = DO_NOTHING;                                                                break;
+        case ICMP6:  action = Icmp6HandleReceivedPacket(trace, scope, srcIp, dstIp, &dataLength, pData); break;
+        case UDP:    action =  Udp6HandleReceivedPacket(trace, scope, srcIp, dstIp, &dataLength, pData); break;
+        case TCP:    action =  Tcp6HandleReceivedPacket(trace, scope, srcIp, dstIp, &dataLength, pData); break;        
         default:
             LogTimeF("IP6 protocol %d unhandled\r\n", protocol);
             return DO_NOTHING;
     }
     if (!action) return DO_NOTHING;
     
-    MacCopy(pDstMac, pSrcMac);
+    if (RaIpNeedsToBeRouted(dstIp)) MacCopy(pDstMac, RaRouterMac); //Send to the router MAC
+    else                            MacCopy(pDstMac, pSrcMac);     //Send back to the source
 
     writeHeader(pHeader);
       
@@ -281,7 +288,8 @@
         case UNICAST_DNS:
         case UNICAST_DHCP:
         case UNICAST_NTP:
-            ArIpToMac6(dstIp, pDstMac);             //Make the remote MAC from NP
+            if (RaIpNeedsToBeRouted(dstIp)) MacCopy(pDstMac, RaRouterMac); //Send to the router MAC
+            else                            ArIpToMac6(dstIp, pDstMac);    //Make the remote MAC from NP
             break;
         case MULTICAST_NODE:
         case MULTICAST_ROUTER: