-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathsocket_bsd.go
63 lines (57 loc) · 1.36 KB
/
socket_bsd.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//go:build darwin || netbsd || freebsd || openbsd || dragonfly
package sonic
import (
"fmt"
"net"
"syscall"
)
// BindToDevice binds the socket to the device with the given name. The device is a network interface (`ip link` to see
// all interfaces).
//
// This makes it such that only packets from the given device will be processed by the socket.
func (s *Socket) BindToDevice(name string) (*net.Interface, error) {
iff, err := net.InterfaceByName(name)
if err != nil {
return nil, err
}
if s.domain == SocketDomainIPv4 {
if err := syscall.SetsockoptInt(
s.fd,
syscall.IPPROTO_IP,
syscall.IP_BOUND_IF,
iff.Index,
); err != nil {
return nil, err
} else {
s.boundInterface = iff
return iff, nil
}
} else {
return nil, fmt.Errorf("cannot yet bind to device when domain is ipv6")
}
}
// UnbindFromDevice is not working, and honestly I have no clue why.
func (s *Socket) UnbindFromDevice() error {
if s.boundInterface == nil {
return nil
}
if s.domain == SocketDomainIPv4 {
_, _, errno := syscall.Syscall6(
uintptr(syscall.SYS_SETSOCKOPT),
uintptr(s.fd),
uintptr(syscall.IPPROTO_IP),
uintptr(syscall.IP_BOUND_IF),
0, 0, 0,
)
if errno != 0 {
var err error
err = errno
return err
} else {
s.boundInterface = nil
return nil
}
} else {
return fmt.Errorf("cannot yet bind to device when domain is ipv6")
}
}