This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new c95fd46be3 net: Simplify getting value for different domain
c95fd46be3 is described below

commit c95fd46be3086c1075c758c3069bb2221866eeba
Author: Zhe Weng <weng...@xiaomi.com>
AuthorDate: Wed Nov 1 15:38:53 2023 +0800

    net: Simplify getting value for different domain
    
    Signed-off-by: Zhe Weng <weng...@xiaomi.com>
---
 include/nuttx/net/ip.h | 45 +++++++++++++++++++++++++++++++--------------
 net/socket/net_fstat.c | 11 +++--------
 net/tcp/tcp_ioctl.c    |  8 +-------
 net/tcp/tcp_send.c     | 24 ++++--------------------
 net/udp/udp_ioctl.c    |  8 +-------
 net/udp/udp_send.c     | 26 ++++++++------------------
 6 files changed, 48 insertions(+), 74 deletions(-)

diff --git a/include/nuttx/net/ip.h b/include/nuttx/net/ip.h
index 24c5b48a65..ec97434a21 100644
--- a/include/nuttx/net/ip.h
+++ b/include/nuttx/net/ip.h
@@ -421,32 +421,49 @@ extern "C"
    (ipv6addr)->s6_addr16[5] == 0xffff)
 
 /****************************************************************************
- * Macro: net_ip_binding_laddr, net_ip_binding_raddr
+ * Macro: net_ip_domain_select
  *
  * Description:
- *   Get the laddr/raddr pointer form an ip_binding_u.
+ *   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:
- *   u      - The union of address binding.
- *   domain - The domain of address.
+ *   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_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))
+#  define net_ip_domain_select(domain, value4, value6) \
+    (((domain) == PF_INET) ? (value4) : (value6))
 #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))
+#  define net_ip_domain_select(domain, value4, value6) (value4)
 #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))
+#  define net_ip_domain_select(domain, value4, value6) (value6)
 #endif
 
+/****************************************************************************
+ * 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, only needs to exist when both IPv4 and
+ *            IPv6 are enabled.
+ *
+ ****************************************************************************/
+
+#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
  *        net_ipv6addr_hdrcopy
diff --git a/net/socket/net_fstat.c b/net/socket/net_fstat.c
index a35b5d9594..a998ad9914 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 83754efa34..f96254fc35 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 68cd1830fc..7651839a46 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 79ce573c20..41392fa9a3 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 39e0ffe665..c3d5228f2e 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 */

Reply via email to