Skip to content

Commit 0a3e5cc

Browse files
bnoordhuisFishrock123
authored andcommitted
dns: implement {ttl: true} for dns.resolve6()
Add an option to retrieve the Time-To-Live of the AAAA record. PR-URL: #9296 Refs: #5893 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Roman Reiss <[email protected]>
1 parent 1bd7936 commit 0a3e5cc

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

doc/api/dns.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ will contain an array of IPv4 addresses (e.g.
200200
rather than an array of strings. The TTL is expressed in seconds.
201201
* `callback` {Function} An `(err, result)` callback function.
202202

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

212+
* `hostname` {String} Hostname to resolve.
213+
* `options` {Object}
214+
* `ttl` {Boolean} Retrieve the Time-To-Live value (TTL) of each record.
215+
The callback receives an array of `{ address: '0:1:2:3:4:5:6:7', ttl: 60 }`
216+
objects rather than an array of strings. The TTL is expressed in seconds.
217+
* `callback` {Function} An `(err, result)` callback function.
218+
212219
## dns.resolveCname(hostname, callback)
213220
<!-- YAML
214221
added: v0.3.2

src/cares_wrap.cc

+12-3
Original file line numberDiff line numberDiff line change
@@ -440,18 +440,27 @@ class QueryAaaaWrap: public QueryWrap {
440440
HandleScope handle_scope(env()->isolate());
441441
Context::Scope context_scope(env()->context());
442442

443-
struct hostent* host;
443+
hostent* host;
444+
ares_addr6ttl addrttls[256];
445+
int naddrttls = arraysize(addrttls);
444446

445-
int status = ares_parse_aaaa_reply(buf, len, &host, nullptr, nullptr);
447+
int status = ares_parse_aaaa_reply(buf, len, &host, addrttls, &naddrttls);
446448
if (status != ARES_SUCCESS) {
447449
ParseError(status);
448450
return;
449451
}
450452

451453
Local<Array> addresses = HostentToAddresses(env(), host);
454+
Local<Array> ttls = Array::New(env()->isolate(), naddrttls);
455+
456+
auto context = env()->context();
457+
for (int i = 0; i < naddrttls; i += 1) {
458+
auto value = Integer::New(env()->isolate(), addrttls[i].ttl);
459+
ttls->Set(context, i, value).FromJust();
460+
}
452461
ares_free_hostent(host);
453462

454-
this->CallOnComplete(addresses);
463+
CallOnComplete(addresses, ttls);
455464
}
456465
};
457466

test/internet/test-dns.js

+21
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,27 @@ TEST(function test_resolve4_ttl(done) {
8181
checkWrap(req);
8282
});
8383

84+
TEST(function test_resolve6_ttl(done) {
85+
var req = dns.resolve6('google.com', { ttl: true }, function(err, result) {
86+
assert.ifError(err);
87+
assert.ok(result.length > 0);
88+
89+
for (var i = 0; i < result.length; i++) {
90+
var item = result[i];
91+
assert.ok(item);
92+
assert.strictEqual(typeof item, 'object');
93+
assert.strictEqual(typeof item.ttl, 'number');
94+
assert.strictEqual(typeof item.address, 'string');
95+
assert.ok(item.ttl > 0);
96+
assert.ok(isIPv6(item.address));
97+
}
98+
99+
done();
100+
});
101+
102+
checkWrap(req);
103+
});
104+
84105
TEST(function test_resolveMx(done) {
85106
var req = dns.resolveMx('gmail.com', function(err, result) {
86107
if (err) throw err;

0 commit comments

Comments
 (0)