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