Skip to content

Commit

Permalink
Fix error discarding and clobbering in Control funcs
Browse files Browse the repository at this point in the history
Both sockopt and control errors can't exist at the same time, but control errors should occur first, and so are given precedence.

Fixes libp2p#101
  • Loading branch information
anacrolix committed Apr 25, 2023
1 parent 194b8a8 commit 3e8ab90
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
14 changes: 6 additions & 8 deletions control_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ import (
"golang.org/x/sys/unix"
)

func Control(network, address string, c syscall.RawConn) error {
var err error
c.Control(func(fd uintptr) {
func Control(network, address string, c syscall.RawConn) (err error) {
controlErr := c.Control(func(fd uintptr) {
err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
if err != nil {
return
}

err = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
if err != nil {
return
}
})
return err
if controlErr != nil {
err = controlErr
}
return
}
6 changes: 5 additions & 1 deletion control_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import (
)

func Control(network, address string, c syscall.RawConn) (err error) {
return c.Control(func(fd uintptr) {
controlErr := c.Control(func(fd uintptr) {
err = windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_REUSEADDR, 1)
})
if controlErr != nil {
err = controlErr
}
return
}

0 comments on commit 3e8ab90

Please sign in to comment.