github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/conn/sockaddr_notlinuxsolariswindows.go (about) 1 //go:build aix || darwin || dragonfly || freebsd || netbsd || openbsd || zos 2 3 package conn 4 5 import ( 6 "net/netip" 7 "unsafe" 8 9 "golang.org/x/sys/unix" 10 ) 11 12 func AddrPortToSockaddrValue(addrPort netip.AddrPort) (rsa6 unix.RawSockaddrInet6, namelen uint32) { 13 addr, port := addrPort.Addr(), addrPort.Port() 14 p := (*[2]byte)(unsafe.Pointer(&rsa6.Port)) 15 p[0] = byte(port >> 8) 16 p[1] = byte(port) 17 if addr.Is4() { 18 rsa6.Len = unix.SizeofSockaddrInet4 19 rsa6.Family = unix.AF_INET 20 a := (*[4]byte)(unsafe.Pointer(&rsa6.Flowinfo)) 21 *a = addr.As4() 22 namelen = unix.SizeofSockaddrInet4 23 return 24 } 25 rsa6.Len = unix.SizeofSockaddrInet6 26 rsa6.Family = unix.AF_INET6 27 rsa6.Addr = addr.As16() 28 namelen = unix.SizeofSockaddrInet6 29 return 30 } 31 32 func AddrPortToSockaddrInet4(addrPort netip.AddrPort) unix.RawSockaddrInet4 { 33 addr := addrPort.Addr() 34 port := addrPort.Port() 35 rsa4 := unix.RawSockaddrInet4{ 36 Len: unix.SizeofSockaddrInet4, 37 Family: unix.AF_INET, 38 Addr: addr.As4(), 39 } 40 p := (*[2]byte)(unsafe.Pointer(&rsa4.Port)) 41 p[0] = byte(port >> 8) 42 p[1] = byte(port) 43 return rsa4 44 } 45 46 func AddrPortToSockaddrInet6(addrPort netip.AddrPort) unix.RawSockaddrInet6 { 47 addr := addrPort.Addr() 48 port := addrPort.Port() 49 rsa6 := unix.RawSockaddrInet6{ 50 Len: unix.SizeofSockaddrInet6, 51 Family: unix.AF_INET6, 52 Addr: addr.As16(), 53 } 54 p := (*[2]byte)(unsafe.Pointer(&rsa6.Port)) 55 p[0] = byte(port >> 8) 56 p[1] = byte(port) 57 return rsa6 58 }