discv5

package
v0.0.0-...-cf7484d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 10, 2024 License: MIT Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PingMsg byte = iota + 1
	PongMsg
	FindnodeMsg
	NodesMsg
	TalkRequestMsg
	TalkResponseMsg

	UnknownPacket   = byte(255) // any non-decryptable packet
	WhoareyouPacket = byte(254) // the WHOAREYOU packet
)

Message types.

Variables

View Source
var DefaultProtocolID = [6]byte{'d', 'i', 's', 'c', 'v', '5'}
View Source
var (
	// ErrInvalidReqID represents error when the ID is invalid.
	ErrInvalidReqID = errors.New("request ID larger than 8 bytes")
)

Public errors.

Functions

func DecodePubkey

func DecodePubkey(curve elliptic.Curve, e []byte) (*ecdsa.PublicKey, error)

DecodePubkey decodes a public key in compressed format.

func EncodePubkey

func EncodePubkey(key *ecdsa.PublicKey) []byte

EncodePubkey encodes a public key.

func IsInvalidHeader

func IsInvalidHeader(err error) bool

IsInvalidHeader reports whether 'err' is related to an invalid packet header. When it returns false, it is pretty certain that the packet causing the error does not belong to discv5.

Types

type Codec

type Codec struct {
	// contains filtered or unexported fields
}

Codec encodes and decodes Discovery v5 packets. This type is not safe for concurrent use.

func NewCodec

func NewCodec(ln *enode.LocalNode, key *ecdsa.PrivateKey, clock mclock.Clock, protocolID *[6]byte) *Codec

NewCodec creates a wire codec.

func (*Codec) Decode

func (c *Codec) Decode(inputData []byte, addr string) (src enode.ID, n *enode.Node, p Packet, err error)

Decode decodes a discovery packet.

func (*Codec) Encode

func (c *Codec) Encode(id enode.ID, addr string, packet Packet, challenge *Whoareyou) ([]byte, Nonce, error)

Encode encodes a packet to a node. 'id' and 'addr' specify the destination node. The 'challenge' parameter should be the most recently received WHOAREYOU packet from that node.

func (*Codec) EncodeRaw

func (c *Codec) EncodeRaw(id enode.ID, head Header, msgdata []byte) ([]byte, error)

EncodeRaw encodes a packet with the given header.

type Findnode

type Findnode struct {
	ReqID     []byte
	Distances []uint

	// OpID is for debugging purposes and is not part of the packet encoding.
	// It identifies the 'operation' on behalf of which the request was sent.
	OpID uint64 `rlp:"-"`
}

Findnode is a query for nodes in the given bucket.

func (*Findnode) AppendLogInfo

func (req *Findnode) AppendLogInfo(ctx []interface{}) []interface{}

func (*Findnode) Kind

func (req *Findnode) Kind() byte

func (*Findnode) Name

func (req *Findnode) Name() string

func (*Findnode) RequestID

func (req *Findnode) RequestID() []byte

func (*Findnode) SetRequestID

func (req *Findnode) SetRequestID(id []byte)

func (*Findnode) String

func (req *Findnode) String() string
type Header struct {
	IV [sizeofMaskingIV]byte
	StaticHeader
	AuthData []byte
	// contains filtered or unexported fields
}

Header represents a packet header.

type Nodes

type Nodes struct {
	ReqID     []byte
	RespCount uint8 // total number of responses to the request
	Nodes     []*enr.Record
}

Nodes is a response to FINDNODE.

func (*Nodes) AppendLogInfo

func (req *Nodes) AppendLogInfo(ctx []interface{}) []interface{}

func (*Nodes) Kind

func (req *Nodes) Kind() byte

func (*Nodes) MarshalJSON

func (req *Nodes) MarshalJSON() ([]byte, error)

为 Nodes 实现 json.Marshaler 接口

func (*Nodes) Name

func (req *Nodes) Name() string

func (*Nodes) RequestID

func (req *Nodes) RequestID() []byte

func (*Nodes) SetRequestID

func (req *Nodes) SetRequestID(id []byte)

func (*Nodes) String

func (req *Nodes) String() string

type Nonce

type Nonce [gcmNonceSize]byte

Nonce represents a nonce used for AES/GCM.

type Packet

type Packet interface {
	Name() string        // Name returns a string corresponding to the message type.
	Kind() byte          // Kind returns the message type.
	RequestID() []byte   // Returns the request ID.
	SetRequestID([]byte) // Sets the request ID.
	// String is the print of packet
	String() string
	// AppendLogInfo returns its argument 'ctx' with additional fields
	// appended for logging purposes.
	AppendLogInfo(ctx []interface{}) []interface{}
}

