mirror of
https://github.com/nodejs/node.git
synced 2026-11-09 15:11:11 -05:00
A user-supplied lookup function is no longer called when the destination is a literal IP of the socket's family; the address is used directly, matching net.connect(), which skips the lookup for a literal IP host before consulting options.lookup. This is a breaking change for a lookup that expected to be invoked for IP addresses. Refs: https://github.com/DataDog/dd-trace-js/issues/2984 Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de> PR-URL: https://github.com/nodejs/node/pull/64131 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
105 lines
2.2 KiB
JavaScript
105 lines
2.2 KiB
JavaScript
'use strict';
|
|
|
|
const {
|
|
FunctionPrototypeBind,
|
|
Symbol,
|
|
} = primordials;
|
|
|
|
const { codes: {
|
|
ERR_SOCKET_BAD_TYPE,
|
|
} } = require('internal/errors');
|
|
const { isIP } = require('internal/net');
|
|
const { UDP } = internalBinding('udp_wrap');
|
|
const { guessHandleType } = require('internal/util');
|
|
const {
|
|
isInt32,
|
|
validateFunction,
|
|
} = require('internal/validators');
|
|
const { UV_EINVAL } = internalBinding('uv');
|
|
const kStateSymbol = Symbol('state symbol');
|
|
let dns; // Lazy load for startup performance.
|
|
|
|
function lookupOrSkip(lookup, address, family, callback) {
|
|
if (isIP(address) === family) {
|
|
process.nextTick(callback, null, address, family);
|
|
return;
|
|
}
|
|
return lookup(address, family, callback);
|
|
}
|
|
|
|
function lookup4(lookup, address, callback) {
|
|
if (address) {
|
|
return lookupOrSkip(lookup, address, 4, callback);
|
|
}
|
|
process.nextTick(callback, null, '127.0.0.1', 4);
|
|
}
|
|
|
|
function lookup6(lookup, address, callback) {
|
|
if (address) {
|
|
return lookupOrSkip(lookup, address, 6, callback);
|
|
}
|
|
process.nextTick(callback, null, '::1', 6);
|
|
}
|
|
|
|
function newHandle(type, lookup) {
|
|
if (lookup === undefined) {
|
|
if (dns === undefined) {
|
|
dns = require('dns');
|
|
}
|
|
|
|
lookup = dns.lookup;
|
|
} else {
|
|
validateFunction(lookup, 'lookup');
|
|
}
|
|
|
|
if (type === 'udp4') {
|
|
const handle = new UDP();
|
|
|
|
handle.lookup = FunctionPrototypeBind(lookup4, handle, lookup);
|
|
return handle;
|
|
}
|
|
|
|
if (type === 'udp6') {
|
|
const handle = new UDP();
|
|
|
|
handle.lookup = FunctionPrototypeBind(lookup6, handle, lookup);
|
|
handle.bind = handle.bind6;
|
|
handle.connect = handle.connect6;
|
|
handle.send = handle.send6;
|
|
return handle;
|
|
}
|
|
|
|
throw new ERR_SOCKET_BAD_TYPE();
|
|
}
|
|
|
|
|
|
function _createSocketHandle(address, port, addressType, fd, flags) {
|
|
const handle = newHandle(addressType);
|
|
let err;
|
|
|
|
if (isInt32(fd) && fd > 0) {
|
|
const type = guessHandleType(fd);
|
|
if (type !== 'UDP') {
|
|
err = UV_EINVAL;
|
|
} else {
|
|
err = handle.open(fd);
|
|
}
|
|
} else if (port || address) {
|
|
err = handle.bind(address, port || 0, flags);
|
|
}
|
|
|
|
if (err) {
|
|
handle.close();
|
|
return err;
|
|
}
|
|
|
|
return handle;
|
|
}
|
|
|
|
|
|
module.exports = {
|
|
kStateSymbol,
|
|
_createSocketHandle,
|
|
newHandle,
|
|
};
|