diff --git a/include/nuttx/net/ip.h b/include/nuttx/net/ip.h index 8457a8579b..b6b554e594 100644 --- a/include/nuttx/net/ip.h +++ b/include/nuttx/net/ip.h @@ -421,6 +421,33 @@ extern "C" (ipv6addr)->s6_addr16[4] == 0 && \ (ipv6addr)->s6_addr16[5] == 0xffff) +/**************************************************************************** + * Macro: net_ip_binding_laddr, net_ip_binding_raddr + * + * Description: + * Get the laddr/raddr pointer form an ip_binding_u. + * + * Input Parameters: + * u - The union of address binding. + * domain - The domain of address. + * + ****************************************************************************/ + +#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 + /**************************************************************************** * Macro: net_ipv4addr_copy, net_ipv4addr_hdrcopy, net_ipv6addr_copy, and * net_ipv6addr_hdrcopy diff --git a/net/procfs/net_tcp.c b/net/procfs/net_tcp.c index 353bc181ca..942b77f114 100644 --- a/net/procfs/net_tcp.c +++ b/net/procfs/net_tcp.c @@ -60,17 +60,14 @@ static ssize_t netprocfs_tcpstats(FAR struct netprocfs_file_s *priv, int addrlen = (domain == PF_INET) ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN; FAR struct tcp_conn_s *conn = NULL; - char remote[INET6_ADDRSTRLEN + 1]; - char local[INET6_ADDRSTRLEN + 1]; + char remote[INET6_ADDRSTRLEN]; + char local[INET6_ADDRSTRLEN]; int len = 0; - void *laddr; - void *raddr; + FAR void *laddr; + FAR void *raddr; net_lock(); - local[addrlen] = '\0'; - remote[addrlen] = '\0'; - while ((conn = tcp_nextconn(conn)) != NULL) { #if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) @@ -90,25 +87,8 @@ static ssize_t netprocfs_tcpstats(FAR struct netprocfs_file_s *priv, break; } -#ifdef CONFIG_NET_IPv4 -# ifdef CONFIG_NET_IPv6 - if (domain == PF_INET) -# endif /* CONFIG_NET_IPv6 */ - { - laddr = &conn->u.ipv4.laddr; - raddr = &conn->u.ipv4.raddr; - } -#endif /* CONFIG_NET_IPv4 */ - -#ifdef CONFIG_NET_IPv6 -# ifdef CONFIG_NET_IPv4 - else -# endif /* CONFIG_NET_IPv4 */ - { - laddr = &conn->u.ipv6.laddr; - raddr = &conn->u.ipv6.raddr; - } -#endif /* CONFIG_NET_IPv6 */ + laddr = net_ip_binding_laddr(&conn->u, domain); + raddr = net_ip_binding_raddr(&conn->u, domain); len += snprintf(buffer + len, buflen - len, " %2" PRIu8 diff --git a/net/procfs/net_udp.c b/net/procfs/net_udp.c index 26ba034dbd..04d616bed2 100644 --- a/net/procfs/net_udp.c +++ b/net/procfs/net_udp.c @@ -60,17 +60,14 @@ static ssize_t netprocfs_udpstats(FAR struct netprocfs_file_s *priv, int addrlen = (domain == PF_INET) ? INET_ADDRSTRLEN : INET6_ADDRSTRLEN; FAR struct udp_conn_s *conn = NULL; - char remote[INET6_ADDRSTRLEN + 1]; - char local[INET6_ADDRSTRLEN + 1]; + char remote[INET6_ADDRSTRLEN]; + char local[INET6_ADDRSTRLEN]; int len = 0; - void *laddr; - void *raddr; + FAR void *laddr; + FAR void *raddr; net_lock(); - local[addrlen] = '\0'; - remote[addrlen] = '\0'; - while ((conn = udp_nextconn(conn)) != NULL) { #if defined(CONFIG_NET_IPv4) && defined(CONFIG_NET_IPv6) @@ -90,25 +87,8 @@ static ssize_t netprocfs_udpstats(FAR struct netprocfs_file_s *priv, break; } -#ifdef CONFIG_NET_IPv4 -# ifdef CONFIG_NET_IPv6 - if (domain == PF_INET) -# endif /* CONFIG_NET_IPv6 */ - { - laddr = &conn->u.ipv4.laddr; - raddr = &conn->u.ipv4.raddr; - } -#endif /* CONFIG_NET_IPv4 */ - -#ifdef CONFIG_NET_IPv6 -# ifdef CONFIG_NET_IPv4 - else -# endif /* CONFIG_NET_IPv4 */ - { - laddr = &conn->u.ipv6.laddr; - raddr = &conn->u.ipv6.raddr; - } -#endif /* CONFIG_NET_IPv6 */ + laddr = net_ip_binding_laddr(&conn->u, domain); + raddr = net_ip_binding_raddr(&conn->u, domain); len += snprintf(buffer + len, buflen - len, " %2" PRIu8