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.

349 lines
9.0 KiB

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