udp: fix endianness for port (#1194)

If the host OS is already big endian, we were swapping bytes when we
shouldn't have. Use the Go helper to make sure we do the endianness
correctly

Fixes: #1189
This commit is contained in:
Wade Simmons 2024-08-14 12:53:00 -04:00 committed by GitHub
parent 248cf194cd
commit 0736cfa562
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 2 additions and 6 deletions

View File

@ -218,9 +218,7 @@ func (u *StdConn) writeTo6(b []byte, ip netip.AddrPort) error {
var rsa unix.RawSockaddrInet6 var rsa unix.RawSockaddrInet6
rsa.Family = unix.AF_INET6 rsa.Family = unix.AF_INET6
rsa.Addr = ip.Addr().As16() rsa.Addr = ip.Addr().As16()
port := ip.Port() binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&rsa.Port))[:], ip.Port())
// Little Endian -> Network Endian
rsa.Port = (port >> 8) | ((port & 0xff) << 8)
for { for {
_, _, err := unix.Syscall6( _, _, err := unix.Syscall6(
@ -251,9 +249,7 @@ func (u *StdConn) writeTo4(b []byte, ip netip.AddrPort) error {
var rsa unix.RawSockaddrInet4 var rsa unix.RawSockaddrInet4
rsa.Family = unix.AF_INET rsa.Family = unix.AF_INET
rsa.Addr = ip.Addr().As4() rsa.Addr = ip.Addr().As4()
port := ip.Port() binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&rsa.Port))[:], ip.Port())
// Little Endian -> Network Endian
rsa.Port = (port >> 8) | ((port & 0xff) << 8)
for { for {
_, _, err := unix.Syscall6( _, _, err := unix.Syscall6(