Skip to content

Commit 126fb04

Browse files
committed
fix: Set reuse flags to allow multiple binds on same port on Linux/Mac
1 parent daac532 commit 126fb04

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ require (
1010
google.golang.org/protobuf v1.36.10
1111
)
1212

13-
require golang.org/x/sys v0.37.0 // indirect
13+
require (
14+
github.com/libp2p/go-reuseport v0.4.0 // indirect
15+
golang.org/x/sys v0.37.0 // indirect
16+
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
22
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3+
github.com/libp2p/go-reuseport v0.4.0 h1:nR5KU7hD0WxXCJbmw7r2rhRYruNRl2koHw8fQscQm2s=
4+
github.com/libp2p/go-reuseport v0.4.0/go.mod h1:ZtI03j/wO5hZVDFo2jKywN6bYKWLOy8Se6DrI2E1cLU=
35
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
46
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
57
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=

pkg/sslnet/multicast_server.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"sync"
77
"time"
88

9+
"github.com/libp2p/go-reuseport"
910
"golang.org/x/net/ipv4"
1011
)
1112

@@ -84,22 +85,31 @@ func (r *MulticastServer) connectToInterface(ifi net.Interface) bool {
8485
return false
8586
}
8687

87-
r.connection, err = net.ListenUDP("udp4", &net.UDPAddr{IP: net.IPv4zero, Port: addr.Port})
88+
// Listen using go-reuseport which sets SO_REUSEADDR and SO_REUSEPORT
89+
listenAddr := &net.UDPAddr{IP: net.IPv4zero, Port: addr.Port}
90+
packetConn, err := reuseport.ListenPacket("udp4", listenAddr.String())
8891
if err != nil {
8992
log.Printf("Could not listen at %v on %v: %v", r.multicastAddress, ifi.Name, err)
9093
return false
9194
}
9295

96+
var ok bool
97+
r.connection, ok = packetConn.(*net.UDPConn)
98+
if !ok {
99+
log.Printf("Could not cast to UDPConn")
100+
return false
101+
}
102+
93103
if err := r.connection.SetReadBuffer(maxDatagramSize); err != nil {
94104
log.Println("Could not set read buffer: ", err)
95105
}
96106

97-
packetConn := ipv4.NewPacketConn(r.connection)
98-
if err := packetConn.JoinGroup(&ifi, addr); err != nil {
107+
ipv4PacketConn := ipv4.NewPacketConn(r.connection)
108+
if err := ipv4PacketConn.JoinGroup(&ifi, addr); err != nil {
99109
log.Printf("Could not join group %v: %v", ifi.Name, err)
100110
return false
101111
}
102-
if err := packetConn.SetMulticastLoopback(true); err != nil {
112+
if err := ipv4PacketConn.SetMulticastLoopback(true); err != nil {
103113
log.Printf("Could not set multicast loopback %v: %v", ifi.Name, err)
104114
return false
105115
}

0 commit comments

Comments
 (0)