2019-03-03 04:01:06 +00:00
|
|
|
/* SPDX-License-Identifier: MIT
|
|
|
|
*
|
2020-05-02 08:08:26 +00:00
|
|
|
* Copyright (C) 2017-2020 WireGuard LLC. All Rights Reserved.
|
2019-03-03 04:01:06 +00:00
|
|
|
*/
|
|
|
|
|
2019-11-07 16:13:05 +00:00
|
|
|
package conn
|
2019-03-03 04:01:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"unsafe"
|
2019-05-14 07:09:52 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
2019-03-03 04:01:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
sockoptIP_UNICAST_IF = 31
|
|
|
|
sockoptIPV6_UNICAST_IF = 31
|
|
|
|
)
|
|
|
|
|
2019-11-07 16:13:05 +00:00
|
|
|
func (bind *nativeBind) BindSocketToInterface4(interfaceIndex uint32, blackhole bool) error {
|
2019-03-03 04:01:06 +00:00
|
|
|
/* 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]))
|
|
|
|
|
2019-11-07 16:13:05 +00:00
|
|
|
sysconn, err := bind.ipv4.SyscallConn()
|
2019-03-03 04:01:06 +00:00
|
|
|
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
|
|
|
|
}
|
2019-11-07 16:13:05 +00:00
|
|
|
bind.blackhole4 = blackhole
|
2019-03-03 04:01:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-07 16:13:05 +00:00
|
|
|
func (bind *nativeBind) BindSocketToInterface6(interfaceIndex uint32, blackhole bool) error {
|
|
|
|
sysconn, err := bind.ipv6.SyscallConn()
|
2019-03-03 04:01:06 +00:00
|
|
|
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
|
|
|
|
}
|
2019-11-07 16:13:05 +00:00
|
|
|
bind.blackhole6 = blackhole
|
2019-03-03 04:01:06 +00:00
|
|
|
return nil
|
2019-03-21 20:43:04 +00:00
|
|
|
}
|