MeterLogger
dhcp.c
Go to the documentation of this file.
1 /**
2  * @file
3  * Dynamic Host Configuration Protocol client
4  *
5  */
6 
7 /*
8  *
9  * Copyright (c) 2001-2004 Leon Woestenberg <leon.woestenberg@gmx.net>
10  * Copyright (c) 2001-2004 Axon Digital Design B.V., The Netherlands.
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without modification,
14  * are permitted provided that the following conditions are met:
15  *
16  * 1. Redistributions of source code must retain the above copyright notice,
17  * this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  * this list of conditions and the following disclaimer in the documentation
20  * and/or other materials provided with the distribution.
21  * 3. The name of the author may not be used to endorse or promote products
22  * derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
29  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33  * OF SUCH DAMAGE.
34  *
35  * This file is a contribution to the lwIP TCP/IP stack.
36  * The Swedish Institute of Computer Science and Adam Dunkels
37  * are specifically granted permission to redistribute this
38  * source code.
39  *
40  * Author: Leon Woestenberg <leon.woestenberg@gmx.net>
41  *
42  * This is a DHCP client for the lwIP TCP/IP stack. It aims to conform
43  * with RFC 2131 and RFC 2132.
44  *
45  * TODO:
46  * - Support for interfaces other than Ethernet (SLIP, PPP, ...)
47  *
48  * Please coordinate changes and requests with Leon Woestenberg
49  * <leon.woestenberg@gmx.net>
50  *
51  * Integration with your code:
52  *
53  * In lwip/dhcp.h
54  * #define DHCP_COARSE_TIMER_SECS (recommended 60 which is a minute)
55  * #define DHCP_FINE_TIMER_MSECS (recommended 500 which equals TCP coarse timer)
56  *
57  * Then have your application call dhcp_coarse_tmr() and
58  * dhcp_fine_tmr() on the defined intervals.
59  *
60  * dhcp_start(struct netif *netif);
61  * starts a DHCP client instance which configures the interface by
62  * obtaining an IP address lease and maintaining it.
63  *
64  * Use dhcp_release(netif) to end the lease and use dhcp_stop(netif)
65  * to remove the DHCP client.
66  *
67  */
68 
69 #include "lwip/opt.h"
70 
71 #if LWIP_DHCP /* don't build if not configured for use in lwipopts.h */
72 
73 #include "lwip/stats.h"
74 #include "lwip/mem.h"
75 #include "lwip/udp.h"
76 #include "lwip/ip_addr.h"
77 #include "lwip/netif.h"
78 #include "lwip/def.h"
79 #include "lwip/sys.h"
80 #include "lwip/dhcp.h"
81 #include "lwip/autoip.h"
82 #include "lwip/dns.h"
83 #include "netif/etharp.h"
84 
85 #include <string.h>
86 
87 #ifdef MEMLEAK_DEBUG
88 static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
89 #endif
90 
91 /** Default for DHCP_GLOBAL_XID is 0xABCD0000
92  * This can be changed by defining DHCP_GLOBAL_XID and DHCP_GLOBAL_XID_HEADER, e.g.
93  * #define DHCP_GLOBAL_XID_HEADER "stdlib.h"
94  * #define DHCP_GLOBAL_XID rand()
95  */
96 #ifdef DHCP_GLOBAL_XID_HEADER
97 #include DHCP_GLOBAL_XID_HEADER /* include optional starting XID generation prototypes */
98 #endif
99 
100 /** DHCP_OPTION_MAX_MSG_SIZE is set to the MTU
101  * MTU is checked to be big enough in dhcp_start */
102 #define DHCP_MAX_MSG_LEN(netif) (netif->mtu)
103 #define DHCP_MAX_MSG_LEN_MIN_REQUIRED 576
104 /** Minimum length for reply before packet is parsed */
105 #define DHCP_MIN_REPLY_LEN 44
106 
107 #define REBOOT_TRIES 2
108 
109 /** Option handling: options are parsed in dhcp_parse_reply
110  * and saved in an array where other functions can load them from.
111  * This might be moved into the struct dhcp (not necessarily since
112  * lwIP is single-threaded and the array is only used while in recv
113  * callback). */
114 #define DHCP_OPTION_IDX_OVERLOAD 0
115 #define DHCP_OPTION_IDX_MSG_TYPE 1
116 #define DHCP_OPTION_IDX_SERVER_ID 2
117 #define DHCP_OPTION_IDX_LEASE_TIME 3
118 #define DHCP_OPTION_IDX_T1 4
119 #define DHCP_OPTION_IDX_T2 5
120 #define DHCP_OPTION_IDX_SUBNET_MASK 6
121 #define DHCP_OPTION_IDX_ROUTER 7
122 #define DHCP_OPTION_IDX_DNS_SERVER 8
123 #define DHCP_OPTION_IDX_MAX (DHCP_OPTION_IDX_DNS_SERVER + DNS_MAX_SERVERS)
124 
125 /** Holds the decoded option values, only valid while in dhcp_recv.
126  @todo: move this into struct dhcp? */
127 u32_t dhcp_rx_options_val[DHCP_OPTION_IDX_MAX];
128 /** Holds a flag which option was received and is contained in dhcp_rx_options_val,
129  only valid while in dhcp_recv.
130  @todo: move this into struct dhcp? */
131 u8_t dhcp_rx_options_given[DHCP_OPTION_IDX_MAX];
132 
133 #define dhcp_option_given(dhcp, idx) (dhcp_rx_options_given[idx] != 0)
134 #define dhcp_got_option(dhcp, idx) (dhcp_rx_options_given[idx] = 1)
135 #define dhcp_clear_option(dhcp, idx) (dhcp_rx_options_given[idx] = 0)
136 #define dhcp_clear_all_options(dhcp) (os_memset(dhcp_rx_options_given, 0, sizeof(dhcp_rx_options_given)))
137 #define dhcp_get_option_value(dhcp, idx) (dhcp_rx_options_val[idx])
138 #define dhcp_set_option_value(dhcp, idx, val) (dhcp_rx_options_val[idx] = (val))
139 
140 
141 /* DHCP client state machine functions */
142 static err_t dhcp_discover(struct netif *netif);
143 static err_t dhcp_select(struct netif *netif);
144 static void dhcp_bind(struct netif *netif);
145 #if DHCP_DOES_ARP_CHECK
146 static err_t dhcp_decline(struct netif *netif);
147 #endif /* DHCP_DOES_ARP_CHECK */
148 static err_t dhcp_rebind(struct netif *netif);
149 static err_t dhcp_reboot(struct netif *netif);
150 static void dhcp_set_state(struct dhcp *dhcp, u8_t new_state);
151 
152 /* receive, unfold, parse and free incoming messages */
153 static void dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port);
154 
155 /* set the DHCP timers */
156 static void dhcp_timeout(struct netif *netif);
157 static void dhcp_t1_timeout(struct netif *netif);
158 static void dhcp_t2_timeout(struct netif *netif);
159 
160 /* build outgoing messages */
161 /* create a DHCP message, fill in common headers */
162 static err_t dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type);
163 /* free a DHCP request */
164 static void dhcp_delete_msg(struct dhcp *dhcp);
165 /* add a DHCP option (type, then length in bytes) */
166 static void dhcp_option(struct dhcp *dhcp, u8_t option_type, u8_t option_len);
167 /* add option values */
168 static void dhcp_option_byte(struct dhcp *dhcp, u8_t value);
169 static void dhcp_option_short(struct dhcp *dhcp, u16_t value);
170 static void dhcp_option_long(struct dhcp *dhcp, u32_t value);
171 /* always add the DHCP options trailer to end and pad */
172 static void dhcp_option_trailer(struct dhcp *dhcp);
173 
174 /**
175  * Back-off the DHCP client (because of a received NAK response).
176  *
177  * Back-off the DHCP client because of a received NAK. Receiving a
178  * NAK means the client asked for something non-sensible, for
179  * example when it tries to renew a lease obtained on another network.
180  *
181  * We clear any existing set IP address and restart DHCP negotiation
182  * afresh (as per RFC2131 3.2.3).
183  *
184  * @param netif the netif under DHCP control
185  */
186 static void ICACHE_FLASH_ATTR
187 dhcp_handle_nak(struct netif *netif)
188 {
189  struct dhcp *dhcp = netif->dhcp;
190  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_handle_nak(netif=%p) %c%c%"U16_F"\n",
191  (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
192  /* Set the interface down since the address must no longer be used, as per RFC2131 */
193  netif_set_down(netif);
194  /* remove IP address from interface */
196  netif_set_gw(netif, IP_ADDR_ANY);
198  /* Change to a defined state */
199  dhcp_set_state(dhcp, DHCP_BACKING_OFF);
200  /* We can immediately restart discovery */
201  dhcp_discover(netif);
202 }
203 
204 #if DHCP_DOES_ARP_CHECK
205 /**
206  * Checks if the offered IP address is already in use.
207  *
208  * It does so by sending an ARP request for the offered address and
209  * entering CHECKING state. If no ARP reply is received within a small
210  * interval, the address is assumed to be free for use by us.
211  *
212  * @param netif the netif under DHCP control
213  */
214 static void ICACHE_FLASH_ATTR
215 dhcp_check(struct netif *netif)
216 {
217  struct dhcp *dhcp = netif->dhcp;
218  err_t result;
219  u16_t msecs;
220  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_check(netif=%p) %c%c\n", (void *)netif, (s16_t)netif->name[0],
221  (s16_t)netif->name[1]));
222  dhcp_set_state(dhcp, DHCP_CHECKING);
223  /* create an ARP query for the offered IP address, expecting that no host
224  responds, as the IP address should not be in use. */
225  result = etharp_query(netif, &dhcp->offered_ip_addr, NULL);
226  if (result != ERR_OK) {
227  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("dhcp_check: could not perform ARP query\n"));
228  }
229  dhcp->tries++;
230  msecs = 500;
231  dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
232  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_check(): set request timeout %"U16_F" msecs\n", msecs));
233 }
234 #endif /* DHCP_DOES_ARP_CHECK */
235 
236 /**
237  * Remember the configuration offered by a DHCP server.
238  *
239  * @param netif the netif under DHCP control
240  */
241 static void ICACHE_FLASH_ATTR
242 dhcp_handle_offer(struct netif *netif)
243 {
244  struct dhcp *dhcp = netif->dhcp;
245  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_handle_offer(netif=%p) %c%c%"U16_F"\n",
246  (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
247  /* obtain the server address */
248  if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_SERVER_ID)) {
249  ip4_addr_set_u32(&dhcp->server_ip_addr, htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_SERVER_ID)));
250  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_handle_offer(): server 0x%08"X32_F"\n",
251  ip4_addr_get_u32(&dhcp->server_ip_addr)));
252  /* remember offered address */
253  ip_addr_copy(dhcp->offered_ip_addr, dhcp->msg_in->yiaddr);
254  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_handle_offer(): offer for 0x%08"X32_F"\n",
255  ip4_addr_get_u32(&dhcp->offered_ip_addr)));
256 
257  dhcp_select(netif);
258  } else {
260  ("dhcp_handle_offer(netif=%p) did not get server ID!\n", (void*)netif));
261  }
262 }
263 
264 /**
265  * Select a DHCP server offer out of all offers.
266  *
267  * Simply select the first offer received.
268  *
269  * @param netif the netif under DHCP control
270  * @return lwIP specific error (see error.h)
271  */
273 dhcp_select(struct netif *netif)
274 {
275  struct dhcp *dhcp = netif->dhcp;
276  err_t result;
277  u16_t msecs;
278 
279  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_select(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
280  dhcp_set_state(dhcp, DHCP_REQUESTING);
281 
282  /* create and initialize the DHCP message header */
283  result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
284  if (result == ERR_OK) {
285  dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
286  dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
287 
288  /* MUST request the offered IP address */
289  dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
290  dhcp_option_long(dhcp, ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
291 
292  dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
293  dhcp_option_long(dhcp, ntohl(ip4_addr_get_u32(&dhcp->server_ip_addr)));
294 
295  dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, 12/*num options*/);
296  dhcp_option_byte(dhcp, DHCP_OPTION_SUBNET_MASK);
297  dhcp_option_byte(dhcp, DHCP_OPTION_ROUTER);
298  dhcp_option_byte(dhcp, DHCP_OPTION_BROADCAST);
299  dhcp_option_byte(dhcp, DHCP_OPTION_DNS_SERVER);
300  dhcp_option_byte(dhcp, DHCP_OPTION_DOMAIN_NAME);
301  dhcp_option_byte(dhcp, DHCP_OPTION_NB_TINS);
302  dhcp_option_byte(dhcp, DHCP_OPTION_NB_TINT);
303  dhcp_option_byte(dhcp, DHCP_OPTION_NB_TIS);
304  dhcp_option_byte(dhcp, DHCP_OPTION_PRD);
305  dhcp_option_byte(dhcp, DHCP_OPTION_STATIC_ROUTER);
306  dhcp_option_byte(dhcp, DHCP_OPTION_CLASSLESS_STATIC_ROUTER);
307  dhcp_option_byte(dhcp, DHCP_OPTION_VSN);
308 
309 #if LWIP_NETIF_HOSTNAME
310  if (netif->hostname != NULL) {
311  const char *p = (const char*)netif->hostname;
312  u8_t namelen = (u8_t)os_strlen(p);
313  if (namelen > 0) {
314  LWIP_ASSERT("DHCP: hostname is too long!", namelen < 255);
315  dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, namelen);
316  while (*p) {
317  dhcp_option_byte(dhcp, *p++);
318  }
319  }
320  }
321 #endif /* LWIP_NETIF_HOSTNAME */
322 
323  dhcp_option_trailer(dhcp);
324  /* shrink the pbuf to the actual content length */
325  pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
326 
327  /* send broadcast to any DHCP server */
328  udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
329  dhcp_delete_msg(dhcp);
330  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_select: REQUESTING\n"));
331  } else {
332  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("dhcp_select: could not allocate DHCP request\n"));
333  }
334  dhcp->tries++;
335  msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000;
336  dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
337  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_select(): set request timeout %"U16_F" msecs\n", msecs));
338  return result;
339 }
340 
341 /**
342  * The DHCP timer that checks for lease renewal/rebind timeouts.
343  */
345 dhcp_coarse_tmr()
346 {
347  struct netif *netif = netif_list;
348  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_coarse_tmr()\n"));
349  /* iterate through all network interfaces */
350  while (netif != NULL) {
351  /* only act on DHCP configured interfaces */
352  if (netif->dhcp != NULL) {
353  /* timer is active (non zero), and triggers (zeroes) now? */
354  if (netif->dhcp->t2_timeout-- == 1) {
355  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_coarse_tmr(): t2 timeout\n"));
356  /* this clients' rebind timeout triggered */
357  dhcp_t2_timeout(netif);
358  /* timer is active (non zero), and triggers (zeroes) now */
359  } else if (netif->dhcp->t1_timeout-- == 1) {
360  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_coarse_tmr(): t1 timeout\n"));
361  /* this clients' renewal timeout triggered */
362  dhcp_t1_timeout(netif);
363  }
364  }
365  /* proceed to next netif */
366  netif = netif->next;
367  }
368 }
369 
370 /**
371  * DHCP transaction timeout handling
372  *
373  * A DHCP server is expected to respond within a short period of time.
374  * This timer checks whether an outstanding DHCP request is timed out.
375  */
377 dhcp_fine_tmr()
378 {
379  struct netif *netif = netif_list;
380  /* loop through netif's */
381  while (netif != NULL) {
382  /* only act on DHCP configured interfaces */
383  if (netif->dhcp != NULL) {
384  /*add DHCP retries processing by LiuHan*/
385  if (DHCP_MAXRTX != 0) {
386  if (netif->dhcp->tries >= DHCP_MAXRTX){
387  os_printf("DHCP timeout\n");
388  if (netif->dhcp_event != NULL)
389  netif->dhcp_event();
390  break;
391  }
392  }
393  /* timer is active (non zero), and is about to trigger now */
394  if (netif->dhcp->request_timeout > 1) {
395  netif->dhcp->request_timeout--;
396  }
397  else if (netif->dhcp->request_timeout == 1) {
398  netif->dhcp->request_timeout--;
399  /* { netif->dhcp->request_timeout == 0 } */
400  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_fine_tmr(): request timeout\n"));
401  /* this client's request timeout triggered */
402  dhcp_timeout(netif);
403  }
404  }
405  /* proceed to next network interface */
406  netif = netif->next;
407  }
408 
409 }
410 
411 /**
412  * A DHCP negotiation transaction, or ARP request, has timed out.
413  *
414  * The timer that was started with the DHCP or ARP request has
415  * timed out, indicating no response was received in time.
416  *
417  * @param netif the netif under DHCP control
418  */
419 static void ICACHE_FLASH_ATTR
420 dhcp_timeout(struct netif *netif)
421 {
422  struct dhcp *dhcp = netif->dhcp;
423  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_timeout()\n"));
424  /* back-off period has passed, or server selection timed out */
425  if ((dhcp->state == DHCP_BACKING_OFF) || (dhcp->state == DHCP_SELECTING)) {
426  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_timeout(): restarting discovery\n"));
427  dhcp_discover(netif);
428  /* receiving the requested lease timed out */
429  } else if (dhcp->state == DHCP_REQUESTING) {
430  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REQUESTING, DHCP request timed out\n"));
431  if (dhcp->tries <= 5) {
432  dhcp_select(netif);
433  } else {
434  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REQUESTING, releasing, restarting\n"));
435  dhcp_release(netif);
436  dhcp_discover(netif);
437  }
438 #if DHCP_DOES_ARP_CHECK
439  /* received no ARP reply for the offered address (which is good) */
440  } else if (dhcp->state == DHCP_CHECKING) {
441  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): CHECKING, ARP request timed out\n"));
442  if (dhcp->tries <= 1) {
443  dhcp_check(netif);
444  /* no ARP replies on the offered address,
445  looks like the IP address is indeed free */
446  } else {
447  /* bind the interface to the offered address */
448  dhcp_bind(netif);
449  }
450 #endif /* DHCP_DOES_ARP_CHECK */
451  }
452  /* did not get response to renew request? */
453  else if (dhcp->state == DHCP_RENEWING) {
454  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): RENEWING, DHCP request timed out\n"));
455  /* just retry renewal */
456  /* note that the rebind timer will eventually time-out if renew does not work */
457  dhcp_renew(netif);
458  /* did not get response to rebind request? */
459  } else if (dhcp->state == DHCP_REBINDING) {
460  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): REBINDING, DHCP request timed out\n"));
461  if (dhcp->tries <= 8) {
462  dhcp_rebind(netif);
463  } else {
464  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_timeout(): RELEASING, DISCOVERING\n"));
465  dhcp_release(netif);
466  dhcp_discover(netif);
467  }
468  } else if (dhcp->state == DHCP_REBOOTING) {
469  if (dhcp->tries < REBOOT_TRIES) {
470  dhcp_reboot(netif);
471  } else {
472  dhcp_discover(netif);
473  }
474  }
475 }
476 
477 /**
478  * The renewal period has timed out.
479  *
480  * @param netif the netif under DHCP control
481  */
482 static void ICACHE_FLASH_ATTR
483 dhcp_t1_timeout(struct netif *netif)
484 {
485  struct dhcp *dhcp = netif->dhcp;
486  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_t1_timeout()\n"));
487  if ((dhcp->state == DHCP_REQUESTING) || (dhcp->state == DHCP_BOUND) ||
488  (dhcp->state == DHCP_RENEWING)) {
489  /* just retry to renew - note that the rebind timer (t2) will
490  * eventually time-out if renew tries fail. */
492  ("dhcp_t1_timeout(): must renew\n"));
493  /* This slightly different to RFC2131: DHCPREQUEST will be sent from state
494  DHCP_RENEWING, not DHCP_BOUND */
495  dhcp_renew(netif);
496  }
497 }
498 
499 /**
500  * The rebind period has timed out.
501  *
502  * @param netif the netif under DHCP control
503  */
504 static void ICACHE_FLASH_ATTR
505 dhcp_t2_timeout(struct netif *netif)
506 {
507  struct dhcp *dhcp = netif->dhcp;
508  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_t2_timeout()\n"));
509  if ((dhcp->state == DHCP_REQUESTING) || (dhcp->state == DHCP_BOUND) ||
510  (dhcp->state == DHCP_RENEWING)) {
511  /* just retry to rebind */
513  ("dhcp_t2_timeout(): must rebind\n"));
514  /* This slightly different to RFC2131: DHCPREQUEST will be sent from state
515  DHCP_REBINDING, not DHCP_BOUND */
516  dhcp_rebind(netif);
517  }
518 }
519 
520 /**
521  * Handle a DHCP ACK packet
522  *
523  * @param netif the netif under DHCP control
524  */
525 static void ICACHE_FLASH_ATTR
526 dhcp_handle_ack(struct netif *netif)
527 {
528  struct dhcp *dhcp = netif->dhcp;
529 #if LWIP_DNS
530  u8_t n;
531 #endif /* LWIP_DNS */
532 
533  /* clear options we might not get from the ACK */
534  ip_addr_set_zero(&dhcp->offered_sn_mask);
535  ip_addr_set_zero(&dhcp->offered_gw_addr);
536 #if LWIP_DHCP_BOOTP_FILE
537  ip_addr_set_zero(&dhcp->offered_si_addr);
538 #endif /* LWIP_DHCP_BOOTP_FILE */
539 
540  /* lease time given? */
541  if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_LEASE_TIME)) {
542  /* remember offered lease time */
543  dhcp->offered_t0_lease = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_LEASE_TIME);
544  }
545  /* renewal period given? */
546  if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_T1)) {
547  /* remember given renewal period */
548  dhcp->offered_t1_renew = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_T1);
549  } else {
550  /* calculate safe periods for renewal */
551  dhcp->offered_t1_renew = dhcp->offered_t0_lease / 2;
552  }
553 
554  /* renewal period given? */
555  if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_T2)) {
556  /* remember given rebind period */
557  dhcp->offered_t2_rebind = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_T2);
558  } else {
559  /* calculate safe periods for rebinding */
560  dhcp->offered_t2_rebind = dhcp->offered_t0_lease;
561  }
562 
563  /* (y)our internet address */
564  ip_addr_copy(dhcp->offered_ip_addr, dhcp->msg_in->yiaddr);
565 
566 #if LWIP_DHCP_BOOTP_FILE
567  /* copy boot server address,
568  boot file name copied in dhcp_parse_reply if not overloaded */
569  ip_addr_copy(dhcp->offered_si_addr, dhcp->msg_in->siaddr);
570 #endif /* LWIP_DHCP_BOOTP_FILE */
571 
572  /* subnet mask given? */
573  if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_SUBNET_MASK)) {
574  /* remember given subnet mask */
575  ip4_addr_set_u32(&dhcp->offered_sn_mask, htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_SUBNET_MASK)));
576  dhcp->subnet_mask_given = 1;
577  } else {
578  dhcp->subnet_mask_given = 0;
579  }
580 
581  /* gateway router */
582  if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_ROUTER)) {
583  ip4_addr_set_u32(&dhcp->offered_gw_addr, htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_ROUTER)));
584  }
585 
586 #if LWIP_DNS
587  /* DNS servers */
588  n = 0;
589  while(dhcp_option_given(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n) && (n < DNS_MAX_SERVERS)) {
590  ip_addr_t dns_addr;
591  ip4_addr_set_u32(&dns_addr, htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n)));
592  dns_setserver(n, &dns_addr);
593  n++;
594  }
595 #endif /* LWIP_DNS */
596 }
597 
598 /** Set a statically allocated struct dhcp to work with.
599  * Using this prevents dhcp_start to allocate it using mem_malloc.
600  *
601  * @param netif the netif for which to set the struct dhcp
602  * @param dhcp (uninitialised) dhcp struct allocated by the application
603  */
605 dhcp_set_struct(struct netif *netif, struct dhcp *dhcp)
606 {
607  LWIP_ASSERT("netif != NULL", netif != NULL);
608  LWIP_ASSERT("dhcp != NULL", dhcp != NULL);
609  LWIP_ASSERT("netif already has a struct dhcp set", netif->dhcp == NULL);
610 
611  /* clear data structure */
612  os_memset(dhcp, 0, sizeof(struct dhcp));
613  /* dhcp_set_state(&dhcp, DHCP_OFF); */
614  netif->dhcp = dhcp;
615 }
616 
617 /** Removes a struct dhcp from a netif.
618  *
619  * ATTENTION: Only use this when not using dhcp_set_struct() to allocate the
620  * struct dhcp since the memory is passed back to the heap.
621  *
622  * @param netif the netif from which to remove the struct dhcp
623  */
624 void ICACHE_FLASH_ATTR dhcp_cleanup(struct netif *netif)
625 {
626  LWIP_ASSERT("netif != NULL", netif != NULL);
627 
628  if (netif->dhcp != NULL) {
629  mem_free(netif->dhcp);
630  netif->dhcp = NULL;
631  }
632 }
633 
634 /**
635  * Start DHCP negotiation for a network interface.
636  *
637  * If no DHCP client instance was attached to this interface,
638  * a new client is created first. If a DHCP client instance
639  * was already present, it restarts negotiation.
640  *
641  * @param netif The lwIP network interface
642  * @return lwIP error code
643  * - ERR_OK - No error
644  * - ERR_MEM - Out of memory
645  */
647 dhcp_start(struct netif *netif)
648 {
649  struct dhcp *dhcp;
650  err_t result = ERR_OK;
651  LWIP_ERROR("netif != NULL", (netif != NULL), return ERR_ARG;);
652  dhcp = netif->dhcp;
653  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_start(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
654  /* Remove the flag that says this netif is handled by DHCP,
655  it is set when we succeeded starting. */
656  netif->flags &= ~NETIF_FLAG_DHCP;
657 
658  /* check hwtype of the netif */
659  if ((netif->flags & NETIF_FLAG_ETHARP) == 0) {
660  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): No ETHARP netif\n"));
661  return ERR_ARG;
662  }
663 
664  /* check MTU of the netif */
665  if (netif->mtu < DHCP_MAX_MSG_LEN_MIN_REQUIRED) {
666  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): Cannot use this netif with DHCP: MTU is too small\n"));
667  return ERR_MEM;
668  }
669 
670  /* no DHCP client attached yet? */
671  if (dhcp == NULL) {
672  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): starting new DHCP client\n"));
673  dhcp = (struct dhcp *)mem_malloc(sizeof(struct dhcp));
674  if (dhcp == NULL) {
675  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): could not allocate dhcp\n"));
676  return ERR_MEM;
677  }
678  /* store this dhcp client in the netif */
679  netif->dhcp = dhcp;
680  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): allocated dhcp"));
681  /* already has DHCP client attached */
682  } else {
683  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_start(): restarting DHCP configuration\n"));
684  if (dhcp->pcb != NULL) {
685  udp_remove(dhcp->pcb);
686  }
687  LWIP_ASSERT("pbuf p_out wasn't freed", dhcp->p_out == NULL);
688  LWIP_ASSERT("reply wasn't freed", dhcp->msg_in == NULL );
689  }
690 
691  /* clear data structure */
692  os_memset(dhcp, 0, sizeof(struct dhcp));
693  /* dhcp_set_state(&dhcp, DHCP_OFF); */
694  /* allocate UDP PCB */
695  dhcp->pcb = udp_new();
696  if (dhcp->pcb == NULL) {
697  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): could not obtain pcb\n"));
698  return ERR_MEM;
699  }
700  dhcp->pcb->so_options |= SOF_BROADCAST;
701  /* set up local and remote port for the pcb */
702  udp_bind(dhcp->pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT);
703  udp_connect(dhcp->pcb, IP_ADDR_ANY, DHCP_SERVER_PORT);
704  /* set up the recv callback and argument */
705  udp_recv(dhcp->pcb, dhcp_recv, netif);
706  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): starting DHCP configuration\n"));
707  /* (re)start the DHCP negotiation */
708  result = dhcp_discover(netif);
709  if (result != ERR_OK) {
710  /* free resources allocated above */
711  dhcp_stop(netif);
712  return ERR_MEM;
713  }
714  /* Set the flag that says this netif is handled by DHCP. */
715  netif->flags |= NETIF_FLAG_DHCP;
716  return result;
717 }
718 
719 /**
720  * Inform a DHCP server of our manual configuration.
721  *
722  * This informs DHCP servers of our fixed IP address configuration
723  * by sending an INFORM message. It does not involve DHCP address
724  * configuration, it is just here to be nice to the network.
725  *
726  * @param netif The lwIP network interface
727  */
729 dhcp_inform(struct netif *netif)
730 {
731  struct dhcp dhcp;
732  err_t result = ERR_OK;
733  struct udp_pcb *pcb;
734 
735  LWIP_ERROR("netif != NULL", (netif != NULL), return;);
736 
737  os_memset(&dhcp, 0, sizeof(struct dhcp));
738  dhcp_set_state(&dhcp, DHCP_INFORM);
739 
740  if ((netif->dhcp != NULL) && (netif->dhcp->pcb != NULL)) {
741  /* re-use existing pcb */
742  pcb = netif->dhcp->pcb;
743  } else {
744  pcb = udp_new();
745  if (pcb == NULL) {
746  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_inform(): could not obtain pcb"));
747  return;
748  }
749  dhcp.pcb = pcb;
750  dhcp.pcb->so_options |= SOF_BROADCAST;
751  udp_bind(dhcp.pcb, IP_ADDR_ANY, DHCP_CLIENT_PORT);
752  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_inform(): created new udp pcb\n"));
753  }
754  /* create and initialize the DHCP message header */
755  result = dhcp_create_msg(netif, &dhcp, DHCP_INFORM);
756  if (result == ERR_OK) {
757  dhcp_option(&dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
758  dhcp_option_short(&dhcp, DHCP_MAX_MSG_LEN(netif));
759 
760  dhcp_option_trailer(&dhcp);
761 
762  pbuf_realloc(dhcp.p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp.options_out_len);
763 
764  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_inform: INFORMING\n"));
765  udp_sendto_if(pcb, dhcp.p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
766  dhcp_delete_msg(&dhcp);
767  } else {
768  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_inform: could not allocate DHCP request\n"));
769  }
770 
771  if (dhcp.pcb != NULL) {
772  /* otherwise, the existing pcb was used */
773  udp_remove(dhcp.pcb);
774  }
775 }
776 
777 /** Handle a possible change in the network configuration.
778  *
779  * This enters the REBOOTING state to verify that the currently bound
780  * address is still valid.
781  */
783 dhcp_network_changed(struct netif *netif)
784 {
785  struct dhcp *dhcp = netif->dhcp;
786  if (!dhcp)
787  return;
788  switch (dhcp->state) {
789  case DHCP_REBINDING:
790  case DHCP_RENEWING:
791  case DHCP_BOUND:
792  case DHCP_REBOOTING:
793  netif_set_down(netif);
794  dhcp->tries = 0;
795  dhcp_reboot(netif);
796  break;
797  case DHCP_OFF:
798  /* stay off */
799  break;
800  default:
801  dhcp->tries = 0;
802 #if LWIP_DHCP_AUTOIP_COOP
803  if(dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
804  autoip_stop(netif);
805  dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
806  }
807 #endif /* LWIP_DHCP_AUTOIP_COOP */
808  dhcp_discover(netif);
809  break;
810  }
811 }
812 
813 #if DHCP_DOES_ARP_CHECK
814 /**
815  * Match an ARP reply with the offered IP address.
816  *
817  * @param netif the network interface on which the reply was received
818  * @param addr The IP address we received a reply from
819  */
820 void ICACHE_FLASH_ATTR dhcp_arp_reply(struct netif *netif, ip_addr_t *addr)
821 {
822  LWIP_ERROR("netif != NULL", (netif != NULL), return;);
823  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_arp_reply()\n"));
824  /* is a DHCP client doing an ARP check? */
825  if ((netif->dhcp != NULL) && (netif->dhcp->state == DHCP_CHECKING)) {
826  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_arp_reply(): CHECKING, arp reply for 0x%08"X32_F"\n",
827  ip4_addr_get_u32(addr)));
828  /* did a host respond with the address we
829  were offered by the DHCP server? */
830  if (ip_addr_cmp(addr, &netif->dhcp->offered_ip_addr)) {
831  /* we will not accept the offered address */
833  ("dhcp_arp_reply(): arp reply matched with offered address, declining\n"));
834  dhcp_decline(netif);
835  }
836  }
837 }
838 
839 /**
840  * Decline an offered lease.
841  *
842  * Tell the DHCP server we do not accept the offered address.
843  * One reason to decline the lease is when we find out the address
844  * is already in use by another host (through ARP).
845  *
846  * @param netif the netif under DHCP control
847  */
849 dhcp_decline(struct netif *netif)
850 {
851  struct dhcp *dhcp = netif->dhcp;
852  err_t result = ERR_OK;
853  u16_t msecs;
854  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline()\n"));
855  dhcp_set_state(dhcp, DHCP_BACKING_OFF);
856  /* create and initialize the DHCP message header */
857  result = dhcp_create_msg(netif, dhcp, DHCP_DECLINE);
858  if (result == ERR_OK) {
859  dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
860  dhcp_option_long(dhcp, ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
861 
862  dhcp_option_trailer(dhcp);
863  /* resize pbuf to reflect true size of options */
864  pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
865 
866  /* per section 4.4.4, broadcast DECLINE messages */
867  udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
868  dhcp_delete_msg(dhcp);
869  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_decline: BACKING OFF\n"));
870  } else {
872  ("dhcp_decline: could not allocate DHCP request\n"));
873  }
874  dhcp->tries++;
875  msecs = 10*1000;
876  dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
877  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline(): set request timeout %"U16_F" msecs\n", msecs));
878  return result;
879 }
880 #endif /* DHCP_DOES_ARP_CHECK */
881 
882 
883 /**
884  * Start the DHCP process, discover a DHCP server.
885  *
886  * @param netif the netif under DHCP control
887  */
889 dhcp_discover(struct netif *netif)
890 {
891  struct dhcp *dhcp = netif->dhcp;
892  err_t result = ERR_OK;
893  u16_t msecs;
894  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover()\n"));
895  ip_addr_set_any(&dhcp->offered_ip_addr);
896  dhcp_set_state(dhcp, DHCP_SELECTING);
897  /* create and initialize the DHCP message header */
898  result = dhcp_create_msg(netif, dhcp, DHCP_DISCOVER);
899  if (result == ERR_OK) {
900  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: making request\n"));
901 
902  dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
903  dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
904 
905 #if LWIP_NETIF_HOSTNAME
906  if (netif->hostname != NULL) {
907  const char *p = (const char*)netif->hostname;
908  u8_t namelen = (u8_t)os_strlen(p);
909  if (namelen > 0) {
910  LWIP_ASSERT("DHCP: hostname is too long!", namelen < 255);
911  dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, namelen);
912  while (*p) {
913  dhcp_option_byte(dhcp, *p++);
914  }
915  }
916  }
917 #endif /* LWIP_NETIF_HOSTNAME */
918  dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, 12/*num options*/);
919  dhcp_option_byte(dhcp, DHCP_OPTION_SUBNET_MASK);
920  dhcp_option_byte(dhcp, DHCP_OPTION_ROUTER);
921  dhcp_option_byte(dhcp, DHCP_OPTION_BROADCAST);
922  dhcp_option_byte(dhcp, DHCP_OPTION_DNS_SERVER);
923  dhcp_option_byte(dhcp, DHCP_OPTION_DOMAIN_NAME);
924  dhcp_option_byte(dhcp, DHCP_OPTION_NB_TINS);
925  dhcp_option_byte(dhcp, DHCP_OPTION_NB_TINT);
926  dhcp_option_byte(dhcp, DHCP_OPTION_NB_TIS);
927  dhcp_option_byte(dhcp, DHCP_OPTION_PRD);
928  dhcp_option_byte(dhcp, DHCP_OPTION_STATIC_ROUTER);
929  dhcp_option_byte(dhcp, DHCP_OPTION_CLASSLESS_STATIC_ROUTER);
930  dhcp_option_byte(dhcp, DHCP_OPTION_VSN);
931 
932  dhcp_option_trailer(dhcp);
933 
934  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: realloc()ing\n"));
935  pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
936 
937  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: sendto(DISCOVER, IP_ADDR_BROADCAST, DHCP_SERVER_PORT)\n"));
938  udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
939  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_discover: deleting()ing\n"));
940  dhcp_delete_msg(dhcp);
941  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover: SELECTING\n"));
942  } else {
943  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_discover: could not allocate DHCP request\n"));
944  }
945  dhcp->tries++;
946 #if LWIP_DHCP_AUTOIP_COOP
947  if(dhcp->tries >= LWIP_DHCP_AUTOIP_COOP_TRIES && dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_OFF) {
948  dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_ON;
949  autoip_start(netif);
950  }
951 #endif /* LWIP_DHCP_AUTOIP_COOP */
952  msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000;
953  dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
954  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_discover(): set request timeout %"U16_F" msecs\n", msecs));
955  return result;
956 }
957 
958 
959 /**
960  * Bind the interface to the offered IP address.
961  *
962  * @param netif network interface to bind to the offered address
963  */
964 static void ICACHE_FLASH_ATTR
965 dhcp_bind(struct netif *netif)
966 {
967  u32_t timeout;
968  struct dhcp *dhcp;
969  ip_addr_t sn_mask, gw_addr;
970  LWIP_ERROR("dhcp_bind: netif != NULL", (netif != NULL), return;);
971  dhcp = netif->dhcp;
972  LWIP_ERROR("dhcp_bind: dhcp != NULL", (dhcp != NULL), return;);
973  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num));
974 
975  /* temporary DHCP lease? */
976  if (dhcp->offered_t1_renew != 0xffffffffUL) {
977  /* set renewal period timer */
978  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t1 renewal timer %"U32_F" secs\n", dhcp->offered_t1_renew));
979  timeout = (dhcp->offered_t1_renew + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
980  if(timeout > 0xffff) {
981  timeout = 0xffff;
982  }
983  dhcp->t1_timeout = (u16_t)timeout;
984  if (dhcp->t1_timeout == 0) {
985  dhcp->t1_timeout = 1;
986  }
987  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t1_renew*1000));
988  }
989  /* set renewal period timer */
990  if (dhcp->offered_t2_rebind != 0xffffffffUL) {
991  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_bind(): t2 rebind timer %"U32_F" secs\n", dhcp->offered_t2_rebind));
992  timeout = (dhcp->offered_t2_rebind + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
993  if(timeout > 0xffff) {
994  timeout = 0xffff;
995  }
996  dhcp->t2_timeout = (u16_t)timeout;
997  if (dhcp->t2_timeout == 0) {
998  dhcp->t2_timeout = 1;
999  }
1000  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_bind(): set request timeout %"U32_F" msecs\n", dhcp->offered_t2_rebind*1000));
1001  }
1002 
1003  /* If we have sub 1 minute lease, t2 and t1 will kick in at the same time. modify by ives at 2014.4.22*/
1004  if ((dhcp->t1_timeout >= dhcp->t2_timeout) && (dhcp->t2_timeout > 0)) {
1005  dhcp->t1_timeout = 0;
1006  }
1007 
1008  if (dhcp->subnet_mask_given) {
1009  /* copy offered network mask */
1010  ip_addr_copy(sn_mask, dhcp->offered_sn_mask);
1011  } else {
1012  /* subnet mask not given, choose a safe subnet mask given the network class */
1013  u8_t first_octet = ip4_addr1(&dhcp->offered_ip_addr);
1014  if (first_octet <= 127) {
1015  ip4_addr_set_u32(&sn_mask, PP_HTONL(0xff000000));
1016  } else if (first_octet >= 192) {
1017  ip4_addr_set_u32(&sn_mask, PP_HTONL(0xffffff00));
1018  } else {
1019  ip4_addr_set_u32(&sn_mask, PP_HTONL(0xffff0000));
1020  }
1021  }
1022 
1023  ip_addr_copy(gw_addr, dhcp->offered_gw_addr);
1024  /* gateway address not given? */
1025  if (ip_addr_isany(&gw_addr)) {
1026  /* copy network address */
1027  ip_addr_get_network(&gw_addr, &dhcp->offered_ip_addr, &sn_mask);
1028  /* use first host address on network as gateway */
1029  ip4_addr_set_u32(&gw_addr, ip4_addr_get_u32(&gw_addr) | PP_HTONL(0x00000001));
1030  }
1031 
1032 #if LWIP_DHCP_AUTOIP_COOP
1033  if(dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
1034  autoip_stop(netif);
1035  dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
1036  }
1037 #endif /* LWIP_DHCP_AUTOIP_COOP */
1038 
1039  // wjg:back up old ip/netmask/gw
1040  ip_addr_t ip, mask, gw;
1041  ip = netif->ip_addr;
1042  mask = netif->netmask;
1043  gw = netif->gw;
1044 
1045  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): IP: 0x%08"X32_F"\n",
1046  ip4_addr_get_u32(&dhcp->offered_ip_addr)));
1047  netif_set_ipaddr(netif, &dhcp->offered_ip_addr);
1048  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): SN: 0x%08"X32_F"\n",
1049  ip4_addr_get_u32(&sn_mask)));
1050  netif_set_netmask(netif, &sn_mask);
1051  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_bind(): GW: 0x%08"X32_F"\n",
1052  ip4_addr_get_u32(&gw_addr)));
1053  netif_set_gw(netif, &gw_addr);
1054 
1055  /* bring the interface up */
1056  netif_set_up(netif);
1057 
1058  // wjg: use old ip/mask/gw to check whether ip/mask/gw changed
1059  system_station_got_ip_set(&ip, &mask, &gw);
1060 
1061  /* netif is now bound to DHCP leased address */
1062  dhcp_set_state(dhcp, DHCP_BOUND);
1063 }
1064 
1065 /**
1066  * Renew an existing DHCP lease at the involved DHCP server.
1067  *
1068  * @param netif network interface which must renew its lease
1069  */
1071 dhcp_renew(struct netif *netif)
1072 {
1073  struct dhcp *dhcp = netif->dhcp;
1074  err_t result;
1075  u16_t msecs;
1076  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_renew()\n"));
1077  dhcp_set_state(dhcp, DHCP_RENEWING);
1078 
1079  /* create and initialize the DHCP message header */
1080  result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
1081  if (result == ERR_OK) {
1082  dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1083  dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
1084 
1085 #if LWIP_NETIF_HOSTNAME
1086  if (netif->hostname != NULL) {
1087  const char *p = (const char*)netif->hostname;
1088  u8_t namelen = (u8_t)os_strlen(p);
1089  if (namelen > 0) {
1090  LWIP_ASSERT("DHCP: hostname is too long!", namelen < 255);
1091  dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, namelen);
1092  while (*p) {
1093  dhcp_option_byte(dhcp, *p++);
1094  }
1095  }
1096  }
1097 #endif /* LWIP_NETIF_HOSTNAME */
1098 
1099 #if 0
1100  dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
1101  dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
1102 #endif
1103 
1104 #if 0
1105  dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
1106  dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));
1107 #endif
1108  /* append DHCP message trailer */
1109  dhcp_option_trailer(dhcp);
1110 
1111  pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1112 
1113  udp_sendto_if(dhcp->pcb, dhcp->p_out, &dhcp->server_ip_addr, DHCP_SERVER_PORT, netif);
1114  dhcp_delete_msg(dhcp);
1115 
1116  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew: RENEWING\n"));
1117  } else {
1118  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_renew: could not allocate DHCP request\n"));
1119  }
1120  dhcp->tries++;
1121  /* back-off on retries, but to a maximum of 20 seconds */
1122  msecs = dhcp->tries < 10 ? dhcp->tries * 2000 : 20 * 1000;
1123  dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1124  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_renew(): set request timeout %"U16_F" msecs\n", msecs));
1125  return result;
1126 }
1127 
1128 /**
1129  * Rebind with a DHCP server for an existing DHCP lease.
1130  *
1131  * @param netif network interface which must rebind with a DHCP server
1132  */
1133 static err_t ICACHE_FLASH_ATTR
1134 dhcp_rebind(struct netif *netif)
1135 {
1136  struct dhcp *dhcp = netif->dhcp;
1137  err_t result;
1138  u16_t msecs;
1139  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind()\n"));
1140  dhcp_set_state(dhcp, DHCP_REBINDING);
1141 
1142  /* create and initialize the DHCP message header */
1143  result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
1144  if (result == ERR_OK) {
1145  dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1146  dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
1147 
1148 #if LWIP_NETIF_HOSTNAME
1149  if (netif->hostname != NULL) {
1150  const char *p = (const char*)netif->hostname;
1151  u8_t namelen = (u8_t)os_strlen(p);
1152  if (namelen > 0) {
1153  LWIP_ASSERT("DHCP: hostname is too long!", namelen < 255);
1154  dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, namelen);
1155  while (*p) {
1156  dhcp_option_byte(dhcp, *p++);
1157  }
1158  }
1159  }
1160 #endif /* LWIP_NETIF_HOSTNAME */
1161 
1162 #if 0
1163  dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
1164  dhcp_option_long(dhcp, ntohl(dhcp->offered_ip_addr.addr));
1165 
1166  dhcp_option(dhcp, DHCP_OPTION_SERVER_ID, 4);
1167  dhcp_option_long(dhcp, ntohl(dhcp->server_ip_addr.addr));
1168 #endif
1169 
1170  dhcp_option_trailer(dhcp);
1171 
1172  pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1173 
1174  /* broadcast to server */
1175  udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
1176  dhcp_delete_msg(dhcp);
1177  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind: REBINDING\n"));
1178  } else {
1179  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_rebind: could not allocate DHCP request\n"));
1180  }
1181  dhcp->tries++;
1182  msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
1183  dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1184  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_rebind(): set request timeout %"U16_F" msecs\n", msecs));
1185  return result;
1186 }
1187 
1188 /**
1189  * Enter REBOOTING state to verify an existing lease
1190  *
1191  * @param netif network interface which must reboot
1192  */
1193 static err_t ICACHE_FLASH_ATTR
1194 dhcp_reboot(struct netif *netif)
1195 {
1196  struct dhcp *dhcp = netif->dhcp;
1197  err_t result;
1198  u16_t msecs;
1199  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot()\n"));
1200  dhcp_set_state(dhcp, DHCP_REBOOTING);
1201 
1202  /* create and initialize the DHCP message header */
1203  result = dhcp_create_msg(netif, dhcp, DHCP_REQUEST);
1204  if (result == ERR_OK) {
1205  dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1206  dhcp_option_short(dhcp, 576);
1207 
1208  dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
1209  dhcp_option_long(dhcp, ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
1210 
1211  dhcp_option_trailer(dhcp);
1212 
1213  pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1214 
1215  /* broadcast to server */
1216  udp_sendto_if(dhcp->pcb, dhcp->p_out, IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
1217  dhcp_delete_msg(dhcp);
1218  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot: REBOOTING\n"));
1219  } else {
1220  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_reboot: could not allocate DHCP request\n"));
1221  }
1222  dhcp->tries++;
1223  msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
1224  dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1225  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_reboot(): set request timeout %"U16_F" msecs\n", msecs));
1226  return result;
1227 }
1228 
1229 
1230 /**
1231  * Release a DHCP lease.
1232  *
1233  * @param netif network interface which must release its lease
1234  */
1236 dhcp_release(struct netif *netif)
1237 {
1238  struct dhcp *dhcp = netif->dhcp;
1239  err_t result;
1240  u16_t msecs;
1241  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_release()\n"));
1242  if (dhcp == NULL) {
1243  return ERR_ARG;
1244  }
1245 
1246  /* idle DHCP client */
1247  dhcp_set_state(dhcp, DHCP_OFF);
1248  /* clean old DHCP offer */
1249  ip_addr_set_zero(&dhcp->server_ip_addr);
1250  ip_addr_set_zero(&dhcp->offered_ip_addr);
1251  ip_addr_set_zero(&dhcp->offered_sn_mask);
1252  ip_addr_set_zero(&dhcp->offered_gw_addr);
1253 #if LWIP_DHCP_BOOTP_FILE
1254  ip_addr_set_zero(&dhcp->offered_si_addr);
1255 #endif /* LWIP_DHCP_BOOTP_FILE */
1256  dhcp->offered_t0_lease = dhcp->offered_t1_renew = dhcp->offered_t2_rebind = 0;
1257 
1258  /* create and initialize the DHCP message header */
1259  result = dhcp_create_msg(netif, dhcp, DHCP_RELEASE);
1260  if (result == ERR_OK) {
1261  dhcp_option_trailer(dhcp);
1262 
1263  pbuf_realloc(dhcp->p_out, sizeof(struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1264 
1265  udp_sendto_if(dhcp->pcb, dhcp->p_out, &dhcp->server_ip_addr, DHCP_SERVER_PORT, netif);
1266  dhcp_delete_msg(dhcp);
1267  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_release: RELEASED, DHCP_OFF\n"));
1268  } else {
1269  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("dhcp_release: could not allocate DHCP request\n"));
1270  }
1271  dhcp->tries++;
1272  msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
1273  dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1274  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_release(): set request timeout %"U16_F" msecs\n", msecs));
1275  /* bring the interface down */
1276  netif_set_down(netif);
1277  /* remove IP address from interface */
1278  netif_set_ipaddr(netif, IP_ADDR_ANY);
1279  netif_set_gw(netif, IP_ADDR_ANY);
1281 
1282  return result;
1283 }
1284 
1285 /**
1286  * Remove the DHCP client from the interface.
1287  *
1288  * @param netif The network interface to stop DHCP on
1289  */
1290 void ICACHE_FLASH_ATTR
1291 dhcp_stop(struct netif *netif)
1292 {
1293  struct dhcp *dhcp;
1294  LWIP_ERROR("dhcp_stop: netif != NULL", (netif != NULL), return;);
1295  dhcp = netif->dhcp;
1296  /* Remove the flag that says this netif is handled by DHCP. */
1297  netif->flags &= ~NETIF_FLAG_DHCP;
1298  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_stop()\n"));
1299  /* netif is DHCP configured? */
1300  if (dhcp != NULL) {
1301 #if LWIP_DHCP_AUTOIP_COOP
1302  if(dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
1303  autoip_stop(netif);
1304  dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
1305  }
1306 #endif /* LWIP_DHCP_AUTOIP_COOP */
1307 
1308  if (dhcp->pcb != NULL) {
1309  udp_remove(dhcp->pcb);
1310  dhcp->pcb = NULL;
1311  }
1312  LWIP_ASSERT("reply wasn't freed", dhcp->msg_in == NULL);
1313  dhcp_set_state(dhcp, DHCP_OFF);
1314  }
1315 }
1316 
1317 /*
1318  * Set the DHCP state of a DHCP client.
1319  *
1320  * If the state changed, reset the number of tries.
1321  */
1322 static void ICACHE_FLASH_ATTR
1323 dhcp_set_state(struct dhcp *dhcp, u8_t new_state)
1324 {
1325  if (new_state != dhcp->state) {
1326  dhcp->state = new_state;
1327  dhcp->tries = 0;
1328  dhcp->request_timeout = 0;
1329  }
1330 }
1331 
1332 /*
1333  * Concatenate an option type and length field to the outgoing
1334  * DHCP message.
1335  *
1336  */
1337 static void ICACHE_FLASH_ATTR
1338 dhcp_option(struct dhcp *dhcp, u8_t option_type, u8_t option_len)
1339 {
1340  LWIP_ASSERT("dhcp_option: dhcp->options_out_len + 2 + option_len <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U + option_len <= DHCP_OPTIONS_LEN);
1341  dhcp->msg_out->options[dhcp->options_out_len++] = option_type;
1342  dhcp->msg_out->options[dhcp->options_out_len++] = option_len;
1343 }
1344 /*
1345  * Concatenate a single byte to the outgoing DHCP message.
1346  *
1347  */
1348 static void ICACHE_FLASH_ATTR
1349 dhcp_option_byte(struct dhcp *dhcp, u8_t value)
1350 {
1351  LWIP_ASSERT("dhcp_option_byte: dhcp->options_out_len < DHCP_OPTIONS_LEN", dhcp->options_out_len < DHCP_OPTIONS_LEN);
1352  dhcp->msg_out->options[dhcp->options_out_len++] = value;
1353 }
1354 
1355 static void ICACHE_FLASH_ATTR
1356 dhcp_option_short(struct dhcp *dhcp, u16_t value)
1357 {
1358  LWIP_ASSERT("dhcp_option_short: dhcp->options_out_len + 2 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U <= DHCP_OPTIONS_LEN);
1359  dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0xff00U) >> 8);
1360  dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t) (value & 0x00ffU);
1361 }
1362 
1363 static void ICACHE_FLASH_ATTR
1364 dhcp_option_long(struct dhcp *dhcp, u32_t value)
1365 {
1366  LWIP_ASSERT("dhcp_option_long: dhcp->options_out_len + 4 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 4U <= DHCP_OPTIONS_LEN);
1367  dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0xff000000UL) >> 24);
1368  dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x00ff0000UL) >> 16);
1369  dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x0000ff00UL) >> 8);
1370  dhcp->msg_out->options[dhcp->options_out_len++] = (u8_t)((value & 0x000000ffUL));
1371 }
1372 
1373 /**
1374  * Extract the DHCP message and the DHCP options.
1375  *
1376  * Extract the DHCP message and the DHCP options, each into a contiguous
1377  * piece of memory. As a DHCP message is variable sized by its options,
1378  * and also allows overriding some fields for options, the easy approach
1379  * is to first unfold the options into a conitguous piece of memory, and
1380  * use that further on.
1381  *
1382  */
1383 static err_t ICACHE_FLASH_ATTR
1384 dhcp_parse_reply(struct dhcp *dhcp, struct pbuf *p)
1385 {
1386  u8_t *options;
1387  u16_t offset;
1388  u16_t offset_max;
1389  u16_t options_idx;
1390  u16_t options_idx_max;
1391  struct pbuf *q;
1392  int parse_file_as_options = 0;
1393  int parse_sname_as_options = 0;
1394 
1395  /* clear received options */
1396  dhcp_clear_all_options(dhcp);
1397  /* check that beginning of dhcp_msg (up to and including chaddr) is in first pbuf */
1398  if (p->len < DHCP_SNAME_OFS) {
1399  return ERR_BUF;
1400  }
1401  dhcp->msg_in = (struct dhcp_msg *)p->payload;
1402 #if LWIP_DHCP_BOOTP_FILE
1403  /* clear boot file name */
1404  dhcp->boot_file_name[0] = 0;
1405 #endif /* LWIP_DHCP_BOOTP_FILE */
1406 
1407  /* parse options */
1408 
1409  /* start with options field */
1410  options_idx = DHCP_OPTIONS_OFS;
1411  /* parse options to the end of the received packet */
1412  options_idx_max = p->tot_len;
1413 again:
1414  q = p;
1415  while((q != NULL) && (options_idx >= q->len)) {
1416  options_idx -= q->len;
1417  options_idx_max -= q->len;
1418  q = q->next;
1419  }
1420  if (q == NULL) {
1421  return ERR_BUF;
1422  }
1423  offset = options_idx;
1424  offset_max = options_idx_max;
1425  options = (u8_t*)q->payload;
1426  /* at least 1 byte to read and no end marker, then at least 3 bytes to read? */
1427  while((q != NULL) && (options[offset] != DHCP_OPTION_END) && (offset < offset_max)) {
1428  u8_t op = options[offset];
1429  u8_t len;
1430  u8_t decode_len = 0;
1431  int decode_idx = -1;
1432  u16_t val_offset = offset + 2;
1433  /* len byte might be in the next pbuf */
1434  if (offset + 1 < q->len) {
1435  len = options[offset + 1];
1436  } else {
1437  len = (q->next != NULL ? ((u8_t*)q->next->payload)[0] : 0);
1438  }
1439  /* LWIP_DEBUGF(DHCP_DEBUG, ("msg_offset=%"U16_F", q->len=%"U16_F, msg_offset, q->len)); */
1440  decode_len = len;
1441  switch(op) {
1442  /* case(DHCP_OPTION_END): handled above */
1443  case(DHCP_OPTION_PAD):
1444  /* special option: no len encoded */
1445  decode_len = len = 0;
1446  /* will be increased below */
1447  offset--;
1448  break;
1450  LWIP_ASSERT("len == 4", len == 4);
1451  decode_idx = DHCP_OPTION_IDX_SUBNET_MASK;
1452  break;
1453  case(DHCP_OPTION_ROUTER):
1454  decode_len = 4; /* only copy the first given router */
1455  LWIP_ASSERT("len >= decode_len", len >= decode_len);
1456  decode_idx = DHCP_OPTION_IDX_ROUTER;
1457  break;
1458  case(DHCP_OPTION_DNS_SERVER):
1459  /* special case: there might be more than one server */
1460  LWIP_ASSERT("len % 4 == 0", len % 4 == 0);
1461  /* limit number of DNS servers */
1462  decode_len = LWIP_MIN(len, 4 * DNS_MAX_SERVERS);
1463  LWIP_ASSERT("len >= decode_len", len >= decode_len);
1464  decode_idx = DHCP_OPTION_IDX_DNS_SERVER;
1465  break;
1466  case(DHCP_OPTION_LEASE_TIME):
1467  LWIP_ASSERT("len == 4", len == 4);
1468  decode_idx = DHCP_OPTION_IDX_LEASE_TIME;
1469  break;
1470  case(DHCP_OPTION_OVERLOAD):
1471  LWIP_ASSERT("len == 1", len == 1);
1472  decode_idx = DHCP_OPTION_IDX_OVERLOAD;
1473  break;
1474  case(DHCP_OPTION_MESSAGE_TYPE):
1475  LWIP_ASSERT("len == 1", len == 1);
1476  decode_idx = DHCP_OPTION_IDX_MSG_TYPE;
1477  break;
1478  case(DHCP_OPTION_SERVER_ID):
1479  LWIP_ASSERT("len == 4", len == 4);
1480  decode_idx = DHCP_OPTION_IDX_SERVER_ID;
1481  break;
1482  case(DHCP_OPTION_T1):
1483  LWIP_ASSERT("len == 4", len == 4);
1484  decode_idx = DHCP_OPTION_IDX_T1;
1485  break;
1486  case(DHCP_OPTION_T2):
1487  LWIP_ASSERT("len == 4", len == 4);
1488  decode_idx = DHCP_OPTION_IDX_T2;
1489  break;
1490  default:
1491  decode_len = 0;
1492  LWIP_DEBUGF(DHCP_DEBUG, ("skipping option %"U16_F" in options\n", op));
1493  break;
1494  }
1495  offset += len + 2;
1496  if (decode_len > 0) {
1497  u32_t value = 0;
1498  u16_t copy_len;
1499 decode_next:
1500  LWIP_ASSERT("check decode_idx", decode_idx >= 0 && decode_idx < DHCP_OPTION_IDX_MAX);
1501  LWIP_ASSERT("option already decoded", !dhcp_option_given(dhcp, decode_idx));
1502  copy_len = LWIP_MIN(decode_len, 4);
1503  pbuf_copy_partial(q, &value, copy_len, val_offset);
1504  if (decode_len > 4) {
1505  /* decode more than one u32_t */
1506  LWIP_ASSERT("decode_len % 4 == 0", decode_len % 4 == 0);
1507  dhcp_got_option(dhcp, decode_idx);
1508  dhcp_set_option_value(dhcp, decode_idx, htonl(value));
1509  decode_len -= 4;
1510  val_offset += 4;
1511  decode_idx++;
1512  goto decode_next;
1513  } else if (decode_len == 4) {
1514  value = ntohl(value);
1515  } else {
1516  LWIP_ASSERT("invalid decode_len", decode_len == 1);
1517  value = ((u8_t*)&value)[0];
1518  }
1519  dhcp_got_option(dhcp, decode_idx);
1520  dhcp_set_option_value(dhcp, decode_idx, value);
1521  }
1522  if (offset >= q->len) {
1523  offset -= q->len;
1524  offset_max -= q->len;
1525  if ((offset < offset_max) && offset_max) { //modify by ives at 2014.4.22
1526  q = q->next;
1527  LWIP_ASSERT("next pbuf was null", q);
1528  options = (u8_t*)q->payload;
1529  } else {
1530  /* We've run out of bytes, probably no end marker. Don't proceed. */
1531  break;
1532  }
1533  }
1534  }
1535  /* is this an overloaded message? */
1536  if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_OVERLOAD)) {
1537  u32_t overload = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_OVERLOAD);
1538  dhcp_clear_option(dhcp, DHCP_OPTION_IDX_OVERLOAD);
1539  if (overload == DHCP_OVERLOAD_FILE) {
1540  parse_file_as_options = 1;
1541  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("overloaded file field\n"));
1542  } else if (overload == DHCP_OVERLOAD_SNAME) {
1543  parse_sname_as_options = 1;
1544  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("overloaded sname field\n"));
1545  } else if (overload == DHCP_OVERLOAD_SNAME_FILE) {
1546  parse_sname_as_options = 1;
1547  parse_file_as_options = 1;
1548  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("overloaded sname and file field\n"));
1549  } else {
1550  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("invalid overload option: %d\n", (int)overload));
1551  }
1552 #if LWIP_DHCP_BOOTP_FILE
1553  if (!parse_file_as_options) {
1554  /* only do this for ACK messages */
1555  if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE) &&
1556  (dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE) == DHCP_ACK))
1557  /* copy bootp file name, don't care for sname (server hostname) */
1558  pbuf_copy_partial(p, dhcp->boot_file_name, DHCP_FILE_LEN-1, DHCP_FILE_OFS);
1559  /* make sure the string is really NULL-terminated */
1560  dhcp->boot_file_name[DHCP_FILE_LEN-1] = 0;
1561  }
1562 #endif /* LWIP_DHCP_BOOTP_FILE */
1563  }
1564  if (parse_file_as_options) {
1565  /* if both are overloaded, parse file first and then sname (RFC 2131 ch. 4.1) */
1566  parse_file_as_options = 0;
1567  options_idx = DHCP_FILE_OFS;
1568  options_idx_max = DHCP_FILE_OFS + DHCP_FILE_LEN;
1569  goto again;
1570  } else if (parse_sname_as_options) {
1571  parse_sname_as_options = 0;
1572  options_idx = DHCP_SNAME_OFS;
1573  options_idx_max = DHCP_SNAME_OFS + DHCP_SNAME_LEN;
1574  goto again;
1575  }
1576  return ERR_OK;
1577 }
1578 
1579 /**
1580  * If an incoming DHCP message is in response to us, then trigger the state machine
1581  */
1582 static void ICACHE_FLASH_ATTR
1583 dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
1584 {
1585  struct netif *netif = (struct netif *)arg;
1586  struct dhcp *dhcp = netif->dhcp;
1587  struct dhcp_msg *reply_msg = (struct dhcp_msg *)p->payload;
1588  u8_t msg_type;
1589  u8_t i;
1590  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_recv(pbuf = %p) from DHCP server %"U16_F".%"U16_F".%"U16_F".%"U16_F" port %"U16_F"\n", (void*)p,
1591  ip4_addr1_16(addr), ip4_addr2_16(addr), ip4_addr3_16(addr), ip4_addr4_16(addr), port));
1592  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->len = %"U16_F"\n", p->len));
1593  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->tot_len = %"U16_F"\n", p->tot_len));
1594  /* prevent warnings about unused arguments */
1595  LWIP_UNUSED_ARG(pcb);
1596  LWIP_UNUSED_ARG(addr);
1597  LWIP_UNUSED_ARG(port);
1598 
1599  LWIP_ASSERT("reply wasn't freed", dhcp->msg_in == NULL);
1600 
1601  if (p->len < DHCP_MIN_REPLY_LEN) {
1602  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("DHCP reply message or pbuf too short\n"));
1603  goto free_pbuf_and_return;
1604  }
1605 
1606  if (reply_msg->op != DHCP_BOOTREPLY) {
1607  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("not a DHCP reply message, but type %"U16_F"\n", (u16_t)reply_msg->op));
1608  goto free_pbuf_and_return;
1609  }
1610  /* iterate through hardware address and match against DHCP message */
1611  for (i = 0; i < netif->hwaddr_len; i++) {
1612  if (netif->hwaddr[i] != reply_msg->chaddr[i]) {
1614  ("netif->hwaddr[%"U16_F"]==%02"X16_F" != reply_msg->chaddr[%"U16_F"]==%02"X16_F"\n",
1615  (u16_t)i, (u16_t)netif->hwaddr[i], (u16_t)i, (u16_t)reply_msg->chaddr[i]));
1616  goto free_pbuf_and_return;
1617  }
1618  }
1619  /* match transaction ID against what we expected */
1620  if (ntohl(reply_msg->xid) != dhcp->xid) {
1622  ("transaction id mismatch reply_msg->xid(%"X32_F")!=dhcp->xid(%"X32_F")\n",ntohl(reply_msg->xid),dhcp->xid));
1623  goto free_pbuf_and_return;
1624  }
1625  /* option fields could be unfold? */
1626  if (dhcp_parse_reply(dhcp, p) != ERR_OK) {
1628  ("problem unfolding DHCP message - too short on memory?\n"));
1629  goto free_pbuf_and_return;
1630  }
1631 
1632  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("searching DHCP_OPTION_MESSAGE_TYPE\n"));
1633  /* obtain pointer to DHCP message type */
1634  if (!dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE)) {
1635  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_WARNING, ("DHCP_OPTION_MESSAGE_TYPE option not found\n"));
1636  goto free_pbuf_and_return;
1637  }
1638 
1639  /* read DHCP message type */
1640  msg_type = (u8_t)dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE);
1641  /* message type is DHCP ACK? */
1642  if (msg_type == DHCP_ACK) {
1643  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("DHCP_ACK received\n"));
1644  /* in requesting state? */
1645  if (dhcp->state == DHCP_REQUESTING) {
1646  dhcp_handle_ack(netif);
1647 #if DHCP_DOES_ARP_CHECK
1648  /* check if the acknowledged lease address is already in use */
1649  dhcp_check(netif);
1650 #else
1651  /* bind interface to the acknowledged lease address */
1652  dhcp_bind(netif);
1653 #endif
1654  }
1655  /* already bound to the given lease address? */
1656  else if ((dhcp->state == DHCP_REBOOTING) || (dhcp->state == DHCP_REBINDING) || (dhcp->state == DHCP_RENEWING)) {
1657  dhcp_bind(netif);
1658  }
1659  }
1660  /* received a DHCP_NAK in appropriate state? */
1661  else if ((msg_type == DHCP_NAK) &&
1662  ((dhcp->state == DHCP_REBOOTING) || (dhcp->state == DHCP_REQUESTING) ||
1663  (dhcp->state == DHCP_REBINDING) || (dhcp->state == DHCP_RENEWING ))) {
1664  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("DHCP_NAK received\n"));
1665  dhcp_handle_nak(netif);
1666  }
1667  /* received a DHCP_OFFER in DHCP_SELECTING state? */
1668  else if ((msg_type == DHCP_OFFER) && (dhcp->state == DHCP_SELECTING)) {
1669  LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("DHCP_OFFER received in DHCP_SELECTING state\n"));
1670  dhcp->request_timeout = 0;
1671  /* remember offered lease */
1672  dhcp_handle_offer(netif);
1673  }
1674 free_pbuf_and_return:
1675  dhcp->msg_in = NULL;
1676  pbuf_free(p);
1677 }
1678 
1679 /**
1680  * Create a DHCP request, fill in common headers
1681  *
1682  * @param netif the netif under DHCP control
1683  * @param dhcp dhcp control struct
1684  * @param message_type message type of the request
1685  */
1686 static err_t ICACHE_FLASH_ATTR
1687 dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type)
1688 {
1689  u16_t i;
1690 #ifndef DHCP_GLOBAL_XID
1691  /** default global transaction identifier starting value (easy to match
1692  * with a packet analyser). We simply increment for each new request.
1693  * Predefine DHCP_GLOBAL_XID to a better value or a function call to generate one
1694  * at runtime, any supporting function prototypes can be defined in DHCP_GLOBAL_XID_HEADER */
1695  static u32_t xid = 0xABCD0000;
1696 #else
1697  static u32_t xid;
1698  static u8_t xid_initialised = 0;
1699  if (!xid_initialised) {
1700  xid = DHCP_GLOBAL_XID;
1701  xid_initialised = !xid_initialised;
1702  }
1703 #endif
1704  LWIP_ERROR("dhcp_create_msg: netif != NULL", (netif != NULL), return ERR_ARG;);
1705  LWIP_ERROR("dhcp_create_msg: dhcp != NULL", (dhcp != NULL), return ERR_VAL;);
1706  LWIP_ASSERT("dhcp_create_msg: dhcp->p_out == NULL", dhcp->p_out == NULL);
1707  LWIP_ASSERT("dhcp_create_msg: dhcp->msg_out == NULL", dhcp->msg_out == NULL);
1708  dhcp->p_out = pbuf_alloc(PBUF_TRANSPORT, sizeof(struct dhcp_msg), PBUF_RAM);
1709  if (dhcp->p_out == NULL) {
1711  ("dhcp_create_msg(): could not allocate pbuf\n"));
1712  return ERR_MEM;
1713  }
1714  LWIP_ASSERT("dhcp_create_msg: check that first pbuf can hold struct dhcp_msg",
1715  (dhcp->p_out->len >= sizeof(struct dhcp_msg)));
1716 
1717  /* DHCP_REQUEST should reuse 'xid' from DHCPOFFER modify by ives at 2014.4.22*/
1718  if (message_type != DHCP_REQUEST) {
1719  /* reuse transaction identifier in retransmissions */
1720  if (dhcp->tries == 0) {
1721  xid++;
1722  }
1723  dhcp->xid = xid;
1724  }
1726  ("transaction id xid(%"X32_F")\n", xid));
1727 
1728  dhcp->msg_out = (struct dhcp_msg *)dhcp->p_out->payload;
1729 
1730  dhcp->msg_out->op = DHCP_BOOTREQUEST;
1731  /* TODO: make link layer independent */
1732  dhcp->msg_out->htype = DHCP_HTYPE_ETH;
1733  dhcp->msg_out->hlen = netif->hwaddr_len;
1734  dhcp->msg_out->hops = 0;
1735  dhcp->msg_out->xid = htonl(dhcp->xid);
1736  dhcp->msg_out->secs = 0;
1737  /* we don't need the broadcast flag since we can receive unicast traffic
1738  before being fully configured! */
1739  dhcp->msg_out->flags = 0;
1740  ip_addr_set_zero(&dhcp->msg_out->ciaddr);
1741  /* set ciaddr to netif->ip_addr based on message_type and state */
1742  if ((message_type == DHCP_INFORM) || (message_type == DHCP_DECLINE) ||
1743  ((message_type == DHCP_REQUEST) && /* DHCP_BOUND not used for sending! */
1744  ((dhcp->state==DHCP_RENEWING) || dhcp->state==DHCP_REBINDING))) {
1745  ip_addr_copy(dhcp->msg_out->ciaddr, netif->ip_addr);
1746  }
1747  ip_addr_set_zero(&dhcp->msg_out->yiaddr);
1748  ip_addr_set_zero(&dhcp->msg_out->siaddr);
1749  ip_addr_set_zero(&dhcp->msg_out->giaddr);
1750  for (i = 0; i < DHCP_CHADDR_LEN; i++) {
1751  /* copy netif hardware address, pad with zeroes */
1752  dhcp->msg_out->chaddr[i] = (i < netif->hwaddr_len && i < NETIF_MAX_HWADDR_LEN) ? netif->hwaddr[i] : 0/* pad byte*/;
1753  }
1754  for (i = 0; i < DHCP_SNAME_LEN; i++) {
1755  dhcp->msg_out->sname[i] = 0;
1756  }
1757  for (i = 0; i < DHCP_FILE_LEN; i++) {
1758  dhcp->msg_out->file[i] = 0;
1759  }
1760  dhcp->msg_out->cookie = PP_HTONL(DHCP_MAGIC_COOKIE);
1761  dhcp->options_out_len = 0;
1762  /* fill options field with an incrementing array (for debugging purposes) */
1763  for (i = 0; i < DHCP_OPTIONS_LEN; i++) {
1764  dhcp->msg_out->options[i] = (u8_t)i; /* for debugging only, no matter if truncated */
1765  }
1766  /* Add option MESSAGE_TYPE */
1767  dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
1768  dhcp_option_byte(dhcp, message_type);
1769  return ERR_OK;
1770 }
1771 
1772 /**
1773  * Free previously allocated memory used to send a DHCP request.
1774  *
1775  * @param dhcp the dhcp struct to free the request from
1776  */
1777 static void ICACHE_FLASH_ATTR
1778 dhcp_delete_msg(struct dhcp *dhcp)
1779 {
1780  LWIP_ERROR("dhcp_delete_msg: dhcp != NULL", (dhcp != NULL), return;);
1781  LWIP_ASSERT("dhcp_delete_msg: dhcp->p_out != NULL", dhcp->p_out != NULL);
1782  LWIP_ASSERT("dhcp_delete_msg: dhcp->msg_out != NULL", dhcp->msg_out != NULL);
1783  if (dhcp->p_out != NULL) {
1784  pbuf_free(dhcp->p_out);
1785  }
1786  dhcp->p_out = NULL;
1787  dhcp->msg_out = NULL;
1788 }
1789 
1790 /**
1791  * Add a DHCP message trailer
1792  *
1793  * Adds the END option to the DHCP message, and if
1794  * necessary, up to three padding bytes.
1795  *
1796  * @param dhcp DHCP state structure
1797  */
1798 static void ICACHE_FLASH_ATTR
1799 dhcp_option_trailer(struct dhcp *dhcp)
1800 {
1801  LWIP_ERROR("dhcp_option_trailer: dhcp != NULL", (dhcp != NULL), return;);
1802  LWIP_ASSERT("dhcp_option_trailer: dhcp->msg_out != NULL\n", dhcp->msg_out != NULL);
1803  LWIP_ASSERT("dhcp_option_trailer: dhcp->options_out_len < DHCP_OPTIONS_LEN\n", dhcp->options_out_len < DHCP_OPTIONS_LEN);
1804  dhcp->msg_out->options[dhcp->options_out_len++] = DHCP_OPTION_END;
1805  /* packet is too small, or not 4 byte aligned? */
1806  while (((dhcp->options_out_len < DHCP_MIN_OPTIONS_LEN) || (dhcp->options_out_len & 3)) &&
1807  (dhcp->options_out_len < DHCP_OPTIONS_LEN)) {
1808  /* LWIP_DEBUGF(DHCP_DEBUG,("dhcp_option_trailer:dhcp->options_out_len=%"U16_F", DHCP_OPTIONS_LEN=%"U16_F, dhcp->options_out_len, DHCP_OPTIONS_LEN)); */
1809  LWIP_ASSERT("dhcp_option_trailer: dhcp->options_out_len < DHCP_OPTIONS_LEN\n", dhcp->options_out_len < DHCP_OPTIONS_LEN);
1810  /* add a fill/padding byte */
1811  dhcp->msg_out->options[dhcp->options_out_len++] = 0;
1812  }
1813 }
1814 
1815 #endif /* LWIP_DHCP */
#define ip4_addr_get_u32(src_ipaddr)
Definition: ip_addr.h:181
#define PP_HTONL(x)
Definition: def.h:94
u8_t hwaddr[NETIF_MAX_HWADDR_LEN]
Definition: netif.h:191
#define LWIP_DHCP_AUTOIP_COOP_TRIES
Definition: opt.h:700
u16_t tot_len
Definition: pbuf.h:90
#define ip4_addr3_16(ipaddr)
Definition: ip_addr.h:228
struct pbuf * next
Definition: pbuf.h:78
u16_t len
Definition: pbuf.h:93
#define U16_F
Definition: cc.h:61
signed short s16_t
Definition: cc.h:55
struct netif * netif_list
Definition: netif.c:75
#define ERR_VAL
Definition: err.h:58
#define NULL
Definition: def.h:47
#define LWIP_DBG_TRACE
Definition: debug.h:56
const ip_addr_t ip_addr_any ICACHE_RODATA_ATTR
Definition: ip_addr.c:44
#define ICACHE_FLASH_ATTR
Definition: c_types.h:99
void * mem_malloc(mem_size_t size) ICACHE_FLASH_ATTR
Definition: mem.c:493
void netif_set_up(struct netif *netif) ICACHE_FLASH_ATTR
Definition: netif.c:454
void netif_set_netmask(struct netif *netif, ip_addr_t *netmask) ICACHE_FLASH_ATTR
Definition: netif.c:410
Definition: pbuf.h:58
#define LWIP_ERROR(message, expression, handler)
Definition: debug.h:73
#define ip_addr_get_network(target, host, netmask)
Definition: ip_addr.h:184
void netif_set_gw(struct netif *netif, ip_addr_t *gw) ICACHE_FLASH_ATTR
Definition: netif.c:389
#define NETIF_MAX_HWADDR_LEN
Definition: netif.h:61
u8_t hwaddr_len
Definition: netif.h:189
#define os_printf
Definition: osapi.h:62
#define LWIP_MIN(x, y)
Definition: def.h:44
#define DHCP_OPTION_END
Definition: dhcpserver.h:83
#define DNS_MAX_SERVERS
Definition: opt.h:813
void netif_set_ipaddr(struct netif *netif, ip_addr_t *ipaddr) ICACHE_FLASH_ATTR
Definition: netif.c:324
#define U32_F
Definition: cc.h:65
#define ntohl(x)
Definition: def.h:84
#define NETIF_FLAG_DHCP
Definition: netif.h:78
#define DHCP_REQUEST
Definition: dhcpserver.h:55
ip_addr_t gw
Definition: netif.h:146
#define os_strlen
Definition: osapi.h:43
unsigned long u32_t
Definition: cc.h:56
#define NETIF_FLAG_ETHARP
Definition: netif.h:88
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:94
#define os_memset
Definition: osapi.h:38
#define DHCP_OPTION_LEASE_TIME
Definition: dhcpserver.h:76
#define ip_addr_cmp(addr1, addr2)
Definition: ip_addr.h:198
#define ip_addr_copy(dest, src)
Definition: ip_addr.h:162
#define LWIP_DBG_LEVEL_SERIOUS
Definition: debug.h:46
struct netif * next
Definition: netif.h:141
#define ERR_OK
Definition: err.h:52
Definition: pbuf.h:76
u8_t flags
Definition: netif.h:193
s8_t err_t
Definition: err.h:47
#define ip_addr_set_any(ipaddr)
Definition: ip_addr.h:170
typedefPACK_STRUCT_END struct ip_addr ip_addr_t
Definition: ip_addr.h:64
#define LWIP_DBG_STATE
Definition: debug.h:58
#define LWIP_DBG_LEVEL_WARNING
Definition: debug.h:45
Definition: netif.h:139
#define ip4_addr4_16(ipaddr)
Definition: ip_addr.h:229
ip_addr_t ip_addr
Definition: netif.h:144
#define IP_ADDR_ANY
Definition: ip_addr.h:92
#define IP_ADDR_BROADCAST
Definition: ip_addr.h:93
#define ip_addr_isany(addr1)
Definition: ip_addr.h:200
#define SOF_BROADCAST
Definition: ip.h:102
#define DHCP_DEBUG
Definition: opt.h:2016
#define DHCP_OPTION_ROUTER
Definition: dhcpserver.h:73
u16_t mtu
Definition: netif.h:187
#define ip_addr_set_zero(ipaddr)
Definition: ip_addr.h:168
#define DHCP_OPTION_DNS_SERVER
Definition: dhcpserver.h:74
#define ERR_ARG
Definition: err.h:68
u8_t pbuf_free(struct pbuf *p) ICACHE_FLASH_ATTR
Definition: pbuf.c:685
void * state
Definition: netif.h:171
#define ip4_addr1_16(ipaddr)
Definition: ip_addr.h:226
unsigned char u8_t
Definition: cc.h:52
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:65
#define DHCP_OPTION_SUBNET_MASK
Definition: dhcpserver.h:72
void * payload
Definition: pbuf.h:81
u16_t pbuf_copy_partial(struct pbuf *p, void *dataptr, u16_t len, u16_t offset) ICACHE_FLASH_ATTR
Definition: pbuf.c:996
char name[2]
Definition: netif.h:195
#define DHCP_MAXRTX
Definition: lwipopts.h:682
#define ip4_addr2_16(ipaddr)
Definition: ip_addr.h:227
ip_addr_t netmask
Definition: netif.h:145
#define X32_F
Definition: cc.h:66
#define ip4_addr_set_u32(dest_ipaddr, src_u32)
Definition: ip_addr.h:179
void pbuf_realloc(struct pbuf *p, u16_t size) ICACHE_FLASH_ATTR
Definition: pbuf.c:492
#define ip4_addr1(ipaddr)
Definition: ip_addr.h:220
void netif_set_down(struct netif *netif) ICACHE_FLASH_ATTR
Definition: netif.c:491
#define ERR_MEM
Definition: err.h:53
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:73
#define htonl(x)
Definition: def.h:83
struct pbuf * pbuf_alloc(pbuf_layer l, u16_t length, pbuf_type type) ICACHE_FLASH_ATTR
Definition: pbuf.c:234
void mem_free(void *mem) ICACHE_FLASH_ATTR
Definition: mem.c:310
unsigned short u16_t
Definition: cc.h:54
#define X16_F
Definition: cc.h:62
#define DHCP_OPTION_SERVER_ID
Definition: dhcpserver.h:78
#define ERR_BUF
Definition: err.h:54