Skip to content

Commit

Permalink
Add Support for resolving DNS using search list for host-name loopkup.
Browse files Browse the repository at this point in the history
lib: dns.js - passing the search field form the options object to
              QueryReqWrap object.

src:
base_object.h - added GetField function to read fields from the
                persistent object (persistent QueryReqWrap object).

cares_wrap.cc - added a AresSearch function.
              - QueryAWrap (resolve4) and QueryAaaaWrap (resolve6)
              utilizes the AresSearch when options.search is true.

doc/api: added search option to the resolve4 and resolve6 documentation

Fixes: nodejs#17850
  • Loading branch information
Manicqin committed Mar 23, 2018
1 parent 9b34ea6 commit 13e9b38
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
6 changes: 6 additions & 0 deletions doc/api/dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ changes:
When `true`, the callback receives an array of
`{ address: '1.2.3.4', ttl: 60 }` objects rather than an array of strings,
with the TTL expressed in seconds.
- `search` {boolean} Resolve the DNS using the local domain name or the search
list for host-name lookup.
When `true`, the resolve will use the search list which can create extra dns queries.
- `callback` {Function}
- `err` {Error}
- `addresses` {string[] | Object[]}
Expand All @@ -310,6 +313,9 @@ changes:
When `true`, the callback receives an array of
`{ address: '0:1:2:3:4:5:6:7', ttl: 60 }` objects rather than an array of
strings, with the TTL expressed in seconds.
- `search` {boolean} Resolve the DNS using the local domain name or the search
list for host-name lookup.
When `true`, the resolve will use the search list which can create extra dns queries.
- `callback` {Function}
- `err` {Error}
- `addresses` {string[] | Object[]}
Expand Down
1 change: 1 addition & 0 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ function resolver(bindingName) {
req.hostname = name;
req.oncomplete = onresolve;
req.ttl = !!(options && options.ttl);
req.search = !!(options && options.search);
var err = this._handle[bindingName](req, name);
if (err) throw dnsException(err, bindingName);
return req;
Expand Down
9 changes: 9 additions & 0 deletions src/base_object-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ inline v8::Local<v8::Object> BaseObject::object() {
}


inline v8::MaybeLocal<v8::Value> BaseObject::GetField(
std::string const& field) {
return object()->Get(env()->context(),
v8::String::NewFromUtf8(env()->isolate(),
field.c_str(),
v8::NewStringType::kNormal).ToLocalChecked());
}


inline Environment* BaseObject::env() const {
return env_;
}
Expand Down
2 changes: 2 additions & 0 deletions src/base_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class BaseObject {
// persistent.IsEmpty() is true.
inline v8::Local<v8::Object> object();

inline v8::MaybeLocal<v8::Value> GetField(std::string const& field);

inline Persistent<v8::Object>& persistent();

inline Environment* env() const;
Expand Down
38 changes: 36 additions & 2 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,14 @@ class QueryWrap : public AsyncWrap {
static_cast<void*>(this));
}

void AresSearch(const char* name,
int dnsclass,
int type) {
channel_->EnsureServers();
ares_search(channel_->cares_channel(), name, dnsclass, type, Callback,
static_cast<void*>(this));
}

static void CaresAsyncClose(uv_handle_t* handle) {
uv_async_t* async = reinterpret_cast<uv_async_t*>(handle);
auto data = static_cast<struct CaresAsyncData*>(async->data);
Expand Down Expand Up @@ -1360,7 +1368,20 @@ class QueryAWrap: public QueryWrap {
}

int Send(const char* name) override {
AresQuery(name, ns_c_in, ns_t_a);
auto prop_value = GetField("search");
bool bSearch = false;

if (!prop_value.IsEmpty()) {
auto search_prop = prop_value.ToLocalChecked()->ToBoolean();
bSearch = search_prop->Value();
}

if (bSearch) {
AresSearch(name, ns_c_in, ns_t_a);
} else {
AresQuery(name, ns_c_in, ns_t_a);
}

return 0;
}

Expand Down Expand Up @@ -1404,7 +1425,20 @@ class QueryAaaaWrap: public QueryWrap {
}

int Send(const char* name) override {
AresQuery(name, ns_c_in, ns_t_aaaa);
auto prop_value = GetField("search");
bool bSearch = false;

if (!prop_value.IsEmpty()) {
auto search_prop = prop_value.ToLocalChecked()->ToBoolean();
bSearch = search_prop->Value();
}

if (bSearch) {
AresSearch(name, ns_c_in, ns_t_a);
} else {
AresQuery(name, ns_c_in, ns_t_a);
}

return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions test/internet/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ TEST(function test_reverse_bogus(done) {

TEST(function test_resolve4_ttl(done) {
const req = dns.resolve4(addresses.INET4_HOST, {
ttl: true
ttl: true, search: false
}, function(err, result) {
assert.ifError(err);
assert.ok(result.length > 0);
Expand All @@ -99,7 +99,7 @@ TEST(function test_resolve4_ttl(done) {

TEST(function test_resolve6_ttl(done) {
const req = dns.resolve6(addresses.INET6_HOST, {
ttl: true
ttl: true, search: false
}, function(err, result) {
assert.ifError(err);
assert.ok(result.length > 0);
Expand Down

0 comments on commit 13e9b38

Please sign in to comment.