From d51aa085684e63c3f9116f7d5527fdc88cd0b98a Mon Sep 17 00:00:00 2001 From: Sukun Date: Fri, 2 Feb 2024 22:06:20 +0530 Subject: [PATCH] Limit read buffer size to 9000B net.Interfaces() contains the loop back interface whose MTU is 65kB. The RFC has a stricter limit of 9000 bytes. This saves us some space. --- conn.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/conn.go b/conn.go index ddff48a..dea7428 100644 --- a/conn.go +++ b/conn.go @@ -47,6 +47,12 @@ const ( destinationAddress = "224.0.0.251:5353" maxMessageRecords = 3 responseTTL = 120 + // maxPacketSize is the maximum size of a mdns packet. + // From RFC 6762: + // Even when fragmentation is used, a Multicast DNS packet, including IP + // and UDP headers, MUST NOT exceed 9000 bytes. + // https://datatracker.ietf.org/doc/html/rfc6762#section-17 + maxPacketSize = 9000 ) var errNoPositiveMTUFound = errors.New("no positive MTU found") @@ -81,6 +87,9 @@ func Server(conn *ipv4.PacketConn, config *Config) (*Conn, error) { if inboundBufferSize == 0 { return nil, errNoPositiveMTUFound } + if inboundBufferSize > maxPacketSize { + inboundBufferSize = maxPacketSize + } if joinErrCount >= len(ifaces) { return nil, errJoiningMulticastGroup }