Self connect detect

This commit is contained in:
obscuren 2014-01-31 00:56:32 +01:00
parent 7ccf51fd30
commit 7f100e9610
2 changed files with 43 additions and 13 deletions

@ -72,7 +72,7 @@ func (s *Ethereum) AddPeer(conn net.Conn) {
peer := NewPeer(conn, s, true) peer := NewPeer(conn, s, true)
if peer != nil { if peer != nil {
if s.peers.Len() > -1 { if s.peers.Len() > 25 {
log.Println("SEED") log.Println("SEED")
peer.Start(true) peer.Start(true)
} else { } else {

54
peer.go

@ -178,6 +178,8 @@ out:
case ethwire.MsgHandshakeTy: case ethwire.MsgHandshakeTy:
// Version message // Version message
p.handleHandshake(msg) p.handleHandshake(msg)
p.QueueMessage(ethwire.NewMessage(ethwire.MsgGetPeersTy, ""))
case ethwire.MsgDiscTy: case ethwire.MsgDiscTy:
p.Stop() p.Stop()
case ethwire.MsgPingTy: case ethwire.MsgPingTy:
@ -216,12 +218,12 @@ out:
// Received a list of peers (probably because MsgGetPeersTy was send) // Received a list of peers (probably because MsgGetPeersTy was send)
// Only act on message if we actually requested for a peers list // Only act on message if we actually requested for a peers list
if p.requestedPeerList { if p.requestedPeerList {
data := ethutil.Conv(msg.Data) data := msg.Data
// Create new list of possible peers for the ethereum to process // Create new list of possible peers for the ethereum to process
peers := make([]string, data.Length()) peers := make([]string, data.Length())
// Parse each possible peer // Parse each possible peer
for i := 0; i < data.Length(); i++ { 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 // Connect to the list of peers
@ -278,15 +280,28 @@ out:
func (p *Peer) Start(seed bool) { func (p *Peer) Start(seed bool) {
p.seed = seed p.seed = seed
if !p.inbound { peerHost, _, _ := net.SplitHostPort(p.conn.LocalAddr().String())
err := p.pushHandshake() servHost, _, _ := net.SplitHostPort(p.conn.RemoteAddr().String())
if err != nil { log.Println(peerHost, servHost)
log.Printf("Peer can't send outbound version ack", err) 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 // Run the outbound handler in a new goroutine
go p.HandleOutbound() go p.HandleOutbound()
// Run the inbound handler in a new goroutine // Run the inbound handler in a new goroutine
@ -320,11 +335,10 @@ func (p *Peer) pushHandshake() error {
// Pushes the list of outbound peers to the client when requested // Pushes the list of outbound peers to the client when requested
func (p *Peer) pushPeers() { func (p *Peer) pushPeers() {
outPeers := make([]interface{}, len(p.ethereum.OutboundPeers())) outPeers := make([]interface{}, len(p.ethereum.OutboundPeers()))
// Serialise each peer // Serialise each peer
for i, peer := range p.ethereum.OutboundPeers() { 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 // 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" istr = "inbound"
} else { } else {
msg := ethwire.NewMessage(ethwire.MsgGetChainTy, []interface{}{p.ethereum.BlockManager.BlockChain().CurrentBlock.Hash(), uint64(100)}) //msg := ethwire.NewMessage(ethwire.MsgGetChainTy, []interface{}{p.ethereum.BlockManager.BlockChain().CurrentBlock.Hash(), uint64(100)})
p.QueueMessage(msg) //p.QueueMessage(msg)
istr = "outbound" 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()) 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 { func (p *Peer) RlpEncode() []byte {
host, prt, err := net.SplitHostPort(p.conn.RemoteAddr().String()) host, prt, err := net.SplitHostPort(p.conn.RemoteAddr().String())
if err != nil { if err != nil {