net/procfs: Simplify logic for tcp/udp stats
By: 1. Take out net_ip_binding_laddr/raddr macro 2. INET6_ADDRSTRLEN is long enough for all address buffer and no need to add 0 to the end of buffer since inet_ntop will do so Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
This commit is contained in:
parent
858e43df91
commit
194d0cdec9
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue