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 Aug 10 17:45:37 2017 +0000
Revision:
30:e34173b7585c
Parent:
22:914b970356f0
Child:
33:714a0345e59b
Added dns address request - only name requests was there before.

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