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

xiaoxiang781216 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 9b748d92a50 libs/libc/net: Make the IPv6 text of inet_ntop() follow 
RFC 5952.
9b748d92a50 is described below

commit 9b748d92a50cb2ddec4c2a272e519f90e417ff68
Author: Marco Casaroli <[email protected]>
AuthorDate: Wed Nov 11 08:16:01 2026 +0100

    libs/libc/net: Make the IPv6 text of inet_ntop() follow RFC 5952.
    
    inet_ntop() with AF_INET6 had three faults:
    
    - "::" also replaced one 16-bit 0 field: 1:0:2:3:4:5:6:7 became
      1::2:3:4:5:6:7.  RFC 5952 4.2.2 forbids this.
    - An IPv4-mapped address (::ffff:1.2.3.4) and an IPv4-compatible address
      (::1.2.3.4) got no dotted IPv4 part: ::ffff:102:304 and ::102:304.
      RFC 5952 5 recommends the dotted form for the mapped address, and glibc
      prints the compatible address so too.
    - The call failed with ENOSPC when size was less than INET6_ADDRSTRLEN
      (INET_ADDRSTRLEN for AF_INET), also when the text fits.  POSIX: ENOSPC
      only when the size is too small for the result.
    
    Shorten only a run of two or more 0 fields, the longest one (the first on
    a tie).  Print the last 32 bits in dotted form for ::ffff:0:0/96 and for
    ::/96 when the 0 run is exactly six fields (that excludes :: and ::1).
    Make the text in a local buffer and fail with ENOSPC only when it and its
    NUL do not fit in size.  AF_INET checks the size in the same way.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 libs/libc/net/lib_inetntop.c | 134 ++++++++++++++++++++++---------------------
 1 file changed, 69 insertions(+), 65 deletions(-)

diff --git a/libs/libc/net/lib_inetntop.c b/libs/libc/net/lib_inetntop.c
index fd2193b17cb..fc8d20c3892 100644
--- a/libs/libc/net/lib_inetntop.c
+++ b/libs/libc/net/lib_inetntop.c
@@ -99,14 +99,8 @@
 static int inet_ipv4_ntop(FAR const void *src, FAR char *dest,
                           socklen_t size)
 {
-  FAR uint8_t *ptr;
-
-  if (size < INET_ADDRSTRLEN)
-    {
-      return -ENOSPC;
-    }
-
-  ptr = (FAR uint8_t *)src;
+  FAR const uint8_t *ptr = src;
+  size_t len;
 
   /* Data is in network order.  However, indexed access is the same in both
    * big and little endian cases:
@@ -131,8 +125,11 @@ static int inet_ipv4_ntop(FAR const void *src, FAR char 
*dest,
    *                +---+---+---+---+
    */
 
-  snprintf(dest, INET_ADDRSTRLEN, "%u.%u.%u.%u",
-           ptr[0], ptr[1], ptr[2], ptr[3]);
+  len = snprintf(dest, size, "%u.%u.%u.%u", ptr[0], ptr[1], ptr[2], ptr[3]);
+  if (len >= size)
+    {
+      return -ENOSPC;
+    }
 
   return OK;
 }
@@ -170,82 +167,89 @@ static int inet_ipv4_ntop(FAR const void *src, FAR char 
*dest,
 static int inet_ipv6_ntop(FAR const void *src, FAR char *dest,
                           socklen_t size)
 {
-  FAR const struct in6_addr *in6_addr;
+  FAR const struct in6_addr *in6_addr = src;
+  char str[INET6_ADDRSTRLEN];
   uint16_t warray[8];
-  int offset;
-  int entry;
-  int count;
-  int maxentry;
-  int maxcount;
-
-  if (size < INET6_ADDRSTRLEN)
-    {
-      return -ENOSPC;
-    }
+  int maxentry = -1;
+  int maxcount = 0;
+  int entry = -1;
+  int count = 0;
+  size_t len = 0;
+  int i;
 
-  in6_addr = (FAR const struct in6_addr *)src;
-  entry    = -1;
-  maxentry = -1;
-  maxcount = 0;
-  offset   = 0;
+  /* Find the longest run of zero words, the first one on a tie */
 
-  while (offset < 8)
+  for (i = 0; i < 8; i++)
     {
-      warray[offset] = NTOHS(in6_addr->s6_addr16[offset]);
-      if (warray[offset] == 0)
+      warray[i] = NTOHS(in6_addr->s6_addr16[i]);
+      if (warray[i] != 0)
         {
-          entry = offset;
-          count = 1;
-          offset++;
-
-          while (offset < 8)
-            {
-              warray[offset] = NTOHS(in6_addr->s6_addr16[offset]);
-              if (warray[offset] != 0)
-                {
-                  break;
-                }
-
-              offset++;
-              count++;
-            }
+          entry = -1;
+          continue;
+        }
 
-          if (count > maxcount)
-            {
-              maxentry = entry;
-              maxcount = count;
-            }
+      if (entry < 0)
+        {
+          entry = i;
+          count = 0;
         }
 
-      offset++;
+      if (++count > maxcount)
+        {
+          maxentry = entry;
+          maxcount = count;
+        }
     }
 
-  offset = 0;
-  dest[0] = '\0';
+  /* RFC 5952 4.2.2: "::" must not shorten just one 16-bit 0 field */
 
-  while (offset < 8)
+  if (maxcount < 2)
     {
-      if (offset == maxentry)
+      maxentry = -1;
+    }
+
+  for (i = 0; i < 8; i++)
+    {
+      if (i == maxentry)
         {
-          size   -= snprintf(&dest[strlen(dest)], size, ":");
-          offset += maxcount;
-          if (offset >= 8)
+          str[len++] = ':';
+          i += maxcount - 1;
+          if (i == 7)
             {
-              size -= snprintf(&dest[strlen(dest)], size, ":");
+              str[len++] = ':';
             }
+
+          continue;
         }
-      else
+
+      if (i > 0)
         {
-          if (offset > 0)
-            {
-              size -= snprintf(&dest[strlen(dest)], size, ":");
-            }
+          str[len++] = ':';
+        }
+
+      /* An IPv4-compatible (::a.b.c.d) or IPv4-mapped (::ffff:a.b.c.d)
+       * address ends in the dotted IPv4 form.
+       */
 
-          size -= snprintf(&dest[strlen(dest)], size, "%x", warray[offset]);
-          offset++;
+      if (i == 6 && maxentry == 0 &&
+          (maxcount == 6 || (maxcount == 5 && warray[5] == 0xffff)))
+        {
+          len += snprintf(&str[len], sizeof(str) - len, "%u.%u.%u.%u",
+                          in6_addr->s6_addr[12], in6_addr->s6_addr[13],
+                          in6_addr->s6_addr[14], in6_addr->s6_addr[15]);
+          break;
         }
+
+      len += snprintf(&str[len], sizeof(str) - len, "%x", warray[i]);
+    }
+
+  str[len] = '\0';
+  if (len >= size)
+    {
+      return -ENOSPC;
     }
 
+  memcpy(dest, str, len + 1);
   return OK;
 }
 #endif

Reply via email to