Skip to content

dns: support IPv6-only and IPv4-only in setLocalAddress() - #58701

Open
mohiuddin-khan-shiam wants to merge 2 commits into
nodejs:mainfrom
mohiuddin-khan-shiam:main
Open

mohiuddin-khan-shiam wants to merge 2 commits into
nodejs:mainfrom
mohiuddin-khan-shiam:main

Conversation

@mohiuddin-khan-shiam

Copy link
Copy Markdown

What was fixed

Resolver.prototype.setLocalAddress(ipv4, ipv6) incorrectly required an IPv4 string even when callers only wanted to set an IPv6 local address.
Calling resolver.setLocalAddress(undefined, '::1') threw ERR_INVALID_ARG_TYPE, breaking the documented API and blocking IPv6-only use-cases.

Changes

  • lib/internal/dns/utils.js
    • Added ERR_MISSING_ARGS to imported error codes.
    • Re-implemented argument validation:
      • Now validates each address only when provided.
      • Throws ERR_MISSING_ARGS('ipv4','ipv6') when both arguments are omitted.
    • Keeps snapshot state logic unchanged.

Impact

Restores conformance with public documentation:

  • Users can bind a resolver to IPv6 only, IPv4 only, or both.
  • No breaking change for existing valid code; previously invalid calls now work as intended.

Refs: documentation for resolver.setLocalAddress

…ly) addresses

## Title
dns: allow `resolver.setLocalAddress()` to accept IPv6-only or IPv4-only addresses

## Description
`ResolverBase.prototype.setLocalAddress()` previously **required** the first argument (`ipv4`) to be a non-empty string, throwing `ERR_INVALID_ARG_TYPE` whenever a user tried to set **only** an IPv6 local address:

```js
const r = new (require('dns').Resolver)();
r.setLocalAddress(undefined, '::1'); // ❌ TypeError

This broke documented API behaviour and prevented IPv6-only configurations.

Changes
lib/internal/dns/utils.js
Imported ERR_MISSING_ARGS.
Updated
setLocalAddress()
:
If both ipv4 and ipv6 are undefined, throw ERR_MISSING_ARGS('ipv4', 'ipv6').
Validate each address only when provided.
Preserve snapshot-state logic.
Result
setLocalAddress()
 now works correctly with any of:

resolver.setLocalAddress('0.0.0.0'); // IPv4-only
resolver.setLocalAddress(undefined, '::1'); // IPv6-only
resolver.setLocalAddress('0.0.0.0', '::1'); // both

Restores compliance with public documentation and unblocks IPv6-only deployments.
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/net

@nodejs-github-bot nodejs-github-bot added dns Issues and PRs related to the dns subsystem. needs-ci PRs that need a full CI run. labels Jun 12, 2025
@codecov

codecov Bot commented Jun 14, 2025 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.14%. Comparing base (bba07d7) to head (5858af6).
⚠️ Report is 4318 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #58701      +/-   ##
==========================================
- Coverage   90.17%   90.14%   -0.03%     
==========================================
  Files         637      637              
  Lines      188023   188029       +6     
  Branches    36887    36889       +2     
==========================================
- Hits       169552   169505      -47     
- Misses      11223    11276      +53     
  Partials     7248     7248              
Files with missing lines Coverage Δ
lib/internal/dns/utils.js 99.45% <100.00%> (+<0.01%) ⬆️

... and 32 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Ethan-Arrowood Ethan-Arrowood 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.

lgtm, please fix commit so it follows guidelines ( i think the title is too long )

@mohiuddin-khan-shiam mohiuddin-khan-shiam changed the title dns: allow resolver.setLocalAddress() to accept IPv6-only / IPv4-only addresses dns: support IPv6-only and IPv4-only in setLocalAddress() Jun 14, 2025
@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been marked as stale due to 210 days of inactivity.
It will be automatically closed in 30 days if no further activity occurs. If this is still relevant, please leave a comment or update it to keep it open.

@github-actions github-actions Bot added stale Issues and PRs marked stale due to inactivity and scheduled for automatic closure. and removed stale Issues and PRs marked stale due to inactivity and scheduled for automatic closure. labels Apr 19, 2026
@panva panva closed this Sep 26, 2026
@panva panva reopened this Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dns Issues and PRs related to the dns subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants