casaroli opened a new pull request, #21688: URL: https://github.com/apache/nuttx/pull/21688
## Summary `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 and an IPv4-compatible address got no dotted IPv4 part: `::ffff:1.2.3.4` became `::ffff:102:304`, and `::1.2.3.4` became `::102:304`. RFC 5952 5 recommends the dotted form for the mapped address; glibc prints both in the dotted form. - 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. This PR shortens only a run of two or more 0 fields, the longest one (the first one on a tie), prints the last 32 bits in dotted form for `::ffff:0:0/96` and for `::/96` when the 0 run is exactly six fields (so not `::`, `::1` or `::102`), and fails with ENOSPC only when the text and its NUL do not fit in `size`. The text is lowercase, as before. The result is the same as glibc for every address and every size (see Testing). ## Impact - Behaviour change, output text: some IPv6 addresses print differently. One 0 field: `2001:db8:0:1:1:1:1:1` (was `2001:db8::1:1:1:1:1`). IPv4-mapped: `::ffff:192.168.1.2` (was `::ffff:c0a8:102`), also `::ffff:0.0.0.0` (was `::ffff:0:0`). IPv4-compatible: `::1.2.3.4` (was `::102:304`). Programs that parse the text with `inet_pton()` get the same address back. Users in the tree: the netdb code (getnameinfo, DNS server list), net/procfs (route, tcp, udp, netdev) and the TCP/UDP debug output show the new text; a dual-stack socket that talks to an IPv4 peer now shows `::ffff:a.b.c.d` in /proc/net/tcp and /proc/net/udp. - Behaviour change, ENOSPC: a buffer smaller than `INET6_ADDRSTRLEN` (`INET_ADDRSTRLEN`) now works when the text fits; before, the call failed. In the tree, net/ and the DNS code pass at least these sizes. `getnameinfo()` with `NI_NUMERICHOST` passes the size of the caller: it now fails with EAI_OVERFLOW only when the text does not fit. - The code is libc: the same in flat, protected and kernel builds, on every architecture. It is built with `NET_IPv4`/`NET_IPv6` or `LIBC_IPv4_ADDRCONV`/`LIBC_IPv6_ADDRCONV`. - Size: +70 bytes of text for lib_inetntop.o (588 -> 658), +72 for the image (qemu-armv8a:nsh with `LIBC_IPv4_ADDRCONV` and `LIBC_IPv6_ADDRCONV`, aarch64-none-elf-gcc 14.2). - Documentation/ does not describe the text of `inet_ntop()` (only Documentation/standards/posix.rst lists the function), so there is no documentation change. ## Testing Host comparison with glibc 2.43 (fedora x86_64): lib_inetntop.c built on the host as `nx_inet_ntop()` and compared with the glibc `inet_ntop()` (return value, errno, text) for: all 256 patterns of 0 and non-0 fields with 9 values each, 20000 IPv4-mapped, IPv4-compatible and near addresses, every buffer size from 0 to 47 for each, random addresses (two sizes each), and `AF_INET` addresses (a quarter of the random count) with every size from 0 to 17. | Source | Cases | Differences from glibc | |---|---|---| | master 03aea786390 (1 million random addresses) | 7,588,593 | 2,043,408 (415,085 of them with a 46-byte buffer, the others ENOSPC) | | this PR, 4 runs with different seeds (5 million random addresses each) | 4 x 33,588,593 | 0 | New test apache/nuttx-apps#NNNN (`testing/libc/inetntop`, 29 cases, also checked against glibc on the host: 0 errors). `EXTRAFLAGS="-Wno-cpp -Werror"`, master 03aea786390 vs this PR, apps master 72e5a3cd6 + the test, `LIBC_IPv4_ADDRCONV` and `LIBC_IPv6_ADDRCONV` (the configurations have no network): | Configuration | master | this PR | ostest | |---|---|---|---| | qemu-armv8a:knsh (kernel, arm64, QEMU HVF) | 37 errors | 0 errors | status 0 (both) | | qemu-armv8a:nsh (flat, arm64, QEMU HVF) | 37 errors | 0 errors | status 0 (both) | | qemu-armv8a:nsh, CMake | not run | 0 errors | not run | On master the 37 errors are 28 ENOSPC with a buffer that fits and 9 wrong texts (one 0 field shortened, no dotted IPv4 part). All builds have no warnings with `-Werror`. `tools/checkpatch.sh -c -u -m -g origin/master..HEAD` passes. Not run: protected build, 32-bit targets (the code has no width dependency: `uint16_t` fields, `size_t` length). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
