Get syscall.Handle from Go * net.UDPConn on Windows?

How to get base syscall.Handle for *net.UDPConn on Windows? I want this handle to set IP_MULTICAST_TTL via syscall.SetsockoptInt . On Linux, I do the following:

 func setTTL(conn *net.UDPConn, ttl int) error { f, err := conn.File() if err != nil { return err } defer f.Close() fd := int(f.Fd()) return syscall.SetsockoptInt(fd, syscall.SOL_IP, syscall.IP_MULTICAST_TTL, ttl) } 

But on Windows, the implicit dup inside *net.UDPConn File() does not work:

04:24:49 main.go: 150: dup: not supported by windows

And in the source code is marked as given. How can I get this handle? Is there any other way to set TTL if not?

Update0

I introduced the flaws to the Go tracker:

+7
source share
1 answer

A short answer is impossible. But since this is not the answer you want to hear, I will give you the right way and the wrong way to solve the problem.

The right way:

  • implement dup() for Windows.
  • submit to Go to change list
  • wait for his release to use it

Obviously, the right way has some problems ... but I highly recommend doing it. Go requires window developers to fix these serious issues. The only reason this cannot be done on Windows is not implemented by the function

Wrong Way:

Until the patch you write is accepted and released, you can fake it using unsafe. How the following code works, mirroring the exact structure of net.UDPConn . This included copying all the structures from the network that make up the UDPConn . Then unsafe used to claim that the local UDPConn matches the UDPConn network. The compiler cannot verify this and takes your word. If the internals of net had ever changed, this would compile, but God knew what he would do.

All code is not verified.

 package reallyunsafenet import ( "net" "sync" "syscall" "unsafe" ) // copied from go/src/pkg/net/fd_windows.go type ioResult struct { qty uint32 err error } // copied from go/src/pkg/net/fd_windows.go type netFD struct { // locking/lifetime of sysfd sysmu sync.Mutex sysref int closing bool // immutable until Close sysfd syscall.Handle family int sotype int isConnected bool net string laddr net.Addr raddr net.Addr resultc [2]chan ioResult errnoc [2]chan error // owned by client rdeadline int64 rio sync.Mutex wdeadline int64 wio sync.Mutex } // copied from go/src/pkg/net/udpsock_posix.go type UDPConn struct { fd *netFD } // function to get fd func GetFD(conn *net.UDPConn) syscall.Handle { c := (*UDPConn)(unsafe.Pointer(conn)) return c.fd.sysfd } 
+10
source

All Articles