summaryrefslogtreecommitdiffhomepage
path: root/device/boundif_darwin.go
blob: a93441c85d64673e9a7f237810842729d518b5d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
}