Skip to content

Commit

Permalink
Squashing multiple commits to ease rebase
Browse files Browse the repository at this point in the history
dns: copy over non-cpp changes

src: introduce BaseWrap, and refactor QueryWrap
to support SearchWrap, which shares much of the functionality
with QueryWrap.

src: cares_wrap formatted

doc: style nit for dns

test: undo unnecessary changes for test-dns.js

test: add dns tests for cares_search

Revert "src: introduce BaseWrap, and refactor QueryWrap"

This reverts commit f8c96e04d894d4af78feaa254373dcaa36990422.

src: introduce a BaseWrap class

- refactor `QueryWrap` and introduce `SearchWrap` classes, to
  use `BaseWrap` class.

doc: update yaml changes for dns.md

dns: pass whether search option is set

src: parse whether search option is set, in cares

remove class `SearchWrap`, and instead, parse within callsite
`Query` itself, whether `ares_query` or `ares_search` needs
to be used.

src: remove BaseWrap indirection

Since we do not need `SearchWrap` class anymore, we wouldn't need
a `BaseWrap` either. Fix linter issues as well.

dns: do not attach search to req object
  • Loading branch information
SirR4T committed Sep 17, 2018
1 parent 4286dcf commit b6a828e
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 53 deletions.
14 changes: 14 additions & 0 deletions doc/api/dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ On error, `err` is an [`Error`][] object, where `err.code` is one of the
<!-- YAML
added: v0.1.16
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/22226
description: The `options.search` parameter is now supported.
- version: v7.2.0
pr-url: https://github.com/nodejs/node/pull/9296
description: This method now supports passing `options`,
Expand All @@ -287,6 +290,10 @@ 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 hostname using the local domain name or the
search list for host-name lookup.
When `true`, the resolver will use the search list, which can create extra
DNS queries. **Default:** `false`.
* `callback` {Function}
- `err` {Error}
- `addresses` {string[] | Object[]}
Expand All @@ -300,6 +307,9 @@ will contain an array of IPv4 addresses (e.g.
<!-- YAML
added: v0.1.16
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/22226
description: The `options.search` parameter is now supported.
- version: v7.2.0
pr-url: https://github.com/nodejs/node/pull/9296
description: This method now supports passing `options`,
Expand All @@ -311,6 +321,10 @@ 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 hostname using the local domain name or the
search list for host-name lookup.
When `true`, the resolver will use the search list, which can create extra
DNS queries. **Default:** `false`.
* `callback` {Function}
- `err` {Error}
- `addresses` {string[] | Object[]}
Expand Down
3 changes: 2 additions & 1 deletion lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ function resolver(bindingName) {
req.hostname = name;
req.oncomplete = onresolve;
req.ttl = !!(options && options.ttl);
var err = this._handle[bindingName](req, name);
const search = !!(options && options.search);
var err = this._handle[bindingName](req, name, search);
if (err) throw dnsException(err, bindingName, name);
return req;
}
Expand Down
8 changes: 5 additions & 3 deletions lib/internal/dns/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function onresolve(err, result, ttls) {
this.resolve(result);
}

function createResolverPromise(resolver, bindingName, hostname, ttl) {
function createResolverPromise(resolver, bindingName, hostname, ttl, isSearch) {
return new Promise((resolve, reject) => {
const req = new QueryReqWrap();

Expand All @@ -181,8 +181,9 @@ function createResolverPromise(resolver, bindingName, hostname, ttl) {
req.resolve = resolve;
req.reject = reject;
req.ttl = ttl;
req.search = isSearch;

const err = resolver._handle[bindingName](req, hostname);
const err = resolver._handle[bindingName](req, hostname, isSearch);

if (err)
reject(dnsException(err, bindingName, hostname));
Expand All @@ -196,7 +197,8 @@ function resolver(bindingName) {
}

const ttl = !!(options && options.ttl);
return createResolverPromise(this, bindingName, name, ttl);
const isSearch = !!(options && options.search);
return createResolverPromise(this, bindingName, name, ttl, isSearch);
}

Object.defineProperty(query, 'name', { value: bindingName });
Expand Down
118 changes: 71 additions & 47 deletions src/cares_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -594,22 +594,28 @@ void ChannelWrap::EnsureServers() {
Setup();
}

typedef void ares_function(ares_channel channel,
const char* name,
int dnsclass,
int type,
ares_callback callback,
void* arg);

class QueryWrap : public AsyncWrap {
public:
QueryWrap(ChannelWrap* channel, Local<Object> req_wrap_obj, const char* name)
QueryWrap(ChannelWrap* channel, Local<Object> req_wrap_obj,
const char* name, ares_function* fn)
: AsyncWrap(channel->env(), req_wrap_obj, AsyncWrap::PROVIDER_QUERYWRAP),
channel_(channel),
trace_name_(name) {
trace_name_(name),
function_(fn) {
// Make sure the channel object stays alive during the query lifetime.
req_wrap_obj->Set(env()->context(),
env()->channel_string(),
channel->object()).FromJust();
}

~QueryWrap() override {
CHECK_EQ(false, persistent().IsEmpty());
}
~QueryWrap() override { CHECK_EQ(false, persistent().IsEmpty()); }

// Subclasses should implement the appropriate Send method.
virtual int Send(const char* name) {
Expand All @@ -630,8 +636,12 @@ class QueryWrap : public AsyncWrap {
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(
TRACING_CATEGORY_NODE2(dns, native), trace_name_, this,
"name", TRACE_STR_COPY(name));
ares_query(channel_->cares_channel(), name, dnsclass, type, Callback,
static_cast<void*>(this));
function_(channel_->cares_channel(),
name,
dnsclass,
type,
Callback,
static_cast<void*>(this));
}

static void CaresAsyncClose(uv_async_t* async) {
Expand Down Expand Up @@ -757,9 +767,9 @@ class QueryWrap : public AsyncWrap {

private:
const char* trace_name_;
ares_function* function_;
};


template <typename T>
Local<Array> AddrTTLToArray(Environment* env,
const T* addrttls,
Expand Down Expand Up @@ -1186,9 +1196,10 @@ int ParseSoaReply(Environment* env,

class QueryAnyWrap: public QueryWrap {
public:
QueryAnyWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
: QueryWrap(channel, req_wrap_obj, "resolveAny") {
}
QueryAnyWrap(ChannelWrap* channel,
Local<Object> req_wrap_obj,
ares_function* fn)
: QueryWrap(channel, req_wrap_obj, "resolveAny", fn) {}

int Send(const char* name) override {
AresQuery(name, ns_c_in, ns_t_any);
Expand Down Expand Up @@ -1364,12 +1375,12 @@ class QueryAnyWrap: public QueryWrap {
}
};


class QueryAWrap: public QueryWrap {
public:
QueryAWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
: QueryWrap(channel, req_wrap_obj, "resolve4") {
}
QueryAWrap(ChannelWrap* channel,
Local<Object> req_wrap_obj,
ares_function* fn)
: QueryWrap(channel, req_wrap_obj, "resolve4", fn) {}

int Send(const char* name) override {
AresQuery(name, ns_c_in, ns_t_a);
Expand Down Expand Up @@ -1415,9 +1426,10 @@ class QueryAWrap: public QueryWrap {

class QueryAaaaWrap: public QueryWrap {
public:
QueryAaaaWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
: QueryWrap(channel, req_wrap_obj, "resolve6") {
}
QueryAaaaWrap(ChannelWrap* channel,
Local<Object> req_wrap_obj,
ares_function* fn)
: QueryWrap(channel, req_wrap_obj, "resolve6", fn) {}

int Send(const char* name) override {
AresQuery(name, ns_c_in, ns_t_aaaa);
Expand Down Expand Up @@ -1460,12 +1472,12 @@ class QueryAaaaWrap: public QueryWrap {
}
};


class QueryCnameWrap: public QueryWrap {
public:
QueryCnameWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
: QueryWrap(channel, req_wrap_obj, "resolveCname") {
}
QueryCnameWrap(ChannelWrap* channel,
Local<Object> req_wrap_obj,
ares_function* fn)
: QueryWrap(channel, req_wrap_obj, "resolveCname", fn) {}

int Send(const char* name) override {
AresQuery(name, ns_c_in, ns_t_cname);
Expand Down Expand Up @@ -1498,9 +1510,10 @@ class QueryCnameWrap: public QueryWrap {

class QueryMxWrap: public QueryWrap {
public:
QueryMxWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
: QueryWrap(channel, req_wrap_obj, "resolveMx") {
}
QueryMxWrap(ChannelWrap* channel,
Local<Object> req_wrap_obj,
ares_function* fn)
: QueryWrap(channel, req_wrap_obj, "resolveMx", fn) {}

int Send(const char* name) override {
AresQuery(name, ns_c_in, ns_t_mx);
Expand Down Expand Up @@ -1533,9 +1546,10 @@ class QueryMxWrap: public QueryWrap {

class QueryNsWrap: public QueryWrap {
public:
QueryNsWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
: QueryWrap(channel, req_wrap_obj, "resolveNs") {
}
QueryNsWrap(ChannelWrap* channel,
Local<Object> req_wrap_obj,
ares_function* fn)
: QueryWrap(channel, req_wrap_obj, "resolveNs", fn) {}

int Send(const char* name) override {
AresQuery(name, ns_c_in, ns_t_ns);
Expand Down Expand Up @@ -1568,9 +1582,10 @@ class QueryNsWrap: public QueryWrap {

class QueryTxtWrap: public QueryWrap {
public:
QueryTxtWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
: QueryWrap(channel, req_wrap_obj, "resolveTxt") {
}
QueryTxtWrap(ChannelWrap* channel,
Local<Object> req_wrap_obj,
ares_function* fn)
: QueryWrap(channel, req_wrap_obj, "resolveTxt", fn) {}

int Send(const char* name) override {
AresQuery(name, ns_c_in, ns_t_txt);
Expand Down Expand Up @@ -1602,9 +1617,10 @@ class QueryTxtWrap: public QueryWrap {

class QuerySrvWrap: public QueryWrap {
public:
explicit QuerySrvWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
: QueryWrap(channel, req_wrap_obj, "resolveSrv") {
}
explicit QuerySrvWrap(ChannelWrap* channel,
Local<Object> req_wrap_obj,
ares_function* fn)
: QueryWrap(channel, req_wrap_obj, "resolveSrv", fn) {}

int Send(const char* name) override {
AresQuery(name, ns_c_in, ns_t_srv);
Expand Down Expand Up @@ -1635,9 +1651,10 @@ class QuerySrvWrap: public QueryWrap {

class QueryPtrWrap: public QueryWrap {
public:
explicit QueryPtrWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
: QueryWrap(channel, req_wrap_obj, "resolvePtr") {
}
explicit QueryPtrWrap(ChannelWrap* channel,
Local<Object> req_wrap_obj,
ares_function* fn)
: QueryWrap(channel, req_wrap_obj, "resolvePtr", fn) {}

int Send(const char* name) override {
AresQuery(name, ns_c_in, ns_t_ptr);
Expand Down Expand Up @@ -1670,9 +1687,10 @@ class QueryPtrWrap: public QueryWrap {

class QueryNaptrWrap: public QueryWrap {
public:
explicit QueryNaptrWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
: QueryWrap(channel, req_wrap_obj, "resolveNaptr") {
}
explicit QueryNaptrWrap(ChannelWrap* channel,
Local<Object> req_wrap_obj,
ares_function* fn)
: QueryWrap(channel, req_wrap_obj, "resolveNaptr", fn) {}

int Send(const char* name) override {
AresQuery(name, ns_c_in, ns_t_naptr);
Expand Down Expand Up @@ -1704,9 +1722,10 @@ class QueryNaptrWrap: public QueryWrap {

class QuerySoaWrap: public QueryWrap {
public:
QuerySoaWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
: QueryWrap(channel, req_wrap_obj, "resolveSoa") {
}
QuerySoaWrap(ChannelWrap* channel,
Local<Object> req_wrap_obj,
ares_function* fn)
: QueryWrap(channel, req_wrap_obj, "resolveSoa", fn) {}

int Send(const char* name) override {
AresQuery(name, ns_c_in, ns_t_soa);
Expand Down Expand Up @@ -1769,9 +1788,10 @@ class QuerySoaWrap: public QueryWrap {

class GetHostByAddrWrap: public QueryWrap {
public:
explicit GetHostByAddrWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
: QueryWrap(channel, req_wrap_obj, "reverse") {
}
explicit GetHostByAddrWrap(ChannelWrap* channel,
Local<Object> req_wrap_obj,
ares_function* fn)
: QueryWrap(channel, req_wrap_obj, "reverse", fn) {}

int Send(const char* name) override {
int length, family;
Expand Down Expand Up @@ -1825,10 +1845,14 @@ static void Query(const FunctionCallbackInfo<Value>& args) {
CHECK_EQ(false, args.IsConstructCall());
CHECK(args[0]->IsObject());
CHECK(args[1]->IsString());
CHECK(args[2]->IsBoolean());

Local<Object> req_wrap_obj = args[0].As<Object>();
Local<String> string = args[1].As<String>();
Wrap* wrap = new Wrap(channel, req_wrap_obj);
bool is_search = args[2]->IsTrue();
ares_function* ares_fn_ = ares_query;
if (is_search) ares_fn_ = ares_search;
Wrap* wrap = new Wrap(channel, req_wrap_obj, ares_fn_);

node::Utf8Value name(env->isolate(), string);
channel->ModifyActivityQueryCount(1);
Expand Down
32 changes: 30 additions & 2 deletions test/internet/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ TEST(async function test_resolve4_ttl(done) {
ttl: true
}));

const req = dns.resolve4(addresses.INET4_HOST, {
validateResult(await dnsPromises.resolve4(addresses.INET4_HOST, {
ttl: true, search: true
}));

let req = dns.resolve4(addresses.INET4_HOST, {
ttl: true
}, function(err, result) {
assert.ifError(err);
Expand All @@ -107,6 +111,16 @@ TEST(async function test_resolve4_ttl(done) {
});

checkWrap(req);

req = dns.resolve4(addresses.INET4_HOST, {
ttl: true, search: true
}, function(err, result) {
assert.ifError(err);
validateResult(result);
done();
});

checkWrap(req);
});

TEST(async function test_resolve6_ttl(done) {
Expand All @@ -128,7 +142,11 @@ TEST(async function test_resolve6_ttl(done) {
ttl: true
}));

const req = dns.resolve6(addresses.INET6_HOST, {
validateResult(await dnsPromises.resolve6(addresses.INET6_HOST, {
ttl: true, search: true
}));

let req = dns.resolve6(addresses.INET6_HOST, {
ttl: true
}, function(err, result) {
assert.ifError(err);
Expand All @@ -137,6 +155,16 @@ TEST(async function test_resolve6_ttl(done) {
});

checkWrap(req);

req = dns.resolve6(addresses.INET6_HOST, {
ttl: true, search: true
}, function(err, result) {
assert.ifError(err);
validateResult(result);
done();
});

checkWrap(req);
});

TEST(async function test_resolveMx(done) {
Expand Down

0 comments on commit b6a828e

Please sign in to comment.