diff --git a/include/nuttx/net/ip.h b/include/nuttx/net/ip.h index 24c5b48a65d..ec97434a210 100644 --- a/include/nuttx/net/ip.h +++ b/include/nuttx/net/ip.h @@ -420,6 +420,30 @@ extern "C" (ipv6addr)->s6_addr16[4] == 0 && \ (ipv6addr)->s6_addr16[5] == 0xffff) +/**************************************************************************** + * Macro: net_ip_domain_select + * + * Description: + * Select different value by the domain (PF_INET/PF_INET6). + * This domain only needs to exist when both IPv4 and IPv6 are enabled. + * + * Input Parameters: + * value4 - The value returned when domain is PF_INET. + * value6 - The value returned when domain is PF_INET6. + * domain - The domain field which may only exists when both IPv4 and IPv6 + * are enabled. + * + ****************************************************************************/ + +#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) +# define net_ip_domain_select(domain, value4, value6) \ + (((domain) == PF_INET) ? (value4) : (value6)) +#elif defined(CONFIG_NET_IPv4) +# define net_ip_domain_select(domain, value4, value6) (value4) +#else +# define net_ip_domain_select(domain, value4, value6) (value6) +#endif + /**************************************************************************** * Macro: net_ip_binding_laddr, net_ip_binding_raddr * @@ -428,24 +452,17 @@ extern "C" * * Input Parameters: * u - The union of address binding. - * domain - The domain of address. + * domain - The domain of address, only needs to exist when both IPv4 and + * IPv6 are enabled. * ****************************************************************************/ -#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) -# define net_ip_binding_laddr(u, domain) \ - (((domain) == PF_INET) ? (FAR void *)(&(u)->ipv4.laddr) : \ - (FAR void *)(&(u)->ipv6.laddr)) -# define net_ip_binding_raddr(u, domain) \ - (((domain) == PF_INET) ? (FAR void *)(&(u)->ipv4.raddr) : \ - (FAR void *)(&(u)->ipv6.raddr)) -#elif defined(CONFIG_NET_IPv4) -# define net_ip_binding_laddr(u, domain) ((FAR void *)(&(u)->ipv4.laddr)) -# define net_ip_binding_raddr(u, domain) ((FAR void *)(&(u)->ipv4.raddr)) -#else -# define net_ip_binding_laddr(u, domain) ((FAR void *)(&(u)->ipv6.laddr)) -# define net_ip_binding_raddr(u, domain) ((FAR void *)(&(u)->ipv6.raddr)) -#endif +#define net_ip_binding_laddr(u, domain) \ + net_ip_domain_select(domain, (FAR void *)(&(u)->ipv4.laddr), \ + (FAR void *)(&(u)->ipv6.laddr)) +#define net_ip_binding_raddr(u, domain) \ + net_ip_domain_select(domain, (FAR void *)(&(u)->ipv4.raddr), \ + (FAR void *)(&(u)->ipv6.raddr)) /**************************************************************************** * Macro: net_ipv4addr_copy, net_ipv4addr_hdrcopy, net_ipv6addr_copy, and diff --git a/net/socket/net_fstat.c b/net/socket/net_fstat.c index a35b5d95940..a998ad99146 100644 --- a/net/socket/net_fstat.c +++ b/net/socket/net_fstat.c @@ -150,14 +150,9 @@ int psock_fstat(FAR struct socket *psock, FAR struct stat *buf) { /* We need the length of the IP header */ -#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) - iplen = (udp_conn->domain == PF_INET) ? IPv4_HDRLEN : - IPv6_HDRLEN; -#elif defined(CONFIG_NET_IPv4) - iplen = IPv4_HDRLEN; -#else - iplen = IPv6_HDRLEN; -#endif + iplen = net_ip_domain_select(udp_conn->domain, + IPv4_HDRLEN, IPv6_HDRLEN); + /* Now we can calculate the MSS */ buf->st_blksize = UDP_MSS(dev, iplen); diff --git a/net/tcp/tcp_ioctl.c b/net/tcp/tcp_ioctl.c index 83754efa34e..f96254fc356 100644 --- a/net/tcp/tcp_ioctl.c +++ b/net/tcp/tcp_ioctl.c @@ -57,13 +57,7 @@ static void tcp_path(FAR struct tcp_conn_s *conn, FAR char *buf, size_t len) { -#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) - uint8_t domain = conn->domain; -#elif defined(CONFIG_NET_IPv4) - const uint8_t domain = PF_INET; -#else - const uint8_t domain = PF_INET6; -#endif + uint8_t domain = net_ip_domain_select(conn->domain, PF_INET, PF_INET6); char remote[INET6_ADDRSTRLEN]; char local[INET6_ADDRSTRLEN]; FAR void *laddr = net_ip_binding_laddr(&conn->u, domain); diff --git a/net/tcp/tcp_send.c b/net/tcp/tcp_send.c index 68cd1830fc2..7651839a46e 100644 --- a/net/tcp/tcp_send.c +++ b/net/tcp/tcp_send.c @@ -703,26 +703,10 @@ uint16_t tcpip_hdrsize(FAR struct tcp_conn_s *conn) { uint16_t hdrsize = sizeof(struct tcp_hdr_s); -#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) - if (conn->domain == PF_INET) - { - /* Select the IPv4 domain */ - - return sizeof(struct ipv4_hdr_s) + hdrsize; - } - else /* if (domain == PF_INET6) */ - { - /* Select the IPv6 domain */ - - return sizeof(struct ipv6_hdr_s) + hdrsize; - } -#elif defined(CONFIG_NET_IPv4) - ((void)conn); - return sizeof(struct ipv4_hdr_s) + hdrsize; -#elif defined(CONFIG_NET_IPv6) - ((void)conn); - return sizeof(struct ipv6_hdr_s) + hdrsize; -#endif + UNUSED(conn); + return net_ip_domain_select(conn->domain, + sizeof(struct ipv4_hdr_s) + hdrsize, + sizeof(struct ipv6_hdr_s) + hdrsize); } #endif /* CONFIG_NET && CONFIG_NET_TCP */ diff --git a/net/udp/udp_ioctl.c b/net/udp/udp_ioctl.c index 79ce573c200..41392fa9a34 100644 --- a/net/udp/udp_ioctl.c +++ b/net/udp/udp_ioctl.c @@ -57,13 +57,7 @@ static void udp_path(FAR struct udp_conn_s *conn, FAR char *buf, size_t len) { -#if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) - uint8_t domain = conn->domain; -#elif defined(CONFIG_NET_IPv4) - const uint8_t domain = PF_INET; -#else - const uint8_t domain = PF_INET6; -#endif + uint8_t domain = net_ip_domain_select(conn->domain, PF_INET, PF_INET6); char remote[INET6_ADDRSTRLEN]; char local[INET6_ADDRSTRLEN]; FAR void *laddr = net_ip_binding_laddr(&conn->u, domain); diff --git a/net/udp/udp_send.c b/net/udp/udp_send.c index 39e0ffe6651..c3d5228f2ec 100644 --- a/net/udp/udp_send.c +++ b/net/udp/udp_send.c @@ -232,28 +232,18 @@ uint16_t udpip_hdrsize(FAR struct udp_conn_s *conn) uint16_t hdrsize = sizeof(struct udp_hdr_s); #if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) - /* Which domain the socket used */ - - if (conn->domain == PF_INET || - (conn->domain == PF_INET6 && - ip6_is_ipv4addr((FAR struct in6_addr *)conn->u.ipv6.raddr))) + if (conn->domain == PF_INET6 && + ip6_is_ipv4addr((FAR struct in6_addr *)conn->u.ipv6.raddr)) { - /* Select the IPv4 domain */ + /* Select the IPv4 domain for hybrid dual-stack IPv6/IPv4 socket */ return sizeof(struct ipv4_hdr_s) + hdrsize; } - else /* if (domain == PF_INET6) */ - { - /* Select the IPv6 domain */ - - return sizeof(struct ipv6_hdr_s) + hdrsize; - } -#elif defined(CONFIG_NET_IPv4) - ((void)conn); - return sizeof(struct ipv4_hdr_s) + hdrsize; -#elif defined(CONFIG_NET_IPv6) - ((void)conn); - return sizeof(struct ipv6_hdr_s) + hdrsize; #endif + + UNUSED(conn); + return net_ip_domain_select(conn->domain, + sizeof(struct ipv4_hdr_s) + hdrsize, + sizeof(struct ipv6_hdr_s) + hdrsize); } #endif /* CONFIG_NET && CONFIG_NET_UDP */