msg.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package v5wire
  17. import (
  18. "fmt"
  19. "net"
  20. "github.com/ethereum/go-ethereum/common/mclock"
  21. "github.com/ethereum/go-ethereum/p2p/enode"
  22. "github.com/ethereum/go-ethereum/p2p/enr"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. )
  25. // Packet is implemented by all message types.
  26. type Packet interface {
  27. Name() string // Name returns a string corresponding to the message type.
  28. Kind() byte // Kind returns the message type.
  29. RequestID() []byte // Returns the request ID.
  30. SetRequestID([]byte) // Sets the request ID.
  31. }
  32. // Message types.
  33. const (
  34. PingMsg byte = iota + 1
  35. PongMsg
  36. FindnodeMsg
  37. NodesMsg
  38. TalkRequestMsg
  39. TalkResponseMsg
  40. RequestTicketMsg
  41. TicketMsg
  42. RegtopicMsg
  43. RegconfirmationMsg
  44. TopicQueryMsg
  45. UnknownPacket = byte(255) // any non-decryptable packet
  46. WhoareyouPacket = byte(254) // the WHOAREYOU packet
  47. )
  48. // Protocol messages.
  49. type (
  50. // Unknown represents any packet that can't be decrypted.
  51. Unknown struct {
  52. Nonce Nonce
  53. }
  54. // WHOAREYOU contains the handshake challenge.
  55. Whoareyou struct {
  56. ChallengeData []byte // Encoded challenge
  57. Nonce Nonce // Nonce of request packet
  58. IDNonce [16]byte // Identity proof data
  59. RecordSeq uint64 // ENR sequence number of recipient
  60. // Node is the locally known node record of recipient.
  61. // This must be set by the caller of Encode.
  62. Node *enode.Node
  63. sent mclock.AbsTime // for handshake GC.
  64. }
  65. // PING is sent during liveness checks.
  66. Ping struct {
  67. ReqID []byte
  68. ENRSeq uint64
  69. }
  70. // PONG is the reply to PING.
  71. Pong struct {
  72. ReqID []byte
  73. ENRSeq uint64
  74. ToIP net.IP // These fields should mirror the UDP envelope address of the ping
  75. ToPort uint16 // packet, which provides a way to discover the the external address (after NAT).
  76. }
  77. // FINDNODE is a query for nodes in the given bucket.
  78. Findnode struct {
  79. ReqID []byte
  80. Distances []uint
  81. }
  82. // NODES is the reply to FINDNODE and TOPICQUERY.
  83. Nodes struct {
  84. ReqID []byte
  85. Total uint8
  86. Nodes []*enr.Record
  87. }
  88. // TALKREQ is an application-level request.
  89. TalkRequest struct {
  90. ReqID []byte
  91. Protocol string
  92. Message []byte
  93. }
  94. // TALKRESP is the reply to TALKREQ.
  95. TalkResponse struct {
  96. ReqID []byte
  97. Message []byte
  98. }
  99. // REQUESTTICKET requests a ticket for a topic queue.
  100. RequestTicket struct {
  101. ReqID []byte
  102. Topic []byte
  103. }
  104. // TICKET is the response to REQUESTTICKET.
  105. Ticket struct {
  106. ReqID []byte
  107. Ticket []byte
  108. }
  109. // REGTOPIC registers the sender in a topic queue using a ticket.
  110. Regtopic struct {
  111. ReqID []byte
  112. Ticket []byte
  113. ENR *enr.Record
  114. }
  115. // REGCONFIRMATION is the reply to REGTOPIC.
  116. Regconfirmation struct {
  117. ReqID []byte
  118. Registered bool
  119. }
  120. // TOPICQUERY asks for nodes with the given topic.
  121. TopicQuery struct {
  122. ReqID []byte
  123. Topic []byte
  124. }
  125. )
  126. // DecodeMessage decodes the message body of a packet.
  127. func DecodeMessage(ptype byte, body []byte) (Packet, error) {
  128. var dec Packet
  129. switch ptype {
  130. case PingMsg:
  131. dec = new(Ping)
  132. case PongMsg:
  133. dec = new(Pong)
  134. case FindnodeMsg:
  135. dec = new(Findnode)
  136. case NodesMsg:
  137. dec = new(Nodes)
  138. case TalkRequestMsg:
  139. dec = new(TalkRequest)
  140. case TalkResponseMsg:
  141. dec = new(TalkResponse)
  142. case RequestTicketMsg:
  143. dec = new(RequestTicket)
  144. case TicketMsg:
  145. dec = new(Ticket)
  146. case RegtopicMsg:
  147. dec = new(Regtopic)
  148. case RegconfirmationMsg:
  149. dec = new(Regconfirmation)
  150. case TopicQueryMsg:
  151. dec = new(TopicQuery)
  152. default:
  153. return nil, fmt.Errorf("unknown packet type %d", ptype)
  154. }
  155. if err := rlp.DecodeBytes(body, dec); err != nil {
  156. return nil, err
  157. }
  158. if dec.RequestID() != nil && len(dec.RequestID()) > 8 {
  159. return nil, ErrInvalidReqID
  160. }
  161. return dec, nil
  162. }
  163. func (*Whoareyou) Name() string { return "WHOAREYOU/v5" }
  164. func (*Whoareyou) Kind() byte { return WhoareyouPacket }
  165. func (*Whoareyou) RequestID() []byte { return nil }
  166. func (*Whoareyou) SetRequestID([]byte) {}
  167. func (*Unknown) Name() string { return "UNKNOWN/v5" }
  168. func (*Unknown) Kind() byte { return UnknownPacket }
  169. func (*Unknown) RequestID() []byte { return nil }
  170. func (*Unknown) SetRequestID([]byte) {}
  171. func (*Ping) Name() string { return "PING/v5" }
  172. func (*Ping) Kind() byte { return PingMsg }
  173. func (p *Ping) RequestID() []byte { return p.ReqID }
  174. func (p *Ping) SetRequestID(id []byte) { p.ReqID = id }
  175. func (*Pong) Name() string { return "PONG/v5" }
  176. func (*Pong) Kind() byte { return PongMsg }
  177. func (p *Pong) RequestID() []byte { return p.ReqID }
  178. func (p *Pong) SetRequestID(id []byte) { p.ReqID = id }
  179. func (*Findnode) Name() string { return "FINDNODE/v5" }
  180. func (*Findnode) Kind() byte { return FindnodeMsg }
  181. func (p *Findnode) RequestID() []byte { return p.ReqID }
  182. func (p *Findnode) SetRequestID(id []byte) { p.ReqID = id }
  183. func (*Nodes) Name() string { return "NODES/v5" }
  184. func (*Nodes) Kind() byte { return NodesMsg }
  185. func (p *Nodes) RequestID() []byte { return p.ReqID }
  186. func (p *Nodes) SetRequestID(id []byte) { p.ReqID = id }
  187. func (*TalkRequest) Name() string { return "TALKREQ/v5" }
  188. func (*TalkRequest) Kind() byte { return TalkRequestMsg }
  189. func (p *TalkRequest) RequestID() []byte { return p.ReqID }
  190. func (p *TalkRequest) SetRequestID(id []byte) { p.ReqID = id }
  191. func (*TalkResponse) Name() string { return "TALKRESP/v5" }
  192. func (*TalkResponse) Kind() byte { return TalkResponseMsg }
  193. func (p *TalkResponse) RequestID() []byte { return p.ReqID }
  194. func (p *TalkResponse) SetRequestID(id []byte) { p.ReqID = id }
  195. func (*RequestTicket) Name() string { return "REQTICKET/v5" }
  196. func (*RequestTicket) Kind() byte { return RequestTicketMsg }
  197. func (p *RequestTicket) RequestID() []byte { return p.ReqID }
  198. func (p *RequestTicket) SetRequestID(id []byte) { p.ReqID = id }
  199. func (*Regtopic) Name() string { return "REGTOPIC/v5" }
  200. func (*Regtopic) Kind() byte { return RegtopicMsg }
  201. func (p *Regtopic) RequestID() []byte { return p.ReqID }
  202. func (p *Regtopic) SetRequestID(id []byte) { p.ReqID = id }
  203. func (*Ticket) Name() string { return "TICKET/v5" }
  204. func (*Ticket) Kind() byte { return TicketMsg }
  205. func (p *Ticket) RequestID() []byte { return p.ReqID }
  206. func (p *Ticket) SetRequestID(id []byte) { p.ReqID = id }
  207. func (*Regconfirmation) Name() string { return "REGCONFIRMATION/v5" }
  208. func (*Regconfirmation) Kind() byte { return RegconfirmationMsg }
  209. func (p *Regconfirmation) RequestID() []byte { return p.ReqID }
  210. func (p *Regconfirmation) SetRequestID(id []byte) { p.ReqID = id }
  211. func (*TopicQuery) Name() string { return "TOPICQUERY/v5" }
  212. func (*TopicQuery) Kind() byte { return TopicQueryMsg }
  213. func (p *TopicQuery) RequestID() []byte { return p.ReqID }
  214. func (p *TopicQuery) SetRequestID(id []byte) { p.ReqID = id }