Skip to content

Commit

Permalink
Remove unnecessary use of SO_REUSEPORT
Browse files Browse the repository at this point in the history
Fixes #856.
  • Loading branch information
anacrolix committed Aug 14, 2023
1 parent b730a99 commit cc4a8a4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 29 deletions.
2 changes: 1 addition & 1 deletion fs/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/anacrolix/envpprof v1.2.1
github.com/anacrolix/fuse v0.2.0
github.com/anacrolix/log v0.14.0
github.com/anacrolix/log v0.14.1
github.com/anacrolix/missinggo/v2 v2.7.2-0.20230527121029-a582b4f397b9
github.com/anacrolix/tagflag v1.3.0
github.com/anacrolix/torrent v1.47.1-0.20221102120345-c63f7e1bd720
Expand Down
2 changes: 2 additions & 0 deletions fs/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ github.com/anacrolix/log v0.10.1-0.20220123034749-3920702c17f8/go.mod h1:GmnE2c0
github.com/anacrolix/log v0.13.1/go.mod h1:D4+CvN8SnruK6zIFS/xPoRJmtvtnxs+CSfDQ+BFxZ68=
github.com/anacrolix/log v0.14.0 h1:mYhTSemILe/Z8tIxbGdTIWWpPspI8W/fhZHpoFbDaL0=
github.com/anacrolix/log v0.14.0/go.mod h1:1OmJESOtxQGNMlUO5rcv96Vpp9mfMqXXbe2RdinFLdY=
github.com/anacrolix/log v0.14.1 h1:j2FcIpYZ5FbANetUcm5JNu+zUBGADSp/VbjhUPrAY0k=
github.com/anacrolix/log v0.14.1/go.mod h1:1OmJESOtxQGNMlUO5rcv96Vpp9mfMqXXbe2RdinFLdY=
github.com/anacrolix/lsan v0.0.0-20211126052245-807000409a62 h1:P04VG6Td13FHMgS5ZBcJX23NPC/fiC4cp9bXwYujdYM=
github.com/anacrolix/lsan v0.0.0-20211126052245-807000409a62/go.mod h1:66cFKPCO7Sl4vbFnAaSq7e4OXtdMhRSBagJGWgmpJbM=
github.com/anacrolix/missinggo v0.0.0-20180725070939-60ef2fbf63df/go.mod h1:kwGiTUTZ0+p4vAz3VbAI5a30t2YbvemcmspjKwrAz5s=
Expand Down
68 changes: 40 additions & 28 deletions socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,38 +54,50 @@ var tcpListenConfig = net.ListenConfig{

func listenTcp(network, address string) (s socket, err error) {
l, err := tcpListenConfig.Listen(context.Background(), network, address)
return tcpSocket{
// Dialling TCP from a local port limits us to a single outgoing TCP connection to each remote
// client. Instead, this should be a last resort if we need to use holepunching, and only then to
// connect to other clients that actually try to holepunch TCP.
const dialFromListenPort = false
netDialer := net.Dialer{
// We don't want fallback, as we explicitly manage the IPv4/IPv6 distinction ourselves,
// although it's probably not triggered as I think the network is already constrained to
// tcp4 or tcp6 at this point.
FallbackDelay: -1,
// BitTorrent connections manage their own keepalives.
KeepAlive: tcpListenConfig.KeepAlive,
Control: func(network, address string, c syscall.RawConn) (err error) {
controlErr := c.Control(func(fd uintptr) {
err = setSockNoLinger(fd)
if err != nil {
// Failing to disable linger is undesirable, but not fatal.
log.Levelf(log.Debug, "error setting linger socket option on tcp socket: %v", err)
err = nil
}
// This is no longer required I think, see
// https://github.com/anacrolix/torrent/discussions/856. I added this originally to
// allow dialling out from the client's listen port, but that doesn't really work. I
// think Linux older than ~2013 doesn't support SO_REUSEPORT.
if dialFromListenPort {
err = setReusePortSockOpts(fd)
}
})
if err == nil {
err = controlErr
}
return
},
}
if dialFromListenPort {
netDialer.LocalAddr = l.Addr()
}
s = tcpSocket{
Listener: l,
NetworkDialer: NetworkDialer{
Network: network,
Dialer: &net.Dialer{
// Dialling TCP from a local port limits us to a single outgoing TCP connection to
// each remote client. Instead this should be a last resort if we need to use holepunching, and only then to connect to other clients that actually try to holepunch TCP.
//LocalAddr: l.Addr(),

// We don't want fallback, as we explicitly manage the IPv4/IPv6 distinction
// ourselves, although it's probably not triggered as I think the network is already
// constrained to tcp4 or tcp6 at this point.
FallbackDelay: -1,
// BitTorrent connections manage their own keep-alives.
KeepAlive: tcpListenConfig.KeepAlive,
Control: func(network, address string, c syscall.RawConn) (err error) {
controlErr := c.Control(func(fd uintptr) {
err = setSockNoLinger(fd)
if err != nil {
// Failing to disable linger is undesirable, but not fatal.
log.Printf("error setting linger socket option on tcp socket: %v", err)
}
err = setReusePortSockOpts(fd)
})
if err == nil {
err = controlErr
}
return
},
},
Dialer: &netDialer,
},
}, err
}
return
}

type tcpSocket struct {
Expand Down

0 comments on commit cc4a8a4

Please sign in to comment.