dhcp.c 66 KB

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