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.

358 lines
9.5 KiB

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