torrent/peer_protocol/pex.go

49 lines
1.1 KiB
Go
Raw Normal View History

2018-07-10 01:21:24 +00:00
package peer_protocol
import (
"github.com/anacrolix/dht/v2/krpc"
2022-11-15 12:22:10 +00:00
"github.com/anacrolix/torrent/bencode"
)
2018-07-10 01:21:24 +00: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-03-31 20:14:43 +00:00
func (m *PexMsg) Len() int {
2020-10-22 21:58:55 +00:00
return len(m.Added) + len(m.Added6) + len(m.Dropped) + len(m.Dropped6)
2020-03-31 20:14:43 +00:00
}
2020-04-08 16:03:29 +00:00
func (m *PexMsg) Message(pexExtendedId ExtensionNumber) Message {
payload := bencode.MustMarshal(m)
return Message{
Type: Extended,
ExtendedID: pexExtendedId,
ExtendedPayload: payload,
}
}
2020-10-22 21:58:55 +00:00
func LoadPexMsg(b []byte) (ret PexMsg, err error) {
err = bencode.Unmarshal(b, &ret)
return
2020-04-08 16:03:29 +00:00
}
2018-07-10 01:21:24 +00:00
type PexPeerFlags byte
func (me PexPeerFlags) Get(f PexPeerFlags) bool {
return me&f == f
}
const (
PexPrefersEncryption PexPeerFlags = 1 << iota
PexSeedUploadOnly
PexSupportsUtp
PexHolepunchSupport
PexOutgoingConn
2018-07-10 01:21:24 +00:00
)