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

     1  package bufio
     2  
     3  import (
     4  	"encoding/binary"
     5  	"net/netip"
     6  	"unsafe"
     7  
     8  	"golang.org/x/sys/unix"
     9  )
    10  
    11  func ToSockaddr(destination netip.AddrPort) (name unsafe.Pointer, nameLen uint32) {
    12  	if destination.Addr().Is4() {
    13  		sa := unix.RawSockaddrInet4{
    14  			Family: unix.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 = unix.SizeofSockaddrInet4
    20  	} else {
    21  		sa := unix.RawSockaddrInet6{
    22  			Family: unix.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 = unix.SizeofSockaddrInet6
    28  	}
    29  	return
    30  }