Swatantra-66 opened a new pull request, #20691:
URL: https://github.com/apache/nuttx/pull/20691

   ## Summary
   Resolves #16693.
   
   When `getaddrinfo()` is called with `hints->ai_socktype` set to 
`SOCK_STREAM` or `SOCK_DGRAM` and `hints->ai_protocol` set to `0`, standard 
networking implementations (KAME, BSD, glibc, musl) populate `ai_protocol` with 
`IPPROTO_TCP` or `IPPROTO_UDP` respectively.
   
   Previously, NuttX left `ai_protocol` as `0`, which broke applications and 
runtimes (such as WAMR - WebAssembly Micro Runtime) that validate or rely on 
standard `ai_protocol` values.
   
   Furthermore, POSIX requires that when both `ai_socktype` and `ai_protocol` 
are unspecified (`0`), `getaddrinfo()` returns all supported transports (e.g., 
both `SOCK_STREAM`/`IPPROTO_TCP` and `SOCK_DGRAM`/`IPPROTO_UDP`) for each 
resolved address so that callers can choose their transport.
   
   ### Key Changes:
   1. **Protocol Defaulting**: Normalizes `ai_protocol` to `IPPROTO_TCP` for 
`SOCK_STREAM` and `IPPROTO_UDP` for `SOCK_DGRAM` when `ai_protocol == 0` under 
Internet address families (`AF_INET`, `AF_INET6`, `AF_UNSPEC`).
   2. **Socket Type Validation**: Validates `ai_socktype` for Internet families 
(accepting `0`, `SOCK_STREAM`, `SOCK_DGRAM`, `SOCK_RAW`) and returns 
`EAI_SOCKTYPE` for incompatible type/protocol combinations without regressing 
non-Internet families (`AF_LOCAL`, `AF_RPMSG`) that support `SOCK_SEQPACKET`.
   3. **Preserve Explicit Protocol**: Preserves non-zero `ai_protocol` values 
during service resolution, returning `EAI_SERVICE` if an incompatible named 
service is queried rather than inferring TCP over the caller's explicit 
protocol.
   4. **Multi-Transport Expansion**: Returns separate records for all supported 
transports (both TCP and UDP) when `ai_socktype == 0` and `ai_protocol == 0`, 
querying both protocols via `getservbyname_r()` for named services.
   5. **Raw Socket Protection**: Rejects `SOCK_RAW` with `EAI_SERVICE` when 
resolving named services.
   6. **Port Preservation**: Updates `port = transports[0].port` after named 
service lookups to ensure local, RPMSG, and VSOCK consumers receive the 
resolved port.
   
   ## Impact
   - Fixes compatibility with WAMR (WebAssembly Micro Runtime) and POSIX 
networking applications expecting standard `ai_protocol` values.
   - Zero breaking changes to existing network drivers or libc consumers.
   - 0 `nxstyle` errors.
   
   ## Testing
   - Verified with standalone unit test harness covering:
     - `hints.ai_socktype = SOCK_STREAM, hints.ai_protocol = 0` -> returns 
`IPPROTO_TCP`
     - `hints.ai_socktype = SOCK_DGRAM, hints.ai_protocol = 0` -> returns 
`IPPROTO_UDP`
     - `hints.ai_socktype = 0, hints.ai_protocol = 0` -> returns both TCP and 
UDP transports
     - `hints.ai_socktype = SOCK_STREAM, hints.ai_protocol = IPPROTO_UDP` -> 
returns `EAI_SOCKTYPE`
     - `hints.ai_socktype = SOCK_RAW, servname = "http"` -> returns 
`EAI_SERVICE`
     - `AF_LOCAL` with `SOCK_SEQPACKET` -> succeeds without `EAI_SOCKTYPE`
   


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

Reply via email to