Skip to content

libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo - #20691

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/libc-getaddrinfo-proto
Oct 25, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/libc-getaddrinfo-proto

Conversation

@Swatantra-66

@Swatantra-66 Swatantra-66 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

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.
  7. Canonical Name Conformance: Populates ai_canonname strictly when AI_CANONNAME is specified in hints->ai_flags per POSIX specification, avoiding unwanted string assignments when not explicitly requested.
  8. Null Hostname Error Return: Returns EAI_NONAME (instead of EAI_MEMORY) when hostname == NULL yields no matching address or transport.
  9. Family Error Return: Returns EAI_FAMILY when the resolved address family is not supported or enabled in the current configuration.

Impact

  • Fixes compatibility with WAMR (WebAssembly Micro Runtime) and POSIX networking applications expecting standard ai_protocol values.
  • Strengthens POSIX error code conformance (EAI_NONAME, EAI_FAMILY, and AI_CANONNAME).
  • 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
    • hints.ai_flags = 0 -> ai_canonname remains NULL
    • hints.ai_flags = AI_CANONNAME -> ai_canonname is populated
    • hostname == NULL with no matching result -> returns EAI_NONAME

@Swatantra-66
Swatantra-66 force-pushed the fix/libc-getaddrinfo-proto branch from 3db5424 to 4ad088a Compare October 15, 2026 19:49
@github-actions github-actions Bot added Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels Oct 15, 2026
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@Swatantra-66 please fix the conflict.

@Swatantra-66
Swatantra-66 force-pushed the fix/libc-getaddrinfo-proto branch from 7264b61 to 110a9c8 Compare October 17, 2026 06:53
@Swatantra-66

Copy link
Copy Markdown
Contributor Author

@xiaoxiang781216 Rebased onto latest master and resolved the conflict.

Comment thread libs/libc/netdb/lib_getaddrinfo.c Resolved
@Swatantra-66
Swatantra-66 force-pushed the fix/libc-getaddrinfo-proto branch from 110a9c8 to 8ea0073 Compare October 17, 2026 08:55
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

please fix:

====================================================================================
Configuration/Tool: lm3s6965-ek/qemu-protected,CONFIG_ARM_TOOLCHAIN_GNU_EABI
2026-10-24 09:41:09
------------------------------------------------------------------------------------
  Cleaning...
  Configuring...
  Disabling CONFIG_ARM_TOOLCHAIN_GNU_EABI
  Enabling CONFIG_ARM_TOOLCHAIN_GNU_EABI
  Building NuttX...
arm-none-eabi-ld: nuttx_user.elf section `.data' will not fit in region `uflash'
arm-none-eabi-ld: region `uflash' overflowed by 28 bytes
make[1]: *** [Makefile:61: nuttx_user.elf] Error 1
make[1]: Target 'all' not remade because of errors.
make: *** [tools/Unix.mk:564: nuttx] Error 2
make: Target 'all' not remade because of errors.
/github/workspace/sources/nuttx/tools/testbuild.sh: line 397: /github/workspace/sources/nuttx/../nuttx/nuttx.manifest: No such file or directory
  [1/1] Normalize lm3s6965-ek/qemu-protected
/github/workspace/sources/nuttx /github/workspace/sources/nuttx
HEAD detached at pull/20691/merge
nothing to commit, working tree clean
/github/workspace/sources/nuttx
/github/workspace/sources/apps /github/workspace/sources/nuttx
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
/github/workspace/sources/nuttx
Wait 58 seconds (60 backoff)
Build Attempt 2 of 4

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) 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.

This patch:
1. Normalizes ai_protocol to IPPROTO_TCP for SOCK_STREAM and
   IPPROTO_UDP for SOCK_DGRAM when ai_protocol is 0 under Internet
   address families (AF_INET, AF_INET6, AF_UNSPEC).
2. Validates compatibility between ai_socktype and ai_protocol,
   returning EAI_SOCKTYPE for incompatible combinations.
3. Preserves explicit non-zero ai_protocol values without
   overwriting them during service resolution.
4. Returns separate records for all supported transports (both TCP
   and UDP) when ai_socktype and ai_protocol are 0.
5. Rejects SOCK_RAW with EAI_SERVICE when resolving named services.

Resolves apache#16693
Signed-off-by: Swatantra Yadav <[email protected]>
@Swatantra-66
Swatantra-66 force-pushed the fix/libc-getaddrinfo-proto branch from 1804549 to 57c1659 Compare October 24, 2026 11:56
@github-actions github-actions Bot added Size: L The size of the change in this PR is large and removed Size: M The size of the change in this PR is medium labels Oct 24, 2026
@Swatantra-66

Copy link
Copy Markdown
Contributor Author

Fixed. Refactored lib_getaddrinfo.c to

Inline addrinfo node construction into add_transports().
Guard unsupported socket families with #ifdef to eliminate dead branch logic on IPv4-only targets.
Unify and streamline the hostname == NULL passive/loopback path.

This reduces the .text size of lib_getaddrinfo by 124 bytes under ARM -Os, easily resolving the 28-byte uflash overflow.

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

let's ignore ci error which is fixed by #20960 instead wasting ci resource by retrigger.

@casaroli

Copy link
Copy Markdown
Contributor

Should we have an apps PR that sets AI_CANONNAME and SOCK_STREAM in the hints of cmd_nslookup() ?

@casaroli casaroli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we mention the behavioral changes in the commit message?

  • ai_canonname is set only with AI_CANONNAME
  • hostname == NULL with no result returns EAI_NONAME, not EAI_MEMORY.
  • A not enabled family, returns EAI_FAMILY

@Swatantra-66

Copy link
Copy Markdown
Contributor Author

Should we have an apps PR that sets AI_CANONNAME and SOCK_STREAM in the hints of cmd_nslookup() ?

Yes, That makes complete sense.
Because #20691 expands unspecified ai_socktype == 0 into distinct SOCK_STREAM and SOCK_DGRAM records, cmd_nslookup() in nshlib/nsh_netcmds.c (which passes hints == NULL) iterates through both transports and prints duplicate IP entries, while also expecting next->ai_canonname (which should formally be requested via AI_CANONNAME).
Setting hints.ai_flags = AI_CANONNAME; and hints.ai_socktype = SOCK_STREAM; in cmd_nslookup() will keep the output clean (single entry per resolved IP) and POSIX-compliant.

I can open the companion nuttx-apps PR

@Swatantra-66

Swatantra-66 commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Should we mention the behavioral changes in the commit message?

  • ai_canonname is set only with AI_CANONNAME
  • hostname == NULL with no result returns EAI_NONAME, not EAI_MEMORY.
  • A not enabled family, returns EAI_FAMILY

@casaroli thank you for approval, I have updated the PR description with these 3 points so they are documented. Since @xiaoxiang781216 noted earlier to avoid retriggering CI by pushing, I updated the PR summary instead of amending the commit to save runner resources.

@Swatantra-66

Copy link
Copy Markdown
Contributor Author

@casaroli I have opened the companion apps PR here: apache/nuttx-apps#3913

@xiaoxiang781216
xiaoxiang781216 merged commit 345643d into apache:master Oct 25, 2026
47 of 52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] getaddrinfo doesn't fill ai_protocol

4 participants