helper_windows.go 1.2 KB

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