Skip to content

Commit

Permalink
Merge pull request #513 from simonferquel/random-port-support
Browse files Browse the repository at this point in the history
fix mac build
  • Loading branch information
djs55 authored Jan 22, 2021
2 parents e91eca6 + a704e45 commit a5fc5f6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions go/pkg/libproxy/udp_encapsulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type UDPListener interface {
ReadFromUDP(b []byte) (int, *net.UDPAddr, error)
WriteToUDP(b []byte, addr *net.UDPAddr) (int, error)
Close() error
LocalAddr() net.Addr
}

// UDPEncapsulator implements net.Conn and reads and writes UDP datagrams framed within a stream connection
Expand Down
23 changes: 17 additions & 6 deletions go/pkg/vpnkit/forward/vmnet_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"syscall"
"time"

"github.com/moby/vpnkit/go/pkg/libproxy"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -70,15 +71,16 @@ func closeTCPVmnet(IP net.IP, Port uint16, l *net.TCPListener) error {
}
}

func listenUDPVmnet(IP net.IP, Port uint16) (*net.UDPConn, error) {
func listenUDPVmnet(IP net.IP, Port uint16) (libproxy.UDPListener, error) {
localAddress := &net.UDPAddr{
IP: IP,
Port: 0,
}
// I don't think it's possible to make a net.UDPConn from a raw file descriptor
// so we use a hack: we create a net.UDPConn listening on a random port and then
// use the `SyscallConn` low-level interface to replace the file descriptor with a
// clone of the one which is listening on the privileged port.
l, err := net.ListenUDP("udp", &net.UDPAddr{
IP: IP,
Port: 0,
})
l, err := net.ListenUDP("udp", localAddress)
if err != nil {
// IP address invalid? Fail early
return nil, err
Expand All @@ -103,7 +105,16 @@ func listenUDPVmnet(IP net.IP, Port uint16) (*net.UDPConn, error) {
return nil, err
}
_ = syscall.Close(int(newFD))
return l, err
return vmnetdUdpWrapper{l, localAddress}, err
}

type vmnetdUdpWrapper struct {
*net.UDPConn
localAddr *net.UDPAddr
}

func (w vmnetdUdpWrapper) LocalAddr() net.Addr {
return w.localAddr
}

func closeUDPVmnet(IP net.IP, Port uint16, l *net.UDPConn) error {
Expand Down

0 comments on commit a5fc5f6

Please sign in to comment.