Skip to content

Commit d4b282f

Browse files
authored
[interfaces] darwin (#423)
1 parent 427d11c commit d4b282f

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

interfaces/bindtodevice_bsd.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
// +build aix freebsd openbsd netbsd darwin
1+
// +build aix freebsd openbsd netbsd
22

33
package interfaces
44

55
import (
66
"net"
7-
"syscall"
7+
8+
"golang.org/x/sys/unix"
89
)
910

1011
// BindToInterface emulates linux's SO_BINDTODEVICE option for a socket by using

interfaces/bindtodevice_darwin.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// +build darwin
2+
3+
package interfaces
4+
5+
import (
6+
"net"
7+
8+
"golang.org/x/sys/unix"
9+
)
10+
11+
// BindToInterface emulates linux's SO_BINDTODEVICE option for a socket by using
12+
// IP_BOUND_IF.
13+
func BindToInterface(fd int, ifname string) error {
14+
iface, err := net.InterfaceByName(ifname)
15+
if err != nil {
16+
return err
17+
}
18+
return unix.SetsockoptInt(fd, unix.IPPROTO_IP, unix.IP_BOUND_IF, iface.Index)
19+
}

0 commit comments

Comments
 (0)