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