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:
172:9bc3c7b2cca1
Parent:
171:f708d6776752
Child:
195:bd5b123143ca
--- a/eth/eth.c	Sat Dec 12 20:10:02 2020 +0000
+++ b/eth/eth.c	Wed Dec 16 17:33:22 2020 +0000
@@ -12,6 +12,8 @@
 
 #define MTU 1500
 
+uint16_t EthProtocol; //Set when receiving or sending packets so that higher levels can read the protocol in use
+
 //header variables
 static char*    hdrDstPtr(char* pPacket) { return pPacket +  0; }
 static char*    hdrSrcPtr(char* pPacket) { return pPacket +  6; }
@@ -24,10 +26,10 @@
 {
     switch (protocol)
     {
-        case ARP:  Log("ARP");              break;
-        case IPV4: Log("IPV4");             break;
-        case IPV6: Log("IPV6");             break;
-        default:   LogF("%04hX", protocol); break;
+        case ETH_ARP:  Log("ARP");              break;
+        case ETH_IPV4: Log("IPV4");             break;
+        case ETH_IPV6: Log("IPV6");             break;
+        default:       LogF("%04hX", protocol); break;
     }
 }
 void LogHeader(char* pPacket)
@@ -71,8 +73,8 @@
         return DO_NOTHING;
     }
     
-    uint16_t protocol = hdrTypGet(pPacketRx);
-    if (protocol < 1500)
+    EthProtocol = hdrTypGet(pPacketRx);
+    if (EthProtocol < 1500)
     {
         RestartPoint = lastRestartPoint;
         return DO_NOTHING; //drop 802.3 messages
@@ -82,11 +84,11 @@
 
     int   action = DO_NOTHING;
     char* macRemote = hdrSrcPtr(pPacketRx);
-    switch (protocol)
+    switch (EthProtocol)
     {
-        case ARP:  action = ArpHandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx);            break;
-        case IPV4: action = Ip4HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, macRemote); break;
-        case IPV6: action = Ip6HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, macRemote); break;
+        case ETH_ARP:  action = ArpHandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx);            break;
+        case ETH_IPV4: action = Ip4HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, macRemote); break;
+        case ETH_IPV6: action = Ip6HandleReceivedPacket(trace, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, macRemote); break;
         case 0x6970: break; //Drop Sonos group membership packet
         case 0x7374: break; //Drop Sky Q packet
         case 0x7475: break; //Drop Sky Q packet
@@ -94,7 +96,7 @@
         case 0x8100: break; //Drop Sky Q VLAN 802.1Q packet
         case 0x887b: break; //Drop Sky Q packet
         default:
-            LogTimeF("ETH protocol %d not handled", protocol);
+            LogTimeF("ETH protocol %d not handled", EthProtocol);
             break;
     }
     if (!action)
@@ -103,10 +105,10 @@
         return DO_NOTHING;
     }
         
-    MacMakeFromDest(ActionGetDestPart(action), protocol, macRemote);
+    MacMakeFromDest(ActionGetDestPart(action), EthProtocol, macRemote);
     MacCopy(hdrSrcPtr(pPacketTx), MacLocal);
     MacCopy(hdrDstPtr(pPacketTx), macRemote);
-    hdrTypSet(pPacketTx, protocol);
+    hdrTypSet(pPacketTx, EthProtocol);
     
     *pSizeTx = HEADER_LENGTH + dataLengthTx;
     
@@ -122,30 +124,30 @@
     if (dataLength > MTU) dataLength = MTU; //Limit the transmitted length to the maximum ethernet frame payload length
     
     int action = DO_NOTHING;
-    uint16_t protocol = 0;
+    EthProtocol = 0;
     if (!action)
     {
+        EthProtocol = ETH_ARP;
         action = ArpPollForPacketToSend(pData, &dataLength);
-        protocol = ARP;
     }
 
     if (!action)
     {
+        EthProtocol = ETH_IPV6;
         action = Ip6PollForPacketToSend(pData, &dataLength, hdrDstPtr(pPacket));
-        protocol = IPV6;
     }
     
     if (!action)
     {
+        EthProtocol = ETH_IPV4;
         action = Ip4PollForPacketToSend(pData, &dataLength, hdrDstPtr(pPacket));
-        protocol = IPV4;
     }
     
     if (!action) return DO_NOTHING;
     
-    MacMakeFromDest(ActionGetDestPart(action), protocol, hdrDstPtr(pPacket));
+    MacMakeFromDest(ActionGetDestPart(action), EthProtocol, hdrDstPtr(pPacket));
     MacCopy(hdrSrcPtr(pPacket), MacLocal);
-    hdrTypSet(pPacket, protocol);
+    hdrTypSet(pPacket, EthProtocol);
     
     *pSize = HEADER_LENGTH + dataLength;