handshake.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2015 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 diff
  17. import (
  18. "fmt"
  19. "time"
  20. "github.com/ethereum/go-ethereum/common/gopool"
  21. "github.com/ethereum/go-ethereum/p2p"
  22. )
  23. const (
  24. // handshakeTimeout is the maximum allowed time for the `diff` handshake to
  25. // complete before dropping the connection as malicious.
  26. handshakeTimeout = 5 * time.Second
  27. )
  28. // Handshake executes the diff protocol handshake,
  29. func (p *Peer) Handshake(diffSync bool) error {
  30. // Send out own handshake in a new thread
  31. errc := make(chan error, 2)
  32. var cap DiffCapPacket // safe to read after two values have been received from errc
  33. gopool.Submit(func() {
  34. errc <- p2p.Send(p.rw, DiffCapMsg, &DiffCapPacket{
  35. DiffSync: diffSync,
  36. Extra: defaultExtra,
  37. })
  38. })
  39. gopool.Submit(func() {
  40. errc <- p.readCap(&cap)
  41. })
  42. timeout := time.NewTimer(handshakeTimeout)
  43. defer timeout.Stop()
  44. for i := 0; i < 2; i++ {
  45. select {
  46. case err := <-errc:
  47. if err != nil {
  48. return err
  49. }
  50. case <-timeout.C:
  51. return p2p.DiscReadTimeout
  52. }
  53. }
  54. p.diffSync = cap.DiffSync
  55. return nil
  56. }
  57. // readStatus reads the remote handshake message.
  58. func (p *Peer) readCap(cap *DiffCapPacket) error {
  59. msg, err := p.rw.ReadMsg()
  60. if err != nil {
  61. return err
  62. }
  63. if msg.Code != DiffCapMsg {
  64. return fmt.Errorf("%w: first msg has code %x (!= %x)", errNoCapMsg, msg.Code, DiffCapMsg)
  65. }
  66. if msg.Size > maxMessageSize {
  67. return fmt.Errorf("%w: %v > %v", errMsgTooLarge, msg.Size, maxMessageSize)
  68. }
  69. // Decode the handshake and make sure everything matches
  70. if err := msg.Decode(cap); err != nil {
  71. return fmt.Errorf("%w: message %v: %v", errDecode, msg, err)
  72. }
  73. return nil
  74. }