helper_unix.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2013 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
  5. package ipv6
  6. import (
  7. "net"
  8. "reflect"
  9. )
  10. func (c *genericOpt) sysfd() (int, error) {
  11. switch p := c.Conn.(type) {
  12. case *net.TCPConn, *net.UDPConn, *net.IPConn:
  13. return sysfd(p)
  14. }
  15. return 0, errInvalidConnType
  16. }
  17. func (c *dgramOpt) sysfd() (int, error) {
  18. switch p := c.PacketConn.(type) {
  19. case *net.UDPConn, *net.IPConn:
  20. return sysfd(p.(net.Conn))
  21. }
  22. return 0, errInvalidConnType
  23. }
  24. func (c *payloadHandler) sysfd() (int, error) {
  25. return sysfd(c.PacketConn.(net.Conn))
  26. }
  27. func sysfd(c net.Conn) (int, error) {
  28. cv := reflect.ValueOf(c)
  29. switch ce := cv.Elem(); ce.Kind() {
  30. case reflect.Struct:
  31. nfd := ce.FieldByName("conn").FieldByName("fd")
  32. switch fe := nfd.Elem(); fe.Kind() {
  33. case reflect.Struct:
  34. fd := fe.FieldByName("sysfd")
  35. return int(fd.Int()), nil
  36. }
  37. }
  38. return 0, errInvalidConnType
  39. }