peer_protocol

package
v1.26.0-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2021 License: MPL-2.0 Imports: 16 Imported by: 234

Documentation

Index

Constants

View Source
const (
	HandshakeExtendedID = 0

	RequestMetadataExtensionMsgType = 0
	DataMetadataExtensionMsgType    = 1
	RejectMetadataExtensionMsgType  = 2
)
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
	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. TODO: Is that before or after the message?

type ExtendedHandshakeMessage

type ExtendedHandshakeMessage struct {
	M          map[ExtensionName]ExtensionNumber `bencode:"m"`
	V          string                            `bencode:"v,omitempty"`
	Reqq       int                               `bencode:"reqq,omitempty"`
	Encryption bool                              `bencode:"e,omitempty"`
	// 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 ExtensionBit

type ExtensionBit uint

type ExtensionName

type ExtensionName string

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

const (
	// http://www.bittorrent.org/beps/bep_0011.html
	ExtensionNamePex ExtensionName = "ut_pex"
	// http://bittorrent.org/beps/bep_0009.html. Note that there's an
	// LT_metadata, but I've never implemented it.
	ExtensionNameMetadata = "ut_metadata"
)

type HandshakeResult

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

func Handshake

func Handshake(
	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 uint32

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

type Message

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

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.

func MakeCancelMessage

func MakeCancelMessage(piece, offset, length Integer) 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)

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
)

func (MessageType) FastExtension

func (mt MessageType) FastExtension() bool

func (MessageType) String

func (i MessageType) String() string

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 (me 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 added in v1.16.0

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

func (*PexMsg) Len added in v1.16.0

func (m *PexMsg) Len() int

func (*PexMsg) Message added in v1.16.0

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

Jump to

Keyboard shortcuts

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