Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mohiuddin-khan-shiam
Copy link

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
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
Copy link

codecov bot commented Jun 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.14%. Comparing base (bba07d7) to head (5858af6).
Report is 8 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.

Copy link
Contributor

@Ethan-Arrowood Ethan-Arrowood left a comment

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

3 participants