Преглед изворни кода

update fd logic for golang 1.9

Darien Raymond пре 8 година
родитељ
комит
896bff2195
2 измењених фајлова са 7 додато и 29 уклоњено
  1. 0 25
      transport/internet/internal/sysfd.go
  2. 7 4
      transport/internet/udp/hub.go

+ 0 - 25
transport/internet/internal/sysfd.go

@@ -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
-}

+ 7 - 4
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())