1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-08 11:17:44 -05:00
v2fly/transport/internet/internal/sysfd.go
2017-01-01 22:12:44 +01:00

28 lines
542 B
Go

package internal
import (
"net"
"reflect"
"v2ray.com/core/common/errors"
)
var (
errInvalidConn = errors.New("Invalid Connection.")
)
// 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
}