2018-07-10 09:21:24 +08:00
|
|
|
package peer_protocol
|
|
|
|
|
2019-12-11 19:45:04 +08:00
|
|
|
import (
|
|
|
|
"github.com/anacrolix/dht/v2/krpc"
|
|
|
|
"github.com/anacrolix/torrent/bencode"
|
|
|
|
)
|
2018-07-10 09:21:24 +08:00
|
|
|
|
|
|
|
type PexMsg struct {
|
|
|
|
Added krpc.CompactIPv4NodeAddrs `bencode:"added"`
|
|
|
|
AddedFlags []PexPeerFlags `bencode:"added.f"`
|
|
|
|
Added6 krpc.CompactIPv6NodeAddrs `bencode:"added6"`
|
|
|
|
Added6Flags []PexPeerFlags `bencode:"added6.f"`
|
|
|
|
Dropped krpc.CompactIPv4NodeAddrs `bencode:"dropped"`
|
|
|
|
Dropped6 krpc.CompactIPv6NodeAddrs `bencode:"dropped6"`
|
|
|
|
}
|
|
|
|
|
2020-04-01 04:14:43 +08:00
|
|
|
func (m *PexMsg) Len() int {
|
2020-10-23 05:58:55 +08:00
|
|
|
return len(m.Added) + len(m.Added6) + len(m.Dropped) + len(m.Dropped6)
|
2020-04-01 04:14:43 +08:00
|
|
|
}
|
|
|
|
|
2020-04-09 00:03:29 +08:00
|
|
|
func (m *PexMsg) Message(pexExtendedId ExtensionNumber) Message {
|
|
|
|
payload := bencode.MustMarshal(m)
|
2019-12-11 19:45:04 +08:00
|
|
|
return Message{
|
|
|
|
Type: Extended,
|
|
|
|
ExtendedID: pexExtendedId,
|
|
|
|
ExtendedPayload: payload,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-23 05:58:55 +08:00
|
|
|
func LoadPexMsg(b []byte) (ret PexMsg, err error) {
|
|
|
|
err = bencode.Unmarshal(b, &ret)
|
|
|
|
return
|
2020-04-09 00:03:29 +08:00
|
|
|
}
|
|
|
|
|
2018-07-10 09:21:24 +08:00
|
|
|
type PexPeerFlags byte
|
|
|
|
|
|
|
|
func (me PexPeerFlags) Get(f PexPeerFlags) bool {
|
|
|
|
return me&f == f
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2019-12-11 19:45:04 +08:00
|
|
|
PexPrefersEncryption PexPeerFlags = 1 << iota
|
|
|
|
PexSeedUploadOnly
|
|
|
|
PexSupportsUtp
|
|
|
|
PexHolepunchSupport
|
|
|
|
PexOutgoingConn
|
2018-07-10 09:21:24 +08:00
|
|
|
)
|