@@ -7,11 +7,8 @@ import (
7
7
"encoding/binary"
8
8
"io"
9
9
"net"
10
- "net/netip"
11
- "reflect"
12
10
"sync"
13
11
"time"
14
- "unsafe"
15
12
16
13
"github.com/pion/logging"
17
14
"github.com/pion/transport/v2/packetio"
@@ -217,51 +214,46 @@ func (c *udpMuxedConn) writePacket(data []byte, addr *net.UDPAddr) error {
217
214
}
218
215
219
216
func encodeUDPAddr (addr * net.UDPAddr , buf []byte ) (int , error ) {
220
- if len (addr .IP ) != 0 && len (addr .IP ) != net .IPv4len && len (addr .IP ) != net .IPv6len {
221
- return 0 , errInvalidAddress
217
+ total := 1 + len (addr .IP ) + 2 + len (addr .Zone )
218
+ if len (buf ) < total {
219
+ return 0 , io .ErrShortBuffer
222
220
}
223
221
224
- var n int
225
- if ip4 := addr .IP .To4 (); len (ip4 ) == net .IPv4len {
226
- d := (* reflect .SliceHeader )(unsafe .Pointer (& ip4 )) // nolint:gosec
227
- n = len (netip .AddrFrom4 (* (* [4 ]byte )(unsafe .Pointer (d .Data ))).AppendTo (buf [2 :2 ])) // nolint:gosec
228
- } else if len (addr .IP ) != 0 {
229
- d := (* reflect .SliceHeader )(unsafe .Pointer (& addr .IP )) // nolint:gosec
230
- n = len (netip .AddrFrom16 (* (* [16 ]byte )(unsafe .Pointer (d .Data ))).AppendTo (buf [2 :2 ])) // nolint:gosec
231
- }
222
+ buf [0 ] = uint8 (len (addr .IP ))
223
+ offset := 1
232
224
233
- total := 2 + n + 2 + len (addr .Zone )
234
- if total > len (buf ) {
235
- return 0 , io .ErrShortBuffer
236
- }
225
+ copy (buf [offset :], addr .IP )
226
+ offset += len (addr .IP )
237
227
238
- binary .LittleEndian .PutUint16 (buf , uint16 (n ))
239
- offset := 2 + n
240
228
binary .LittleEndian .PutUint16 (buf [offset :], uint16 (addr .Port ))
241
229
offset += 2
230
+
242
231
copy (buf [offset :], addr .Zone )
243
232
return total , nil
244
233
}
245
234
246
235
func decodeUDPAddr (buf []byte ) (* net.UDPAddr , error ) {
247
- addr := net.UDPAddr {}
236
+ addr := & net.UDPAddr {}
248
237
249
- offset := 0
250
- ipLen := int (binary .LittleEndian .Uint16 (buf [:2 ]))
251
- offset += 2
252
238
// Basic bounds checking
253
- if ipLen + offset > len (buf ) {
239
+ if len ( buf ) == 0 || len (buf ) < int ( buf [ 0 ]) + 3 {
254
240
return nil , io .ErrShortBuffer
255
241
}
256
- if err := addr .IP .UnmarshalText (buf [offset : offset + ipLen ]); err != nil {
257
- return nil , err
242
+
243
+ ipLen := int (buf [0 ])
244
+ offset := 1
245
+
246
+ if ipLen == 0 {
247
+ addr .IP = nil
248
+ } else {
249
+ addr .IP = append (addr .IP [:0 ], buf [offset :offset + ipLen ]... )
250
+ offset += ipLen
258
251
}
259
- offset += ipLen
260
- addr .Port = int (binary .LittleEndian .Uint16 (buf [offset : offset + 2 ]))
252
+
253
+ addr .Port = int (binary .LittleEndian .Uint16 (buf [offset : ]))
261
254
offset += 2
262
- zone := make ([]byte , len (buf [offset :]))
263
- copy (zone , buf [offset :])
264
- addr .Zone = string (zone )
265
255
266
- return & addr , nil
256
+ addr .Zone = string (buf [offset :])
257
+
258
+ return addr , nil
267
259
}
0 commit comments