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 2 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
32 changes: 27 additions & 5 deletions doc/api/dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,20 @@ 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 and port strings that are being used for name
Copy link
Contributor

Choose a reason for hiding this comment

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

🚲 🎪 Suggestion:

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.
...

[rfc3986]: https://tools.ietf.org/html/rfc3986#section-3.2.2

resolution, splited with `:`. If a server uses a default DNS server port, the
port part will be ignored in the result.

eg.

```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 @@ -484,10 +496,20 @@ added: v0.11.3
-->
- `servers` {string[]}
Copy link
Contributor

Choose a reason for hiding this comment

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

- `servers` {string[]} array of [rfc3986][] formatted addresses


Sets the IP addresses of the servers to be used when resolving. The `servers`
argument is an array of IPv4 or IPv6 addresses.
Sets the IP addresses and port of the servers to be used when resolving. The
Copy link
Contributor

Choose a reason for hiding this comment

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

Sets the IP address and port of servers to be used when performing DNS
resolution. If the port is the IANA default DNS port (53) it can be omitted.

`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.

If a port is specified on the address, it will be removed.
eg.

```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
27 changes: 20 additions & 7 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 ipVersion = isIP(val[0]);
return ipVersion === 6 ? `[${val[0]}]:${val[1]}` : `${val[0]}:${val[1]}`;
Copy link
Contributor

Choose a reason for hiding this comment

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

Personal preference nit:

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

});
}


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

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 portMatch = serv.match(addrSplitRE) || [];
Copy link
Contributor

Choose a reason for hiding this comment

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

personal style nit:

const addrSplitRE = /(^.+?)(?::(\d+))?$/;
const port = parseInt(serv.replace(addrSplitRE, '$2')) || 53;
return newSet.push([ipVersion, match[1], port);

return newSet.push([ipVersion, match[1], parseInt(portMatch[1] || 53)]);
}
}

const s = serv.split(addrSplitRE)[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

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(serv.substr(serv.indexOf(':') + 1))
]);
}

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
11 changes: 11 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 @@ -79,10 +86,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