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:
Thu Oct 19 20:56:58 2017 +0000
Revision:
43:bc028d5a6424
Parent:
42:222a4f45f916
Child:
44:83ce5ace337b
Added verbose option to trace

Who changed what in which revision?

UserRevisionLine numberNew 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 43:bc028d5a6424 7 #include "ar.h"
andrewboyson 43:bc028d5a6424 8 #include "nr.h"
andrewboyson 43:bc028d5a6424 9 #include "dhcp.h"
andrewboyson 43:bc028d5a6424 10 #include "eth.h"
andrewboyson 43:bc028d5a6424 11 #include "ip.h"
andrewboyson 43:bc028d5a6424 12 #include "ip4.h"
andrewboyson 43:bc028d5a6424 13 #include "ntp.h"
andrewboyson 43:bc028d5a6424 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 43:bc028d5a6424 138 if (NetTraceVerbose)
andrewboyson 43:bc028d5a6424 139 {
andrewboyson 43:bc028d5a6424 140 Log ("IPv4 header\r\n");
andrewboyson 43:bc028d5a6424 141 LogF(" Version %d\r\n", version);
andrewboyson 43:bc028d5a6424 142 LogF(" Header length %d\r\n", headerLength);
andrewboyson 43:bc028d5a6424 143 LogF(" Type of service %d\r\n", tos);
andrewboyson 43:bc028d5a6424 144 LogF(" Data length %d\r\n", dataLength);
andrewboyson 43:bc028d5a6424 145 LogF(" Identification %d\r\n", id);
andrewboyson 43:bc028d5a6424 146 if (dontFragment) LogF(" Don't fragment\r\n");
andrewboyson 43:bc028d5a6424 147 else LogF(" Do fragment\r\n");
andrewboyson 43:bc028d5a6424 148 if (moreFragments) LogF(" More fragments\r\n");
andrewboyson 43:bc028d5a6424 149 else LogF(" No more fragments\r\n");
andrewboyson 43:bc028d5a6424 150 LogF(" Offset %d\r\n", offset);
andrewboyson 43:bc028d5a6424 151 LogF(" Time to live %d\r\n", ttl);
andrewboyson 43:bc028d5a6424 152 IpProtocolToString(protocol, sizeof(text), text);
andrewboyson 43:bc028d5a6424 153 LogF(" Protocol %s\r\n", text);
andrewboyson 43:bc028d5a6424 154 LogF(" Checksum (hex) %04hX\r\n", checksum);
andrewboyson 43:bc028d5a6424 155 LogF(" Calculated (hex) %04hX\r\n", calcsum);
andrewboyson 43:bc028d5a6424 156 Ip4AddressToString(srcIp, sizeof(text), text);
andrewboyson 43:bc028d5a6424 157 LogF(" Source IP %s\r\n", text);
andrewboyson 43:bc028d5a6424 158 Ip4AddressToString(dstIp, sizeof(text), text);
andrewboyson 43:bc028d5a6424 159 LogF(" Destination IP %s\r\n", text);
andrewboyson 43:bc028d5a6424 160 }
andrewboyson 43:bc028d5a6424 161 else
andrewboyson 43:bc028d5a6424 162 {
andrewboyson 43:bc028d5a6424 163 Log ("IPv4 header ");
andrewboyson 43:bc028d5a6424 164 IpProtocolToString(protocol, sizeof(text), text);
andrewboyson 43:bc028d5a6424 165 Log(text);
andrewboyson 43:bc028d5a6424 166 Log(" ");
andrewboyson 43:bc028d5a6424 167 Ip4AddressToString(srcIp, sizeof(text), text);
andrewboyson 43:bc028d5a6424 168 Log(text);
andrewboyson 43:bc028d5a6424 169 Log(" >>> ");
andrewboyson 43:bc028d5a6424 170 Ip4AddressToString(dstIp, sizeof(text), text);
andrewboyson 43:bc028d5a6424 171 Log(text);
andrewboyson 43:bc028d5a6424 172 Log("\r\n");
andrewboyson 43:bc028d5a6424 173 }
andrewboyson 11:c051adb70c5a 174 }
andrewboyson 37:793b39683406 175 static void (*pTraceBack)(void);
andrewboyson 37:793b39683406 176 static void trace()
andrewboyson 10:f0854784e960 177 {
andrewboyson 37:793b39683406 178 pTraceBack();
andrewboyson 37:793b39683406 179 logHeader();
andrewboyson 37:793b39683406 180 }
andrewboyson 37:793b39683406 181 int Ip4HandleReceivedPacket(void (*traceback)(void), char* pSrcMac, void* pPacket, int* pSize, char* pDstMac)
andrewboyson 37:793b39683406 182 {
andrewboyson 37:793b39683406 183 pTraceBack = traceback;
andrewboyson 10:f0854784e960 184 struct header * pHeader = (header*)pPacket;
andrewboyson 10:f0854784e960 185 readHeader(pHeader);
andrewboyson 10:f0854784e960 186
andrewboyson 37:793b39683406 187 bool isMe = dstIp == DhcpLocalIp;
andrewboyson 35:93c39d260a83 188 bool isLocalBroadcast = dstIp == DhcpLocalIp | 0xFF000000;
andrewboyson 37:793b39683406 189 bool isBroadcast = dstIp == IP4_BROADCAST_ADDRESS;
andrewboyson 37:793b39683406 190 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 191
andrewboyson 15:6ca6778168b1 192 bool doIt = isMe || isLocalBroadcast || isBroadcast || isMulticast;
andrewboyson 15:6ca6778168b1 193 if (!doIt)
andrewboyson 15:6ca6778168b1 194 {
andrewboyson 37:793b39683406 195 if (SHOW_FILTERED)
andrewboyson 15:6ca6778168b1 196 {
andrewboyson 15:6ca6778168b1 197 char text[20];
andrewboyson 35:93c39d260a83 198 Ip4AddressToString(dstIp, sizeof(text), text);
andrewboyson 15:6ca6778168b1 199 LogTimeF("IP4 filtered out ip %s ", text);
andrewboyson 35:93c39d260a83 200 Ip4AddressToString(srcIp, sizeof(text), text);
andrewboyson 15:6ca6778168b1 201 LogF("from %s \r\n", text);
andrewboyson 15:6ca6778168b1 202 }
andrewboyson 15:6ca6778168b1 203 return DO_NOTHING;
andrewboyson 15:6ca6778168b1 204 }
andrewboyson 10:f0854784e960 205
andrewboyson 35:93c39d260a83 206 ArAddIp4Record(pSrcMac, srcIp);
andrewboyson 35:93c39d260a83 207 NrMakeRequestForNameFromIp4(srcIp);
andrewboyson 10:f0854784e960 208
andrewboyson 10:f0854784e960 209 int action = DO_NOTHING;
andrewboyson 10:f0854784e960 210 switch (protocol)
andrewboyson 10:f0854784e960 211 {
andrewboyson 43:bc028d5a6424 212 case ICMP: action = Icmp4HandleReceivedPacket(trace, &srcIp, &dstIp, &dataLength, pData); break;
andrewboyson 37:793b39683406 213 case IGMP: break;
andrewboyson 43:bc028d5a6424 214 case UDP: action = Udp4HandleReceivedPacket(trace, &srcIp, &dstIp, &dataLength, pData); break;
andrewboyson 43:bc028d5a6424 215 case TCP: action = Tcp4HandleReceivedPacket(trace, &srcIp, &dstIp, &dataLength, pData); break;
andrewboyson 37:793b39683406 216 case IP6IN4: break;
andrewboyson 10:f0854784e960 217 default:
andrewboyson 37:793b39683406 218 LogTimeF("IP4 received packet unknown protocol %d\r\n");
andrewboyson 10:f0854784e960 219 return DO_NOTHING;
andrewboyson 10:f0854784e960 220 }
andrewboyson 11:c051adb70c5a 221 if (!action) return DO_NOTHING;
andrewboyson 10:f0854784e960 222
andrewboyson 36:900e24b27bfb 223 MacCopy(pDstMac, pSrcMac);
andrewboyson 37:793b39683406 224
andrewboyson 10:f0854784e960 225 writeHeader(pHeader);
andrewboyson 10:f0854784e960 226
andrewboyson 10:f0854784e960 227 *pSize = headerLength + dataLength;
andrewboyson 10:f0854784e960 228
andrewboyson 37:793b39683406 229 if (ActionGetTracePart(action)) logHeader();
andrewboyson 37:793b39683406 230
andrewboyson 10:f0854784e960 231 return action;
andrewboyson 10:f0854784e960 232 }
andrewboyson 10:f0854784e960 233 int Ip4PollForPacketToSend(void* pPacket, int* pSize, char* pDstMac)
andrewboyson 10:f0854784e960 234 {
andrewboyson 11:c051adb70c5a 235 headerLength = HEADER_LENGTH;
andrewboyson 11:c051adb70c5a 236 pData = (char*)pPacket + headerLength;
andrewboyson 11:c051adb70c5a 237 dataLength = 0;
andrewboyson 10:f0854784e960 238 version = 4;
andrewboyson 10:f0854784e960 239 tos = 0;
andrewboyson 10:f0854784e960 240 id = 0;
andrewboyson 10:f0854784e960 241 dontFragment = true;
andrewboyson 10:f0854784e960 242 moreFragments = false;
andrewboyson 10:f0854784e960 243 offset = 0;
andrewboyson 10:f0854784e960 244 ttl = 255;
andrewboyson 10:f0854784e960 245 protocol = UDP;
andrewboyson 10:f0854784e960 246
andrewboyson 11:c051adb70c5a 247 int action = DO_NOTHING;
andrewboyson 35:93c39d260a83 248 if (!action) action = Udp4PollForPacketToSend(pData, &dataLength, &srcIp, &dstIp);
andrewboyson 11:c051adb70c5a 249 if (!action) return DO_NOTHING;
andrewboyson 42:222a4f45f916 250 int dest = ActionGetDestPart(action);
andrewboyson 42:222a4f45f916 251 switch (dest)
andrewboyson 11:c051adb70c5a 252 {
andrewboyson 11:c051adb70c5a 253 case UNICAST:
andrewboyson 11:c051adb70c5a 254 case UNICAST_DNS:
andrewboyson 11:c051adb70c5a 255 case UNICAST_DHCP:
andrewboyson 22:914b970356f0 256 case UNICAST_NTP:
andrewboyson 35:93c39d260a83 257 ArIpToMac4(dstIp, pDstMac); //Make the remote MAC from ARP
andrewboyson 11:c051adb70c5a 258 break;
andrewboyson 42:222a4f45f916 259 case MULTICAST_NODE:
andrewboyson 42:222a4f45f916 260 case MULTICAST_ROUTER:
andrewboyson 42:222a4f45f916 261 case MULTICAST_MDNS:
andrewboyson 42:222a4f45f916 262 case MULTICAST_LLMNR:
andrewboyson 22:914b970356f0 263 case BROADCAST:
andrewboyson 22:914b970356f0 264 break;
andrewboyson 22:914b970356f0 265 default:
andrewboyson 42:222a4f45f916 266 LogTimeF("Ip4PollForPacketToSend - undefined destination %d\r\n", dest);
andrewboyson 22:914b970356f0 267 break;
andrewboyson 11:c051adb70c5a 268 }
andrewboyson 10:f0854784e960 269
andrewboyson 11:c051adb70c5a 270 writeHeader((header*)pPacket);
andrewboyson 10:f0854784e960 271
andrewboyson 10:f0854784e960 272 *pSize = headerLength + dataLength;
andrewboyson 10:f0854784e960 273
andrewboyson 37:793b39683406 274 if (ActionGetTracePart(action)) logHeader();
andrewboyson 37:793b39683406 275
andrewboyson 10:f0854784e960 276 return action;
andrewboyson 10:f0854784e960 277 }