From 177c06b1f16bd81826934850f82dd3f0dafda595 Mon Sep 17 00:00:00 2001 From: sg Date: Thu, 20 Aug 2015 22:39:48 +0200 Subject: [PATCH] - prework for fixing bug #45029: access IPv4 configuration of struct netif via new API (netif_ip4_addr()/netif_ip4_netmask()/netif_ip4_gw()) instead of accessing the struct member directly. This way, we can change the struct member types from ip4_addr_t to ip_addr_t; - fixed some bugs in calls to ip4_addr*() where the cast to u8_t* did not reveal the wrong address type --- src/core/dhcp.c | 9 ++++++-- src/core/dns.c | 2 +- src/core/ipv4/autoip.c | 2 +- src/core/ipv4/ip4.c | 34 +++++++++++++++--------------- src/core/ipv4/ip4_addr.c | 10 ++++----- src/core/ipv6/ip6_addr.c | 2 +- src/core/netif.c | 42 ++++++++++++++++++------------------- src/core/snmp/mib2.c | 24 ++++++++++----------- src/core/udp.c | 10 ++++----- src/include/lwip/ip4.h | 2 +- src/include/lwip/ip4_addr.h | 8 +++---- src/include/lwip/ip_addr.h | 2 +- src/include/lwip/netif.h | 6 ++++++ src/include/netif/etharp.h | 2 +- src/netif/etharp.c | 18 ++++++++-------- 15 files changed, 92 insertions(+), 81 deletions(-) diff --git a/src/core/dhcp.c b/src/core/dhcp.c index 23b7eed3..13c009aa 100644 --- a/src/core/dhcp.c +++ b/src/core/dhcp.c @@ -1591,8 +1591,13 @@ dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, struct dhcp_msg *reply_msg = (struct dhcp_msg *)p->payload; u8_t msg_type; u8_t i; + +#if LWIP_IPV6 + LWIP_ASSERT("invalid server address type", !IP_IS_V6(addr)); +#endif /* LWIP_IPV6 */ + LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("dhcp_recv(pbuf = %p) from DHCP server %"U16_F".%"U16_F".%"U16_F".%"U16_F" port %"U16_F"\n", (void*)p, - ip4_addr1_16(addr), ip4_addr2_16(addr), ip4_addr3_16(addr), ip4_addr4_16(addr), port)); + 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)); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->len = %"U16_F"\n", p->len)); LWIP_DEBUGF(DHCP_DEBUG | LWIP_DBG_TRACE, ("pbuf->tot_len = %"U16_F"\n", p->tot_len)); /* prevent warnings about unused arguments */ @@ -1753,7 +1758,7 @@ dhcp_create_msg(struct netif *netif, struct dhcp *dhcp, u8_t message_type) if ((message_type == DHCP_INFORM) || (message_type == DHCP_DECLINE) || (message_type == DHCP_RELEASE) || ((message_type == DHCP_REQUEST) && /* DHCP_STATE_BOUND not used for sending! */ ((dhcp->state== DHCP_STATE_RENEWING) || dhcp->state== DHCP_STATE_REBINDING))) { - ip4_addr_copy(dhcp->msg_out->ciaddr, netif->ip_addr); + ip4_addr_copy(dhcp->msg_out->ciaddr, *netif_ip4_addr(netif)); } ip4_addr_set_zero(&dhcp->msg_out->yiaddr); ip4_addr_set_zero(&dhcp->msg_out->siaddr); diff --git a/src/core/dns.c b/src/core/dns.c index 8c71202d..c11b08a5 100644 --- a/src/core/dns.c +++ b/src/core/dns.c @@ -1094,7 +1094,7 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, /* read the IP address after answer resource record's header */ pbuf_copy_partial(p, &(entry->ipaddr), sizeof(entry->ipaddr), res_idx); LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: \"%s\": response = ", entry->name)); - ip4_addr_debug_print(DNS_DEBUG, (&(entry->ipaddr))); + ip_addr_debug_print(DNS_DEBUG, (&(entry->ipaddr))); LWIP_DEBUGF(DNS_DEBUG, ("\n")); /* call specified callback function if provided */ dns_call_found(entry_idx, &entry->ipaddr); diff --git a/src/core/ipv4/autoip.c b/src/core/ipv4/autoip.c index 920d67ff..7b19d3e6 100644 --- a/src/core/ipv4/autoip.c +++ b/src/core/ipv4/autoip.c @@ -373,7 +373,7 @@ autoip_stop(struct netif *netif) { if (netif->autoip) { netif->autoip->state = AUTOIP_STATE_OFF; - if (ip4_addr_islinklocal(&netif->ip_addr)) { + if (ip4_addr_islinklocal(netif_ip4_addr(netif))) { netif_set_addr(netif, IP4_ADDR_ANY, IP4_ADDR_ANY, IP4_ADDR_ANY); } } diff --git a/src/core/ipv4/ip4.c b/src/core/ipv4/ip4.c index b57bd91f..d1c47f11 100644 --- a/src/core/ipv4/ip4.c +++ b/src/core/ipv4/ip4.c @@ -144,9 +144,9 @@ ip4_route(const ip4_addr_t *dest) /* iterate through netifs */ for (netif = netif_list; netif != NULL; netif = netif->next) { /* is the netif up, does it have a link and a valid address? */ - if (netif_is_up(netif) && netif_is_link_up(netif) && !ip4_addr_isany_val(netif->ip_addr)) { + if (netif_is_up(netif) && netif_is_link_up(netif) && !ip4_addr_isany_val(*netif_ip4_addr(netif))) { /* network mask matches? */ - if (ip4_addr_netcmp(dest, &(netif->ip_addr), &(netif->netmask))) { + if (ip4_addr_netcmp(dest, netif_ip4_addr(netif), netif_ip4_netmask(netif))) { /* return netif on which to forward IP packet */ return netif; } @@ -154,7 +154,7 @@ ip4_route(const ip4_addr_t *dest) } if ((netif_default == NULL) || !netif_is_up(netif_default) || !netif_is_link_up(netif_default) || - ip4_addr_isany_val(netif_default->ip_addr)) { + ip4_addr_isany_val(*netif_ip4_addr(netif_default))) { /* No matching netif found an default netif is not usable. If this is not good enough for you, use LWIP_HOOK_IP4_ROUTE() */ LWIP_DEBUGF(IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("ip_route: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", @@ -247,8 +247,8 @@ ip4_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp) /* RFC3927 2.7: do not forward link-local addresses */ if (ip4_addr_islinklocal(ip4_current_dest_addr())) { LWIP_DEBUGF(IP_DEBUG, ("ip_forward: not forwarding LLA %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", - ip4_addr1_16(ip_current_dest_addr()), ip4_addr2_16(ip_current_dest_addr()), - ip4_addr3_16(ip_current_dest_addr()), ip4_addr4_16(ip_current_dest_addr()))); + ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()), + ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr()))); goto return_noroute; } @@ -256,8 +256,8 @@ ip4_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp) netif = ip4_route(ip4_current_dest_addr()); if (netif == NULL) { LWIP_DEBUGF(IP_DEBUG, ("ip_forward: no forwarding route for %"U16_F".%"U16_F".%"U16_F".%"U16_F" found\n", - ip4_addr1_16(ip_current_dest_addr()), ip4_addr2_16(ip_current_dest_addr()), - ip4_addr3_16(ip_current_dest_addr()), ip4_addr4_16(ip_current_dest_addr()))); + ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()), + ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr()))); /* @todo: send ICMP_DUR_NET? */ goto return_noroute; } @@ -292,8 +292,8 @@ ip4_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp) } LWIP_DEBUGF(IP_DEBUG, ("ip_forward: forwarding packet to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", - ip4_addr1_16(ip_current_dest_addr()), ip4_addr2_16(ip_current_dest_addr()), - ip4_addr3_16(ip_current_dest_addr()), ip4_addr4_16(ip_current_dest_addr()))); + ip4_addr1_16(ip4_current_dest_addr()), ip4_addr2_16(ip4_current_dest_addr()), + ip4_addr3_16(ip4_current_dest_addr()), ip4_addr4_16(ip4_current_dest_addr()))); IP_STATS_INC(ip.fw); IP_STATS_INC(ip.xmit); @@ -462,15 +462,15 @@ ip4_input(struct pbuf *p, struct netif *inp) netif = inp; do { LWIP_DEBUGF(IP_DEBUG, ("ip_input: iphdr->dest 0x%"X32_F" netif->ip_addr 0x%"X32_F" (0x%"X32_F", 0x%"X32_F", 0x%"X32_F")\n", - ip4_addr_get_u32(&iphdr->dest), ip4_addr_get_u32(&netif->ip_addr), - ip4_addr_get_u32(&iphdr->dest) & ip4_addr_get_u32(&netif->netmask), - ip4_addr_get_u32(&netif->ip_addr) & ip4_addr_get_u32(&netif->netmask), - ip4_addr_get_u32(&iphdr->dest) & ~ip4_addr_get_u32(&netif->netmask))); + ip4_addr_get_u32(&iphdr->dest), ip4_addr_get_u32(netif_ip4_addr(netif)), + ip4_addr_get_u32(&iphdr->dest) & ip4_addr_get_u32(netif_ip4_netmask(netif)), + ip4_addr_get_u32(netif_ip4_addr(netif)) & ip4_addr_get_u32(netif_ip4_netmask(netif)), + ip4_addr_get_u32(&iphdr->dest) & ~ip4_addr_get_u32(netif_ip4_netmask(netif)))); /* interface is up and configured? */ - if ((netif_is_up(netif)) && (!ip4_addr_isany_val(netif->ip_addr))) { + if ((netif_is_up(netif)) && (!ip4_addr_isany_val(*netif_ip4_addr(netif)))) { /* unicast to this interface address? */ - if (ip4_addr_cmp(ip4_current_dest_addr(), &(netif->ip_addr)) || + if (ip4_addr_cmp(ip4_current_dest_addr(), netif_ip4_addr(netif)) || /* or broadcast on this interface network address? */ ip_addr_isbroadcast(ip_current_dest_addr(), netif) #if LWIP_NETIF_LOOPBACK && !LWIP_HAVE_LOOPIF @@ -736,7 +736,7 @@ err_t ip4_output_if_opt(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t const ip4_addr_t *src_used = src; if (dest != IP_HDRINCL) { if (ip4_addr_isany(src)) { - src_used = &netif->ip_addr; + src_used = netif_ip4_addr(netif); } } @@ -886,7 +886,7 @@ err_t ip4_output_if_opt_src(struct pbuf *p, const ip4_addr_t *src, const ip4_add ip4_debug_print(p); #if ENABLE_LOOPBACK - if (ip4_addr_cmp(dest, &netif->ip_addr) + if (ip4_addr_cmp(dest, netif_ip4_addr(netif)) #if !LWIP_HAVE_LOOPIF || ip4_addr_isloopback(dest) #endif /* !LWIP_HAVE_LOOPIF */ diff --git a/src/core/ipv4/ip4_addr.c b/src/core/ipv4/ip4_addr.c index 5f4f5fa3..318a4835 100644 --- a/src/core/ipv4/ip4_addr.c +++ b/src/core/ipv4/ip4_addr.c @@ -70,13 +70,13 @@ ip4_addr_isbroadcast_u32(u32_t addr, const struct netif *netif) * nor can we check against any broadcast addresses */ return 0; /* address matches network interface address exactly? => no broadcast */ - } else if (addr == ip4_addr_get_u32(&netif->ip_addr)) { + } else if (addr == ip4_addr_get_u32(netif_ip4_addr(netif))) { return 0; /* on the same (sub) network... */ - } else if (ip4_addr_netcmp(&ipaddr, &(netif->ip_addr), &(netif->netmask)) + } else if (ip4_addr_netcmp(&ipaddr, netif_ip4_addr(netif), netif_ip4_netmask(netif)) /* ...and host identifier bits are all ones? =>... */ - && ((addr & ~ip4_addr_get_u32(&netif->netmask)) == - (IPADDR_BROADCAST & ~ip4_addr_get_u32(&netif->netmask)))) { + && ((addr & ~ip4_addr_get_u32(netif_ip4_netmask(netif))) == + (IPADDR_BROADCAST & ~ip4_addr_get_u32(netif_ip4_netmask(netif))))) { /* => network broadcast address */ return 1; } else { @@ -325,7 +325,7 @@ ip4_2_ip(const ip4_addr_t *ip4addr, ip_addr_t* storage) if ((ip4addr == NULL) || (storage == NULL)) { return NULL; } - ip4_addr_copy(storage->addr.ip4, *ip4addr); + ip4_addr_copy(storage->u_addr.ip4, *ip4addr); IP_SET_TYPE_VAL(*storage, IPADDR_TYPE_V4); return storage; } diff --git a/src/core/ipv6/ip6_addr.c b/src/core/ipv6/ip6_addr.c index caeddd38..bb3c609d 100644 --- a/src/core/ipv6/ip6_addr.c +++ b/src/core/ipv6/ip6_addr.c @@ -285,7 +285,7 @@ ip6_2_ip(const ip6_addr_t *ip6addr, ip_addr_t* storage) if ((ip6addr == NULL) || (storage == NULL)) { return NULL; } - ip6_addr_copy(storage->addr.ip6, *ip6addr); + ip6_addr_copy(storage->u_addr.ip6, *ip6addr); IP_SET_TYPE_VAL(*storage, IPADDR_TYPE_V6); return storage; } diff --git a/src/core/netif.c b/src/core/netif.c index a07f1df4..2fb48266 100644 --- a/src/core/netif.c +++ b/src/core/netif.c @@ -320,9 +320,9 @@ netif_remove(struct netif *netif) } #if LWIP_IPV4 - if (!ip4_addr_isany_val(netif->ip_addr)) { + if (!ip4_addr_isany_val(*netif_ip4_addr(netif))) { #if LWIP_TCP - tcp_netif_ipv4_addr_changed(&netif->ip_addr, NULL); + tcp_netif_ipv4_addr_changed(netif_ip4_addr(netif), NULL); #endif /* LWIP_TCP */ /* cannot do this for UDP, as there is no 'err' callback in udp pcbs */ } @@ -421,19 +421,19 @@ netif_set_ipaddr(struct netif *netif, const ip4_addr_t *ipaddr) { ip4_addr_t new_addr = (ipaddr ? *ipaddr : *IP4_ADDR_ANY); /* address is actually being changed? */ - if (ip4_addr_cmp(&new_addr, &(netif->ip_addr)) == 0) { + if (ip4_addr_cmp(&new_addr, netif_ip4_addr(netif)) == 0) { LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_STATE, ("netif_set_ipaddr: netif address being changed\n")); #if LWIP_TCP - tcp_netif_ipv4_addr_changed(&netif->ip_addr, ipaddr); + tcp_netif_ipv4_addr_changed(netif_ip4_addr(netif), ipaddr); #endif /* LWIP_TCP */ #if LWIP_UDP - udp_netif_ipv4_addr_changed(&netif->ip_addr, ipaddr); + udp_netif_ipv4_addr_changed(netif_ip4_addr(netif), ipaddr); #endif /* LWIP_UDP */ snmp_delete_ipaddridx_tree(netif); snmp_delete_iprteidx_tree(0, netif); /* set new IP address to netif */ - ip4_addr_set(&(netif->ip_addr), ipaddr); + ip4_addr_set(netif_ip4_addr(netif), ipaddr); snmp_insert_ipaddridx_tree(netif); snmp_insert_iprteidx_tree(0, netif); @@ -444,10 +444,10 @@ netif_set_ipaddr(struct netif *netif, const ip4_addr_t *ipaddr) LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: IP address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", netif->name[0], netif->name[1], - ip4_addr1_16(&netif->ip_addr), - ip4_addr2_16(&netif->ip_addr), - ip4_addr3_16(&netif->ip_addr), - ip4_addr4_16(&netif->ip_addr))); + ip4_addr1_16(netif_ip4_addr(netif)), + ip4_addr2_16(netif_ip4_addr(netif)), + ip4_addr3_16(netif_ip4_addr(netif)), + ip4_addr4_16(netif_ip4_addr(netif)))); } /** @@ -461,13 +461,13 @@ netif_set_ipaddr(struct netif *netif, const ip4_addr_t *ipaddr) void netif_set_gw(struct netif *netif, const ip4_addr_t *gw) { - ip4_addr_set(&(netif->gw), gw); + ip4_addr_set(netif_ip4_gw(netif), gw); LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: GW address of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", netif->name[0], netif->name[1], - ip4_addr1_16(&netif->gw), - ip4_addr2_16(&netif->gw), - ip4_addr3_16(&netif->gw), - ip4_addr4_16(&netif->gw))); + ip4_addr1_16(netif_ip4_gw(netif)), + ip4_addr2_16(netif_ip4_gw(netif)), + ip4_addr3_16(netif_ip4_gw(netif)), + ip4_addr4_16(netif_ip4_gw(netif)))); } /** @@ -484,14 +484,14 @@ netif_set_netmask(struct netif *netif, const ip4_addr_t *netmask) { snmp_delete_iprteidx_tree(0, netif); /* set new netmask to netif */ - ip4_addr_set(&(netif->netmask), netmask); + ip4_addr_set(netif_ip4_netmask(netif), netmask); snmp_insert_iprteidx_tree(0, netif); LWIP_DEBUGF(NETIF_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_STATE, ("netif: netmask of interface %c%c set to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n", netif->name[0], netif->name[1], - ip4_addr1_16(&netif->netmask), - ip4_addr2_16(&netif->netmask), - ip4_addr3_16(&netif->netmask), - ip4_addr4_16(&netif->netmask))); + ip4_addr1_16(netif_ip4_netmask(netif)), + ip4_addr2_16(netif_ip4_netmask(netif)), + ip4_addr3_16(netif_ip4_netmask(netif)), + ip4_addr4_16(netif_ip4_netmask(netif)))); } #endif /* LWIP_IPV4 */ @@ -549,7 +549,7 @@ netif_issue_reports(struct netif* netif, u8_t report_type) { #if LWIP_IPV4 if ((report_type & NETIF_REPORT_TYPE_IPV4) && - !ip4_addr_isany_val(netif->ip_addr)) { + !ip4_addr_isany_val(*netif_ip4_addr(netif))) { #if LWIP_ARP /* For Ethernet network interfaces, we would like to send a "gratuitous ARP" */ if (netif->flags & (NETIF_FLAG_ETHARP)) { diff --git a/src/core/snmp/mib2.c b/src/core/snmp/mib2.c index 89ab1307..6fa1ae58 100644 --- a/src/core/snmp/mib2.c +++ b/src/core/snmp/mib2.c @@ -1304,7 +1304,7 @@ void snmp_insert_ipaddridx_tree(struct netif *ni) u8_t level; LWIP_ASSERT("ni != NULL", ni != NULL); - snmp_iptooid(&ni->ip_addr, &ipaddridx[0]); + snmp_iptooid(netif_ip4_addr(ni), &ipaddridx[0]); level = 0; ipa_rn = &ipaddrtree_root; @@ -1358,7 +1358,7 @@ void snmp_delete_ipaddridx_tree(struct netif *ni) u8_t fc, level, del_cnt; LWIP_ASSERT("ni != NULL", ni != NULL); - snmp_iptooid(&ni->ip_addr, &ipaddridx[0]); + snmp_iptooid(netif_ip4_addr(ni), &ipaddridx[0]); /* mark nodes for deletion */ level = 0; @@ -1431,7 +1431,7 @@ void snmp_insert_iprteidx_tree(u8_t dflt, struct netif *ni) else { /* route to the network address */ - ip4_addr_get_network(&dst, &ni->ip_addr, &ni->netmask); + ip4_addr_get_network(&dst, netif_ip4_addr(ni), netif_ip4_netmask(ni)); /* exclude 0.0.0.0 network (reserved for default rte) */ if (!ip4_addr_isany_val(dst)) { insert = 1; @@ -1508,7 +1508,7 @@ void snmp_delete_iprteidx_tree(u8_t dflt, struct netif *ni) else { /* route to the network address */ - ip4_addr_get_network(&dst, &ni->ip_addr, &ni->netmask); + ip4_addr_get_network(&dst, netif_ip4_addr(ni), netif_ip4_netmask(ni)); /* exclude 0.0.0.0 network (reserved for default rte) */ if (!ip4_addr_isany_val(dst)) { del = 1; @@ -3084,7 +3084,7 @@ ip_addrentry_get_value(struct obj_def *od, u16_t len, void *value) LWIP_UNUSED_ARG(len); snmp_oidtoip(&od->id_inst_ptr[1], &ip); ifidx = 0; - while ((netif != NULL) && !ip4_addr_cmp(&ip, &netif->ip_addr)) + while ((netif != NULL) && !ip4_addr_cmp(&ip, netif_ip4_addr(netif))) { netif = netif->next; ifidx++; @@ -3099,7 +3099,7 @@ ip_addrentry_get_value(struct obj_def *od, u16_t len, void *value) case 1: /* ipAdEntAddr */ { ip4_addr_t *dst = (ip4_addr_t*)value; - *dst = netif->ip_addr; + *dst = *netif_ip4_addr(netif); } break; case 2: /* ipAdEntIfIndex */ @@ -3111,7 +3111,7 @@ ip_addrentry_get_value(struct obj_def *od, u16_t len, void *value) case 3: /* ipAdEntNetMask */ { ip4_addr_t *dst = (ip4_addr_t*)value; - *dst = netif->netmask; + *dst = *netif_ip4_netmask(netif); } break; case 4: /* ipAdEntBcastAddr */ @@ -3238,7 +3238,7 @@ ip_rteentry_get_value(struct obj_def *od, u16_t len, void *value) /* not using ip_route(), need exact match! */ netif = netif_list; while ((netif != NULL) && - !ip4_addr_netcmp(&dest, &(netif->ip_addr), &(netif->netmask)) ) + !ip4_addr_netcmp(&dest, netif_ip4_addr(netif), netif_ip4_netmask(netif)) ) { netif = netif->next; } @@ -3261,7 +3261,7 @@ ip_rteentry_get_value(struct obj_def *od, u16_t len, void *value) else { /* netifs have netaddress dest */ - ip4_addr_get_network(dst, &netif->ip_addr, &netif->netmask); + ip4_addr_get_network(dst, netif_ip4_addr(netif), netif_ip4_netmask(netif)); } } break; @@ -3305,12 +3305,12 @@ ip_rteentry_get_value(struct obj_def *od, u16_t len, void *value) if (ip4_addr_isany_val(dest)) { /* default rte: gateway */ - *dst = netif->gw; + *dst = *netif_ip4_gw(netif); } else { /* other rtes: netif ip_addr */ - *dst = netif->ip_addr; + *dst = *netif_ip4_addr(netif); } } break; @@ -3357,7 +3357,7 @@ ip_rteentry_get_value(struct obj_def *od, u16_t len, void *value) else { /* other rtes use netmask */ - *dst = netif->netmask; + *dst = *netif_ip4_netmask(netif); } } break; diff --git a/src/core/udp.c b/src/core/udp.c index d5d35088..79abc0d4 100644 --- a/src/core/udp.c +++ b/src/core/udp.c @@ -263,11 +263,11 @@ udp_input(struct pbuf *p, struct netif *inp) #if IP_SOF_BROADCAST_RECV (broadcast && ip_get_option(pcb, SOF_BROADCAST) && (ip_addr_isany(&pcb->local_ip) || - ip_addr_netcmp(&pcb->local_ip, ip_current_dest_addr(), &inp->netmask))))) + ip_addr_netcmp(&pcb->local_ip, ip_current_dest_addr(), netif_ip4_netmask(inp)))))) #else /* IP_SOF_BROADCAST_RECV */ (broadcast && (ip_addr_isany(&pcb->local_ip) || - ip_addr_netcmp(&pcb->local_ip, ip_current_dest_addr(), &inp->netmask))))) + ip_addr_netcmp(&pcb->local_ip, ip_current_dest_addr(), netif_ip4_netmask(inp)))))) #endif /* IP_SOF_BROADCAST_RECV */ #endif /* LWIP_IPV4 */ ) { @@ -318,7 +318,7 @@ udp_input(struct pbuf *p, struct netif *inp) #endif /* LWIP_IPV6 */ #if LWIP_IPV4 { - for_us = ip4_addr_cmp(&inp->ip_addr, ip4_current_dest_addr()); + for_us = ip4_addr_cmp(netif_ip4_addr(inp), ip4_current_dest_addr()); } #endif /* LWIP_IPV4 */ } @@ -664,11 +664,11 @@ udp_sendto_if_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_i #if LWIP_IPV4 if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip))) { /* use outgoing network interface IP address as source address */ - src_ip = ip4_2_ip(&netif->ip_addr, &src_ip_tmp); + src_ip = ip4_2_ip(netif_ip4_addr(netif), &src_ip_tmp); } else { /* check if UDP PCB local IP address is correct * this could be an old address if netif->ip_addr has changed */ - if (!ip4_addr_cmp(ip_2_ip4(&(pcb->local_ip)), &(netif->ip_addr))) { + if (!ip4_addr_cmp(ip_2_ip4(&(pcb->local_ip)), netif_ip4_addr(netif))) { /* local_ip doesn't match, drop the packet */ return ERR_VAL; } diff --git a/src/include/lwip/ip4.h b/src/include/lwip/ip4.h index 130a1cdd..2447e250 100644 --- a/src/include/lwip/ip4.h +++ b/src/include/lwip/ip4.h @@ -132,7 +132,7 @@ err_t ip4_output_if_opt_src(struct pbuf *p, const ip4_addr_t *src, const ip4_add void ip4_set_default_multicast_netif(struct netif* default_multicast_netif); #endif /* LWIP_MULTICAST_TX_OPTIONS */ -#define ip4_netif_get_local_ip(netif) (((netif) != NULL) ? &((netif)->ip_addr) : NULL) +#define ip4_netif_get_local_ip(netif) (((netif) != NULL) ? netif_ip4_addr(netif) : NULL) #if IP_DEBUG void ip4_debug_print(struct pbuf *p); diff --git a/src/include/lwip/ip4_addr.h b/src/include/lwip/ip4_addr.h index 858ce4cd..9483a2f7 100644 --- a/src/include/lwip/ip4_addr.h +++ b/src/include/lwip/ip4_addr.h @@ -221,10 +221,10 @@ u8_t ip4_addr_netmask_valid(u32_t netmask); ip4_addr4_16(&(ipaddr))) /* Get one byte from the 4-byte address */ -#define ip4_addr1(ipaddr) (((const u8_t*)(ipaddr))[0]) -#define ip4_addr2(ipaddr) (((const u8_t*)(ipaddr))[1]) -#define ip4_addr3(ipaddr) (((const u8_t*)(ipaddr))[2]) -#define ip4_addr4(ipaddr) (((const u8_t*)(ipaddr))[3]) +#define ip4_addr1(ipaddr) (((const u8_t*)(&(ipaddr)->addr))[0]) +#define ip4_addr2(ipaddr) (((const u8_t*)(&(ipaddr)->addr))[1]) +#define ip4_addr3(ipaddr) (((const u8_t*)(&(ipaddr)->addr))[2]) +#define ip4_addr4(ipaddr) (((const u8_t*)(&(ipaddr)->addr))[3]) /* These are cast to u16_t, with the intent that they are often arguments * to printf using the U16_F format from cc.h. */ #define ip4_addr1_16(ipaddr) ((u16_t)ip4_addr1(ipaddr)) diff --git a/src/include/lwip/ip_addr.h b/src/include/lwip/ip_addr.h index 0b682f20..95f646c1 100644 --- a/src/include/lwip/ip_addr.h +++ b/src/include/lwip/ip_addr.h @@ -50,7 +50,7 @@ typedef struct _ip_addr { union { ip6_addr_t ip6; ip4_addr_t ip4; - } addr; + } u_addr; u8_t type; } ip_addr_t; diff --git a/src/include/lwip/netif.h b/src/include/lwip/netif.h index 09ad65f0..e4678740 100644 --- a/src/include/lwip/netif.h +++ b/src/include/lwip/netif.h @@ -388,6 +388,12 @@ void netif_poll_all(void); #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */ #endif /* ENABLE_LOOPBACK */ +#if LWIP_IPV4 +#define netif_ip4_addr(netif) (&((netif)->ip_addr)) +#define netif_ip4_netmask(netif) (&((netif)->netmask)) +#define netif_ip4_gw(netif) (&((netif)->gw)) +#endif /* LWIP_IPV4 */ + #if LWIP_IPV6 #define netif_ip6_addr(netif, i) (&((netif)->ip6_addr[(i)])) #define netif_ip6_addr_state(netif, i) ((netif)->ip6_addr_state[(i)]) diff --git a/src/include/netif/etharp.h b/src/include/netif/etharp.h index 3f895455..0b965b32 100644 --- a/src/include/netif/etharp.h +++ b/src/include/netif/etharp.h @@ -205,7 +205,7 @@ err_t etharp_request(struct netif *netif, const ip4_addr_t *ipaddr); * this is an ARP packet sent by a node in order to spontaneously cause other * nodes to update an entry in their ARP cache. * From RFC 3220 "IP Mobility Support for IPv4" section 4.6. */ -#define etharp_gratuitous(netif) etharp_request((netif), &(netif)->ip_addr) +#define etharp_gratuitous(netif) etharp_request((netif), netif_ip4_addr(netif)) void etharp_cleanup_netif(struct netif *netif); #if ETHARP_SUPPORT_STATIC_ENTRIES diff --git a/src/netif/etharp.c b/src/netif/etharp.c index 0dc71201..05d8181a 100644 --- a/src/netif/etharp.c +++ b/src/netif/etharp.c @@ -777,11 +777,11 @@ etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p) IPADDR2_COPY(&dipaddr, &hdr->dipaddr); /* this interface is not configured? */ - if (ip4_addr_isany_val(netif->ip_addr)) { + if (ip4_addr_isany_val(*netif_ip4_addr(netif))) { for_us = 0; } else { /* ARP packet directed to us? */ - for_us = (u8_t)ip4_addr_cmp(&dipaddr, &(netif->ip_addr)); + for_us = (u8_t)ip4_addr_cmp(&dipaddr, netif_ip4_addr(netif)); } /* ARP message directed to us? @@ -811,7 +811,7 @@ etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p) hdr->opcode = htons(ARP_REPLY); IPADDR2_COPY(&hdr->dipaddr, &hdr->sipaddr); - IPADDR2_COPY(&hdr->sipaddr, &netif->ip_addr); + IPADDR2_COPY(&hdr->sipaddr, netif_ip4_addr(netif)); LWIP_ASSERT("netif->hwaddr_len must be the same as ETHARP_HWADDR_LEN for etharp!", (netif->hwaddr_len == ETHARP_HWADDR_LEN)); @@ -819,7 +819,7 @@ etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p) /* If we are using Link-Local, all ARP packets that contain a Link-Local * 'sender IP address' MUST be sent using link-layer broadcast instead of * link-layer unicast. (See RFC3927 Section 2.5, last paragraph) */ - ethdst_hwaddr = ip4_addr_islinklocal(&netif->ip_addr) ? (const u8_t*)(ethbroadcast.addr) : hdr->shwaddr.addr; + ethdst_hwaddr = ip4_addr_islinklocal(netif_ip4_addr(netif)) ? (const u8_t*)(ethbroadcast.addr) : hdr->shwaddr.addr; #endif /* LWIP_AUTOIP */ ETHADDR16_COPY(&hdr->dhwaddr, &hdr->shwaddr); @@ -837,7 +837,7 @@ etharp_arp_input(struct netif *netif, struct eth_addr *ethaddr, struct pbuf *p) /* return ARP reply */ netif->linkoutput(netif, p); /* we are not configured? */ - } else if (ip4_addr_isany_val(netif->ip_addr)) { + } else if (ip4_addr_isany_val(*netif_ip4_addr(netif))) { /* { for_us == 0 and netif->ip_addr.addr == 0 } */ LWIP_DEBUGF(ETHARP_DEBUG | LWIP_DBG_TRACE, ("etharp_arp_input: we are unconfigured, ARP request ignored.\n")); /* request was not directed to us */ @@ -960,7 +960,7 @@ etharp_output(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr) s8_t i; /* outside local network? if so, this can neither be a global broadcast nor a subnet broadcast. */ - if (!ip4_addr_netcmp(ipaddr, &(netif->ip_addr), &(netif->netmask)) && + if (!ip4_addr_netcmp(ipaddr, netif_ip4_addr(netif), netif_ip4_netmask(netif)) && !ip4_addr_islinklocal(ipaddr)) { #if LWIP_AUTOIP struct ip_hdr *iphdr = (struct ip_hdr*)((u8_t*)q->payload + @@ -983,9 +983,9 @@ etharp_output(struct netif *netif, struct pbuf *q, const ip4_addr_t *ipaddr) #endif /* LWIP_HOOK_ETHARP_GET_GW */ { /* interface has default gateway? */ - if (!ip4_addr_isany_val(netif->gw)) { + if (!ip4_addr_isany_val(*netif_ip4_gw(netif))) { /* send to hardware address of default gateway IP address */ - dst_addr = &(netif->gw); + dst_addr = netif_ip4_gw(netif); /* no default gateway available */ } else { /* no route to destination error (default gateway missing) */ @@ -1353,7 +1353,7 @@ static err_t etharp_request_dst(struct netif *netif, const ip4_addr_t *ipaddr, const struct eth_addr* hw_dst_addr) { return etharp_raw(netif, (struct eth_addr *)netif->hwaddr, hw_dst_addr, - (struct eth_addr *)netif->hwaddr, &netif->ip_addr, ðzero, + (struct eth_addr *)netif->hwaddr, netif_ip4_addr(netif), ðzero, ipaddr, ARP_REQUEST); }