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:
Sun Apr 16 14:21:55 2017 +0000
Revision:
10:f0854784e960
Child:
13:9cd54f7db57a
MDNS and LLMNR now working.

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 "eth.h"
andrewboyson 10:f0854784e960 5 #include "udp.h"
andrewboyson 10:f0854784e960 6
andrewboyson 10:f0854784e960 7 #define DEBUG false
andrewboyson 10:f0854784e960 8
andrewboyson 10:f0854784e960 9 #define REQUEST 1
andrewboyson 10:f0854784e960 10 #define REPLY 2
andrewboyson 10:f0854784e960 11
andrewboyson 10:f0854784e960 12 #define DHCPDISCOVER 1
andrewboyson 10:f0854784e960 13 #define DHCPOFFER 2
andrewboyson 10:f0854784e960 14 #define DHCPREQUEST 3
andrewboyson 10:f0854784e960 15 #define DHCPDECLINE 4
andrewboyson 10:f0854784e960 16 #define DHCPACK 5
andrewboyson 10:f0854784e960 17 #define DHCPNAK 6
andrewboyson 10:f0854784e960 18 #define DHCPRELEASE 7
andrewboyson 10:f0854784e960 19 #define DHCPINFORM 8
andrewboyson 10:f0854784e960 20
andrewboyson 10:f0854784e960 21 #define ID 75648
andrewboyson 10:f0854784e960 22 #define COOKIE 0x63825363
andrewboyson 10:f0854784e960 23 #define HEADER_LENGTH 240
andrewboyson 10:f0854784e960 24
andrewboyson 10:f0854784e960 25 __packed struct header
andrewboyson 10:f0854784e960 26 {
andrewboyson 10:f0854784e960 27 uint8_t op;
andrewboyson 10:f0854784e960 28 uint8_t htype;
andrewboyson 10:f0854784e960 29 uint8_t hlen;
andrewboyson 10:f0854784e960 30 uint8_t hops;
andrewboyson 10:f0854784e960 31
andrewboyson 10:f0854784e960 32 uint32_t xid;
andrewboyson 10:f0854784e960 33
andrewboyson 10:f0854784e960 34 uint16_t secs;
andrewboyson 10:f0854784e960 35 uint16_t flags;
andrewboyson 10:f0854784e960 36
andrewboyson 10:f0854784e960 37 uint32_t ciaddr;
andrewboyson 10:f0854784e960 38 uint32_t yiaddr;
andrewboyson 10:f0854784e960 39 uint32_t siaddr;
andrewboyson 10:f0854784e960 40 uint32_t giaddr;
andrewboyson 10:f0854784e960 41
andrewboyson 10:f0854784e960 42 uint8_t chaddr[16];
andrewboyson 10:f0854784e960 43
andrewboyson 10:f0854784e960 44 char legacy[192];
andrewboyson 10:f0854784e960 45
andrewboyson 10:f0854784e960 46 uint32_t cookie;
andrewboyson 10:f0854784e960 47 };
andrewboyson 10:f0854784e960 48
andrewboyson 10:f0854784e960 49 #define REPEAT_DELAY_TIME 60
andrewboyson 10:f0854784e960 50 static uint32_t delayTime = 10000000; //Reset whenever a message is sent and blocks another send until count exceeds REPEAT_DELAY_TIME
andrewboyson 10:f0854784e960 51 static uint32_t elapsedTime = 0; //Reset whenever an IP address request has been acknowledged
andrewboyson 10:f0854784e960 52 void DhcpTick()
andrewboyson 10:f0854784e960 53 {
andrewboyson 10:f0854784e960 54 elapsedTime++;
andrewboyson 10:f0854784e960 55 delayTime++;
andrewboyson 10:f0854784e960 56 }
andrewboyson 10:f0854784e960 57
andrewboyson 10:f0854784e960 58 static uint8_t dhcpMessageType = 0;
andrewboyson 10:f0854784e960 59 uint32_t DhcpLeaseTime = 0;
andrewboyson 10:f0854784e960 60 uint32_t DhcpServer = 0;
andrewboyson 10:f0854784e960 61 uint32_t DhcpRouter = 0;
andrewboyson 10:f0854784e960 62 uint32_t DhcpSubnetMask = 0;
andrewboyson 10:f0854784e960 63 uint32_t DhcpNtp = 0;
andrewboyson 10:f0854784e960 64 uint32_t DhcpRenewalT1 = 0;
andrewboyson 10:f0854784e960 65 uint32_t DhcpRenewalT2 = 0;
andrewboyson 10:f0854784e960 66 uint32_t DhcpBroadcastIp = 0;
andrewboyson 10:f0854784e960 67 uint32_t DhcpLocalIp = 0;
andrewboyson 10:f0854784e960 68 uint32_t DhcpDnsServer = 0;
andrewboyson 10:f0854784e960 69 char DhcpDomainName[20];
andrewboyson 10:f0854784e960 70 char DhcpHostName[20];
andrewboyson 10:f0854784e960 71 static uint32_t readOption32(char** pp)
andrewboyson 10:f0854784e960 72 {
andrewboyson 10:f0854784e960 73 uint32_t value = 0;
andrewboyson 10:f0854784e960 74 char* p = *pp;
andrewboyson 10:f0854784e960 75 int len = *++p;
andrewboyson 10:f0854784e960 76 if (len >= 4)
andrewboyson 10:f0854784e960 77 {
andrewboyson 10:f0854784e960 78 value = *++p << 24;
andrewboyson 10:f0854784e960 79 value |= *++p << 16;
andrewboyson 10:f0854784e960 80 value |= *++p << 8;
andrewboyson 10:f0854784e960 81 value |= *++p << 0;
andrewboyson 10:f0854784e960 82 }
andrewboyson 10:f0854784e960 83 *pp += len + 1;
andrewboyson 10:f0854784e960 84 return value;
andrewboyson 10:f0854784e960 85 }
andrewboyson 10:f0854784e960 86 static uint32_t readIp(char** pp)
andrewboyson 10:f0854784e960 87 {
andrewboyson 10:f0854784e960 88 uint32_t value = 0;
andrewboyson 10:f0854784e960 89 char* p = *pp;
andrewboyson 10:f0854784e960 90 int len = *++p;
andrewboyson 10:f0854784e960 91 if (len >= 4)
andrewboyson 10:f0854784e960 92 {
andrewboyson 10:f0854784e960 93 value = *++p << 0;
andrewboyson 10:f0854784e960 94 value |= *++p << 8;
andrewboyson 10:f0854784e960 95 value |= *++p << 16;
andrewboyson 10:f0854784e960 96 value |= *++p << 24;
andrewboyson 10:f0854784e960 97 }
andrewboyson 10:f0854784e960 98 *pp += len + 1;
andrewboyson 10:f0854784e960 99 return value;
andrewboyson 10:f0854784e960 100 }
andrewboyson 10:f0854784e960 101 static void readString(char** pp, char* pText)
andrewboyson 10:f0854784e960 102 {
andrewboyson 10:f0854784e960 103 char* p = *pp;
andrewboyson 10:f0854784e960 104 int len = *++p;
andrewboyson 10:f0854784e960 105 for (int i = 0; i < len; i++) pText[i] = *++p;
andrewboyson 10:f0854784e960 106 *pp += len + 1;
andrewboyson 10:f0854784e960 107 }
andrewboyson 10:f0854784e960 108 static void readOptions(int size, char * pOptions)
andrewboyson 10:f0854784e960 109 {
andrewboyson 10:f0854784e960 110 int len = 0;
andrewboyson 10:f0854784e960 111 char* p = pOptions;
andrewboyson 10:f0854784e960 112 char* pE = pOptions + size;
andrewboyson 10:f0854784e960 113 while( p < pE)
andrewboyson 10:f0854784e960 114 {
andrewboyson 10:f0854784e960 115 switch (*p)
andrewboyson 10:f0854784e960 116 {
andrewboyson 10:f0854784e960 117 case 0: break; //NOP
andrewboyson 10:f0854784e960 118 case 255: return; //End of options
andrewboyson 10:f0854784e960 119 case 1: DhcpSubnetMask = readIp(&p); break; //Subnet Mask
andrewboyson 10:f0854784e960 120 case 3: DhcpRouter = readIp(&p); break; //Router
andrewboyson 10:f0854784e960 121 case 6: DhcpDnsServer = readIp(&p); break; //DNS server
andrewboyson 10:f0854784e960 122 case 12: readString(&p, DhcpHostName); break; //Host name
andrewboyson 10:f0854784e960 123 case 15: readString(&p, DhcpDomainName); break; //Domain name
andrewboyson 10:f0854784e960 124 case 19: len = *++p; p+= len; break; //IP forwarding yes/no
andrewboyson 10:f0854784e960 125 case 28: DhcpBroadcastIp = readIp(&p); break; //Broadcast IP
andrewboyson 10:f0854784e960 126 case 42: DhcpNtp = readIp(&p); break; //NTP
andrewboyson 10:f0854784e960 127 case 44: len = *++p; p+= len; break; //NetBIOS name server
andrewboyson 10:f0854784e960 128 case 45: len = *++p; p+= len; break; //NetBIOS datagram server
andrewboyson 10:f0854784e960 129 case 46: len = *++p; p+= len; break; //NetBIOS node type
andrewboyson 10:f0854784e960 130 case 47: len = *++p; p+= len; break; //NetBIOS scope
andrewboyson 10:f0854784e960 131 case 53: len = *++p; dhcpMessageType = *++p; break; //DHCP message type
andrewboyson 10:f0854784e960 132 case 51: DhcpLeaseTime = readOption32(&p); break; //Address lease time
andrewboyson 10:f0854784e960 133 case 54: DhcpServer = readIp(&p); break; //DHCP server
andrewboyson 10:f0854784e960 134 case 58: DhcpRenewalT1 = readOption32(&p); break; //T1
andrewboyson 10:f0854784e960 135 case 59: DhcpRenewalT2 = readOption32(&p); break; //T2
andrewboyson 10:f0854784e960 136 default:
andrewboyson 10:f0854784e960 137 if (DEBUG) LogTimeF("Ignoring option %d\r\n", *p);
andrewboyson 10:f0854784e960 138 len = *++p;
andrewboyson 10:f0854784e960 139 p += len;
andrewboyson 10:f0854784e960 140 return;
andrewboyson 10:f0854784e960 141 }
andrewboyson 10:f0854784e960 142 p++;
andrewboyson 10:f0854784e960 143 }
andrewboyson 10:f0854784e960 144 }
andrewboyson 10:f0854784e960 145 static void writeIp(uint8_t code, uint32_t value, char** pp)
andrewboyson 10:f0854784e960 146 {
andrewboyson 10:f0854784e960 147 if (!value) return;
andrewboyson 10:f0854784e960 148
andrewboyson 10:f0854784e960 149 char* p = *pp;
andrewboyson 10:f0854784e960 150
andrewboyson 10:f0854784e960 151 *p++ = code;
andrewboyson 10:f0854784e960 152 *p++ = 4;
andrewboyson 10:f0854784e960 153 *p++ = (value & 0x000000FF) >> 0;
andrewboyson 10:f0854784e960 154 *p++ = (value & 0x0000FF00) >> 8;
andrewboyson 10:f0854784e960 155 *p++ = (value & 0x00FF0000) >> 16;
andrewboyson 10:f0854784e960 156 *p++ = (value & 0xFF000000) >> 24;
andrewboyson 10:f0854784e960 157
andrewboyson 10:f0854784e960 158 *pp += 6;
andrewboyson 10:f0854784e960 159 }
andrewboyson 10:f0854784e960 160 int sendRequest(void* pPacket, uint8_t code, uint32_t srvIp, uint32_t reqIp)
andrewboyson 10:f0854784e960 161 {
andrewboyson 10:f0854784e960 162
andrewboyson 10:f0854784e960 163 switch (code)
andrewboyson 10:f0854784e960 164 {
andrewboyson 10:f0854784e960 165 case DHCPDISCOVER:
andrewboyson 10:f0854784e960 166 if (DEBUG) LogTimeF("DHCP -> discover");
andrewboyson 10:f0854784e960 167 break;
andrewboyson 10:f0854784e960 168 case DHCPREQUEST:
andrewboyson 10:f0854784e960 169 if (DEBUG) LogTimeF("DHCP -> request");
andrewboyson 10:f0854784e960 170 break;
andrewboyson 10:f0854784e960 171 default:
andrewboyson 10:f0854784e960 172 LogTimeF("DHCP -> unknown message %d", code);
andrewboyson 10:f0854784e960 173 break;
andrewboyson 10:f0854784e960 174 }
andrewboyson 10:f0854784e960 175 if (DEBUG)
andrewboyson 10:f0854784e960 176 {
andrewboyson 10:f0854784e960 177 char text[20];
andrewboyson 10:f0854784e960 178 NetIp4AddressToString(srvIp, sizeof(text), text);
andrewboyson 10:f0854784e960 179 LogF(" server=%s", text);
andrewboyson 10:f0854784e960 180 NetIp4AddressToString(reqIp, sizeof(text), text);
andrewboyson 10:f0854784e960 181 LogF(" request=%s", text);
andrewboyson 10:f0854784e960 182 NetIp4AddressToString(DhcpLocalIp, sizeof(text), text);
andrewboyson 10:f0854784e960 183 LogF(" local=%s\r\n", text);
andrewboyson 10:f0854784e960 184 }
andrewboyson 10:f0854784e960 185
andrewboyson 10:f0854784e960 186 bool broadcast = DhcpLocalIp == 0;
andrewboyson 10:f0854784e960 187 uint16_t flags = 0;
andrewboyson 10:f0854784e960 188 if (broadcast) flags |= 0x0080; //0x8000 on the net == 0x0080 in little endian
andrewboyson 10:f0854784e960 189 struct header* pHeader = (header*)pPacket;
andrewboyson 10:f0854784e960 190 pHeader->op = REQUEST;
andrewboyson 10:f0854784e960 191 pHeader->htype = ETHERNET;
andrewboyson 10:f0854784e960 192 pHeader->hlen = 6;
andrewboyson 10:f0854784e960 193 pHeader->hops = 0;
andrewboyson 10:f0854784e960 194 pHeader->xid = ID; //Randomly chosed transaction id used to associate messages to responses
andrewboyson 10:f0854784e960 195 pHeader->secs = 0; //Seconds since started to boot
andrewboyson 10:f0854784e960 196 pHeader->flags = flags; //Broadcast (1) Unicast (0)
andrewboyson 10:f0854784e960 197 pHeader->ciaddr = DhcpLocalIp; //'Client' address set by client or 0 if don't know address
andrewboyson 10:f0854784e960 198 pHeader->yiaddr = 0; //'Your' address returned by server
andrewboyson 10:f0854784e960 199 pHeader->siaddr = srvIp; //'Server' address to use if required
andrewboyson 10:f0854784e960 200 pHeader->giaddr = 0; //'Gateway' address
andrewboyson 10:f0854784e960 201 memcpy(pHeader->chaddr, EthLocalMac, 6); //'Client hardware' address. 6 bytes for ethernet
andrewboyson 10:f0854784e960 202 memset(pHeader->legacy, 0, 192); //BootP legacy fill with zeros
andrewboyson 10:f0854784e960 203 pHeader->cookie = NetToHost32(COOKIE); //Magic cookie
andrewboyson 10:f0854784e960 204
andrewboyson 10:f0854784e960 205 char* pOptions = (char*)pPacket + HEADER_LENGTH;
andrewboyson 10:f0854784e960 206 char* p = pOptions;
andrewboyson 10:f0854784e960 207 *p++ = 53; //Message code
andrewboyson 10:f0854784e960 208 *p++ = 1;
andrewboyson 10:f0854784e960 209 *p++ = code;
andrewboyson 10:f0854784e960 210
andrewboyson 10:f0854784e960 211 writeIp(50, reqIp, &p); //Requested IP
andrewboyson 10:f0854784e960 212 writeIp(54, srvIp, &p); //Server ip
andrewboyson 10:f0854784e960 213
andrewboyson 10:f0854784e960 214 *p++ = 255; //End of options
andrewboyson 10:f0854784e960 215
andrewboyson 10:f0854784e960 216 delayTime = 0;
andrewboyson 10:f0854784e960 217 return HEADER_LENGTH + p - pOptions;
andrewboyson 10:f0854784e960 218 }
andrewboyson 10:f0854784e960 219 int DhcpHandleResponse(int* pSize, void* pPacket)
andrewboyson 10:f0854784e960 220 {
andrewboyson 10:f0854784e960 221 struct header* pHeader = (header*)pPacket;
andrewboyson 10:f0854784e960 222
andrewboyson 10:f0854784e960 223 uint8_t op = pHeader->op;
andrewboyson 10:f0854784e960 224 uint8_t htype = pHeader->htype;
andrewboyson 10:f0854784e960 225 uint8_t hlen = pHeader->hlen;
andrewboyson 10:f0854784e960 226
andrewboyson 10:f0854784e960 227 uint32_t xid = pHeader->xid; //Randomly chosed transaction id used to associate messages to responses
andrewboyson 10:f0854784e960 228
andrewboyson 10:f0854784e960 229 uint16_t secs = NetToHost16(pHeader->secs);
andrewboyson 10:f0854784e960 230 uint16_t flags = NetToHost16(pHeader->flags);
andrewboyson 10:f0854784e960 231
andrewboyson 10:f0854784e960 232 uint32_t yiaddr = pHeader->yiaddr;
andrewboyson 10:f0854784e960 233 uint32_t siaddr = pHeader->siaddr;
andrewboyson 10:f0854784e960 234 uint32_t cookie = NetToHost32(pHeader->cookie);
andrewboyson 10:f0854784e960 235
andrewboyson 10:f0854784e960 236 if (op != REPLY) return DO_NOTHING;
andrewboyson 10:f0854784e960 237 if (htype != ETHERNET) return DO_NOTHING;
andrewboyson 10:f0854784e960 238 if (hlen != 6) return DO_NOTHING;
andrewboyson 10:f0854784e960 239 if (xid != ID) return DO_NOTHING;
andrewboyson 10:f0854784e960 240 if (cookie != COOKIE) return DO_NOTHING;
andrewboyson 10:f0854784e960 241
andrewboyson 10:f0854784e960 242 char* pOptions = (char*)pPacket + HEADER_LENGTH;
andrewboyson 10:f0854784e960 243 readOptions(*pSize - HEADER_LENGTH, pOptions);
andrewboyson 10:f0854784e960 244
andrewboyson 10:f0854784e960 245 char text[30];
andrewboyson 10:f0854784e960 246 switch (dhcpMessageType)
andrewboyson 10:f0854784e960 247 {
andrewboyson 10:f0854784e960 248 case DHCPOFFER:
andrewboyson 10:f0854784e960 249 NetIp4AddressToString(yiaddr, sizeof(text), text);
andrewboyson 10:f0854784e960 250 if (DEBUG) LogTimeF("DHCP <- offer ip %s\r\n", text);
andrewboyson 10:f0854784e960 251 *pSize =sendRequest(pPacket, DHCPREQUEST, siaddr, yiaddr);
andrewboyson 10:f0854784e960 252 return BROADCAST;
andrewboyson 10:f0854784e960 253 case DHCPACK:
andrewboyson 10:f0854784e960 254 NetIp4AddressToString(yiaddr, sizeof(text), text);
andrewboyson 10:f0854784e960 255 if (DEBUG) LogTimeF("DHCP <- ack ip %s\r\n", text);
andrewboyson 10:f0854784e960 256 DhcpLocalIp = yiaddr;
andrewboyson 10:f0854784e960 257 elapsedTime = 0;
andrewboyson 10:f0854784e960 258 break;
andrewboyson 10:f0854784e960 259 case DHCPNAK:
andrewboyson 10:f0854784e960 260 NetIp4AddressToString(yiaddr, sizeof(text), text);
andrewboyson 10:f0854784e960 261 if (DEBUG) LogTimeF("DHCP <- nack ip %s\r\n", text);
andrewboyson 10:f0854784e960 262 break;
andrewboyson 10:f0854784e960 263 default:
andrewboyson 10:f0854784e960 264 LogTimeF("DHCP <- unknown message %d\r\n", dhcpMessageType);
andrewboyson 10:f0854784e960 265 break;
andrewboyson 10:f0854784e960 266 }
andrewboyson 10:f0854784e960 267 return DO_NOTHING;
andrewboyson 10:f0854784e960 268 }
andrewboyson 10:f0854784e960 269
andrewboyson 10:f0854784e960 270 int DhcpPollForRequestToSend(void* pPacket, int* pSize)
andrewboyson 10:f0854784e960 271 {
andrewboyson 10:f0854784e960 272 if (delayTime < REPEAT_DELAY_TIME) return DO_NOTHING; //Don't retry within the delay time
andrewboyson 10:f0854784e960 273
andrewboyson 10:f0854784e960 274 if (DhcpLocalIp && elapsedTime < (DhcpLeaseTime >> 1)) return 0; //Do nothing if have address and within T1
andrewboyson 10:f0854784e960 275
andrewboyson 10:f0854784e960 276 *pSize = 0;
andrewboyson 10:f0854784e960 277
andrewboyson 10:f0854784e960 278 if (DhcpLocalIp && elapsedTime < DhcpLeaseTime)
andrewboyson 10:f0854784e960 279 {
andrewboyson 10:f0854784e960 280 *pSize = sendRequest(pPacket, DHCPREQUEST, DhcpServer, DhcpLocalIp); //if within T2 then send request to the server - not broadcast
andrewboyson 10:f0854784e960 281 return UNICAST_DHCP;
andrewboyson 10:f0854784e960 282 }
andrewboyson 10:f0854784e960 283 else
andrewboyson 10:f0854784e960 284 {
andrewboyson 10:f0854784e960 285 if (DEBUG) LogTimeF("DHCP lease has expired\r\n");
andrewboyson 10:f0854784e960 286 DhcpLocalIp = 0;
andrewboyson 10:f0854784e960 287 DhcpServer = 0;
andrewboyson 10:f0854784e960 288 *pSize = sendRequest(pPacket, DHCPDISCOVER, 0, 0); //If outside T2 then start from scratch to do a full DHCP
andrewboyson 10:f0854784e960 289 return BROADCAST;
andrewboyson 10:f0854784e960 290 }
andrewboyson 10:f0854784e960 291
andrewboyson 10:f0854784e960 292 }