diff --git a/client.go b/client.go index 956c448e..9808a2aa 100644 --- a/client.go +++ b/client.go @@ -239,7 +239,7 @@ func (cl *Client) acceptConnections() { } log.Printf("accepted connection from %s", conn.RemoteAddr()) go func() { - if err := cl.runConnection(conn, nil); err != nil { + if err := cl.runConnection(conn, nil, peerSourceIncoming); err != nil { log.Print(err) } }() @@ -290,8 +290,8 @@ func (me *Client) initiateConn(peer Peer, torrent *torrent) { log.Printf("error connecting to peer: %s %#v", err, err) return } - log.Printf("connected to %s", conn.RemoteAddr()) - err = me.runConnection(conn, torrent) + // log.Printf("connected to %s", conn.RemoteAddr()) + err = me.runConnection(conn, torrent, peer.Source) if err != nil { log.Print(err) } @@ -314,9 +314,9 @@ func (cl *Client) incomingPeerPort() int { return i } -func (me *Client) runConnection(sock net.Conn, torrent *torrent) (err error) { +func (me *Client) runConnection(sock net.Conn, torrent *torrent, discovery peerSource) (err error) { conn := &connection{ - Incoming: torrent == nil, + Discovery: discovery, Socket: sock, Choked: true, PeerChoked: true, @@ -704,8 +704,9 @@ func (me *Client) connectionLoop(t *torrent, c *connection) error { err := me.AddPeers(t.InfoHash, func() (ret []Peer) { for _, cp := range pexMsg.Added { p := Peer{ - IP: make([]byte, 4), - Port: int(cp.Port), + IP: make([]byte, 4), + Port: int(cp.Port), + Source: peerSourcePEX, } if n := copy(p.IP, cp.IP[:]); n != 4 { panic(n) @@ -931,8 +932,9 @@ func (cl *Client) announceTorrentDHT(t *torrent) { err = cl.AddPeers(t.InfoHash, func() (ret []Peer) { for _, cp := range cps { ret = append(ret, Peer{ - IP: cp.IP[:], - Port: int(cp.Port), + IP: cp.IP[:], + Port: int(cp.Port), + Source: peerSourceDHT, }) log.Printf("peer from dht: %s", &net.UDPAddr{ IP: cp.IP[:], diff --git a/connection.go b/connection.go index 10e84d02..2832e0cf 100644 --- a/connection.go +++ b/connection.go @@ -13,14 +13,22 @@ import ( "bitbucket.org/anacrolix/go.torrent/peer_protocol" ) +type peerSource byte + +const ( + peerSourceIncoming = 'I' + peerSourceDHT = 'H' + peerSourcePEX = 'X' +) + // Maintains the state of a connection with a peer. type connection struct { - Socket net.Conn - Incoming bool - closed bool - mu sync.Mutex // Only for closing. - post chan peer_protocol.Message - write chan []byte + Socket net.Conn + Discovery peerSource + closed bool + mu sync.Mutex // Only for closing. + post chan peer_protocol.Message + write chan []byte // Stuff controlled by the local peer. Interested bool @@ -86,8 +94,8 @@ func (cn *connection) WriteStatus(w io.Writer) { if !cn.Choked && !cn.PeerInterested { c('?') } - if cn.Incoming { - c('I') + if cn.Discovery != 0 { + c(byte(cn.Discovery)) } fmt.Fprintln(w) } diff --git a/torrent.go b/torrent.go index e9361c86..8a212457 100644 --- a/torrent.go +++ b/torrent.go @@ -347,9 +347,10 @@ func (t *torrent) pendAllChunkSpecs(index pp.Integer) { } type Peer struct { - Id [20]byte - IP net.IP - Port int + Id [20]byte + IP net.IP + Port int + Source peerSource } func (t *torrent) PieceLength(piece pp.Integer) (len_ pp.Integer) {