func DecodeMessage

func DecodeMessage(ptype byte, body []byte) (Packet, error)

DecodeMessage decodes the message body of a packet.

type Ping

type Ping struct {
	ReqID  []byte
	ENRSeq uint64
}

Ping is sent during liveness checks.

func (*Ping) AppendLogInfo

func (req *Ping) AppendLogInfo(ctx []interface{}) []interface{}

func (*Ping) Kind

func (req *Ping) Kind() byte

func (*Ping) Name

func (req *Ping) Name() string

func (*Ping) RequestID

func (req *Ping) RequestID() []byte

func (*Ping) SetRequestID

func (req *Ping) SetRequestID(id []byte)

func (*Ping) String

func (req *Ping) String() string

type Pong

type Pong struct {
	ReqID  []byte
	ENRSeq uint64
	ToIP   net.IP // These fields should mirror the UDP envelope address of the ping
	ToPort uint16 // packet, which provides a way to discover the external address (after NAT).
}

Pong is the reply to PING.

func (*Pong) AppendLogInfo

func (req *Pong) AppendLogInfo(ctx []interface{}) []interface{}

func (*Pong) Kind

func (req *Pong) Kind() byte

func (*Pong) Name

func (req *Pong) Name() string

func (*Pong) RequestID

func (req *Pong) RequestID() []byte

func (*Pong) SetRequestID

func (req *Pong) SetRequestID(id []byte)

func (*Pong) String

func (req *Pong) String() string

type SessionCache

type SessionCache struct {
	// contains filtered or unexported fields
}

The SessionCache keeps negotiated encryption keys and state for in-progress handshakes in the Discovery v5 wire protocol.

func NewSessionCache

func NewSessionCache(maxItems int, clock mclock.Clock) *SessionCache

type StaticHeader

type StaticHeader struct {
	ProtocolID [6]byte
	Version    uint16
	Flag       byte
	Nonce      Nonce
	AuthSize   uint16
}

StaticHeader contains the static fields of a packet header.

type TalkRequest

type TalkRequest struct {
	ReqID    []byte
	Protocol string
	Message  []byte
}

TalkRequest TALKREQ is an application-level request.

func (*TalkRequest) AppendLogInfo

func (req *TalkRequest) AppendLogInfo(ctx []interface{}) []interface{}

func (*TalkRequest) Kind

func (*TalkRequest) Kind() byte

func (*TalkRequest) Name

func (req *TalkRequest) Name() string

func (*TalkRequest) RequestID

func (req *TalkRequest) RequestID() []byte

func (*TalkRequest) SetRequestID

func (req *TalkRequest) SetRequestID(id []byte)

func (*TalkRequest) String

func (req *TalkRequest) String() string

type TalkRequestHandler

type TalkRequestHandler func(enode.ID, *net.UDPAddr, []byte) []byte

TalkRequestHandler callback processes a talk request and returns a response.

Note that talk handlers are expected to come up with a response very quickly, within at most 200ms or so. If the handler takes longer than that, the remote end may time out and won't receive the response.

type TalkResponse

type TalkResponse struct {
	ReqID   []byte
	Message []byte
}

TalkResponse TALKRESP is the reply to TALKREQ.

func (*TalkResponse) AppendLogInfo

func (req *TalkResponse) AppendLogInfo(ctx []interface{}) []interface{}

func (*TalkResponse) Kind

func (req *TalkResponse) Kind() byte

func (*TalkResponse) Name

func (req *TalkResponse) Name() string

func (*TalkResponse) RequestID

func (req *TalkResponse) RequestID() []byte

func (*TalkResponse) SetRequestID

func (req *TalkResponse) SetRequestID(id []byte)

func (*TalkResponse) String

func (req *TalkResponse) String() string

type UDPv5

type UDPv5 struct {
	// contains filtered or unexported fields
}

UDPv5 is the implementation of protocol version 5.

func ListenV5

func ListenV5(conn d2p.UDPConn, ln *enode.LocalNode, cfg d2p.Config) (*UDPv5, error)

ListenV5 listens on the given connection.

func (*UDPv5) CallDone

func (t *UDPv5) CallDone(c *callV5)

callDone tells dispatch that the active call is done.

func (*UDPv5) CallToNode

