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: make dns.setServers support customized port #13723

Closed
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions doc/api/dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,21 @@ the [Implementation considerations section][] for more information.
added: v0.11.3
-->

Returns an array of IP address strings that are being used for name
resolution.
Returns an array of IP address strings, formatted according to [rfc3986][],
that are currently configured for DNS resolution. A string will include a port
section if a custom port is used.

eg.

<!-- eslint-disable -->
```js
[
'4.4.4.4',
'[2001:4860:4860::8888]',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to remove [] for this line.
Ref: #13795

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs to be changed, since currently the output will be without the []

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> dns.setServers(['192.168.1.1', '2001:4860:4860::8888'])
undefined
> dns.getServers()
[ '192.168.1.1', '2001:4860:4860::8888' ]

'4.4.4.4:1053',
'[2001:4860:4860::8888]:1053'
]
```

## dns.lookup(hostname[, options], callback)
<!-- YAML
Expand Down Expand Up @@ -482,12 +495,22 @@ one of the [DNS error codes][].
<!-- YAML
added: v0.11.3
-->
- `servers` {string[]}
- `servers` {string[]} array of [rfc3986][] formatted addresses

Sets the IP address and port of servers to be used when performing DNS
resolution. The `servers` argument is an array of [rfc3986][] formatted
addresses. If the port is the IANA default DNS port (53) it can be omitted.

Sets the IP addresses of the servers to be used when resolving. The `servers`
argument is an array of IPv4 or IPv6 addresses.
eg.

If a port is specified on the address, it will be removed.
```js
dns.setServers([
'4.4.4.4',
'[2001:4860:4860::8888]',
'4.4.4.4:1053',
'[2001:4860:4860::8888]:1053'
]);
```

An error will be thrown if an invalid address is provided.

