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: ip6/ip6.cpp
- Revision:
- 59:e0e556c8bd46
- Parent:
- 57:e0fb648acf48
--- a/ip6/ip6.cpp Thu Dec 07 20:44:32 2017 +0000 +++ b/ip6/ip6.cpp Thu Dec 14 20:55:40 2017 +0000 @@ -18,8 +18,6 @@ bool Ip6Trace = true; - -#define HEADER_LENGTH 40 __packed struct header { uint32_t versionTrafficFlow; @@ -46,7 +44,7 @@ hoplimit = pHeader->hoplimit; Ip6AddressCopy(srcIp, pHeader->src); Ip6AddressCopy(dstIp, pHeader->dst); - pData = (char*)pHeader + HEADER_LENGTH; + pData = (char*)pHeader + sizeof(header); } static void writeHeader(struct header * pHeader) { @@ -120,12 +118,21 @@ pTraceBack(); logHeader(); } -int Ip6HandleReceivedPacket(void (*traceback)(void), char* pSrcMac, void* pPacket, int* pSize, char* pDstMac) +int Ip6HandleReceivedPacket(void (*traceback)(void), void* pPacketRx, int sizeRx, void* pPacketTx, int* pSizeTx, char* macRemote) { pTraceBack = traceback; - struct header * pHeader = (header*)pPacket; - readHeader(pHeader); + struct header* pHeaderRx = (header*)pPacketRx; + struct header* pHeaderTx = (header*)pPacketTx; + + char* pDataRx = (char*)pHeaderRx + sizeof(header); + char* pDataTx = (char*)pHeaderTx + sizeof(header); + + readHeader(pHeaderRx); //This also fetches the datalength out of the header + int dataLengthRx = dataLength; + if (dataLengthRx > sizeRx) dataLengthRx = sizeRx; //Do not exceed the buffer size + + int dataLengthTx = *pSizeTx; int scope = SlaacScope(dstIp); bool isMulticast = dstIp[0] == 0xFF; @@ -149,16 +156,16 @@ NetTraceHostCheckIp6(srcIp); - Ar6AddIpRecord(trace, pSrcMac, srcIp); + Ar6AddIpRecord(trace, macRemote, srcIp); Nr6MakeRequestForNameFromIp(srcIp); int action = DO_NOTHING; switch (protocol) { - 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; + case HOPOPT: action = DO_NOTHING; break; + case ICMP6: action = Icmp6HandleReceivedPacket(trace, scope, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, srcIp, dstIp); break; + case UDP: action = Udp6HandleReceivedPacket(trace, scope, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, srcIp, dstIp); break; + case TCP: action = Tcp6HandleReceivedPacket(trace, scope, pDataRx, dataLengthRx, pDataTx, &dataLengthTx, srcIp, dstIp); break; default: LogTimeF("IP6 protocol %d unhandled\r\n", protocol); return DO_NOTHING; @@ -167,19 +174,19 @@ if (NdpIpNeedsToBeRouted(dstIp)) { - MacCopy(pDstMac, NdpRouterMac); //Send to the router MAC + MacCopy(macRemote, NdpRouterMac); //Send to the router MAC hoplimit = NdpHopLimit; } else { - MacCopy(pDstMac, pSrcMac); //Send back to the source hoplimit = 255; } - + + dataLength = dataLengthTx; - writeHeader(pHeader); + writeHeader(pHeaderTx); - *pSize = HEADER_LENGTH + dataLength; + *pSizeTx = sizeof(header) + dataLength; if (ActionGetTracePart(action)) logHeader(); @@ -187,7 +194,7 @@ } int Ip6PollForPacketToSend(void* pPacket, int* pSize, char* pDstMac) { - pData = (char*)pPacket + HEADER_LENGTH; + pData = (char*)pPacket + sizeof(header); dataLength = 0; version = 6; @@ -238,7 +245,7 @@ writeHeader((header*)pPacket); - *pSize = HEADER_LENGTH + dataLength; + *pSize = sizeof(header) + dataLength; if (ActionGetTracePart(action)) logHeader();