forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer_info.go
44 lines (38 loc) · 1.17 KB
/
peer_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package torrent
import (
"github.com/anacrolix/dht/v2/krpc"
"github.com/anacrolix/torrent/peer_protocol"
)
// Peer connection info, handed about publicly.
type PeerInfo struct {
Id [20]byte
Addr PeerRemoteAddr
Source PeerSource
// Peer is known to support encryption.
SupportsEncryption bool
peer_protocol.PexPeerFlags
// Whether we can ignore poor or bad behaviour from the peer.
Trusted bool
}
func (me PeerInfo) equal(other PeerInfo) 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
}
// 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
// If they prefer encryption, they must support it.
if fs.Get(peer_protocol.PexPrefersEncryption) {
me.SupportsEncryption = true
}
me.PexPeerFlags = fs
}
func (me PeerInfo) addr() IpPort {
ipPort, _ := tryIpPortFromNetAddr(me.Addr)
return IpPort{ipPort.IP, uint16(ipPort.Port)}
}