Browse Source

dns: implement {ttl: true} for dns.resolve6()

Add an option to retrieve the Time-To-Live of the AAAA record.

PR-URL: https://github.com/nodejs/node/pull/9296
Refs: https://github.com/nodejs/node/issues/5893
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Roman Reiss <me@silverwind.io>
v6
Ben Noordhuis 8 years ago
parent
commit
6b1a8d03c7
  1. 9
      doc/api/dns.md
  2. 15
      src/cares_wrap.cc
  3. 21
      test/internet/test-dns.js

9
doc/api/dns.md

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

15
src/cares_wrap.cc

@ -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);
}
};

21
test/internet/test-dns.js

@ -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;

Loading…
Cancel
Save