peer_info.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package torrent
  2. import (
  3. "github.com/anacrolix/dht/v2/krpc"
  4. "github.com/anacrolix/torrent/peer_protocol"
  5. )
  6. // Peer connection info, handed about publicly.
  7. type PeerInfo struct {
  8. Id [20]byte
  9. Addr PeerRemoteAddr
  10. Source PeerSource
  11. // Peer is known to support encryption.
  12. SupportsEncryption bool
  13. peer_protocol.PexPeerFlags
  14. // Whether we can ignore poor or bad behaviour from the peer.
  15. Trusted bool
  16. }
  17. func (me PeerInfo) equal(other PeerInfo) bool {
  18. return me.Id == other.Id &&
  19. me.Addr.String() == other.Addr.String() &&
  20. me.Source == other.Source &&
  21. me.SupportsEncryption == other.SupportsEncryption &&
  22. me.PexPeerFlags == other.PexPeerFlags &&
  23. me.Trusted == other.Trusted
  24. }
  25. // Generate PeerInfo from peer exchange
  26. func (me *PeerInfo) FromPex(na krpc.NodeAddr, fs peer_protocol.PexPeerFlags) {
  27. me.Addr = ipPortAddr{append([]byte(nil), na.IP...), 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. func (me PeerInfo) addr() IpPort {
  36. ipPort, _ := tryIpPortFromNetAddr(me.Addr)
  37. return IpPort{ipPort.IP, uint16(ipPort.Port)}
  38. }