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.

361 lines
9.7 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
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
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. // NOTE: probably will want to remove this
  194. // but could be helpful while the feature is new
  195. p.Logger.Debug("Unknown channel for peer", "channel", chID, "channels", p.channels)
  196. return false
  197. }
  198. //---------------------------------------------------
  199. // methods used by the Switch
  200. // CloseConn should be called by the Switch if the peer was created but never started.
  201. func (p *peer) CloseConn() {
  202. p.conn.Close() // nolint: errcheck
  203. }
  204. // HandshakeTimeout performs the Tendermint P2P handshake between a given node and the peer
  205. // by exchanging their NodeInfo. It sets the received nodeInfo on the peer.
  206. // NOTE: blocking
  207. func (p *peer) HandshakeTimeout(ourNodeInfo NodeInfo, timeout time.Duration) error {
  208. // Set deadline for handshake so we don't block forever on conn.ReadFull
  209. if err := p.conn.SetDeadline(time.Now().Add(timeout)); err != nil {
  210. return errors.Wrap(err, "Error setting deadline")
  211. }
  212. var peerNodeInfo NodeInfo
  213. var err1 error
  214. var err2 error
  215. cmn.Parallel(
  216. func() {
  217. var n int
  218. wire.WriteBinary(&ourNodeInfo, p.conn, &n, &err1)
  219. },
  220. func() {
  221. var n int
  222. wire.ReadBinary(&peerNodeInfo, p.conn, MaxNodeInfoSize(), &n, &err2)
  223. p.Logger.Info("Peer handshake", "peerNodeInfo", peerNodeInfo)
  224. })
  225. if err1 != nil {
  226. return errors.Wrap(err1, "Error during handshake/write")
  227. }
  228. if err2 != nil {
  229. return errors.Wrap(err2, "Error during handshake/read")
  230. }
  231. // Remove deadline
  232. if err := p.conn.SetDeadline(time.Time{}); err != nil {
  233. return errors.Wrap(err, "Error removing deadline")
  234. }
  235. p.setNodeInfo(peerNodeInfo)
  236. return nil
  237. }
  238. func (p *peer) setNodeInfo(nodeInfo NodeInfo) {
  239. p.nodeInfo = nodeInfo
  240. // cache the channels so we dont copy nodeInfo
  241. // every time we check hasChannel
  242. p.channels = nodeInfo.Channels
  243. }
  244. // Addr returns peer's remote network address.
  245. func (p *peer) Addr() net.Addr {
  246. return p.conn.RemoteAddr()
  247. }
  248. // PubKey returns peer's public key.
  249. func (p *peer) PubKey() crypto.PubKey {
  250. if !p.nodeInfo.PubKey.Empty() {
  251. return p.nodeInfo.PubKey
  252. } else if p.config.AuthEnc {
  253. return p.conn.(*tmconn.SecretConnection).RemotePubKey()
  254. }
  255. panic("Attempt to get peer's PubKey before calling Handshake")
  256. }
  257. // CanSend returns true if the send queue is not full, false otherwise.
  258. func (p *peer) CanSend(chID byte) bool {
  259. if !p.IsRunning() {
  260. return false
  261. }
  262. return p.mconn.CanSend(chID)
  263. }
  264. // String representation.
  265. func (p *peer) String() string {
  266. if p.outbound {
  267. return fmt.Sprintf("Peer{%v %v out}", p.mconn, p.ID())
  268. }
  269. return fmt.Sprintf("Peer{%v %v in}", p.mconn, p.ID())
  270. }
  271. //------------------------------------------------------------------
  272. // helper funcs
  273. func dial(addr *NetAddress, config *PeerConfig) (net.Conn, error) {
  274. conn, err := addr.DialTimeout(config.DialTimeout * time.Second)
  275. if err != nil {
  276. return nil, err
  277. }
  278. return conn, nil
  279. }
  280. func createMConnection(conn net.Conn, p *peer, reactorsByCh map[byte]Reactor, chDescs []*tmconn.ChannelDescriptor,
  281. onPeerError func(Peer, interface{}), config *tmconn.MConnConfig) *tmconn.MConnection {
  282. onReceive := func(chID byte, msgBytes []byte) {
  283. reactor := reactorsByCh[chID]
  284. if reactor == nil {
  285. cmn.PanicSanity(cmn.Fmt("Unknown channel %X", chID))
  286. }
  287. reactor.Receive(chID, p, msgBytes)
  288. }
  289. onError := func(r interface{}) {
  290. onPeerError(p, r)
  291. }
  292. return tmconn.NewMConnectionWithConfig(conn, chDescs, onReceive, onError, config)
  293. }