diff options
Diffstat (limited to 'interfaces')
-rw-r--r-- | interfaces/bindtodevice_bsd.go | 18 | ||||
-rw-r--r-- | interfaces/bindtodevice_linux.go | 9 |
2 files changed, 27 insertions, 0 deletions
diff --git a/interfaces/bindtodevice_bsd.go b/interfaces/bindtodevice_bsd.go new file mode 100644 index 0000000..180c3eb --- /dev/null +++ b/interfaces/bindtodevice_bsd.go @@ -0,0 +1,18 @@ +// +build freebsd openbsd netbsd darwin + +package interfaces + +import ( + "net" + "syscall" +) + +// BindToInterface emulates linux's SO_BINDTODEVICE option for a socket by using +// IP_RECVIF. +func BindToInterface(fd int, ifname string) error { + iface, err := net.InterfaceByName(ifname) + if err != nil { + return err + } + return syscall.SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_RECVIF, iface.Index) +} diff --git a/interfaces/bindtodevice_linux.go b/interfaces/bindtodevice_linux.go new file mode 100644 index 0000000..52c7177 --- /dev/null +++ b/interfaces/bindtodevice_linux.go @@ -0,0 +1,9 @@ +// +build linux + +package interfaces + +import "golang.org/x/sys/unix" + +func BindToInterface(fd int, ifname string) error { + return unix.BindToDevice(fd, ifname) +} |