dhcp.c 66 KB

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