func (t *UDPv5) CallToNode(n *enode.Node, responseType byte, req Packet) *callV5

callToNode sends the given call and sets up a handler for response packets (of message type responseType). Responses are dispatched to the call's response channel.

func (*UDPv5) Close

func (t *UDPv5) Close()

Close shuts down packet processing.

func (*UDPv5) Decode

func (t *UDPv5) Decode(input []byte, fromAddr string) (Packet, Nonce, error)

func (*UDPv5) DecodePacket

func (t *UDPv5) DecodePacket(input []byte, fromAddr string) (enode.ID, *enode.Node, Packet, error)

func (*UDPv5) EncodePacket

func (t *UDPv5) EncodePacket(id enode.ID, addr string, packet Packet, challenge *Whoareyou) ([]byte, Nonce, error)

func (*UDPv5) GenPacket

func (t *UDPv5) GenPacket(packetType string, n *enode.Node) Packet

func (*UDPv5) GetCallErrorChan

func (t *UDPv5) GetCallErrorChan(c *callV5) <-chan error

func (*UDPv5) GetCallResponseChan

func (t *UDPv5) GetCallResponseChan(c *callV5) <-chan Packet

func (*UDPv5) LocalNode

func (t *UDPv5) LocalNode() *enode.LocalNode

LocalNode returns the current local node running the protocol.

func (*UDPv5) ReadFromUDP

func (t *UDPv5) ReadFromUDP(b []byte) (int, *net.UDPAddr, error)

func (*UDPv5) RegisterTalkHandler

func (t *UDPv5) RegisterTalkHandler(protocol string, handler TalkRequestHandler)

RegisterTalkHandler adds a handler for 'talk requests'. The handler function is called whenever a request for the given protocol is received and should return the response data or nil.

func (*UDPv5) RequestENR

func (t *UDPv5) RequestENR(n *enode.Node) (*enode.Node, error)

RequestENR requests n's record.

func (*UDPv5) Self

func (t *UDPv5) Self() *enode.Node

Self returns the local node record.

func (*UDPv5) Send

func (t *UDPv5) Send(n *enode.Node, p Packet, challenge *Whoareyou) (Nonce, error)

func (*UDPv5) SetActiveCall

func (t *UDPv5) SetActiveCall(nonce Nonce, call *callV5)

func (*UDPv5) SetCallResponseType

func (t *UDPv5) SetCallResponseType(c *callV5, responseType byte)

SetCallResponseType 在 UDPv5 结构体所在的包中添加这些方法

func (*UDPv5) SetReadDeadline

func (t *UDPv5) SetReadDeadline(deadline time.Time)

func (*UDPv5) TalkRequestToID

func (t *UDPv5) TalkRequestToID(id enode.ID, addr *net.UDPAddr, protocol string, request []byte) ([]byte, error)

TalkRequestToID sends a talk request to a node and waits for a response.

type Unknown

type Unknown struct {
	Nonce Nonce
}

Unknown represents any packet that can't be decrypted.

func (*Unknown) AppendLogInfo

func (req *Unknown) AppendLogInfo(ctx []interface{}) []interface{}

func (*Unknown) Kind

func (req *Unknown) Kind() byte

func (*Unknown) Name

func (req *Unknown) Name() string

func (*Unknown) RequestID

func (req *Unknown) RequestID() []byte

func (*Unknown) SetRequestID

func (req *Unknown) SetRequestID([]byte)

func (*Unknown) String

func (req *Unknown) String() string

type Whoareyou

type Whoareyou struct {
	ChallengeData []byte   // Encoded challenge
	Nonce         Nonce    // Nonce of request packet
	IDNonce       [16]byte // Identity proof data
	RecordSeq     uint64   // ENR sequence number of recipient

	// Node is the locally known node record of recipient.
	// This must be set by the caller of Encode.
	Node *enode.Node
	// contains filtered or unexported fields
}

Whoareyou contains the handshake challenge.

func (*Whoareyou) AppendLogInfo

func (req *Whoareyou) AppendLogInfo(ctx []interface{}) []interface{}

func (*Whoareyou) Kind

func (req *Whoareyou) Kind() byte

func (*Whoareyou) Name

func (req *Whoareyou) Name() string

func (*Whoareyou) RequestID

func (req *Whoareyou) RequestID() []byte

func (*Whoareyou) SetRequestID

func (req *Whoareyou) SetRequestID([]byte)

func (*Whoareyou) String

func (req *Whoareyou) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL