|
|
|
@ -98,7 +98,7 @@ const ( |
|
|
|
|
randomPacketMsgSize = 20 |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var protocolID = [6]byte{'d', 'i', 's', 'c', 'v', '5'} |
|
|
|
|
var DefaultProtocolID = [6]byte{'d', 'i', 's', 'c', 'v', '5'} |
|
|
|
|
|
|
|
|
|
// Errors.
|
|
|
|
|
var ( |
|
|
|
@ -134,10 +134,11 @@ var ( |
|
|
|
|
// Codec encodes and decodes Discovery v5 packets.
|
|
|
|
|
// This type is not safe for concurrent use.
|
|
|
|
|
type Codec struct { |
|
|
|
|
sha256 hash.Hash |
|
|
|
|
localnode *enode.LocalNode |
|
|
|
|
privkey *ecdsa.PrivateKey |
|
|
|
|
sc *SessionCache |
|
|
|
|
sha256 hash.Hash |
|
|
|
|
localnode *enode.LocalNode |
|
|
|
|
privkey *ecdsa.PrivateKey |
|
|
|
|
sc *SessionCache |
|
|
|
|
protocolID [6]byte |
|
|
|
|
|
|
|
|
|
// encoder buffers
|
|
|
|
|
buf bytes.Buffer // whole packet
|
|
|
|
@ -150,12 +151,16 @@ type Codec struct { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// NewCodec creates a wire codec.
|
|
|
|
|
func NewCodec(ln *enode.LocalNode, key *ecdsa.PrivateKey, clock mclock.Clock) *Codec { |
|
|
|
|
func NewCodec(ln *enode.LocalNode, key *ecdsa.PrivateKey, clock mclock.Clock, protocolID *[6]byte) *Codec { |
|
|
|
|
c := &Codec{ |
|
|
|
|
sha256: sha256.New(), |
|
|
|
|
localnode: ln, |
|
|
|
|
privkey: key, |
|
|
|
|
sc: NewSessionCache(1024, clock), |
|
|
|
|
sha256: sha256.New(), |
|
|
|
|
localnode: ln, |
|
|
|
|
privkey: key, |
|
|
|
|
sc: NewSessionCache(1024, clock), |
|
|
|
|
protocolID: DefaultProtocolID, |
|
|
|
|
} |
|
|
|
|
if protocolID != nil { |
|
|
|
|
c.protocolID = *protocolID |
|
|
|
|
} |
|
|
|
|
return c |
|
|
|
|
} |
|
|
|
@ -255,7 +260,7 @@ func (c *Codec) makeHeader(toID enode.ID, flag byte, authsizeExtra int) Header { |
|
|
|
|
} |
|
|
|
|
return Header{ |
|
|
|
|
StaticHeader: StaticHeader{ |
|
|
|
|
ProtocolID: protocolID, |
|
|
|
|
ProtocolID: c.protocolID, |
|
|
|
|
Version: version, |
|
|
|
|
Flag: flag, |
|
|
|
|
AuthSize: uint16(authsize), |
|
|
|
@ -434,7 +439,7 @@ func (c *Codec) Decode(input []byte, addr string) (src enode.ID, n *enode.Node, |
|
|
|
|
c.reader.Reset(staticHeader) |
|
|
|
|
binary.Read(&c.reader, binary.BigEndian, &head.StaticHeader) |
|
|
|
|
remainingInput := len(input) - sizeofStaticPacketData |
|
|
|
|
if err := head.checkValid(remainingInput); err != nil { |
|
|
|
|
if err := head.checkValid(remainingInput, c.protocolID); err != nil { |
|
|
|
|
return enode.ID{}, nil, nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -621,7 +626,7 @@ func (c *Codec) decryptMessage(input, nonce, headerData, readKey []byte) (Packet |
|
|
|
|
|
|
|
|
|
// checkValid performs some basic validity checks on the header.
|
|
|
|
|
// The packetLen here is the length remaining after the static header.
|
|
|
|
|
func (h *StaticHeader) checkValid(packetLen int) error { |
|
|
|
|
func (h *StaticHeader) checkValid(packetLen int, protocolID [6]byte) error { |
|
|
|
|
if h.ProtocolID != protocolID { |
|
|
|
|
return errInvalidHeader |
|
|
|
|
} |
|
|
|
|