]> Sergey Matveev's repositories - btrtrc.git/blob - peer_info.go
Tidy up the interface changes
[btrtrc.git] / peer_info.go
1 package torrent
2
3 import (
4         "net"
5
6         "github.com/anacrolix/dht/v2/krpc"
7
8         "github.com/anacrolix/torrent/peer_protocol"
9 )
10
11 // Peer connection info, handed about publicly.
12 type PeerInfo struct {
13         Id     [20]byte
14         Addr   net.Addr
15         Source PeerSource
16         // Peer is known to support encryption.
17         SupportsEncryption bool
18         peer_protocol.PexPeerFlags
19         // Whether we can ignore poor or bad behaviour from the peer.
20         Trusted bool
21 }
22
23 func (me PeerInfo) equal(other PeerInfo) bool {
24         return me.Id == other.Id &&
25                 me.Addr.String() == other.Addr.String() &&
26                 me.Source == other.Source &&
27                 me.SupportsEncryption == other.SupportsEncryption &&
28                 me.PexPeerFlags == other.PexPeerFlags &&
29                 me.Trusted == other.Trusted
30 }
31
32 // Generate PeerInfo from peer exchange
33 func (me *PeerInfo) FromPex(na krpc.NodeAddr, fs peer_protocol.PexPeerFlags) {
34         me.Addr = ipPortAddr{append([]byte(nil), na.IP...), na.Port}
35         me.Source = PeerSourcePex
36         // If they prefer encryption, they must support it.
37         if fs.Get(peer_protocol.PexPrefersEncryption) {
38                 me.SupportsEncryption = true
39         }
40         me.PexPeerFlags = fs
41 }
42
43 func (me PeerInfo) addr() IpPort {
44         return IpPort{IP: addrIpOrNil(me.Addr), Port: uint16(addrPortOrZero(me.Addr))}
45 }