Skip to content

nshlib/nsh_netcmds: set AI_CANONNAME and SOCK_STREAM hints in cmd_nslookup - #3913

Open
Swatantra-66 wants to merge 1 commit into
apache:masterfrom
Swatantra-66:fix/nsh-nslookup-hints
Open

Swatantra-66 wants to merge 1 commit into
apache:masterfrom
Swatantra-66:fix/nsh-nslookup-hints

Conversation

@Swatantra-66

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

Copy link
Copy Markdown
Contributor

Summary

Companion to apache/nuttx#20691 (addressing review suggestion from @casaroli).

Previously, cmd_nslookup() in nshlib/nsh_netcmds.c called getaddrinfo() with hints == NULL.
When ai_socktype is unspecified (0), POSIX-compliant getaddrinfo() (such as after apache/nuttx PR #20691) performs multi-transport expansion and returns distinct records for all supported transports (both SOCK_STREAM and SOCK_DGRAM), causing nslookup to iterate and print duplicate IP entries for every host.

Additionally, per IEEE Std 1003.1 (POSIX):

  1. ai_canonname should be explicitly requested via hints.ai_flags = AI_CANONNAME.
  2. ai_canonname is only guaranteed to be set on the first returned addrinfo structure (info->ai_canonname). For hosts with multiple addresses (e.g., dual-stack IPv4/IPv6 or multiple A records), subsequent next->ai_canonname entries can be NULL.

This PR:

  1. Defines and passes struct addrinfo hints with hints.ai_socktype = SOCK_STREAM so that cmd_nslookup() receives a single clean record per resolved IP address.
  2. Sets hints.ai_flags = AI_CANONNAME to formally request the canonical name.
  3. Uses info->ai_canonname != NULL ? info->ai_canonname : argv[1] across all output rows so each address line prints the host name safely without passing NULL to %s.

Impact

nshlib: nslookup command displays single, clean entries per IP address with the canonical host name properly populated.
Zero breaking changes to other NSH commands.
0 nxstyle errors.

Testing

Verified with git diff --check (0 whitespace errors).
Validated column length and code style against nxstyle guidelines (max line 75 chars, 0 tabs, clean variable scoping).

…ookup

cmd_nslookup() previously called getaddrinfo() with hints == NULL.
When ai_socktype is unspecified (0), getaddrinfo() performs
multi-transport expansion and returns distinct records for all supported
transports (both SOCK_STREAM and SOCK_DGRAM), resulting in duplicate IP
entries being printed for each host.

Additionally, IEEE Std 1003.1 (POSIX) specifies that ai_canonname is
only guaranteed in the first returned addrinfo structure when AI_CANONNAME
is explicitly set in hints.ai_flags.

This commit:
1. Specifies AI_CANONNAME and SOCK_STREAM in hints so cmd_nslookup()
   returns a single record per IP address.
2. Uses the first result's ai_canonname with a fallback to argv[1]
   across all rows.
3. Fixes pre-existing nxstyle declaration blank line warnings in
   wget_callback() and cmd_wget().

Signed-off-by: Swatantra Yadav <maverickswatantra@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants