From 7f100e96101a057cba7b2d5c58c12d2f7accf381 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 31 Jan 2014 00:56:32 +0100 Subject: Self connect detect --- ethereum.go | 2 +- peer.go | 54 ++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/ethereum.go b/ethereum.go index 72d023f5c..98316cf04 100644 --- a/ethereum.go +++ b/ethereum.go @@ -72,7 +72,7 @@ func (s *Ethereum) AddPeer(conn net.Conn) { peer := NewPeer(conn, s, true) if peer != nil { - if s.peers.Len() > -1 { + if s.peers.Len() > 25 { log.Println("SEED") peer.Start(true) } else { diff --git a/peer.go b/peer.go index 627e57b05..bd75a0039 100644 --- a/peer.go +++ b/peer.go @@ -178,6 +178,8 @@ out: case ethwire.MsgHandshakeTy: // Version message p.handleHandshake(msg) + + p.QueueMessage(ethwire.NewMessage(ethwire.MsgGetPeersTy, "")) case ethwire.MsgDiscTy: p.Stop() case ethwire.MsgPingTy: @@ -216,12 +218,12 @@ out: // Received a list of peers (probably because MsgGetPeersTy was send) // Only act on message if we actually requested for a peers list if p.requestedPeerList { - data := ethutil.Conv(msg.Data) + data := msg.Data // Create new list of possible peers for the ethereum to process peers := make([]string, data.Length()) // Parse each possible peer for i := 0; i < data.Length(); i++ { - peers[i] = data.Get(i).AsString() + strconv.Itoa(int(data.Get(i).AsUint())) + peers[i] = data.Get(i).Get(0).AsString() + ":" + strconv.Itoa(int(data.Get(i).Get(1).AsUint())) } // Connect to the list of peers @@ -278,14 +280,27 @@ out: func (p *Peer) Start(seed bool) { p.seed = seed - if !p.inbound { - err := p.pushHandshake() - if err != nil { - log.Printf("Peer can't send outbound version ack", err) + peerHost, _, _ := net.SplitHostPort(p.conn.LocalAddr().String()) + servHost, _, _ := net.SplitHostPort(p.conn.RemoteAddr().String()) + log.Println(peerHost, servHost) + if peerHost == servHost { + log.Println("Connected to self") - p.Stop() - } + p.Stop() + + return + } + + //if !p.inbound { + err := p.pushHandshake() + if err != nil { + log.Printf("Peer can't send outbound version ack", err) + + p.Stop() + + return } + //} // Run the outbound handler in a new goroutine go p.HandleOutbound() @@ -320,11 +335,10 @@ func (p *Peer) pushHandshake() error { // Pushes the list of outbound peers to the client when requested func (p *Peer) pushPeers() { - outPeers := make([]interface{}, len(p.ethereum.OutboundPeers())) // Serialise each peer for i, peer := range p.ethereum.OutboundPeers() { - outPeers[i] = peer.RlpEncode() + outPeers[i] = peer.RlpData() } // Send message to the peer with the known list of connected clients @@ -351,8 +365,8 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) { */ istr = "inbound" } else { - msg := ethwire.NewMessage(ethwire.MsgGetChainTy, []interface{}{p.ethereum.BlockManager.BlockChain().CurrentBlock.Hash(), uint64(100)}) - p.QueueMessage(msg) + //msg := ethwire.NewMessage(ethwire.MsgGetChainTy, []interface{}{p.ethereum.BlockManager.BlockChain().CurrentBlock.Hash(), uint64(100)}) + //p.QueueMessage(msg) istr = "outbound" } @@ -360,6 +374,22 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) { log.Printf("peer connect (%s) %v %s\n", istr, p.conn.RemoteAddr(), c.Get(2).AsString()) } +func (p *Peer) RlpData() []interface{} { + host, prt, err := net.SplitHostPort(p.conn.RemoteAddr().String()) + if err != nil { + return nil + } + + port, err := strconv.Atoi(prt) + if err != nil { + return nil + } + + //port := ethutil.NumberToBytes(uint16(i), 16) + + return []interface{}{host, port} +} + func (p *Peer) RlpEncode() []byte { host, prt, err := net.SplitHostPort(p.conn.RemoteAddr().String()) if err != nil { -- cgit v1.2.3