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

Committer:
andrewboyson
Date:
Mon Sep 25 07:09:32 2017 +0000
Revision:
36:900e24b27bfb
Parent:
35:93c39d260a83
Child:
37:793b39683406
Corrected DNS NameCompare to correctly compare a shortcut name

Who changed what in which revision?

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