96 #ifdef DHCP_GLOBAL_XID_HEADER 97 #include DHCP_GLOBAL_XID_HEADER 102 #define DHCP_MAX_MSG_LEN(netif) (netif->mtu) 103 #define DHCP_MAX_MSG_LEN_MIN_REQUIRED 576 105 #define DHCP_MIN_REPLY_LEN 44 107 #define REBOOT_TRIES 2 114 #define DHCP_OPTION_IDX_OVERLOAD 0 115 #define DHCP_OPTION_IDX_MSG_TYPE 1 116 #define DHCP_OPTION_IDX_SERVER_ID 2 117 #define DHCP_OPTION_IDX_LEASE_TIME 3 118 #define DHCP_OPTION_IDX_T1 4 119 #define DHCP_OPTION_IDX_T2 5 120 #define DHCP_OPTION_IDX_SUBNET_MASK 6 121 #define DHCP_OPTION_IDX_ROUTER 7 122 #define DHCP_OPTION_IDX_DNS_SERVER 8 123 #define DHCP_OPTION_IDX_MAX (DHCP_OPTION_IDX_DNS_SERVER + DNS_MAX_SERVERS) 127 u32_t dhcp_rx_options_val[DHCP_OPTION_IDX_MAX];
131 u8_t dhcp_rx_options_given[DHCP_OPTION_IDX_MAX];
133 #define dhcp_option_given(dhcp, idx) (dhcp_rx_options_given[idx] != 0) 134 #define dhcp_got_option(dhcp, idx) (dhcp_rx_options_given[idx] = 1) 135 #define dhcp_clear_option(dhcp, idx) (dhcp_rx_options_given[idx] = 0) 136 #define dhcp_clear_all_options(dhcp) (os_memset(dhcp_rx_options_given, 0, sizeof(dhcp_rx_options_given))) 137 #define dhcp_get_option_value(dhcp, idx) (dhcp_rx_options_val[idx]) 138 #define dhcp_set_option_value(dhcp, idx, val) (dhcp_rx_options_val[idx] = (val)) 145 #if DHCP_DOES_ARP_CHECK 150 static void dhcp_set_state(
struct dhcp *dhcp,
u8_t new_state);
153 static void dhcp_recv(
void *arg,
struct udp_pcb *pcb,
struct pbuf *p,
ip_addr_t *addr,
u16_t port);
156 static void dhcp_timeout(
struct netif *
netif);
157 static void dhcp_t1_timeout(
struct netif *
netif);
158 static void dhcp_t2_timeout(
struct netif *
netif);
164 static void dhcp_delete_msg(
struct dhcp *dhcp);
166 static void dhcp_option(
struct dhcp *dhcp,
u8_t option_type,
u8_t option_len);
168 static void dhcp_option_byte(
struct dhcp *dhcp,
u8_t value);
169 static void dhcp_option_short(
struct dhcp *dhcp,
u16_t value);
170 static void dhcp_option_long(
struct dhcp *dhcp,
u32_t value);
172 static void dhcp_option_trailer(
struct dhcp *dhcp);
189 struct dhcp *dhcp = netif->dhcp;
191 (
void*)netif, netif->
name[0], netif->name[1], (
u16_t)netif->num));
199 dhcp_set_state(dhcp, DHCP_BACKING_OFF);
201 dhcp_discover(netif);
204 #if DHCP_DOES_ARP_CHECK 215 dhcp_check(
struct netif *netif)
217 struct dhcp *dhcp = netif->dhcp;
221 (
s16_t)netif->name[1]));
222 dhcp_set_state(dhcp, DHCP_CHECKING);
225 result = etharp_query(netif, &dhcp->offered_ip_addr,
NULL);
231 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
242 dhcp_handle_offer(
struct netif *netif)
244 struct dhcp *dhcp = netif->dhcp;
246 (
void*)netif, netif->
name[0], netif->name[1], (
u16_t)netif->num));
248 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_SERVER_ID)) {
249 ip4_addr_set_u32(&dhcp->server_ip_addr,
htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_SERVER_ID)));
253 ip_addr_copy(dhcp->offered_ip_addr, dhcp->msg_in->yiaddr);
260 (
"dhcp_handle_offer(netif=%p) did not get server ID!\n", (
void*)netif));
273 dhcp_select(
struct netif *netif)
275 struct dhcp *dhcp = netif->dhcp;
280 dhcp_set_state(dhcp, DHCP_REQUESTING);
285 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
286 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
289 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
295 dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, 12);
298 dhcp_option_byte(dhcp, DHCP_OPTION_BROADCAST);
300 dhcp_option_byte(dhcp, DHCP_OPTION_DOMAIN_NAME);
301 dhcp_option_byte(dhcp, DHCP_OPTION_NB_TINS);
302 dhcp_option_byte(dhcp, DHCP_OPTION_NB_TINT);
303 dhcp_option_byte(dhcp, DHCP_OPTION_NB_TIS);
304 dhcp_option_byte(dhcp, DHCP_OPTION_PRD);
305 dhcp_option_byte(dhcp, DHCP_OPTION_STATIC_ROUTER);
306 dhcp_option_byte(dhcp, DHCP_OPTION_CLASSLESS_STATIC_ROUTER);
307 dhcp_option_byte(dhcp, DHCP_OPTION_VSN);
309 #if LWIP_NETIF_HOSTNAME 310 if (netif->hostname !=
NULL) {
311 const char *p = (
const char*)netif->hostname;
314 LWIP_ASSERT(
"DHCP: hostname is too long!", namelen < 255);
315 dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, namelen);
317 dhcp_option_byte(dhcp, *p++);
323 dhcp_option_trailer(dhcp);
325 pbuf_realloc(dhcp->p_out,
sizeof(
struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
328 udp_sendto_if(dhcp->pcb, dhcp->p_out,
IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
329 dhcp_delete_msg(dhcp);
335 msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000;
336 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
350 while (netif !=
NULL) {
352 if (netif->dhcp !=
NULL) {
354 if (netif->dhcp->t2_timeout-- == 1) {
357 dhcp_t2_timeout(netif);
359 }
else if (netif->dhcp->t1_timeout-- == 1) {
362 dhcp_t1_timeout(netif);
381 while (netif !=
NULL) {
383 if (netif->dhcp !=
NULL) {
388 if (netif->dhcp_event !=
NULL)
394 if (netif->dhcp->request_timeout > 1) {
395 netif->dhcp->request_timeout--;
397 else if (netif->dhcp->request_timeout == 1) {
398 netif->dhcp->request_timeout--;
420 dhcp_timeout(
struct netif *netif)
422 struct dhcp *dhcp = netif->dhcp;
425 if ((dhcp->state == DHCP_BACKING_OFF) || (dhcp->state == DHCP_SELECTING)) {
427 dhcp_discover(netif);
429 }
else if (dhcp->state == DHCP_REQUESTING) {
431 if (dhcp->tries <= 5) {
436 dhcp_discover(netif);
438 #if DHCP_DOES_ARP_CHECK 440 }
else if (dhcp->state == DHCP_CHECKING) {
442 if (dhcp->tries <= 1) {
453 else if (dhcp->state == DHCP_RENEWING) {
459 }
else if (dhcp->state == DHCP_REBINDING) {
461 if (dhcp->tries <= 8) {
466 dhcp_discover(netif);
468 }
else if (dhcp->state == DHCP_REBOOTING) {
469 if (dhcp->tries < REBOOT_TRIES) {
472 dhcp_discover(netif);
483 dhcp_t1_timeout(
struct netif *netif)
485 struct dhcp *dhcp = netif->dhcp;
487 if ((dhcp->state == DHCP_REQUESTING) || (dhcp->state == DHCP_BOUND) ||
488 (dhcp->state == DHCP_RENEWING)) {
492 (
"dhcp_t1_timeout(): must renew\n"));
505 dhcp_t2_timeout(
struct netif *netif)
507 struct dhcp *dhcp = netif->dhcp;
509 if ((dhcp->state == DHCP_REQUESTING) || (dhcp->state == DHCP_BOUND) ||
510 (dhcp->state == DHCP_RENEWING)) {
513 (
"dhcp_t2_timeout(): must rebind\n"));
526 dhcp_handle_ack(
struct netif *netif)
528 struct dhcp *dhcp = netif->dhcp;
536 #if LWIP_DHCP_BOOTP_FILE 541 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_LEASE_TIME)) {
543 dhcp->offered_t0_lease = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_LEASE_TIME);
546 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_T1)) {
548 dhcp->offered_t1_renew = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_T1);
551 dhcp->offered_t1_renew = dhcp->offered_t0_lease / 2;
555 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_T2)) {
557 dhcp->offered_t2_rebind = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_T2);
560 dhcp->offered_t2_rebind = dhcp->offered_t0_lease;
564 ip_addr_copy(dhcp->offered_ip_addr, dhcp->msg_in->yiaddr);
566 #if LWIP_DHCP_BOOTP_FILE 569 ip_addr_copy(dhcp->offered_si_addr, dhcp->msg_in->siaddr);
573 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_SUBNET_MASK)) {
575 ip4_addr_set_u32(&dhcp->offered_sn_mask,
htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_SUBNET_MASK)));
576 dhcp->subnet_mask_given = 1;
578 dhcp->subnet_mask_given = 0;
582 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_ROUTER)) {
583 ip4_addr_set_u32(&dhcp->offered_gw_addr,
htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_ROUTER)));
589 while(dhcp_option_given(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n) && (n <
DNS_MAX_SERVERS)) {
592 dns_setserver(n, &dns_addr);
605 dhcp_set_struct(
struct netif *netif,
struct dhcp *dhcp)
609 LWIP_ASSERT(
"netif already has a struct dhcp set", netif->dhcp ==
NULL);
628 if (netif->dhcp !=
NULL) {
647 dhcp_start(
struct netif *netif)
665 if (netif->
mtu < DHCP_MAX_MSG_LEN_MIN_REQUIRED) {
673 dhcp = (
struct dhcp *)
mem_malloc(
sizeof(
struct dhcp));
684 if (dhcp->pcb !=
NULL) {
685 udp_remove(dhcp->pcb);
695 dhcp->pcb = udp_new();
696 if (dhcp->pcb ==
NULL) {
702 udp_bind(dhcp->pcb,
IP_ADDR_ANY, DHCP_CLIENT_PORT);
703 udp_connect(dhcp->pcb,
IP_ADDR_ANY, DHCP_SERVER_PORT);
705 udp_recv(dhcp->pcb, dhcp_recv, netif);
708 result = dhcp_discover(netif);
729 dhcp_inform(
struct netif *netif)
737 os_memset(&dhcp, 0,
sizeof(
struct dhcp));
738 dhcp_set_state(&dhcp, DHCP_INFORM);
740 if ((netif->dhcp !=
NULL) && (netif->dhcp->pcb !=
NULL)) {
742 pcb = netif->dhcp->pcb;
755 result = dhcp_create_msg(netif, &dhcp, DHCP_INFORM);
757 dhcp_option(&dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
758 dhcp_option_short(&dhcp, DHCP_MAX_MSG_LEN(netif));
760 dhcp_option_trailer(&dhcp);
762 pbuf_realloc(dhcp.p_out,
sizeof(
struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp.options_out_len);
766 dhcp_delete_msg(&dhcp);
771 if (dhcp.pcb !=
NULL) {
773 udp_remove(dhcp.pcb);
783 dhcp_network_changed(
struct netif *netif)
785 struct dhcp *dhcp = netif->dhcp;
788 switch (dhcp->state) {
802 #if LWIP_DHCP_AUTOIP_COOP 803 if(dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
805 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
808 dhcp_discover(netif);
813 #if DHCP_DOES_ARP_CHECK 825 if ((netif->dhcp !=
NULL) && (netif->dhcp->
state == DHCP_CHECKING)) {
830 if (
ip_addr_cmp(addr, &netif->dhcp->offered_ip_addr)) {
833 (
"dhcp_arp_reply(): arp reply matched with offered address, declining\n"));
849 dhcp_decline(
struct netif *netif)
851 struct dhcp *dhcp = netif->dhcp;
855 dhcp_set_state(dhcp, DHCP_BACKING_OFF);
857 result = dhcp_create_msg(netif, dhcp, DHCP_DECLINE);
859 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
862 dhcp_option_trailer(dhcp);
864 pbuf_realloc(dhcp->p_out,
sizeof(
struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
867 udp_sendto_if(dhcp->pcb, dhcp->p_out,
IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
868 dhcp_delete_msg(dhcp);
872 (
"dhcp_decline: could not allocate DHCP request\n"));
876 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
889 dhcp_discover(
struct netif *netif)
891 struct dhcp *dhcp = netif->dhcp;
896 dhcp_set_state(dhcp, DHCP_SELECTING);
898 result = dhcp_create_msg(netif, dhcp, DHCP_DISCOVER);
902 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
903 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
905 #if LWIP_NETIF_HOSTNAME 906 if (netif->hostname !=
NULL) {
907 const char *p = (
const char*)netif->hostname;
910 LWIP_ASSERT(
"DHCP: hostname is too long!", namelen < 255);
911 dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, namelen);
913 dhcp_option_byte(dhcp, *p++);
918 dhcp_option(dhcp, DHCP_OPTION_PARAMETER_REQUEST_LIST, 12);
921 dhcp_option_byte(dhcp, DHCP_OPTION_BROADCAST);
923 dhcp_option_byte(dhcp, DHCP_OPTION_DOMAIN_NAME);
924 dhcp_option_byte(dhcp, DHCP_OPTION_NB_TINS);
925 dhcp_option_byte(dhcp, DHCP_OPTION_NB_TINT);
926 dhcp_option_byte(dhcp, DHCP_OPTION_NB_TIS);
927 dhcp_option_byte(dhcp, DHCP_OPTION_PRD);
928 dhcp_option_byte(dhcp, DHCP_OPTION_STATIC_ROUTER);
929 dhcp_option_byte(dhcp, DHCP_OPTION_CLASSLESS_STATIC_ROUTER);
930 dhcp_option_byte(dhcp, DHCP_OPTION_VSN);
932 dhcp_option_trailer(dhcp);
935 pbuf_realloc(dhcp->p_out,
sizeof(
struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
938 udp_sendto_if(dhcp->pcb, dhcp->p_out,
IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
940 dhcp_delete_msg(dhcp);
946 #if LWIP_DHCP_AUTOIP_COOP 948 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_ON;
952 msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000;
953 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
965 dhcp_bind(
struct netif *netif)
976 if (dhcp->offered_t1_renew != 0xffffffffUL) {
979 timeout = (dhcp->offered_t1_renew + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
980 if(timeout > 0xffff) {
983 dhcp->t1_timeout = (
u16_t)timeout;
984 if (dhcp->t1_timeout == 0) {
985 dhcp->t1_timeout = 1;
990 if (dhcp->offered_t2_rebind != 0xffffffffUL) {
992 timeout = (dhcp->offered_t2_rebind + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
993 if(timeout > 0xffff) {
996 dhcp->t2_timeout = (
u16_t)timeout;
997 if (dhcp->t2_timeout == 0) {
998 dhcp->t2_timeout = 1;
1004 if ((dhcp->t1_timeout >= dhcp->t2_timeout) && (dhcp->t2_timeout > 0)) {
1005 dhcp->t1_timeout = 0;
1008 if (dhcp->subnet_mask_given) {
1014 if (first_octet <= 127) {
1016 }
else if (first_octet >= 192) {
1032 #if LWIP_DHCP_AUTOIP_COOP 1033 if(dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
1035 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
1059 system_station_got_ip_set(&ip, &mask, &gw);
1062 dhcp_set_state(dhcp, DHCP_BOUND);
1071 dhcp_renew(
struct netif *netif)
1073 struct dhcp *dhcp = netif->dhcp;
1077 dhcp_set_state(dhcp, DHCP_RENEWING);
1082 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1083 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
1085 #if LWIP_NETIF_HOSTNAME 1086 if (netif->hostname !=
NULL) {
1087 const char *p = (
const char*)netif->hostname;
1090 LWIP_ASSERT(
"DHCP: hostname is too long!", namelen < 255);
1091 dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, namelen);
1093 dhcp_option_byte(dhcp, *p++);
1100 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
1101 dhcp_option_long(dhcp,
ntohl(dhcp->offered_ip_addr.addr));
1106 dhcp_option_long(dhcp,
ntohl(dhcp->server_ip_addr.addr));
1109 dhcp_option_trailer(dhcp);
1111 pbuf_realloc(dhcp->p_out,
sizeof(
struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1113 udp_sendto_if(dhcp->pcb, dhcp->p_out, &dhcp->server_ip_addr, DHCP_SERVER_PORT, netif);
1114 dhcp_delete_msg(dhcp);
1122 msecs = dhcp->tries < 10 ? dhcp->tries * 2000 : 20 * 1000;
1123 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1134 dhcp_rebind(
struct netif *netif)
1136 struct dhcp *dhcp = netif->dhcp;
1140 dhcp_set_state(dhcp, DHCP_REBINDING);
1145 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1146 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(netif));
1148 #if LWIP_NETIF_HOSTNAME 1149 if (netif->hostname !=
NULL) {
1150 const char *p = (
const char*)netif->hostname;
1153 LWIP_ASSERT(
"DHCP: hostname is too long!", namelen < 255);
1154 dhcp_option(dhcp, DHCP_OPTION_HOSTNAME, namelen);
1156 dhcp_option_byte(dhcp, *p++);
1163 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
1164 dhcp_option_long(dhcp,
ntohl(dhcp->offered_ip_addr.addr));
1167 dhcp_option_long(dhcp,
ntohl(dhcp->server_ip_addr.addr));
1170 dhcp_option_trailer(dhcp);
1172 pbuf_realloc(dhcp->p_out,
sizeof(
struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1175 udp_sendto_if(dhcp->pcb, dhcp->p_out,
IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
1176 dhcp_delete_msg(dhcp);
1182 msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
1183 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1194 dhcp_reboot(
struct netif *netif)
1196 struct dhcp *dhcp = netif->dhcp;
1200 dhcp_set_state(dhcp, DHCP_REBOOTING);
1205 dhcp_option(dhcp, DHCP_OPTION_MAX_MSG_SIZE, DHCP_OPTION_MAX_MSG_SIZE_LEN);
1206 dhcp_option_short(dhcp, 576);
1208 dhcp_option(dhcp, DHCP_OPTION_REQUESTED_IP, 4);
1211 dhcp_option_trailer(dhcp);
1213 pbuf_realloc(dhcp->p_out,
sizeof(
struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1216 udp_sendto_if(dhcp->pcb, dhcp->p_out,
IP_ADDR_BROADCAST, DHCP_SERVER_PORT, netif);
1217 dhcp_delete_msg(dhcp);
1223 msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
1224 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1236 dhcp_release(
struct netif *netif)
1238 struct dhcp *dhcp = netif->dhcp;
1247 dhcp_set_state(dhcp, DHCP_OFF);
1253 #if LWIP_DHCP_BOOTP_FILE 1256 dhcp->offered_t0_lease = dhcp->offered_t1_renew = dhcp->offered_t2_rebind = 0;
1259 result = dhcp_create_msg(netif, dhcp, DHCP_RELEASE);
1261 dhcp_option_trailer(dhcp);
1263 pbuf_realloc(dhcp->p_out,
sizeof(
struct dhcp_msg) - DHCP_OPTIONS_LEN + dhcp->options_out_len);
1265 udp_sendto_if(dhcp->pcb, dhcp->p_out, &dhcp->server_ip_addr, DHCP_SERVER_PORT, netif);
1266 dhcp_delete_msg(dhcp);
1272 msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
1273 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1291 dhcp_stop(
struct netif *netif)
1294 LWIP_ERROR(
"dhcp_stop: netif != NULL", (netif !=
NULL),
return;);
1301 #if LWIP_DHCP_AUTOIP_COOP 1302 if(dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
1304 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
1308 if (dhcp->pcb !=
NULL) {
1309 udp_remove(dhcp->pcb);
1313 dhcp_set_state(dhcp, DHCP_OFF);
1323 dhcp_set_state(
struct dhcp *dhcp,
u8_t new_state)
1325 if (new_state != dhcp->state) {
1326 dhcp->state = new_state;
1328 dhcp->request_timeout = 0;
1338 dhcp_option(
struct dhcp *dhcp,
u8_t option_type,
u8_t option_len)
1340 LWIP_ASSERT(
"dhcp_option: dhcp->options_out_len + 2 + option_len <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U + option_len <= DHCP_OPTIONS_LEN);
1341 dhcp->msg_out->options[dhcp->options_out_len++] = option_type;
1342 dhcp->msg_out->options[dhcp->options_out_len++] = option_len;
1349 dhcp_option_byte(
struct dhcp *dhcp,
u8_t value)
1351 LWIP_ASSERT(
"dhcp_option_byte: dhcp->options_out_len < DHCP_OPTIONS_LEN", dhcp->options_out_len < DHCP_OPTIONS_LEN);
1352 dhcp->msg_out->options[dhcp->options_out_len++] = value;
1356 dhcp_option_short(
struct dhcp *dhcp,
u16_t value)
1358 LWIP_ASSERT(
"dhcp_option_short: dhcp->options_out_len + 2 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U <= DHCP_OPTIONS_LEN);
1359 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t)((value & 0xff00U) >> 8);
1360 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t) (value & 0x00ffU);
1364 dhcp_option_long(
struct dhcp *dhcp,
u32_t value)
1366 LWIP_ASSERT(
"dhcp_option_long: dhcp->options_out_len + 4 <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 4U <= DHCP_OPTIONS_LEN);
1367 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t)((value & 0xff000000UL) >> 24);
1368 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t)((value & 0x00ff0000UL) >> 16);
1369 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t)((value & 0x0000ff00UL) >> 8);
1370 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t)((value & 0x000000ffUL));
1384 dhcp_parse_reply(
struct dhcp *dhcp,
struct pbuf *p)
1390 u16_t options_idx_max;
1392 int parse_file_as_options = 0;
1393 int parse_sname_as_options = 0;
1396 dhcp_clear_all_options(dhcp);
1398 if (p->
len < DHCP_SNAME_OFS) {
1401 dhcp->msg_in = (
struct dhcp_msg *)p->
payload;
1402 #
if LWIP_DHCP_BOOTP_FILE
1404 dhcp->boot_file_name[0] = 0;
1410 options_idx = DHCP_OPTIONS_OFS;
1415 while((q !=
NULL) && (options_idx >= q->
len)) {
1416 options_idx -= q->
len;
1417 options_idx_max -= q->
len;
1423 offset = options_idx;
1424 offset_max = options_idx_max;
1428 u8_t op = options[offset];
1430 u8_t decode_len = 0;
1431 int decode_idx = -1;
1432 u16_t val_offset = offset + 2;
1434 if (offset + 1 < q->
len) {
1435 len = options[offset + 1];
1443 case(DHCP_OPTION_PAD):
1445 decode_len = len = 0;
1451 decode_idx = DHCP_OPTION_IDX_SUBNET_MASK;
1455 LWIP_ASSERT(
"len >= decode_len", len >= decode_len);
1456 decode_idx = DHCP_OPTION_IDX_ROUTER;
1463 LWIP_ASSERT(
"len >= decode_len", len >= decode_len);
1464 decode_idx = DHCP_OPTION_IDX_DNS_SERVER;
1468 decode_idx = DHCP_OPTION_IDX_LEASE_TIME;
1470 case(DHCP_OPTION_OVERLOAD):
1472 decode_idx = DHCP_OPTION_IDX_OVERLOAD;
1474 case(DHCP_OPTION_MESSAGE_TYPE):
1476 decode_idx = DHCP_OPTION_IDX_MSG_TYPE;
1480 decode_idx = DHCP_OPTION_IDX_SERVER_ID;
1482 case(DHCP_OPTION_T1):
1484 decode_idx = DHCP_OPTION_IDX_T1;
1486 case(DHCP_OPTION_T2):
1488 decode_idx = DHCP_OPTION_IDX_T2;
1496 if (decode_len > 0) {
1500 LWIP_ASSERT(
"check decode_idx", decode_idx >= 0 && decode_idx < DHCP_OPTION_IDX_MAX);
1501 LWIP_ASSERT(
"option already decoded", !dhcp_option_given(dhcp, decode_idx));
1502 copy_len =
LWIP_MIN(decode_len, 4);
1504 if (decode_len > 4) {
1506 LWIP_ASSERT(
"decode_len % 4 == 0", decode_len % 4 == 0);
1507 dhcp_got_option(dhcp, decode_idx);
1508 dhcp_set_option_value(dhcp, decode_idx,
htonl(value));
1513 }
else if (decode_len == 4) {
1514 value =
ntohl(value);
1516 LWIP_ASSERT(
"invalid decode_len", decode_len == 1);
1517 value = ((
u8_t*)&value)[0];
1519 dhcp_got_option(dhcp, decode_idx);
1520 dhcp_set_option_value(dhcp, decode_idx, value);
1522 if (offset >= q->
len) {
1524 offset_max -= q->
len;
1525 if ((offset < offset_max) && offset_max) {
1536 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_OVERLOAD)) {
1537 u32_t overload = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_OVERLOAD);
1538 dhcp_clear_option(dhcp, DHCP_OPTION_IDX_OVERLOAD);
1539 if (overload == DHCP_OVERLOAD_FILE) {
1540 parse_file_as_options = 1;
1542 }
else if (overload == DHCP_OVERLOAD_SNAME) {
1543 parse_sname_as_options = 1;
1545 }
else if (overload == DHCP_OVERLOAD_SNAME_FILE) {
1546 parse_sname_as_options = 1;
1547 parse_file_as_options = 1;
1552 #if LWIP_DHCP_BOOTP_FILE 1553 if (!parse_file_as_options) {
1555 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE) &&
1556 (dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE) == DHCP_ACK))
1560 dhcp->boot_file_name[DHCP_FILE_LEN-1] = 0;
1564 if (parse_file_as_options) {
1566 parse_file_as_options = 0;
1567 options_idx = DHCP_FILE_OFS;
1568 options_idx_max = DHCP_FILE_OFS + DHCP_FILE_LEN;
1570 }
else if (parse_sname_as_options) {
1571 parse_sname_as_options = 0;
1572 options_idx = DHCP_SNAME_OFS;
1573 options_idx_max = DHCP_SNAME_OFS + DHCP_SNAME_LEN;
1583 dhcp_recv(
void *arg,
struct udp_pcb *pcb,
struct pbuf *p,
ip_addr_t *addr,
u16_t port)
1585 struct netif *netif = (
struct netif *)arg;
1586 struct dhcp *dhcp = netif->dhcp;
1587 struct dhcp_msg *reply_msg = (
struct dhcp_msg *)p->
payload;
1601 if (p->
len < DHCP_MIN_REPLY_LEN) {
1603 goto free_pbuf_and_return;
1606 if (reply_msg->op != DHCP_BOOTREPLY) {
1608 goto free_pbuf_and_return;
1612 if (netif->
hwaddr[i] != reply_msg->chaddr[i]) {
1614 (
"netif->hwaddr[%"U16_F
"]==%02"X16_F" != reply_msg->chaddr[%"U16_F
"]==%02"X16_F"\n",
1616 goto free_pbuf_and_return;
1620 if (
ntohl(reply_msg->xid) != dhcp->xid) {
1622 (
"transaction id mismatch reply_msg->xid(%"X32_F")!=dhcp->xid(%"X32_F")\n",
ntohl(reply_msg->xid),dhcp->xid));
1623 goto free_pbuf_and_return;
1626 if (dhcp_parse_reply(dhcp, p) !=
ERR_OK) {
1628 (
"problem unfolding DHCP message - too short on memory?\n"));
1629 goto free_pbuf_and_return;
1634 if (!dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE)) {
1636 goto free_pbuf_and_return;
1640 msg_type = (
u8_t)dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE);
1642 if (msg_type == DHCP_ACK) {
1645 if (dhcp->state == DHCP_REQUESTING) {
1646 dhcp_handle_ack(netif);
1647 #if DHCP_DOES_ARP_CHECK 1656 else if ((dhcp->state == DHCP_REBOOTING) || (dhcp->state == DHCP_REBINDING) || (dhcp->state == DHCP_RENEWING)) {
1661 else if ((msg_type == DHCP_NAK) &&
1662 ((dhcp->state == DHCP_REBOOTING) || (dhcp->state == DHCP_REQUESTING) ||
1663 (dhcp->state == DHCP_REBINDING) || (dhcp->state == DHCP_RENEWING ))) {
1665 dhcp_handle_nak(netif);
1668 else if ((msg_type == DHCP_OFFER) && (dhcp->state == DHCP_SELECTING)) {
1670 dhcp->request_timeout = 0;
1672 dhcp_handle_offer(netif);
1674 free_pbuf_and_return:
1675 dhcp->msg_in =
NULL;
1687 dhcp_create_msg(
struct netif *netif,
struct dhcp *dhcp,
u8_t message_type)
1690 #ifndef DHCP_GLOBAL_XID 1695 static u32_t xid = 0xABCD0000;
1698 static u8_t xid_initialised = 0;
1699 if (!xid_initialised) {
1700 xid = DHCP_GLOBAL_XID;
1701 xid_initialised = !xid_initialised;
1706 LWIP_ASSERT(
"dhcp_create_msg: dhcp->p_out == NULL", dhcp->p_out ==
NULL);
1707 LWIP_ASSERT(
"dhcp_create_msg: dhcp->msg_out == NULL", dhcp->msg_out ==
NULL);
1709 if (dhcp->p_out ==
NULL) {
1711 (
"dhcp_create_msg(): could not allocate pbuf\n"));
1714 LWIP_ASSERT(
"dhcp_create_msg: check that first pbuf can hold struct dhcp_msg",
1715 (dhcp->p_out->len >=
sizeof(
struct dhcp_msg)));
1720 if (dhcp->tries == 0) {
1726 (
"transaction id xid(%"X32_F")\n", xid));
1728 dhcp->msg_out = (
struct dhcp_msg *)dhcp->p_out->payload;
1730 dhcp->msg_out->op = DHCP_BOOTREQUEST;
1732 dhcp->msg_out->htype = DHCP_HTYPE_ETH;
1734 dhcp->msg_out->hops = 0;
1735 dhcp->msg_out->xid =
htonl(dhcp->xid);
1736 dhcp->msg_out->secs = 0;
1739 dhcp->msg_out->flags = 0;
1742 if ((message_type == DHCP_INFORM) || (message_type == DHCP_DECLINE) ||
1744 ((dhcp->state==DHCP_RENEWING) || dhcp->state==DHCP_REBINDING))) {
1750 for (i = 0; i < DHCP_CHADDR_LEN; i++) {
1754 for (i = 0; i < DHCP_SNAME_LEN; i++) {
1755 dhcp->msg_out->sname[i] = 0;
1757 for (i = 0; i < DHCP_FILE_LEN; i++) {
1758 dhcp->msg_out->file[i] = 0;
1760 dhcp->msg_out->cookie =
PP_HTONL(DHCP_MAGIC_COOKIE);
1761 dhcp->options_out_len = 0;
1763 for (i = 0; i < DHCP_OPTIONS_LEN; i++) {
1764 dhcp->msg_out->options[i] = (
u8_t)i;
1767 dhcp_option(dhcp, DHCP_OPTION_MESSAGE_TYPE, DHCP_OPTION_MESSAGE_TYPE_LEN);
1768 dhcp_option_byte(dhcp, message_type);
1778 dhcp_delete_msg(
struct dhcp *dhcp)
1780 LWIP_ERROR(
"dhcp_delete_msg: dhcp != NULL", (dhcp !=
NULL),
return;);
1781 LWIP_ASSERT(
"dhcp_delete_msg: dhcp->p_out != NULL", dhcp->p_out !=
NULL);
1782 LWIP_ASSERT(
"dhcp_delete_msg: dhcp->msg_out != NULL", dhcp->msg_out !=
NULL);
1783 if (dhcp->p_out !=
NULL) {
1787 dhcp->msg_out =
NULL;
1799 dhcp_option_trailer(
struct dhcp *dhcp)
1801 LWIP_ERROR(
"dhcp_option_trailer: dhcp != NULL", (dhcp !=
NULL),
return;);
1802 LWIP_ASSERT(
"dhcp_option_trailer: dhcp->msg_out != NULL\n", dhcp->msg_out !=
NULL);
1803 LWIP_ASSERT(
"dhcp_option_trailer: dhcp->options_out_len < DHCP_OPTIONS_LEN\n", dhcp->options_out_len < DHCP_OPTIONS_LEN);
1806 while (((dhcp->options_out_len < DHCP_MIN_OPTIONS_LEN) || (dhcp->options_out_len & 3)) &&
1807 (dhcp->options_out_len < DHCP_OPTIONS_LEN)) {
1809 LWIP_ASSERT(
"dhcp_option_trailer: dhcp->options_out_len < DHCP_OPTIONS_LEN\n", dhcp->options_out_len < DHCP_OPTIONS_LEN);
1811 dhcp->msg_out->options[dhcp->options_out_len++] = 0;
#define ip4_addr_get_u32(src_ipaddr)
u8_t hwaddr[NETIF_MAX_HWADDR_LEN]
#define LWIP_DHCP_AUTOIP_COOP_TRIES
#define ip4_addr3_16(ipaddr)
struct netif * netif_list
const ip_addr_t ip_addr_any ICACHE_RODATA_ATTR
#define ICACHE_FLASH_ATTR
void * mem_malloc(mem_size_t size) ICACHE_FLASH_ATTR
void netif_set_up(struct netif *netif) ICACHE_FLASH_ATTR
void netif_set_netmask(struct netif *netif, ip_addr_t *netmask) ICACHE_FLASH_ATTR
#define LWIP_ERROR(message, expression, handler)
#define ip_addr_get_network(target, host, netmask)
void netif_set_gw(struct netif *netif, ip_addr_t *gw) ICACHE_FLASH_ATTR
#define NETIF_MAX_HWADDR_LEN
void netif_set_ipaddr(struct netif *netif, ip_addr_t *ipaddr) ICACHE_FLASH_ATTR
#define NETIF_FLAG_ETHARP
#define LWIP_DEBUGF(debug, message)
#define DHCP_OPTION_LEASE_TIME
#define ip_addr_cmp(addr1, addr2)
#define ip_addr_copy(dest, src)
#define LWIP_DBG_LEVEL_SERIOUS
#define ip_addr_set_any(ipaddr)
typedefPACK_STRUCT_END struct ip_addr ip_addr_t
#define LWIP_DBG_LEVEL_WARNING
#define ip4_addr4_16(ipaddr)
#define IP_ADDR_BROADCAST
#define ip_addr_isany(addr1)
#define DHCP_OPTION_ROUTER
#define ip_addr_set_zero(ipaddr)
#define DHCP_OPTION_DNS_SERVER
u8_t pbuf_free(struct pbuf *p) ICACHE_FLASH_ATTR
#define ip4_addr1_16(ipaddr)
#define LWIP_ASSERT(message, assertion)
#define DHCP_OPTION_SUBNET_MASK
u16_t pbuf_copy_partial(struct pbuf *p, void *dataptr, u16_t len, u16_t offset) ICACHE_FLASH_ATTR
#define ip4_addr2_16(ipaddr)
#define ip4_addr_set_u32(dest_ipaddr, src_u32)
void pbuf_realloc(struct pbuf *p, u16_t size) ICACHE_FLASH_ATTR
#define ip4_addr1(ipaddr)
void netif_set_down(struct netif *netif) ICACHE_FLASH_ATTR
#define LWIP_UNUSED_ARG(x)
struct pbuf * pbuf_alloc(pbuf_layer l, u16_t length, pbuf_type type) ICACHE_FLASH_ATTR
void mem_free(void *mem) ICACHE_FLASH_ATTR
#define DHCP_OPTION_SERVER_ID