From 736e632215d49dd7bc61126f78dda4bad12768ea Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Fri, 27 Feb 2015 03:06:55 +0000 Subject: p2p: use RLPx frames for messaging --- p2p/server.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'p2p/server.go') diff --git a/p2p/server.go b/p2p/server.go index 3ea2538d1..e53e832aa 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -358,14 +358,15 @@ func (srv *Server) findPeers() { func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) { // TODO: handle/store session token - fd.SetDeadline(time.Now().Add(handshakeTimeout)) + // TODO: reenable deadlines + // fd.SetDeadline(time.Now().Add(handshakeTimeout)) conn, err := srv.setupFunc(fd, srv.PrivateKey, srv.ourHandshake, dest) if err != nil { fd.Close() srvlog.Debugf("Handshake with %v failed: %v", fd.RemoteAddr(), err) return } - p := newPeer(conn, srv.Protocols) + p := newPeer(fd, conn, srv.Protocols) if ok, reason := srv.addPeer(conn.ID, p); !ok { srvlog.DebugDetailf("Not adding %v (%v)\n", p, reason) p.politeDisconnect(reason) @@ -375,7 +376,7 @@ func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) { srvlog.Debugf("Added %v\n", p) srvjslog.LogJson(&logger.P2PConnected{ RemoteId: fmt.Sprintf("%x", conn.ID[:]), - RemoteAddress: conn.RemoteAddr().String(), + RemoteAddress: fd.RemoteAddr().String(), RemoteVersionString: conn.Name, NumConnections: srv.PeerCount(), }) -- cgit v1.2.3 From 7964f30dcbdde00b2960ef6e98320e0a0f9300e2 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 4 Mar 2015 12:03:43 +0100 Subject: p2p: msg.Payload contains list data With RLPx frames, the message code is contained in the frame and is no longer part of the encoded data. EncodeMsg, Msg.Decode have been updated to match. Code that decodes RLP directly from Msg.Payload will need to change. --- p2p/server.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'p2p/server.go') diff --git a/p2p/server.go b/p2p/server.go index e53e832aa..67d5514b4 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -10,6 +10,7 @@ import ( "sync" "time" + "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/nat" @@ -135,7 +136,7 @@ func (srv *Server) SuggestPeer(n *discover.Node) { func (srv *Server) Broadcast(protocol string, code uint64, data ...interface{}) { var payload []byte if data != nil { - payload = encodePayload(data...) + payload = ethutil.Encode(data) } srv.lock.RLock() defer srv.lock.RUnlock() -- cgit v1.2.3 From 22659a7feaf4e939a33762c3f83b43d8bec757db Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 4 Mar 2015 16:27:37 +0100 Subject: p2p: restore read/write timeouts They got lost in the transition to rlpxFrameRW. --- p2p/server.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'p2p/server.go') diff --git a/p2p/server.go b/p2p/server.go index 67d5514b4..8f99bc33d 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -17,9 +17,17 @@ import ( ) const ( - handshakeTimeout = 5 * time.Second defaultDialTimeout = 10 * time.Second refreshPeersInterval = 30 * time.Second + + // total timeout for encryption handshake and protocol + // handshake in both directions. + handshakeTimeout = 5 * time.Second + // maximum time allowed for reading a complete message. + // this is effectively the amount of time a connection can be idle. + frameReadTimeout = 1 * time.Minute + // maximum amount of time allowed for writing a complete message. + frameWriteTimeout = 5 * time.Second ) var srvlog = logger.NewLogger("P2P Server") @@ -359,14 +367,18 @@ func (srv *Server) findPeers() { func (srv *Server) startPeer(fd net.Conn, dest *discover.Node) { // TODO: handle/store session token - // TODO: reenable deadlines - // fd.SetDeadline(time.Now().Add(handshakeTimeout)) + fd.SetDeadline(time.Now().Add(handshakeTimeout)) conn, err := srv.setupFunc(fd, srv.PrivateKey, srv.ourHandshake, dest) if err != nil { fd.Close() srvlog.Debugf("Handshake with %v failed: %v", fd.RemoteAddr(), err) return } + + conn.MsgReadWriter = &netWrapper{ + wrapped: conn.MsgReadWriter, + conn: fd, rtimeout: frameReadTimeout, wtimeout: frameWriteTimeout, + } p := newPeer(fd, conn, srv.Protocols) if ok, reason := srv.addPeer(conn.ID, p); !ok { srvlog.DebugDetailf("Not adding %v (%v)\n", p, reason) -- cgit v1.2.3 From 215c763d53fc8e06e8c9807875eacaccf3ef45fa Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Wed, 4 Mar 2015 16:54:36 +0100 Subject: eth, p2p: delete p2p.Blacklist It is unused and untested right now. We can bring it back later if required. --- p2p/server.go | 59 ----------------------------------------------------------- 1 file changed, 59 deletions(-) (limited to 'p2p/server.go') diff --git a/p2p/server.go b/p2p/server.go index 8f99bc33d..34000cb4c 100644 --- a/p2p/server.go +++ b/p2p/server.go @@ -66,10 +66,6 @@ type Server struct { // each peer. Protocols []Protocol - // If Blacklist is set to a non-nil value, the given Blacklist - // is used to verify peer connections. - Blacklist Blacklist - // If ListenAddr is set to a non-nil address, the server // will listen for incoming connections. // @@ -183,9 +179,6 @@ func (srv *Server) Start() (err error) { if srv.setupFunc == nil { srv.setupFunc = setupConn } - if srv.Blacklist == nil { - srv.Blacklist = NewBlacklist() - } // node table ntab, err := discover.ListenUDP(srv.PrivateKey, srv.ListenAddr, srv.NAT) @@ -417,8 +410,6 @@ func (srv *Server) addPeer(id discover.NodeID, p *Peer) (bool, DiscReason) { return false, DiscTooManyPeers case srv.peers[id] != nil: return false, DiscAlreadyConnected - case srv.Blacklist.Exists(id[:]): - return false, DiscUselessPeer case id == srv.ntab.Self(): return false, DiscSelf } @@ -432,53 +423,3 @@ func (srv *Server) removePeer(p *Peer) { srv.lock.Unlock() srv.peerWG.Done() } - -type Blacklist interface { - Get([]byte) (bool, error) - Put([]byte) error - Delete([]byte) error - Exists(pubkey []byte) (ok bool) -} - -type BlacklistMap struct { - blacklist map[string]bool - lock sync.RWMutex -} - -func NewBlacklist() *BlacklistMap { - return &BlacklistMap{ - blacklist: make(map[string]bool), - } -} - -func (self *BlacklistMap) Get(pubkey []byte) (bool, error) { - self.lock.RLock() - defer self.lock.RUnlock() - v, ok := self.blacklist[string(pubkey)] - var err error - if !ok { - err = fmt.Errorf("not found") - } - return v, err -} - -func (self *BlacklistMap) Exists(pubkey []byte) (ok bool) { - self.lock.RLock() - defer self.lock.RUnlock() - _, ok = self.blacklist[string(pubkey)] - return -} - -func (self *BlacklistMap) Put(pubkey []byte) error { - self.lock.Lock() - defer self.lock.Unlock() - self.blacklist[string(pubkey)] = true - return nil -} - -func (self *BlacklistMap) Delete(pubkey []byte) error { - self.lock.Lock() - defer self.lock.Unlock() - delete(self.blacklist, string(pubkey)) - return nil -} -- cgit v1.2.3