Skip to content

Commit b6a828e

Browse files
Sarat AddepalliSirR4T
authored andcommitted
Squashing multiple commits to ease rebase
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
1 parent 4286dcf commit b6a828e

File tree

5 files changed

+122
-53
lines changed

5 files changed

+122
-53
lines changed

doc/api/dns.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ On error, `err` is an [`Error`][] object, where `err.code` is one of the
276276
<!-- YAML
277277
added: v0.1.16
278278
changes:
279+
- version: REPLACEME
280+
pr-url: https://github.com/nodejs/node/pull/22226
281+
description: The `options.search` parameter is now supported.
279282
- version: v7.2.0
280283
pr-url: https://github.com/nodejs/node/pull/9296
281284
description: This method now supports passing `options`,
@@ -287,6 +290,10 @@ changes:
287290
When `true`, the callback receives an array of
288291
`{ address: '1.2.3.4', ttl: 60 }` objects rather than an array of strings,
289292
with the TTL expressed in seconds.
293+
- `search` {boolean} Resolve the hostname using the local domain name or the
294+
search list for host-name lookup.
295+
When `true`, the resolver will use the search list, which can create extra
296+
DNS queries. **Default:** `false`.
290297
* `callback` {Function}
291298
- `err` {Error}
292299
- `addresses` {string[] | Object[]}
@@ -300,6 +307,9 @@ will contain an array of IPv4 addresses (e.g.
300307
<!-- YAML
301308
added: v0.1.16
302309
changes:
310+
- version: REPLACEME
311+
pr-url: https://github.com/nodejs/node/pull/22226
312+
description: The `options.search` parameter is now supported.
303313
- version: v7.2.0
304314
pr-url: https://github.com/nodejs/node/pull/9296
305315
description: This method now supports passing `options`,
@@ -311,6 +321,10 @@ changes:
311321
When `true`, the callback receives an array of
312322
`{ address: '0:1:2:3:4:5:6:7', ttl: 60 }` objects rather than an array of
313323
strings, with the TTL expressed in seconds.
324+
- `search` {boolean} Resolve the hostname using the local domain name or the
325+
search list for host-name lookup.
326+
When `true`, the resolver will use the search list, which can create extra
327+
DNS queries. **Default:** `false`.
314328
* `callback` {Function}
315329
- `err` {Error}
316330
- `addresses` {string[] | Object[]}

lib/dns.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ function resolver(bindingName) {
219219
req.hostname = name;
220220
req.oncomplete = onresolve;
221221
req.ttl = !!(options && options.ttl);
222-
var err = this._handle[bindingName](req, name);
222+
const search = !!(options && options.search);
223+
var err = this._handle[bindingName](req, name, search);
223224
if (err) throw dnsException(err, bindingName, name);
224225
return req;
225226
}

lib/internal/dns/promises.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ function onresolve(err, result, ttls) {
171171
this.resolve(result);
172172
}
173173

174-
function createResolverPromise(resolver, bindingName, hostname, ttl) {
174+
function createResolverPromise(resolver, bindingName, hostname, ttl, isSearch) {
175175
return new Promise((resolve, reject) => {
176176
const req = new QueryReqWrap();
177177

@@ -181,8 +181,9 @@ function createResolverPromise(resolver, bindingName, hostname, ttl) {
181181
req.resolve = resolve;
182182
req.reject = reject;
183183
req.ttl = ttl;
184+
req.search = isSearch;
184185

185-
const err = resolver._handle[bindingName](req, hostname);
186+
const err = resolver._handle[bindingName](req, hostname, isSearch);
186187

187188
if (err)
188189
reject(dnsException(err, bindingName, hostname));
@@ -196,7 +197,8 @@ function resolver(bindingName) {
196197
}
197198

198199
const ttl = !!(options && options.ttl);
199-
return createResolverPromise(this, bindingName, name, ttl);
200+
const isSearch = !!(options && options.search);
201+
return createResolverPromise(this, bindingName, name, ttl, isSearch);
200202
}
201203

202204
Object.defineProperty(query, 'name', { value: bindingName });

src/cares_wrap.cc

Lines changed: 71 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -594,22 +594,28 @@ void ChannelWrap::EnsureServers() {
594594
Setup();
595595
}
596596

597+
typedef void ares_function(ares_channel channel,
598+
const char* name,
599+
int dnsclass,
600+
int type,
601+
ares_callback callback,
602+
void* arg);
597603

598604
class QueryWrap : public AsyncWrap {
599605
public:
600-
QueryWrap(ChannelWrap* channel, Local<Object> req_wrap_obj, const char* name)
606+
QueryWrap(ChannelWrap* channel, Local<Object> req_wrap_obj,
607+
const char* name, ares_function* fn)
601608
: AsyncWrap(channel->env(), req_wrap_obj, AsyncWrap::PROVIDER_QUERYWRAP),
602609
channel_(channel),
603-
trace_name_(name) {
610+
trace_name_(name),
611+
function_(fn) {
604612
// Make sure the channel object stays alive during the query lifetime.
605613
req_wrap_obj->Set(env()->context(),
606614
env()->channel_string(),
607615
channel->object()).FromJust();
608616
}
609617

610-
~QueryWrap() override {
611-
CHECK_EQ(false, persistent().IsEmpty());
612-
}
618+
~QueryWrap() override { CHECK_EQ(false, persistent().IsEmpty()); }
613619

614620
// Subclasses should implement the appropriate Send method.
615621
virtual int Send(const char* name) {
@@ -630,8 +636,12 @@ class QueryWrap : public AsyncWrap {
630636
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(
631637
TRACING_CATEGORY_NODE2(dns, native), trace_name_, this,
632638
"name", TRACE_STR_COPY(name));
633-
ares_query(channel_->cares_channel(), name, dnsclass, type, Callback,
634-
static_cast<void*>(this));
639+
function_(channel_->cares_channel(),
640+
name,
641+
dnsclass,
642+
type,
643+
Callback,
644+
static_cast<void*>(this));
635645
}
636646

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

758768
private:
759769
const char* trace_name_;
770+
ares_function* function_;
760771
};
761772

762-
763773
template <typename T>
764774
Local<Array> AddrTTLToArray(Environment* env,
765775
const T* addrttls,
@@ -1186,9 +1196,10 @@ int ParseSoaReply(Environment* env,
11861196

11871197
class QueryAnyWrap: public QueryWrap {
11881198
public:
1189-
QueryAnyWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
1190-
: QueryWrap(channel, req_wrap_obj, "resolveAny") {
1191-
}
1199+
QueryAnyWrap(ChannelWrap* channel,
1200+
Local<Object> req_wrap_obj,
1201+
ares_function* fn)
1202+
: QueryWrap(channel, req_wrap_obj, "resolveAny", fn) {}
11921203

11931204
int Send(const char* name) override {
11941205
AresQuery(name, ns_c_in, ns_t_any);
@@ -1364,12 +1375,12 @@ class QueryAnyWrap: public QueryWrap {
13641375
}
13651376
};
13661377

1367-
13681378
class QueryAWrap: public QueryWrap {
13691379
public:
1370-
QueryAWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
1371-
: QueryWrap(channel, req_wrap_obj, "resolve4") {
1372-
}
1380+
QueryAWrap(ChannelWrap* channel,
1381+
Local<Object> req_wrap_obj,
1382+
ares_function* fn)
1383+
: QueryWrap(channel, req_wrap_obj, "resolve4", fn) {}
13731384

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

14161427
class QueryAaaaWrap: public QueryWrap {
14171428
public:
1418-
QueryAaaaWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
1419-
: QueryWrap(channel, req_wrap_obj, "resolve6") {
1420-
}
1429+
QueryAaaaWrap(ChannelWrap* channel,
1430+
Local<Object> req_wrap_obj,
1431+
ares_function* fn)
1432+
: QueryWrap(channel, req_wrap_obj, "resolve6", fn) {}
14211433

14221434
int Send(const char* name) override {
14231435
AresQuery(name, ns_c_in, ns_t_aaaa);
@@ -1460,12 +1472,12 @@ class QueryAaaaWrap: public QueryWrap {
14601472
}
14611473
};
14621474

1463-
14641475
class QueryCnameWrap: public QueryWrap {
14651476
public:
1466-
QueryCnameWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
1467-
: QueryWrap(channel, req_wrap_obj, "resolveCname") {
1468-
}
1477+
QueryCnameWrap(ChannelWrap* channel,
1478+
Local<Object> req_wrap_obj,
1479+
ares_function* fn)
1480+
: QueryWrap(channel, req_wrap_obj, "resolveCname", fn) {}
14691481

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

14991511
class QueryMxWrap: public QueryWrap {
15001512
public:
1501-
QueryMxWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
1502-
: QueryWrap(channel, req_wrap_obj, "resolveMx") {
1503-
}
1513+
QueryMxWrap(ChannelWrap* channel,
1514+
Local<Object> req_wrap_obj,
1515+
ares_function* fn)
1516+
: QueryWrap(channel, req_wrap_obj, "resolveMx", fn) {}
15041517

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

15341547
class QueryNsWrap: public QueryWrap {
15351548
public:
1536-
QueryNsWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
1537-
: QueryWrap(channel, req_wrap_obj, "resolveNs") {
1538-
}
1549+
QueryNsWrap(ChannelWrap* channel,
1550+
Local<Object> req_wrap_obj,
1551+
ares_function* fn)
1552+
: QueryWrap(channel, req_wrap_obj, "resolveNs", fn) {}
15391553

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

15691583
class QueryTxtWrap: public QueryWrap {
15701584
public:
1571-
QueryTxtWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
1572-
: QueryWrap(channel, req_wrap_obj, "resolveTxt") {
1573-
}
1585+
QueryTxtWrap(ChannelWrap* channel,
1586+
Local<Object> req_wrap_obj,
1587+
ares_function* fn)
1588+
: QueryWrap(channel, req_wrap_obj, "resolveTxt", fn) {}
15741589

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

16031618
class QuerySrvWrap: public QueryWrap {
16041619
public:
1605-
explicit QuerySrvWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
1606-
: QueryWrap(channel, req_wrap_obj, "resolveSrv") {
1607-
}
1620+
explicit QuerySrvWrap(ChannelWrap* channel,
1621+
Local<Object> req_wrap_obj,
1622+
ares_function* fn)
1623+
: QueryWrap(channel, req_wrap_obj, "resolveSrv", fn) {}
16081624

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

16361652
class QueryPtrWrap: public QueryWrap {
16371653
public:
1638-
explicit QueryPtrWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
1639-
: QueryWrap(channel, req_wrap_obj, "resolvePtr") {
1640-
}
1654+
explicit QueryPtrWrap(ChannelWrap* channel,
1655+
Local<Object> req_wrap_obj,
1656+
ares_function* fn)
1657+
: QueryWrap(channel, req_wrap_obj, "resolvePtr", fn) {}
16411658

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

16711688
class QueryNaptrWrap: public QueryWrap {
16721689
public:
1673-
explicit QueryNaptrWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
1674-
: QueryWrap(channel, req_wrap_obj, "resolveNaptr") {
1675-
}
1690+
explicit QueryNaptrWrap(ChannelWrap* channel,
1691+
Local<Object> req_wrap_obj,
1692+
ares_function* fn)
1693+
: QueryWrap(channel, req_wrap_obj, "resolveNaptr", fn) {}
16761694

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

17051723
class QuerySoaWrap: public QueryWrap {
17061724
public:
1707-
QuerySoaWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
1708-
: QueryWrap(channel, req_wrap_obj, "resolveSoa") {
1709-
}
1725+
QuerySoaWrap(ChannelWrap* channel,
1726+
Local<Object> req_wrap_obj,
1727+
ares_function* fn)
1728+
: QueryWrap(channel, req_wrap_obj, "resolveSoa", fn) {}
17101729

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

17701789
class GetHostByAddrWrap: public QueryWrap {
17711790
public:
1772-
explicit GetHostByAddrWrap(ChannelWrap* channel, Local<Object> req_wrap_obj)
1773-
: QueryWrap(channel, req_wrap_obj, "reverse") {
1774-
}
1791+
explicit GetHostByAddrWrap(ChannelWrap* channel,
1792+
Local<Object> req_wrap_obj,
1793+
ares_function* fn)
1794+
: QueryWrap(channel, req_wrap_obj, "reverse", fn) {}
17751795

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

18291850
Local<Object> req_wrap_obj = args[0].As<Object>();
18301851
Local<String> string = args[1].As<String>();
1831-
Wrap* wrap = new Wrap(channel, req_wrap_obj);
1852+
bool is_search = args[2]->IsTrue();
1853+
ares_function* ares_fn_ = ares_query;
1854+
if (is_search) ares_fn_ = ares_search;
1855+
Wrap* wrap = new Wrap(channel, req_wrap_obj, ares_fn_);
18321856

18331857
node::Utf8Value name(env->isolate(), string);
18341858
channel->ModifyActivityQueryCount(1);

test/internet/test-dns.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,11 @@ TEST(async function test_resolve4_ttl(done) {
9898
ttl: true
9999
}));
100100

101-
const req = dns.resolve4(addresses.INET4_HOST, {
101+
validateResult(await dnsPromises.resolve4(addresses.INET4_HOST, {
102+
ttl: true, search: true
103+
}));
104+
105+
let req = dns.resolve4(addresses.INET4_HOST, {
102106
ttl: true
103107
}, function(err, result) {
104108
assert.ifError(err);
@@ -107,6 +111,16 @@ TEST(async function test_resolve4_ttl(done) {
107111
});
108112

109113
checkWrap(req);
114+
115+
req = dns.resolve4(addresses.INET4_HOST, {
116+
ttl: true, search: true
117+
}, function(err, result) {
118+
assert.ifError(err);
119+
validateResult(result);
120+
done();
121+
});
122+
123+
checkWrap(req);
110124
});
111125

112126
TEST(async function test_resolve6_ttl(done) {
@@ -128,7 +142,11 @@ TEST(async function test_resolve6_ttl(done) {
128142
ttl: true
129143
}));
130144

131-
const req = dns.resolve6(addresses.INET6_HOST, {
145+
validateResult(await dnsPromises.resolve6(addresses.INET6_HOST, {
146+
ttl: true, search: true
147+
}));
148+
149+
let req = dns.resolve6(addresses.INET6_HOST, {
132150
ttl: true
133151
}, function(err, result) {
134152
assert.ifError(err);
@@ -137,6 +155,16 @@ TEST(async function test_resolve6_ttl(done) {
137155
});
138156

139157
checkWrap(req);
158+
159+
req = dns.resolve6(addresses.INET6_HOST, {
160+
ttl: true, search: true
161+
}, function(err, result) {
162+
assert.ifError(err);
163+
validateResult(result);
164+
done();
165+
});
166+
167+
checkWrap(req);
140168
});
141169

142170
TEST(async function test_resolveMx(done) {

0 commit comments

Comments
 (0)