diff --git a/conn/boundif_android.go b/conn/boundif_android.go new file mode 100644 index 0000000..3e10607 --- /dev/null +++ b/conn/boundif_android.go @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved. + */ + +package conn + +func (bind *nativeBind) PeekLookAtSocketFd4() (fd int, err error) { + sysconn, err := bind.ipv4.SyscallConn() + if err != nil { + return -1, err + } + err = sysconn.Control(func(f uintptr) { + fd = int(f) + }) + if err != nil { + return -1, err + } + return +} + +func (bind *nativeBind) PeekLookAtSocketFd6() (fd int, err error) { + sysconn, err := bind.ipv6.SyscallConn() + if err != nil { + return -1, err + } + err = sysconn.Control(func(f uintptr) { + fd = int(f) + }) + if err != nil { + return -1, err + } + return +} diff --git a/conn/conn.go b/conn/conn.go index 6e04386..c0ca3b8 100644 --- a/conn/conn.go +++ b/conn/conn.go @@ -57,6 +57,13 @@ type BindSocketToInterface interface { BindSocketToInterface6(interfaceIndex uint32, blackhole bool) error } +// PeekLookAtSocketFd is implemented by Bind objects that support having their +// file descriptor peeked at. +type PeekLookAtSocketFd interface { + PeekLookAtSocketFd4() (fd int, err error) + PeekLookAtSocketFd6() (fd int, err error) +} + // An Endpoint maintains the source/destination caching for a peer. // // dst : the remote address of a peer ("endpoint" in uapi terminology) diff --git a/device/bindsocketshim.go b/device/bindsocketshim.go index 896c7d2..68e1504 100644 --- a/device/bindsocketshim.go +++ b/device/bindsocketshim.go @@ -34,3 +34,27 @@ func (device *Device) BindSocketToInterface6(interfaceIndex uint32, blackhole bo } return nil } + +// TODO(crawshaw): this method is a compatibility shim. Replace with direct use of conn. +func (device *Device) PeekLookAtSocketFd4() (fd int, err error) { + if device.net.bind == nil { + return -1, errors.New("Bind is not yet initialized") + } + + if iface, ok := device.net.bind.(conn.PeekLookAtSocketFd); ok { + return iface.PeekLookAtSocketFd4() + } + return -1, errors.New("unimplemented") +} + +// TODO(crawshaw): this method is a compatibility shim. Replace with direct use of conn. +func (device *Device) PeekLookAtSocketFd6() (fd int, err error) { + if device.net.bind == nil { + return -1, errors.New("Bind is not yet initialized") + } + + if iface, ok := device.net.bind.(conn.PeekLookAtSocketFd); ok { + return iface.PeekLookAtSocketFd6() + } + return -1, errors.New("unimplemented") +} diff --git a/device/boundif_android.go b/device/boundif_android.go deleted file mode 100644 index a4be8de..0000000 --- a/device/boundif_android.go +++ /dev/null @@ -1,44 +0,0 @@ -/* SPDX-License-Identifier: MIT - * - * Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved. - */ - -package device - -import "errors" - -func (device *Device) PeekLookAtSocketFd4() (fd int, err error) { - nb, ok := device.net.bind.(*nativeBind) - if !ok { - return 0, errors.New("no socket exists") - } - sysconn, err := nb.ipv4.SyscallConn() - if err != nil { - return - } - err = sysconn.Control(func(f uintptr) { - fd = int(f) - }) - if err != nil { - return - } - return -} - -func (device *Device) PeekLookAtSocketFd6() (fd int, err error) { - nb, ok := device.net.bind.(*nativeBind) - if !ok { - return 0, errors.New("no socket exists") - } - sysconn, err := nb.ipv6.SyscallConn() - if err != nil { - return - } - err = sysconn.Control(func(f uintptr) { - fd = int(f) - }) - if err != nil { - return - } - return -}