peer_protocol

package
v0.0.0-...-6e19431 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 10, 2024 License: MPL-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// http://www.bittorrent.org/beps/bep_0011.html
	ExtensionNamePex ExtensionName = "ut_pex"

	ExtensionDeleteNumber ExtensionNumber = 0
)
View Source
const (
	ExtensionBitDht  = 0 // http://www.bittorrent.org/beps/bep_0005.html
	ExtensionBitFast = 2 // http://www.bittorrent.org/beps/bep_0006.html
	// A peer connection initiator can set this when sending a v1 infohash during handshake if they
	// allow the receiving end to upgrade to v2 by responding with the corresponding v2 infohash.
	// BEP 52, and BEP 4. TODO: Set by default and then clear it when it's not appropriate to send.
	ExtensionBitV2Upgrade                    = 4
	ExtensionBitAzureusExtensionNegotiation1 = 16
	ExtensionBitAzureusExtensionNegotiation2 = 17
	// LibTorrent Extension Protocol, http://www.bittorrent.org/beps/bep_0010.html
	ExtensionBitLtep = 20
	// https://wiki.theory.org/BitTorrent_Location-aware_Protocol_1
	ExtensionBitLocationAwareProtocol    = 43
	ExtensionBitAzureusMessagingProtocol = 63 // https://www.bittorrent.org/beps/bep_0004.html

)

https://www.bittorrent.org/beps/bep_0004.html https://wiki.theory.org/BitTorrentSpecification.html#Reserved_Bytes