Expand Down Expand Up @@ -583,3 +606,4 @@ uses. For instance, _they do not use the configuration from `/etc/hosts`_.
[supported `getaddrinfo` flags]: #dns_supported_getaddrinfo_flags
[the official libuv documentation]: http://docs.libuv.org/en/latest/threadpool.html
[`util.promisify()`]: util.html#util_util_promisify_original
[rfc3986]: https://tools.ietf.org/html/rfc3986#section-3.2.2
25 changes: 17 additions & 8 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,13 @@ function resolve(hostname, rrtype, callback) {


function getServers() {
return cares.getServers();
const ret = cares.getServers();
return ret.map((val) => {
if (!val[1] || val[1] === 53) return val[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed this, IMHO we should format IPv6 with [], so need to move the const host up:
e.g.

return ret.map(([host, port]) => {
  if (isIP(host) === 6) host = `[${host}]`;
  return (!port || port === 53) host : `${host}:${port}`;
}

But that will make this semver-major (since till now IPv6 would be represented without [])

Copy link
Contributor

@refack refack Jun 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix this in a future PR.
Ref: #13795

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put the 53 into a constant. It's used a few times throughout.

Copy link
Contributor

@refack refack Jun 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: IANA_DNS_PORT


const host = isIP(val[0]) === 6 ? `[${val[0]}]` : val[0];
return `${host}:${val[1]}`;
});
}


Expand All @@ -311,26 +317,29 @@ function setServers(servers) {
const orig = cares.getServers();
const newSet = [];
const IPv6RE = /\[(.*)\]/;
const addrSplitRE = /:\d+$/;
const addrSplitRE = /(^.+?)(?::(\d+))?$/;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another possible way to handle this that may provide more robust and consistent handling of the address and port would be to leverage the new URL parser. Essentially:

const URL = require('url').URL;
const u = new URL(`dns://${serv}`);

If perf is a concern around creating multiple URL objects, one can be created and reused:

const url = new URL(`dns://${serv}`);
// use it
url.host = nextServ;
// etc

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm +1 since this is a "cold-spot"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But how about something like 8.8.8.8:53/abc?foo=bar?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

three options:

  1. allow
  2. check url.path === ''
  3. keep RegEx

Copy link
Contributor Author

@XadillaX XadillaX Jun 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think keeping RegEx would be fine because URL does some extra operation in semantics, though URL's performance may be better than RegEx.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I follow? Just ignore any additional input. The host and the port are the only bits we care about. If you do not think it is appropriate to ignore the rest, then throw if values for the other parts are provided.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I follow? Just ignore any additional input. The host and the port are the only bits we care about. If you do not think it is appropriate to ignore the rest, then throw if values for the other parts are provided.

I think the issue is that this is our only validity check. Now setServers(['8.8.8.8:53/abc?foo=bar']) would throw. If we only use URL it would be accepted with the path ignored (which is a future bug and semverity issue)


servers.forEach((serv) => {
var ipVersion = isIP(serv);
if (ipVersion !== 0)
return newSet.push([ipVersion, serv]);
return newSet.push([ipVersion, serv, 53]);

const match = serv.match(IPv6RE);
// we have an IPv6 in brackets
if (match) {
ipVersion = isIP(match[1]);
if (ipVersion !== 0)
return newSet.push([ipVersion, match[1]]);
if (ipVersion !== 0) {
const port = parseInt(serv.replace(addrSplitRE, '$2')) || 53;
return newSet.push([ipVersion, match[1], port]);
}
}

const s = serv.split(addrSplitRE)[0];
const [, s, p] = serv.match(addrSplitRE);
ipVersion = isIP(s);

if (ipVersion !== 0)
return newSet.push([ipVersion, s]);
if (ipVersion !== 0) {
return newSet.push([ipVersion, s, parseInt(p)]);
}

throw new Error(`IP address is not properly formatted: ${serv}`);
});
Expand Down
32 changes: 20 additions & 12 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,9 @@ void AresEnsureServers(Environment* env) {
}

ares_channel channel = env->cares_channel();
ares_addr_node* servers = nullptr;
ares_addr_port_node* servers = nullptr;

ares_get_servers(channel, &servers);
ares_get_servers_ports(channel, &servers);

/* if no server or multi-servers, ignore */
if (servers == nullptr) return;
Expand All @@ -456,7 +456,9 @@ void AresEnsureServers(Environment* env) {

/* if the only server is not 127.0.0.1, ignore */
if (servers[0].family != AF_INET ||
servers[0].addr.addr4.s_addr != htonl(INADDR_LOOPBACK)) {
servers[0].addr.addr4.s_addr != htonl(INADDR_LOOPBACK) ||
servers[0].tcp_port != 0 ||
servers[0].udp_port != 0) {
ares_free_data(servers);
env->set_cares_is_servers_default(false);
return;
Expand Down Expand Up @@ -1924,12 +1926,12 @@ void GetServers(const FunctionCallbackInfo<Value>& args) {

Local<Array> server_array = Array::New(env->isolate());

ares_addr_node* servers;
ares_addr_port_node* servers;

int r = ares_get_servers(env->cares_channel(), &servers);
int r = ares_get_servers_ports(env->cares_channel(), &servers);
CHECK_EQ(r, ARES_SUCCESS);

ares_addr_node* cur = servers;
ares_addr_port_node* cur = servers;

for (uint32_t i = 0; cur != nullptr; ++i, cur = cur->next) {
char ip[INET6_ADDRSTRLEN];
Expand All @@ -1938,8 +1940,11 @@ void GetServers(const FunctionCallbackInfo<Value>& args) {
int err = uv_inet_ntop(cur->family, caddr, ip, sizeof(ip));
CHECK_EQ(err, 0);

Local<String> addr = OneByteString(env->isolate(), ip);
server_array->Set(i, addr);
Local<Array> ret = Array::New(env->isolate(), 2);
ret->Set(0, OneByteString(env->isolate(), ip));
ret->Set(1, Integer::New(env->isolate(), cur->udp_port));

server_array->Set(i, ret);
}

ares_free_data(servers);
Expand All @@ -1962,8 +1967,8 @@ void SetServers(const FunctionCallbackInfo<Value>& args) {
return args.GetReturnValue().Set(rv);
}

ares_addr_node* servers = new ares_addr_node[len];
ares_addr_node* last = nullptr;
ares_addr_port_node* servers = new ares_addr_port_node[len];
ares_addr_port_node* last = nullptr;

int err;

Expand All @@ -1974,12 +1979,15 @@ void SetServers(const FunctionCallbackInfo<Value>& args) {

CHECK(elm->Get(0)->Int32Value());
CHECK(elm->Get(1)->IsString());
CHECK(elm->Get(2)->Int32Value());

int fam = elm->Get(0)->Int32Value();
node::Utf8Value ip(env->isolate(), elm->Get(1));
int port = elm->Get(2)->Int32Value();

ares_addr_node* cur = &servers[i];
ares_addr_port_node* cur = &servers[i];

cur->tcp_port = cur->udp_port = port;
switch (fam) {
case 4:
cur->family = AF_INET;
Expand All @@ -2005,7 +2013,7 @@ void SetServers(const FunctionCallbackInfo<Value>& args) {
}

if (err == 0)
err = ares_set_servers(env->cares_channel(), &servers[0]);
err = ares_set_servers_ports(env->cares_channel(), &servers[0]);
else
err = ARES_EBADSTR;

Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ assert.doesNotThrow(() => {
servers[0] = '127.0.0.1';
servers[2] = '0.0.0.0';
dns.setServers(servers);

assert.deepStrictEqual(dns.getServers(), ['127.0.0.1', '0.0.0.0']);
});

assert.doesNotThrow(() => {
Expand All @@ -53,6 +55,11 @@ assert.doesNotThrow(() => {
});

dns.setServers(servers);
assert.deepStrictEqual(dns.getServers(), [
'127.0.0.1',
'192.168.1.1',
'0.0.0.0'
]);
});

const goog = [
Expand All @@ -63,6 +70,8 @@ assert.doesNotThrow(() => dns.setServers(goog));
assert.deepStrictEqual(dns.getServers(), goog);
assert.throws(() => dns.setServers(['foobar']),
/^Error: IP address is not properly formatted: foobar$/);
assert.throws(() => dns.setServers(['127.0.0.1:va']),
/^Error: IP address is not properly formatted: 127\.0\.0\.1:va$/);
assert.deepStrictEqual(dns.getServers(), goog);

const goog6 = [
Expand All @@ -79,10 +88,14 @@ assert.deepStrictEqual(dns.getServers(), goog6);
const ports = [
'4.4.4.4:53',
'[2001:4860:4860::8888]:53',
'103.238.225.181:666',
'[fe80::483a:5aff:fee6:1f04]:666'
];
const portsExpected = [
'4.4.4.4',
'2001:4860:4860::8888',
'103.238.225.181:666',
'[fe80::483a:5aff:fee6:1f04]:666'
];
dns.setServers(ports);
assert.deepStrictEqual(dns.getServers(), portsExpected);
Expand Down