summaryrefslogtreecommitdiffhomepage
path: root/interfaces/bindtodevice_bsd.go
blob: 50e21c69a9c750639e52e4f9e32e1fa4e78a77d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// +build aix freebsd openbsd netbsd darwin

package interfaces

import (
	"net"
	"syscall"
)

// BindToInterface emulates linux's SO_BINDTODEVICE option for a socket by using
// IP_RECVIF.
func BindToInterface(fd int, ifname string) error {
	iface, err := net.InterfaceByName(ifname)
	if err != nil {
		return err
	}
	return syscall.SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_RECVIF, iface.Index)
}