diff --git a/src/core/init.c b/src/core/init.c index dbd630dd..a02c8bd6 100644 --- a/src/core/init.c +++ b/src/core/init.c @@ -50,6 +50,7 @@ #include "lwip/udp.h" #include "lwip/priv/tcp_priv.h" #include "lwip/autoip.h" +#include "lwip/dhcp.h" #include "lwip/igmp.h" #include "lwip/dns.h" #include "lwip/timeouts.h" @@ -351,6 +352,9 @@ lwip_init(void) #if LWIP_AUTOIP autoip_init(); #endif /* LWIP_AUTOIP */ +#if LWIP_DHCP + dhcp_init(); +#endif /* LWIP_DHCP */ #if LWIP_IGMP igmp_init(); #endif /* LWIP_IGMP */ diff --git a/src/core/ipv4/dhcp.c b/src/core/ipv4/dhcp.c index af6fcde0..16c7a9b4 100644 --- a/src/core/ipv4/dhcp.c +++ b/src/core/ipv4/dhcp.c @@ -156,6 +156,7 @@ static u8_t xid_initialised; static struct udp_pcb *dhcp_pcb; static u8_t dhcp_pcb_refcount; +static u8_t dhcp_netif_client_id; /* DHCP client state machine functions */ static err_t dhcp_discover(struct netif *netif); @@ -248,7 +249,8 @@ dhcp_dec_pcb_refcount(void) static void dhcp_handle_nak(struct netif *netif) { - struct dhcp *dhcp = netif->dhcp; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_handle_nak(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num)); /* Change to a defined state - set this before assigning the address @@ -273,7 +275,7 @@ dhcp_handle_nak(struct netif *netif) static void dhcp_check(struct netif *netif) { - struct dhcp *dhcp = netif->dhcp; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; err_t result; u16_t msecs; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_check(netif=%p) %c%c\n", (void *)netif, (s16_t)netif->name[0], @@ -302,7 +304,8 @@ dhcp_check(struct netif *netif) static void dhcp_handle_offer(struct netif *netif) { - struct dhcp *dhcp = netif->dhcp; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_handle_offer(netif=%p) %c%c%"U16_F"\n", (void*)netif, netif->name[0], netif->name[1], (u16_t)netif->num)); /* obtain the server address */ @@ -333,7 +336,7 @@ dhcp_handle_offer(struct netif *netif) static err_t dhcp_select(struct netif *netif) { - struct dhcp *dhcp = netif->dhcp; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; err_t result; u16_t msecs; u8_t i; @@ -395,7 +398,7 @@ dhcp_coarse_tmr(void) /* iterate through all network interfaces */ while (netif != NULL) { /* only act on DHCP configured interfaces */ - struct dhcp* dhcp = netif->dhcp; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; if ((dhcp != NULL) && (dhcp->state != DHCP_STATE_OFF)) { /* compare lease time to expire timeout */ if (dhcp->t0_timeout && (++dhcp->lease_used == dhcp->t0_timeout)) { @@ -433,14 +436,15 @@ dhcp_fine_tmr(void) struct netif *netif = netif_list; /* loop through netif's */ while (netif != NULL) { + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; /* only act on DHCP configured interfaces */ - if (netif->dhcp != NULL) { + if (dhcp != NULL) { /* timer is active (non zero), and is about to trigger now */ - if (netif->dhcp->request_timeout > 1) { - netif->dhcp->request_timeout--; + if (dhcp->request_timeout > 1) { + dhcp->request_timeout--; } - else if (netif->dhcp->request_timeout == 1) { - netif->dhcp->request_timeout--; + else if (dhcp->request_timeout == 1) { + dhcp->request_timeout--; /* { netif->dhcp->request_timeout == 0 } */ LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_fine_tmr(): request timeout\n")); /* this client's request timeout triggered */ @@ -463,7 +467,8 @@ dhcp_fine_tmr(void) static void dhcp_timeout(struct netif *netif) { - struct dhcp *dhcp = netif->dhcp; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_timeout()\n")); /* back-off period has passed, or server selection timed out */ if ((dhcp->state == DHCP_STATE_BACKING_OFF) || (dhcp->state == DHCP_STATE_SELECTING)) { @@ -509,7 +514,8 @@ dhcp_timeout(struct netif *netif) static void dhcp_t1_timeout(struct netif *netif) { - struct dhcp *dhcp = netif->dhcp; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_t1_timeout()\n")); if ((dhcp->state == DHCP_STATE_REQUESTING) || (dhcp->state == DHCP_STATE_BOUND) || (dhcp->state == DHCP_STATE_RENEWING)) { @@ -521,9 +527,9 @@ dhcp_t1_timeout(struct netif *netif) DHCP_STATE_RENEWING, not DHCP_STATE_BOUND */ dhcp_renew(netif); /* Calculate next timeout */ - if (((netif->dhcp->t2_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS)) + if (((dhcp->t2_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS)) { - netif->dhcp->t1_renew_time = ((netif->dhcp->t2_timeout - dhcp->lease_used) / 2); + dhcp->t1_renew_time = ((dhcp->t2_timeout - dhcp->lease_used) / 2); } } } @@ -536,7 +542,8 @@ dhcp_t1_timeout(struct netif *netif) static void dhcp_t2_timeout(struct netif *netif) { - struct dhcp *dhcp = netif->dhcp; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_t2_timeout()\n")); if ((dhcp->state == DHCP_STATE_REQUESTING) || (dhcp->state == DHCP_STATE_BOUND) || (dhcp->state == DHCP_STATE_RENEWING) || (dhcp->state == DHCP_STATE_REBINDING)) { @@ -547,9 +554,9 @@ dhcp_t2_timeout(struct netif *netif) DHCP_STATE_REBINDING, not DHCP_STATE_BOUND */ dhcp_rebind(netif); /* Calculate next timeout */ - if (((netif->dhcp->t0_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS)) + if (((dhcp->t0_timeout - dhcp->lease_used) / 2) >= ((60 + DHCP_COARSE_TIMER_SECS / 2) / DHCP_COARSE_TIMER_SECS)) { - netif->dhcp->t2_rebind_time = ((netif->dhcp->t0_timeout - dhcp->lease_used) / 2); + dhcp->t2_rebind_time = ((dhcp->t0_timeout - dhcp->lease_used) / 2); } } } @@ -562,7 +569,8 @@ dhcp_t2_timeout(struct netif *netif) static void dhcp_handle_ack(struct netif *netif) { - struct dhcp *dhcp = netif->dhcp; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + #if LWIP_DNS || LWIP_DHCP_GET_NTP_SRV u8_t n; #endif /* LWIP_DNS || LWIP_DHCP_GET_NTP_SRV */ @@ -654,12 +662,12 @@ dhcp_set_struct(struct netif *netif, struct dhcp *dhcp) { LWIP_ASSERT("netif != NULL", netif != NULL); LWIP_ASSERT("dhcp != NULL", dhcp != NULL); - LWIP_ASSERT("netif already has a struct dhcp set", netif->dhcp == NULL); + LWIP_ASSERT("netif already has a struct dhcp set", netif->client_data[dhcp_netif_client_id] == NULL); /* clear data structure */ memset(dhcp, 0, sizeof(struct dhcp)); /* dhcp_set_state(&dhcp, DHCP_STATE_OFF); */ - netif->dhcp = dhcp; + netif->client_data[dhcp_netif_client_id] = dhcp; } /** @@ -675,9 +683,9 @@ void dhcp_cleanup(struct netif *netif) { LWIP_ASSERT("netif != NULL", netif != NULL); - if (netif->dhcp != NULL) { - mem_free(netif->dhcp); - netif->dhcp = NULL; + if (netif->client_data[dhcp_netif_client_id] != NULL) { + mem_free(netif->client_data[dhcp_netif_client_id]); + netif->client_data[dhcp_netif_client_id] = NULL; } } @@ -702,7 +710,7 @@ dhcp_start(struct netif *netif) LWIP_ERROR("netif != NULL", (netif != NULL), return ERR_ARG;); LWIP_ERROR("netif is not up, old style port?", netif_is_up(netif), return ERR_ARG;); - dhcp = netif->dhcp; + dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; 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)); /* check MTU of the netif */ @@ -721,7 +729,7 @@ dhcp_start(struct netif *netif) } /* store this dhcp client in the netif */ - netif->dhcp = dhcp; + netif->client_data[dhcp_netif_client_id] = dhcp; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): allocated dhcp")); /* already has DHCP client attached */ } else { @@ -820,7 +828,8 @@ dhcp_inform(struct netif *netif) void dhcp_network_changed(struct netif *netif) { - struct dhcp *dhcp = netif->dhcp; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + if (!dhcp) return; switch (dhcp->state) { @@ -862,15 +871,18 @@ dhcp_network_changed(struct netif *netif) void dhcp_arp_reply(struct netif *netif, const ip4_addr_t *addr) { + struct dhcp *dhcp; + LWIP_ERROR("netif != NULL", (netif != NULL), return;); + dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_arp_reply()\n")); /* is a DHCP client doing an ARP check? */ - if ((netif->dhcp != NULL) && (netif->dhcp->state == DHCP_STATE_CHECKING)) { + if ((dhcp != NULL) && (dhcp->state == DHCP_STATE_CHECKING)) { LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("dhcp_arp_reply(): CHECKING, arp reply for 0x%08"X32_F"\n", ip4_addr_get_u32(addr))); /* did a host respond with the address we were offered by the DHCP server? */ - if (ip4_addr_cmp(addr, &netif->dhcp->offered_ip_addr)) { + if (ip4_addr_cmp(addr, &dhcp->offered_ip_addr)) { /* we will not accept the offered address */ LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE | LWIP_DBG_LEVEL_WARNING, ("dhcp_arp_reply(): arp reply matched with offered address, declining\n")); @@ -891,7 +903,7 @@ dhcp_arp_reply(struct netif *netif, const ip4_addr_t *addr) static err_t dhcp_decline(struct netif *netif) { - struct dhcp *dhcp = netif->dhcp; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; err_t result = ERR_OK; u16_t msecs; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline()\n")); @@ -933,7 +945,7 @@ dhcp_decline(struct netif *netif) static err_t dhcp_discover(struct netif *netif) { - struct dhcp *dhcp = netif->dhcp; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; err_t result = ERR_OK; u16_t msecs; u8_t i; @@ -993,7 +1005,7 @@ dhcp_bind(struct netif *netif) struct dhcp *dhcp; ip4_addr_t sn_mask, gw_addr; LWIP_ERROR("dhcp_bind: netif != NULL", (netif != NULL), return;); - dhcp = netif->dhcp; + dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; LWIP_ERROR("dhcp_bind: dhcp != NULL", (dhcp != NULL), return;); 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)); @@ -1099,7 +1111,7 @@ dhcp_bind(struct netif *netif) err_t dhcp_renew(struct netif *netif) { - struct dhcp *dhcp = netif->dhcp; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; err_t result; u16_t msecs; u8_t i; @@ -1151,7 +1163,7 @@ dhcp_renew(struct netif *netif) static err_t dhcp_rebind(struct netif *netif) { - struct dhcp *dhcp = netif->dhcp; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; err_t result; u16_t msecs; u8_t i; @@ -1201,7 +1213,7 @@ dhcp_rebind(struct netif *netif) static err_t dhcp_reboot(struct netif *netif) { - struct dhcp *dhcp = netif->dhcp; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; err_t result; u16_t msecs; u8_t i; @@ -1252,7 +1264,7 @@ dhcp_reboot(struct netif *netif) err_t dhcp_release(struct netif *netif) { - struct dhcp *dhcp = netif->dhcp; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; err_t result; ip_addr_t server_ip_addr; u8_t is_dhcp_supplied_address; @@ -1317,7 +1329,7 @@ dhcp_stop(struct netif *netif) { struct dhcp *dhcp; LWIP_ERROR("dhcp_stop: netif != NULL", (netif != NULL), return;); - dhcp = netif->dhcp; + dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_stop()\n")); /* netif is DHCP configured? */ @@ -1642,7 +1654,7 @@ static void dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) { struct netif *netif = ip_current_input_netif(); - struct dhcp *dhcp = netif->dhcp; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; struct dhcp_msg *reply_msg = (struct dhcp_msg *)p->payload; u8_t msg_type; u8_t i; @@ -1904,13 +1916,17 @@ dhcp_option_trailer(struct dhcp *dhcp) u8_t dhcp_supplied_address(const struct netif *netif) { - if ((netif != NULL) && (netif->dhcp != NULL)) { - if ((netif->dhcp->state == DHCP_STATE_BOUND) || - (netif->dhcp->state == DHCP_STATE_RENEWING)) { - return 1; - } + if ((netif != NULL) && (netif->client_data[dhcp_netif_client_id] != NULL)) { + struct dhcp* dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + return (dhcp->state == DHCP_STATE_BOUND) || (dhcp->state == DHCP_STATE_RENEWING); } return 0; } +void +dhcp_init(void) +{ + dhcp_netif_client_id = netif_alloc_client_data_id(); +} + #endif /* LWIP_IPV4 && LWIP_DHCP */ diff --git a/src/core/netif.c b/src/core/netif.c index 5927d8f7..9bad9c29 100644 --- a/src/core/netif.c +++ b/src/core/netif.c @@ -241,10 +241,6 @@ netif_add(struct netif *netif, #endif /* LWIP_IPV6 */ NETIF_SET_CHECKSUM_CTRL(netif, NETIF_CHECKSUM_ENABLE_ALL); netif->flags = 0; -#if LWIP_DHCP - /* netif not under DHCP control by default */ - netif->dhcp = NULL; -#endif /* LWIP_DHCP */ #if LWIP_AUTOIP /* netif not under AutoIP control by default */ netif->autoip = NULL; @@ -259,10 +255,6 @@ netif_add(struct netif *netif, #if LWIP_IPV6_SEND_ROUTER_SOLICIT netif->rs_count = LWIP_ND6_MAX_MULTICAST_SOLICIT; #endif /* LWIP_IPV6_SEND_ROUTER_SOLICIT */ -#if LWIP_IPV6_DHCP6 - /* netif not under DHCPv6 control by default */ - netif->dhcp6 = NULL; -#endif /* LWIP_IPV6_DHCP6 */ #if LWIP_NETIF_STATUS_CALLBACK netif->status_callback = NULL; #endif /* LWIP_NETIF_STATUS_CALLBACK */ @@ -696,9 +688,7 @@ netif_set_link_up(struct netif *netif) netif->flags |= NETIF_FLAG_LINK_UP; #if LWIP_DHCP - if (netif->dhcp) { - dhcp_network_changed(netif); - } + dhcp_network_changed(netif); #endif /* LWIP_DHCP */ #if LWIP_AUTOIP @@ -961,7 +951,7 @@ u8_t netif_alloc_client_data_id(void) { u8_t result = netif_client_id; netif_client_id++; - LWIP_ASSERT("Increase LWIP_NUM_NETIF_CLIENT_DATA in lwipopts.h", netif_client_id