2017-01-03 22:16:48 +01:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-04-09 09:55:53 +02:00
|
|
|
const { Object } = primordials;
|
|
|
|
|
2018-08-23 15:28:41 +02:00
|
|
|
const cares = internalBinding('cares_wrap');
|
2018-10-10 20:23:48 +02:00
|
|
|
const { toASCII } = require('internal/idna');
|
2019-04-16 20:07:17 +02:00
|
|
|
const { isIP, isLegalPort } = require('internal/net');
|
2017-04-16 21:35:29 +02:00
|
|
|
const { customPromisifyArgs } = require('internal/util');
|
2017-07-13 01:58:59 +02:00
|
|
|
const errors = require('internal/errors');
|
2018-02-27 14:55:32 +01:00
|
|
|
const {
|
2018-06-11 20:56:33 +02:00
|
|
|
bindDefaultResolver,
|
|
|
|
getDefaultResolver,
|
|
|
|
setDefaultResolver,
|
|
|
|
Resolver,
|
2018-09-30 12:17:36 +02:00
|
|
|
validateHints,
|
|
|
|
emitInvalidHostnameWarning,
|
2018-06-11 20:56:33 +02:00
|
|
|
} = require('internal/dns/utils');
|
|
|
|
const {
|
2018-02-27 14:55:32 +01:00
|
|
|
ERR_INVALID_ARG_TYPE,
|
|
|
|
ERR_INVALID_CALLBACK,
|
|
|
|
ERR_INVALID_OPT_VALUE,
|
|
|
|
ERR_MISSING_ARGS,
|
|
|
|
ERR_SOCKET_BAD_PORT
|
|
|
|
} = errors.codes;
|
2018-08-03 00:51:02 +02:00
|
|
|
const { validateString } = require('internal/validators');
|
2013-07-18 23:18:50 +02:00
|
|
|
|
2017-07-27 16:34:40 +02:00
|
|
|
const {
|
|
|
|
GetAddrInfoReqWrap,
|
|
|
|
GetNameInfoReqWrap,
|
|
|
|
QueryReqWrap,
|
|
|
|
} = cares;
|
|
|
|
|
2018-02-03 10:09:15 +01:00
|
|
|
const dnsException = errors.dnsException;
|
2013-07-18 23:18:50 +02:00
|
|
|
|
2018-12-02 18:25:39 +01:00
|
|
|
let promises = null; // Lazy loaded
|
2018-06-11 20:56:33 +02:00
|
|
|
|
2013-08-12 17:54:11 +02:00
|
|
|
function onlookup(err, addresses) {
|
|
|
|
if (err) {
|
2018-02-03 10:09:15 +01:00
|
|
|
return this.callback(dnsException(err, 'getaddrinfo', this.hostname));
|
2013-08-12 17:54:11 +02:00
|
|
|
}
|
|
|
|
if (this.family) {
|
|
|
|
this.callback(null, addresses[0], this.family);
|
|
|
|
} else {
|
2019-04-16 20:07:17 +02:00
|
|
|
this.callback(null, addresses[0], isIP(addresses[0]));
|
2013-08-12 17:54:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-06 21:43:50 +01:00
|
|
|
function onlookupall(err, addresses) {
|
|
|
|
if (err) {
|
2018-02-03 10:09:15 +01:00
|
|
|
return this.callback(dnsException(err, 'getaddrinfo', this.hostname));
|
2015-02-06 21:43:50 +01:00
|
|
|
}
|
|
|
|
|
2019-03-26 05:21:27 +01:00
|
|
|
const family = this.family;
|
2015-02-06 21:43:50 +01:00
|
|
|
for (var i = 0; i < addresses.length; i++) {
|
2017-05-28 03:44:03 +02:00
|
|
|
const addr = addresses[i];
|
|
|
|
addresses[i] = {
|
|
|
|
address: addr,
|
2019-04-16 20:07:17 +02:00
|
|
|
family: family || isIP(addr)
|
2017-05-28 03:44:03 +02:00
|
|
|
};
|
2015-02-06 21:43:50 +01:00
|
|
|
}
|
|
|
|
|
2017-05-28 03:44:03 +02:00
|
|
|
this.callback(null, addresses);
|
2015-02-06 21:43:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-05 00:17:20 +02:00
|
|
|
// Easy DNS A/AAAA look up
|
2014-05-22 04:13:09 +02:00
|
|
|
// lookup(hostname, [options,] callback)
|
2017-02-28 02:54:16 +01:00
|
|
|
function lookup(hostname, options, callback) {
|
2014-05-22 04:13:09 +02:00
|
|
|
var hints = 0;
|
2014-08-08 04:36:56 +02:00
|
|
|
var family = -1;
|
2015-02-06 21:43:50 +01:00
|
|
|
var all = false;
|
2017-08-10 11:19:13 +02:00
|
|
|
var verbatim = false;
|
2014-05-22 04:13:09 +02:00
|
|
|
|
|
|
|
// Parse arguments
|
2014-08-15 16:40:09 +02:00
|
|
|
if (hostname && typeof hostname !== 'string') {
|
2018-09-30 12:17:36 +02:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('hostname', 'string', hostname);
|
2014-08-15 16:40:09 +02:00
|
|
|
} else if (typeof options === 'function') {
|
2014-05-22 04:13:09 +02:00
|
|
|
callback = options;
|
2011-07-05 00:17:20 +02:00
|
|
|
family = 0;
|
2014-05-22 04:13:09 +02:00
|
|
|
} else if (typeof callback !== 'function') {
|
2019-04-02 03:46:17 +02:00
|
|
|
throw new ERR_INVALID_CALLBACK(callback);
|
2015-01-29 02:05:53 +01:00
|
|
|
} else if (options !== null && typeof options === 'object') {
|
2014-05-22 04:13:09 +02:00
|
|
|
hints = options.hints >>> 0;
|
|
|
|
family = options.family >>> 0;
|
2015-02-06 21:43:50 +01:00
|
|
|
all = options.all === true;
|
2017-08-10 11:19:13 +02:00
|
|
|
verbatim = options.verbatim === true;
|
2014-05-22 04:13:09 +02:00
|
|
|
|
2018-06-11 20:56:33 +02:00
|
|
|
validateHints(hints);
|
2014-08-08 04:36:56 +02:00
|
|
|
} else {
|
|
|
|
family = options >>> 0;
|
2011-07-05 00:17:20 +02:00
|
|
|
}
|
2014-05-22 04:13:09 +02:00
|
|
|
|
|
|
|
if (family !== 0 && family !== 4 && family !== 6)
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_OPT_VALUE('family', family);
|
2014-05-22 04:13:09 +02:00
|
|
|
|
2013-10-15 12:51:12 +02:00
|
|
|
if (!hostname) {
|
2018-09-30 12:17:36 +02:00
|
|
|
emitInvalidHostnameWarning(hostname);
|
2015-02-06 21:43:50 +01:00
|
|
|
if (all) {
|
2017-05-28 02:35:13 +02:00
|
|
|
process.nextTick(callback, null, []);
|
2015-02-06 21:43:50 +01:00
|
|
|
} else {
|
2017-05-28 02:35:13 +02:00
|
|
|
process.nextTick(callback, null, null, family === 6 ? 6 : 4);
|
2015-02-06 21:43:50 +01:00
|
|
|
}
|
2011-07-05 00:17:20 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-03-26 05:21:27 +01:00
|
|
|
const matchedFamily = isIP(hostname);
|
2011-07-05 00:17:20 +02:00
|
|
|
if (matchedFamily) {
|
2015-02-06 21:43:50 +01:00
|
|
|
if (all) {
|
2017-05-28 02:35:13 +02:00
|
|
|
process.nextTick(
|
2017-07-11 02:55:21 +02:00
|
|
|
callback, null, [{ address: hostname, family: matchedFamily }]);
|
2015-02-06 21:43:50 +01:00
|
|
|
} else {
|
2017-05-28 02:35:13 +02:00
|
|
|
process.nextTick(callback, null, hostname, matchedFamily);
|
2015-02-06 21:43:50 +01:00
|
|
|
}
|
2011-07-05 00:17:20 +02:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-03-26 05:21:27 +01:00
|
|
|
const req = new GetAddrInfoReqWrap();
|
2014-12-09 05:29:47 +01:00
|
|
|
req.callback = callback;
|
|
|
|
req.family = family;
|
|
|
|
req.hostname = hostname;
|
2015-02-06 21:43:50 +01:00
|
|
|
req.oncomplete = all ? onlookupall : onlookup;
|
2014-06-05 02:26:34 +02:00
|
|
|
|
2019-03-26 05:21:27 +01:00
|
|
|
const err = cares.getaddrinfo(
|
|
|
|
req, toASCII(hostname), family, hints, verbatim
|
|
|
|
);
|
2014-06-05 02:26:34 +02:00
|
|
|
if (err) {
|
2018-02-03 10:09:15 +01:00
|
|
|
process.nextTick(callback, dnsException(err, 'getaddrinfo', hostname));
|
2014-06-05 02:26:34 +02:00
|
|
|
return {};
|
|
|
|
}
|
2013-07-18 23:18:50 +02:00
|
|
|
return req;
|
2017-02-28 02:54:16 +01:00
|
|
|
}
|
2011-07-05 00:17:20 +02:00
|
|
|
|
2017-04-16 21:35:29 +02:00
|
|
|
Object.defineProperty(lookup, customPromisifyArgs,
|
|
|
|
{ value: ['address', 'family'], enumerable: false });
|
|
|
|
|
2011-07-05 00:17:20 +02:00
|
|
|
|
2018-06-22 19:41:42 +02:00
|
|
|
function onlookupservice(err, hostname, service) {
|
2014-06-21 00:43:00 +02:00
|
|
|
if (err)
|
2018-06-22 19:41:42 +02:00
|
|
|
return this.callback(dnsException(err, 'getnameinfo', this.hostname));
|
2014-06-21 00:43:00 +02:00
|
|
|
|
2018-06-22 19:41:42 +02:00
|
|
|
this.callback(null, hostname, service);
|
2014-06-21 00:43:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// lookupService(address, port, callback)
|
2018-06-22 19:41:42 +02:00
|
|
|
function lookupService(hostname, port, callback) {
|
2014-06-21 00:43:00 +02:00
|
|
|
if (arguments.length !== 3)
|
2018-06-22 19:41:42 +02:00
|
|
|
throw new ERR_MISSING_ARGS('hostname', 'port', 'callback');
|
2014-06-21 00:43:00 +02:00
|
|
|
|
2018-06-22 19:41:42 +02:00
|
|
|
if (isIP(hostname) === 0)
|
|
|
|
throw new ERR_INVALID_OPT_VALUE('hostname', hostname);
|
2014-06-21 00:43:00 +02:00
|
|
|
|
2016-08-18 23:09:37 +02:00
|
|
|
if (!isLegalPort(port))
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_SOCKET_BAD_PORT(port);
|
2016-01-24 15:58:56 +01:00
|
|
|
|
2016-08-18 23:09:37 +02:00
|
|
|
if (typeof callback !== 'function')
|
2019-04-02 03:46:17 +02:00
|
|
|
throw new ERR_INVALID_CALLBACK(callback);
|
2016-08-18 23:09:37 +02:00
|
|
|
|
2016-01-26 15:15:53 +01:00
|
|
|
port = +port;
|
2014-06-21 00:43:00 +02:00
|
|
|
|
2019-03-26 05:21:27 +01:00
|
|
|
const req = new GetNameInfoReqWrap();
|
2014-12-09 05:29:47 +01:00
|
|
|
req.callback = callback;
|
2018-06-22 19:41:42 +02:00
|
|
|
req.hostname = hostname;
|
2014-12-09 05:29:47 +01:00
|
|
|
req.port = port;
|
|
|
|
req.oncomplete = onlookupservice;
|
|
|
|
|
2019-03-26 05:21:27 +01:00
|
|
|
const err = cares.getnameinfo(req, hostname, port);
|
2018-06-22 19:41:42 +02:00
|
|
|
if (err) throw dnsException(err, 'getnameinfo', hostname);
|
2014-06-21 00:43:00 +02:00
|
|
|
return req;
|
2017-02-28 02:54:16 +01:00
|
|
|
}
|
2014-06-21 00:43:00 +02:00
|
|
|
|
2017-04-16 21:35:29 +02:00
|
|
|
Object.defineProperty(lookupService, customPromisifyArgs,
|
|
|
|
{ value: ['hostname', 'service'], enumerable: false });
|
|
|
|
|
2014-06-21 00:43:00 +02:00
|
|
|
|
2016-10-26 07:51:34 +02:00
|
|
|
function onresolve(err, result, ttls) {
|
|
|
|
if (ttls && this.ttl)
|
|
|
|
result = result.map((address, index) => ({ address, ttl: ttls[index] }));
|
|
|
|
|
2013-08-12 17:54:11 +02:00
|
|
|
if (err)
|
2018-02-03 10:09:15 +01:00
|
|
|
this.callback(dnsException(err, this.bindingName, this.hostname));
|
2013-08-12 17:54:11 +02:00
|
|
|
else
|
|
|
|
this.callback(null, result);
|
|
|
|
}
|
|
|
|
|
2011-07-05 00:17:20 +02:00
|
|
|
function resolver(bindingName) {
|
2017-07-31 22:06:47 +02:00
|
|
|
function query(name, /* options, */ callback) {
|
2016-10-26 07:51:34 +02:00
|
|
|
var options;
|
|
|
|
if (arguments.length > 2) {
|
|
|
|
options = callback;
|
|
|
|
callback = arguments[2];
|
|
|
|
}
|
|
|
|
|
2018-08-03 00:51:02 +02:00
|
|
|
validateString(name, 'name');
|
|
|
|
if (typeof callback !== 'function') {
|
2019-04-02 03:46:17 +02:00
|
|
|
throw new ERR_INVALID_CALLBACK(callback);
|
2014-02-07 18:50:29 +01:00
|
|
|
}
|
|
|
|
|
2019-03-26 05:21:27 +01:00
|
|
|
const req = new QueryReqWrap();
|
2015-09-30 23:55:06 +02:00
|
|
|
req.bindingName = bindingName;
|
|
|
|
req.callback = callback;
|
|
|
|
req.hostname = name;
|
|
|
|
req.oncomplete = onresolve;
|
2016-10-26 07:51:34 +02:00
|
|
|
req.ttl = !!(options && options.ttl);
|
2019-03-26 05:21:27 +01:00
|
|
|
const err = this._handle[bindingName](req, toASCII(name));
|
2018-04-01 07:54:32 +02:00
|
|
|
if (err) throw dnsException(err, bindingName, name);
|
2013-07-18 23:18:50 +02:00
|
|
|
return req;
|
2017-07-31 22:06:47 +02:00
|
|
|
}
|
|
|
|
Object.defineProperty(query, 'name', { value: bindingName });
|
|
|
|
return query;
|
2011-07-05 00:17:20 +02:00
|
|
|
}
|
|
|
|
|
2019-03-26 05:21:27 +01:00
|
|
|
const resolveMap = Object.create(null);
|
2017-07-27 18:54:20 +02:00
|
|
|
Resolver.prototype.resolveAny = resolveMap.ANY = resolver('queryAny');
|
|
|
|
Resolver.prototype.resolve4 = resolveMap.A = resolver('queryA');
|
|
|
|
Resolver.prototype.resolve6 = resolveMap.AAAA = resolver('queryAaaa');
|
|
|
|
Resolver.prototype.resolveCname = resolveMap.CNAME = resolver('queryCname');
|
|
|
|
Resolver.prototype.resolveMx = resolveMap.MX = resolver('queryMx');
|
|
|
|
Resolver.prototype.resolveNs = resolveMap.NS = resolver('queryNs');
|
|
|
|
Resolver.prototype.resolveTxt = resolveMap.TXT = resolver('queryTxt');
|
|
|
|
Resolver.prototype.resolveSrv = resolveMap.SRV = resolver('querySrv');
|
|
|
|
Resolver.prototype.resolvePtr = resolveMap.PTR = resolver('queryPtr');
|
|
|
|
Resolver.prototype.resolveNaptr = resolveMap.NAPTR = resolver('queryNaptr');
|
|
|
|
Resolver.prototype.resolveSoa = resolveMap.SOA = resolver('querySoa');
|
|
|
|
Resolver.prototype.reverse = resolver('getHostByAddr');
|
|
|
|
|
|
|
|
Resolver.prototype.resolve = resolve;
|
2017-02-28 02:54:16 +01:00
|
|
|
|
2017-05-17 22:37:30 +02:00
|
|
|
function resolve(hostname, rrtype, callback) {
|
|
|
|
var resolver;
|
|
|
|
if (typeof rrtype === 'string') {
|
|
|
|
resolver = resolveMap[rrtype];
|
|
|
|
} else if (typeof rrtype === 'function') {
|
2017-02-28 02:54:16 +01:00
|
|
|
resolver = resolveMap.A;
|
2017-05-17 22:37:30 +02:00
|
|
|
callback = rrtype;
|
2014-02-07 18:18:27 +01:00
|
|
|
} else {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_ARG_TYPE('rrtype', 'string', rrtype);
|
2011-07-05 00:17:20 +02:00
|
|
|
}
|
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof resolver === 'function') {
|
2017-07-27 18:54:20 +02:00
|
|
|
return resolver.call(this, hostname, callback);
|
2011-07-05 00:17:20 +02:00
|
|
|
} else {
|
2018-02-27 14:55:32 +01:00
|
|
|
throw new ERR_INVALID_OPT_VALUE('rrtype', rrtype);
|
2011-07-05 00:17:20 +02:00
|
|
|
}
|
2017-02-28 02:54:16 +01:00
|
|
|
}
|
2011-07-05 00:17:20 +02:00
|
|
|
|
2017-11-15 08:09:33 +01:00
|
|
|
function defaultResolverSetServers(servers) {
|
|
|
|
const resolver = new Resolver();
|
2018-06-11 20:56:33 +02:00
|
|
|
|
2017-11-15 08:09:33 +01:00
|
|
|
resolver.setServers(servers);
|
2018-06-11 20:56:33 +02:00
|
|
|
setDefaultResolver(resolver);
|
|
|
|
bindDefaultResolver(module.exports, Resolver.prototype);
|
|
|
|
|
2018-12-02 18:25:39 +01:00
|
|
|
if (promises !== null)
|
2018-06-11 20:56:33 +02:00
|
|
|
bindDefaultResolver(promises, promises.Resolver.prototype);
|
2017-11-15 08:09:33 +01:00
|
|
|
}
|
2017-07-27 18:54:20 +02:00
|
|
|
|
2017-02-28 02:54:16 +01:00
|
|
|
module.exports = {
|
|
|
|
lookup,
|
|
|
|
lookupService,
|
2017-07-27 18:54:20 +02:00
|
|
|
|
|
|
|
Resolver,
|
2017-11-15 08:09:33 +01:00
|
|
|
setServers: defaultResolverSetServers,
|
2017-02-28 02:54:16 +01:00
|
|
|
|
|
|
|
// uv_getaddrinfo flags
|
|
|
|
ADDRCONFIG: cares.AI_ADDRCONFIG,
|
|
|
|
V4MAPPED: cares.AI_V4MAPPED,
|
|
|
|
|
|
|
|
// ERROR CODES
|
|
|
|
NODATA: 'ENODATA',
|
|
|
|
FORMERR: 'EFORMERR',
|
|
|
|
SERVFAIL: 'ESERVFAIL',
|
|
|
|
NOTFOUND: 'ENOTFOUND',
|
|
|
|
NOTIMP: 'ENOTIMP',
|
|
|
|
REFUSED: 'EREFUSED',
|
|
|
|
BADQUERY: 'EBADQUERY',
|
|
|
|
BADNAME: 'EBADNAME',
|
|
|
|
BADFAMILY: 'EBADFAMILY',
|
|
|
|
BADRESP: 'EBADRESP',
|
|
|
|
CONNREFUSED: 'ECONNREFUSED',
|
|
|
|
TIMEOUT: 'ETIMEOUT',
|
|
|
|
EOF: 'EOF',
|
|
|
|
FILE: 'EFILE',
|
|
|
|
NOMEM: 'ENOMEM',
|
|
|
|
DESTRUCTION: 'EDESTRUCTION',
|
|
|
|
BADSTR: 'EBADSTR',
|
|
|
|
BADFLAGS: 'EBADFLAGS',
|
|
|
|
NONAME: 'ENONAME',
|
|
|
|
BADHINTS: 'EBADHINTS',
|
|
|
|
NOTINITIALIZED: 'ENOTINITIALIZED',
|
|
|
|
LOADIPHLPAPI: 'ELOADIPHLPAPI',
|
|
|
|
ADDRGETNETWORKPARAMS: 'EADDRGETNETWORKPARAMS',
|
|
|
|
CANCELLED: 'ECANCELLED'
|
|
|
|
};
|
2017-11-15 08:09:33 +01:00
|
|
|
|
2018-06-11 20:56:33 +02:00
|
|
|
bindDefaultResolver(module.exports, getDefaultResolver());
|
|
|
|
|
|
|
|
Object.defineProperties(module.exports, {
|
|
|
|
promises: {
|
|
|
|
configurable: true,
|
2019-03-27 18:21:25 +01:00
|
|
|
enumerable: true,
|
2018-06-11 20:56:33 +02:00
|
|
|
get() {
|
2018-12-02 18:25:39 +01:00
|
|
|
if (promises === null) {
|
2018-06-11 20:56:33 +02:00
|
|
|
promises = require('internal/dns/promises');
|
|
|
|
promises.setServers = defaultResolverSetServers;
|
|
|
|
}
|
|
|
|
return promises;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|