From 7f60cb3889f5f31299d34bc7fc1f9d1913d598c3 Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Thu, 18 Aug 2016 12:37:21 +0200 Subject: [PATCH] Improve netif client data API for lwIP internal clients - these can use a compile-time constant to access their data now --- src/core/init.c | 8 ----- src/core/ipv4/autoip.c | 44 ++++++++++--------------- src/core/ipv4/dhcp.c | 67 +++++++++++++++++--------------------- src/core/netif.c | 9 ++--- src/include/lwip/autoip.h | 1 - src/include/lwip/dhcp.h | 4 --- src/include/lwip/netif.h | 15 +++++++-- src/include/lwip/opt.h | 4 +-- test/unit/dhcp/test_dhcp.c | 32 +++++++++--------- test/unit/lwipopts.h | 1 + 10 files changed, 85 insertions(+), 100 deletions(-) diff --git a/src/core/init.c b/src/core/init.c index a02c8bd6..182aaff7 100644 --- a/src/core/init.c +++ b/src/core/init.c @@ -49,8 +49,6 @@ #include "lwip/raw.h" #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" @@ -349,12 +347,6 @@ lwip_init(void) #if LWIP_TCP tcp_init(); #endif /* LWIP_TCP */ -#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/autoip.c b/src/core/ipv4/autoip.c index 33249786..1da929c0 100644 --- a/src/core/ipv4/autoip.c +++ b/src/core/ipv4/autoip.c @@ -71,8 +71,6 @@ #include #include -static u8_t autoip_netif_client_id; - /** Pseudo random macro based on netif informations. * You could use "rand()" from the C Library if you define LWIP_AUTOIP_RAND in lwipopts.h */ #ifndef LWIP_AUTOIP_RAND @@ -80,7 +78,7 @@ static u8_t autoip_netif_client_id; ((u32_t)((netif->hwaddr[3]) & 0xff) << 16) | \ ((u32_t)((netif->hwaddr[2]) & 0xff) << 8) | \ ((u32_t)((netif->hwaddr[4]) & 0xff))) + \ - (netif->client_data[autoip_netif_client_id]? ((struct autoip*)netif->client_data[autoip_netif_client_id])->tried_llipaddr : 0)) + (netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP]? ((struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP])->tried_llipaddr : 0)) #endif /* LWIP_AUTOIP_RAND */ /** @@ -112,12 +110,12 @@ autoip_set_struct(struct netif *netif, struct autoip *autoip) LWIP_ASSERT("netif != NULL", netif != NULL); LWIP_ASSERT("autoip != NULL", autoip != NULL); LWIP_ASSERT("netif already has a struct autoip set", - netif->client_data[autoip_netif_client_id] == NULL); + netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP] == NULL); /* clear data structure */ memset(autoip, 0, sizeof(struct autoip)); /* autoip->state = AUTOIP_STATE_OFF; */ - netif->client_data[autoip_netif_client_id] = autoip; + netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP] = autoip; } /** Restart AutoIP client and check the next address (conflict detected) @@ -127,7 +125,7 @@ autoip_set_struct(struct netif *netif, struct autoip *autoip) static void autoip_restart(struct netif *netif) { - struct autoip* autoip = (struct autoip*)netif->client_data[autoip_netif_client_id]; + struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP]; autoip->tried_llipaddr++; autoip_start(netif); } @@ -138,7 +136,7 @@ autoip_restart(struct netif *netif) static void autoip_handle_arp_conflict(struct netif *netif) { - struct autoip* autoip = (struct autoip*)netif->client_data[autoip_netif_client_id]; + struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP]; /* RFC3927, 2.5 "Conflict Detection and Defense" allows two options where a) means retreat on the first conflict and @@ -171,7 +169,7 @@ autoip_handle_arp_conflict(struct netif *netif) static void autoip_create_addr(struct netif *netif, ip4_addr_t *ipaddr) { - struct autoip* autoip = (struct autoip*)netif->client_data[autoip_netif_client_id]; + struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP]; /* Here we create an IP-Address out of range 169.254.1.0 to 169.254.254.255 * compliant to RFC 3927 Section 2.1 @@ -206,7 +204,7 @@ autoip_create_addr(struct netif *netif, ip4_addr_t *ipaddr) static err_t autoip_arp_probe(struct netif *netif) { - struct autoip* autoip = (struct autoip*)netif->client_data[autoip_netif_client_id]; + struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP]; /* this works because netif->ip_addr is ANY */ return etharp_request(netif, &autoip->llipaddr); } @@ -230,7 +228,7 @@ autoip_arp_announce(struct netif *netif) static err_t autoip_bind(struct netif *netif) { - struct autoip* autoip = (struct autoip*)netif->client_data[autoip_netif_client_id]; + struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP]; ip4_addr_t sn_mask, gw_addr; LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, @@ -257,7 +255,7 @@ autoip_bind(struct netif *netif) err_t autoip_start(struct netif *netif) { - struct autoip* autoip = (struct autoip*)netif->client_data[autoip_netif_client_id]; + struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP]; err_t result = ERR_OK; LWIP_ERROR("netif is not up, old style port?", netif_is_up(netif), return ERR_ARG;); @@ -282,7 +280,7 @@ autoip_start(struct netif *netif) } memset(autoip, 0, sizeof(struct autoip)); /* store this AutoIP client in the netif */ - netif->client_data[autoip_netif_client_id] = autoip; + netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP] = autoip; LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_start(): allocated autoip")); } else { autoip->state = AUTOIP_STATE_OFF; @@ -301,7 +299,7 @@ autoip_start(struct netif *netif) static void autoip_start_probing(struct netif *netif) { - struct autoip* autoip = (struct autoip*)netif->client_data[autoip_netif_client_id]; + struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP]; autoip->state = AUTOIP_STATE_PROBING; autoip->sent_num = 0; @@ -335,7 +333,7 @@ autoip_start_probing(struct netif *netif) void autoip_network_changed(struct netif *netif) { - struct autoip* autoip = (struct autoip*)netif->client_data[autoip_netif_client_id]; + struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP]; if (autoip && (autoip->state != AUTOIP_STATE_OFF)) { autoip_start_probing(netif); @@ -351,7 +349,7 @@ autoip_network_changed(struct netif *netif) err_t autoip_stop(struct netif *netif) { - struct autoip* autoip = (struct autoip*)netif->client_data[autoip_netif_client_id]; + struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP]; if (autoip != NULL) { autoip->state = AUTOIP_STATE_OFF; @@ -371,7 +369,7 @@ autoip_tmr(void) struct netif *netif = netif_list; /* loop through netif's */ while (netif != NULL) { - struct autoip* autoip = (struct autoip*)netif->client_data[autoip_netif_client_id]; + struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP]; /* only act on AutoIP configured interfaces */ if (autoip != NULL) { if (autoip->lastconflict > 0) { @@ -456,7 +454,7 @@ autoip_tmr(void) void autoip_arp_reply(struct netif *netif, struct etharp_hdr *hdr) { - struct autoip* autoip = (struct autoip*)netif->client_data[autoip_netif_client_id]; + struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP]; LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_arp_reply()\n")); if ((autoip != NULL) && (autoip->state != AUTOIP_STATE_OFF)) { @@ -513,8 +511,8 @@ autoip_arp_reply(struct netif *netif, struct etharp_hdr *hdr) u8_t autoip_supplied_address(const struct netif *netif) { - if ((netif != NULL) && (netif->client_data[autoip_netif_client_id] != NULL)) { - struct autoip* autoip = (struct autoip*)netif->client_data[autoip_netif_client_id]; + if ((netif != NULL) && (netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP] != NULL)) { + struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP]; return (autoip->state == AUTOIP_STATE_BOUND) || (autoip->state == AUTOIP_STATE_ANNOUNCING); } return 0; @@ -523,14 +521,8 @@ autoip_supplied_address(const struct netif *netif) u8_t autoip_accept_packet(struct netif *netif, const ip4_addr_t *addr) { - struct autoip* autoip = (struct autoip*)netif->client_data[autoip_netif_client_id]; + struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP]; return (autoip != NULL) && ip4_addr_cmp(addr, &(autoip->llipaddr)); } -void -autoip_init(void) -{ - autoip_netif_client_id = netif_alloc_client_data_id(); -} - #endif /* LWIP_IPV4 && LWIP_AUTOIP */ diff --git a/src/core/ipv4/dhcp.c b/src/core/ipv4/dhcp.c index 2417a0fb..b1180627 100644 --- a/src/core/ipv4/dhcp.c +++ b/src/core/ipv4/dhcp.c @@ -156,7 +156,6 @@ static u8_t xid_initialised; static struct udp_pcb *dhcp_pcb; static u8_t dhcp_pcb_refcount; -u8_t dhcp_netif_client_id; /* DHCP client state machine functions */ static err_t dhcp_discover(struct netif *netif); @@ -249,7 +248,7 @@ dhcp_dec_pcb_refcount(void) static void dhcp_handle_nak(struct netif *netif) { - struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; 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)); @@ -275,7 +274,7 @@ dhcp_handle_nak(struct netif *netif) static void dhcp_check(struct netif *netif) { - struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; 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], @@ -304,7 +303,7 @@ dhcp_check(struct netif *netif) static void dhcp_handle_offer(struct netif *netif) { - struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; 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)); @@ -336,7 +335,7 @@ dhcp_handle_offer(struct netif *netif) static err_t dhcp_select(struct netif *netif) { - struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; err_t result; u16_t msecs; u8_t i; @@ -398,7 +397,7 @@ dhcp_coarse_tmr(void) /* iterate through all network interfaces */ while (netif != NULL) { /* only act on DHCP configured interfaces */ - struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; if ((dhcp != NULL) && (dhcp->state != DHCP_STATE_OFF)) { /* compare lease time to expire timeout */ if (dhcp->t0_timeout && (++dhcp->lease_used == dhcp->t0_timeout)) { @@ -436,7 +435,7 @@ 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]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; /* only act on DHCP configured interfaces */ if (dhcp != NULL) { /* timer is active (non zero), and is about to trigger now */ @@ -467,7 +466,7 @@ dhcp_fine_tmr(void) static void dhcp_timeout(struct netif *netif) { - struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_timeout()\n")); /* back-off period has passed, or server selection timed out */ @@ -514,7 +513,7 @@ dhcp_timeout(struct netif *netif) static void dhcp_t1_timeout(struct netif *netif) { - struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_t1_timeout()\n")); if ((dhcp->state == DHCP_STATE_REQUESTING) || (dhcp->state == DHCP_STATE_BOUND) || @@ -542,7 +541,7 @@ dhcp_t1_timeout(struct netif *netif) static void dhcp_t2_timeout(struct netif *netif) { - struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; 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) || @@ -569,7 +568,7 @@ dhcp_t2_timeout(struct netif *netif) static void dhcp_handle_ack(struct netif *netif) { - struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; #if LWIP_DNS || LWIP_DHCP_GET_NTP_SRV u8_t n; @@ -662,12 +661,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->client_data[dhcp_netif_client_id] == NULL); + LWIP_ASSERT("netif already has a struct dhcp set", netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP] == NULL); /* clear data structure */ memset(dhcp, 0, sizeof(struct dhcp)); /* dhcp_set_state(&dhcp, DHCP_STATE_OFF); */ - netif->client_data[dhcp_netif_client_id] = dhcp; + netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP] = dhcp; } /** @@ -683,9 +682,9 @@ void dhcp_cleanup(struct netif *netif) { LWIP_ASSERT("netif != NULL", netif != 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; + if (netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP] != NULL) { + mem_free(netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]); + netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP] = NULL; } } @@ -710,7 +709,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 = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; 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 */ @@ -729,7 +728,7 @@ dhcp_start(struct netif *netif) } /* store this dhcp client in the netif */ - netif->client_data[dhcp_netif_client_id] = dhcp; + netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP] = dhcp; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_start(): allocated dhcp")); /* already has DHCP client attached */ } else { @@ -828,7 +827,7 @@ dhcp_inform(struct netif *netif) void dhcp_network_changed(struct netif *netif) { - struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; if (!dhcp) return; @@ -874,7 +873,7 @@ 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]; + dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_arp_reply()\n")); /* is a DHCP client doing an ARP check? */ if ((dhcp != NULL) && (dhcp->state == DHCP_STATE_CHECKING)) { @@ -903,7 +902,7 @@ dhcp_arp_reply(struct netif *netif, const ip4_addr_t *addr) static err_t dhcp_decline(struct netif *netif) { - struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; err_t result = ERR_OK; u16_t msecs; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline()\n")); @@ -945,7 +944,7 @@ dhcp_decline(struct netif *netif) static err_t dhcp_discover(struct netif *netif) { - struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; err_t result = ERR_OK; u16_t msecs; u8_t i; @@ -1005,7 +1004,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 = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; 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)); @@ -1111,7 +1110,7 @@ dhcp_bind(struct netif *netif) err_t dhcp_renew(struct netif *netif) { - struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; err_t result; u16_t msecs; u8_t i; @@ -1163,7 +1162,7 @@ dhcp_renew(struct netif *netif) static err_t dhcp_rebind(struct netif *netif) { - struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; err_t result; u16_t msecs; u8_t i; @@ -1213,7 +1212,7 @@ dhcp_rebind(struct netif *netif) static err_t dhcp_reboot(struct netif *netif) { - struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; err_t result; u16_t msecs; u8_t i; @@ -1264,7 +1263,7 @@ dhcp_reboot(struct netif *netif) err_t dhcp_release(struct netif *netif) { - struct dhcp *dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; err_t result; ip_addr_t server_ip_addr; u8_t is_dhcp_supplied_address; @@ -1329,7 +1328,7 @@ dhcp_stop(struct netif *netif) { struct dhcp *dhcp; LWIP_ERROR("dhcp_stop: netif != NULL", (netif != NULL), return;); - dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_stop()\n")); /* netif is DHCP configured? */ @@ -1654,7 +1653,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 = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; struct dhcp_msg *reply_msg = (struct dhcp_msg *)p->payload; u8_t msg_type; u8_t i; @@ -1916,17 +1915,11 @@ dhcp_option_trailer(struct dhcp *dhcp) u8_t dhcp_supplied_address(const struct netif *netif) { - if ((netif != NULL) && (netif->client_data[dhcp_netif_client_id] != NULL)) { - struct dhcp* dhcp = (struct dhcp*)netif->client_data[dhcp_netif_client_id]; + if ((netif != NULL) && (netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP] != NULL)) { + struct dhcp* dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]; 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 cb759f5f..dfc25324 100644 --- a/src/core/netif.c +++ b/src/core/netif.c @@ -92,7 +92,7 @@ struct netif *netif_default; static u8_t netif_num; #if LWIP_NUM_NETIF_CLIENT_DATA > 0 -static u8_t netif_client_id = 0; +static u8_t netif_client_id; #endif #define NETIF_REPORT_TYPE_IPV4 0x01 @@ -245,7 +245,7 @@ netif_add(struct netif *netif, #endif /* LWIP_IPV6 */ NETIF_SET_CHECKSUM_CTRL(netif, NETIF_CHECKSUM_ENABLE_ALL); netif->flags = 0; -#if LWIP_NUM_NETIF_CLIENT_DATA > 0 +#if LWIP_DHCP || LWIP_AUTOIP || (LWIP_NUM_NETIF_CLIENT_DATA > 0) memset(netif->client_data, 0, sizeof(netif->client_data)); #endif /* LWIP_NUM_NETIF_CLIENT_DATA */ #if LWIP_IPV6_AUTOCONFIG @@ -948,8 +948,9 @@ 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<=LWIP_NUM_NETIF_CLIENT_DATA); - return result; + + LWIP_ASSERT("Increase LWIP_NUM_NETIF_CLIENT_DATA in lwipopts.h", result 0 - void* client_data[LWIP_NUM_NETIF_CLIENT_DATA]; +#if LWIP_DHCP || LWIP_AUTOIP || (LWIP_NUM_NETIF_CLIENT_DATA > 0) + void* client_data[LWIP_NETIF_CLIENT_DATA_INDEX_MAX + LWIP_NUM_NETIF_CLIENT_DATA]; #endif #if LWIP_IPV6_AUTOCONFIG /** is this netif enabled for IPv6 autoconfiguration */ diff --git a/src/include/lwip/opt.h b/src/include/lwip/opt.h index f65c7b8c..24a9fc68 100644 --- a/src/include/lwip/opt.h +++ b/src/include/lwip/opt.h @@ -1468,10 +1468,10 @@ /** * LWIP_NUM_NETIF_CLIENT_DATA: Number of clients that may store - * per data in client_data member array of struct netif. + * data in client_data member array of struct netif. */ #if !defined LWIP_NUM_NETIF_CLIENT_DATA || defined __DOXYGEN__ -#define LWIP_NUM_NETIF_CLIENT_DATA (LWIP_DHCP + LWIP_AUTOIP) +#define LWIP_NUM_NETIF_CLIENT_DATA 0 #endif /** * @} diff --git a/test/unit/dhcp/test_dhcp.c b/test/unit/dhcp/test_dhcp.c index 169a0431..850c213f 100644 --- a/test/unit/dhcp/test_dhcp.c +++ b/test/unit/dhcp/test_dhcp.c @@ -448,7 +448,7 @@ START_TEST(test_dhcp) dhcp_start(&net_test); fail_unless(txpacket == 1); /* DHCP discover sent */ - xid = ((struct dhcp*)net_test.client_data[dhcp_netif_client_id])->xid; /* Write bad xid, not using htonl! */ + xid = ((struct dhcp*)net_test.client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP])->xid; /* Write bad xid, not using htonl! */ memcpy(&dhcp_offer[46], &xid, 4); send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer)); @@ -458,17 +458,17 @@ START_TEST(test_dhcp) fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t))); fail_unless(txpacket == 1, "TX %d packets, expected 1", txpacket); /* Nothing more sent */ - xid = htonl(((struct dhcp*)net_test.client_data[dhcp_netif_client_id])->xid); + xid = htonl(((struct dhcp*)net_test.client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP])->xid); memcpy(&dhcp_offer[46], &xid, 4); /* insert correct transaction id */ send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer)); fail_unless(txpacket == 2, "TX %d packets, expected 2", txpacket); /* DHCP request sent */ - xid = ((struct dhcp*)net_test.client_data[dhcp_netif_client_id])->xid; /* Write bad xid, not using htonl! */ + xid = ((struct dhcp*)net_test.client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP])->xid; /* Write bad xid, not using htonl! */ memcpy(&dhcp_ack[46], &xid, 4); send_pkt(&net_test, dhcp_ack, sizeof(dhcp_ack)); fail_unless(txpacket == 2, "TX %d packets, still expected 2", txpacket); /* No more sent */ - xid = htonl(((struct dhcp*)net_test.client_data[dhcp_netif_client_id])->xid); /* xid updated */ + xid = htonl(((struct dhcp*)net_test.client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP])->xid); /* xid updated */ memcpy(&dhcp_ack[46], &xid, 4); /* insert transaction id */ send_pkt(&net_test, dhcp_ack, sizeof(dhcp_ack)); @@ -517,7 +517,7 @@ START_TEST(test_dhcp_nak) dhcp_start(&net_test); fail_unless(txpacket == 1); /* DHCP discover sent */ - xid = ((struct dhcp*)net_test.client_data[dhcp_netif_client_id])->xid; /* Write bad xid, not using htonl! */ + xid = ((struct dhcp*)net_test.client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP])->xid; /* Write bad xid, not using htonl! */ memcpy(&dhcp_offer[46], &xid, 4); send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer)); @@ -527,17 +527,17 @@ START_TEST(test_dhcp_nak) fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t))); fail_unless(txpacket == 1); /* Nothing more sent */ - xid = htonl(((struct dhcp*)net_test.client_data[dhcp_netif_client_id])->xid); + xid = htonl(((struct dhcp*)net_test.client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP])->xid); memcpy(&dhcp_offer[46], &xid, 4); /* insert correct transaction id */ send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer)); fail_unless(txpacket == 2); /* DHCP request sent */ - xid = ((struct dhcp*)net_test.client_data[dhcp_netif_client_id])->xid; /* Write bad xid, not using htonl! */ + xid = ((struct dhcp*)net_test.client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP])->xid; /* Write bad xid, not using htonl! */ memcpy(&dhcp_ack[46], &xid, 4); send_pkt(&net_test, dhcp_ack, sizeof(dhcp_ack)); fail_unless(txpacket == 2); /* No more sent */ - xid = htonl(((struct dhcp*)net_test.client_data[dhcp_netif_client_id])->xid); /* xid updated */ + xid = htonl(((struct dhcp*)net_test.client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP])->xid); /* xid updated */ memcpy(&dhcp_ack[46], &xid, 4); /* insert transaction id */ send_pkt(&net_test, dhcp_ack, sizeof(dhcp_ack)); @@ -742,13 +742,13 @@ START_TEST(test_dhcp_relayed) fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t))); fail_unless(txpacket == 1); /* Nothing more sent */ - xid = htonl(((struct dhcp*)net_test.client_data[dhcp_netif_client_id])->xid); + xid = htonl(((struct dhcp*)net_test.client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP])->xid); memcpy(&relay_offer[46], &xid, 4); /* insert correct transaction id */ send_pkt(&net_test, relay_offer, sizeof(relay_offer)); /* request sent? */ fail_unless(txpacket == 2, "txpkt = %d, should be 2", txpacket); - xid = htonl(((struct dhcp*)net_test.client_data[dhcp_netif_client_id])->xid); /* xid updated */ + xid = htonl(((struct dhcp*)net_test.client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP])->xid); /* xid updated */ memcpy(&relay_ack1[46], &xid, 4); /* insert transaction id */ send_pkt(&net_test, relay_ack1, sizeof(relay_ack1)); @@ -784,7 +784,7 @@ START_TEST(test_dhcp_relayed) fail_unless(txpacket == 7, "txpacket = %d", txpacket); fail_unless(netif_is_up(&net_test)); - xid = htonl(((struct dhcp*)net_test.client_data[dhcp_netif_client_id])->xid); /* xid updated */ + xid = htonl(((struct dhcp*)net_test.client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP])->xid); /* xid updated */ memcpy(&relay_ack2[46], &xid, 4); /* insert transaction id */ send_pkt(&net_test, relay_ack2, sizeof(relay_ack2)); @@ -873,7 +873,7 @@ START_TEST(test_dhcp_nak_no_endmarker) dhcp_start(&net_test); fail_unless(txpacket == 1); /* DHCP discover sent */ - xid = ((struct dhcp*)net_test.client_data[dhcp_netif_client_id])->xid; /* Write bad xid, not using htonl! */ + xid = ((struct dhcp*)net_test.client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP])->xid; /* Write bad xid, not using htonl! */ memcpy(&dhcp_offer[46], &xid, 4); send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer)); @@ -883,19 +883,19 @@ START_TEST(test_dhcp_nak_no_endmarker) fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t))); fail_unless(txpacket == 1); /* Nothing more sent */ - xid = htonl(((struct dhcp*)net_test.client_data[dhcp_netif_client_id])->xid); + xid = htonl(((struct dhcp*)net_test.client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP])->xid); memcpy(&dhcp_offer[46], &xid, 4); /* insert correct transaction id */ send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer)); - fail_unless(((struct dhcp*)net_test.client_data[dhcp_netif_client_id])->state == DHCP_STATE_REQUESTING); + fail_unless(((struct dhcp*)net_test.client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP])->state == DHCP_STATE_REQUESTING); fail_unless(txpacket == 2); /* No more sent */ - xid = htonl(((struct dhcp*)net_test.client_data[dhcp_netif_client_id])->xid); /* xid updated */ + xid = htonl(((struct dhcp*)net_test.client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP])->xid); /* xid updated */ memcpy(&dhcp_nack_no_endmarker[46], &xid, 4); /* insert transaction id */ send_pkt(&net_test, dhcp_nack_no_endmarker, sizeof(dhcp_nack_no_endmarker)); /* NAK should put us in another state for a while, no other way detecting it */ - fail_unless(((struct dhcp*)net_test.client_data[dhcp_netif_client_id])->state != DHCP_STATE_REQUESTING); + fail_unless(((struct dhcp*)net_test.client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP])->state != DHCP_STATE_REQUESTING); netif_remove(&net_test); } diff --git a/test/unit/lwipopts.h b/test/unit/lwipopts.h index d826042a..25252b42 100644 --- a/test/unit/lwipopts.h +++ b/test/unit/lwipopts.h @@ -54,6 +54,7 @@ /* Enable IGMP and MDNS for MDNS tests */ #define LWIP_IGMP 1 #define LWIP_MDNS_RESPONDER 1 +#define LWIP_NUM_NETIF_CLIENT_DATA (LWIP_MDNS_RESPONDER) /* Minimal changes to opt.h required for etharp unit tests: */ #define ETHARP_SUPPORT_STATIC_ENTRIES 1