From 896bff2195768e3274b57118b1cd4b545aa8d072 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Fri, 25 Aug 2017 21:42:07 +0200 Subject: [PATCH] update fd logic for golang 1.9 --- transport/internet/internal/sysfd.go | 25 ------------------------- transport/internet/udp/hub.go | 11 +++++++---- 2 files changed, 7 insertions(+), 29 deletions(-) delete mode 100644 transport/internet/internal/sysfd.go diff --git a/transport/internet/internal/sysfd.go b/transport/internet/internal/sysfd.go deleted file mode 100644 index d2720aed2..000000000 --- a/transport/internet/internal/sysfd.go +++ /dev/null @@ -1,25 +0,0 @@ -package internal - -import ( - "net" - "reflect" -) - -var ( - errInvalidConn = newError("not a net.Conn") -) - -// GetSysFd returns the underlying fd of a connection. -func GetSysFd(conn net.Conn) (int, error) { - cv := reflect.ValueOf(conn) - switch ce := cv.Elem(); ce.Kind() { - case reflect.Struct: - netfd := ce.FieldByName("conn").FieldByName("fd") - switch fe := netfd.Elem(); fe.Kind() { - case reflect.Struct: - fd := fe.FieldByName("sysfd") - return int(fd.Int()), nil - } - } - return 0, errInvalidConn -} diff --git a/transport/internet/udp/hub.go b/transport/internet/udp/hub.go index 4fb129f79..6e76ab896 100644 --- a/transport/internet/udp/hub.go +++ b/transport/internet/udp/hub.go @@ -8,7 +8,6 @@ import ( "v2ray.com/core/common/buf" "v2ray.com/core/common/dice" v2net "v2ray.com/core/common/net" - "v2ray.com/core/transport/internet/internal" ) // Payload represents a single UDP payload. @@ -94,13 +93,17 @@ func ListenUDP(address v2net.Address, port v2net.Port, option ListenOption) (*Hu } log.Trace(newError("listening UDP on ", address, ":", port)) if option.ReceiveOriginalDest { - fd, err := internal.GetSysFd(udpConn) + rawConn, err := udpConn.SyscallConn() if err != nil { return nil, newError("failed to get fd").Base(err) } - err = SetOriginalDestOptions(fd) + err = rawConn.Control(func(fd uintptr) { + if err := SetOriginalDestOptions(int(fd)); err != nil { + log.Trace(newError("failed to set socket options").Base(err)) + } + }) if err != nil { - return nil, newError("failed to set socket options").Base(err) + return nil, newError("failed to control socket").Base(err) } } ctx, cancel := context.WithCancel(context.Background())