net: Simplify getting value for different domain

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
This commit is contained in:
Zhe Weng 2023-11-01 15:38:53 +08:00 committed by Xiang Xiao
parent 2f5bb9200a
commit c95fd46be3
6 changed files with 49 additions and 75 deletions

View File

@ -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

View File

@ -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);

View File

@ -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);

View File

@ -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 */

View File

@ -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);

View File

@ -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 */