torrent/peer_info.go

45 lines
1.2 KiB
Go
Raw Normal View History

2018-04-04 07:59:28 +00:00
package torrent
import (
2019-08-10 08:46:07 +00:00
"github.com/anacrolix/dht/v2/krpc"
2019-08-21 10:58:40 +00:00
2018-07-10 01:21:24 +00:00
"github.com/anacrolix/torrent/peer_protocol"
2018-04-04 07:59:28 +00:00
)
// Peer connection info, handed about publicly.
type PeerInfo struct {
2018-04-04 07:59:28 +00:00
Id [20]byte
Addr PeerRemoteAddr
Source PeerSource
2018-04-04 07:59:28 +00:00
// Peer is known to support encryption.
SupportsEncryption bool
2018-07-10 01:21:24 +00:00
peer_protocol.PexPeerFlags
// Whether we can ignore poor or bad behaviour from the peer.
Trusted bool
2018-04-04 07:59:28 +00:00
}
2020-06-02 07:41:59 +00:00
func (me PeerInfo) equal(other PeerInfo) bool {
2020-04-16 02:17:18 +00:00
return me.Id == other.Id &&
me.Addr.String() == other.Addr.String() &&
me.Source == other.Source &&
me.SupportsEncryption == other.SupportsEncryption &&
me.PexPeerFlags == other.PexPeerFlags &&
me.Trusted == other.Trusted
}
// Generate PeerInfo from peer exchange
func (me *PeerInfo) FromPex(na krpc.NodeAddr, fs peer_protocol.PexPeerFlags) {
me.Addr = ipPortAddr{append([]byte(nil), na.IP...), na.Port}
me.Source = PeerSourcePex
2018-04-04 07:59:28 +00:00
// If they prefer encryption, they must support it.
2018-07-10 01:21:24 +00:00
if fs.Get(peer_protocol.PexPrefersEncryption) {
2018-04-04 07:59:28 +00:00
me.SupportsEncryption = true
}
2018-07-10 01:21:24 +00:00
me.PexPeerFlags = fs
2018-04-04 07:59:28 +00:00
}
func (me PeerInfo) addr() IpPort {
ipPort, _ := tryIpPortFromNetAddr(me.Addr)
return IpPort{ipPort.IP, uint16(ipPort.Port)}
2018-04-04 07:59:28 +00:00
}