v5wire

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 9, 2022 License: GPL-3.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PingMsg byte = iota + 1
	PongMsg
	FindnodeMsg
	NodesMsg
	TalkRequestMsg
	TalkResponseMsg
	RequestTicketMsg
	TicketMsg
	RegtopicMsg
	RegconfirmationMsg
	TopicQueryMsg

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

Message types.

Variables

View Source
var (
	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. 将压缩格式的公钥还原成ecdsa.PublicKey对象 输入的曲线curve必须是secp256k1

func EncodePubkey

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

EncodePubkey encodes a public key. 获取压缩格式的公钥,公钥所在的曲线必须是secp256k1

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) *Codec

NewCodec creates a wire codec. 创建一个Codec对象,需要LocalNode对象和本地私钥

func (*Codec) Decode

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

Decode decodes a discovery packet. addr是数据包的来源ip地址,input是要进行解码的数据包 input最开始保存了IV,恢复前16字节到head中 利用IV和本地id解码头部信息,恢复静态部分到head中 继续解密恢复authData到head中 消息包利用msgData构造Packet对象,WHOAREYOU包构造Whoareyou类型的Packet对象 当解码的数据包是握手包时返回的enode.Node对象可能不是nil

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. 编码一个数据包,数据包有三种 普通包,WHOAREYOU包,握手包 id,addr代表了数据包发送到节点,packet代表要发送的消息,challenge不为nil代表要发送握手包

func (*Codec) EncodeRaw

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

EncodeRaw encodes a packet with the given header. 给定数据包头Header对象和已经处理好的消息字节,编码成一个实际发送的字节数组 msgdata就是真正发送的消息,不需要再进行加密 需要提供接收者的id,用来对Header对象编码后的结果加密 1. 编码Header对象到c.buf中 2. 利用id前16字节作为密钥对c.buf的IV后面部分进行加密 3. 再向c.buf写入msgdata 4. 返回c.buf.Bytes()

type Findnode

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

FINDNODE is a query for nodes in the given bucket.

func (*Findnode) Kind

func (*Findnode) Kind() byte

func (*Findnode) Name

func (*Findnode) Name() string

func (*Findnode) RequestID

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

func (*Findnode) SetRequestID

func (p *Findnode) SetRequestID(id []byte)
type Header struct {
	IV [sizeofMaskingIV]byte
	StaticHeader
	AuthData []byte
	// contains filtered or unexported fields
}

Header represents a packet header. 数据包的头部 头部包括16字节明文的IV,以及加密部分

加密使用AES-CTR模式,密钥是对方节点的id前16字节,IV就是最开始16字节明文的IV

加密前的原始头部信息包括 定长信息和认证信息,分别由StaticHeader和AuthData表示

type Nodes

type Nodes struct {
	ReqID []byte
	Total uint8
	Nodes []*enr.Record
}

NODES is the reply to FINDNODE and TOPICQUERY.

func (*Nodes) Kind

func (*Nodes) Kind() byte

func (*Nodes) Name

func (*Nodes) Name() string

func (*Nodes) RequestID

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

func (*Nodes) SetRequestID

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

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.
}

Packet is implemented by all message types. v5版本的Packet对象 Packet的实现类型是各种消息

func DecodeMessage

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

DecodeMessage decodes the message body of a packet. 输入消息的rlp编码和类型,解码到Packet对象

type Ping

type Ping struct {
	ReqID  []byte
	ENRSeq uint64
}

PING is sent during liveness checks.

func (*Ping) Kind

func (*Ping) Kind() byte

func (*Ping) Name

func (*Ping) Name() string

func (*Ping) RequestID

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

func (*Ping) SetRequestID

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

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) Kind

func (*Pong) Kind() byte

func (*Pong) Name

func (*Pong) Name() string

func (*Pong) RequestID

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

func (*Pong) SetRequestID

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

type Regconfirmation

type Regconfirmation struct {
	ReqID      []byte
	Registered bool
}

REGCONFIRMATION is the reply to REGTOPIC.

func (*Regconfirmation) Kind

func (*Regconfirmation) Kind() byte

func (*Regconfirmation) Name

func (*Regconfirmation) Name() string

func (*Regconfirmation) RequestID

func (p *Regconfirmation) RequestID() []byte

func (*Regconfirmation) SetRequestID

func (p *Regconfirmation) SetRequestID(id []byte)

type Regtopic

type Regtopic struct {
	ReqID  []byte
	Ticket []byte
	ENR    *enr.Record
}

