-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
UDP/Datagram: Can't send messages while bind is done to 127.0.0.1 must bind to 0.0.0.0 #29047
Comments
I assume you think there is a bug, but I'm not sure what it is. Could you elaborate? That bind is implicitly done on first send is documented:
|
While the bind on first send (if not already bounded) is documented, in case the dgram module is used only as a UDP client (outgoing messages only), opening a local port to 0.0.0.0 seems like a security issue. |
That's how UDP sockets work in general, it's not specific to Node.js. From http://man7.org/linux/man-pages/man7/udp.7.html:
I don't follow that line of reasoning. You ask the operating system to restrict it to localhost and that's what it does. Thanks for taking the time to open an issue but I'm closing this out as notabug. |
So in order to (only) send UDP packets to a remote machine, you have to open a random local port to listen to 0.0.0.0? |
netcat does the exact same thing. And actually it's not netcat doing that but the kernel. Think about it: the UDP packet header requires a source address and port. |
The source port on the UDP packet header is optional (see https://tools.ietf.org/html/rfc768): I also don't think that the kernel does this bind, looking in uv/src/unix/udp.c: |
I linked you to the relevant man page. That rather closes the discussion, don't you think? Yes, libuv does an explicit bind. If it didn't, the kernel would do it implicitly on libuv's behalf. |
I agree, it looks like the bind will happen anyway. In any case, thank you for addressing this issue and the quick responses! |
When using dgram for only sending UDP messages, upon the first send there is an automatic bind to 0.0.0.0
Trying to explicitly bind to 127.0.0.1 will cause send to fail with the error message:
Error: send EINVAL 111.111.111.111:11111
at SendWrap.afterSend [as oncomplete] (dgram.js:467:11)
Reproduction:
let PORT = 11111;
let HOST = '111.111.111.111';
let dgram = require('dgram');
let message = Buffer.from('Testing!');
let client = dgram.createSocket('udp4');
client.bind(0, '127.0.0.1'); /////////////////// remove this line for auto bind or change to 0.0.0.0 to be able to send
client.send(message, 0, message.length, PORT, HOST, function(err, bytes) {
if (err) throw err;
console.log('UDP message sent to ' + HOST +':'+ PORT);
client.close();
});
The text was updated successfully, but these errors were encountered: