summaryrefslogtreecommitdiffhomepage
path: root/device/boundif_darwin.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2019-03-03 05:01:06 +0100
committerJason A. Donenfeld <Jason@zx2c4.com>2019-03-04 16:37:11 +0100
commitb8e85267cf22528a96cefba5f86bac5958ce0c58 (patch)
treed6441205ba32a2ea5cb6f8c5dcba80a6387024dd /device/boundif_darwin.go
parent69f0fe67b63d90e523a5a1241fb1b46c2e8dbe03 (diff)
boundif: introduce API for socket binding
Diffstat (limited to 'device/boundif_darwin.go')
-rw-r--r--device/boundif_darwin.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/device/boundif_darwin.go b/device/boundif_darwin.go
new file mode 100644
index 0000000..b3d10ba
--- /dev/null
+++ b/device/boundif_darwin.go
@@ -0,0 +1,44 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
+ */
+
+package device
+
+import (
+ "golang.org/x/sys/unix"
+)
+
+func (device *Device) BindSocketToInterface4(interfaceIndex uint32) error {
+ sysconn, err := device.net.bind.(*nativeBind).ipv4.SyscallConn()
+ if err != nil {
+ return nil
+ }
+ err2 := sysconn.Control(func(fd uintptr) {
+ err = unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_BOUND_IF, int(interfaceIndex))
+ })
+ if err2 != nil {
+ return err2
+ }
+ if err != nil {
+ return err
+ }
+ return nil
+}
+
+func (device *Device) BindSocketToInterface6(interfaceIndex uint32) error {
+ sysconn, err := device.net.bind.(*nativeBind).ipv4.SyscallConn()
+ if err != nil {
+ return nil
+ }
+ err2 := sysconn.Control(func(fd uintptr) {
+ err = unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, int(interfaceIndex))
+ })
+ if err2 != nil {
+ return err2
+ }
+ if err != nil {
+ return err
+ }
+ return nil
+} \ No newline at end of file