]> Sergey Matveev's repositories - btrtrc.git/blob - Peer.go
02b2c31aec76ef98af52bc8aa4cc6aa095d2b4d2
[btrtrc.git] / Peer.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 Peer struct {
13         Id     [20]byte
14         IP     net.IP
15         Port   int
16         Source peerSource
17         // Peer is known to support encryption.
18         SupportsEncryption bool
19         peer_protocol.PexPeerFlags
20         // Whether we can ignore poor or bad behaviour from the peer.
21         Trusted bool
22 }
23
24 // FromPex generate Peer from peer exchange
25 func (me *Peer) FromPex(na krpc.NodeAddr, fs peer_protocol.PexPeerFlags) {
26         me.IP = append([]byte(nil), na.IP...)
27         me.Port = na.Port
28         me.Source = peerSourcePex
29         // If they prefer encryption, they must support it.
30         if fs.Get(peer_protocol.PexPrefersEncryption) {
31                 me.SupportsEncryption = true
32         }
33         me.PexPeerFlags = fs
34 }
35
36 func (me Peer) addr() IpPort {
37         return IpPort{IP: me.IP, Port: uint16(me.Port)}
38 }