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 Jun 26 15:24:06 2017 +0000
Revision:
18:accfcb80d9c3
Parent:
15:6ca6778168b1
Child:
22:914b970356f0
Added Ip4Parse routine

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