peer.go 14 KB

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