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.

367 lines
9.8 KiB

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