Add control conn func

This commit is contained in:
世界 2022-08-16 17:47:48 +08:00
parent 9719bdcd4a
commit fb82be7f3f
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
8 changed files with 18 additions and 10 deletions

View file

@ -44,7 +44,7 @@ func BindToInterfaceIndexFunc(interfaceIndexFunc func() int) Func {
}
func bindToInterface(conn syscall.RawConn, network string, index int) error {
return Control(conn, func(fd uintptr) error {
return Raw(conn, func(fd uintptr) error {
switch network {
case "tcp6", "udp6":
return unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, index)

View file

@ -10,7 +10,7 @@ func NewBindManager() BindManager {
func BindToInterface(manager BindManager, interfaceName string) Func {
return func(network, address string, conn syscall.RawConn) error {
return Control(conn, func(fd uintptr) error {
return Raw(conn, func(fd uintptr) error {
return syscall.BindToDevice(int(fd), interfaceName)
})
}
@ -22,7 +22,7 @@ func BindToInterfaceFunc(manager BindManager, interfaceNameFunc func() string) F
if interfaceName == "" {
return nil
}
return Control(conn, func(fd uintptr) error {
return Raw(conn, func(fd uintptr) error {
return syscall.BindToDevice(int(fd), interfaceName)
})
}

View file

@ -37,7 +37,7 @@ func bindInterfaceIndex(network string, address string, conn syscall.RawConn, in
return err
}
}
return Control(conn, func(fd uintptr) error {
return Raw(conn, func(fd uintptr) error {
handle := syscall.Handle(fd)
// handle ip empty, e.g. net.Listen("udp", ":0")
if ipStr == "" {

View file

@ -22,9 +22,17 @@ func Append(oldFunc Func, newFunc Func) Func {
}
}
func Control(conn syscall.RawConn, block func(fd uintptr) error) error {
func Conn(conn syscall.Conn, block func(fd uintptr) error) error {
rawConn, err := conn.SyscallConn()
if err != nil {
return err
}
return Raw(rawConn, block)
}
func Raw(rawConn syscall.RawConn, block func(fd uintptr) error) error {
var innerErr error
err := conn.Control(func(fd uintptr) {
err := rawConn.Control(func(fd uintptr) {
innerErr = block(fd)
})
return E.Errors(innerErr, err)

View file

@ -6,7 +6,7 @@ import (
func RoutingMark(mark int) Func {
return func(network, address string, conn syscall.RawConn) error {
return Control(conn, func(fd uintptr) error {
return Raw(conn, func(fd uintptr) error {
return syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, mark)
})
}

View file

@ -34,7 +34,7 @@ func sendAncillaryFileDescriptors(protectPath string, fileDescriptors []int) err
func ProtectPath(protectPath string) Func {
return func(network, address string, conn syscall.RawConn) error {
return Control(conn, func(fd uintptr) error {
return Raw(conn, func(fd uintptr) error {
return sendAncillaryFileDescriptors(protectPath, []int{int(fd)})
})
}

View file

@ -12,7 +12,7 @@ import (
func ReuseAddr() Func {
return func(network, address string, conn syscall.RawConn) error {
return Control(conn, func(fd uintptr) error {
return Raw(conn, func(fd uintptr) error {
return E.Errors(
unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1),
unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1),

View file

@ -6,7 +6,7 @@ import (
func ReuseAddr() Func {
return func(network, address string, conn syscall.RawConn) error {
return Control(conn, func(fd uintptr) error {
return Raw(conn, func(fd uintptr) error {
return syscall.SetsockoptInt(syscall.Handle(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
})
}