Skip to content

Commit da70161

Browse files
bnoordhuisMylesBorins
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 <jasnell@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io>
1 parent 0bc14b6 commit da70161

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
@@ -212,7 +212,7 @@ will contain an array of IPv4 addresses (e.g.
212212
rather than an array of strings. The TTL is expressed in seconds.
213213
* `callback` {Function} An `(err, result)` callback function.
214214

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

224+
* `hostname` {String} Hostname to resolve.
225+
* `options` {Object}
226+
* `ttl` {Boolean} Retrieve the Time-To-Live value (TTL) of each record.
227+
The callback receives an array of `{ address: '0:1:2:3:4:5:6:7', ttl: 60 }`
228+
objects rather than an array of strings. The TTL is expressed in seconds.
229+
* `callback` {Function} An `(err, result)` callback function.
230+
224231
## dns.resolveCname(hostname, callback)
225232
<!-- YAML
226233
added: v0.3.2

src/cares_wrap.cc

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

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

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

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

455-
this->CallOnComplete(addresses);
464+
CallOnComplete(addresses, ttls);
456465
}
457466
};
458467

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+
const req = dns.resolve6('google.com', { ttl: true }, function(err, result) {
86+
assert.ifError(err);
87+
assert.ok(result.length > 0);
88+
89+
for (let i = 0; i < result.length; i++) {
90+
const 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
const req = dns.resolveMx('gmail.com', function(err, result) {
86107
if (err) throw err;

0 commit comments

Comments
 (0)