Skip to content
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

socket bind vs SO_BINDTODEVICE #35769

Open
dcharbonnier opened this issue Oct 23, 2020 · 2 comments
Open

socket bind vs SO_BINDTODEVICE #35769

dcharbonnier opened this issue Oct 23, 2020 · 2 comments
Labels

Comments

@dcharbonnier
Copy link
Contributor

@dcharbonnier dcharbonnier commented Oct 23, 2020

I'm exclusively speaking about client Socket on Linux, server socket is not an issue and other platforms behave differently.

This issue is not a setup issue (as mentioned on many of the issues closed), routing is not a solution, the use case is to connect to the same remote address on the same port with two different IP through two different interface based on software decisions. Routing won't solve this.

The only mechanism for socket binding is the option "localAddress", this simply binds the socket to a specific address.
In case of outbound connection, we have no way to force the outbound interface and most likely, when people create an outbound connection what they expect with this parameter is to bind the socket to an interface.
That makes little sense to bind to an address and still lets the kernel route the packets through another interface, it will probably lead to impossible routing of the response.

There is been some workaround using the private method _createHandle (node 10), to access the fs and set some socket options, but this is not possible in the latest versions (can still use nextTick after socket creation, but...).

#3625 (comment)

There are two options :

  • It could be safe to assume that and always bind to the corresponding interface when localAddress is set, the socket is an AF_INET and it's a client socket with setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, iface, length)
  • Add an event on the Socket emitted when the socket is created (fd exists) and before the connection, as some of the socket options can't be set after that.

If it was about designing a new API, the first option would probably be the best one, but the second one is more robust and let people write libraries to do whatever they want with the socket, + leave to them the compatibility issues of those options with different platforms.

https://www.man7.org/linux/man-pages/man7/socket.7.html
https://codingrelic.geekhold.com/2009/10/code-snippet-sobindtodevice.html

@dcharbonnier
Copy link
Contributor Author

@dcharbonnier dcharbonnier commented Oct 23, 2020

libuv, net

This issue is not http related, all (TCP at least) sockets suffer the same issue, I'm not having a specific issue with HTTP. I didn't try an Http request but that should make no difference as it's using the same TCP sockets implementation.

@dcharbonnier
Copy link
Contributor Author

@dcharbonnier dcharbonnier commented Oct 23, 2020

Similar to the fs 'open' event we could add

Event: 'open'
fd <integer> Integer file descriptor used by the Socket.

could be emitted here https://github.com/nodejs/node/blob/master/lib/net.js#L248
_handle could be a pipe or a tcp wrap but both of them have a fd.
Sounds like an easy change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.