65 #if LWIP_IPV4 && LWIP_DHCP
84 #ifndef DHCP_CREATE_RAND_XID
85 #define DHCP_CREATE_RAND_XID 1
93 #ifdef DHCP_GLOBAL_XID_HEADER
94 #include DHCP_GLOBAL_XID_HEADER
99 #define DHCP_MAX_MSG_LEN(netif) (netif->mtu)
100 #define DHCP_MAX_MSG_LEN_MIN_REQUIRED 576
102 #define DHCP_MIN_REPLY_LEN 44
104 #define REBOOT_TRIES 2
106 #if LWIP_DNS && LWIP_DHCP_MAX_DNS_SERVERS
107 #if DNS_MAX_SERVERS > LWIP_DHCP_MAX_DNS_SERVERS
108 #define LWIP_DHCP_PROVIDE_DNS_SERVERS LWIP_DHCP_MAX_DNS_SERVERS
110 #define LWIP_DHCP_PROVIDE_DNS_SERVERS DNS_MAX_SERVERS
113 #define LWIP_DHCP_PROVIDE_DNS_SERVERS 0
121 enum dhcp_option_idx {
122 DHCP_OPTION_IDX_OVERLOAD = 0,
123 DHCP_OPTION_IDX_MSG_TYPE,
124 DHCP_OPTION_IDX_SERVER_ID,
125 DHCP_OPTION_IDX_LEASE_TIME,
128 DHCP_OPTION_IDX_SUBNET_MASK,
129 DHCP_OPTION_IDX_ROUTER,
130 #if LWIP_DHCP_PROVIDE_DNS_SERVERS
131 DHCP_OPTION_IDX_DNS_SERVER,
132 DHCP_OPTION_IDX_DNS_SERVER_LAST = DHCP_OPTION_IDX_DNS_SERVER + LWIP_DHCP_PROVIDE_DNS_SERVERS - 1,
134 #if LWIP_DHCP_GET_NTP_SRV
135 DHCP_OPTION_IDX_NTP_SERVER,
143 u32_t dhcp_rx_options_val[DHCP_OPTION_IDX_MAX];
147 u8_t dhcp_rx_options_given[DHCP_OPTION_IDX_MAX];
149 static u8_t dhcp_discover_request_options[] = {
153 #if LWIP_DHCP_PROVIDE_DNS_SERVERS
156 #if LWIP_DHCP_GET_NTP_SRV
161 #ifdef DHCP_GLOBAL_XID
163 static u8_t xid_initialised;
166 #define dhcp_option_given(dhcp, idx) (dhcp_rx_options_given[idx] != 0)
167 #define dhcp_got_option(dhcp, idx) (dhcp_rx_options_given[idx] = 1)
168 #define dhcp_clear_option(dhcp, idx) (dhcp_rx_options_given[idx] = 0)
169 #define dhcp_clear_all_options(dhcp) (memset(dhcp_rx_options_given, 0, sizeof(dhcp_rx_options_given)))
170 #define dhcp_get_option_value(dhcp, idx) (dhcp_rx_options_val[idx])
171 #define dhcp_set_option_value(dhcp, idx, val) (dhcp_rx_options_val[idx] = (val))
173 static struct udp_pcb *dhcp_pcb;
174 static u8_t dhcp_pcb_refcount;
180 #if DHCP_DOES_ARP_CHECK
185 static void dhcp_set_state(
struct dhcp *dhcp,
u8_t new_state);
188 static void dhcp_recv(
void *arg,
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *addr,
u16_t port);
191 static void dhcp_timeout(
struct netif *
netif);
192 static void dhcp_t1_timeout(
struct netif *
netif);
193 static void dhcp_t2_timeout(
struct netif *
netif);
199 static void dhcp_delete_msg(
struct dhcp *dhcp);
201 static void dhcp_option(
struct dhcp *dhcp,
u8_t option_type,
u8_t option_len);
203 static void dhcp_option_byte(
struct dhcp *dhcp,
u8_t value);
204 static void dhcp_option_short(
struct dhcp *dhcp,
u16_t value);
205 static void dhcp_option_long(
struct dhcp *dhcp,
u32_t value);
206 #if LWIP_NETIF_HOSTNAME
207 static void dhcp_option_hostname(
struct dhcp *dhcp,
struct netif *
netif);
210 static void dhcp_option_trailer(
struct dhcp *dhcp);
214 dhcp_inc_pcb_refcount(
void)
216 if (dhcp_pcb_refcount == 0) {
220 dhcp_pcb = udp_new();
222 if (dhcp_pcb ==
NULL) {
231 udp_recv(dhcp_pcb, dhcp_recv,
NULL);
241 dhcp_dec_pcb_refcount(
void)
243 LWIP_ASSERT(
"dhcp_pcb_refcount(): refcount error", (dhcp_pcb_refcount > 0));
246 if (dhcp_pcb_refcount == 0) {
247 udp_remove(dhcp_pcb);
267 struct dhcp *dhcp = netif_dhcp_data(
netif);
275 netif_set_addr(
netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
277 dhcp_discover(
netif);
280 #if DHCP_DOES_ARP_CHECK
293 struct dhcp *dhcp = netif_dhcp_data(
netif);
301 result = etharp_query(
netif, &dhcp->offered_ip_addr,
NULL);
305 if (dhcp->tries < 255) {
309 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
322 struct dhcp *dhcp = netif_dhcp_data(
netif);
327 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_SERVER_ID)) {
328 ip_addr_set_ip4_u32(&dhcp->server_ip_addr,
lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_SERVER_ID)));
330 ip4_addr_get_u32(ip_2_ip4(&dhcp->server_ip_addr))));
332 ip4_addr_copy(dhcp->offered_ip_addr, dhcp->msg_in->yiaddr);
334 ip4_addr_get_u32(&dhcp->offered_ip_addr)));
339 (
"dhcp_handle_offer(netif=%p) did not get server ID!\n", (
void*)
netif));
354 struct dhcp *dhcp = netif_dhcp_data(
netif);
366 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(
netif));
370 dhcp_option_long(dhcp,
lwip_ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
373 dhcp_option_long(dhcp,
lwip_ntohl(ip4_addr_get_u32(ip_2_ip4(&dhcp->server_ip_addr))));
376 for (i = 0; i <
LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
377 dhcp_option_byte(dhcp, dhcp_discover_request_options[i]);
380 #if LWIP_NETIF_HOSTNAME
381 dhcp_option_hostname(dhcp,
netif);
384 dhcp_option_trailer(dhcp);
390 dhcp_delete_msg(dhcp);
395 if (dhcp->tries < 255) {
398 msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000;
399 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
409 dhcp_coarse_tmr(
void)
416 struct dhcp *dhcp = netif_dhcp_data(
netif);
419 if (dhcp->t0_timeout && (++dhcp->lease_used == dhcp->t0_timeout)) {
423 dhcp_discover(
netif);
425 }
else if (dhcp->t2_rebind_time && (dhcp->t2_rebind_time-- == 1)) {
428 dhcp_t2_timeout(
netif);
430 }
else if (dhcp->t1_renew_time && (dhcp->t1_renew_time-- == 1)) {
433 dhcp_t1_timeout(
netif);
454 struct dhcp *dhcp = netif_dhcp_data(
netif);
458 if (dhcp->request_timeout > 1) {
459 dhcp->request_timeout--;
461 else if (dhcp->request_timeout == 1) {
462 dhcp->request_timeout--;
485 struct dhcp *dhcp = netif_dhcp_data(
netif);
491 dhcp_discover(
netif);
495 if (dhcp->tries <= 5) {
500 dhcp_discover(
netif);
502 #if DHCP_DOES_ARP_CHECK
506 if (dhcp->tries <= 1) {
516 if (dhcp->tries < REBOOT_TRIES) {
519 dhcp_discover(
netif);
532 struct dhcp *dhcp = netif_dhcp_data(
netif);
540 (
"dhcp_t1_timeout(): must renew\n"));
545 if (((dhcp->t2_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS))
547 dhcp->t1_renew_time = ((dhcp->t2_timeout - dhcp->lease_used) / 2);
560 struct dhcp *dhcp = netif_dhcp_data(
netif);
567 (
"dhcp_t2_timeout(): must rebind\n"));
572 if (((dhcp->t0_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS))
574 dhcp->t2_rebind_time = ((dhcp->t0_timeout - dhcp->lease_used) / 2);
587 struct dhcp *dhcp = netif_dhcp_data(
netif);
589 #if LWIP_DHCP_PROVIDE_DNS_SERVERS || LWIP_DHCP_GET_NTP_SRV
592 #if LWIP_DHCP_GET_NTP_SRV
597 ip4_addr_set_zero(&dhcp->offered_sn_mask);
598 ip4_addr_set_zero(&dhcp->offered_gw_addr);
599 #if LWIP_DHCP_BOOTP_FILE
600 ip4_addr_set_zero(&dhcp->offered_si_addr);
604 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_LEASE_TIME)) {
606 dhcp->offered_t0_lease = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_LEASE_TIME);
609 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_T1)) {
611 dhcp->offered_t1_renew = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_T1);
614 dhcp->offered_t1_renew = dhcp->offered_t0_lease / 2;
618 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_T2)) {
620 dhcp->offered_t2_rebind = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_T2);
623 dhcp->offered_t2_rebind = (dhcp->offered_t0_lease * 7U) / 8U;
627 ip4_addr_copy(dhcp->offered_ip_addr, dhcp->msg_in->yiaddr);
629 #if LWIP_DHCP_BOOTP_FILE
632 ip4_addr_copy(dhcp->offered_si_addr, dhcp->msg_in->siaddr);
636 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_SUBNET_MASK)) {
638 ip4_addr_set_u32(&dhcp->offered_sn_mask,
lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_SUBNET_MASK)));
639 dhcp->subnet_mask_given = 1;
641 dhcp->subnet_mask_given = 0;
645 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_ROUTER)) {
646 ip4_addr_set_u32(&dhcp->offered_gw_addr,
lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_ROUTER)));
649 #if LWIP_DHCP_GET_NTP_SRV
652 ip4_addr_set_u32(&ntp_server_addrs[n],
lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_NTP_SERVER + n)));
654 dhcp_set_ntp_servers(n, ntp_server_addrs);
657 #if LWIP_DHCP_PROVIDE_DNS_SERVERS
659 for (n = 0; (n < LWIP_DHCP_PROVIDE_DNS_SERVERS) && dhcp_option_given(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n); n++) {
661 ip_addr_set_ip4_u32(&dns_addr,
lwip_htonl(dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n)));
662 dns_setserver(n, &dns_addr);
676 dhcp_set_struct(
struct netif *
netif,
struct dhcp *dhcp)
683 memset(dhcp, 0,
sizeof(
struct dhcp));
685 netif_set_client_data(
netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP, dhcp);
703 netif_set_client_data(
netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP,
NULL);
728 dhcp = netif_dhcp_data(
netif);
732 if (
netif->
mtu < DHCP_MAX_MSG_LEN_MIN_REQUIRED) {
740 dhcp = (
struct dhcp *)
mem_malloc(
sizeof(
struct dhcp));
747 netif_set_client_data(
netif, LWIP_NETIF_CLIENT_DATA_INDEX_DHCP, dhcp);
755 if (dhcp->pcb_allocated != 0) {
756 dhcp_dec_pcb_refcount();
762 memset(dhcp, 0,
sizeof(
struct dhcp));
767 if (dhcp_inc_pcb_refcount() !=
ERR_OK) {
770 dhcp->pcb_allocated = 1;
772 #if LWIP_DHCP_CHECK_LINK_UP
782 result = dhcp_discover(
netif);
809 if (dhcp_inc_pcb_refcount() !=
ERR_OK) {
813 memset(&dhcp, 0,
sizeof(
struct dhcp));
820 dhcp_option_short(&dhcp, DHCP_MAX_MSG_LEN(
netif));
822 dhcp_option_trailer(&dhcp);
830 dhcp_delete_msg(&dhcp);
835 dhcp_dec_pcb_refcount();
846 struct dhcp *dhcp = netif_dhcp_data(
netif);
850 switch (dhcp->state) {
865 #if LWIP_DHCP_AUTOIP_COOP
866 if (dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
868 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
873 dhcp_discover(
netif);
878 #if DHCP_DOES_ARP_CHECK
887 dhcp_arp_reply(
struct netif *
netif,
const ip4_addr_t *addr)
892 dhcp = netif_dhcp_data(
netif);
897 ip4_addr_get_u32(addr)));
900 if (ip4_addr_cmp(addr, &dhcp->offered_ip_addr)) {
903 (
"dhcp_arp_reply(): arp reply matched with offered address, declining\n"));
921 struct dhcp *dhcp = netif_dhcp_data(
netif);
930 dhcp_option_long(dhcp,
lwip_ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
932 dhcp_option_trailer(dhcp);
938 dhcp_delete_msg(dhcp);
942 (
"dhcp_decline: could not allocate DHCP request\n"));
944 if (dhcp->tries < 255) {
948 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
963 struct dhcp *dhcp = netif_dhcp_data(
netif);
968 ip4_addr_set_any(&dhcp->offered_ip_addr);
976 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(
netif));
979 for (i = 0; i <
LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
980 dhcp_option_byte(dhcp, dhcp_discover_request_options[i]);
982 dhcp_option_trailer(dhcp);
990 dhcp_delete_msg(dhcp);
995 if (dhcp->tries < 255) {
998 #if LWIP_DHCP_AUTOIP_COOP
1000 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_ON;
1001 autoip_start(
netif);
1004 msecs = (dhcp->tries < 6 ? 1 << dhcp->tries : 60) * 1000;
1005 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1021 ip4_addr_t sn_mask, gw_addr;
1023 dhcp = netif_dhcp_data(
netif);
1028 dhcp->lease_used = 0;
1030 if (dhcp->offered_t0_lease != 0xffffffffUL) {
1033 timeout = (dhcp->offered_t0_lease + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
1034 if (timeout > 0xffff) {
1037 dhcp->t0_timeout = (
u16_t)timeout;
1038 if (dhcp->t0_timeout == 0) {
1039 dhcp->t0_timeout = 1;
1045 if (dhcp->offered_t1_renew != 0xffffffffUL) {
1048 timeout = (dhcp->offered_t1_renew + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
1049 if (timeout > 0xffff) {
1052 dhcp->t1_timeout = (
u16_t)timeout;
1053 if (dhcp->t1_timeout == 0) {
1054 dhcp->t1_timeout = 1;
1057 dhcp->t1_renew_time = dhcp->t1_timeout;
1060 if (dhcp->offered_t2_rebind != 0xffffffffUL) {
1062 timeout = (dhcp->offered_t2_rebind + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS;
1063 if (timeout > 0xffff) {
1066 dhcp->t2_timeout = (
u16_t)timeout;
1067 if (dhcp->t2_timeout == 0) {
1068 dhcp->t2_timeout = 1;
1071 dhcp->t2_rebind_time = dhcp->t2_timeout;
1075 if ((dhcp->t1_timeout >= dhcp->t2_timeout) && (dhcp->t2_timeout > 0)) {
1076 dhcp->t1_timeout = 0;
1079 if (dhcp->subnet_mask_given) {
1081 ip4_addr_copy(sn_mask, dhcp->offered_sn_mask);
1084 u8_t first_octet = ip4_addr1(&dhcp->offered_ip_addr);
1085 if (first_octet <= 127) {
1086 ip4_addr_set_u32(&sn_mask,
PP_HTONL(0xff000000UL));
1087 }
else if (first_octet >= 192) {
1088 ip4_addr_set_u32(&sn_mask,
PP_HTONL(0xffffff00UL));
1090 ip4_addr_set_u32(&sn_mask,
PP_HTONL(0xffff0000UL));
1094 ip4_addr_copy(gw_addr, dhcp->offered_gw_addr);
1096 if (ip4_addr_isany_val(gw_addr)) {
1098 ip4_addr_get_network(&gw_addr, &dhcp->offered_ip_addr, &sn_mask);
1100 ip4_addr_set_u32(&gw_addr, ip4_addr_get_u32(&gw_addr) |
PP_HTONL(0x00000001UL));
1103 #if LWIP_DHCP_AUTOIP_COOP
1104 if (dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
1106 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
1111 ip4_addr_get_u32(&dhcp->offered_ip_addr), ip4_addr_get_u32(&sn_mask), ip4_addr_get_u32(&gw_addr)));
1116 netif_set_addr(
netif, &dhcp->offered_ip_addr, &sn_mask, &gw_addr);
1129 struct dhcp *dhcp = netif_dhcp_data(
netif);
1140 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(
netif));
1143 for (i = 0; i <
LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
1144 dhcp_option_byte(dhcp, dhcp_discover_request_options[i]);
1147 #if LWIP_NETIF_HOSTNAME
1148 dhcp_option_hostname(dhcp,
netif);
1152 dhcp_option_trailer(dhcp);
1157 dhcp_delete_msg(dhcp);
1163 if (dhcp->tries < 255) {
1167 msecs = dhcp->tries < 10 ? dhcp->tries * 2000 : 20 * 1000;
1168 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1181 struct dhcp *dhcp = netif_dhcp_data(
netif);
1192 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN(
netif));
1195 for (i = 0; i <
LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
1196 dhcp_option_byte(dhcp, dhcp_discover_request_options[i]);
1199 #if LWIP_NETIF_HOSTNAME
1200 dhcp_option_hostname(dhcp,
netif);
1203 dhcp_option_trailer(dhcp);
1209 dhcp_delete_msg(dhcp);
1214 if (dhcp->tries < 255) {
1217 msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
1218 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1231 struct dhcp *dhcp = netif_dhcp_data(
netif);
1242 dhcp_option_short(dhcp, DHCP_MAX_MSG_LEN_MIN_REQUIRED);
1245 dhcp_option_long(dhcp,
lwip_ntohl(ip4_addr_get_u32(&dhcp->offered_ip_addr)));
1248 for (i = 0; i <
LWIP_ARRAYSIZE(dhcp_discover_request_options); i++) {
1249 dhcp_option_byte(dhcp, dhcp_discover_request_options[i]);
1252 dhcp_option_trailer(dhcp);
1258 dhcp_delete_msg(dhcp);
1263 if (dhcp->tries < 255) {
1266 msecs = dhcp->tries < 10 ? dhcp->tries * 1000 : 10 * 1000;
1267 dhcp->request_timeout = (msecs + DHCP_FINE_TIMER_MSECS - 1) / DHCP_FINE_TIMER_MSECS;
1282 struct dhcp *dhcp = netif_dhcp_data(
netif);
1285 u8_t is_dhcp_supplied_address;
1293 is_dhcp_supplied_address = dhcp_supplied_address(
netif);
1298 ip_addr_set_zero_ip4(&dhcp->server_ip_addr);
1299 ip4_addr_set_zero(&dhcp->offered_ip_addr);
1300 ip4_addr_set_zero(&dhcp->offered_sn_mask);
1301 ip4_addr_set_zero(&dhcp->offered_gw_addr);
1302 #if LWIP_DHCP_BOOTP_FILE
1303 ip4_addr_set_zero(&dhcp->offered_si_addr);
1305 dhcp->offered_t0_lease = dhcp->offered_t1_renew = dhcp->offered_t2_rebind = 0;
1306 dhcp->t1_renew_time = dhcp->t2_rebind_time = dhcp->lease_used = dhcp->t0_timeout = 0;
1308 if (!is_dhcp_supplied_address) {
1317 dhcp_option_long(dhcp,
lwip_ntohl(ip4_addr_get_u32(ip_2_ip4(&server_ip_addr))));
1319 dhcp_option_trailer(dhcp);
1324 dhcp_delete_msg(dhcp);
1331 netif_set_addr(
netif, IP4_ADDR_ANY4, IP4_ADDR_ANY4, IP4_ADDR_ANY4);
1347 dhcp = netif_dhcp_data(
netif);
1352 #if LWIP_DHCP_AUTOIP_COOP
1353 if (dhcp->autoip_coop_state == DHCP_AUTOIP_COOP_STATE_ON) {
1355 dhcp->autoip_coop_state = DHCP_AUTOIP_COOP_STATE_OFF;
1362 if (dhcp->pcb_allocated != 0) {
1363 dhcp_dec_pcb_refcount();
1364 dhcp->pcb_allocated = 0;
1375 dhcp_set_state(
struct dhcp *dhcp,
u8_t new_state)
1377 if (new_state != dhcp->state) {
1378 dhcp->state = new_state;
1380 dhcp->request_timeout = 0;
1390 dhcp_option(
struct dhcp *dhcp,
u8_t option_type,
u8_t option_len)
1392 LWIP_ASSERT(
"dhcp_option: dhcp->options_out_len + 2 + option_len <= DHCP_OPTIONS_LEN", dhcp->options_out_len + 2U + option_len <=
DHCP_OPTIONS_LEN);
1393 dhcp->msg_out->options[dhcp->options_out_len++] = option_type;
1394 dhcp->msg_out->options[dhcp->options_out_len++] = option_len;
1401 dhcp_option_byte(
struct dhcp *dhcp,
u8_t value)
1404 dhcp->msg_out->options[dhcp->options_out_len++] = value;
1408 dhcp_option_short(
struct dhcp *dhcp,
u16_t value)
1411 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t)((value & 0xff00U) >> 8);
1412 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t) (value & 0x00ffU);
1416 dhcp_option_long(
struct dhcp *dhcp,
u32_t value)
1419 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t)((value & 0xff000000UL) >> 24);
1420 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t)((value & 0x00ff0000UL) >> 16);
1421 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t)((value & 0x0000ff00UL) >> 8);
1422 dhcp->msg_out->options[dhcp->options_out_len++] = (
u8_t)((value & 0x000000ffUL));
1425 #if LWIP_NETIF_HOSTNAME
1427 dhcp_option_hostname(
struct dhcp *dhcp,
struct netif *
netif)
1433 const char *p =
netif->hostname;
1437 LWIP_ASSERT(
"DHCP: hostname is too long!", namelen <= available);
1438 len =
LWIP_MIN(namelen, available);
1439 LWIP_ASSERT(
"DHCP: hostname is too long!", len <= 0xFF);
1442 dhcp_option_byte(dhcp, *p++);
1460 dhcp_parse_reply(
struct dhcp *dhcp,
struct pbuf *p)
1466 u16_t options_idx_max;
1468 int parse_file_as_options = 0;
1469 int parse_sname_as_options = 0;
1472 dhcp_clear_all_options(dhcp);
1480 dhcp->boot_file_name[0] = 0;
1491 while ((q !=
NULL) && (options_idx >= q->
len)) {
1492 options_idx -= q->
len;
1493 options_idx_max -= q->
len;
1499 offset = options_idx;
1500 offset_max = options_idx_max;
1504 u8_t op = options[offset];
1506 u8_t decode_len = 0;
1507 int decode_idx = -1;
1508 u16_t val_offset = offset + 2;
1510 if ((offset + 1) < q->
len) {
1511 len = options[offset + 1];
1521 decode_len = len = 0;
1527 decode_idx = DHCP_OPTION_IDX_SUBNET_MASK;
1532 decode_idx = DHCP_OPTION_IDX_ROUTER;
1534 #if LWIP_DHCP_PROVIDE_DNS_SERVERS
1541 decode_idx = DHCP_OPTION_IDX_DNS_SERVER;
1546 decode_idx = DHCP_OPTION_IDX_LEASE_TIME;
1548 #if LWIP_DHCP_GET_NTP_SRV
1555 decode_idx = DHCP_OPTION_IDX_NTP_SERVER;
1562 decode_idx = DHCP_OPTION_IDX_OVERLOAD;
1566 decode_idx = DHCP_OPTION_IDX_MSG_TYPE;
1570 decode_idx = DHCP_OPTION_IDX_SERVER_ID;
1574 decode_idx = DHCP_OPTION_IDX_T1;
1578 decode_idx = DHCP_OPTION_IDX_T2;
1586 if (decode_len > 0) {
1590 LWIP_ASSERT(
"check decode_idx", decode_idx >= 0 && decode_idx < DHCP_OPTION_IDX_MAX);
1591 if (!dhcp_option_given(dhcp, decode_idx)) {
1592 copy_len =
LWIP_MIN(decode_len, 4);
1596 if (decode_len > 4) {
1599 dhcp_got_option(dhcp, decode_idx);
1600 dhcp_set_option_value(dhcp, decode_idx,
lwip_htonl(value));
1605 }
else if (decode_len == 4) {
1609 value = ((
u8_t*)&value)[0];
1611 dhcp_got_option(dhcp, decode_idx);
1612 dhcp_set_option_value(dhcp, decode_idx, value);
1615 if (offset >= q->
len) {
1617 offset_max -= q->
len;
1618 if ((offset < offset_max) && offset_max) {
1629 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_OVERLOAD)) {
1630 u32_t overload = dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_OVERLOAD);
1631 dhcp_clear_option(dhcp, DHCP_OPTION_IDX_OVERLOAD);
1633 parse_file_as_options = 1;
1636 parse_sname_as_options = 1;
1639 parse_sname_as_options = 1;
1640 parse_file_as_options = 1;
1645 #if LWIP_DHCP_BOOTP_FILE
1646 if (!parse_file_as_options) {
1648 if (dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE) &&
1649 (dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE) ==
DHCP_ACK))
1659 if (parse_file_as_options) {
1661 parse_file_as_options = 0;
1665 }
else if (parse_sname_as_options) {
1666 parse_sname_as_options = 0;
1678 dhcp_recv(
void *arg,
struct udp_pcb *pcb,
struct pbuf *p,
const ip_addr_t *addr,
u16_t port)
1681 struct dhcp *dhcp = netif_dhcp_data(
netif);
1689 if ((dhcp ==
NULL) || (dhcp->pcb_allocated == 0)) {
1690 goto free_pbuf_and_return;
1696 ip4_addr1_16(ip_2_ip4(addr)), ip4_addr2_16(ip_2_ip4(addr)), ip4_addr3_16(ip_2_ip4(addr)), ip4_addr4_16(ip_2_ip4(addr)), port));
1706 if (p->len < DHCP_MIN_REPLY_LEN) {
1708 goto free_pbuf_and_return;
1713 goto free_pbuf_and_return;
1721 goto free_pbuf_and_return;
1725 if (
lwip_ntohl(reply_msg->xid) != dhcp->xid) {
1727 (
"transaction id mismatch reply_msg->xid(%"X32_F")!=dhcp->xid(%"X32_F")\n",
lwip_ntohl(reply_msg->xid),dhcp->xid));
1728 goto free_pbuf_and_return;
1731 if (dhcp_parse_reply(dhcp, p) !=
ERR_OK) {
1733 (
"problem unfolding DHCP message - too short on memory?\n"));
1734 goto free_pbuf_and_return;
1739 if (!dhcp_option_given(dhcp, DHCP_OPTION_IDX_MSG_TYPE)) {
1741 goto free_pbuf_and_return;
1745 msg_type = (
u8_t)dhcp_get_option_value(dhcp, DHCP_OPTION_IDX_MSG_TYPE);
1751 dhcp_handle_ack(
netif);
1752 #if DHCP_DOES_ARP_CHECK
1768 dhcp_handle_ack(
netif);
1777 dhcp_handle_nak(
netif);
1782 dhcp->request_timeout = 0;
1784 dhcp_handle_offer(
netif);
1787 free_pbuf_and_return:
1789 dhcp->msg_in =
NULL;
1802 dhcp_create_msg(
struct netif *
netif,
struct dhcp *dhcp,
u8_t message_type)
1805 #ifndef DHCP_GLOBAL_XID
1810 #if DHCP_CREATE_RAND_XID && defined(LWIP_RAND)
1813 static u32_t xid = 0xABCD0000;
1816 if (!xid_initialised) {
1817 xid = DHCP_GLOBAL_XID;
1818 xid_initialised = !xid_initialised;
1823 LWIP_ASSERT(
"dhcp_create_msg: dhcp->p_out == NULL", dhcp->p_out ==
NULL);
1824 LWIP_ASSERT(
"dhcp_create_msg: dhcp->msg_out == NULL", dhcp->msg_out ==
NULL);
1826 if (dhcp->p_out ==
NULL) {
1828 (
"dhcp_create_msg(): could not allocate pbuf\n"));
1831 LWIP_ASSERT(
"dhcp_create_msg: check that first pbuf can hold struct dhcp_msg",
1832 (dhcp->p_out->len >=
sizeof(
struct dhcp_msg)));
1837 if (dhcp->tries == 0) {
1838 #if DHCP_CREATE_RAND_XID && defined(LWIP_RAND)
1847 (
"transaction id xid(%"X32_F")\n", xid));
1849 dhcp->msg_out = (
struct dhcp_msg *)dhcp->p_out->payload;
1855 dhcp->msg_out->hops = 0;
1857 dhcp->msg_out->secs = 0;
1860 dhcp->msg_out->flags = 0;
1861 ip4_addr_set_zero(&dhcp->msg_out->ciaddr);
1866 ip4_addr_copy(dhcp->msg_out->ciaddr, *netif_ip4_addr(
netif));
1868 ip4_addr_set_zero(&dhcp->msg_out->yiaddr);
1869 ip4_addr_set_zero(&dhcp->msg_out->siaddr);
1870 ip4_addr_set_zero(&dhcp->msg_out->giaddr);
1876 dhcp->msg_out->sname[i] = 0;
1879 dhcp->msg_out->file[i] = 0;
1882 dhcp->options_out_len = 0;
1885 dhcp->msg_out->options[i] = (
u8_t)i;
1889 dhcp_option_byte(dhcp, message_type);
1899 dhcp_delete_msg(
struct dhcp *dhcp)
1901 LWIP_ERROR(
"dhcp_delete_msg: dhcp != NULL", (dhcp !=
NULL),
return;);
1902 LWIP_ASSERT(
"dhcp_delete_msg: dhcp->p_out != NULL", dhcp->p_out !=
NULL);
1903 LWIP_ASSERT(
"dhcp_delete_msg: dhcp->msg_out != NULL", dhcp->msg_out !=
NULL);
1904 if (dhcp->p_out !=
NULL) {
1908 dhcp->msg_out =
NULL;
1920 dhcp_option_trailer(
struct dhcp *dhcp)
1922 LWIP_ERROR(
"dhcp_option_trailer: dhcp != NULL", (dhcp !=
NULL),
return;);
1923 LWIP_ASSERT(
"dhcp_option_trailer: dhcp->msg_out != NULL\n", dhcp->msg_out !=
NULL);
1930 dhcp->msg_out->options[dhcp->options_out_len++] = 0;
1941 dhcp_supplied_address(
const struct netif *
netif)
1944 struct dhcp* dhcp = netif_dhcp_data(
netif);