Documentation ¶
Index ¶
- Constants
- Variables
- func DecodePubkey(curve elliptic.Curve, e []byte) (*ecdsa.PublicKey, error)
- func EncodePubkey(key *ecdsa.PublicKey) []byte
- type Codec
- type Findnode
- type Header
- type Nodes
- type Nonce
- type Packet
- type Ping
- type Pong
- type Regconfirmation
- type Regtopic
- type RequestTicket
- type SessionCache
- type StaticHeader
- type TalkRequest
- type TalkResponse
- type Ticket
- type TopicQuery
- type Unknown
- type Whoareyou
Constants ¶
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 ¶
var (
ErrInvalidReqID = errors.New("request ID larger than 8 bytes")
)
Public errors.
Functions ¶
func DecodePubkey ¶
DecodePubkey decodes a public key in compressed format. 将压缩格式的公钥还原成ecdsa.PublicKey对象 输入的曲线curve必须是secp256k1
func EncodePubkey ¶
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 (*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代表要发送握手包
type Header ¶
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 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的实现类型是各种消息
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) SetRequestID ¶
type Regconfirmation ¶
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 ¶
REGTOPIC registers the sender in a topic queue using a ticket.
func (*Regtopic) SetRequestID ¶
type RequestTicket ¶
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 ¶
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 ¶
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 TopicQuery ¶
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) SetRequestID ¶
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数据包本身没有消息,所以将数据包头部的信息视作一个消息