REGTOPIC registers the sender in a topic queue using a ticket.

func (*Regtopic) Kind

func (*Regtopic) Kind() byte

func (*Regtopic) Name

func (*Regtopic) Name() string

func (*Regtopic) RequestID

func (p *Regtopic) RequestID() []byte

func (*Regtopic) SetRequestID

func (p *Regtopic) SetRequestID(id []byte)

type RequestTicket

type RequestTicket struct {
	ReqID []byte
	Topic []byte
}

REQUESTTICKET requests a ticket for a topic queue.

func (*RequestTicket) Kind

func (*RequestTicket) Kind() byte

func (*RequestTicket) Name

func (*RequestTicket) Name() string

func (*RequestTicket) RequestID

func (p *RequestTicket) RequestID() []byte

func (*RequestTicket) SetRequestID

func (p *RequestTicket) SetRequestID(id []byte)

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

创建一个SessionCache对象 maxItems代表缓存的最大条目

type StaticHeader

type StaticHeader struct {
	// 使用6个字节保存协议版本,现在是 "discv5"
	ProtocolID [6]byte
	// 2字节version
	Version uint16
	// 1字节flag
	Flag byte
	// 12字节Nonce
	Nonce Nonce
	// 2字节AuthSize,代表头部后面AuthData的长度
	AuthSize uint16
}

StaticHeader contains the static fields of a packet header. 头部中定长的信息,总共23字节,5个字段

type TalkRequest

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

TALKREQ is an application-level request.

func (*TalkRequest) Kind

func (*TalkRequest) Kind() byte

func (*TalkRequest) Name

func (*TalkRequest) Name() string

func (*TalkRequest) RequestID

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

func (*TalkRequest) SetRequestID

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

type TalkResponse

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

TALKRESP is the reply to TALKREQ.

func (*TalkResponse) Kind

func (*TalkResponse) Kind() byte

func (*TalkResponse) Name

func (*TalkResponse) Name() string

func (*TalkResponse) RequestID

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

func (*TalkResponse) SetRequestID

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

type Ticket

type Ticket struct {
	ReqID  []byte
	Ticket []byte
}

TICKET is the response to REQUESTTICKET.

func (*Ticket) Kind

func (*Ticket) Kind() byte

func (*Ticket) Name

func (*Ticket) Name() string

func (*Ticket) RequestID

func (p *Ticket) RequestID() []byte

func (*Ticket) SetRequestID

func (p *Ticket) SetRequestID(id []byte)

type TopicQuery

type TopicQuery struct {
	ReqID []byte
	Topic []byte
}

TOPICQUERY asks for nodes with the given topic.

func (*TopicQuery) Kind

func (*TopicQuery) Kind() byte

func (*TopicQuery) Name

func (*TopicQuery) Name() string

func (*TopicQuery) RequestID

func (p *TopicQuery) RequestID() []byte

func (*TopicQuery) SetRequestID

func (p *TopicQuery) SetRequestID(id []byte)

type Unknown

type Unknown struct {
	Nonce Nonce
}

Unknown represents any packet that can't be decrypted.

func (*Unknown) Kind

func (*Unknown) Kind() byte

func (*Unknown) Name

func (*Unknown) Name() string

func (*Unknown) RequestID

func (*Unknown) RequestID() []byte

func (*Unknown) SetRequestID

func (*Unknown) SetRequestID([]byte)

type Whoareyou

type Whoareyou struct {
	// ChallengeData其实就是收到整个WHOYOUARE数据包解密后的内容
	ChallengeData []byte // Encoded challenge
	// 引起发送Whoareyou包的Nonce,接收Whoareyou包的节点可以根据这个Nonce来得知发送Whoareyou包的节点是哪一个
	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代表Whoareyou发送方的记录,本地根据Nonce来得知之前发送的请求包,所以就能直到发送给了哪个节点
	Node *enode.Node
	// contains filtered or unexported fields
}

WHOAREYOU contains the handshake challenge. WHOAREYOU数据包本身没有消息,所以将数据包头部的信息视作一个消息

func (*Whoareyou) Kind

func (*Whoareyou) Kind() byte

func (*Whoareyou) Name

func (*Whoareyou) Name() string

func (*Whoareyou) RequestID

func (*Whoareyou) RequestID() []byte

func (*Whoareyou) SetRequestID

func (*Whoareyou) SetRequestID([]byte)

Jump to

Keyboard shortcuts

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