SetDeadline for authEnc. Stop peer if Add fails

This commit is contained in:
Ethan Buchman 2017-04-10 15:59:32 -04:00
parent 8bb3a2e1d7
commit a9bb6734e7
3 changed files with 9 additions and 2 deletions

View File

@ -48,12 +48,18 @@ func newPeerFromExistingConn(conn net.Conn, outbound bool, reactorsByCh map[byte
// Encrypt connection
if config.GetBool(configKeyAuthEnc) {
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)
if err != nil {
return nil, err
}
// remove deadline
conn.SetDeadline(time.Time{})
}
// Key and NodeInfo are set after Handshake
p := &Peer{
outbound: outbound,
authEnc: config.GetBool(configKeyAuthEnc),

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
if err := sw.peers.Add(peer); err != nil {
log.Notice("Ignoring peer", "error", err, "peer", peer)
peer.Stop()
return err
}
@ -544,7 +545,7 @@ func makeSwitch(i int, network, version string, initSwitch func(int, *Switch) *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 {
peer, err := newPeerFromExistingConn(conn, outbound, reactorsByCh, chDescs, onPeerError, config, privKey)
if err != nil {

View File

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