How do I obtain the underlying syscall.Handle for a *net.UDPConn on Windows? I want this handle to set the 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‘s File() fails with:
04:24:49 main.go:150: dup: not supported by windows
And in the source code is marked as a to-do. How can I get this handle? Is there some other way to set the TTL if not?
Update0
I’ve submitted the shortcomings to the Go issue tracker:
The short answer is impossible. But since that isn’t an answer you want to hear, I will give you the right way and wrong way to solve the problem.
The right way:
dup()for Windows.Obviously the right way has some issues… but I highly recommend doing it. Go needs windows developers to fix up these types of serious problems. The only reason this can’t be done in Windows is no one implemented the function
The wrong way:
Until the patch you write gets accepted and released, you can fake it through unsafe. The way the following code works by mirroring the exact structure of a
net.UDPConn. This included copying over all structs from net that make up aUDPConn. Thenunsafeis used to assert that the localUDPConnis the same as net’sUDPConn. The compiler can not check this and takes your word for it. Were the internals ofnetto ever change, it would compile but god knows what it would do.All code is untested.