socket_posix.go 710 B

123456789101112131415161718192021222324252627282930
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build darwin dragonfly freebsd linux netbsd openbsd solaris windows
  5. package netreflect
  6. import (
  7. "net"
  8. "reflect"
  9. "runtime"
  10. )
  11. func socketOf(c net.Conn) (uintptr, error) {
  12. v := reflect.ValueOf(c)
  13. switch e := v.Elem(); e.Kind() {
  14. case reflect.Struct:
  15. fd := e.FieldByName("conn").FieldByName("fd")
  16. switch e := fd.Elem(); e.Kind() {
  17. case reflect.Struct:
  18. sysfd := e.FieldByName("sysfd")
  19. if runtime.GOOS == "windows" {
  20. return uintptr(sysfd.Uint()), nil
  21. }
  22. return uintptr(sysfd.Int()), nil
  23. }
  24. }
  25. return 0, errInvalidType
  26. }