Skip to content

Commit

Permalink
dns: add dns.ALL hints flag constant
Browse files Browse the repository at this point in the history
PR-URL: #32183
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
murgatroid99 authored and addaleax committed Mar 30, 2020
1 parent b494053 commit e14317a
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 10 deletions.
2 changes: 2 additions & 0 deletions doc/api/dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -2188,6 +2188,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit e14317a

Please sign in to comment.