peer.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. // Copyright 2014 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 p2p
  17. import (
  18. "errors"
  19. "fmt"
  20. "io"
  21. "net"
  22. "sort"
  23. "sync"
  24. "time"
  25. "github.com/ethereum/go-ethereum/common/mclock"
  26. "github.com/ethereum/go-ethereum/event"
  27. "github.com/ethereum/go-ethereum/log"
  28. "github.com/ethereum/go-ethereum/metrics"
  29. "github.com/ethereum/go-ethereum/p2p/enode"
  30. "github.com/ethereum/go-ethereum/p2p/enr"
  31. "github.com/ethereum/go-ethereum/rlp"
  32. )
  33. var (
  34. ErrShuttingDown = errors.New("shutting down")
  35. )
  36. const (
  37. baseProtocolVersion = 5
  38. baseProtocolLength = uint64(16)
  39. baseProtocolMaxMsgSize = 2 * 1024
  40. snappyProtocolVersion = 5
  41. pingInterval = 15 * time.Second
  42. )
  43. const (
  44. // devp2p message codes
  45. handshakeMsg = 0x00
  46. discMsg = 0x01
  47. pingMsg = 0x02
  48. pongMsg = 0x03
  49. )
  50. // protoHandshake is the RLP structure of the protocol handshake.
  51. type protoHandshake struct {
  52. Version uint64
  53. Name string
  54. Caps []Cap
  55. ListenPort uint64
  56. ID []byte // secp256k1 public key
  57. // Ignore additional fields (for forward compatibility).
  58. Rest []rlp.RawValue `rlp:"tail"`
  59. }
  60. // PeerEventType is the type of peer events emitted by a p2p.Server
  61. type PeerEventType string
  62. const (
  63. // PeerEventTypeAdd is the type of event emitted when a peer is added
  64. // to a p2p.Server
  65. PeerEventTypeAdd PeerEventType = "add"
  66. // PeerEventTypeDrop is the type of event emitted when a peer is
  67. // dropped from a p2p.Server
  68. PeerEventTypeDrop PeerEventType = "drop"
  69. // PeerEventTypeMsgSend is the type of event emitted when a
  70. // message is successfully sent to a peer
  71. PeerEventTypeMsgSend PeerEventType = "msgsend"
  72. // PeerEventTypeMsgRecv is the type of event emitted when a
  73. // message is received from a peer
  74. PeerEventTypeMsgRecv PeerEventType = "msgrecv"
  75. )
  76. // PeerEvent is an event emitted when peers are either added or dropped from
  77. // a p2p.Server or when a message is sent or received on a peer connection
  78. type PeerEvent struct {
  79. Type PeerEventType `json:"type"`
  80. Peer enode.ID `json:"peer"`
  81. Error string `json:"error,omitempty"`
  82. Protocol string `json:"protocol,omitempty"`
  83. MsgCode *uint64 `json:"msg_code,omitempty"`
  84. MsgSize *uint32 `json:"msg_size,omitempty"`
  85. LocalAddress string `json:"local,omitempty"`
  86. RemoteAddress string `json:"remote,omitempty"`
  87. }
  88. // Peer represents a connected remote node.
  89. type Peer struct {
  90. rw *conn
  91. running map[string]*protoRW
  92. log log.Logger
  93. created mclock.AbsTime
  94. wg sync.WaitGroup
  95. protoErr chan error
  96. closed chan struct{}
  97. disc chan DiscReason
  98. // events receives message send / receive events if set
  99. events *event.Feed
  100. testPipe *MsgPipeRW // for testing
  101. }
  102. // NewPeer returns a peer for testing purposes.
  103. func NewPeer(id enode.ID, name string, caps []Cap) *Peer {
  104. // Generate a fake set of local protocols to match as running caps. Almost
  105. // no fields needs to be meaningful here as we're only using it to cross-
  106. // check with the "remote" caps array.
  107. protos := make([]Protocol, len(caps))
  108. for i, cap := range caps {
  109. protos[i].Name = cap.Name
  110. protos[i].Version = cap.Version
  111. }
  112. pipe, _ := net.Pipe()
  113. node := enode.SignNull(new(enr.Record), id)
  114. conn := &conn{fd: pipe, transport: nil, node: node, caps: caps, name: name}
  115. peer := newPeer(log.Root(), conn, protos)
  116. close(peer.closed) // ensures Disconnect doesn't block
  117. return peer
  118. }
  119. // NewPeerPipe creates a peer for testing purposes.
  120. // The message pipe given as the last parameter is closed when
  121. // Disconnect is called on the peer.
  122. func NewPeerPipe(id enode.ID, name string, caps []Cap, pipe *MsgPipeRW) *Peer {
  123. p := NewPeer(id, name, caps)
  124. p.testPipe = pipe
  125. return p
  126. }
  127. // ID returns the node's public key.
  128. func (p *Peer) ID() enode.ID {
  129. return p.rw.node.ID()
  130. }
  131. // Node returns the peer's node descriptor.
  132. func (p *Peer) Node() *enode.Node {
  133. return p.rw.node
  134. }
  135. // Name returns an abbreviated form of the name
  136. func (p *Peer) Name() string {
  137. s := p.rw.name
  138. if len(s) > 20 {
  139. return s[:20] + "..."
  140. }
  141. return s
  142. }
  143. // Fullname returns the node name that the remote node advertised.
  144. func (p *Peer) Fullname() string {
  145. return p.rw.name
  146. }
  147. // Caps returns the capabilities (supported subprotocols) of the remote peer.
  148. func (p *Peer) Caps() []Cap {
  149. // TODO: maybe return copy
  150. return p.rw.caps
  151. }
  152. // RunningCap returns true if the peer is actively connected using any of the
  153. // enumerated versions of a specific protocol, meaning that at least one of the
  154. // versions is supported by both this node and the peer p.
  155. func (p *Peer) RunningCap(protocol string, versions []uint) bool {
  156. if proto, ok := p.running[protocol]; ok {
  157. for _, ver := range versions {
  158. if proto.Version == ver {
  159. return true
  160. }
  161. }
  162. }
  163. return false
  164. }
  165. // RemoteAddr returns the remote address of the network connection.
  166. func (p *Peer) RemoteAddr() net.Addr {
  167. return p.rw.fd.RemoteAddr()
  168. }
  169. // LocalAddr returns the local address of the network connection.
  170. func (p *Peer) LocalAddr() net.Addr {
  171. return p.rw.fd.LocalAddr()
  172. }
  173. // Disconnect terminates the peer connection with the given reason.
  174. // It returns immediately and does not wait until the connection is closed.
  175. func (p *Peer) Disconnect(reason DiscReason) {
  176. if p.testPipe != nil {
  177. p.testPipe.Close()
  178. }
  179. select {
  180. case p.disc <- reason:
  181. case <-p.closed:
  182. }
  183. }
  184. // String implements fmt.Stringer.
  185. func (p *Peer) String() string {
  186. id := p.ID()
  187. return fmt.Sprintf("Peer %x %v", id[:8], p.RemoteAddr())
  188. }
  189. // Inbound returns true if the peer is an inbound connection
  190. func (p *Peer) Inbound() bool {
  191. return p.rw.is(inboundConn)
  192. }
  193. func newPeer(log log.Logger, conn *conn, protocols []Protocol) *Peer {
  194. protomap := matchProtocols(protocols, conn.caps, conn)
  195. p := &Peer{
  196. rw: conn,
  197. running: protomap,
  198. created: mclock.Now(),
  199. disc: make(chan DiscReason),
  200. protoErr: make(chan error, len(protomap)+1), // protocols + pingLoop
  201. closed: make(chan struct{}),
  202. log: log.New("id", conn.node.ID(), "conn", conn.flags),
  203. }
  204. return p
  205. }
  206. func (p *Peer) Log() log.Logger {
  207. return p.log
  208. }
  209. func (p *Peer) run() (remoteRequested bool, err error) {
  210. var (
  211. writeStart = make(chan struct{}, 1)
  212. writeErr = make(chan error, 1)
  213. readErr = make(chan error, 1)
  214. reason DiscReason // sent to the peer
  215. )
  216. p.wg.Add(2)
  217. go p.readLoop(readErr)
  218. go p.pingLoop()
  219. // Start all protocol handlers.
  220. writeStart <- struct{}{}
  221. p.startProtocols(writeStart, writeErr)
  222. // Wait for an error or disconnect.
  223. loop:
  224. for {
  225. select {
  226. case err = <-writeErr:
  227. // A write finished. Allow the next write to start if
  228. // there was no error.
  229. if err != nil {
  230. reason = DiscNetworkError
  231. break loop
  232. }
  233. writeStart <- struct{}{}
  234. case err = <-readErr:
  235. if r, ok := err.(DiscReason); ok {
  236. remoteRequested = true
  237. reason = r
  238. } else {
  239. reason = DiscNetworkError
  240. }
  241. break loop
  242. case err = <-p.protoErr:
  243. reason = discReasonForError(err)
  244. break loop
  245. case err = <-p.disc:
  246. reason = discReasonForError(err)
  247. break loop
  248. }
  249. }
  250. close(p.closed)
  251. p.rw.close(reason)
  252. p.wg.Wait()
  253. return remoteRequested, err
  254. }
  255. func (p *Peer) pingLoop() {
  256. ping := time.NewTimer(pingInterval)
  257. defer p.wg.Done()
  258. defer ping.Stop()
  259. for {
  260. select {
  261. case <-ping.C:
  262. if err := SendItems(p.rw, pingMsg); err != nil {
  263. p.protoErr <- err
  264. return
  265. }
  266. ping.Reset(pingInterval)
  267. case <-p.closed:
  268. return
  269. }
  270. }
  271. }
  272. func (p *Peer) readLoop(errc chan<- error) {
  273. defer p.wg.Done()
  274. for {
  275. msg, err := p.rw.ReadMsg()
  276. if err != nil {
  277. errc <- err
  278. return
  279. }
  280. msg.ReceivedAt = time.Now()
  281. if err = p.handle(msg); err != nil {
  282. errc <- err
  283. return
  284. }
  285. }
  286. }
  287. func (p *Peer) handle(msg Msg) error {
  288. switch {
  289. case msg.Code == pingMsg:
  290. msg.Discard()
  291. go SendItems(p.rw, pongMsg)
  292. case msg.Code == discMsg:
  293. // This is the last message. We don't need to discard or
  294. // check errors because, the connection will be closed after it.
  295. var m struct{ R DiscReason }
  296. rlp.Decode(msg.Payload, &m)
  297. return m.R
  298. case msg.Code < baseProtocolLength:
  299. // ignore other base protocol messages
  300. return msg.Discard()
  301. default:
  302. // it's a subprotocol message
  303. proto, err := p.getProto(msg.Code)
  304. if err != nil {
  305. return fmt.Errorf("msg code out of range: %v", msg.Code)
  306. }
  307. if metrics.Enabled {
  308. m := fmt.Sprintf("%s/%s/%d/%#02x", ingressMeterName, proto.Name, proto.Version, msg.Code-proto.offset)
  309. metrics.GetOrRegisterMeter(m, nil).Mark(int64(msg.meterSize))
  310. metrics.GetOrRegisterMeter(m+"/packets", nil).Mark(1)
  311. }
  312. select {
  313. case proto.in <- msg:
  314. return nil
  315. case <-p.closed:
  316. return io.EOF
  317. }
  318. }
  319. return nil
  320. }
  321. func countMatchingProtocols(protocols []Protocol, caps []Cap) int {
  322. n := 0
  323. for _, cap := range caps {
  324. for _, proto := range protocols {
  325. if proto.Name == cap.Name && proto.Version == cap.Version {
  326. n++
  327. }
  328. }
  329. }
  330. return n
  331. }
  332. // matchProtocols creates structures for matching named subprotocols.
  333. func matchProtocols(protocols []Protocol, caps []Cap, rw MsgReadWriter) map[string]*protoRW {
  334. sort.Sort(capsByNameAndVersion(caps))
  335. offset := baseProtocolLength
  336. result := make(map[string]*protoRW)
  337. outer:
  338. for _, cap := range caps {
  339. for _, proto := range protocols {
  340. if proto.Name == cap.Name && proto.Version == cap.Version {
  341. // If an old protocol version matched, revert it
  342. if old := result[cap.Name]; old != nil {
  343. offset -= old.Length
  344. }
  345. // Assign the new match
  346. result[cap.Name] = &protoRW{Protocol: proto, offset: offset, in: make(chan Msg), w: rw}
  347. offset += proto.Length
  348. continue outer
  349. }
  350. }
  351. }
  352. return result
  353. }
  354. func (p *Peer) startProtocols(writeStart <-chan struct{}, writeErr chan<- error) {
  355. p.wg.Add(len(p.running))
  356. for _, proto := range p.running {
  357. proto := proto
  358. proto.closed = p.closed
  359. proto.wstart = writeStart
  360. proto.werr = writeErr
  361. var rw MsgReadWriter = proto
  362. if p.events != nil {
  363. rw = newMsgEventer(rw, p.events, p.ID(), proto.Name, p.Info().Network.RemoteAddress, p.Info().Network.LocalAddress)
  364. }
  365. p.log.Trace(fmt.Sprintf("Starting protocol %s/%d", proto.Name, proto.Version))
  366. go func() {
  367. defer p.wg.Done()
  368. err := proto.Run(p, rw)
  369. if err == nil {
  370. p.log.Trace(fmt.Sprintf("Protocol %s/%d returned", proto.Name, proto.Version))
  371. err = errProtocolReturned
  372. } else if !errors.Is(err, io.EOF) {
  373. p.log.Trace(fmt.Sprintf("Protocol %s/%d failed", proto.Name, proto.Version), "err", err)
  374. }
  375. p.protoErr <- err
  376. }()
  377. }
  378. }
  379. // getProto finds the protocol responsible for handling
  380. // the given message code.
  381. func (p *Peer) getProto(code uint64) (*protoRW, error) {
  382. for _, proto := range p.running {
  383. if code >= proto.offset && code < proto.offset+proto.Length {
  384. return proto, nil
  385. }
  386. }
  387. return nil, newPeerError(errInvalidMsgCode, "%d", code)
  388. }
  389. type protoRW struct {
  390. Protocol
  391. in chan Msg // receives read messages
  392. closed <-chan struct{} // receives when peer is shutting down
  393. wstart <-chan struct{} // receives when write may start
  394. werr chan<- error // for write results
  395. offset uint64
  396. w MsgWriter
  397. }
  398. func (rw *protoRW) WriteMsg(msg Msg) (err error) {
  399. if msg.Code >= rw.Length {
  400. return newPeerError(errInvalidMsgCode, "not handled")
  401. }
  402. msg.meterCap = rw.cap()
  403. msg.meterCode = msg.Code
  404. msg.Code += rw.offset
  405. select {
  406. case <-rw.wstart:
  407. err = rw.w.WriteMsg(msg)
  408. // Report write status back to Peer.run. It will initiate
  409. // shutdown if the error is non-nil and unblock the next write
  410. // otherwise. The calling protocol code should exit for errors
  411. // as well but we don't want to rely on that.
  412. rw.werr <- err
  413. case <-rw.closed:
  414. err = ErrShuttingDown
  415. }
  416. return err
  417. }
  418. func (rw *protoRW) ReadMsg() (Msg, error) {
  419. select {
  420. case msg := <-rw.in:
  421. msg.Code -= rw.offset
  422. return msg, nil
  423. case <-rw.closed:
  424. return Msg{}, io.EOF
  425. }
  426. }
  427. // PeerInfo represents a short summary of the information known about a connected
  428. // peer. Sub-protocol independent fields are contained and initialized here, with
  429. // protocol specifics delegated to all connected sub-protocols.
  430. type PeerInfo struct {
  431. ENR string `json:"enr,omitempty"` // Ethereum Node Record
  432. Enode string `json:"enode"` // Node URL
  433. ID string `json:"id"` // Unique node identifier
  434. Name string `json:"name"` // Name of the node, including client type, version, OS, custom data
  435. Caps []string `json:"caps"` // Protocols advertised by this peer
  436. Network struct {
  437. LocalAddress string `json:"localAddress"` // Local endpoint of the TCP data connection
  438. RemoteAddress string `json:"remoteAddress"` // Remote endpoint of the TCP data connection
  439. Inbound bool `json:"inbound"`
  440. Trusted bool `json:"trusted"`
  441. Static bool `json:"static"`
  442. } `json:"network"`
  443. Protocols map[string]interface{} `json:"protocols"` // Sub-protocol specific metadata fields
  444. }
  445. // Info gathers and returns a collection of metadata known about a peer.
  446. func (p *Peer) Info() *PeerInfo {
  447. // Gather the protocol capabilities
  448. var caps []string
  449. for _, cap := range p.Caps() {
  450. caps = append(caps, cap.String())
  451. }
  452. // Assemble the generic peer metadata
  453. info := &PeerInfo{
  454. Enode: p.Node().URLv4(),
  455. ID: p.ID().String(),
  456. Name: p.Fullname(),
  457. Caps: caps,
  458. Protocols: make(map[string]interface{}),
  459. }
  460. if p.Node().Seq() > 0 {
  461. info.ENR = p.Node().String()
  462. }
  463. info.Network.LocalAddress = p.LocalAddr().String()
  464. info.Network.RemoteAddress = p.RemoteAddr().String()
  465. info.Network.Inbound = p.rw.is(inboundConn)
  466. info.Network.Trusted = p.rw.is(trustedConn)
  467. info.Network.Static = p.rw.is(staticDialedConn)
  468. // Gather all the running protocol infos
  469. for _, proto := range p.running {
  470. protoInfo := interface{}("unknown")
  471. if query := proto.Protocol.PeerInfo; query != nil {
  472. if metadata := query(p.ID()); metadata != nil {
  473. protoInfo = metadata
  474. } else {
  475. protoInfo = "handshake"
  476. }
  477. }
  478. info.Protocols[proto.Name] = protoInfo
  479. }
  480. return info
  481. }