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:
- 47:73af5c0b0dc2
- Parent:
- 44:83ce5ace337b
- Child:
- 57:e0fb648acf48
--- a/eth/eth.cpp Tue Oct 24 07:01:35 2017 +0000 +++ b/eth/eth.cpp Thu Oct 26 14:50:24 2017 +0000 @@ -20,25 +20,24 @@ uint16_t typ; }; static uint16_t protocol; - -void EthProtocolToString(uint16_t prototype, int size, char* text) +void EthProtocolLog(uint16_t prototype) { switch (prototype) { - case ARP: strncpy (text, "ARP" , size); break; - case IPV4: strncpy (text, "IPV4", size); break; - case IPV6: strncpy (text, "IPV6", size); break; - default: snprintf(text, size, "%04hX", prototype); break; + case ARP: Log("ARP"); break; + case IPV4: Log("IPV4"); break; + case IPV6: Log("IPV6"); break; + default: LogF("%04hX", prototype); break; } } -static void finalisePacket(int action, int dataLength, void* pPacket, int* pSize) +static void finalisePacket(int dest, int dataLength, void* pPacket, int* pSize) { - if (!action) return; + if (!dest) return; struct header * pHeader = (header*)pPacket; - MacMake(action, protocol, pHeader->dst); + MacMakeFromDest(dest, protocol, pHeader->dst); MacCopy(pHeader->src, MacLocal); //Put our MAC into the source pHeader->typ = NetToHost16(protocol); @@ -47,25 +46,21 @@ } void LogHeader(struct header* pHeader) { - char text[20]; if (NetTraceVerbose) { - Log ("ETH header\r\n"); - MacToString(pHeader->dst, sizeof(text), text); LogF(" Destination: %s\r\n", text); - MacToString(pHeader->src, sizeof(text), text); LogF(" Source: %s\r\n", text); - EthProtocolToString(NetToHost16(pHeader->typ), sizeof(text), text); LogF(" EtherType: %s\r\n", text); + Log("ETH header\r\n"); + Log(" Destination: "); MacLog(pHeader->dst); Log("\r\n"); + Log(" Source: "); MacLog(pHeader->src); Log("\r\n"); + Log(" EtherType: "); EthProtocolLog(NetToHost16(pHeader->typ)); Log("\r\n"); } else { Log("ETH header "); - EthProtocolToString(NetToHost16(pHeader->typ), sizeof(text), text); - Log(text); + EthProtocolLog(NetToHost16(pHeader->typ)); Log(" "); - MacToString(pHeader->src, sizeof(text), text); - Log(text); + MacLog(pHeader->src); Log(" >>> "); - MacToString(pHeader->dst, sizeof(text), text); - Log(text); + MacLog(pHeader->dst); Log("\r\n"); } } @@ -106,8 +101,9 @@ LogTimeF("ETH protocol %d not handled", protocol); break; } + if (!action) return DO_NOTHING; - finalisePacket(action, dataLength, pPacket, pSize); + finalisePacket(ActionGetDestPart(action), dataLength, pPacket, pSize); if (ActionGetTracePart(action)) LogHeader(pHeader); @@ -122,25 +118,27 @@ protocol = 0; int action = DO_NOTHING; - if (action == DO_NOTHING) + if (!action) { action = ArpPollForPacketToSend(pData, &dataLength); protocol = ARP; } - if (action == DO_NOTHING) + if (!action) { action = Ip6PollForPacketToSend(pData, &dataLength, pHeader->dst); protocol = IPV6; } - if (action == DO_NOTHING) + if (!action) { action = Ip4PollForPacketToSend(pData, &dataLength, pHeader->dst); protocol = IPV4; } - finalisePacket(action, dataLength, pPacket, pSize); + if (!action) return DO_NOTHING; + + finalisePacket(ActionGetDestPart(action), dataLength, pPacket, pSize); if (ActionGetTracePart(action)) LogHeader(pHeader);