Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
dnsopts.hints = dns.ADDRCONFIG;
// The AI_V4MAPPED hint is not supported on FreeBSD, and getaddrinfo
// returns EAI_BADFLAGS. However, it seems to be supported on most other
// systems. See
// http://lists.freebsd.org/pipermail/freebsd-bugs/2008-February/028260.html
// and
// https://svnweb.freebsd.org/base/head/lib/libc/net/getaddrinfo.c?r1=172052&r2=175955
// for more information on the lack of support for FreeBSD.
if (process.platform !== 'freebsd')
dnsopts.hints |= dns.V4MAPPED;
}
debug('connect: find host ' + host);
debug('connect: dns options ' + dnsopts);
self._host = host;
dns.lookup(host, dnsopts, function(err, ip, addressType) {
self.emit('lookup', err, ip, addressType);
// It's possible we were destroyed while looking this up.
// XXX it would be great if we could cancel the promise returned by
// the look up.
if (!self._connecting) return;
if (err) {
// net.createConnection() creates a net.Socket object and
// immediately calls net.Socket.connect() on it (that's us).
// There are no event listeners registered yet so defer the
// error event to the next tick.
process.nextTick(function() {
self.emit('error', err);
self._destroy();
});
dnsopts.hints = dns.ADDRCONFIG;
// The AI_V4MAPPED hint is not supported on FreeBSD, and getaddrinfo
// returns EAI_BADFLAGS. However, it seems to be supported on most other
// systems. See
// http://lists.freebsd.org/pipermail/freebsd-bugs/2008-February/028260.html
// and
// https://svnweb.freebsd.org/base/head/lib/libc/net/getaddrinfo.c?r1=172052&r2=175955
// for more information on the lack of support for FreeBSD.
if (process.platform !== 'freebsd')
dnsopts.hints |= dns.V4MAPPED;
}
debug('connect: find host ' + host);
debug('connect: dns options ' + dnsopts);
self._host = host;
dns.lookup(host, dnsopts, function(err, ip, addressType) {
self.emit('lookup', err, ip, addressType);
// It's possible we were destroyed while looking this up.
// XXX it would be great if we could cancel the promise returned by
// the look up.
if (!self._connecting) return;
if (err) {
// net.createConnection() creates a net.Socket object and
// immediately calls net.Socket.connect() on it (that's us).
// There are no event listeners registered yet so defer the
// error event to the next tick.
process.nextTick(function() {
self.emit('error', err);
self._destroy();
});
// been solved before in https://github.com/nodejs/node/pull/12342, but was
// reverted as it had unintended side effects.
if (Array.isArray(args[0]) && args[0][normalizedArgsSymbol])
normalized = args[0];
else
normalized = normalizeArgs(args);
let options = normalized[0];
let cb = normalized[1];
if (!options.host)
options.host = '127.0.0.1';
let family = options.port === undefined ? 0 : native.isIP(options.host);
if (options.port === undefined || family)
this._connect(options, family, options.port === undefined ? options.path : options.host, cb);
else
dns.lookup(options.host, (err, host, family) => {
this.emit('lookup', err, options.host, family, host);
if (err) {
this.connecting = false;
this._updateRef();
this.emit('error', err);
return;
}
if (this.destroyed) {
this.connecting = false;
this._updateRef();
return;
}
this._connect(options, family, host, cb);
});
var lookup = function(address, family, callback) {
// implicit 'bind before send' needs to run on the same tick
var matchedFamily = isIP(address);
if (matchedFamily)
return callback(null, address, matchedFamily);
if (!dns)
dns = require('dns');
return dns.lookup(address, family, callback);
};
TEST(function test_lookup_ipv4_explicit(done) {
var req = dns.lookup('www.google.com', 4, function(err, ip, family) {
if (err) throw err;
assert.ok(net.isIPv4(ip));
assert.strictEqual(family, 4);
done();
});
checkWrap(req);
});
TEST(function test_lookup_localhost_ipv4(done) {
var req = dns.lookup('localhost', 4, function(err, ip, family) {
if (err) throw err;
assert.strictEqual(ip, '127.0.0.1');
assert.strictEqual(family, 4);
done();
});
checkWrap(req);
});
var dns = require('dns');
var host = options.host ? options.host : 'localhost';
var port = Number(options.port);
var dnsopts = {
family: (options.family || 4) >>> 0,
hints: 0,
};
if (!util.isNumber(port) || port < 0 || port > 65535)
throw new RangeError('port should be >= 0 and < 65536: ' + options.port);
if (dnsopts.family !== 0 && dnsopts.family !== 4 && dnsopts.family !== 6)
throw new RangeError('family should be 4 or 6: ' + dnsopts.family);
self._host = host;
dns.lookup(host, dnsopts, function(err, ip, family) {
if (self._socketState.destroyed) {
return;
}
self.emit('lookup', err, ip, family);
if (err) {
emitError(self, err);
} else {
resetSocketTimeout(self);
process.nextTick(function() {
connect(self, ip, port);
});
}
});
return self;
DnsApi.prototype.lookup = function (hostname, family, callback) {
if (typeof family === 'function') {
callback = family;
family = 0;
}
dns.lookup(hostname, family, callback);
};