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:
Wed Jan 23 15:42:35 2019 +0000
Revision:
115:5c003909bcf3
Parent:
udp/dhcp.c@93:580fc113d9e9
Child:
116:60521b29e4c9
Updated TFTP module

Who changed what in which revision?

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