Skip to content

Commit e44d750

Browse files
committed
dns: add resolveAny support
`dns.resolveAny` and `dns.resolve` with `"ANY"` has the similar behavior like `$ dig <domain> any` and returns an array with several types of records. `dns.resolveAny` parses the result packet by several rules in turn. Supported types: * A * AAAA * CNAME * MX * NAPTR * NS * PTR * SOA * SRV * TXT Refs: nodejs#2848
1 parent 5254975 commit e44d750

File tree

5 files changed

+899
-156
lines changed

5 files changed

+899
-156
lines changed

doc/api/dns.md

+47
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ records. The type and structure of individual results varies based on `rrtype`:
197197
| `'SOA'` | start of authority records | {Object} | [`dns.resolveSoa()`][] |
198198
| `'SRV'` | service records | {Object} | [`dns.resolveSrv()`][] |
199199
| `'TXT'` | text records | {string} | [`dns.resolveTxt()`][] |
200+
| `'ANY'` | any records | {Object} | [`dns.resolveAny()`][] |
200201

201202
On error, `err` is an [`Error`][] object, where `err.code` is one of the
202203
[DNS error codes](#dns_error_codes).
@@ -417,6 +418,51 @@ is a two-dimensional array of the text records available for `hostname` (e.g.,
417418
one record. Depending on the use case, these could be either joined together or
418419
treated separately.
419420

421+
## dns.resolveAny(hostname, callback)
422+
423+
- `hostname` {string}
424+
- `callback` {Function}
425+
- `err` {Error}
426+
- `ret` {Object[][]}
427+
428+
Uses the DNS protocol to resolve any queries (`ANY` records) for the `hostname`.
429+
The `ret` argument passed to the `callback` function will be an array of objects
430+
with uncertain type of records. Each object has a property `type` that indicates
431+
the type of current record. And for each type of record, the object structure
432+
will be like:
433+
434+
| Type | Properties |
435+
|------|------------|
436+
| `"A"` | `address` / `ttl` |
437+
| `"AAAA"` | `address` / `ttl` |
438+
| `"CNAME"` | `value` |
439+
| `"MX"` | Refer to [`dns.resolveMx()`][] |
440+
| `"NAPTR"` | Refer to [`dns.resolveNaptr()`][] |
441+
| `"NS"` | `value` |
442+
| `"PTR"` | `value` |
443+
| `"SOA"` | Refer to [`dns.resolveSoa()`][] |
444+
| `"SRV"` | Refer to [`dns.resolveSrv()`][] |
445+
| `"TXT"` | This is an array-liked object with `length` and `indexes`, eg. `{'0':'sth','length':1}` |
446+
447+
Following is a example of the `ret` object passed to the callback:
448+
449+
<!-- eslint-disable -->
450+
```js
451+
[ { address: '127.0.0.1', ttl: 299, type: 'A' },
452+
{ value: 'example.com', type: 'CNAME' }, // in fact, CNAME can't stay with A
453+
{ exchange: 'alt4.aspmx.l.example.com', priority: 50, type: 'MX' },
454+
{ value: 'ns1.example.com', type: 'NS' },
455+
{ '0': 'v=spf1 include:_spf.example.com ~all', type: 'TXT', length: 1 },
456+
{ nsname: 'ns1.example.com',
457+
hostmaster: 'admin.example.com',
458+
serial: 156696742,
459+
refresh: 900,
460+
retry: 900,
461+
expire: 1800,
462+
minttl: 60,
463+
type: 'SOA' } ]
464+
```
465+
420466
## dns.reverse(ip, callback)
421467
<!-- YAML
422468
added: v0.1.16
@@ -531,6 +577,7 @@ uses. For instance, _they do not use the configuration from `/etc/hosts`_.
531577
[`dns.resolveSoa()`]: #dns_dns_resolvesoa_hostname_callback
532578
[`dns.resolveSrv()`]: #dns_dns_resolvesrv_hostname_callback
533579
[`dns.resolveTxt()`]: #dns_dns_resolvetxt_hostname_callback
580+
[`dns.resolveAny()`]: #dns_dns_resolveany_hostname_callback
534581
[DNS error codes]: #dns_error_codes
535582
[Implementation considerations section]: #dns_implementation_considerations
536583
[supported `getaddrinfo` flags]: #dns_supported_getaddrinfo_flags

lib/dns.js

+2
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ function resolver(bindingName) {
279279

280280

281281
var resolveMap = Object.create(null);
282+
resolveMap.ANY = resolver('queryAny');
282283
resolveMap.A = resolver('queryA');
283284
resolveMap.AAAA = resolver('queryAaaa');
284285
resolveMap.CNAME = resolver('queryCname');
@@ -361,6 +362,7 @@ module.exports = {
361362
getServers,
362363
setServers,
363364
resolve,
365+
resolveAny: resolveMap.ANY,
364366
resolve4: resolveMap.A,
365367
resolve6: resolveMap.AAAA,
366368
resolveCname: resolveMap.CNAME,

0 commit comments

Comments
 (0)