From e44d75011f571cc6b4930b11e07cfa3a117cb9e0 Mon Sep 17 00:00:00 2001 From: XadillaX Date: Sun, 21 May 2017 18:02:33 +0800 Subject: [PATCH] dns: add resolveAny support `dns.resolveAny` and `dns.resolve` with `"ANY"` has the similar behavior like `$ dig 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: https://github.com/nodejs/node/issues/2848 --- doc/api/dns.md | 47 ++ lib/dns.js | 2 + src/cares_wrap.cc | 795 +++++++++++++++++++++++++++------- src/env.h | 12 + test/internet/test-dns-any.js | 199 +++++++++ 5 files changed, 899 insertions(+), 156 deletions(-) create mode 100644 test/internet/test-dns-any.js diff --git a/doc/api/dns.md b/doc/api/dns.md index 42d93a14e7a020..d2cee6a6b3cd4e 100644 --- a/doc/api/dns.md +++ b/doc/api/dns.md @@ -197,6 +197,7 @@ records. The type and structure of individual results varies based on `rrtype`: | `'SOA'` | start of authority records | {Object} | [`dns.resolveSoa()`][] | | `'SRV'` | service records | {Object} | [`dns.resolveSrv()`][] | | `'TXT'` | text records | {string} | [`dns.resolveTxt()`][] | +| `'ANY'` | any records | {Object} | [`dns.resolveAny()`][] | On error, `err` is an [`Error`][] object, where `err.code` is one of the [DNS error codes](#dns_error_codes). @@ -417,6 +418,51 @@ is a two-dimensional array of the text records available for `hostname` (e.g., one record. Depending on the use case, these could be either joined together or treated separately. +## dns.resolveAny(hostname, callback) + +- `hostname` {string} +- `callback` {Function} + - `err` {Error} + - `ret` {Object[][]} + +Uses the DNS protocol to resolve any queries (`ANY` records) for the `hostname`. +The `ret` argument passed to the `callback` function will be an array of objects +with uncertain type of records. Each object has a property `type` that indicates +the type of current record. And for each type of record, the object structure +will be like: + +| Type | Properties | +|------|------------| +| `"A"` | `address` / `ttl` | +| `"AAAA"` | `address` / `ttl` | +| `"CNAME"` | `value` | +| `"MX"` | Refer to [`dns.resolveMx()`][] | +| `"NAPTR"` | Refer to [`dns.resolveNaptr()`][] | +| `"NS"` | `value` | +| `"PTR"` | `value` | +| `"SOA"` | Refer to [`dns.resolveSoa()`][] | +| `"SRV"` | Refer to [`dns.resolveSrv()`][] | +| `"TXT"` | This is an array-liked object with `length` and `indexes`, eg. `{'0':'sth','length':1}` | + +Following is a example of the `ret` object passed to the callback: + + +```js +[ { address: '127.0.0.1', ttl: 299, type: 'A' }, + { value: 'example.com', type: 'CNAME' }, // in fact, CNAME can't stay with A + { exchange: 'alt4.aspmx.l.example.com', priority: 50, type: 'MX' }, + { value: 'ns1.example.com', type: 'NS' }, + { '0': 'v=spf1 include:_spf.example.com ~all', type: 'TXT', length: 1 }, + { nsname: 'ns1.example.com', + hostmaster: 'admin.example.com', + serial: 156696742, + refresh: 900, + retry: 900, + expire: 1800, + minttl: 60, + type: 'SOA' } ] +``` + ## dns.reverse(ip, callback)