mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 17:35:40 +08:00
22 lines
537 B
Go
22 lines
537 B
Go
package sockopt
|
|
|
|
import (
|
|
"net"
|
|
"syscall"
|
|
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
func BindControl(iface *net.Interface) func(network, address string, c syscall.RawConn) error {
|
|
return func(network, address string, c syscall.RawConn) (err error) {
|
|
return c.Control(func(fd uintptr) {
|
|
switch network {
|
|
case "tcp4", "udp4":
|
|
err = unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_BOUND_IF, iface.Index)
|
|
case "tcp6", "udp6":
|
|
err = unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, iface.Index)
|
|
}
|
|
})
|
|
}
|
|
}
|