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:
43:bc028d5a6424
Parent:
42:222a4f45f916
Child:
44:83ce5ace337b
--- a/ip4/ip4.cpp	Sun Oct 15 17:54:09 2017 +0000
+++ b/ip4/ip4.cpp	Thu Oct 19 20:56:58 2017 +0000
@@ -1,17 +1,17 @@
-#include   "mbed.h"
-#include    "log.h"
-#include    "net.h"
-#include "action.h"
-#include   "icmp.h"
+#include    "mbed.h"
+#include     "log.h"
+#include     "net.h"
+#include  "action.h"
+#include   "icmp4.h"
 #include "udptcp4.h"
-#include     "ar.h"
-#include     "nr.h"
-#include   "dhcp.h"
-#include    "eth.h"
-#include     "ip.h"
-#include    "ip4.h"
-#include    "ntp.h"
-#include    "mac.h"
+#include      "ar.h"
+#include      "nr.h"
+#include    "dhcp.h"
+#include     "eth.h"
+#include      "ip.h"
+#include     "ip4.h"
+#include     "ntp.h"
+#include     "mac.h"
 
 #define SHOW_FILTERED true
 
@@ -135,26 +135,42 @@
 static void logHeader()
 {
     char text[30];
-    Log ("IPv4 header\r\n");
-    LogF("  Version           %d\r\n", version);
-    LogF("  Header length     %d\r\n", headerLength);
-    LogF("  Type of service   %d\r\n", tos);
-    LogF("  Data length       %d\r\n", dataLength);
-    LogF("  Identification    %d\r\n", id);
-    if (dontFragment)  LogF("  Don't fragment\r\n");
-    else               LogF("  Do fragment\r\n");
-    if (moreFragments) LogF("  More fragments\r\n");
-    else               LogF("  No more fragments\r\n");
-    LogF("  Offset            %d\r\n", offset);
-    LogF("  Time to live      %d\r\n", ttl);
-    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);
-    Ip4AddressToString(srcIp, sizeof(text), text);
-    LogF("  Source IP         %s\r\n", text);
-    Ip4AddressToString(dstIp, sizeof(text), text);
-    LogF("  Destination IP    %s\r\n", text);
+    if (NetTraceVerbose)
+    {
+        Log ("IPv4 header\r\n");
+        LogF("  Version           %d\r\n", version);
+        LogF("  Header length     %d\r\n", headerLength);
+        LogF("  Type of service   %d\r\n", tos);
+        LogF("  Data length       %d\r\n", dataLength);
+        LogF("  Identification    %d\r\n", id);
+        if (dontFragment)  LogF("  Don't fragment\r\n");
+        else               LogF("  Do fragment\r\n");
+        if (moreFragments) LogF("  More fragments\r\n");
+        else               LogF("  No more fragments\r\n");
+        LogF("  Offset            %d\r\n", offset);
+        LogF("  Time to live      %d\r\n", ttl);
+        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);
+        Ip4AddressToString(srcIp, sizeof(text), text);
+        LogF("  Source IP         %s\r\n", text);
+        Ip4AddressToString(dstIp, sizeof(text), text);
+        LogF("  Destination IP    %s\r\n", text);
+    }
+    else
+    {
+        Log ("IPv4 header ");
+        IpProtocolToString(protocol, sizeof(text), text);
+        Log(text);
+        Log(" ");
+        Ip4AddressToString(srcIp, sizeof(text), text);
+        Log(text);
+        Log(" >>> ");
+        Ip4AddressToString(dstIp, sizeof(text), text);
+        Log(text);
+        Log("\r\n");
+    }
 }
 static void (*pTraceBack)(void);
 static void trace()
@@ -193,10 +209,10 @@
     int action = DO_NOTHING;
     switch (protocol)
     {
-        case ICMP:   action = IcmpHandleReceivedPacket(trace, &srcIp, &dstIp, &dataLength, pData); break;
+        case ICMP:   action = Icmp4HandleReceivedPacket(trace, &srcIp, &dstIp, &dataLength, pData); break;
         case IGMP:                                                                                 break;
-        case UDP:    action = Udp4HandleReceivedPacket(trace, &srcIp, &dstIp, &dataLength, pData); break;
-        case TCP:    action = Tcp4HandleReceivedPacket(trace, &srcIp, &dstIp, &dataLength, pData); break;
+        case UDP:    action =  Udp4HandleReceivedPacket(trace, &srcIp, &dstIp, &dataLength, pData); break;
+        case TCP:    action =  Tcp4HandleReceivedPacket(trace, &srcIp, &dstIp, &dataLength, pData); break;
         case IP6IN4:                                                                               break;
         default:
             LogTimeF("IP4 received packet unknown protocol %d\r\n");