2
0
mirror of synced 2025-02-24 22:58:28 +00:00
torrent/Peer.go

46 lines
1.1 KiB
Go
Raw Normal View History

2018-04-04 17:59:28 +10:00
package torrent
import (
"net"
2019-08-10 18:46:07 +10:00
"github.com/anacrolix/dht/v2/krpc"
2019-08-21 20:58:40 +10:00
2018-07-10 11:21:24 +10:00
"github.com/anacrolix/torrent/peer_protocol"
2018-04-04 17:59:28 +10:00
)
// Peer connection info, handed about publicly.
2018-04-04 17:59:28 +10:00
type Peer struct {
Id [20]byte
Addr net.Addr
Source PeerSource
2018-04-04 17:59:28 +10:00
// Peer is known to support encryption.
SupportsEncryption bool
2018-07-10 11:21:24 +10:00
peer_protocol.PexPeerFlags
// Whether we can ignore poor or bad behaviour from the peer.
Trusted bool
2018-04-04 17:59:28 +10:00
}
2020-04-16 12:17:18 +10:00
func (me Peer) Equal(other Peer) bool {
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
}
2020-01-12 14:26:29 -05:00
// FromPex generate Peer from peer exchange
2018-07-10 11:21:24 +10:00
func (me *Peer) FromPex(na krpc.NodeAddr, fs peer_protocol.PexPeerFlags) {
me.Addr = ipPortAddr{append([]byte(nil), na.IP...), na.Port}
me.Source = PeerSourcePex
2018-04-04 17:59:28 +10:00
// If they prefer encryption, they must support it.
2018-07-10 11:21:24 +10:00
if fs.Get(peer_protocol.PexPrefersEncryption) {
2018-04-04 17:59:28 +10:00
me.SupportsEncryption = true
}
2018-07-10 11:21:24 +10:00
me.PexPeerFlags = fs
2018-04-04 17:59:28 +10:00
}
2018-11-16 10:35:30 +11:00
func (me Peer) addr() IpPort {
return IpPort{IP: addrIpOrNil(me.Addr), Port: uint16(addrPortOrZero(me.Addr))}
2018-04-04 17:59:28 +10:00
}