Skip to content

Commit

Permalink
udp: fix endianness for port (#1194)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
wadey authored Aug 14, 2024
1 parent 248cf19 commit 0736cfa
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions udp/udp_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,7 @@ func (u *StdConn) writeTo6(b []byte, ip netip.AddrPort) error {
var rsa unix.RawSockaddrInet6
rsa.Family = unix.AF_INET6
rsa.Addr = ip.Addr().As16()
port := ip.Port()
// Little Endian -> Network Endian
rsa.Port = (port >> 8) | ((port & 0xff) << 8)
binary.BigEndian.PutUint16((*[2]byte)(unsafe.Pointer(&rsa.Port))[:], ip.Port())

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

for {
_, _, err := unix.Syscall6(
Expand Down

0 comments on commit 0736cfa

Please sign in to comment.