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: ip4/ip4.cpp
- Revision:
- 37:793b39683406
- Parent:
- 36:900e24b27bfb
- Child:
- 42:222a4f45f916
--- a/ip4/ip4.cpp Mon Sep 25 07:09:32 2017 +0000 +++ b/ip4/ip4.cpp Wed Oct 04 07:51:02 2017 +0000 @@ -1,18 +1,19 @@ -#include "mbed.h" -#include "log.h" -#include "net.h" -#include "icmp.h" +#include "mbed.h" +#include "log.h" +#include "net.h" +#include "action.h" +#include "icmp.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 DEBUG false +#define SHOW_FILTERED true #define IP4_BROADCAST_ADDRESS 0xFFFFFFFF #define IP4_MULTICAST_ALL_HOSTS 0x010000E0 @@ -36,9 +37,9 @@ return (ints[0] << 24) + (ints[1] << 16) + (ints[2] << 8) + ints[3]; } -void Ip4DestIpFromAction(int action, uint32_t* pDstIp) +void Ip4DestIpFromDest(int dest, uint32_t* pDstIp) { - switch (action) + switch (dest) { case UNICAST: break; case UNICAST_DNS: *pDstIp = DhcpDnsServer; break; @@ -50,7 +51,7 @@ case MULTICAST_LLMNR: *pDstIp = IP4_MULTICAST_LLMNR_ADDRESS; break; case BROADCAST: *pDstIp = IP4_BROADCAST_ADDRESS; break; default: - LogTimeF("Ip4 DestIpFromAction unknown action %d\r\n", action); + LogTimeF("Ip4DestIpFromDest unknown destination %d\r\n", dest); break; } } @@ -88,18 +89,6 @@ static void* pData; static int dataLength; -void Ip4LogHeaderSrc() -{ - char text[64]; - Ip4AddressToString(srcIp, sizeof(text), text); - Log(text); -} -void Ip4LogHeaderDst() -{ - char text[64]; - Ip4AddressToString(dstIp, sizeof(text), text); - Log(text); -} void readHeader(struct header * pHeader) { version = pHeader->versionIhl >> 4; @@ -143,10 +132,10 @@ calcsum = 0; } -static void logHeader(char* title) +static void logHeader() { char text[30]; - LogTimeF("%s\r\n", title); + 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); @@ -167,20 +156,27 @@ Ip4AddressToString(dstIp, sizeof(text), text); LogF(" Destination IP %s\r\n", text); } -int Ip4HandleReceivedPacket(char* pSrcMac, void* pPacket, int* pSize, char* pDstMac) +static void (*pTraceBack)(void); +static void trace() { + pTraceBack(); + logHeader(); +} +int Ip4HandleReceivedPacket(void (*traceback)(void), char* pSrcMac, void* pPacket, int* pSize, char* pDstMac) +{ + pTraceBack = traceback; struct header * pHeader = (header*)pPacket; readHeader(pHeader); - bool isMe = dstIp == DhcpLocalIp; + bool isMe = dstIp == DhcpLocalIp; bool isLocalBroadcast = dstIp == DhcpLocalIp | 0xFF000000; - bool isBroadcast = dstIp == IP4_BROADCAST_ADDRESS; - bool isMulticast = (dstIp & 0xE0) == 0xE0; //224.x.x.x == 1110 0000 == E0.xx.xx.xx == xx.xx.xx.E0 in little endian + bool isBroadcast = dstIp == IP4_BROADCAST_ADDRESS; + bool isMulticast = (dstIp & 0xE0) == 0xE0; //224.x.x.x == 1110 0000 == E0.xx.xx.xx == xx.xx.xx.E0 in little endian bool doIt = isMe || isLocalBroadcast || isBroadcast || isMulticast; if (!doIt) { - if (DEBUG) + if (SHOW_FILTERED) { char text[20]; Ip4AddressToString(dstIp, sizeof(text), text); @@ -193,31 +189,29 @@ ArAddIp4Record(pSrcMac, srcIp); NrMakeRequestForNameFromIp4(srcIp); - - if (DEBUG) logHeader("IP4 packet received"); int action = DO_NOTHING; switch (protocol) { - case ICMP: action = IcmpHandleReceivedPacket(&srcIp, &dstIp, &dataLength, pData); break; - case IGMP: return DO_NOTHING; - case UDP: action = Udp4HandleReceivedPacket(&srcIp, &dstIp, &dataLength, pData); break; - case TCP: action = Tcp4HandleReceivedPacket(&srcIp, &dstIp, &dataLength, pData); break; - case IP6IN4: return DO_NOTHING; + case ICMP: action = IcmpHandleReceivedPacket(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 IP6IN4: break; default: - logHeader("IP4 packet unhandled"); + LogTimeF("IP4 received packet unknown protocol %d\r\n"); return DO_NOTHING; } if (!action) return DO_NOTHING; MacCopy(pDstMac, pSrcMac); - - if (DEBUG) logHeader("IP4 packet replied to"); - + writeHeader(pHeader); *pSize = headerLength + dataLength; + if (ActionGetTracePart(action)) logHeader(); + return action; } int Ip4PollForPacketToSend(void* pPacket, int* pSize, char* pDstMac) @@ -252,12 +246,11 @@ break; } - - if (DEBUG) logHeader("IP4 polled packet sent"); - writeHeader((header*)pPacket); *pSize = headerLength + dataLength; + if (ActionGetTracePart(action)) logHeader(); + return action; }