summaryrefslogtreecommitdiffhomepage
path: root/interfaces
diff options
context:
space:
mode:
authorAnatole Denis <natolumin@unverle.fr>2019-09-17 11:41:54 +0200
committerAnatole Denis <natolumin@unverle.fr>2019-09-17 16:53:03 +0200
commiteac77666371b0272d980e9ea739cc890957971a9 (patch)
tree650026e5a6b8869e87faa732276953b27d0b4232 /interfaces
parentc20a7603ebe7f3246901e85d22ccc6476bcfbc96 (diff)
dhcpv4: Move BindToInterface to interfaces package
This moves the implementations of the BindToInterface to the interfaces/ package, since they aren't ipv4-specific. The BindToInterface function remains in dhcpv4 (simply wraps the one in interfaces) to keep backwards-compatibility Additionally, fold bindtodevice_darwin into bindtodevice_bsd: darwin is mostly a BSD, and happens to support IP_RECVIF, so use that instead of IP_BOUND_IF, which only affects sends, not receives according to the code comments in bsd/netinet/ip_output.c as well as being v4-only Signed-off-by: Anatole Denis <natolumin@unverle.fr>
Diffstat (limited to 'interfaces')
-rw-r--r--interfaces/bindtodevice_bsd.go18
-rw-r--r--interfaces/bindtodevice_linux.go9
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)
+}