From 309e07223883615a9ad9d70810bc5dd55e610698 Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Wed, 6 Apr 2016 22:31:31 +0200 Subject: [PATCH] Create new IP_IS_V4 macros and use them at instead of !IP_IS_V6 - since we now have an IPADDR_ANY_TYPE, just checking for !V6 does not mean it is V4 --- src/api/netdb.c | 2 +- src/apps/snmp/snmp_mib2_tcp.c | 6 +++--- src/apps/snmp/snmp_mib2_udp.c | 4 ++-- src/core/dns.c | 2 +- src/core/ipv4/dhcp.c | 2 +- src/core/raw.c | 2 +- src/core/tcp.c | 4 ++-- src/core/udp.c | 8 ++++---- src/include/lwip/apps/snmp_core.h | 1 + src/include/lwip/ip_addr.h | 8 +++++++- 10 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/api/netdb.c b/src/api/netdb.c index 65510f55..c6edf837 100644 --- a/src/api/netdb.c +++ b/src/api/netdb.c @@ -318,7 +318,7 @@ lwip_getaddrinfo(const char *nodename, const char *servname, } #if LWIP_IPV4 && LWIP_IPV6 if ((IP_IS_V6_VAL(addr) && ai_family == AF_INET) || - (!IP_IS_V6_VAL(addr) && ai_family == AF_INET6)) { + (IP_IS_V4_VAL(addr) && ai_family == AF_INET6)) { return EAI_NONAME; } #endif /* LWIP_IPV4 && LWIP_IPV6 */ diff --git a/src/apps/snmp/snmp_mib2_tcp.c b/src/apps/snmp/snmp_mib2_tcp.c index ee0d7702..3e365b71 100644 --- a/src/apps/snmp/snmp_mib2_tcp.c +++ b/src/apps/snmp/snmp_mib2_tcp.c @@ -219,7 +219,7 @@ tcp_ConnTable_get_cell_value(const u32_t* column, const u32_t* row_oid, u8_t row while (pcb != NULL) { /* do local IP and local port match? */ - if(!IP_IS_V6_VAL(pcb->local_ip) && + if(IP_IS_V4_VAL(pcb->local_ip) && ip4_addr_cmp(&local_ip, ip_2_ip4(&pcb->local_ip)) && (local_port == pcb->local_port)) { /* PCBs in state LISTEN are not connected and have no remote_ip or remote_port */ @@ -229,7 +229,7 @@ tcp_ConnTable_get_cell_value(const u32_t* column, const u32_t* row_oid, u8_t row return tcp_ConnTable_get_cell_value_core(pcb, column, value, value_len); } } else { - if(!IP_IS_V6_VAL(pcb->remote_ip) && + if(IP_IS_V4_VAL(pcb->remote_ip) && ip4_addr_cmp(&remote_ip, ip_2_ip4(&pcb->remote_ip)) && (remote_port == pcb->remote_port)) { /* fill in object properties */ return tcp_ConnTable_get_cell_value_core(pcb, column, value, value_len); @@ -262,7 +262,7 @@ tcp_ConnTable_get_next_cell_instance_and_value(const u32_t* column, struct snmp_ while (pcb != NULL) { u32_t test_oid[LWIP_ARRAYSIZE(tcp_ConnTable_oid_ranges)]; - if(!IP_IS_V6_VAL(pcb->local_ip)) { + if(IP_IS_V4_VAL(pcb->local_ip)) { snmp_ip4_to_oid(ip_2_ip4(&pcb->local_ip), &test_oid[0]); test_oid[4] = pcb->local_port; diff --git a/src/apps/snmp/snmp_mib2_udp.c b/src/apps/snmp/snmp_mib2_udp.c index 5214e38b..67d185ea 100644 --- a/src/apps/snmp/snmp_mib2_udp.c +++ b/src/apps/snmp/snmp_mib2_udp.c @@ -254,7 +254,7 @@ udp_Table_get_cell_value(const u32_t* column, const u32_t* row_oid, u8_t row_oid /* find udp_pcb with requested ip and port*/ pcb = udp_pcbs; while (pcb != NULL) { - if(!IP_IS_V6_VAL(pcb->local_ip)) { + if(IP_IS_V4_VAL(pcb->local_ip)) { if(ip4_addr_cmp(&ip, ip_2_ip4(&pcb->local_ip)) && (port == pcb->local_port)) { /* fill in object properties */ return udp_Table_get_cell_value_core(pcb, column, value, value_len); @@ -282,7 +282,7 @@ udp_Table_get_next_cell_instance_and_value(const u32_t* column, struct snmp_obj_ while (pcb != NULL) { u32_t test_oid[LWIP_ARRAYSIZE(udp_Table_oid_ranges)]; - if(!IP_IS_V6_VAL(pcb->local_ip)) { + if(IP_IS_V4_VAL(pcb->local_ip)) { snmp_ip4_to_oid(ip_2_ip4(&pcb->local_ip), &test_oid[0]); test_oid[4] = pcb->local_port; diff --git a/src/core/dns.c b/src/core/dns.c index 6b89b392..24ec717f 100644 --- a/src/core/dns.c +++ b/src/core/dns.c @@ -1438,7 +1438,7 @@ dns_gethostbyname_addrtype(const char *hostname, ip_addr_t *addr, dns_found_call if (ipaddr_aton(hostname, addr)) { #if LWIP_IPV4 && LWIP_IPV6 if ((IP_IS_V6(addr) && (dns_addrtype != LWIP_DNS_ADDRTYPE_IPV4)) || - (!IP_IS_V6(addr) && (dns_addrtype != LWIP_DNS_ADDRTYPE_IPV6))) + (IP_IS_V4(addr) && (dns_addrtype != LWIP_DNS_ADDRTYPE_IPV6))) #endif /* LWIP_IPV4 && LWIP_IPV6 */ { return ERR_OK; diff --git a/src/core/ipv4/dhcp.c b/src/core/ipv4/dhcp.c index 06a24b71..e0b78178 100644 --- a/src/core/ipv4/dhcp.c +++ b/src/core/ipv4/dhcp.c @@ -1649,7 +1649,7 @@ dhcp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, goto free_pbuf_and_return; } - LWIP_ASSERT("invalid server address type", !IP_IS_V6(addr)); + LWIP_ASSERT("invalid server address type", IP_IS_V4(addr)); 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(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)); diff --git a/src/core/raw.c b/src/core/raw.c index 82ce4e3a..c777f23e 100644 --- a/src/core/raw.c +++ b/src/core/raw.c @@ -323,7 +323,7 @@ raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr) } #if IP_SOF_BROADCAST - if (!IP_IS_V6(ipaddr)) + if (IP_IS_V4(ipaddr)) { /* broadcast filter? */ if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(ipaddr, netif)) { diff --git a/src/core/tcp.c b/src/core/tcp.c index 2285ecc6..ac4c11a5 100644 --- a/src/core/tcp.c +++ b/src/core/tcp.c @@ -1872,7 +1872,7 @@ tcp_netif_ipv4_addr_changed_pcblist(const ip4_addr_t* old_addr, struct tcp_pcb* pcb = pcb_list; while (pcb != NULL) { /* PCB bound to current local interface address? */ - if (!IP_IS_V6_VAL(pcb->local_ip) && ip4_addr_cmp(ip_2_ip4(&pcb->local_ip), old_addr) + if (IP_IS_V4_VAL(pcb->local_ip) && ip4_addr_cmp(ip_2_ip4(&pcb->local_ip), old_addr) #if LWIP_AUTOIP /* connections to link-local addresses must persist (RFC3927 ch. 1.9) */ && !ip4_addr_islinklocal(ip_2_ip4(&pcb->local_ip)) @@ -1906,7 +1906,7 @@ void tcp_netif_ipv4_addr_changed(const ip4_addr_t* old_addr, const ip4_addr_t* n for (lpcb = tcp_listen_pcbs.listen_pcbs; lpcb != NULL; lpcb = next) { next = lpcb->next; /* Is this an IPv4 pcb? */ - if (!IP_IS_V6_VAL(lpcb->local_ip)) { + if (IP_IS_V4_VAL(lpcb->local_ip)) { /* PCB bound to current local interface address? */ if ((!(ip4_addr_isany(ip_2_ip4(&lpcb->local_ip)))) && (ip4_addr_cmp(ip_2_ip4(&lpcb->local_ip), old_addr))) { diff --git a/src/core/udp.c b/src/core/udp.c index ad368959..7550c64b 100644 --- a/src/core/udp.c +++ b/src/core/udp.c @@ -701,7 +701,7 @@ udp_sendto_if_src_chksum(struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *d /* broadcast filter? */ if (!ip_get_option(pcb, SOF_BROADCAST) && #if LWIP_IPV6 - !IP_IS_V6(dst_ip) && + IP_IS_V4(dst_ip) && #endif /* LWIP_IPV6 */ ip_addr_isbroadcast(dst_ip, netif)) { LWIP_DEBUGF(UDP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, @@ -943,9 +943,9 @@ udp_bind(struct udp_pcb *pcb, const ip_addr_t *ipaddr, u16_t port) #endif /* SO_REUSE */ { /* port matches that of PCB in list and REUSEADDR not set -> reject */ - if ((ipcb->local_port == port) && (IP_IS_V6(ipaddr) == IP_IS_V6_VAL(ipcb->local_ip)) && + if ((ipcb->local_port == port) && /* IP address matches? */ - ip_addr_cmp(&ipcb->local_ip, ipaddr)) { + ip_addr_cmp(&ipcb->local_ip, ipaddr)) { /* other PCB already binds to this local IP and port */ LWIP_DEBUGF(UDP_DEBUG, ("udp_bind: local port %"U16_F" already bound by another pcb\n", port)); @@ -1165,7 +1165,7 @@ void udp_netif_ipv4_addr_changed(const ip4_addr_t* old_addr, const ip4_addr_t* n if (!ip4_addr_isany(new_addr)) { for (upcb = udp_pcbs; upcb != NULL; upcb = upcb->next) { /* Is this an IPv4 pcb? */ - if (!IP_IS_V6_VAL(upcb->local_ip)) { + if (IP_IS_V4_VAL(upcb->local_ip)) { /* PCB bound to current local interface address? */ if (!ip4_addr_isany(ip_2_ip4(&upcb->local_ip)) && ip4_addr_cmp(ip_2_ip4(&upcb->local_ip), old_addr)) { diff --git a/src/include/lwip/apps/snmp_core.h b/src/include/lwip/apps/snmp_core.h index 70ad7126..807a7a41 100644 --- a/src/include/lwip/apps/snmp_core.h +++ b/src/include/lwip/apps/snmp_core.h @@ -315,6 +315,7 @@ u8_t snmp_oid_to_ip(const u32_t *oid, u8_t oid_len, ip_addr_t *ip); u8_t snmp_oid_to_ip_port(const u32_t *oid, u8_t oid_len, ip_addr_t *ip, u16_t *port); #endif /* LWIP_IPV4 || LWIP_IPV6 */ +struct netif; u8_t netif_to_num(const struct netif *netif); snmp_err_t snmp_set_test_ok(struct snmp_node_instance* instance, u16_t value_len, void* value); /* generic function which can be used if test is always successful */ diff --git a/src/include/lwip/ip_addr.h b/src/include/lwip/ip_addr.h index 30ddbc62..e7e09fd9 100644 --- a/src/include/lwip/ip_addr.h +++ b/src/include/lwip/ip_addr.h @@ -67,7 +67,9 @@ extern const ip_addr_t ip_addr_any_type; #define IP_IS_ANY_TYPE_VAL(ipaddr) (IP_GET_TYPE(&ipaddr) == IPADDR_TYPE_ANY) #define IPADDR_ANY_TYPE_INIT { { { { 0ul, 0ul, 0ul, 0ul } } }, IPADDR_TYPE_ANY } +#define IP_IS_V4_VAL(ipaddr) (IP_GET_TYPE(&ipaddr) == IPADDR_TYPE_V4) #define IP_IS_V6_VAL(ipaddr) (IP_GET_TYPE(&ipaddr) == IPADDR_TYPE_V6) +#define IP_IS_V4(ipaddr) (((ipaddr) == NULL) || IP_IS_V4_VAL(*(ipaddr))) #define IP_IS_V6(ipaddr) (((ipaddr) != NULL) && IP_IS_V6_VAL(*(ipaddr))) #define IP_SET_TYPE_VAL(ipaddr, iptype) do { (ipaddr).type = (iptype); }while(0) #define IP_SET_TYPE(ipaddr, iptype) do { if((ipaddr) != NULL) { IP_SET_TYPE_VAL(*(ipaddr), iptype); }}while(0) @@ -94,7 +96,7 @@ extern const ip_addr_t ip_addr_any_type; ip4_addr_copy(*ip_2_ip4(&(dest)), src); IP_SET_TYPE_VAL(dest, IPADDR_TYPE_V4); }while(0) #define ip_addr_set_ip4_u32(ipaddr, val) do{if(ipaddr){ip4_addr_set_u32(ip_2_ip4(ipaddr), val); \ IP_SET_TYPE(ipaddr, IPADDR_TYPE_V4); }}while(0) -#define ip_addr_get_ip4_u32(ipaddr) (((ipaddr) && !IP_IS_V6(ipaddr)) ? \ +#define ip_addr_get_ip4_u32(ipaddr) (((ipaddr) && IP_IS_V4(ipaddr)) ? \ ip4_addr_get_u32(ip_2_ip4(ipaddr)) : 0) #define ip_addr_set(dest, src) do{ IP_SET_TYPE(dest, IP_GET_TYPE(src)); if(IP_IS_V6(src)){ \ ip6_addr_set(ip_2_ip6(dest), ip_2_ip6(src)); }else{ \ @@ -165,7 +167,9 @@ int ipaddr_aton(const char *cp, ip_addr_t *addr); typedef ip4_addr_t ip_addr_t; #define IPADDR4_INIT(u32val) { u32val } +#define IP_IS_V4_VAL(ipaddr) 1 #define IP_IS_V6_VAL(ipaddr) 0 +#define IP_IS_V4(ipaddr) 1 #define IP_IS_V6(ipaddr) 0 #define IP_IS_ANY_TYPE_VAL(ipaddr) 0 #define IP_SET_TYPE_VAL(ipaddr, iptype) @@ -206,7 +210,9 @@ typedef ip4_addr_t ip_addr_t; typedef ip6_addr_t ip_addr_t; #define IPADDR6_INIT(a, b, c, d) { { a, b, c, d } } +#define IP_IS_V4_VAL(ipaddr) 0 #define IP_IS_V6_VAL(ipaddr) 1 +#define IP_IS_V4(ipaddr) 0 #define IP_IS_V6(ipaddr) 1 #define IP_IS_ANY_TYPE_VAL(ipaddr) 0 #define IP_SET_TYPE_VAL(ipaddr, iptype)