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
Diff: eth/eth.cpp
- Revision:
- 59:e0e556c8bd46
- Parent:
- 57:e0fb648acf48
--- a/eth/eth.cpp Thu Dec 07 20:44:32 2017 +0000 +++ b/eth/eth.cpp Thu Dec 14 20:55:40 2017 +0000 @@ -6,11 +6,11 @@ #include "arp.h" #include "ip4.h" #include "ip6.h" -#include "phy.h" +#include "link.h" #include "eth.h" #include "mac.h" -#define HEADER_SIZE 14 +#define MTU 1500 //header variables __packed struct header @@ -30,20 +30,6 @@ default: LogF("%04hX", prototype); break; } } - -static void finalisePacket(int dest, int dataLength, void* pPacket, int* pSize) -{ - if (!dest) return; - - struct header * pHeader = (header*)pPacket; - - MacMakeFromDest(dest, protocol, pHeader->dst); - - MacCopy(pHeader->src, MacLocal); //Put our MAC into the source - pHeader->typ = NetToHost16(protocol); - - *pSize = HEADER_SIZE + dataLength; -} void LogHeader(struct header* pHeader) { if (NetTraceVerbose) @@ -64,35 +50,38 @@ Log("\r\n"); } } -static void (*pTraceBack)(void); static void* tracePacket; static void trace() { - pTraceBack(); struct header * pHeader = (header*)tracePacket; LogHeader(pHeader); } -int EthHandlePacket(void (*traceback)(void), void* pPacket, int* pSize) +int EthHandlePacket(void* pPacketRx, int sizeRx, void* pPacketTx, int* pSizeTx) { - pTraceBack = traceback; - tracePacket = pPacket; - struct header * pHeader = (header*)pPacket; - int dataLength = *pSize - HEADER_SIZE; - void* pData = (char*)pPacket + HEADER_SIZE; + tracePacket = pPacketRx; + + struct header * pHeaderRx = (header*)pPacketRx; + struct header * pHeaderTx = (header*)pPacketTx; + void* pDataRx = (char*)pPacketRx + sizeof(header); + void* pDataTx = (char*)pPacketTx + sizeof(header); + int dataLengthRx = sizeRx - sizeof(header); + int dataLengthTx = *pSizeTx - sizeof(header); + if (dataLengthTx > MTU) dataLengthTx = MTU; //Limit the transmitted length to the maximum ethernet frame payload length - if (!MacAccept(pHeader->dst)) return DO_NOTHING; + if (!MacAccept(pHeaderRx->dst)) return DO_NOTHING; - protocol = NetToHost16(pHeader->typ); + protocol = NetToHost16(pHeaderRx->typ); if (protocol < 1500) return DO_NOTHING; //drop 802.3 messages - NetTraceHostCheckMac(pHeader->src); + NetTraceHostCheckMac(pHeaderRx->src); int action = DO_NOTHING; + char* macRemote = pHeaderRx->src; switch (protocol) { - case ARP: action = ArpHandleReceivedPacket(trace, pHeader->src, pData, &dataLength, pHeader->dst); break; - case IPV4: action = Ip4HandleReceivedPacket(trace, pHeader->src, pData, &dataLength, pHeader->dst); break; - case IPV6: action = Ip6HandleReceivedPacket(trace, pHeader->src, pData, &dataLength, pHeader->dst); break; + 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 0x6970: break; //Drop Sonos group membership packet case 0x7374: break; //Drop Sky Q packet case 0x7475: break; //Drop Sky Q packet @@ -104,22 +93,26 @@ break; } if (!action) return DO_NOTHING; + + MacMakeFromDest(ActionGetDestPart(action), protocol, macRemote); + MacCopy(pHeaderTx->src, MacLocal); + MacCopy(pHeaderTx->dst, macRemote); + pHeaderTx->typ = NetToHost16(protocol); - finalisePacket(ActionGetDestPart(action), dataLength, pPacket, pSize); + *pSizeTx = sizeof(header) + dataLengthTx; - if (ActionGetTracePart(action)) LogHeader(pHeader); + if (ActionGetTracePart(action)) LogHeader(pHeaderTx); return action; } int EthPollForPacketToSend(void* pPacket, int* pSize) { struct header * pHeader = (header*)pPacket; - void* pData = (char*)pPacket + HEADER_SIZE; + void* pData = (char*)pPacket + sizeof(header); int dataLength = 0; protocol = 0; int action = DO_NOTHING; - if (!action) { action = ArpPollForPacketToSend(pData, &dataLength); @@ -140,7 +133,11 @@ if (!action) return DO_NOTHING; - finalisePacket(ActionGetDestPart(action), dataLength, pPacket, pSize); + MacMakeFromDest(ActionGetDestPart(action), protocol, pHeader->dst); + MacCopy(pHeader->src, MacLocal); + pHeader->typ = NetToHost16(protocol); + + *pSize = sizeof(header) + dataLength; if (ActionGetTracePart(action)) LogHeader(pHeader);