View Source
const (
	// http://bittorrent.org/beps/bep_0009.html. Note that there's an
	// LT_metadata, but I've never implemented it.
	ExtensionNameMetadata = "ut_metadata"
)
View Source
const IntegerMax = math.MaxUint32
View Source
const (
	Protocol = "\x13BitTorrent protocol"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CompactIp

type CompactIp net.IP

Marshals to the smallest compact byte representation.

func (CompactIp) MarshalBencode

func (me CompactIp) MarshalBencode() ([]byte, error)

type Decoder

type Decoder struct {
	R *bufio.Reader
	// This must return *[]byte where the slices can fit data for piece messages. I think we store
	// *[]byte in the pool to avoid an extra allocation every time we put the slice back into the
	// pool. The chunk size should not change for the life of the decoder.
	Pool      *sync.Pool
	MaxLength Integer // TODO: Should this include the length header or not?
}

func (*Decoder) Decode

func (d *Decoder) Decode(msg *Message) (err error)

io.EOF is returned if the source terminates cleanly on a message boundary.

type ExtendedHandshakeMessage

type ExtendedHandshakeMessage struct {
	M    map[ExtensionName]ExtensionNumber `bencode:"m"`
	V    string                            `bencode:"v,omitempty"`
	Reqq int                               `bencode:"reqq,omitempty"`
	// The only mention of this I can find is in https://www.bittorrent.org/beps/bep_0011.html
	// for bit 0x01.
	Encryption bool `bencode:"e"`
	// BEP 9
	MetadataSize int `bencode:"metadata_size,omitempty"`
	// The local client port. It would be redundant for the receiving side of
	// a connection to send this.
	Port   int       `bencode:"p,omitempty"`
	YourIp CompactIp `bencode:"yourip,omitempty"`
	Ipv4   CompactIp `bencode:"ipv4,omitempty"`
	Ipv6   net.IP    `bencode:"ipv6,omitempty"`
}

http://www.bittorrent.org/beps/bep_0010.html

type ExtendedMetadataRequestMsg

type ExtendedMetadataRequestMsg struct {
	Piece     int                            `bencode:"piece"`
	TotalSize int                            `bencode:"total_size"`
	Type      ExtendedMetadataRequestMsgType `bencode:"msg_type"`
}

func (ExtendedMetadataRequestMsg) PieceSize

func (me ExtendedMetadataRequestMsg) PieceSize() int

Returns the expected piece size for this request message. This is needed to determine the offset into an extension message payload that the request metadata piece data starts.

type ExtendedMetadataRequestMsgType

type ExtendedMetadataRequestMsgType int
const (
	HandshakeExtendedID = 0

	RequestMetadataExtensionMsgType ExtendedMetadataRequestMsgType = 0
	DataMetadataExtensionMsgType    ExtendedMetadataRequestMsgType = 1
	RejectMetadataExtensionMsgType  ExtendedMetadataRequestMsgType = 2
)

type ExtensionBit

type ExtensionBit uint

type ExtensionNumber

type ExtensionNumber uint8

http://www.bittorrent.org/beps/bep_0010.html

func (*ExtensionNumber) UnmarshalBinary

func (me *ExtensionNumber) UnmarshalBinary(b []byte) error

type HandshakeResult

type HandshakeResult struct {
	PeerExtensionBits
	PeerID [20]byte
	metainfo.Hash
}

func Handshake

func Handshake(
	ctx context.Context,
	sock io.ReadWriter,
	ih *metainfo.Hash,
	peerID [20]byte,
	extensions PeerExtensionBits,
) (
	res HandshakeResult, err error,
)

ih is nil if we expect the peer to declare the InfoHash, such as when the peer initiated the connection. Returns ok if the Handshake was successful, and err if there was an unexpected condition other than the peer simply abandoning the Handshake.

type Integer

type Integer IntegerKind

func (Integer) Int

func (i Integer) Int() int

It's perfectly fine to cast these to an int. TODO: Or is it?

func (*Integer) Read

func (i *Integer) Read(r io.Reader) error

func (Integer) Uint32

func (i Integer) Uint32() uint32

func (Integer) Uint64

func (i Integer) Uint64() uint64

func (*Integer) UnmarshalBinary

func (i *Integer) UnmarshalBinary(b []byte) error

type IntegerKind

type IntegerKind = uint32

An alias for the underlying type of Integer. This is needed for fuzzing.

type Message

type Message struct {
	PiecesRoot           [32]byte
	Piece                []byte
	Bitfield             []bool
	ExtendedPayload      []byte
	Hashes               [][32]byte
	Index, Begin, Length Integer
	BaseLayer            Integer
	ProofLayers          Integer
	Port                 uint16
	Type                 MessageType
	ExtendedID           ExtensionNumber
	Keepalive            bool
}

This is a lazy union representing all the possible fields for messages. Go doesn't have ADTs, and I didn't choose to use type-assertions. Fields are ordered to minimize struct size and padding.

func MakeCancelMessage

func MakeCancelMessage(piece, offset, length Integer) Message

func MetadataExtensionRequestMsg

func MetadataExtensionRequestMsg(peerMetadataExtensionId ExtensionNumber, piece int) Message

func (Message) MarshalBinary

func (msg Message) MarshalBinary() (data []byte, err error)

func (Message) MustMarshalBinary

func (msg Message) MustMarshalBinary() []byte

func (Message) RequestSpec

func (msg Message) RequestSpec() (ret RequestSpec)

func (*Message) UnmarshalBinary

func (me *Message) UnmarshalBinary(b []byte) error

func (*Message) WriteTo

func (msg *Message) WriteTo(w MessageWriter) (err error)

type MessageType

type MessageType byte
const (
	// BEP 3
	Choke         MessageType = 0
	Unchoke       MessageType = 1
	Interested    MessageType = 2
	NotInterested MessageType = 3
	Have          MessageType = 4
	Bitfield      MessageType = 5
	Request       MessageType = 6
	Piece         MessageType = 7
	Cancel        MessageType = 8

	// BEP 5
	Port MessageType = 9

	// BEP 6 - Fast extension
	Suggest     MessageType = 0x0d // 13
	HaveAll     MessageType = 0x0e // 14
	HaveNone    MessageType = 0x0f // 15
	Reject      MessageType = 0x10 // 16
	AllowedFast MessageType = 0x11 // 17

	// BEP 10
	Extended MessageType = 20

	// BEP 52
	HashRequest MessageType = 21
	Hashes      MessageType = 22
	HashReject  MessageType = 23
)

func (MessageType) FastExtension

func (mt MessageType) FastExtension() bool

func (MessageType) String

func (i MessageType) String() string

func (*MessageType) UnmarshalBinary

func (mt *MessageType) UnmarshalBinary(b []byte) error

type MessageWriter

type MessageWriter interface {
	io.ByteWriter
	io.Writer
}

type PeerExtensionBits

type PeerExtensionBits [8]byte

func NewPeerExtensionBytes

func NewPeerExtensionBytes(bits ...ExtensionBit) (ret PeerExtensionBits)

func (PeerExtensionBits) GetBit

func (pex PeerExtensionBits) GetBit(bit ExtensionBit) bool

func (*PeerExtensionBits) SetBit

func (pex *PeerExtensionBits) SetBit(bit ExtensionBit, on bool)

func (PeerExtensionBits) String

func (pex PeerExtensionBits) String() string

func (PeerExtensionBits) SupportsDHT

func (pex PeerExtensionBits) SupportsDHT() bool

func (PeerExtensionBits) SupportsExtended

func (pex PeerExtensionBits) SupportsExtended() bool

func (PeerExtensionBits) SupportsFast

func (pex PeerExtensionBits) SupportsFast() bool

type PexMsg

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"`
}

func LoadPexMsg

func LoadPexMsg(b []byte) (ret PexMsg, err error)

Unmarshals and returns a PEX message.

func (*PexMsg) Len

func (m *PexMsg) Len() int

func (*PexMsg) Message

func (m *PexMsg) Message(pexExtendedId ExtensionNumber) Message

type PexPeerFlags

type PexPeerFlags byte
const (
	PexPrefersEncryption PexPeerFlags = 1 << iota
	PexSeedUploadOnly
	PexSupportsUtp
	PexHolepunchSupport
	PexOutgoingConn
)

func (PexPeerFlags) Get

func (me PexPeerFlags) Get(f PexPeerFlags) bool

type RequestSpec

type RequestSpec struct {
	Index, Begin, Length Integer
}

func (RequestSpec) String

func (me RequestSpec) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL