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:
- 35:93c39d260a83
- Parent:
- 34:e3a7bff69bfc
- Child:
- 36:900e24b27bfb
diff -r e3a7bff69bfc -r 93c39d260a83 ip6/ip6.cpp --- a/ip6/ip6.cpp Fri Sep 01 15:06:59 2017 +0000 +++ b/ip6/ip6.cpp Fri Sep 22 13:55:56 2017 +0000 @@ -4,13 +4,14 @@ #include "icmp6.h" #include "udptcp6.h" #include "ar.h" -#include "dnscache.h" +#include "nr.h" #include "slaac.h" #include "eth.h" #include "ip.h" #include "ip6.h" #include "ra.h" #include "io.h" +#include "ntp.h" #define DEBUG false #define SHOW_FILTERED true @@ -52,6 +53,18 @@ *p = 0; return p - pText; } +bool Ip6IsSame(char* ipA, char* ipB) +{ + return memcmp(ipA, ipB, 16) == 0; +} +bool Ip6IsEmpty(char* ip) +{ + return !*ip; //Just check for the first byte being non zero +} +void Ip6CopyToFrom(char* ipTo, char* ipFrom) +{ + memcpy(ipTo, ipFrom, 16); +} char Ip6AllNodes [] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; char Ip6AllRouters[] = {0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}; @@ -64,6 +77,7 @@ { case UNICAST: break; case UNICAST_DNS: memcpy(pDstIp, RaDnsServer, 16); break; + case UNICAST_NTP: memcpy(pDstIp, NtpServerIp6, 16); break; case MULTICAST_NODE: memcpy(pDstIp, Ip6AllNodes, 16); break; case MULTICAST_ROUTER: memcpy(pDstIp, Ip6AllRouters, 16); break; case MULTICAST_MDNS: memcpy(pDstIp, Ip6Mdns, 16); break; @@ -89,18 +103,31 @@ static int dataLength; static uint8_t protocol; static uint8_t hoplimit; -char Ip6Src[16]; -char Ip6Dst[16]; +static char srcIp[16]; +static char dstIp[16]; static void* pData; +void Ip6LogHeaderSrc() +{ + char text[64]; + Ip6AddressToString(srcIp, sizeof(text), text); + Log(text); +} +void Ip6LogHeaderDst() +{ + char text[64]; + Ip6AddressToString(dstIp, sizeof(text), text); + Log(text); +} + static void readHeader(struct header * pHeader) { version = (pHeader->versionTrafficFlow >> 4) & 0xF; dataLength = NetToHost16(pHeader->dataLength); protocol = pHeader->protocol; hoplimit = pHeader->hoplimit; - memcpy(Ip6Src, pHeader->src, 16); - memcpy(Ip6Dst, pHeader->dst, 16); + memcpy(srcIp, pHeader->src, 16); + memcpy(dstIp, pHeader->dst, 16); pData = (char*)pHeader + HEADER_LENGTH; } static void writeHeader(struct header * pHeader) @@ -108,8 +135,8 @@ pHeader->versionTrafficFlow = version << 4; pHeader->protocol = protocol; pHeader->hoplimit = 255; - memcpy(pHeader->dst, Ip6Dst, 16); - memcpy(pHeader->src, Ip6Src, 16); + memcpy(pHeader->dst, dstIp, 16); + memcpy(pHeader->src, srcIp, 16); pHeader->dataLength = NetToHost16(dataLength); } @@ -122,9 +149,9 @@ LogF(" Hop limit %d\r\n", hoplimit); IpProtocolToString(protocol, sizeof(text), text); LogF(" Protocol %s\r\n", text); - Ip6AddressToString(Ip6Src, sizeof(text), text); + Ip6AddressToString(srcIp, sizeof(text), text); LogF(" Source IP %s\r\n", text); - Ip6AddressToString(Ip6Dst, sizeof(text), text); + Ip6AddressToString(dstIp, sizeof(text), text); LogF(" Destination IP %s\r\n", text); } @@ -152,10 +179,6 @@ return true; } -static bool getIsSame(char* pA, char* pB) -{ - return memcmp(pA, pB, 16) == 0; -} static bool getIsSameGroup(char* pA, char* pB) { pA += 13; @@ -169,10 +192,10 @@ struct header * pHeader = (header*)pPacket; readHeader(pHeader); - bool isMe = getIsSame(Ip6Dst, SlaacLinkLocalIp) || getIsSame(Ip6Dst, SlaacGlobalIp); - bool isMulticast = Ip6Dst[0] == 0xFF; - bool isSolicited = getIsSolicited(Ip6Dst); - bool isGroup = getIsSameGroup(Ip6Dst, SlaacLinkLocalIp); + bool isMe = SlaacIsMe(dstIp); + bool isMulticast = dstIp[0] == 0xFF; + bool isSolicited = getIsSolicited(dstIp); + bool isGroup = getIsSameGroup(dstIp, SlaacLinkLocalIp); bool doIt = isMe || (isMulticast && !isSolicited) || (isGroup && isSolicited); @@ -181,26 +204,26 @@ if (DEBUG || SHOW_FILTERED) { char text[100]; - Ip6AddressToString(Ip6Dst, sizeof(text), text); + Ip6AddressToString(dstIp, sizeof(text), text); LogTimeF("IP6 filtered out ip %s ", text); - Ip6AddressToString(Ip6Src, sizeof(text), text); + Ip6AddressToString(srcIp, sizeof(text), text); LogF("from %s \r\n", text); } return DO_NOTHING; } - ArAdd6(pSrcMac, Ip6Src); - DnsCacheMakeRequestForNameFromIp6(Ip6Src); + ArAddIp6Record(pSrcMac, srcIp); + NrMakeRequestForNameFromIp6(srcIp); if (DEBUG) logHeader("IP6 packet received"); int action = DO_NOTHING; switch (protocol) { - case HOPOPT: action = DO_NOTHING; break; - case ICMP6: action = Icmp6HandleReceivedPacket(Ip6Src, Ip6Dst, &dataLength, pData); break; - case UDP: action = Udp6HandleReceivedPacket(Ip6Src, Ip6Dst, &dataLength, pData); break; - case TCP: action = Tcp6HandleReceivedPacket(Ip6Src, Ip6Dst, &dataLength, pData); break; + case HOPOPT: action = DO_NOTHING; break; + case ICMP6: action = Icmp6HandleReceivedPacket(srcIp, dstIp, &dataLength, pData); break; + case UDP: action = Udp6HandleReceivedPacket(srcIp, dstIp, &dataLength, pData); break; + case TCP: action = Tcp6HandleReceivedPacket(srcIp, dstIp, &dataLength, pData); break; default: logHeader("IP6 packet unhandled"); return DO_NOTHING; @@ -227,13 +250,13 @@ int action = DO_NOTHING; if (action == DO_NOTHING) { - action = Icmp6PollForPacketToSend(pData, &dataLength, Ip6Src, Ip6Dst); + action = Icmp6PollForPacketToSend(pData, &dataLength, srcIp, dstIp); protocol = ICMP6; } if (action == DO_NOTHING) { - action = Udp6PollForPacketToSend(pData, &dataLength, Ip6Src, Ip6Dst); + action = Udp6PollForPacketToSend(pData, &dataLength, srcIp, dstIp); protocol = UDP; } if (!action) return DO_NOTHING; @@ -243,7 +266,7 @@ case UNICAST_DNS: case UNICAST_DHCP: case UNICAST_NTP: - ArRev6(Ip6Dst, pDstMac); //Make the remote MAC from NP + ArIpToMac6(dstIp, pDstMac); //Make the remote MAC from NP break; case MULTICAST_NODE: case MULTICAST_ROUTER: