casaroli opened a new pull request, #22439:
URL: https://github.com/apache/nuttx/pull/22439
## Summary
`gethostbyname()` and `gethostbyname_r()` asked the resolver for
`AF_UNSPEC`. Then the first address of the answer decides the family of the
result. The DNS client asks for the AAAA record before the A record
(`lib_dnsquery.c`), so a name with both kinds of address gave an IPv6 address
on a system with IPv4 and IPv6. `gethostbyname("::1")` also gave an IPv6
address. POSIX makes `gethostbyname()` the IPv4 interface, and programs copy
`h_addr_list[0]` into a `struct sockaddr_in` (in-tree: `system/nxplayer` does
this). With an IPv6 result, such a program connects to a wrong address.
With `CONFIG_NET_IPv4`, the two functions now ask for `AF_INET`, as glibc
and musl do. Without `CONFIG_NET_IPv4` they keep `AF_UNSPEC` and give IPv6
addresses, as before.
## Impact
Behaviour change on systems with IPv4 and IPv6 (`CONFIG_NET_IPv4` and
`CONFIG_NET_IPv6`): `gethostbyname()` and `gethostbyname_r()` now give IPv4
addresses only. A name that has only IPv6 addresses, or an IPv6 literal such as
`"::1"`, now gives no result (`HOST_NOT_FOUND`), as on Linux. Programs that
want IPv6 addresses use `getaddrinfo()` or `gethostbyname2()`, which do not
change. IPv4-only and IPv6-only systems do not change.
In-tree users: `system/nxplayer` copies the result into a `struct
sockaddr_in` (fixed by this change); `system/netdb --host` prints the address
it gets (now the IPv4 address for a dual-stack name). No other caller in nuttx
or nuttx-apps. No API, ABI or Kconfig change. C library code: the same in flat,
protected and kernel builds and on all architectures.
## Testing
`qemu-armv8a:nsh` (flat, IPv4 + IPv6 + loopback), a test program:
| Call | master | this PR |
|---|---|---|
| `gethostbyname("127.0.0.1")` | `AF_INET` | `AF_INET` |
| `gethostbyname("localhost")` | `AF_INET` | `AF_INET` |
| `gethostbyname("::1")` | `AF_INET6`, length 16 | NULL |
| `gethostbyname_r("::1", ...)` | `AF_INET6`, length 16 | no result |
| `gethostbyname2("::1", AF_INET6)` | `AF_INET6` | `AF_INET6` |
Builds with `-Werror` (CI image): `qemu-armv8a:netnsh` with IPv6 (flat),
`qemu-armv8a:netnsh` with IPv6 and without IPv4 (flat), `qemu-armv8a:knsh` with
networking (kernel), `rv-virt:netnsh64` (flat; the RWX linker warning is also
on master), `rv-virt:knetnsh64` (kernel), all OK with no new warnings.
checkpatch (`-c -u -m -g`) passes.
--
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]