aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/server.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2015-04-10 19:25:35 +0800
committerFelix Lange <fjl@twurst.com>2015-04-10 22:57:56 +0800
commitb3c058a9e4e9296583ba516c537768b96a2fb8a0 (patch)
tree031181c2a7691a063808d567c8e53d4d491d0e27 /p2p/server.go
parent99a1db2d4076861d19e2bd704732ff91d509f34b (diff)
downloaddexon-b3c058a9e4e9296583ba516c537768b96a2fb8a0.tar
dexon-b3c058a9e4e9296583ba516c537768b96a2fb8a0.tar.gz
dexon-b3c058a9e4e9296583ba516c537768b96a2fb8a0.tar.bz2
dexon-b3c058a9e4e9296583ba516c537768b96a2fb8a0.tar.lz
dexon-b3c058a9e4e9296583ba516c537768b96a2fb8a0.tar.xz
dexon-b3c058a9e4e9296583ba516c537768b96a2fb8a0.tar.zst
dexon-b3c058a9e4e9296583ba516c537768b96a2fb8a0.zip
p2p: improve disconnect signaling at handshake time
As of this commit, p2p will disconnect nodes directly after the encryption handshake if too many peer connections are active. Errors in the protocol handshake packet are now handled more politely by sending a disconnect packet before closing the connection.
Diffstat (limited to 'p2p/server.go')
-rw-r--r--p2p/server.go16
1 files changed, 10 insertions, 6 deletions
diff --git a/p2p/server.go b/p2p/server.go
index d227d477a..88f7ba2ec 100644
--- a/p2p/server.go
+++ b/p2p/server.go
@@ -99,7 +99,7 @@ type Server struct {
peerConnect chan *discover.Node
}
-type setupFunc func(net.Conn, *ecdsa.PrivateKey, *protoHandshake, *discover.Node) (*conn, error)
+type setupFunc func(net.Conn, *ecdsa.PrivateKey, *protoHandshake, *discover.Node, bool) (*conn, error)
type newPeerHook func(*Peer)
// Peers returns all connected peers.
@@ -261,6 +261,11 @@ func (srv *Server) Stop() {
srv.peerWG.Wait()
}
+// Self returns the local node's endpoint information.
+func (srv *Server) Self() *discover.Node {
+ return srv.ntab.Self()
+}
+
// main loop for adding connections via listening
func (srv *Server) listenLoop() {
defer srv.loopWG.Done()
@@ -354,10 +359,6 @@ func (srv *Server) dialNode(dest *discover.Node) {
srv.startPeer(conn, dest)
}
-func (srv *Server) Self() *discover.Node {
- return srv.ntab.Self()
-}
-
func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
// TODO: handle/store session token
@@ -366,7 +367,10 @@ func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
// returns during that exchange need to call peerWG.Done because
// the callers of startPeer added the peer to the wait group already.
fd.SetDeadline(time.Now().Add(handshakeTimeout))
- conn, err := srv.setupFunc(fd, srv.PrivateKey, srv.ourHandshake, dest)
+ srv.lock.RLock()
+ atcap := len(srv.peers) == srv.MaxPeers
+ srv.lock.RUnlock()
+ conn, err := srv.setupFunc(fd, srv.PrivateKey, srv.ourHandshake, dest, atcap)
if err != nil {
fd.Close()
glog.V(logger.Debug).Infof("Handshake with %v failed: %v", fd.RemoteAddr(), err)