lnwire+peer: clamp pong bytes, make ping handler more effcieint
This was not properly enforced and would be a spec violation on the peer's end. Also re-use a pong buffer to save on heap allocations if there are a lot of peers. The pong buffer is only read from, so this is concurrent safe.
This commit is contained in:
6 files changed
+55
-7
No files matched your search
@@ -864,6 +864,22 @@ func TestLightningWireProtocol(t *testing.T) {
|
||||
NewShortChanIDFromInt(uint64(r.Int63())))
|
||||
}
|
||||
|
||||
v[0] = reflect.ValueOf(req)
|
||||
},
|
||||
MsgPing: func(v []reflect.Value, r *rand.Rand) {
|
||||
// We use a special message generator here to ensure we
|
||||
// don't generate ping messages that are too large,
|
||||
// which'll cause the test to fail.
|
||||
//
|
||||
// We'll allow the test to generate padding bytes up to
|
||||
// the max message limit, factoring in the 2 bytes for
|
||||
// the num pong bytes.
|
||||
paddingBytes := make([]byte, r.Intn(MaxMsgBody-1))
|
||||
req := Ping{
|
||||
NumPongBytes: uint16(r.Intn(MaxPongBytes + 1)),
|
||||
PaddingBytes: paddingBytes,
|
||||
}
|
||||
|
||||
v[0] = reflect.ValueOf(req)
|
||||
},
|
||||
}
|
||||
|
||||
+10
-3
@@ -38,9 +38,16 @@ var _ Message = (*Ping)(nil)
|
||||
//
|
||||
// This is part of the lnwire.Message interface.
|
||||
func (p *Ping) Decode(r io.Reader, pver uint32) error {
|
||||
return ReadElements(r,
|
||||
&p.NumPongBytes,
|
||||
&p.PaddingBytes)
|
||||
err := ReadElements(r, &p.NumPongBytes, &p.PaddingBytes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if p.NumPongBytes > MaxPongBytes {
|
||||
return ErrMaxPongBytesExceeded
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Encode serializes the target Ping into the passed io.Writer observing the
|
||||
|
||||
@@ -2,9 +2,19 @@ package lnwire
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// MaxPongBytes is the maximum number of extra bytes a pong can be requested to
|
||||
// send. The type of the message (19) takes 2 bytes, the length field takes up
|
||||
// 2 bytes, leaving 65531 bytes.
|
||||
const MaxPongBytes = 65531
|
||||
|
||||
// ErrMaxPongBytesExceeded indicates that the NumPongBytes field from the ping
|
||||
// message has exceeded MaxPongBytes.
|
||||
var ErrMaxPongBytesExceeded = fmt.Errorf("pong bytes exceeded")
|
||||
|
||||
// PongPayload is a set of opaque bytes sent in response to a ping message.
|
||||
type PongPayload []byte
|
||||
|
||||
|
||||
Reference in new issue
Block a user