Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
update after reviewing
  • Loading branch information
XadillaX committed Jun 16, 2017
commit 41da7fac40e932bc9a4c95df9fd5f4df797c5e4a
16 changes: 9 additions & 7 deletions doc/api/dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ the [Implementation considerations section][] for more information.
added: v0.11.3
-->

Returns an array of IP address and port strings that are being used for name
resolution, splited with `:`. If a server uses a default DNS server port, the
port part will be ignored in the result.
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',
Expand Down Expand Up @@ -494,11 +495,11 @@ one of the [DNS error codes][].
<!-- YAML
added: v0.11.3
-->
- `servers` {string[]}
- `servers` {string[]} array of [rfc3986][] formatted addresses

Sets the IP addresses and port of the servers to be used when resolving. The
`servers` argument is an array of IPv4 or IPv6 addresses. If the port of a
server is the default DNS server port, this part can be ignored in the string.
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.

eg.

Expand Down Expand Up @@ -605,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
18 changes: 7 additions & 11 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ function getServers() {
return ret.map((val) => {
if (!val[1] || val[1] === 53) return val[0];
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 ipVersion = isIP(val[0]);
return ipVersion === 6 ? `[${val[0]}]:${val[1]}` : `${val[0]}:${val[1]}`;
const host = isIP(val[0]) === 6 ? `[${val[0]}]` : val[0];
return `${host}:${val[1]}`;
});
}

Expand All @@ -317,7 +317,7 @@ function setServers(servers) {
const orig = cares.getServers();
const newSet = [];
const IPv6RE = /\[(.*)\]/;
const addrSplitRE = /:(\d+)$/;
const addrSplitRE = /(^.+?)(?::(\d+))?$/;
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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);
Expand All @@ -329,20 +329,16 @@ function setServers(servers) {
if (match) {
ipVersion = isIP(match[1]);
if (ipVersion !== 0) {
const portMatch = serv.match(addrSplitRE) || [];
return newSet.push([ipVersion, match[1], parseInt(portMatch[1] || 53)]);
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,
parseInt(serv.substr(serv.indexOf(':') + 1))
]);
return newSet.push([ipVersion, s, parseInt(p)]);
}

throw new Error(`IP address is not properly formatted: ${serv}`);
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,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 Down