Skip to content

Commit

Permalink
Limit read buffer size to 9000B
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sukunrt authored and Sean-Der committed Feb 3, 2024
1 parent a32023d commit d51aa08
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit d51aa08

Please sign in to comment.