Open
Description
Version
14.x
Platform
All
Subsystem
dgram
What steps will reproduce the bug?
It appears that UDP/dgram connections perform unnecessary DNS lookups when an IP address is provided as the address to connect to.
The issue is easily demonstrated via this snippet:
const dns = require("dns");
const _lookup = dns.lookup;
dns.lookup = function (...args) {
console.log("Called lookup", args);
return _lookup(...arguments);
};
const dgram = require("dgram");
const socket = dgram.createSocket({ type: "udp4" });
console.log("Socket created");
socket.connect(1234, "192.168.1.1", (err) => {
console.log("Socket connected", { err });
});
Output:
$ node index.js
Socket created
Called lookup [ '0.0.0.0', 4, [Function (anonymous)] ]
Called lookup [ '192.168.1.1', 4, [Function: afterDns] ]
Socket connected { err: undefined }
How often does it reproduce? Is there a required condition?
These lookups appear to happen 100% of the time a UDP socket connects to an address that is a raw IP address.
What is the expected behavior?
DNS lookups only occur when the address is a hostname.
What do you see instead?
No response
Additional information
Note that the net
module contains code that bypasses dns.lookup()
when the target address is a valid IP address. It seems like we could improve efficiency by adding a similar check to dgram
without breaking compatibility.