Skip to content

Commit

Permalink
dns: implement {ttl: true} for dns.resolve6()
Browse files Browse the repository at this point in the history
Add an option to retrieve the Time-To-Live of the AAAA record.

PR-URL: #9296
Refs: #5893
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
  • Loading branch information
bnoordhuis authored and Fishrock123 committed Nov 22, 2016
1 parent 1bd7936 commit 0a3e5cc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
9 changes: 8 additions & 1 deletion doc/api/dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ will contain an array of IPv4 addresses (e.g.
rather than an array of strings. The TTL is expressed in seconds.
* `callback` {Function} An `(err, result)` callback function.

## dns.resolve6(hostname, callback)
## dns.resolve6(hostname[, options], callback)
<!-- YAML
added: v0.1.16
-->
Expand All @@ -209,6 +209,13 @@ Uses the DNS protocol to resolve a IPv6 addresses (`AAAA` records) for the
`hostname`. The `addresses` argument passed to the `callback` function
will contain an array of IPv6 addresses.

* `hostname` {String} Hostname to resolve.
* `options` {Object}
* `ttl` {Boolean} Retrieve the Time-To-Live value (TTL) of each record.
The callback receives an array of `{ address: '0:1:2:3:4:5:6:7', ttl: 60 }`
objects rather than an array of strings. The TTL is expressed in seconds.
* `callback` {Function} An `(err, result)` callback function.

## dns.resolveCname(hostname, callback)
<!-- YAML
added: v0.3.2
Expand Down
15 changes: 12 additions & 3 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -440,18 +440,27 @@ class QueryAaaaWrap: public QueryWrap {
HandleScope handle_scope(env()->isolate());
Context::Scope context_scope(env()->context());

struct hostent* host;
hostent* host;
ares_addr6ttl addrttls[256];
int naddrttls = arraysize(addrttls);

int status = ares_parse_aaaa_reply(buf, len, &host, nullptr, nullptr);
int status = ares_parse_aaaa_reply(buf, len, &host, addrttls, &naddrttls);
if (status != ARES_SUCCESS) {
ParseError(status);
return;
}

Local<Array> addresses = HostentToAddresses(env(), host);
Local<Array> ttls = Array::New(env()->isolate(), naddrttls);

auto context = env()->context();
for (int i = 0; i < naddrttls; i += 1) {
auto value = Integer::New(env()->isolate(), addrttls[i].ttl);
ttls->Set(context, i, value).FromJust();
}
ares_free_hostent(host);

this->CallOnComplete(addresses);
CallOnComplete(addresses, ttls);
}
};

Expand Down
21 changes: 21 additions & 0 deletions test/internet/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@ TEST(function test_resolve4_ttl(done) {
checkWrap(req);
});

TEST(function test_resolve6_ttl(done) {
var req = dns.resolve6('google.com', { ttl: true }, function(err, result) {
assert.ifError(err);
assert.ok(result.length > 0);

for (var i = 0; i < result.length; i++) {
var item = result[i];
assert.ok(item);
assert.strictEqual(typeof item, 'object');
assert.strictEqual(typeof item.ttl, 'number');
assert.strictEqual(typeof item.address, 'string');
assert.ok(item.ttl > 0);
assert.ok(isIPv6(item.address));
}

done();
});

checkWrap(req);
});

TEST(function test_resolveMx(done) {
var req = dns.resolveMx('gmail.com', function(err, result) {
if (err) throw err;
Expand Down

0 comments on commit 0a3e5cc

Please sign in to comment.