Closed yyk808 closed 5 months ago
The option --reuse-port
corresponds to SO_REUSE_PORT
socket option, which is only meaningful for a listening socket. But I'll look to provide a way to change socket options before binding/connecting a client-side socket, if that could be helpful in your case.
@yyk808 In the latest commit cae19357ed6c73e623653e4c50ea3acb0d6bb3ed a new state 'open' is added to connect() filter's callback, where you can do setsockopt() right after a socket is open and before anything else, like:
var SOL_SOCKET = 1
var SO_REUSEPORT = 15
pipeline($=>$
.connect('x.x.x.x:nnn', {
onState: inb => {
if (inb.state === 'open') {
inb.socket.setRawOption(SOL_SOCKET, SO_REUSEPORT, new Data([1]))
}
}
})
)
@yyk808 The last change for adding an 'open'
state to an Outbound had a bug that caused a crash. The issue was just fixed in commit d36b333d910ed6886e89b1c105924fec344ebec8. You'll probably need an update as you use the onState
callback.
Edit
After reading source code in
connect.cpp
andlistener.cpp
, I found that the option--reuse-port
is only set for Listener sockets. When theconnect
filter try to bind the same address without this option, the kernel would refuse. Well, needless to say, this is a corner case indeed. However I DO need this to do something.I'm trying to implement NAT traversal in pure PJS, which need to take full control of socket, so that I can cheat the firewall and punch a hole through NAT. Using a specific pair to send something then open a server on it is one of the fundamental step. (
For more information about NAT traversal, go check this!)
So I reopen this issue and expecting someone can help.
Issue
First I have a tcp server running on port 8080. And then I wrote some code in
test.js
Run it with option
--reuse-port
:I wonder if it's the right way to use
--reuse-port
, but I do need a way to use the same port for both inbound and outbound connections in the same time.Furthermore, I'm expecting a feature in
connect
orlisten
like:So the socket option can be separately set.