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
ip4/ip4.cpp@50:492f2d2954e4, 2017-11-03 (annotated)
- Committer:
- andrewboyson
- Date:
- Fri Nov 03 16:19:15 2017 +0000
- Revision:
- 50:492f2d2954e4
- Parent:
- 49:1a6336f2b3f9
- Child:
- 53:77f8a49adf89
Split name resolution into NR4 and NR6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 43:bc028d5a6424 | 1 | #include "mbed.h" |
andrewboyson | 43:bc028d5a6424 | 2 | #include "log.h" |
andrewboyson | 43:bc028d5a6424 | 3 | #include "net.h" |
andrewboyson | 43:bc028d5a6424 | 4 | #include "action.h" |
andrewboyson | 43:bc028d5a6424 | 5 | #include "icmp4.h" |
andrewboyson | 10:f0854784e960 | 6 | #include "udptcp4.h" |
andrewboyson | 48:952dddb74b8b | 7 | #include "ar4.h" |
andrewboyson | 50:492f2d2954e4 | 8 | #include "nr4.h" |
andrewboyson | 43:bc028d5a6424 | 9 | #include "dhcp.h" |
andrewboyson | 43:bc028d5a6424 | 10 | #include "eth.h" |
andrewboyson | 43:bc028d5a6424 | 11 | #include "ip.h" |
andrewboyson | 49:1a6336f2b3f9 | 12 | #include "ip4addr.h" |
andrewboyson | 43:bc028d5a6424 | 13 | #include "ntp.h" |
andrewboyson | 43:bc028d5a6424 | 14 | #include "mac.h" |
andrewboyson | 10:f0854784e960 | 15 | |
andrewboyson | 48:952dddb74b8b | 16 | bool Ip4Trace = true; |
andrewboyson | 10:f0854784e960 | 17 | |
andrewboyson | 10:f0854784e960 | 18 | #define HEADER_LENGTH 20 |
andrewboyson | 10:f0854784e960 | 19 | |
andrewboyson | 10:f0854784e960 | 20 | __packed struct header |
andrewboyson | 10:f0854784e960 | 21 | { |
andrewboyson | 10:f0854784e960 | 22 | uint8_t versionIhl; |
andrewboyson | 10:f0854784e960 | 23 | uint8_t tos; |
andrewboyson | 10:f0854784e960 | 24 | uint16_t length; |
andrewboyson | 10:f0854784e960 | 25 | uint16_t id; |
andrewboyson | 10:f0854784e960 | 26 | uint16_t flagsOffset; |
andrewboyson | 10:f0854784e960 | 27 | uint8_t ttl; |
andrewboyson | 10:f0854784e960 | 28 | uint8_t protocol; |
andrewboyson | 10:f0854784e960 | 29 | uint16_t checksum; |
andrewboyson | 10:f0854784e960 | 30 | uint32_t src; |
andrewboyson | 10:f0854784e960 | 31 | uint32_t dst; |
andrewboyson | 10:f0854784e960 | 32 | }; |
andrewboyson | 10:f0854784e960 | 33 | |
andrewboyson | 10:f0854784e960 | 34 | //Header variables |
andrewboyson | 10:f0854784e960 | 35 | static uint8_t version; |
andrewboyson | 10:f0854784e960 | 36 | static int headerLength; |
andrewboyson | 10:f0854784e960 | 37 | static uint8_t tos; |
andrewboyson | 10:f0854784e960 | 38 | static uint16_t id; |
andrewboyson | 10:f0854784e960 | 39 | static bool dontFragment; |
andrewboyson | 10:f0854784e960 | 40 | static bool moreFragments; |
andrewboyson | 10:f0854784e960 | 41 | static uint16_t offset; |
andrewboyson | 10:f0854784e960 | 42 | static uint8_t ttl; |
andrewboyson | 10:f0854784e960 | 43 | static uint8_t protocol; |
andrewboyson | 10:f0854784e960 | 44 | static uint16_t checksum; |
andrewboyson | 10:f0854784e960 | 45 | static uint16_t calcsum; |
andrewboyson | 35:93c39d260a83 | 46 | static uint32_t srcIp; |
andrewboyson | 35:93c39d260a83 | 47 | static uint32_t dstIp; |
andrewboyson | 10:f0854784e960 | 48 | static void* pData; |
andrewboyson | 10:f0854784e960 | 49 | static int dataLength; |
andrewboyson | 10:f0854784e960 | 50 | |
andrewboyson | 10:f0854784e960 | 51 | void readHeader(struct header * pHeader) |
andrewboyson | 10:f0854784e960 | 52 | { |
andrewboyson | 10:f0854784e960 | 53 | version = pHeader->versionIhl >> 4; |
andrewboyson | 10:f0854784e960 | 54 | uint8_t ihl = pHeader->versionIhl & 0xF; |
andrewboyson | 10:f0854784e960 | 55 | headerLength = ihl * 4; |
andrewboyson | 10:f0854784e960 | 56 | tos = pHeader->tos; |
andrewboyson | 10:f0854784e960 | 57 | uint16_t totalLength = NetToHost16(pHeader->length); |
andrewboyson | 10:f0854784e960 | 58 | id = NetToHost16(pHeader->id); |
andrewboyson | 10:f0854784e960 | 59 | uint16_t flagsOffset = NetToHost16(pHeader->flagsOffset); |
andrewboyson | 10:f0854784e960 | 60 | dontFragment = flagsOffset & 0x4000; |
andrewboyson | 10:f0854784e960 | 61 | moreFragments = flagsOffset & 0x8000; |
andrewboyson | 10:f0854784e960 | 62 | offset = flagsOffset & 0x1FFF; |
andrewboyson | 10:f0854784e960 | 63 | ttl = pHeader->ttl; |
andrewboyson | 10:f0854784e960 | 64 | protocol = pHeader->protocol; |
andrewboyson | 10:f0854784e960 | 65 | checksum = NetToHost16(pHeader->checksum); |
andrewboyson | 10:f0854784e960 | 66 | calcsum = NetCheckSum(headerLength, pHeader); |
andrewboyson | 35:93c39d260a83 | 67 | srcIp = pHeader->src; |
andrewboyson | 35:93c39d260a83 | 68 | dstIp = pHeader->dst; |
andrewboyson | 10:f0854784e960 | 69 | pData = (char*)pHeader + headerLength; |
andrewboyson | 10:f0854784e960 | 70 | dataLength = totalLength - headerLength; |
andrewboyson | 10:f0854784e960 | 71 | } |
andrewboyson | 10:f0854784e960 | 72 | void writeHeader(struct header * pHeader) |
andrewboyson | 10:f0854784e960 | 73 | { |
andrewboyson | 10:f0854784e960 | 74 | uint16_t flagsOffset = offset; |
andrewboyson | 10:f0854784e960 | 75 | if (dontFragment) flagsOffset |= 0x4000; |
andrewboyson | 10:f0854784e960 | 76 | if (moreFragments) flagsOffset |= 0x8000; |
andrewboyson | 10:f0854784e960 | 77 | |
andrewboyson | 10:f0854784e960 | 78 | uint8_t ihl = headerLength >> 2; |
andrewboyson | 10:f0854784e960 | 79 | pHeader->versionIhl = (version << 4) + ihl; |
andrewboyson | 10:f0854784e960 | 80 | pHeader->tos = tos; |
andrewboyson | 10:f0854784e960 | 81 | pHeader->id = NetToHost16(id); |
andrewboyson | 10:f0854784e960 | 82 | pHeader->flagsOffset = NetToHost16(flagsOffset); |
andrewboyson | 10:f0854784e960 | 83 | pHeader->ttl = ttl; |
andrewboyson | 10:f0854784e960 | 84 | pHeader->protocol = protocol; |
andrewboyson | 10:f0854784e960 | 85 | |
andrewboyson | 35:93c39d260a83 | 86 | pHeader->dst = dstIp; |
andrewboyson | 35:93c39d260a83 | 87 | pHeader->src = srcIp; |
andrewboyson | 10:f0854784e960 | 88 | pHeader->length = NetToHost16(headerLength + dataLength); |
andrewboyson | 10:f0854784e960 | 89 | pHeader->checksum = 0; |
andrewboyson | 10:f0854784e960 | 90 | pHeader->checksum = NetCheckSum(headerLength, pHeader); |
andrewboyson | 10:f0854784e960 | 91 | calcsum = 0; |
andrewboyson | 10:f0854784e960 | 92 | } |
andrewboyson | 11:c051adb70c5a | 93 | |
andrewboyson | 37:793b39683406 | 94 | static void logHeader() |
andrewboyson | 11:c051adb70c5a | 95 | { |
andrewboyson | 43:bc028d5a6424 | 96 | if (NetTraceVerbose) |
andrewboyson | 43:bc028d5a6424 | 97 | { |
andrewboyson | 44:83ce5ace337b | 98 | Log ("IP4 header\r\n"); |
andrewboyson | 43:bc028d5a6424 | 99 | LogF(" Version %d\r\n", version); |
andrewboyson | 43:bc028d5a6424 | 100 | LogF(" Header length %d\r\n", headerLength); |
andrewboyson | 43:bc028d5a6424 | 101 | LogF(" Type of service %d\r\n", tos); |
andrewboyson | 43:bc028d5a6424 | 102 | LogF(" Data length %d\r\n", dataLength); |
andrewboyson | 43:bc028d5a6424 | 103 | LogF(" Identification %d\r\n", id); |
andrewboyson | 43:bc028d5a6424 | 104 | if (dontFragment) LogF(" Don't fragment\r\n"); |
andrewboyson | 43:bc028d5a6424 | 105 | else LogF(" Do fragment\r\n"); |
andrewboyson | 43:bc028d5a6424 | 106 | if (moreFragments) LogF(" More fragments\r\n"); |
andrewboyson | 43:bc028d5a6424 | 107 | else LogF(" No more fragments\r\n"); |
andrewboyson | 43:bc028d5a6424 | 108 | LogF(" Offset %d\r\n", offset); |
andrewboyson | 43:bc028d5a6424 | 109 | LogF(" Time to live %d\r\n", ttl); |
andrewboyson | 47:73af5c0b0dc2 | 110 | LogF(" Protocol "); IpProtocolLog(protocol); Log("\r\n"); |
andrewboyson | 43:bc028d5a6424 | 111 | LogF(" Checksum (hex) %04hX\r\n", checksum); |
andrewboyson | 43:bc028d5a6424 | 112 | LogF(" Calculated (hex) %04hX\r\n", calcsum); |
andrewboyson | 47:73af5c0b0dc2 | 113 | LogF(" Source IP "); Ip4AddressLog(srcIp); Log("\r\n"); |
andrewboyson | 47:73af5c0b0dc2 | 114 | LogF(" Destination IP "); Ip4AddressLog(dstIp); Log("\r\n"); |
andrewboyson | 43:bc028d5a6424 | 115 | } |
andrewboyson | 43:bc028d5a6424 | 116 | else |
andrewboyson | 43:bc028d5a6424 | 117 | { |
andrewboyson | 44:83ce5ace337b | 118 | Log ("IP4 header "); |
andrewboyson | 47:73af5c0b0dc2 | 119 | IpProtocolLog(protocol); |
andrewboyson | 43:bc028d5a6424 | 120 | Log(" "); |
andrewboyson | 47:73af5c0b0dc2 | 121 | Ip4AddressLog(srcIp); |
andrewboyson | 43:bc028d5a6424 | 122 | Log(" >>> "); |
andrewboyson | 47:73af5c0b0dc2 | 123 | Ip4AddressLog(dstIp); |
andrewboyson | 43:bc028d5a6424 | 124 | Log("\r\n"); |
andrewboyson | 43:bc028d5a6424 | 125 | } |
andrewboyson | 11:c051adb70c5a | 126 | } |
andrewboyson | 37:793b39683406 | 127 | static void (*pTraceBack)(void); |
andrewboyson | 37:793b39683406 | 128 | static void trace() |
andrewboyson | 10:f0854784e960 | 129 | { |
andrewboyson | 37:793b39683406 | 130 | pTraceBack(); |
andrewboyson | 37:793b39683406 | 131 | logHeader(); |
andrewboyson | 37:793b39683406 | 132 | } |
andrewboyson | 37:793b39683406 | 133 | int Ip4HandleReceivedPacket(void (*traceback)(void), char* pSrcMac, void* pPacket, int* pSize, char* pDstMac) |
andrewboyson | 37:793b39683406 | 134 | { |
andrewboyson | 37:793b39683406 | 135 | pTraceBack = traceback; |
andrewboyson | 10:f0854784e960 | 136 | struct header * pHeader = (header*)pPacket; |
andrewboyson | 10:f0854784e960 | 137 | readHeader(pHeader); |
andrewboyson | 10:f0854784e960 | 138 | |
andrewboyson | 37:793b39683406 | 139 | bool isMe = dstIp == DhcpLocalIp; |
andrewboyson | 35:93c39d260a83 | 140 | bool isLocalBroadcast = dstIp == DhcpLocalIp | 0xFF000000; |
andrewboyson | 37:793b39683406 | 141 | bool isBroadcast = dstIp == IP4_BROADCAST_ADDRESS; |
andrewboyson | 37:793b39683406 | 142 | bool isMulticast = (dstIp & 0xE0) == 0xE0; //224.x.x.x == 1110 0000 == E0.xx.xx.xx == xx.xx.xx.E0 in little endian |
andrewboyson | 10:f0854784e960 | 143 | |
andrewboyson | 15:6ca6778168b1 | 144 | bool doIt = isMe || isLocalBroadcast || isBroadcast || isMulticast; |
andrewboyson | 15:6ca6778168b1 | 145 | if (!doIt) |
andrewboyson | 15:6ca6778168b1 | 146 | { |
andrewboyson | 48:952dddb74b8b | 147 | if (Ip4Trace); |
andrewboyson | 15:6ca6778168b1 | 148 | { |
andrewboyson | 47:73af5c0b0dc2 | 149 | LogTimeF("IP4 filtered out ip "); Ip4AddressLog(dstIp); |
andrewboyson | 47:73af5c0b0dc2 | 150 | Log(" from "); |
andrewboyson | 47:73af5c0b0dc2 | 151 | Ip4AddressLog(srcIp); |
andrewboyson | 47:73af5c0b0dc2 | 152 | Log("\r\n"); |
andrewboyson | 15:6ca6778168b1 | 153 | } |
andrewboyson | 15:6ca6778168b1 | 154 | return DO_NOTHING; |
andrewboyson | 15:6ca6778168b1 | 155 | } |
andrewboyson | 10:f0854784e960 | 156 | |
andrewboyson | 50:492f2d2954e4 | 157 | if (srcIp) |
andrewboyson | 50:492f2d2954e4 | 158 | { |
andrewboyson | 50:492f2d2954e4 | 159 | Ar4AddIpRecord(trace, pSrcMac, srcIp); |
andrewboyson | 50:492f2d2954e4 | 160 | Nr4MakeRequestForNameFromIp(srcIp); |
andrewboyson | 50:492f2d2954e4 | 161 | } |
andrewboyson | 10:f0854784e960 | 162 | |
andrewboyson | 10:f0854784e960 | 163 | int action = DO_NOTHING; |
andrewboyson | 10:f0854784e960 | 164 | switch (protocol) |
andrewboyson | 10:f0854784e960 | 165 | { |
andrewboyson | 43:bc028d5a6424 | 166 | case ICMP: action = Icmp4HandleReceivedPacket(trace, &srcIp, &dstIp, &dataLength, pData); break; |
andrewboyson | 44:83ce5ace337b | 167 | case IGMP: break; |
andrewboyson | 43:bc028d5a6424 | 168 | case UDP: action = Udp4HandleReceivedPacket(trace, &srcIp, &dstIp, &dataLength, pData); break; |
andrewboyson | 43:bc028d5a6424 | 169 | case TCP: action = Tcp4HandleReceivedPacket(trace, &srcIp, &dstIp, &dataLength, pData); break; |
andrewboyson | 44:83ce5ace337b | 170 | case IP6IN4: break; |
andrewboyson | 10:f0854784e960 | 171 | default: |
andrewboyson | 37:793b39683406 | 172 | LogTimeF("IP4 received packet unknown protocol %d\r\n"); |
andrewboyson | 10:f0854784e960 | 173 | return DO_NOTHING; |
andrewboyson | 10:f0854784e960 | 174 | } |
andrewboyson | 11:c051adb70c5a | 175 | if (!action) return DO_NOTHING; |
andrewboyson | 10:f0854784e960 | 176 | |
andrewboyson | 48:952dddb74b8b | 177 | if (DhcpIpNeedsToBeRouted(dstIp)) Ar4IpToMac(DhcpRouter, pDstMac); //Send back to the router |
andrewboyson | 44:83ce5ace337b | 178 | else MacCopy(pDstMac, pSrcMac); //Send back to the source |
andrewboyson | 37:793b39683406 | 179 | |
andrewboyson | 10:f0854784e960 | 180 | writeHeader(pHeader); |
andrewboyson | 10:f0854784e960 | 181 | |
andrewboyson | 10:f0854784e960 | 182 | *pSize = headerLength + dataLength; |
andrewboyson | 10:f0854784e960 | 183 | |
andrewboyson | 37:793b39683406 | 184 | if (ActionGetTracePart(action)) logHeader(); |
andrewboyson | 37:793b39683406 | 185 | |
andrewboyson | 10:f0854784e960 | 186 | return action; |
andrewboyson | 10:f0854784e960 | 187 | } |
andrewboyson | 10:f0854784e960 | 188 | int Ip4PollForPacketToSend(void* pPacket, int* pSize, char* pDstMac) |
andrewboyson | 10:f0854784e960 | 189 | { |
andrewboyson | 11:c051adb70c5a | 190 | headerLength = HEADER_LENGTH; |
andrewboyson | 11:c051adb70c5a | 191 | pData = (char*)pPacket + headerLength; |
andrewboyson | 11:c051adb70c5a | 192 | dataLength = 0; |
andrewboyson | 10:f0854784e960 | 193 | version = 4; |
andrewboyson | 10:f0854784e960 | 194 | tos = 0; |
andrewboyson | 10:f0854784e960 | 195 | id = 0; |
andrewboyson | 10:f0854784e960 | 196 | dontFragment = true; |
andrewboyson | 10:f0854784e960 | 197 | moreFragments = false; |
andrewboyson | 10:f0854784e960 | 198 | offset = 0; |
andrewboyson | 10:f0854784e960 | 199 | ttl = 255; |
andrewboyson | 10:f0854784e960 | 200 | protocol = UDP; |
andrewboyson | 10:f0854784e960 | 201 | |
andrewboyson | 11:c051adb70c5a | 202 | int action = DO_NOTHING; |
andrewboyson | 35:93c39d260a83 | 203 | if (!action) action = Udp4PollForPacketToSend(pData, &dataLength, &srcIp, &dstIp); |
andrewboyson | 11:c051adb70c5a | 204 | if (!action) return DO_NOTHING; |
andrewboyson | 42:222a4f45f916 | 205 | int dest = ActionGetDestPart(action); |
andrewboyson | 42:222a4f45f916 | 206 | switch (dest) |
andrewboyson | 11:c051adb70c5a | 207 | { |
andrewboyson | 11:c051adb70c5a | 208 | case UNICAST: |
andrewboyson | 11:c051adb70c5a | 209 | case UNICAST_DNS: |
andrewboyson | 11:c051adb70c5a | 210 | case UNICAST_DHCP: |
andrewboyson | 22:914b970356f0 | 211 | case UNICAST_NTP: |
andrewboyson | 48:952dddb74b8b | 212 | if (DhcpIpNeedsToBeRouted(dstIp)) Ar4IpToMac(DhcpRouter, pDstMac); //send via router |
andrewboyson | 48:952dddb74b8b | 213 | else Ar4IpToMac(dstIp, pDstMac); //Send direct |
andrewboyson | 11:c051adb70c5a | 214 | break; |
andrewboyson | 42:222a4f45f916 | 215 | case MULTICAST_NODE: |
andrewboyson | 42:222a4f45f916 | 216 | case MULTICAST_ROUTER: |
andrewboyson | 42:222a4f45f916 | 217 | case MULTICAST_MDNS: |
andrewboyson | 42:222a4f45f916 | 218 | case MULTICAST_LLMNR: |
andrewboyson | 22:914b970356f0 | 219 | case BROADCAST: |
andrewboyson | 22:914b970356f0 | 220 | break; |
andrewboyson | 22:914b970356f0 | 221 | default: |
andrewboyson | 42:222a4f45f916 | 222 | LogTimeF("Ip4PollForPacketToSend - undefined destination %d\r\n", dest); |
andrewboyson | 22:914b970356f0 | 223 | break; |
andrewboyson | 11:c051adb70c5a | 224 | } |
andrewboyson | 10:f0854784e960 | 225 | |
andrewboyson | 11:c051adb70c5a | 226 | writeHeader((header*)pPacket); |
andrewboyson | 10:f0854784e960 | 227 | |
andrewboyson | 10:f0854784e960 | 228 | *pSize = headerLength + dataLength; |
andrewboyson | 10:f0854784e960 | 229 | |
andrewboyson | 37:793b39683406 | 230 | if (ActionGetTracePart(action)) logHeader(); |
andrewboyson | 37:793b39683406 | 231 | |
andrewboyson | 10:f0854784e960 | 232 | return action; |
andrewboyson | 10:f0854784e960 | 233 | } |