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:
Tue Apr 18 19:07:05 2017 +0000
Revision:
11:c051adb70c5a
Parent:
10:f0854784e960
Child:
14:e75a59c1123d
Tidied IPv6 code

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