Skip to content

Commit

Permalink
Prevent allocation from encoding UDP address (#684)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulwe committed Apr 20, 2024
1 parent 1684ed2 commit 528d31f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
14 changes: 14 additions & 0 deletions udp_mux_test.go
Expand Up @@ -150,6 +150,20 @@ func TestAddressEncoding(t *testing.T) {
}
}

func BenchmarkAddressEncoding(b *testing.B) {
addr := &net.UDPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 1234,
}
buf := make([]byte, 64)

for i := 0; i < b.N; i++ {
if _, err := encodeUDPAddr(addr, buf); err != nil {
require.NoError(b, err)
}
}
}

func testMuxConnection(t *testing.T, udpMux *UDPMuxDefault, ufrag string, network string) {
pktConn, err := udpMux.GetConn(ufrag, udpMux.LocalAddr())
require.NoError(t, err, "error retrieving muxed connection for ufrag")
Expand Down
26 changes: 18 additions & 8 deletions udp_muxed_conn.go
Expand Up @@ -7,8 +7,11 @@ import (
"encoding/binary"
"io"
"net"
"net/netip"
"reflect"
"sync"
"time"
"unsafe"

"github.com/pion/logging"
"github.com/pion/transport/v2/packetio"
Expand Down Expand Up @@ -214,19 +217,26 @@ func (c *udpMuxedConn) writePacket(data []byte, addr *net.UDPAddr) error {
}

func encodeUDPAddr(addr *net.UDPAddr, buf []byte) (int, error) {
ipData, err := addr.IP.MarshalText()
if err != nil {
return 0, err
if len(addr.IP) != 0 && len(addr.IP) != net.IPv4len && len(addr.IP) != net.IPv6len {
return 0, errInvalidAddress
}

var n int
if ip4 := addr.IP.To4(); len(ip4) == net.IPv4len {
d := (*reflect.SliceHeader)(unsafe.Pointer(&ip4)) // nolint:gosec
n = len(netip.AddrFrom4(*(*[4]byte)(unsafe.Pointer(d.Data))).AppendTo(buf[2:2])) // nolint:gosec
} else if len(addr.IP) != 0 {
d := (*reflect.SliceHeader)(unsafe.Pointer(&addr.IP)) // nolint:gosec
n = len(netip.AddrFrom16(*(*[16]byte)(unsafe.Pointer(d.Data))).AppendTo(buf[2:2])) // nolint:gosec
}
total := 2 + len(ipData) + 2 + len(addr.Zone)

total := 2 + n + 2 + len(addr.Zone)
if total > len(buf) {
return 0, io.ErrShortBuffer
}

binary.LittleEndian.PutUint16(buf, uint16(len(ipData)))
offset := 2
n := copy(buf[offset:], ipData)
offset += n
binary.LittleEndian.PutUint16(buf, uint16(n))
offset := 2 + n
binary.LittleEndian.PutUint16(buf[offset:], uint16(addr.Port))
offset += 2
copy(buf[offset:], addr.Zone)
Expand Down

0 comments on commit 528d31f

Please sign in to comment.