Skip to content

Commit e2df2ad

Browse files
committed
Fix insufficient UDP buffer size, add byte count log
The previous buffer size of 1024 meant that data would be dropped for UDP packets larger than 1024 bytes, since each ReadFromUDP dequeues the entire packet (no partial reads to be continue on the next call like in TCP; i.e. TCP buffers bytes, UDP buffers whole packets).
1 parent 94fe4ae commit e2df2ad

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

internal/schemes/udp/udp.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ func Listen(cfg ndog.ListenConfig) error {
3838

3939
streams := map[string]ndog.Stream{}
4040

41-
buf := make([]byte, 1024)
41+
// ReadFromUDP dequeues an entire packet from the socket each time it's
42+
// called, so the buffer needs to have enough space to read entire packets
43+
// at once. 65535 bytes is the maximum possible packet size; ndog doesn't
44+
// know anything about the expected inner protocol, so best to use this
45+
// maximum size as the buffer size.
46+
buf := make([]byte, 65535)
4247
for {
4348
nr, remoteAddr, err := conn.ReadFromUDP(buf)
4449
if err != nil {
@@ -48,6 +53,7 @@ func Listen(cfg ndog.ListenConfig) error {
4853
return err
4954
}
5055
remoteAddrStr := remoteAddr.String()
56+
ndog.Logf(10, "%d bytes from %s", nr, remoteAddrStr)
5157

5258
var stream ndog.Stream
5359
if existingStream, ok := streams[remoteAddrStr]; ok {

0 commit comments

Comments
 (0)