commit
7c678554b5
|
@ -195,7 +195,7 @@ var (
|
||||||
MaxPeersFlag = cli.IntFlag{
|
MaxPeersFlag = cli.IntFlag{
|
||||||
Name: "maxpeers",
|
Name: "maxpeers",
|
||||||
Usage: "Maximum number of network peers (network disabled if set to 0)",
|
Usage: "Maximum number of network peers (network disabled if set to 0)",
|
||||||
Value: 16,
|
Value: 25,
|
||||||
}
|
}
|
||||||
MaxPendingPeersFlag = cli.IntFlag{
|
MaxPendingPeersFlag = cli.IntFlag{
|
||||||
Name: "maxpendpeers",
|
Name: "maxpendpeers",
|
||||||
|
|
|
@ -65,26 +65,26 @@ type protoHandshake struct {
|
||||||
ID discover.NodeID
|
ID discover.NodeID
|
||||||
}
|
}
|
||||||
|
|
||||||
// setupConn starts a protocol session on the given connection.
|
// setupConn starts a protocol session on the given connection. It
|
||||||
// It runs the encryption handshake and the protocol handshake.
|
// runs the encryption handshake and the protocol handshake. If dial
|
||||||
// If dial is non-nil, the connection the local node is the initiator.
|
// is non-nil, the connection the local node is the initiator. If
|
||||||
// If atcap is true, the connection will be disconnected with DiscTooManyPeers
|
// keepconn returns false, the connection will be disconnected with
|
||||||
// after the key exchange.
|
// DiscTooManyPeers after the key exchange.
|
||||||
func setupConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, atcap bool, trusted map[discover.NodeID]bool) (*conn, error) {
|
func setupConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, keepconn func(discover.NodeID) bool) (*conn, error) {
|
||||||
if dial == nil {
|
if dial == nil {
|
||||||
return setupInboundConn(fd, prv, our, atcap, trusted)
|
return setupInboundConn(fd, prv, our, keepconn)
|
||||||
} else {
|
} else {
|
||||||
return setupOutboundConn(fd, prv, our, dial, atcap, trusted)
|
return setupOutboundConn(fd, prv, our, dial, keepconn)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupInboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, atcap bool, trusted map[discover.NodeID]bool) (*conn, error) {
|
func setupInboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, keepconn func(discover.NodeID) bool) (*conn, error) {
|
||||||
secrets, err := receiverEncHandshake(fd, prv, nil)
|
secrets, err := receiverEncHandshake(fd, prv, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("encryption handshake failed: %v", err)
|
return nil, fmt.Errorf("encryption handshake failed: %v", err)
|
||||||
}
|
}
|
||||||
rw := newRlpxFrameRW(fd, secrets)
|
rw := newRlpxFrameRW(fd, secrets)
|
||||||
if atcap && !trusted[secrets.RemoteID] {
|
if !keepconn(secrets.RemoteID) {
|
||||||
SendItems(rw, discMsg, DiscTooManyPeers)
|
SendItems(rw, discMsg, DiscTooManyPeers)
|
||||||
return nil, errors.New("we have too many peers")
|
return nil, errors.New("we have too many peers")
|
||||||
}
|
}
|
||||||
|
@ -99,13 +99,13 @@ func setupInboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, a
|
||||||
return &conn{rw, rhs}, nil
|
return &conn{rw, rhs}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupOutboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, atcap bool, trusted map[discover.NodeID]bool) (*conn, error) {
|
func setupOutboundConn(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, keepconn func(discover.NodeID) bool) (*conn, error) {
|
||||||
secrets, err := initiatorEncHandshake(fd, prv, dial.ID, nil)
|
secrets, err := initiatorEncHandshake(fd, prv, dial.ID, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("encryption handshake failed: %v", err)
|
return nil, fmt.Errorf("encryption handshake failed: %v", err)
|
||||||
}
|
}
|
||||||
rw := newRlpxFrameRW(fd, secrets)
|
rw := newRlpxFrameRW(fd, secrets)
|
||||||
if atcap && !trusted[secrets.RemoteID] {
|
if !keepconn(secrets.RemoteID) {
|
||||||
SendItems(rw, discMsg, DiscTooManyPeers)
|
SendItems(rw, discMsg, DiscTooManyPeers)
|
||||||
return nil, errors.New("we have too many peers")
|
return nil, errors.New("we have too many peers")
|
||||||
}
|
}
|
||||||
|
|
|
@ -141,9 +141,10 @@ func TestSetupConn(t *testing.T) {
|
||||||
fd0, fd1 := net.Pipe()
|
fd0, fd1 := net.Pipe()
|
||||||
|
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
|
keepalways := func(discover.NodeID) bool { return true }
|
||||||
go func() {
|
go func() {
|
||||||
defer close(done)
|
defer close(done)
|
||||||
conn0, err := setupConn(fd0, prv0, hs0, node1, false, nil)
|
conn0, err := setupConn(fd0, prv0, hs0, node1, keepalways)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("outbound side error: %v", err)
|
t.Errorf("outbound side error: %v", err)
|
||||||
return
|
return
|
||||||
|
@ -156,7 +157,7 @@ func TestSetupConn(t *testing.T) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
conn1, err := setupConn(fd1, prv1, hs1, nil, false, nil)
|
conn1, err := setupConn(fd1, prv1, hs1, nil, keepalways)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("inbound side error: %v", err)
|
t.Fatalf("inbound side error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
12
p2p/peer.go
12
p2p/peer.go
|
@ -211,6 +211,18 @@ func (p *Peer) handle(msg Msg) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func countMatchingProtocols(protocols []Protocol, caps []Cap) int {
|
||||||
|
n := 0
|
||||||
|
for _, cap := range caps {
|
||||||
|
for _, proto := range protocols {
|
||||||
|
if proto.Name == cap.Name && proto.Version == cap.Version {
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
// matchProtocols creates structures for matching named subprotocols.
|
// matchProtocols creates structures for matching named subprotocols.
|
||||||
func matchProtocols(protocols []Protocol, caps []Cap, rw MsgReadWriter) map[string]*protoRW {
|
func matchProtocols(protocols []Protocol, caps []Cap, rw MsgReadWriter) map[string]*protoRW {
|
||||||
sort.Sort(capsByName(caps))
|
sort.Sort(capsByName(caps))
|
||||||
|
|
|
@ -126,7 +126,7 @@ type Server struct {
|
||||||
peerWG sync.WaitGroup // active peer goroutines
|
peerWG sync.WaitGroup // active peer goroutines
|
||||||
}
|
}
|
||||||
|
|
||||||
type setupFunc func(net.Conn, *ecdsa.PrivateKey, *protoHandshake, *discover.Node, bool, map[discover.NodeID]bool) (*conn, error)
|
type setupFunc func(net.Conn, *ecdsa.PrivateKey, *protoHandshake, *discover.Node, func(discover.NodeID) bool) (*conn, error)
|
||||||
type newPeerHook func(*Peer)
|
type newPeerHook func(*Peer)
|
||||||
|
|
||||||
// Peers returns all connected peers.
|
// Peers returns all connected peers.
|
||||||
|
@ -412,7 +412,7 @@ func (srv *Server) dialLoop() {
|
||||||
defer refresh.Stop()
|
defer refresh.Stop()
|
||||||
|
|
||||||
// Limit the number of concurrent dials
|
// Limit the number of concurrent dials
|
||||||
tokens := maxAcceptConns
|
tokens := maxDialingConns
|
||||||
if srv.MaxPendingPeers > 0 {
|
if srv.MaxPendingPeers > 0 {
|
||||||
tokens = srv.MaxPendingPeers
|
tokens = srv.MaxPendingPeers
|
||||||
}
|
}
|
||||||
|
@ -506,17 +506,7 @@ func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
|
||||||
// the callers of startPeer added the peer to the wait group already.
|
// the callers of startPeer added the peer to the wait group already.
|
||||||
fd.SetDeadline(time.Now().Add(handshakeTimeout))
|
fd.SetDeadline(time.Now().Add(handshakeTimeout))
|
||||||
|
|
||||||
// Check capacity, but override for static nodes
|
conn, err := srv.setupFunc(fd, srv.PrivateKey, srv.ourHandshake, dest, srv.keepconn)
|
||||||
srv.lock.RLock()
|
|
||||||
atcap := len(srv.peers) == srv.MaxPeers
|
|
||||||
if dest != nil {
|
|
||||||
if _, ok := srv.staticNodes[dest.ID]; ok {
|
|
||||||
atcap = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
srv.lock.RUnlock()
|
|
||||||
|
|
||||||
conn, err := srv.setupFunc(fd, srv.PrivateKey, srv.ourHandshake, dest, atcap, srv.trustedNodes)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fd.Close()
|
fd.Close()
|
||||||
glog.V(logger.Debug).Infof("Handshake with %v failed: %v", fd.RemoteAddr(), err)
|
glog.V(logger.Debug).Infof("Handshake with %v failed: %v", fd.RemoteAddr(), err)
|
||||||
|
@ -528,7 +518,7 @@ func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
|
||||||
conn: fd, rtimeout: frameReadTimeout, wtimeout: frameWriteTimeout,
|
conn: fd, rtimeout: frameReadTimeout, wtimeout: frameWriteTimeout,
|
||||||
}
|
}
|
||||||
p := newPeer(fd, conn, srv.Protocols)
|
p := newPeer(fd, conn, srv.Protocols)
|
||||||
if ok, reason := srv.addPeer(conn.ID, p); !ok {
|
if ok, reason := srv.addPeer(conn, p); !ok {
|
||||||
glog.V(logger.Detail).Infof("Not adding %v (%v)\n", p, reason)
|
glog.V(logger.Detail).Infof("Not adding %v (%v)\n", p, reason)
|
||||||
p.politeDisconnect(reason)
|
p.politeDisconnect(reason)
|
||||||
srv.peerWG.Done()
|
srv.peerWG.Done()
|
||||||
|
@ -539,6 +529,21 @@ func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) {
|
||||||
go srv.runPeer(p)
|
go srv.runPeer(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// preflight checks whether a connection should be kept. it runs
|
||||||
|
// after the encryption handshake, as soon as the remote identity is
|
||||||
|
// known.
|
||||||
|
func (srv *Server) keepconn(id discover.NodeID) bool {
|
||||||
|
srv.lock.RLock()
|
||||||
|
defer srv.lock.RUnlock()
|
||||||
|
if _, ok := srv.staticNodes[id]; ok {
|
||||||
|
return true // static nodes are always allowed
|
||||||
|
}
|
||||||
|
if _, ok := srv.trustedNodes[id]; ok {
|
||||||
|
return true // trusted nodes are always allowed
|
||||||
|
}
|
||||||
|
return len(srv.peers) < srv.MaxPeers
|
||||||
|
}
|
||||||
|
|
||||||
func (srv *Server) runPeer(p *Peer) {
|
func (srv *Server) runPeer(p *Peer) {
|
||||||
glog.V(logger.Debug).Infof("Added %v\n", p)
|
glog.V(logger.Debug).Infof("Added %v\n", p)
|
||||||
srvjslog.LogJson(&logger.P2PConnected{
|
srvjslog.LogJson(&logger.P2PConnected{
|
||||||
|
@ -559,13 +564,18 @@ func (srv *Server) runPeer(p *Peer) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Server) addPeer(id discover.NodeID, p *Peer) (bool, DiscReason) {
|
func (srv *Server) addPeer(conn *conn, p *Peer) (bool, DiscReason) {
|
||||||
|
// drop connections with no matching protocols.
|
||||||
|
if len(srv.Protocols) > 0 && countMatchingProtocols(srv.Protocols, conn.protoHandshake.Caps) == 0 {
|
||||||
|
return false, DiscUselessPeer
|
||||||
|
}
|
||||||
|
// add the peer if it passes the other checks.
|
||||||
srv.lock.Lock()
|
srv.lock.Lock()
|
||||||
defer srv.lock.Unlock()
|
defer srv.lock.Unlock()
|
||||||
if ok, reason := srv.checkPeer(id); !ok {
|
if ok, reason := srv.checkPeer(conn.ID); !ok {
|
||||||
return false, reason
|
return false, reason
|
||||||
}
|
}
|
||||||
srv.peers[id] = p
|
srv.peers[conn.ID] = p
|
||||||
return true, 0
|
return true, 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,8 +22,11 @@ func startTestServer(t *testing.T, pf newPeerHook) *Server {
|
||||||
ListenAddr: "127.0.0.1:0",
|
ListenAddr: "127.0.0.1:0",
|
||||||
PrivateKey: newkey(),
|
PrivateKey: newkey(),
|
||||||
newPeerHook: pf,
|
newPeerHook: pf,
|
||||||
setupFunc: func(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, atcap bool, trusted map[discover.NodeID]bool) (*conn, error) {
|
setupFunc: func(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, keepconn func(discover.NodeID) bool) (*conn, error) {
|
||||||
id := randomID()
|
id := randomID()
|
||||||
|
if !keepconn(id) {
|
||||||
|
return nil, DiscAlreadyConnected
|
||||||
|
}
|
||||||
rw := newRlpxFrameRW(fd, secrets{
|
rw := newRlpxFrameRW(fd, secrets{
|
||||||
MAC: zero16,
|
MAC: zero16,
|
||||||
AES: zero16,
|
AES: zero16,
|
||||||
|
@ -200,7 +203,7 @@ func TestServerDisconnectAtCap(t *testing.T) {
|
||||||
// Run the handshakes just like a real peer would.
|
// Run the handshakes just like a real peer would.
|
||||||
key := newkey()
|
key := newkey()
|
||||||
hs := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
|
hs := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
|
||||||
_, err = setupConn(conn, key, hs, srv.Self(), false, srv.trustedNodes)
|
_, err = setupConn(conn, key, hs, srv.Self(), keepalways)
|
||||||
if i == nconns-1 {
|
if i == nconns-1 {
|
||||||
// When handling the last connection, the server should
|
// When handling the last connection, the server should
|
||||||
// disconnect immediately instead of running the protocol
|
// disconnect immediately instead of running the protocol
|
||||||
|
@ -250,7 +253,7 @@ func TestServerStaticPeers(t *testing.T) {
|
||||||
// Run the handshakes just like a real peer would, and wait for completion
|
// Run the handshakes just like a real peer would, and wait for completion
|
||||||
key := newkey()
|
key := newkey()
|
||||||
shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
|
shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
|
||||||
if _, err = setupConn(conn, key, shake, server.Self(), false, server.trustedNodes); err != nil {
|
if _, err = setupConn(conn, key, shake, server.Self(), keepalways); err != nil {
|
||||||
t.Fatalf("conn %d: unexpected error: %v", i, err)
|
t.Fatalf("conn %d: unexpected error: %v", i, err)
|
||||||
}
|
}
|
||||||
<-started
|
<-started
|
||||||
|
@ -344,7 +347,7 @@ func TestServerTrustedPeers(t *testing.T) {
|
||||||
// Run the handshakes just like a real peer would, and wait for completion
|
// Run the handshakes just like a real peer would, and wait for completion
|
||||||
key := newkey()
|
key := newkey()
|
||||||
shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
|
shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
|
||||||
if _, err = setupConn(conn, key, shake, server.Self(), false, server.trustedNodes); err != nil {
|
if _, err = setupConn(conn, key, shake, server.Self(), keepalways); err != nil {
|
||||||
t.Fatalf("conn %d: unexpected error: %v", i, err)
|
t.Fatalf("conn %d: unexpected error: %v", i, err)
|
||||||
}
|
}
|
||||||
<-started
|
<-started
|
||||||
|
@ -357,7 +360,7 @@ func TestServerTrustedPeers(t *testing.T) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
shake := &protoHandshake{Version: baseProtocolVersion, ID: trusted.ID}
|
shake := &protoHandshake{Version: baseProtocolVersion, ID: trusted.ID}
|
||||||
if _, err = setupConn(conn, key, shake, server.Self(), false, server.trustedNodes); err != nil {
|
if _, err = setupConn(conn, key, shake, server.Self(), keepalways); err != nil {
|
||||||
t.Fatalf("trusted node: unexpected error: %v", err)
|
t.Fatalf("trusted node: unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
|
@ -472,7 +475,7 @@ func TestServerMaxPendingAccepts(t *testing.T) {
|
||||||
go func() {
|
go func() {
|
||||||
key := newkey()
|
key := newkey()
|
||||||
shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
|
shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
|
||||||
if _, err := setupConn(conns[1], key, shake, server.Self(), false, server.trustedNodes); err != nil {
|
if _, err := setupConn(conns[1], key, shake, server.Self(), keepalways); err != nil {
|
||||||
t.Fatalf("failed to run handshake: %v", err)
|
t.Fatalf("failed to run handshake: %v", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -486,7 +489,7 @@ func TestServerMaxPendingAccepts(t *testing.T) {
|
||||||
go func() {
|
go func() {
|
||||||
key := newkey()
|
key := newkey()
|
||||||
shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
|
shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)}
|
||||||
if _, err := setupConn(conns[0], key, shake, server.Self(), false, server.trustedNodes); err != nil {
|
if _, err := setupConn(conns[0], key, shake, server.Self(), keepalways); err != nil {
|
||||||
t.Fatalf("failed to run handshake: %v", err)
|
t.Fatalf("failed to run handshake: %v", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -513,3 +516,7 @@ func randomID() (id discover.NodeID) {
|
||||||
}
|
}
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func keepalways(id discover.NodeID) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue