From 9366c0eaabde4ef2744ab93d40e96def453925dc Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Thu, 17 Nov 2016 12:41:00 +0100 Subject: [PATCH] I decided to keep the "complexity" of handling IPv6 mapped IPv4 addresses out of netconn API. Only socket API understands this address type now. --- src/api/api_lib.c | 70 +++++++++++++---------------------------------- src/api/sockets.c | 35 +++++++++++++++++++++++- 2 files changed, 53 insertions(+), 52 deletions(-) diff --git a/src/api/api_lib.c b/src/api/api_lib.c index ae366ad4..31be589c 100644 --- a/src/api/api_lib.c +++ b/src/api/api_lib.c @@ -259,33 +259,22 @@ netconn_bind(struct netconn *conn, const ip_addr_t *addr, u16_t port) addr = IP4_ADDR_ANY; } - { #if LWIP_IPV4 && LWIP_IPV6 - ip_addr_t ipaddr; - - /* "Socket API like" dual-stack support: If IP to bind to is IP6_ADDR_ANY, - * and NETCONN_FLAG_IPV6_V6ONLY is 0, use IP_ANY_TYPE to bind - */ - if ((netconn_get_ipv6only(conn) == 0) && - ip_addr_cmp(addr, IP6_ADDR_ANY)) { - addr = IP_ANY_TYPE; - } - - /* Dual-stack: Unmap IPv6 mapped IPv4 addresses */ - if (IP_IS_V6(addr) && ip6_addr_isipv6mappedipv4(ip_2_ip6(addr))) { - unmap_ipv6_mapped_ipv4(ip_2_ip4(&ipaddr), ip_2_ip6(addr)); - IP_SET_TYPE_VAL(ipaddr, IPADDR_TYPE_V4); - addr = &ipaddr; - } + /* "Socket API like" dual-stack support: If IP to bind to is IP6_ADDR_ANY, + * and NETCONN_FLAG_IPV6_V6ONLY is 0, use IP_ANY_TYPE to bind + */ + if ((netconn_get_ipv6only(conn) == 0) && + ip_addr_cmp(addr, IP6_ADDR_ANY)) { + addr = IP_ANY_TYPE; + } #endif /* LWIP_IPV4 && LWIP_IPV6 */ - API_MSG_VAR_ALLOC(msg); - API_MSG_VAR_REF(msg).conn = conn; - API_MSG_VAR_REF(msg).msg.bc.ipaddr = API_MSG_VAR_REF(addr); - API_MSG_VAR_REF(msg).msg.bc.port = port; - err = netconn_apimsg(lwip_netconn_do_bind, &API_MSG_VAR_REF(msg)); - API_MSG_VAR_FREE(msg); - } + API_MSG_VAR_ALLOC(msg); + API_MSG_VAR_REF(msg).conn = conn; + API_MSG_VAR_REF(msg).msg.bc.ipaddr = API_MSG_VAR_REF(addr); + API_MSG_VAR_REF(msg).msg.bc.port = port; + err = netconn_apimsg(lwip_netconn_do_bind, &API_MSG_VAR_REF(msg)); + API_MSG_VAR_FREE(msg); return err; } @@ -312,25 +301,12 @@ netconn_connect(struct netconn *conn, const ip_addr_t *addr, u16_t port) addr = IP4_ADDR_ANY; } - { -#if LWIP_IPV4 && LWIP_IPV6 - ip_addr_t ipaddr; - - /* Dual-stack: Unmap IPv6 mapped IPv4 addresses */ - if (IP_IS_V6(addr) && ip6_addr_isipv6mappedipv4(ip_2_ip6(addr))) { - unmap_ipv6_mapped_ipv4(ip_2_ip4(&ipaddr), ip_2_ip6(addr)); - IP_SET_TYPE_VAL(ipaddr, IPADDR_TYPE_V4); - addr = &ipaddr; - } -#endif /* LWIP_IPV4 && LWIP_IPV6 */ - - API_MSG_VAR_ALLOC(msg); - API_MSG_VAR_REF(msg).conn = conn; - API_MSG_VAR_REF(msg).msg.bc.ipaddr = API_MSG_VAR_REF(addr); - API_MSG_VAR_REF(msg).msg.bc.port = port; - err = netconn_apimsg(lwip_netconn_do_connect, &API_MSG_VAR_REF(msg)); - API_MSG_VAR_FREE(msg); - } + API_MSG_VAR_ALLOC(msg); + API_MSG_VAR_REF(msg).conn = conn; + API_MSG_VAR_REF(msg).msg.bc.ipaddr = API_MSG_VAR_REF(addr); + API_MSG_VAR_REF(msg).msg.bc.port = port; + err = netconn_apimsg(lwip_netconn_do_connect, &API_MSG_VAR_REF(msg)); + API_MSG_VAR_FREE(msg); return err; } @@ -731,14 +707,6 @@ netconn_send(struct netconn *conn, struct netbuf *buf) LWIP_ERROR("netconn_send: invalid conn", (conn != NULL), return ERR_ARG;); LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %"U16_F" bytes\n", buf->p->tot_len)); - -#if LWIP_IPV4 && LWIP_IPV6 - /* Dual-stack: Unmap IPv6 mapped IPv4 addresses */ - if (IP_IS_V6_VAL(buf->addr) && ip6_addr_isipv6mappedipv4(ip_2_ip6(&buf->addr))) { - unmap_ipv6_mapped_ipv4(ip_2_ip4(&buf->addr), ip_2_ip6(&buf->addr)); - IP_SET_TYPE_VAL(buf->addr, IPADDR_TYPE_V4); - } -#endif /* LWIP_IPV4 && LWIP_IPV6 */ API_MSG_VAR_ALLOC(msg); API_MSG_VAR_REF(msg).conn = conn; diff --git a/src/api/sockets.c b/src/api/sockets.c index 22f3acd6..2db0949d 100644 --- a/src/api/sockets.c +++ b/src/api/sockets.c @@ -584,6 +584,14 @@ lwip_bind(int s, const struct sockaddr *name, socklen_t namelen) ip_addr_debug_print_val(SOCKETS_DEBUG, local_addr); LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", local_port)); +#if LWIP_IPV4 && LWIP_IPV6 + /* Dual-stack: Unmap IPv6 mapped IPv4 addresses */ + if (IP_IS_V6_VAL(local_addr) && ip6_addr_isipv6mappedipv4(ip_2_ip6(&local_addr))) { + unmap_ipv6_mapped_ipv4(ip_2_ip4(&local_addr), ip_2_ip6(&local_addr)); + IP_SET_TYPE_VAL(local_addr, IPADDR_TYPE_V4); + } +#endif /* LWIP_IPV4 && LWIP_IPV6 */ + err = netconn_bind(sock->conn, &local_addr, local_port); if (err != ERR_OK) { @@ -668,6 +676,14 @@ lwip_connect(int s, const struct sockaddr *name, socklen_t namelen) ip_addr_debug_print_val(SOCKETS_DEBUG, remote_addr); LWIP_DEBUGF(SOCKETS_DEBUG, (" port=%"U16_F")\n", remote_port)); +#if LWIP_IPV4 && LWIP_IPV6 + /* Dual-stack: Unmap IPv6 mapped IPv4 addresses */ + if (IP_IS_V6_VAL(remote_addr) && ip6_addr_isipv6mappedipv4(ip_2_ip6(&remote_addr))) { + unmap_ipv6_mapped_ipv4(ip_2_ip4(&remote_addr), ip_2_ip6(&remote_addr)); + IP_SET_TYPE_VAL(remote_addr, IPADDR_TYPE_V4); + } +#endif /* LWIP_IPV4 && LWIP_IPV6 */ + err = netconn_connect(sock->conn, &remote_addr, remote_port); } @@ -1075,6 +1091,14 @@ lwip_sendmsg(int s, const struct msghdr *msg, int flags) #endif /* LWIP_NETIF_TX_SINGLE_PBUF */ if (err == ERR_OK) { +#if LWIP_IPV4 && LWIP_IPV6 + /* Dual-stack: Unmap IPv6 mapped IPv4 addresses */ + if (IP_IS_V6_VAL(chain_buf->addr) && ip6_addr_isipv6mappedipv4(ip_2_ip6(&chain_buf->addr))) { + unmap_ipv6_mapped_ipv4(ip_2_ip4(&chain_buf->addr), ip_2_ip6(&chain_buf->addr)); + IP_SET_TYPE_VAL(chain_buf->addr, IPADDR_TYPE_V4); + } +#endif /* LWIP_IPV4 && LWIP_IPV6 */ + /* send the data */ err = netconn_send(sock->conn, chain_buf); } @@ -1165,6 +1189,14 @@ lwip_sendto(int s, const void *data, size_t size, int flags, err = netbuf_ref(&buf, data, short_size); #endif /* LWIP_NETIF_TX_SINGLE_PBUF */ if (err == ERR_OK) { +#if LWIP_IPV4 && LWIP_IPV6 + /* Dual-stack: Unmap IPv6 mapped IPv4 addresses */ + if (IP_IS_V6_VAL(buf.addr) && ip6_addr_isipv6mappedipv4(ip_2_ip6(&buf.addr))) { + unmap_ipv6_mapped_ipv4(ip_2_ip4(&buf.addr), ip_2_ip6(&buf.addr)); + IP_SET_TYPE_VAL(buf.addr, IPADDR_TYPE_V4); + } +#endif /* LWIP_IPV4 && LWIP_IPV6 */ + /* send the data */ err = netconn_send(sock->conn, &buf); } @@ -1722,7 +1754,8 @@ lwip_getaddrname(int s, struct sockaddr *name, socklen_t *namelen, u8_t local) #if LWIP_IPV4 && LWIP_IPV6 /* Dual-stack: Map IPv4 addresses to IPv6 mapped IPv4 */ - if (NETCONNTYPE_ISIPV6(netconn_type(sock->conn)) && IP_IS_V4_VAL(naddr)) { + if (NETCONNTYPE_ISIPV6(netconn_type(sock->conn)) && + IP_IS_V4_VAL(naddr)) { ip4_2_ipv6_mapped_ipv4(ip_2_ip6(&naddr), ip_2_ip4(&naddr)); IP_SET_TYPE_VAL(naddr, IPADDR_TYPE_V6); }