p2p/discover: make maximum packet size a constant (#19061)

ChrisChinchilla-patch-3
Matthew Halpern 6 years ago committed by Felix Lange
parent bf42535d31
commit f1537b774c
  1. 16
      p2p/discover/udp.go

@ -54,6 +54,11 @@ const (
ntpFailureThreshold = 32 // Continuous timeouts after which to check NTP ntpFailureThreshold = 32 // Continuous timeouts after which to check NTP
ntpWarningCooldown = 10 * time.Minute // Minimum amount of time to pass before repeating NTP warning ntpWarningCooldown = 10 * time.Minute // Minimum amount of time to pass before repeating NTP warning
driftThreshold = 10 * time.Second // Allowed clock drift before warning user driftThreshold = 10 * time.Second // Allowed clock drift before warning user
// Discovery packets are defined to be no larger than 1280 bytes.
// Packets larger than this size will be cut at the end and treated
// as invalid because their hash won't match.
maxPacketSize = 1280
) )
// RPC packet types // RPC packet types
@ -496,7 +501,7 @@ var (
headSpace = make([]byte, headSize) headSpace = make([]byte, headSize)
// Neighbors replies are sent across multiple packets to // Neighbors replies are sent across multiple packets to
// stay below the 1280 byte limit. We compute the maximum number // stay below the packet size limit. We compute the maximum number
// of entries by stuffing a packet until it grows too large. // of entries by stuffing a packet until it grows too large.
maxNeighbors int maxNeighbors int
) )
@ -511,7 +516,7 @@ func init() {
// If this ever happens, it will be caught by the unit tests. // If this ever happens, it will be caught by the unit tests.
panic("cannot encode: " + err.Error()) panic("cannot encode: " + err.Error())
} }
if headSize+size+1 >= 1280 { if headSize+size+1 >= maxPacketSize {
maxNeighbors = n maxNeighbors = n
break break
} }
@ -562,10 +567,7 @@ func (t *udp) readLoop(unhandled chan<- ReadPacket) {
defer close(unhandled) defer close(unhandled)
} }
// Discovery packets are defined to be no larger than 1280 bytes. buf := make([]byte, maxPacketSize)
// Packets larger than this size will be cut at the end and treated
// as invalid because their hash won't match.
buf := make([]byte, 1280)
for { for {
nbytes, from, err := t.conn.ReadFromUDP(buf) nbytes, from, err := t.conn.ReadFromUDP(buf)
if netutil.IsTemporaryError(err) { if netutil.IsTemporaryError(err) {
@ -715,7 +717,7 @@ func (req *findnode) handle(t *udp, from *net.UDPAddr, fromID enode.ID, mac []by
t.tab.mutex.Unlock() t.tab.mutex.Unlock()
// Send neighbors in chunks with at most maxNeighbors per packet // Send neighbors in chunks with at most maxNeighbors per packet
// to stay below the 1280 byte limit. // to stay below the packet size limit.
p := neighbors{Expiration: uint64(time.Now().Add(expiration).Unix())} p := neighbors{Expiration: uint64(time.Now().Add(expiration).Unix())}
var sent bool var sent bool
for _, n := range closest { for _, n := range closest {

Loading…
Cancel
Save