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@42:222a4f45f916, 2017-10-15 (annotated)
- Committer:
- andrewboyson
- Date:
- Sun Oct 15 17:54:09 2017 +0000
- Revision:
- 42:222a4f45f916
- Parent:
- 37:793b39683406
- Child:
- 43:bc028d5a6424
Made the use of IPv4 or IPv6 for client requests a configurable option
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 37:793b39683406 | 1 | #include "mbed.h" |
andrewboyson | 37:793b39683406 | 2 | #include "log.h" |
andrewboyson | 37:793b39683406 | 3 | #include "net.h" |
andrewboyson | 37:793b39683406 | 4 | #include "action.h" |
andrewboyson | 37:793b39683406 | 5 | #include "icmp.h" |
andrewboyson | 10:f0854784e960 | 6 | #include "udptcp4.h" |
andrewboyson | 37:793b39683406 | 7 | #include "ar.h" |
andrewboyson | 37:793b39683406 | 8 | #include "nr.h" |
andrewboyson | 37:793b39683406 | 9 | #include "dhcp.h" |
andrewboyson | 37:793b39683406 | 10 | #include "eth.h" |
andrewboyson | 37:793b39683406 | 11 | #include "ip.h" |
andrewboyson | 37:793b39683406 | 12 | #include "ip4.h" |
andrewboyson | 37:793b39683406 | 13 | #include "ntp.h" |
andrewboyson | 37:793b39683406 | 14 | #include "mac.h" |
andrewboyson | 10:f0854784e960 | 15 | |
andrewboyson | 37:793b39683406 | 16 | #define SHOW_FILTERED true |
andrewboyson | 10:f0854784e960 | 17 | |
andrewboyson | 11:c051adb70c5a | 18 | #define IP4_BROADCAST_ADDRESS 0xFFFFFFFF |
andrewboyson | 11:c051adb70c5a | 19 | #define IP4_MULTICAST_ALL_HOSTS 0x010000E0 |
andrewboyson | 11:c051adb70c5a | 20 | #define IP4_MULTICAST_ALL_ROUTERS 0x020000E0 |
andrewboyson | 11:c051adb70c5a | 21 | #define IP4_MULTICAST_DNS_ADDRESS 0xFB0000E0 |
andrewboyson | 11:c051adb70c5a | 22 | #define IP4_MULTICAST_LLMNR_ADDRESS 0xFC0000E0 |
andrewboyson | 11:c051adb70c5a | 23 | |
andrewboyson | 14:e75a59c1123d | 24 | int Ip4AddressToString(uint32_t ip, int size, char* text) |
andrewboyson | 14:e75a59c1123d | 25 | { |
andrewboyson | 14:e75a59c1123d | 26 | int a0 = (ip & 0xFF000000) >> 24; |
andrewboyson | 14:e75a59c1123d | 27 | int a1 = (ip & 0x00FF0000) >> 16; |
andrewboyson | 14:e75a59c1123d | 28 | int a2 = (ip & 0x0000FF00) >> 8; |
andrewboyson | 14:e75a59c1123d | 29 | int a3 = (ip & 0x000000FF); |
andrewboyson | 14:e75a59c1123d | 30 | return snprintf(text, size, "%d.%d.%d.%d", a3, a2, a1, a0); |
andrewboyson | 14:e75a59c1123d | 31 | } |
andrewboyson | 14:e75a59c1123d | 32 | |
andrewboyson | 18:accfcb80d9c3 | 33 | uint32_t Ip4Parse(char* text) |
andrewboyson | 18:accfcb80d9c3 | 34 | { |
andrewboyson | 18:accfcb80d9c3 | 35 | int ints[4]; |
andrewboyson | 18:accfcb80d9c3 | 36 | sscanf(text, "%d.%d.%d.%d", &ints[3], &ints[2], &ints[1], &ints[0]); |
andrewboyson | 18:accfcb80d9c3 | 37 | return (ints[0] << 24) + (ints[1] << 16) + (ints[2] << 8) + ints[3]; |
andrewboyson | 18:accfcb80d9c3 | 38 | } |
andrewboyson | 18:accfcb80d9c3 | 39 | |
andrewboyson | 37:793b39683406 | 40 | void Ip4DestIpFromDest(int dest, uint32_t* pDstIp) |
andrewboyson | 11:c051adb70c5a | 41 | { |
andrewboyson | 37:793b39683406 | 42 | switch (dest) |
andrewboyson | 11:c051adb70c5a | 43 | { |
andrewboyson | 11:c051adb70c5a | 44 | case UNICAST: break; |
andrewboyson | 11:c051adb70c5a | 45 | case UNICAST_DNS: *pDstIp = DhcpDnsServer; break; |
andrewboyson | 11:c051adb70c5a | 46 | case UNICAST_DHCP: *pDstIp = DhcpServer; break; |
andrewboyson | 22:914b970356f0 | 47 | case UNICAST_NTP: *pDstIp = NtpServerIp4; break; |
andrewboyson | 11:c051adb70c5a | 48 | case MULTICAST_NODE: *pDstIp = IP4_MULTICAST_ALL_HOSTS; break; |
andrewboyson | 11:c051adb70c5a | 49 | case MULTICAST_ROUTER: *pDstIp = IP4_MULTICAST_ALL_ROUTERS; break; |
andrewboyson | 11:c051adb70c5a | 50 | case MULTICAST_MDNS: *pDstIp = IP4_MULTICAST_DNS_ADDRESS; break; |
andrewboyson | 11:c051adb70c5a | 51 | case MULTICAST_LLMNR: *pDstIp = IP4_MULTICAST_LLMNR_ADDRESS; break; |
andrewboyson | 11:c051adb70c5a | 52 | case BROADCAST: *pDstIp = IP4_BROADCAST_ADDRESS; break; |
andrewboyson | 11:c051adb70c5a | 53 | default: |
andrewboyson | 37:793b39683406 | 54 | LogTimeF("Ip4DestIpFromDest unknown destination %d\r\n", dest); |
andrewboyson | 11:c051adb70c5a | 55 | break; |
andrewboyson | 11:c051adb70c5a | 56 | } |
andrewboyson | 11:c051adb70c5a | 57 | } |
andrewboyson | 11:c051adb70c5a | 58 | |
andrewboyson | 10:f0854784e960 | 59 | #define HEADER_LENGTH 20 |
andrewboyson | 10:f0854784e960 | 60 | |
andrewboyson | 10:f0854784e960 | 61 | __packed struct header |
andrewboyson | 10:f0854784e960 | 62 | { |
andrewboyson | 10:f0854784e960 | 63 | uint8_t versionIhl; |
andrewboyson | 10:f0854784e960 | 64 | uint8_t tos; |
andrewboyson | 10:f0854784e960 | 65 | uint16_t length; |
andrewboyson | 10:f0854784e960 | 66 | uint16_t id; |
andrewboyson | 10:f0854784e960 | 67 | uint16_t flagsOffset; |
andrewboyson | 10:f0854784e960 | 68 | uint8_t ttl; |
andrewboyson | 10:f0854784e960 | 69 | uint8_t protocol; |
andrewboyson | 10:f0854784e960 | 70 | uint16_t checksum; |
andrewboyson | 10:f0854784e960 | 71 | uint32_t src; |
andrewboyson | 10:f0854784e960 | 72 | uint32_t dst; |
andrewboyson | 10:f0854784e960 | 73 | }; |
andrewboyson | 10:f0854784e960 | 74 | |
andrewboyson | 10:f0854784e960 | 75 | //Header variables |
andrewboyson | 10:f0854784e960 | 76 | static uint8_t version; |
andrewboyson | 10:f0854784e960 | 77 | static int headerLength; |
andrewboyson | 10:f0854784e960 | 78 | static uint8_t tos; |
andrewboyson | 10:f0854784e960 | 79 | static uint16_t id; |
andrewboyson | 10:f0854784e960 | 80 | static bool dontFragment; |
andrewboyson | 10:f0854784e960 | 81 | static bool moreFragments; |
andrewboyson | 10:f0854784e960 | 82 | static uint16_t offset; |
andrewboyson | 10:f0854784e960 | 83 | static uint8_t ttl; |
andrewboyson | 10:f0854784e960 | 84 | static uint8_t protocol; |
andrewboyson | 10:f0854784e960 | 85 | static uint16_t checksum; |
andrewboyson | 10:f0854784e960 | 86 | static uint16_t calcsum; |
andrewboyson | 35:93c39d260a83 | 87 | static uint32_t srcIp; |
andrewboyson | 35:93c39d260a83 | 88 | static uint32_t dstIp; |
andrewboyson | 10:f0854784e960 | 89 | static void* pData; |
andrewboyson | 10:f0854784e960 | 90 | static int dataLength; |
andrewboyson | 10:f0854784e960 | 91 | |
andrewboyson | 10:f0854784e960 | 92 | void readHeader(struct header * pHeader) |
andrewboyson | 10:f0854784e960 | 93 | { |
andrewboyson | 10:f0854784e960 | 94 | version = pHeader->versionIhl >> 4; |
andrewboyson | 10:f0854784e960 | 95 | uint8_t ihl = pHeader->versionIhl & 0xF; |
andrewboyson | 10:f0854784e960 | 96 | headerLength = ihl * 4; |
andrewboyson | 10:f0854784e960 | 97 | tos = pHeader->tos; |
andrewboyson | 10:f0854784e960 | 98 | uint16_t totalLength = NetToHost16(pHeader->length); |
andrewboyson | 10:f0854784e960 | 99 | id = NetToHost16(pHeader->id); |
andrewboyson | 10:f0854784e960 | 100 | uint16_t flagsOffset = NetToHost16(pHeader->flagsOffset); |
andrewboyson | 10:f0854784e960 | 101 | dontFragment = flagsOffset & 0x4000; |
andrewboyson | 10:f0854784e960 | 102 | moreFragments = flagsOffset & 0x8000; |
andrewboyson | 10:f0854784e960 | 103 | offset = flagsOffset & 0x1FFF; |
andrewboyson | 10:f0854784e960 | 104 | ttl = pHeader->ttl; |
andrewboyson | 10:f0854784e960 | 105 | protocol = pHeader->protocol; |
andrewboyson | 10:f0854784e960 | 106 | checksum = NetToHost16(pHeader->checksum); |
andrewboyson | 10:f0854784e960 | 107 | calcsum = NetCheckSum(headerLength, pHeader); |
andrewboyson | 35:93c39d260a83 | 108 | srcIp = pHeader->src; |
andrewboyson | 35:93c39d260a83 | 109 | dstIp = pHeader->dst; |
andrewboyson | 10:f0854784e960 | 110 | pData = (char*)pHeader + headerLength; |
andrewboyson | 10:f0854784e960 | 111 | dataLength = totalLength - headerLength; |
andrewboyson | 10:f0854784e960 | 112 | } |
andrewboyson | 10:f0854784e960 | 113 | void writeHeader(struct header * pHeader) |
andrewboyson | 10:f0854784e960 | 114 | { |
andrewboyson | 10:f0854784e960 | 115 | uint16_t flagsOffset = offset; |
andrewboyson | 10:f0854784e960 | 116 | if (dontFragment) flagsOffset |= 0x4000; |
andrewboyson | 10:f0854784e960 | 117 | if (moreFragments) flagsOffset |= 0x8000; |
andrewboyson | 10:f0854784e960 | 118 | |
andrewboyson | 10:f0854784e960 | 119 | uint8_t ihl = headerLength >> 2; |
andrewboyson | 10:f0854784e960 | 120 | pHeader->versionIhl = (version << 4) + ihl; |
andrewboyson | 10:f0854784e960 | 121 | pHeader->tos = tos; |
andrewboyson | 10:f0854784e960 | 122 | pHeader->id = NetToHost16(id); |
andrewboyson | 10:f0854784e960 | 123 | pHeader->flagsOffset = NetToHost16(flagsOffset); |
andrewboyson | 10:f0854784e960 | 124 | pHeader->ttl = ttl; |
andrewboyson | 10:f0854784e960 | 125 | pHeader->protocol = protocol; |
andrewboyson | 10:f0854784e960 | 126 | |
andrewboyson | 35:93c39d260a83 | 127 | pHeader->dst = dstIp; |
andrewboyson | 35:93c39d260a83 | 128 | pHeader->src = srcIp; |
andrewboyson | 10:f0854784e960 | 129 | pHeader->length = NetToHost16(headerLength + dataLength); |
andrewboyson | 10:f0854784e960 | 130 | pHeader->checksum = 0; |
andrewboyson | 10:f0854784e960 | 131 | pHeader->checksum = NetCheckSum(headerLength, pHeader); |
andrewboyson | 10:f0854784e960 | 132 | calcsum = 0; |
andrewboyson | 10:f0854784e960 | 133 | } |
andrewboyson | 11:c051adb70c5a | 134 | |
andrewboyson | 37:793b39683406 | 135 | static void logHeader() |
andrewboyson | 11:c051adb70c5a | 136 | { |
andrewboyson | 11:c051adb70c5a | 137 | char text[30]; |
andrewboyson | 37:793b39683406 | 138 | Log ("IPv4 header\r\n"); |
andrewboyson | 11:c051adb70c5a | 139 | LogF(" Version %d\r\n", version); |
andrewboyson | 11:c051adb70c5a | 140 | LogF(" Header length %d\r\n", headerLength); |
andrewboyson | 11:c051adb70c5a | 141 | LogF(" Type of service %d\r\n", tos); |
andrewboyson | 11:c051adb70c5a | 142 | LogF(" Data length %d\r\n", dataLength); |
andrewboyson | 11:c051adb70c5a | 143 | LogF(" Identification %d\r\n", id); |
andrewboyson | 11:c051adb70c5a | 144 | if (dontFragment) LogF(" Don't fragment\r\n"); |
andrewboyson | 11:c051adb70c5a | 145 | else LogF(" Do fragment\r\n"); |
andrewboyson | 11:c051adb70c5a | 146 | if (moreFragments) LogF(" More fragments\r\n"); |
andrewboyson | 11:c051adb70c5a | 147 | else LogF(" No more fragments\r\n"); |
andrewboyson | 11:c051adb70c5a | 148 | LogF(" Offset %d\r\n", offset); |
andrewboyson | 11:c051adb70c5a | 149 | LogF(" Time to live %d\r\n", ttl); |
andrewboyson | 14:e75a59c1123d | 150 | IpProtocolToString(protocol, sizeof(text), text); |
andrewboyson | 11:c051adb70c5a | 151 | LogF(" Protocol %s\r\n", text); |
andrewboyson | 11:c051adb70c5a | 152 | LogF(" Checksum (hex) %04hX\r\n", checksum); |
andrewboyson | 11:c051adb70c5a | 153 | LogF(" Calculated (hex) %04hX\r\n", calcsum); |
andrewboyson | 35:93c39d260a83 | 154 | Ip4AddressToString(srcIp, sizeof(text), text); |
andrewboyson | 11:c051adb70c5a | 155 | LogF(" Source IP %s\r\n", text); |
andrewboyson | 35:93c39d260a83 | 156 | Ip4AddressToString(dstIp, sizeof(text), text); |
andrewboyson | 11:c051adb70c5a | 157 | LogF(" Destination IP %s\r\n", text); |
andrewboyson | 11:c051adb70c5a | 158 | } |
andrewboyson | 37:793b39683406 | 159 | static void (*pTraceBack)(void); |
andrewboyson | 37:793b39683406 | 160 | static void trace() |
andrewboyson | 10:f0854784e960 | 161 | { |
andrewboyson | 37:793b39683406 | 162 | pTraceBack(); |
andrewboyson | 37:793b39683406 | 163 | logHeader(); |
andrewboyson | 37:793b39683406 | 164 | } |
andrewboyson | 37:793b39683406 | 165 | int Ip4HandleReceivedPacket(void (*traceback)(void), char* pSrcMac, void* pPacket, int* pSize, char* pDstMac) |
andrewboyson | 37:793b39683406 | 166 | { |
andrewboyson | 37:793b39683406 | 167 | pTraceBack = traceback; |
andrewboyson | 10:f0854784e960 | 168 | struct header * pHeader = (header*)pPacket; |
andrewboyson | 10:f0854784e960 | 169 | readHeader(pHeader); |
andrewboyson | 10:f0854784e960 | 170 | |
andrewboyson | 37:793b39683406 | 171 | bool isMe = dstIp == DhcpLocalIp; |
andrewboyson | 35:93c39d260a83 | 172 | bool isLocalBroadcast = dstIp == DhcpLocalIp | 0xFF000000; |
andrewboyson | 37:793b39683406 | 173 | bool isBroadcast = dstIp == IP4_BROADCAST_ADDRESS; |
andrewboyson | 37:793b39683406 | 174 | 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 | 175 | |
andrewboyson | 15:6ca6778168b1 | 176 | bool doIt = isMe || isLocalBroadcast || isBroadcast || isMulticast; |
andrewboyson | 15:6ca6778168b1 | 177 | if (!doIt) |
andrewboyson | 15:6ca6778168b1 | 178 | { |
andrewboyson | 37:793b39683406 | 179 | if (SHOW_FILTERED) |
andrewboyson | 15:6ca6778168b1 | 180 | { |
andrewboyson | 15:6ca6778168b1 | 181 | char text[20]; |
andrewboyson | 35:93c39d260a83 | 182 | Ip4AddressToString(dstIp, sizeof(text), text); |
andrewboyson | 15:6ca6778168b1 | 183 | LogTimeF("IP4 filtered out ip %s ", text); |
andrewboyson | 35:93c39d260a83 | 184 | Ip4AddressToString(srcIp, sizeof(text), text); |
andrewboyson | 15:6ca6778168b1 | 185 | LogF("from %s \r\n", text); |
andrewboyson | 15:6ca6778168b1 | 186 | } |
andrewboyson | 15:6ca6778168b1 | 187 | return DO_NOTHING; |
andrewboyson | 15:6ca6778168b1 | 188 | } |
andrewboyson | 10:f0854784e960 | 189 | |
andrewboyson | 35:93c39d260a83 | 190 | ArAddIp4Record(pSrcMac, srcIp); |
andrewboyson | 35:93c39d260a83 | 191 | NrMakeRequestForNameFromIp4(srcIp); |
andrewboyson | 10:f0854784e960 | 192 | |
andrewboyson | 10:f0854784e960 | 193 | int action = DO_NOTHING; |
andrewboyson | 10:f0854784e960 | 194 | switch (protocol) |
andrewboyson | 10:f0854784e960 | 195 | { |
andrewboyson | 37:793b39683406 | 196 | case ICMP: action = IcmpHandleReceivedPacket(trace, &srcIp, &dstIp, &dataLength, pData); break; |
andrewboyson | 37:793b39683406 | 197 | case IGMP: break; |
andrewboyson | 37:793b39683406 | 198 | case UDP: action = Udp4HandleReceivedPacket(trace, &srcIp, &dstIp, &dataLength, pData); break; |
andrewboyson | 37:793b39683406 | 199 | case TCP: action = Tcp4HandleReceivedPacket(trace, &srcIp, &dstIp, &dataLength, pData); break; |
andrewboyson | 37:793b39683406 | 200 | case IP6IN4: break; |
andrewboyson | 10:f0854784e960 | 201 | default: |
andrewboyson | 37:793b39683406 | 202 | LogTimeF("IP4 received packet unknown protocol %d\r\n"); |
andrewboyson | 10:f0854784e960 | 203 | return DO_NOTHING; |
andrewboyson | 10:f0854784e960 | 204 | } |
andrewboyson | 11:c051adb70c5a | 205 | if (!action) return DO_NOTHING; |
andrewboyson | 10:f0854784e960 | 206 | |
andrewboyson | 36:900e24b27bfb | 207 | MacCopy(pDstMac, pSrcMac); |
andrewboyson | 37:793b39683406 | 208 | |
andrewboyson | 10:f0854784e960 | 209 | writeHeader(pHeader); |
andrewboyson | 10:f0854784e960 | 210 | |
andrewboyson | 10:f0854784e960 | 211 | *pSize = headerLength + dataLength; |
andrewboyson | 10:f0854784e960 | 212 | |
andrewboyson | 37:793b39683406 | 213 | if (ActionGetTracePart(action)) logHeader(); |
andrewboyson | 37:793b39683406 | 214 | |
andrewboyson | 10:f0854784e960 | 215 | return action; |
andrewboyson | 10:f0854784e960 | 216 | } |
andrewboyson | 10:f0854784e960 | 217 | int Ip4PollForPacketToSend(void* pPacket, int* pSize, char* pDstMac) |
andrewboyson | 10:f0854784e960 | 218 | { |
andrewboyson | 11:c051adb70c5a | 219 | headerLength = HEADER_LENGTH; |
andrewboyson | 11:c051adb70c5a | 220 | pData = (char*)pPacket + headerLength; |
andrewboyson | 11:c051adb70c5a | 221 | dataLength = 0; |
andrewboyson | 10:f0854784e960 | 222 | version = 4; |
andrewboyson | 10:f0854784e960 | 223 | tos = 0; |
andrewboyson | 10:f0854784e960 | 224 | id = 0; |
andrewboyson | 10:f0854784e960 | 225 | dontFragment = true; |
andrewboyson | 10:f0854784e960 | 226 | moreFragments = false; |
andrewboyson | 10:f0854784e960 | 227 | offset = 0; |
andrewboyson | 10:f0854784e960 | 228 | ttl = 255; |
andrewboyson | 10:f0854784e960 | 229 | protocol = UDP; |
andrewboyson | 10:f0854784e960 | 230 | |
andrewboyson | 11:c051adb70c5a | 231 | int action = DO_NOTHING; |
andrewboyson | 35:93c39d260a83 | 232 | if (!action) action = Udp4PollForPacketToSend(pData, &dataLength, &srcIp, &dstIp); |
andrewboyson | 11:c051adb70c5a | 233 | if (!action) return DO_NOTHING; |
andrewboyson | 42:222a4f45f916 | 234 | int dest = ActionGetDestPart(action); |
andrewboyson | 42:222a4f45f916 | 235 | switch (dest) |
andrewboyson | 11:c051adb70c5a | 236 | { |
andrewboyson | 11:c051adb70c5a | 237 | case UNICAST: |
andrewboyson | 11:c051adb70c5a | 238 | case UNICAST_DNS: |
andrewboyson | 11:c051adb70c5a | 239 | case UNICAST_DHCP: |
andrewboyson | 22:914b970356f0 | 240 | case UNICAST_NTP: |
andrewboyson | 35:93c39d260a83 | 241 | ArIpToMac4(dstIp, pDstMac); //Make the remote MAC from ARP |
andrewboyson | 11:c051adb70c5a | 242 | break; |
andrewboyson | 42:222a4f45f916 | 243 | case MULTICAST_NODE: |
andrewboyson | 42:222a4f45f916 | 244 | case MULTICAST_ROUTER: |
andrewboyson | 42:222a4f45f916 | 245 | case MULTICAST_MDNS: |
andrewboyson | 42:222a4f45f916 | 246 | case MULTICAST_LLMNR: |
andrewboyson | 22:914b970356f0 | 247 | case BROADCAST: |
andrewboyson | 22:914b970356f0 | 248 | break; |
andrewboyson | 22:914b970356f0 | 249 | default: |
andrewboyson | 42:222a4f45f916 | 250 | LogTimeF("Ip4PollForPacketToSend - undefined destination %d\r\n", dest); |
andrewboyson | 22:914b970356f0 | 251 | break; |
andrewboyson | 11:c051adb70c5a | 252 | } |
andrewboyson | 10:f0854784e960 | 253 | |
andrewboyson | 11:c051adb70c5a | 254 | writeHeader((header*)pPacket); |
andrewboyson | 10:f0854784e960 | 255 | |
andrewboyson | 10:f0854784e960 | 256 | *pSize = headerLength + dataLength; |
andrewboyson | 10:f0854784e960 | 257 | |
andrewboyson | 37:793b39683406 | 258 | if (ActionGetTracePart(action)) logHeader(); |
andrewboyson | 37:793b39683406 | 259 | |
andrewboyson | 10:f0854784e960 | 260 | return action; |
andrewboyson | 10:f0854784e960 | 261 | } |