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:
37:793b39683406
Parent:
36:900e24b27bfb
Child:
42:222a4f45f916
--- a/ip6/ip6.cpp	Mon Sep 25 07:09:32 2017 +0000
+++ b/ip6/ip6.cpp	Wed Oct 04 07:51:02 2017 +0000
@@ -1,6 +1,7 @@
 #include     "mbed.h"
 #include      "log.h"
 #include      "net.h"
+#include   "action.h"
 #include    "icmp6.h"
 #include  "udptcp6.h"
 #include       "ar.h"
@@ -14,7 +15,6 @@
 #include      "ntp.h"
 #include      "mac.h"
 
-#define DEBUG false
 #define SHOW_FILTERED true
 
 void Ip6Clear(char* ip)
@@ -75,9 +75,9 @@
 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 Ip6DestIpFromAction(int action, char* pDstIp)
+void Ip6DestIpFromDest(int dest, char* pDstIp)
 {
-    switch (action)
+    switch (dest)
     {
         case   UNICAST:                                        break;
         case   UNICAST_DNS:    Ip6Copy(pDstIp, RaDnsServer  ); break;
@@ -87,7 +87,7 @@
         case MULTICAST_MDNS:   Ip6Copy(pDstIp, Ip6Mdns      ); break;
         case MULTICAST_LLMNR:  Ip6Copy(pDstIp, Ip6Llmnr     ); break;
         default:
-            LogTimeF("Ip6 DestIpFromAction unknown action %d\r\n", action);
+            LogTimeF("Ip6DestIpFromDest unknown destination %d\r\n", dest);
             break;           
     }
 }
@@ -111,19 +111,6 @@
 static char      dstIp[16];
 static void*         pData;
 
-void Ip6LogHeaderSrc()
-{
-    char text[64];
-    Ip6AddressToString(srcIp, sizeof(text), text);
-    Log(text);
-}
-void Ip6LogHeaderDst()
-{
-    char text[64];
-    Ip6AddressToString(dstIp, sizeof(text), text);
-    Log(text);
-}
-
 static void readHeader(struct header * pHeader)
 {
     version    =            (pHeader->versionTrafficFlow >> 4) & 0xF;
@@ -144,10 +131,10 @@
             pHeader->dataLength          = NetToHost16(dataLength);
 }
 
-static void logHeader(char* title)
+static void logHeader()
 {
     char text[100];
-    LogTimeF("%s\r\n", title);
+    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);
@@ -191,8 +178,16 @@
     if (*pA++ != *pB++) return false;
     return *pA == *pB;
 }
-int Ip6HandleReceivedPacket(char* pSrcMac, void* pPacket, int* pSize, char* pDstMac)
+static void (*pTraceBack)(void);
+static void trace()
 {
+    pTraceBack();
+    logHeader();
+}
+int Ip6HandleReceivedPacket(void (*traceback)(void), char* pSrcMac, void* pPacket, int* pSize, char* pDstMac)
+{
+    pTraceBack = traceback;
+    
     struct header * pHeader = (header*)pPacket;
     readHeader(pHeader);
     
@@ -205,7 +200,7 @@
     
     if (!doIt)
     {
-        if (DEBUG || SHOW_FILTERED)
+        if (SHOW_FILTERED)
         {
             char text[100];
             Ip6AddressToString(dstIp, sizeof(text), text);
@@ -218,30 +213,28 @@
     
     ArAddIp6Record(pSrcMac, srcIp);
     NrMakeRequestForNameFromIp6(srcIp);
-    
-    if (DEBUG) logHeader("IP6 packet received");
 
     int action = DO_NOTHING;
     switch (protocol)
     {
         case HOPOPT: action = DO_NOTHING;                                                  break;
-        case ICMP6:  action = Icmp6HandleReceivedPacket(srcIp, dstIp, &dataLength, pData); break;
-        case UDP:    action =  Udp6HandleReceivedPacket(srcIp, dstIp, &dataLength, pData); break;
-        case TCP:    action =  Tcp6HandleReceivedPacket(srcIp, dstIp, &dataLength, pData); 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;        
         default:
-            logHeader("IP6 packet unhandled");
+            LogTimeF("IP6 protocol %d unhandled\r\n", protocol);
             return DO_NOTHING;
     }
     if (!action) return DO_NOTHING;
     
     MacCopy(pDstMac, pSrcMac);
-    
-    if (DEBUG) logHeader("IP6 packet replied to");
 
     writeHeader(pHeader);
       
     *pSize = HEADER_LENGTH + dataLength;
     
+    if (ActionGetTracePart(action)) logHeader();
+
     return action;
 }
 int Ip6PollForPacketToSend(void* pPacket, int* pSize, char* pDstMac)
@@ -264,7 +257,9 @@
         protocol = UDP;
     }
     if (!action) return DO_NOTHING;
-    switch (action)
+    
+    int dest = ActionGetDestPart(action);
+    switch (dest)
     {
         case UNICAST:
         case UNICAST_DNS:
@@ -279,16 +274,15 @@
         case SOLICITED_NODE:
             break;
         default:
-            LogTimeF("Ip6PollForPacketToSend - undefined action %d\r\n", action);
+            LogTimeF("Ip6PollForPacketToSend - undefined destination %d\r\n", dest);
             break;
-
     }
-        
-    if (DEBUG) logHeader("IP6 polled packet sent");
 
     writeHeader((header*)pPacket);
 
     *pSize = HEADER_LENGTH + dataLength;
     
+    if (ActionGetTracePart(action)) logHeader();
+
     return action;
 }