interfaces/bindtodevice_darwin.go
raw| 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 | } |