Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1bd7936

Browse filesBrowse files
bnoordhuisFishrock123
authored andcommitted
dns: implement {ttl: true} for dns.resolve4()
Add an option to retrieve the Time-To-Live of the A record. PR-URL: #9296 Refs: #5893 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io>
1 parent 15af912 commit 1bd7936
Copy full SHA for 1bd7936

File tree

Expand file treeCollapse file tree

4 files changed

+57
-19
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+57
-19
lines changed
Open diff view settings
Collapse file

‎doc/api/dns.md‎

Copy file name to clipboardExpand all lines: doc/api/dns.md
+8-1Lines changed: 8 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ corresponding lookup methods.
183183
On error, `err` is an [`Error`][] object, where `err.code` is
184184
one of the error codes listed [here](#dns_error_codes).
185185

186-
## dns.resolve4(hostname, callback)
186+
## dns.resolve4(hostname[, options], callback)
187187
<!-- YAML
188188
added: v0.1.16
189189
-->
@@ -193,6 +193,13 @@ Uses the DNS protocol to resolve a IPv4 addresses (`A` records) for the
193193
will contain an array of IPv4 addresses (e.g.
194194
`['74.125.79.104', '74.125.79.105', '74.125.79.106']`).
195195

196+
* `hostname` {String} Hostname to resolve.
197+
* `options` {Object}
198+
* `ttl` {Boolean} Retrieve the Time-To-Live value (TTL) of each record.
199+
The callback receives an array of `{ address: '1.2.3.4', ttl: 60 }` objects
200+
rather than an array of strings. The TTL is expressed in seconds.
201+
* `callback` {Function} An `(err, result)` callback function.
202+
196203
## dns.resolve6(hostname, callback)
197204
<!-- YAML
198205
added: v0.1.16
Collapse file

‎lib/dns.js‎

Copy file name to clipboardExpand all lines: lib/dns.js
+12-2Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ exports.lookupService = function lookupService(host, port, callback) {
211211
};
212212

213213

214-
function onresolve(err, result) {
214+
function onresolve(err, result, ttls) {
215+
if (ttls && this.ttl)
216+
result = result.map((address, index) => ({ address, ttl: ttls[index] }));
217+
215218
if (err)
216219
this.callback(errnoException(err, this.bindingName, this.hostname));
217220
else
@@ -222,7 +225,13 @@ function onresolve(err, result) {
222225
function resolver(bindingName) {
223226
var binding = cares[bindingName];
224227

225-
return function query(name, callback) {
228+
return function query(name, /* options, */ callback) {
229+
var options;
230+
if (arguments.length > 2) {
231+
options = callback;
232+
callback = arguments[2];
233+
}
234+
226235
if (typeof name !== 'string') {
227236
throw new Error('"name" argument must be a string');
228237
} else if (typeof callback !== 'function') {
@@ -235,6 +244,7 @@ function resolver(bindingName) {
235244
req.callback = callback;
236245
req.hostname = name;
237246
req.oncomplete = onresolve;
247+
req.ttl = !!(options && options.ttl);
238248
var err = binding(req, name);
239249
if (err) throw errnoException(err, bindingName);
240250
callback.immediately = true;
Collapse file

‎src/cares_wrap.cc‎

Copy file name to clipboardExpand all lines: src/cares_wrap.cc
+17-16Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -336,25 +336,17 @@ class QueryWrap : public AsyncWrap {
336336
delete wrap;
337337
}
338338

339-
void CallOnComplete(Local<Value> answer) {
340-
HandleScope handle_scope(env()->isolate());
341-
Context::Scope context_scope(env()->context());
342-
Local<Value> argv[] = {
343-
Integer::New(env()->isolate(), 0),
344-
answer
345-
};
346-
MakeCallback(env()->oncomplete_string(), arraysize(argv), argv);
347-
}
348-
349-
void CallOnComplete(Local<Value> answer, Local<Value> family) {
339+
void CallOnComplete(Local<Value> answer,
340+
Local<Value> extra = Local<Value>()) {
350341
HandleScope handle_scope(env()->isolate());
351342
Context::Scope context_scope(env()->context());
352343
Local<Value> argv[] = {
353344
Integer::New(env()->isolate(), 0),
354345
answer,
355-
family
346+
extra
356347
};
357-
MakeCallback(env()->oncomplete_string(), arraysize(argv), argv);
348+
const int argc = arraysize(argv) - extra.IsEmpty();
349+
MakeCallback(env()->oncomplete_string(), argc, argv);
358350
}
359351

360352
void ParseError(int status) {
@@ -400,18 +392,27 @@ class QueryAWrap: public QueryWrap {
400392
HandleScope handle_scope(env()->isolate());
401393
Context::Scope context_scope(env()->context());
402394

403-
struct hostent* host;
395+
hostent* host;
396+
ares_addrttl addrttls[256];
397+
int naddrttls = arraysize(addrttls);
404398

405-
int status = ares_parse_a_reply(buf, len, &host, nullptr, nullptr);
399+
int status = ares_parse_a_reply(buf, len, &host, addrttls, &naddrttls);
406400
if (status != ARES_SUCCESS) {
407401
ParseError(status);
408402
return;
409403
}
410404

411405
Local<Array> addresses = HostentToAddresses(env(), host);
406+
Local<Array> ttls = Array::New(env()->isolate(), naddrttls);
407+
408+
auto context = env()->context();
409+
for (int i = 0; i < naddrttls; i += 1) {
410+
auto value = Integer::New(env()->isolate(), addrttls[i].ttl);
411+
ttls->Set(context, i, value).FromJust();
412+
}
412413
ares_free_hostent(host);
413414

414-
this->CallOnComplete(addresses);
415+
CallOnComplete(addresses, ttls);
415416
}
416417
};
417418

Collapse file

‎test/internet/test-dns.js‎

Copy file name to clipboardExpand all lines: test/internet/test-dns.js
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ TEST(function test_reverse_bogus(done) {
6060
done();
6161
});
6262

63+
TEST(function test_resolve4_ttl(done) {
64+
var req = dns.resolve4('google.com', { ttl: true }, function(err, result) {
65+
assert.ifError(err);
66+
assert.ok(result.length > 0);
67+
68+
for (var i = 0; i < result.length; i++) {
69+
var item = result[i];
70+
assert.ok(item);
71+
assert.strictEqual(typeof item, 'object');
72+
assert.strictEqual(typeof item.ttl, 'number');
73+
assert.strictEqual(typeof item.address, 'string');
74+
assert.ok(item.ttl > 0);
75+
assert.ok(isIPv4(item.address));
76+
}
77+
78+
done();
79+
});
80+
81+
checkWrap(req);
82+
});
6383

6484
TEST(function test_resolveMx(done) {
6585
var req = dns.resolveMx('gmail.com', function(err, result) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.