Skip to content
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

dns: Add dns.ALL hints flag constant #32183

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/api/dns.md
Expand Up @@ -209,6 +209,8 @@ configured. Loopback addresses are not considered.
* `dns.V4MAPPED`: If the IPv6 family was specified, but no IPv6 addresses were
found, then return IPv4 mapped IPv6 addresses. It is not supported
on some operating systems (e.g FreeBSD 10.1).
* `dns.ALL`: If `dns.V4MAPPED` is specified, return resolved IPv6 addresses as
well as IPv4 mapped IPv6 addresses.

## `dns.lookupService(address, port, callback)`
<!-- YAML
Expand Down
1 change: 1 addition & 0 deletions lib/dns.js
Expand Up @@ -290,6 +290,7 @@ module.exports = {

// uv_getaddrinfo flags
ADDRCONFIG: cares.AI_ADDRCONFIG,
ALL: cares.AI_ALL,
V4MAPPED: cares.AI_V4MAPPED,

// ERROR CODES
Expand Down
8 changes: 3 additions & 5 deletions lib/internal/dns/utils.js
Expand Up @@ -10,7 +10,8 @@ const {
ChannelWrap,
strerror,
AI_ADDRCONFIG,
AI_V4MAPPED
AI_ALL,
AI_V4MAPPED,
} = internalBinding('cares_wrap');
const IANA_DNS_PORT = 53;
const IPv6RE = /^\[([^[\]]*)\]/;
Expand Down Expand Up @@ -136,10 +137,7 @@ function bindDefaultResolver(target, source) {
}

function validateHints(hints) {
if (hints !== 0 &&
hints !== AI_ADDRCONFIG &&
hints !== AI_V4MAPPED &&
hints !== (AI_ADDRCONFIG | AI_V4MAPPED)) {
if ((hints & ~(AI_ADDRCONFIG | AI_ALL | AI_V4MAPPED)) !== 0) {
throw new ERR_INVALID_OPT_VALUE('hints', hints);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/cares_wrap.cc
Expand Up @@ -2187,6 +2187,9 @@ void Initialize(Local<Object> target,
target->Set(env->context(), FIXED_ONE_BYTE_STRING(env->isolate(),
"AI_ADDRCONFIG"),
Integer::New(env->isolate(), AI_ADDRCONFIG)).Check();
target->Set(env->context(), FIXED_ONE_BYTE_STRING(env->isolate(),
"AI_ALL"),
Integer::New(env->isolate(), AI_ALL)).Check();
target->Set(env->context(), FIXED_ONE_BYTE_STRING(env->isolate(),
"AI_V4MAPPED"),
Integer::New(env->isolate(), AI_V4MAPPED)).Check();
Expand Down
25 changes: 21 additions & 4 deletions test/parallel/test-dns.js
Expand Up @@ -209,14 +209,14 @@ assert.deepStrictEqual(dns.getServers(), []);
{
/*
* Make sure that dns.lookup throws if hints does not represent a valid flag.
* (dns.V4MAPPED | dns.ADDRCONFIG) + 1 is invalid because:
* - it's different from dns.V4MAPPED and dns.ADDRCONFIG.
* - it's different from them bitwise ored.
* (dns.V4MAPPED | dns.ADDRCONFIG | dns.ALL) + 1 is invalid because:
* - it's different from dns.V4MAPPED and dns.ADDRCONFIG and dns.ALL.
* - it's different from any subset of them bitwise ored.
* - it's different from 0.
* - it's an odd number different than 1, and thus is invalid, because
* flags are either === 1 or even.
*/
const hints = (dns.V4MAPPED | dns.ADDRCONFIG) + 1;
const hints = (dns.V4MAPPED | dns.ADDRCONFIG | dns.ALL) + 1;
const err = {
code: 'ERR_INVALID_OPT_VALUE',
name: 'TypeError',
Expand Down Expand Up @@ -254,11 +254,28 @@ dns.lookup('', {
hints: dns.ADDRCONFIG | dns.V4MAPPED
}, common.mustCall());

dns.lookup('', {
hints: dns.ALL
}, common.mustCall());

dns.lookup('', {
hints: dns.V4MAPPED | dns.ALL
}, common.mustCall());

dns.lookup('', {
hints: dns.ADDRCONFIG | dns.V4MAPPED | dns.ALL
}, common.mustCall());

(async function() {
await dnsPromises.lookup('', { family: 4, hints: 0 });
await dnsPromises.lookup('', { family: 6, hints: dns.ADDRCONFIG });
await dnsPromises.lookup('', { hints: dns.V4MAPPED });
await dnsPromises.lookup('', { hints: dns.ADDRCONFIG | dns.V4MAPPED });
await dnsPromises.lookup('', { hints: dns.ALL });
await dnsPromises.lookup('', { hints: dns.V4MAPPED | dns.ALL });
await dnsPromises.lookup('', {
hints: dns.ADDRCONFIG | dns.V4MAPPED | dns.ALL
});
})();

{
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-net-connect-options-port.js
Expand Up @@ -59,7 +59,7 @@ const net = require('net');
// Test invalid hints
{
// connect({hint}, cb) and connect({hint})
const hints = (dns.ADDRCONFIG | dns.V4MAPPED) + 42;
const hints = (dns.ADDRCONFIG | dns.V4MAPPED | dns.ALL) + 42;
const hintOptBlocks = doConnect([{ hints }],
() => common.mustNotCall());
for (const fn of hintOptBlocks) {
Expand Down
4 changes: 4 additions & 0 deletions test/parallel/test-tls-connect-hints-option.js
Expand Up @@ -15,7 +15,11 @@ const hints = 512;

assert.notStrictEqual(hints, dns.ADDRCONFIG);
assert.notStrictEqual(hints, dns.V4MAPPED);
assert.notStrictEqual(hints, dns.ALL);
assert.notStrictEqual(hints, dns.ADDRCONFIG | dns.V4MAPPED);
assert.notStrictEqual(hints, dns.ADDRCONFIG | dns.ALL);
assert.notStrictEqual(hints, dns.V4MAPPED | dns.ALL);
assert.notStrictEqual(hints, dns.ADDRCONFIG | dns.V4MAPPED | dns.ALL);

tls.connect({
lookup: common.mustCall((host, options) => {
Expand Down