From 548f91af2c0e13fc24ff6a51f7f8fb2e5af9cc64 Mon Sep 17 00:00:00 2001 From: Josh Dague Date: Mon, 17 Aug 2020 23:23:25 -0700 Subject: [PATCH] dns: add setLocalAddress to Resolver Fixes: https://github.com/nodejs/node/issues/34818 PR-URL: https://github.com/nodejs/node/pull/34824 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Roman Reiss --- doc/api/dns.md | 21 ++++++++ lib/internal/dns/promises.js | 1 + lib/internal/dns/utils.js | 12 +++++ src/cares_wrap.cc | 66 +++++++++++++++++++++++ test/parallel/test-dns-setlocaladdress.js | 37 +++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 test/parallel/test-dns-setlocaladdress.js diff --git a/doc/api/dns.md b/doc/api/dns.md index a1f4240981f6cf..638321f222a940 100644 --- a/doc/api/dns.md +++ b/doc/api/dns.md @@ -117,6 +117,27 @@ added: v8.3.0 Cancel all outstanding DNS queries made by this resolver. The corresponding callbacks will be called with an error with code `ECANCELLED`. +### `resolver.setLocalAddress([ipv4][, ipv6])` + + +* `ipv4` {string} A string representation of an IPv4 address. + **Default:** `'0.0.0.0'` +* `ipv6` {string} A string representation of an IPv6 address. + **Default:** `'::0'` + +The resolver instance will send its requests from the specified IP address. +This allows programs to specify outbound interfaces when used on multi-homed +systems. + +If a v4 or v6 address is not specified, it is set to the default, and the +operating system will choose a local address automatically. + +The resolver will use the v4 local address when making requests to IPv4 DNS +servers, and the v6 local address when making requests to IPv6 DNS servers. +The `rrtype` of resolution requests has no impact on the local address used. + ## `dns.getServers()`