Code cleanup in autoip.c and dhcp.c

This commit is contained in:
Dirk Ziegelmeier 2016-08-18 12:49:59 +02:00
parent 7f60cb3889
commit 97fae7e41b
2 changed files with 46 additions and 43 deletions

View File

@ -78,7 +78,7 @@
((u32_t)((netif->hwaddr[3]) & 0xff) << 16) | \
((u32_t)((netif->hwaddr[2]) & 0xff) << 8) | \
((u32_t)((netif->hwaddr[4]) & 0xff))) + \
(netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP]? ((struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP])->tried_llipaddr : 0))
(netif_autoip_data(netif)? netif_autoip_data(netif)->tried_llipaddr : 0))
#endif /* LWIP_AUTOIP_RAND */
/**
@ -95,6 +95,7 @@
static err_t autoip_arp_announce(struct netif *netif);
static void autoip_start_probing(struct netif *netif);
#define netif_autoip_data(netif) ((struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP])
/**
* @ingroup autoip
@ -110,7 +111,7 @@ 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[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP] == NULL);
netif_autoip_data(netif) == NULL);
/* clear data structure */
memset(autoip, 0, sizeof(struct autoip));
@ -125,7 +126,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[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP];
struct autoip* autoip = netif_autoip_data(netif);
autoip->tried_llipaddr++;
autoip_start(netif);
}
@ -136,7 +137,7 @@ autoip_restart(struct netif *netif)
static void
autoip_handle_arp_conflict(struct netif *netif)
{
struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP];
struct autoip* autoip = netif_autoip_data(netif);
/* RFC3927, 2.5 "Conflict Detection and Defense" allows two options where
a) means retreat on the first conflict and
@ -169,7 +170,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[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP];
struct autoip* autoip = netif_autoip_data(netif);
/* 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
@ -204,7 +205,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[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP];
struct autoip* autoip = netif_autoip_data(netif);
/* this works because netif->ip_addr is ANY */
return etharp_request(netif, &autoip->llipaddr);
}
@ -228,7 +229,7 @@ autoip_arp_announce(struct netif *netif)
static err_t
autoip_bind(struct netif *netif)
{
struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP];
struct autoip* autoip = netif_autoip_data(netif);
ip4_addr_t sn_mask, gw_addr;
LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
@ -255,7 +256,7 @@ autoip_bind(struct netif *netif)
err_t
autoip_start(struct netif *netif)
{
struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP];
struct autoip* autoip = netif_autoip_data(netif);
err_t result = ERR_OK;
LWIP_ERROR("netif is not up, old style port?", netif_is_up(netif), return ERR_ARG;);
@ -299,7 +300,7 @@ autoip_start(struct netif *netif)
static void
autoip_start_probing(struct netif *netif)
{
struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP];
struct autoip* autoip = netif_autoip_data(netif);
autoip->state = AUTOIP_STATE_PROBING;
autoip->sent_num = 0;
@ -333,7 +334,7 @@ autoip_start_probing(struct netif *netif)
void
autoip_network_changed(struct netif *netif)
{
struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP];
struct autoip* autoip = netif_autoip_data(netif);
if (autoip && (autoip->state != AUTOIP_STATE_OFF)) {
autoip_start_probing(netif);
@ -349,7 +350,7 @@ autoip_network_changed(struct netif *netif)
err_t
autoip_stop(struct netif *netif)
{
struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP];
struct autoip* autoip = netif_autoip_data(netif);
if (autoip != NULL) {
autoip->state = AUTOIP_STATE_OFF;
@ -369,7 +370,7 @@ autoip_tmr(void)
struct netif *netif = netif_list;
/* loop through netif's */
while (netif != NULL) {
struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP];
struct autoip* autoip = netif_autoip_data(netif);
/* only act on AutoIP configured interfaces */
if (autoip != NULL) {
if (autoip->lastconflict > 0) {
@ -454,7 +455,7 @@ autoip_tmr(void)
void
autoip_arp_reply(struct netif *netif, struct etharp_hdr *hdr)
{
struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP];
struct autoip* autoip = netif_autoip_data(netif);
LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_arp_reply()\n"));
if ((autoip != NULL) && (autoip->state != AUTOIP_STATE_OFF)) {
@ -511,8 +512,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[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP] != NULL)) {
struct autoip* autoip = (struct autoip*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP];
if ((netif != NULL) && (netif_autoip_data(netif) != NULL)) {
struct autoip* autoip = netif_autoip_data(netif);
return (autoip->state == AUTOIP_STATE_BOUND) || (autoip->state == AUTOIP_STATE_ANNOUNCING);
}
return 0;
@ -521,7 +522,7 @@ 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[LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP];
struct autoip* autoip = netif_autoip_data(netif);
return (autoip != NULL) && ip4_addr_cmp(addr, &(autoip->llipaddr));
}

View File

@ -193,6 +193,8 @@ static void dhcp_option_hostname(struct dhcp *dhcp, struct netif *netif);
/* always add the DHCP options trailer to end and pad */
static void dhcp_option_trailer(struct dhcp *dhcp);
#define netif_dhcp_data(netif) ((struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP])
/** Ensure DHCP PCB is allocated and bound */
static err_t
dhcp_inc_pcb_refcount(void)
@ -248,7 +250,7 @@ dhcp_dec_pcb_refcount(void)
static void
dhcp_handle_nak(struct netif *netif)
{
struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
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));
@ -274,7 +276,7 @@ dhcp_handle_nak(struct netif *netif)
static void
dhcp_check(struct netif *netif)
{
struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
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],
@ -303,7 +305,7 @@ dhcp_check(struct netif *netif)
static void
dhcp_handle_offer(struct netif *netif)
{
struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
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));
@ -335,7 +337,7 @@ dhcp_handle_offer(struct netif *netif)
static err_t
dhcp_select(struct netif *netif)
{
struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
err_t result;
u16_t msecs;
u8_t i;
@ -397,7 +399,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[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
if ((dhcp != NULL) && (dhcp->state != DHCP_STATE_OFF)) {
/* compare lease time to expire timeout */
if (dhcp->t0_timeout && (++dhcp->lease_used == dhcp->t0_timeout)) {
@ -435,7 +437,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[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
/* only act on DHCP configured interfaces */
if (dhcp != NULL) {
/* timer is active (non zero), and is about to trigger now */
@ -466,7 +468,7 @@ dhcp_fine_tmr(void)
static void
dhcp_timeout(struct netif *netif)
{
struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_timeout()\n"));
/* back-off period has passed, or server selection timed out */
@ -513,7 +515,7 @@ dhcp_timeout(struct netif *netif)
static void
dhcp_t1_timeout(struct netif *netif)
{
struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_STATE, ("dhcp_t1_timeout()\n"));
if ((dhcp->state == DHCP_STATE_REQUESTING) || (dhcp->state == DHCP_STATE_BOUND) ||
@ -541,7 +543,7 @@ dhcp_t1_timeout(struct netif *netif)
static void
dhcp_t2_timeout(struct netif *netif)
{
struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
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) ||
@ -568,7 +570,7 @@ dhcp_t2_timeout(struct netif *netif)
static void
dhcp_handle_ack(struct netif *netif)
{
struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
#if LWIP_DNS || LWIP_DHCP_GET_NTP_SRV
u8_t n;
@ -661,7 +663,7 @@ 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[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP] == NULL);
LWIP_ASSERT("netif already has a struct dhcp set", netif_dhcp_data(netif) == NULL);
/* clear data structure */
memset(dhcp, 0, sizeof(struct dhcp));
@ -682,8 +684,8 @@ void dhcp_cleanup(struct netif *netif)
{
LWIP_ASSERT("netif != NULL", netif != NULL);
if (netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP] != NULL) {
mem_free(netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP]);
if (netif_dhcp_data(netif) != NULL) {
mem_free(netif_dhcp_data(netif));
netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP] = NULL;
}
}
@ -709,7 +711,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[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
dhcp = netif_dhcp_data(netif);
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 */
@ -827,7 +829,7 @@ dhcp_inform(struct netif *netif)
void
dhcp_network_changed(struct netif *netif)
{
struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
if (!dhcp)
return;
@ -873,7 +875,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[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
dhcp = netif_dhcp_data(netif);
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)) {
@ -902,7 +904,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[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
err_t result = ERR_OK;
u16_t msecs;
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_decline()\n"));
@ -944,7 +946,7 @@ dhcp_decline(struct netif *netif)
static err_t
dhcp_discover(struct netif *netif)
{
struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
err_t result = ERR_OK;
u16_t msecs;
u8_t i;
@ -1004,7 +1006,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[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
dhcp = netif_dhcp_data(netif);
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));
@ -1110,7 +1112,7 @@ dhcp_bind(struct netif *netif)
err_t
dhcp_renew(struct netif *netif)
{
struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
err_t result;
u16_t msecs;
u8_t i;
@ -1162,7 +1164,7 @@ dhcp_renew(struct netif *netif)
static err_t
dhcp_rebind(struct netif *netif)
{
struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
err_t result;
u16_t msecs;
u8_t i;
@ -1212,7 +1214,7 @@ dhcp_rebind(struct netif *netif)
static err_t
dhcp_reboot(struct netif *netif)
{
struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
err_t result;
u16_t msecs;
u8_t i;
@ -1263,7 +1265,7 @@ dhcp_reboot(struct netif *netif)
err_t
dhcp_release(struct netif *netif)
{
struct dhcp *dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
err_t result;
ip_addr_t server_ip_addr;
u8_t is_dhcp_supplied_address;
@ -1328,7 +1330,7 @@ dhcp_stop(struct netif *netif)
{
struct dhcp *dhcp;
LWIP_ERROR("dhcp_stop: netif != NULL", (netif != NULL), return;);
dhcp = (struct dhcp*)netif->client_data[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
dhcp = netif_dhcp_data(netif);
LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_stop()\n"));
/* netif is DHCP configured? */
@ -1653,7 +1655,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[LWIP_NETIF_CLIENT_DATA_INDEX_DHCP];
struct dhcp *dhcp = netif_dhcp_data(netif);
struct dhcp_msg *reply_msg = (struct dhcp_msg *)p->payload;
u8_t msg_type;
u8_t i;
@ -1915,8 +1917,8 @@ dhcp_option_trailer(struct dhcp *dhcp)
u8_t
dhcp_supplied_address(const struct netif *netif)
{
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];
if ((netif != NULL) && (netif_dhcp_data(netif) != NULL)) {
struct dhcp* dhcp = netif_dhcp_data(netif);
return (dhcp->state == DHCP_STATE_BOUND) || (dhcp->state == DHCP_STATE_RENEWING);
}
return 0;