github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/bufio/addr_windows.go (about)

     1  package bufio
     2  
     3  import (
     4  	"encoding/binary"
     5  	"net/netip"
     6  	"unsafe"
     7  
     8  	"golang.org/x/sys/windows"
     9  )
    10  
    11  func ToSockaddr(destination netip.AddrPort) (name unsafe.Pointer, nameLen int32) {
    12  	if destination.Addr().Is4() {
    13  		sa := windows.RawSockaddrInet4{
    14  			Family: windows.AF_INET,
    15  			Addr:   destination.Addr().As4(),
    16  		}
    17  		binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&sa.Port))[:], destination.Port())
    18  		name = unsafe.Pointer(&sa)
    19  		nameLen = int32(unsafe.Sizeof(sa))
    20  	} else {
    21  		sa := windows.RawSockaddrInet6{
    22  			Family: windows.AF_INET6,
    23  			Addr:   destination.Addr().As16(),
    24  		}
    25  		binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&sa.Port))[:], destination.Port())
    26  		name = unsafe.Pointer(&sa)
    27  		nameLen = int32(unsafe.Sizeof(sa))
    28  	}
    29  	return
    30  }