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:
- 35:93c39d260a83
- Parent:
- 33:714a0345e59b
- Child:
- 36:900e24b27bfb
--- a/ip4/ip4.cpp Fri Sep 01 15:06:59 2017 +0000 +++ b/ip4/ip4.cpp Fri Sep 22 13:55:56 2017 +0000 @@ -4,7 +4,7 @@ #include "icmp.h" #include "udptcp4.h" #include "ar.h" -#include "dnscache.h" +#include "nr.h" #include "dhcp.h" #include "eth.h" #include "ip.h" @@ -82,11 +82,23 @@ static uint8_t protocol; static uint16_t checksum; static uint16_t calcsum; -uint32_t Ip4Src; -uint32_t Ip4Dst; +static uint32_t srcIp; +static uint32_t dstIp; 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; @@ -103,8 +115,8 @@ protocol = pHeader->protocol; checksum = NetToHost16(pHeader->checksum); calcsum = NetCheckSum(headerLength, pHeader); - Ip4Src = pHeader->src; - Ip4Dst = pHeader->dst; + srcIp = pHeader->src; + dstIp = pHeader->dst; pData = (char*)pHeader + headerLength; dataLength = totalLength - headerLength; } @@ -122,8 +134,8 @@ pHeader->ttl = ttl; pHeader->protocol = protocol; - pHeader->dst = Ip4Dst; - pHeader->src = Ip4Src; + pHeader->dst = dstIp; + pHeader->src = srcIp; pHeader->length = NetToHost16(headerLength + dataLength); pHeader->checksum = 0; pHeader->checksum = NetCheckSum(headerLength, pHeader); @@ -149,9 +161,9 @@ LogF(" Protocol %s\r\n", text); LogF(" Checksum (hex) %04hX\r\n", checksum); LogF(" Calculated (hex) %04hX\r\n", calcsum); - Ip4AddressToString(Ip4Src, sizeof(text), text); + Ip4AddressToString(srcIp, sizeof(text), text); LogF(" Source IP %s\r\n", text); - Ip4AddressToString(Ip4Dst, sizeof(text), text); + Ip4AddressToString(dstIp, sizeof(text), text); LogF(" Destination IP %s\r\n", text); } int Ip4HandleReceivedPacket(char* pSrcMac, void* pPacket, int* pSize, char* pDstMac) @@ -159,10 +171,10 @@ struct header * pHeader = (header*)pPacket; readHeader(pHeader); - bool isMe = Ip4Dst == DhcpLocalIp; - bool isLocalBroadcast = Ip4Dst == DhcpLocalIp | 0xFF000000; - bool isBroadcast = Ip4Dst == IP4_BROADCAST_ADDRESS; - bool isMulticast = (Ip4Dst & 0xE0) == 0xE0; //224.x.x.x == 1110 0000 == E0.xx.xx.xx == xx.xx.xx.E0 in little endian + 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 doIt = isMe || isLocalBroadcast || isBroadcast || isMulticast; if (!doIt) @@ -170,26 +182,26 @@ if (DEBUG) { char text[20]; - Ip4AddressToString(Ip4Dst, sizeof(text), text); + Ip4AddressToString(dstIp, sizeof(text), text); LogTimeF("IP4 filtered out ip %s ", text); - Ip4AddressToString(Ip4Src, sizeof(text), text); + Ip4AddressToString(srcIp, sizeof(text), text); LogF("from %s \r\n", text); } return DO_NOTHING; } - ArAdd4(pSrcMac, Ip4Src); - DnsCacheMakeRequestForNameFromIp4(Ip4Src); + ArAddIp4Record(pSrcMac, srcIp); + NrMakeRequestForNameFromIp4(srcIp); if (DEBUG) logHeader("IP4 packet received"); int action = DO_NOTHING; switch (protocol) { - case ICMP: action = IcmpHandleReceivedPacket(&Ip4Src, &Ip4Dst, &dataLength, pData); break; + case ICMP: action = IcmpHandleReceivedPacket(&srcIp, &dstIp, &dataLength, pData); break; case IGMP: return DO_NOTHING; - case UDP: action = Udp4HandleReceivedPacket(&Ip4Src, &Ip4Dst, &dataLength, pData); break; - case TCP: action = Tcp4HandleReceivedPacket(&Ip4Src, &Ip4Dst, &dataLength, pData); break; + case UDP: action = Udp4HandleReceivedPacket(&srcIp, &dstIp, &dataLength, pData); break; + case TCP: action = Tcp4HandleReceivedPacket(&srcIp, &dstIp, &dataLength, pData); break; case IP6IN4: return DO_NOTHING; default: logHeader("IP4 packet unhandled"); @@ -222,7 +234,7 @@ protocol = UDP; int action = DO_NOTHING; - if (!action) action = Udp4PollForPacketToSend(pData, &dataLength, &Ip4Src, &Ip4Dst); + if (!action) action = Udp4PollForPacketToSend(pData, &dataLength, &srcIp, &dstIp); if (!action) return DO_NOTHING; switch (action) { @@ -230,7 +242,7 @@ case UNICAST_DNS: case UNICAST_DHCP: case UNICAST_NTP: - ArRev4(Ip4Dst, pDstMac); //Make the remote MAC from ARP + ArIpToMac4(dstIp, pDstMac); //Make the remote MAC from ARP break; case BROADCAST: break;