Browse Source

SetDeadline for authEnc. Stop peer if Add fails

pull/456/head
Ethan Buchman 8 years ago
parent
commit
a9bb6734e7
3 changed files with 9 additions and 2 deletions
  1. +6
    -0
      peer.go
  2. +2
    -1
      switch.go
  3. +1
    -1
      switch_test.go

+ 6
- 0
peer.go View File

@ -48,12 +48,18 @@ func newPeerFromExistingConn(conn net.Conn, outbound bool, reactorsByCh map[byte
// Encrypt connection // Encrypt connection
if config.GetBool(configKeyAuthEnc) { if config.GetBool(configKeyAuthEnc) {
var err error var err error
// Set deadline for handshake so we don't block forever on conn.ReadFull
timeout := time.Duration(config.GetInt(configKeyHandshakeTimeoutSeconds)) * time.Second
conn.SetDeadline(time.Now().Add(timeout))
conn, err = MakeSecretConnection(conn, privKey) conn, err = MakeSecretConnection(conn, privKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// remove deadline
conn.SetDeadline(time.Time{})
} }
// Key and NodeInfo are set after Handshake
p := &Peer{ p := &Peer{
outbound: outbound, outbound: outbound,
authEnc: config.GetBool(configKeyAuthEnc), authEnc: config.GetBool(configKeyAuthEnc),


+ 2
- 1
switch.go View File

@ -226,6 +226,7 @@ func (sw *Switch) AddPeer(peer *Peer) error {
// ignore if duplicate or if we already have too many for that IP range // ignore if duplicate or if we already have too many for that IP range
if err := sw.peers.Add(peer); err != nil { if err := sw.peers.Add(peer); err != nil {
log.Notice("Ignoring peer", "error", err, "peer", peer) log.Notice("Ignoring peer", "error", err, "peer", peer)
peer.Stop()
return err return err
} }
@ -544,7 +545,7 @@ func makeSwitch(i int, network, version string, initSwitch func(int, *Switch) *S
return s return s
} }
// AddPeerWithConnection is a helper function for testing.
// AddPeerWithConnection creates a newPeer from the connection, performs the handshake, and adds it to the switch.
func (sw *Switch) AddPeerWithConnection(conn net.Conn, outbound bool, reactorsByCh map[byte]Reactor, chDescs []*ChannelDescriptor, onPeerError func(*Peer, interface{}), config cfg.Config, privKey crypto.PrivKeyEd25519) error { func (sw *Switch) AddPeerWithConnection(conn net.Conn, outbound bool, reactorsByCh map[byte]Reactor, chDescs []*ChannelDescriptor, onPeerError func(*Peer, interface{}), config cfg.Config, privKey crypto.PrivKeyEd25519) error {
peer, err := newPeerFromExistingConn(conn, outbound, reactorsByCh, chDescs, onPeerError, config, privKey) peer, err := newPeerFromExistingConn(conn, outbound, reactorsByCh, chDescs, onPeerError, config, privKey)
if err != nil { if err != nil {


+ 1
- 1
switch_test.go View File

@ -260,7 +260,7 @@ func TestSwitchStopsNonPersistentPeerOnError(t *testing.T) {
assert.False(peer.IsRunning()) assert.False(peer.IsRunning())
} }
func TestSwitchReconnectsToPeerIfItIsPersistent(t *testing.T) {
func TestSwitchReconnectsToPersistentPeer(t *testing.T) {
assert, require := assert.New(t), require.New(t) assert, require := assert.New(t), require.New(t)
sw := makeSwitch(1, "testing", "123.123.123", initSwitchFunc) sw := makeSwitch(1, "testing", "123.123.123", initSwitchFunc)


Loading…
Cancel
Save