aboutsummaryrefslogtreecommitdiffstats
path: root/device/boundif_windows.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_windows.go
parentglobal: begin modularization (diff)
downloadwireguard-go-b8e85267cf22528a96cefba5f86bac5958ce0c58.tar.xz
wireguard-go-b8e85267cf22528a96cefba5f86bac5958ce0c58.zip
boundif: introduce API for socket binding
Diffstat (limited to 'device/boundif_windows.go')
-rw-r--r--device/boundif_windows.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/device/boundif_windows.go b/device/boundif_windows.go
new file mode 100644
index 0000000..00631cb
--- /dev/null
+++ b/device/boundif_windows.go
@@ -0,0 +1,56 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2017-2019 WireGuard LLC. All Rights Reserved.
+ */
+
+package device
+
+import (
+ "encoding/binary"
+ "golang.org/x/sys/windows"
+ "unsafe"
+)
+
+const (
+ sockoptIP_UNICAST_IF = 31
+ sockoptIPV6_UNICAST_IF = 31
+)
+
+func (device *Device) BindSocketToInterface4(interfaceIndex uint32) error {
+ /* MSDN says for IPv4 this needs to be in net byte order, so that it's like an IP address with leading zeros. */
+ bytes := make([]byte, 4)
+ binary.BigEndian.PutUint32(bytes, interfaceIndex)
+ interfaceIndex = *(*uint32)(unsafe.Pointer(&bytes[0]))
+
+ sysconn, err := device.net.bind.(*nativeBind).ipv4.SyscallConn()
+ if err != nil {
+ return err
+ }
+ err2 := sysconn.Control(func(fd uintptr) {
+ err = windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IP, sockoptIP_UNICAST_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).ipv6.SyscallConn()
+ if err != nil {
+ return err
+ }
+ err2 := sysconn.Control(func(fd uintptr) {
+ err = windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_IPV6, sockoptIPV6_UNICAST_IF, int(interfaceIndex))
+ })
+ if err2 != nil {
+ return err2
+ }
+ if err != nil {
+ return err
+ }
+ return nil
+} \ No newline at end of file