You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "Swatantra-66 (via GitHub)" <gi...@apache.org> on 2026/10/15 18:46:25 UTC

[PR] libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo [nuttx]

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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo [nuttx]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #20691:
URL: https://github.com/apache/nuttx/pull/20691#issuecomment-6202767950

   <!-- membrowse-pr-comment -->
   ## <img src="https://membrowse.com/membrowse-logo.svg" height="24" align="top"> MemBrowse Memory Report
   
   *No memory changes detected for:*
   - [arduino-mega2560](https://membrowse.com/public/apache/nuttx/target/arduino-mega2560/compare?head=4ad088a1ebc17e631e7d1be8c5e4369abb280347&base=c3e180f3aeaa01bf10b9264d92eac5043c071f4c)
   - [esp32-devkitc](https://membrowse.com/public/apache/nuttx/target/esp32-devkitc/compare?head=4ad088a1ebc17e631e7d1be8c5e4369abb280347&base=c3e180f3aeaa01bf10b9264d92eac5043c071f4c)
   - [hifive1-revb](https://membrowse.com/public/apache/nuttx/target/hifive1-revb/compare?head=4ad088a1ebc17e631e7d1be8c5e4369abb280347&base=c3e180f3aeaa01bf10b9264d92eac5043c071f4c)
   - [mirtoo](https://membrowse.com/public/apache/nuttx/target/mirtoo/compare?head=4ad088a1ebc17e631e7d1be8c5e4369abb280347&base=c3e180f3aeaa01bf10b9264d92eac5043c071f4c)
   - [qemu-armv8a](https://membrowse.com/public/apache/nuttx/target/qemu-armv8a/compare?head=4ad088a1ebc17e631e7d1be8c5e4369abb280347&base=c3e180f3aeaa01bf10b9264d92eac5043c071f4c)
   - [qemu-intel64](https://membrowse.com/public/apache/nuttx/target/qemu-intel64/compare?head=4ad088a1ebc17e631e7d1be8c5e4369abb280347&base=c3e180f3aeaa01bf10b9264d92eac5043c071f4c)
   - [rx65n-rsk2mb](https://membrowse.com/public/apache/nuttx/target/rx65n-rsk2mb/compare?head=4ad088a1ebc17e631e7d1be8c5e4369abb280347&base=c3e180f3aeaa01bf10b9264d92eac5043c071f4c)
   - [s698pm-dkit](https://membrowse.com/public/apache/nuttx/target/s698pm-dkit/compare?head=4ad088a1ebc17e631e7d1be8c5e4369abb280347&base=c3e180f3aeaa01bf10b9264d92eac5043c071f4c)
   - [stm32-nucleo-f103rb](https://membrowse.com/public/apache/nuttx/target/stm32-nucleo-f103rb/compare?head=4ad088a1ebc17e631e7d1be8c5e4369abb280347&base=c3e180f3aeaa01bf10b9264d92eac5043c071f4c)
   


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo [nuttx]

Posted by "Swatantra-66 (via GitHub)" <gi...@apache.org>.
Swatantra-66 commented on PR #20691:
URL: https://github.com/apache/nuttx/pull/20691#issuecomment-6396875891

   > 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


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo [nuttx]

Posted by "Swatantra-66 (via GitHub)" <gi...@apache.org>.
Swatantra-66 commented on PR #20691:
URL: https://github.com/apache/nuttx/pull/20691#issuecomment-6230913365

   @xiaoxiang781216 Rebased onto latest master and resolved the conflict.
   


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo [nuttx]

Posted by "Swatantra-66 (via GitHub)" <gi...@apache.org>.
Swatantra-66 commented on PR #20691:
URL: https://github.com/apache/nuttx/pull/20691#issuecomment-6396900644

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


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo [nuttx]

Posted by "Swatantra-66 (via GitHub)" <gi...@apache.org>.
Swatantra-66 commented on code in PR #20691:
URL: https://github.com/apache/nuttx/pull/20691#discussion_r4302117993


##########
libs/libc/netdb/lib_getaddrinfo.c:
##########
@@ -122,6 +130,38 @@ FAR static struct ai_s *alloc_ai(int family, int socktype, int protocol,
   return ai;
 }
 
+static int add_ai(FAR struct addrinfo **res,

Review Comment:
   thank you @xiaoxiang781216.
   
   Updated `add_ai()` to eliminate `res` and let `prev_ai` (as `FAR struct addrinfo **prev_ai = res;`) thread and manage the single linked list directly.
   



-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on PR #20691:
URL: https://github.com/apache/nuttx/pull/20691#issuecomment-6229736745

   @Swatantra-66 please fix the conflict.


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #20691:
URL: https://github.com/apache/nuttx/pull/20691#discussion_r4302063655


##########
libs/libc/netdb/lib_getaddrinfo.c:
##########
@@ -122,6 +130,38 @@ FAR static struct ai_s *alloc_ai(int family, int socktype, int protocol,
   return ai;
 }
 
+static int add_ai(FAR struct addrinfo **res,

Review Comment:
   why not remove res, prev_ai is enough to keep all result in the single link list.



-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on PR #20691:
URL: https://github.com/apache/nuttx/pull/20691#issuecomment-6383030424

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


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on PR #20691:
URL: https://github.com/apache/nuttx/pull/20691#issuecomment-6387278645

   let's ignore ci error which is fixed by https://github.com/apache/nuttx/pull/20960 instead wasting ci resource by retrigger.


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo [nuttx]

Posted by "Swatantra-66 (via GitHub)" <gi...@apache.org>.
Swatantra-66 commented on code in PR #20691:
URL: https://github.com/apache/nuttx/pull/20691#discussion_r4302101559


##########
libs/libc/netdb/lib_getaddrinfo.c:
##########
@@ -122,6 +130,38 @@ FAR static struct ai_s *alloc_ai(int family, int socktype, int protocol,
   return ai;
 }
 
+static int add_ai(FAR struct addrinfo **res,

Review Comment:
   thank you @xiaoxiang781216.
   
   Updated `add_ai()` to eliminate `res` and let `prev_ai` (as `FAR struct addrinfo **prev_ai = res;`) thread and manage the single linked list directly.
   



-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo [nuttx]

Posted by "Swatantra-66 (via GitHub)" <gi...@apache.org>.
Swatantra-66 closed pull request #20691: libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo
URL: https://github.com/apache/nuttx/pull/20691


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo [nuttx]

Posted by "Swatantra-66 (via GitHub)" <gi...@apache.org>.
Swatantra-66 commented on PR #20691:
URL: https://github.com/apache/nuttx/pull/20691#issuecomment-6397393929

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


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo [nuttx]

Posted by "Swatantra-66 (via GitHub)" <gi...@apache.org>.
Swatantra-66 commented on code in PR #20691:
URL: https://github.com/apache/nuttx/pull/20691#discussion_r4302101559


##########
libs/libc/netdb/lib_getaddrinfo.c:
##########
@@ -122,6 +130,38 @@ FAR static struct ai_s *alloc_ai(int family, int socktype, int protocol,
   return ai;
 }
 
+static int add_ai(FAR struct addrinfo **res,

Review Comment:
   thank you @xiaoxiang781216.
   
   Updated `add_ai()` to eliminate `res` and let `prev_ai` (as `FAR struct addrinfo **prev_ai = res;`) thread and manage the single linked list directly.
   



-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo [nuttx]

Posted by "Swatantra-66 (via GitHub)" <gi...@apache.org>.
Swatantra-66 commented on PR #20691:
URL: https://github.com/apache/nuttx/pull/20691#issuecomment-6383933250

   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.
   


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 merged PR #20691:
URL: https://github.com/apache/nuttx/pull/20691


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc/netdb: populate default ai_protocol and support multi-transport in getaddrinfo [nuttx]

Posted by "casaroli (via GitHub)" <gi...@apache.org>.
casaroli commented on PR #20691:
URL: https://github.com/apache/nuttx/pull/20691#issuecomment-6396793743

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


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org