node/lib/internal/dgram.js
Ruben Bridgewater 1c12dd6347
dgram: skip dns.lookup() for literal IP addresses
Every unconnected send(), and the implicit bind on first send,
resolved the destination through dns.lookup() even when it was
already a literal IP. The address resolves to itself, so the call is
redundant, and tools that instrument dns.lookup() record a lookup for
every datagram sent to an IP.

Skip the resolver for a literal IP of the socket's family and report
it on the next tick, keeping dns.lookup()'s asynchronous contract. A
custom lookup function is still consulted for every address.

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/64133
Reviewed-By: Bryan English <bryan@bryanenglish.com>
Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day>
Reviewed-By: James M Snell <jasnell@gmail.com>
2026-06-30 13:07:49 +00:00

101 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 lookup4(lookup, address, callback) {
return lookup(address || '127.0.0.1', 4, callback);
}
function lookup6(lookup, address, callback) {
return lookup(address || '::1', 6, callback);
}
// A literal IP of the socket's family resolves to itself, so skip dns.lookup().
// Defer with nextTick to keep the callback async (e.g. bind()'s 'listening').
function defaultLookup(address, family, callback) {
if (isIP(address) === family) {
process.nextTick(callback, null, address, family);
return;
}
if (dns === undefined) {
dns = require('dns');
}
return dns.lookup(address, family, callback);
}
function newHandle(type, lookup) {
if (lookup === undefined) {
lookup = defaultLookup;
} 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,
};