You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

335 lines
8.8 KiB

9 years ago
9 years ago
9 years ago
7 years ago
7 years ago
7 years ago
9 years ago
8 years ago
9 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
p2p: peer should respect errors from SetDeadline Noticed while auditing the code that we aren't respecting (*net.Conn) SetDeadline errors which return after a connection has been killed and is simultaneously being used. For example given program, without SetDeadline error checks ```go package main import ( "log" "net" "time" ) func main() { conn, err := net.Dial("tcp", "tendermint.com:443") if err != nil { log.Fatal(err) } go func() { <-time.After(400 * time.Millisecond) conn.Close() }() for i := 0; i < 5; i++ { if err := conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))); err != nil { log.Fatalf("set deadline #%d, err: %v", i, err) } log.Printf("Successfully set deadline #%d", i) <-time.After(150 * time.Millisecond) } } ``` erraneously gives ```shell 2017/11/14 17:46:28 Successfully set deadline #0 2017/11/14 17:46:29 Successfully set deadline #1 2017/11/14 17:46:29 Successfully set deadline #2 2017/11/14 17:46:29 Successfully set deadline #3 2017/11/14 17:46:29 Successfully set deadline #4 ``` However, if we properly fix it to respect that error with ```diff --- wild.go 2017-11-14 17:44:38.000000000 -0700 +++ main.go 2017-11-14 17:45:40.000000000 -0700 @@ -16,7 +16,9 @@ conn.Close() }() for i := 0; i < 5; i++ { - conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))) + if err := conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))); err != nil { + log.Fatalf("set deadline #%d, err: %v", i, err) + } log.Printf("Successfully set deadline #%d", i) <-time.After(150 * time.Millisecond) } ``` properly catches any problems and gives ```shell $ go run main.go 2017/11/14 17:43:44 Successfully set deadline #0 2017/11/14 17:43:45 Successfully set deadline #1 2017/11/14 17:43:45 Successfully set deadline #2 2017/11/14 17:43:45 set deadline #3, err: set tcp 10.182.253.51:57395: use of closed network connection exit status 1 ```
7 years ago
8 years ago
8 years ago
7 years ago
8 years ago
8 years ago
9 years ago
7 years ago
9 years ago
p2p: peer should respect errors from SetDeadline Noticed while auditing the code that we aren't respecting (*net.Conn) SetDeadline errors which return after a connection has been killed and is simultaneously being used. For example given program, without SetDeadline error checks ```go package main import ( "log" "net" "time" ) func main() { conn, err := net.Dial("tcp", "tendermint.com:443") if err != nil { log.Fatal(err) } go func() { <-time.After(400 * time.Millisecond) conn.Close() }() for i := 0; i < 5; i++ { if err := conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))); err != nil { log.Fatalf("set deadline #%d, err: %v", i, err) } log.Printf("Successfully set deadline #%d", i) <-time.After(150 * time.Millisecond) } } ``` erraneously gives ```shell 2017/11/14 17:46:28 Successfully set deadline #0 2017/11/14 17:46:29 Successfully set deadline #1 2017/11/14 17:46:29 Successfully set deadline #2 2017/11/14 17:46:29 Successfully set deadline #3 2017/11/14 17:46:29 Successfully set deadline #4 ``` However, if we properly fix it to respect that error with ```diff --- wild.go 2017-11-14 17:44:38.000000000 -0700 +++ main.go 2017-11-14 17:45:40.000000000 -0700 @@ -16,7 +16,9 @@ conn.Close() }() for i := 0; i < 5; i++ { - conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))) + if err := conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))); err != nil { + log.Fatalf("set deadline #%d, err: %v", i, err) + } log.Printf("Successfully set deadline #%d", i) <-time.After(150 * time.Millisecond) } ``` properly catches any problems and gives ```shell $ go run main.go 2017/11/14 17:43:44 Successfully set deadline #0 2017/11/14 17:43:45 Successfully set deadline #1 2017/11/14 17:43:45 Successfully set deadline #2 2017/11/14 17:43:45 set deadline #3, err: set tcp 10.182.253.51:57395: use of closed network connection exit status 1 ```
7 years ago
9 years ago
9 years ago
9 years ago
8 years ago
9 years ago
9 years ago
9 years ago
p2p: peer should respect errors from SetDeadline Noticed while auditing the code that we aren't respecting (*net.Conn) SetDeadline errors which return after a connection has been killed and is simultaneously being used. For example given program, without SetDeadline error checks ```go package main import ( "log" "net" "time" ) func main() { conn, err := net.Dial("tcp", "tendermint.com:443") if err != nil { log.Fatal(err) } go func() { <-time.After(400 * time.Millisecond) conn.Close() }() for i := 0; i < 5; i++ { if err := conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))); err != nil { log.Fatalf("set deadline #%d, err: %v", i, err) } log.Printf("Successfully set deadline #%d", i) <-time.After(150 * time.Millisecond) } } ``` erraneously gives ```shell 2017/11/14 17:46:28 Successfully set deadline #0 2017/11/14 17:46:29 Successfully set deadline #1 2017/11/14 17:46:29 Successfully set deadline #2 2017/11/14 17:46:29 Successfully set deadline #3 2017/11/14 17:46:29 Successfully set deadline #4 ``` However, if we properly fix it to respect that error with ```diff --- wild.go 2017-11-14 17:44:38.000000000 -0700 +++ main.go 2017-11-14 17:45:40.000000000 -0700 @@ -16,7 +16,9 @@ conn.Close() }() for i := 0; i < 5; i++ { - conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))) + if err := conn.SetDeadline(time.Now().Add(time.Duration(10 * time.Second))); err != nil { + log.Fatalf("set deadline #%d, err: %v", i, err) + } log.Printf("Successfully set deadline #%d", i) <-time.After(150 * time.Millisecond) } ``` properly catches any problems and gives ```shell $ go run main.go 2017/11/14 17:43:44 Successfully set deadline #0 2017/11/14 17:43:45 Successfully set deadline #1 2017/11/14 17:43:45 Successfully set deadline #2 2017/11/14 17:43:45 set deadline #3, err: set tcp 10.182.253.51:57395: use of closed network connection exit status 1 ```
7 years ago
7 years ago
7 years ago
9 years ago
7 years ago
9 years ago
7 years ago
9 years ago
9 years ago
9 years ago
7 years ago
8 years ago
7 years ago
8 years ago
  1. package p2p
  2. import (
  3. "fmt"
  4. "net"
  5. "time"
  6. "github.com/pkg/errors"
  7. crypto "github.com/tendermint/go-crypto"
  8. wire "github.com/tendermint/go-wire"
  9. cmn "github.com/tendermint/tmlibs/common"
  10. "github.com/tendermint/tmlibs/log"
  11. )
  12. // Peer is an interface representing a peer connected on a reactor.
  13. type Peer interface {
  14. cmn.Service
  15. ID() ID // peer's cryptographic ID
  16. IsOutbound() bool // did we dial the peer
  17. IsPersistent() bool // do we redial this peer when we disconnect
  18. NodeInfo() NodeInfo // peer's info
  19. Status() ConnectionStatus
  20. Send(byte, interface{}) bool
  21. TrySend(byte, interface{}) bool
  22. Set(string, interface{})
  23. Get(string) interface{}
  24. }
  25. //----------------------------------------------------------
  26. // peer implements Peer.
  27. //
  28. // Before using a peer, you will need to perform a handshake on connection.
  29. type peer struct {
  30. cmn.BaseService
  31. outbound bool
  32. conn net.Conn // source connection
  33. mconn *MConnection // multiplex connection
  34. persistent bool
  35. config *PeerConfig
  36. nodeInfo NodeInfo
  37. Data *cmn.CMap // User data.
  38. }
  39. // PeerConfig is a Peer configuration.
  40. type PeerConfig struct {
  41. AuthEnc bool `mapstructure:"auth_enc"` // authenticated encryption
  42. // times are in seconds
  43. HandshakeTimeout time.Duration `mapstructure:"handshake_timeout"`
  44. DialTimeout time.Duration `mapstructure:"dial_timeout"`
  45. MConfig *MConnConfig `mapstructure:"connection"`
  46. Fuzz bool `mapstructure:"fuzz"` // fuzz connection (for testing)
  47. FuzzConfig *FuzzConnConfig `mapstructure:"fuzz_config"`
  48. }
  49. // DefaultPeerConfig returns the default config.
  50. func DefaultPeerConfig() *PeerConfig {
  51. return &PeerConfig{
  52. AuthEnc: true,
  53. HandshakeTimeout: 20, // * time.Second,
  54. DialTimeout: 3, // * time.Second,
  55. MConfig: DefaultMConnConfig(),
  56. Fuzz: false,
  57. FuzzConfig: DefaultFuzzConnConfig(),
  58. }
  59. }
  60. func newOutboundPeer(addr *NetAddress, reactorsByCh map[byte]Reactor, chDescs []*ChannelDescriptor,
  61. onPeerError func(Peer, interface{}), ourNodePrivKey crypto.PrivKey, config *PeerConfig, persistent bool) (*peer, error) {
  62. conn, err := dial(addr, config)
  63. if err != nil {
  64. return nil, errors.Wrap(err, "Error creating peer")
  65. }
  66. peer, err := newPeerFromConnAndConfig(conn, true, reactorsByCh, chDescs, onPeerError, ourNodePrivKey, config)
  67. if err != nil {
  68. if err := conn.Close(); err != nil {
  69. return nil, err
  70. }
  71. return nil, err
  72. }
  73. peer.persistent = persistent
  74. return peer, nil
  75. }
  76. func newInboundPeer(conn net.Conn, reactorsByCh map[byte]Reactor, chDescs []*ChannelDescriptor,
  77. onPeerError func(Peer, interface{}), ourNodePrivKey crypto.PrivKey, config *PeerConfig) (*peer, error) {
  78. // TODO: issue PoW challenge
  79. return newPeerFromConnAndConfig(conn, false, reactorsByCh, chDescs, onPeerError, ourNodePrivKey, config)
  80. }
  81. func newPeerFromConnAndConfig(rawConn net.Conn, outbound bool, reactorsByCh map[byte]Reactor, chDescs []*ChannelDescriptor,
  82. onPeerError func(Peer, interface{}), ourNodePrivKey crypto.PrivKey, config *PeerConfig) (*peer, error) {
  83. conn := rawConn
  84. // Fuzz connection
  85. if config.Fuzz {
  86. // so we have time to do peer handshakes and get set up
  87. conn = FuzzConnAfterFromConfig(conn, 10*time.Second, config.FuzzConfig)
  88. }
  89. // Encrypt connection
  90. if config.AuthEnc {
  91. if err := conn.SetDeadline(time.Now().Add(config.HandshakeTimeout * time.Second)); err != nil {
  92. return nil, errors.Wrap(err, "Error setting deadline while encrypting connection")
  93. }
  94. var err error
  95. conn, err = MakeSecretConnection(conn, ourNodePrivKey)
  96. if err != nil {
  97. return nil, errors.Wrap(err, "Error creating peer")
  98. }
  99. }
  100. // NodeInfo is set after Handshake
  101. p := &peer{
  102. outbound: outbound,
  103. conn: conn,
  104. config: config,
  105. Data: cmn.NewCMap(),
  106. }
  107. p.mconn = createMConnection(conn, p, reactorsByCh, chDescs, onPeerError, config.MConfig)
  108. p.BaseService = *cmn.NewBaseService(nil, "Peer", p)
  109. return p, nil
  110. }
  111. //---------------------------------------------------
  112. // Implements cmn.Service
  113. // SetLogger implements BaseService.
  114. func (p *peer) SetLogger(l log.Logger) {
  115. p.Logger = l
  116. p.mconn.SetLogger(l)
  117. }
  118. // OnStart implements BaseService.
  119. func (p *peer) OnStart() error {
  120. if err := p.BaseService.OnStart(); err != nil {
  121. return err
  122. }
  123. err := p.mconn.Start()
  124. return err
  125. }
  126. // OnStop implements BaseService.
  127. func (p *peer) OnStop() {
  128. p.BaseService.OnStop()
  129. p.mconn.Stop() // stop everything and close the conn
  130. }
  131. //---------------------------------------------------
  132. // Implements Peer
  133. // ID returns the peer's ID - the hex encoded hash of its pubkey.
  134. func (p *peer) ID() ID {
  135. return PubKeyToID(p.PubKey())
  136. }
  137. // IsOutbound returns true if the connection is outbound, false otherwise.
  138. func (p *peer) IsOutbound() bool {
  139. return p.outbound
  140. }
  141. // IsPersistent returns true if the peer is persitent, false otherwise.
  142. func (p *peer) IsPersistent() bool {
  143. return p.persistent
  144. }
  145. // NodeInfo returns a copy of the peer's NodeInfo.
  146. func (p *peer) NodeInfo() NodeInfo {
  147. return p.nodeInfo
  148. }
  149. // Status returns the peer's ConnectionStatus.
  150. func (p *peer) Status() ConnectionStatus {
  151. return p.mconn.Status()
  152. }
  153. // Send msg to the channel identified by chID byte. Returns false if the send
  154. // queue is full after timeout, specified by MConnection.
  155. func (p *peer) Send(chID byte, msg interface{}) bool {
  156. if !p.IsRunning() {
  157. // see Switch#Broadcast, where we fetch the list of peers and loop over
  158. // them - while we're looping, one peer may be removed and stopped.
  159. return false
  160. }
  161. return p.mconn.Send(chID, msg)
  162. }
  163. // TrySend msg to the channel identified by chID byte. Immediately returns
  164. // false if the send queue is full.
  165. func (p *peer) TrySend(chID byte, msg interface{}) bool {
  166. if !p.IsRunning() {
  167. return false
  168. }
  169. return p.mconn.TrySend(chID, msg)
  170. }
  171. // Get the data for a given key.
  172. func (p *peer) Get(key string) interface{} {
  173. return p.Data.Get(key)
  174. }
  175. // Set sets the data for the given key.
  176. func (p *peer) Set(key string, data interface{}) {
  177. p.Data.Set(key, data)
  178. }
  179. //---------------------------------------------------
  180. // methods used by the Switch
  181. // CloseConn should be called by the Switch if the peer was created but never started.
  182. func (p *peer) CloseConn() {
  183. p.conn.Close() // nolint: errcheck
  184. }
  185. // HandshakeTimeout performs the Tendermint P2P handshake between a given node and the peer
  186. // by exchanging their NodeInfo. It sets the received nodeInfo on the peer.
  187. // NOTE: blocking
  188. func (p *peer) HandshakeTimeout(ourNodeInfo NodeInfo, timeout time.Duration) error {
  189. // Set deadline for handshake so we don't block forever on conn.ReadFull
  190. if err := p.conn.SetDeadline(time.Now().Add(timeout)); err != nil {
  191. return errors.Wrap(err, "Error setting deadline")
  192. }
  193. var peerNodeInfo NodeInfo
  194. var err1 error
  195. var err2 error
  196. cmn.Parallel(
  197. func() {
  198. var n int
  199. wire.WriteBinary(&ourNodeInfo, p.conn, &n, &err1)
  200. },
  201. func() {
  202. var n int
  203. wire.ReadBinary(&peerNodeInfo, p.conn, maxNodeInfoSize, &n, &err2)
  204. p.Logger.Info("Peer handshake", "peerNodeInfo", peerNodeInfo)
  205. })
  206. if err1 != nil {
  207. return errors.Wrap(err1, "Error during handshake/write")
  208. }
  209. if err2 != nil {
  210. return errors.Wrap(err2, "Error during handshake/read")
  211. }
  212. // Remove deadline
  213. if err := p.conn.SetDeadline(time.Time{}); err != nil {
  214. return errors.Wrap(err, "Error removing deadline")
  215. }
  216. // TODO: fix the peerNodeInfo.ListenAddr
  217. p.nodeInfo = peerNodeInfo
  218. return nil
  219. }
  220. // Addr returns peer's remote network address.
  221. func (p *peer) Addr() net.Addr {
  222. return p.conn.RemoteAddr()
  223. }
  224. // PubKey returns peer's public key.
  225. func (p *peer) PubKey() crypto.PubKey {
  226. if !p.nodeInfo.PubKey.Empty() {
  227. return p.nodeInfo.PubKey
  228. } else if p.config.AuthEnc {
  229. return p.conn.(*SecretConnection).RemotePubKey()
  230. }
  231. panic("Attempt to get peer's PubKey before calling Handshake")
  232. }
  233. // CanSend returns true if the send queue is not full, false otherwise.
  234. func (p *peer) CanSend(chID byte) bool {
  235. if !p.IsRunning() {
  236. return false
  237. }
  238. return p.mconn.CanSend(chID)
  239. }
  240. // String representation.
  241. func (p *peer) String() string {
  242. if p.outbound {
  243. return fmt.Sprintf("Peer{%v %v out}", p.mconn, p.ID())
  244. }
  245. return fmt.Sprintf("Peer{%v %v in}", p.mconn, p.ID())
  246. }
  247. //------------------------------------------------------------------
  248. // helper funcs
  249. func dial(addr *NetAddress, config *PeerConfig) (net.Conn, error) {
  250. conn, err := addr.DialTimeout(config.DialTimeout * time.Second)
  251. if err != nil {
  252. return nil, err
  253. }
  254. return conn, nil
  255. }
  256. func createMConnection(conn net.Conn, p *peer, reactorsByCh map[byte]Reactor, chDescs []*ChannelDescriptor,
  257. onPeerError func(Peer, interface{}), config *MConnConfig) *MConnection {
  258. onReceive := func(chID byte, msgBytes []byte) {
  259. reactor := reactorsByCh[chID]
  260. if reactor == nil {
  261. cmn.PanicSanity(cmn.Fmt("Unknown channel %X", chID))
  262. }
  263. reactor.Receive(chID, p, msgBytes)
  264. }
  265. onError := func(r interface{}) {
  266. onPeerError(p, r)
  267. }
  268. return NewMConnectionWithConfig(conn, chDescs, onReceive, onError, config)
  269. }