Skip to content

Commit

Permalink
net/tcp: don't use MD5 signature and bind to device on non-Linux OS
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentbernat authored and BarbarossaTM committed Jul 30, 2023
1 parent 3f5d7cf commit f71bc38
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion net/tcp/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (lf *ListenerFactory) NewListener(v *vrf.VRF, laddr *net.TCPAddr, ttl uint8
}

if v.Name() != vrf.DefaultVRFName {
err = unix.SetsockoptString(fd, SOL_IP, unix.SO_BINDTODEVICE, v.Name())
err = bindToDev(fd, v.Name())
if err != nil {
unix.Close(fd)
return nil, fmt.Errorf("unable to set SO_BINDTODEVICE (%s): %v", v.Name(), err)
Expand Down
2 changes: 2 additions & 0 deletions net/tcp/md5sig.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build linux

package tcp

import (
Expand Down
12 changes: 12 additions & 0 deletions net/tcp/md5sig_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build !linux

package tcp

import (
"fmt"
"net"
)

func setTCPMD5Option(fd int, addr net.IP, md5secret string) error {
return fmt.Errorf("setting md5 is not supported")
}
2 changes: 1 addition & 1 deletion net/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (c *Conn) SetNoDelay() error {

// SetBindToDev sets the SO_BINDTODEVICE option
func (c *Conn) SetBindToDev(devName string) error {
return unix.SetsockoptString(c.fd, unix.IPPROTO_TCP, unix.SO_BINDTODEVICE, devName)
return bindToDev(c.fd, devName)
}

// MockConn is mocked TCP connection
Expand Down
10 changes: 10 additions & 0 deletions net/tcp/tcp_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build linux

package tcp

import "golang.org/x/sys/unix"

// bindToDev sets the SO_BINDTODEVICE option
func bindToDev(fd int, devName string) error {
return unix.SetsockoptString(fd, unix.IPPROTO_TCP, unix.SO_BINDTODEVICE, devName)
}
10 changes: 10 additions & 0 deletions net/tcp/tcp_stub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build !linux

package tcp

import "fmt"

// bindToDev sets the SO_BINDTODEVICE option
func bindToDev(fd int, devName string) error {
return fmt.Errorf("binding to device is not supported")
}

0 comments on commit f71bc38

Please sign in to comment.