btprotocol

package
v0.0.0-...-09dbcf8 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: MPL-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

Extension bits for bittorrent protocol

View Source
const (
	PexPrefersEncryption = 0x01
	PexSeedUploadOnly    = 0x02
	PexSupportsUtp       = 0x04
	PexHolepunchSupport  = 0x08
	PexOutgoingConn      = 0x10
)

Constants for PEX messages.

View Source
const (
	HandshakeExtendedID = iota
	MetadataExtendedID
	PEXExtendedID
	ExtendedIdMax
)
View Source
const (
	RequestMetadataExtensionMsgType = 0
	DataMetadataExtensionMsgType    = 1
	RejectMetadataExtensionMsgType  = 2
)
View Source
const (
	Protocol = "\x13BitTorrent protocol"
)

Variables

This section is empty.

Functions

func Write

func Write(w io.Writer, set ...encoding.BinaryMarshaler) (n int, err error)

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 NewDecoder

func NewDecoder(i io.Reader, p *sync.Pool) *Decoder

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 EncryptionHandshake

type EncryptionHandshake struct {
	Keys               mse.SecretKey
	mse.CryptoSelector // available crypto algorithms
}

EncryptionHandshake encrypt a net.Conn

func (EncryptionHandshake) Incoming

func (t EncryptionHandshake) Incoming(rw io.ReadWriter) (updated io.ReadWriter, buffed io.ReadWriter, err error)

Incoming establishes an encrypted connection. if it is unable to establish an encrypted connection. it returns a buffed read/writer that contains the previously read data from the input. when the encryption handshake is successful, buffed === updated.

func (EncryptionHandshake) Outgoing

Outgoing initiates an outgoing encrypted connection.

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 ExtensionBits

type ExtensionBits [8]byte

ExtensionBits used by the Handshake to determine capabilities of a peer.

func NewExtensionBits

func NewExtensionBits(bits ...uint) (ret ExtensionBits)

NewExtensionBits initiatize extension bits

func (ExtensionBits) GetBit

func (pex ExtensionBits) GetBit(bit uint) bool

GetBit ...

func (*ExtensionBits) SetBit

func (pex *ExtensionBits) SetBit(bit uint)

SetBit ...

func (ExtensionBits) String

func (pex ExtensionBits) String() string

func (ExtensionBits) Supported

func (pex ExtensionBits) Supported(rpex ExtensionBits, bits ...uint) bool

func (ExtensionBits) SupportsDHT

func (pex ExtensionBits) SupportsDHT() bool

SupportsDHT ...

func (ExtensionBits) SupportsExtended

func (pex ExtensionBits) SupportsExtended() bool

SupportsExtended ...

func (ExtensionBits) SupportsFast

func (pex ExtensionBits) SupportsFast() bool

SupportsFast ...

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 Handshake

type Handshake struct {
	Bits   ExtensionBits
	PeerID [20]byte
}

Handshake ...

func (Handshake) Incoming

func (t Handshake) Incoming(sock io.ReadWriter) (pbits ExtensionBits, pinfo HandshakeInfoMessage, err error)

Incoming handshake, used to accept a connection from a peer.

func (Handshake) Outgoing

func (t Handshake) Outgoing(sock io.ReadWriter, hash [20]byte) (resbits ExtensionBits, res HandshakeInfoMessage, err error)

Outgoing handshake, used to establish a connection to a peer.

type HandshakeInfoMessage

type HandshakeInfoMessage struct {
	PeerID [20]byte
	Hash   [20]byte
}

HandshakeInfoMessage sent after the HandshakeMessage containing the peers ID and the info hash.

func (*HandshakeInfoMessage) ReadFrom

func (t *HandshakeInfoMessage) ReadFrom(src io.Reader) (n int64, err error)

ReadFrom reads a Handshake from a reader

func (HandshakeInfoMessage) WriteTo

func (t HandshakeInfoMessage) WriteTo(dst io.Writer) (n int64, err error)

WriteTo write the header to the provided writer.

type HandshakeMessage

type HandshakeMessage struct {
	Extensions [8]byte
}

HandshakeMessage writes the handshake into a destination.

func (*HandshakeMessage) ReadFrom

func (t *HandshakeMessage) ReadFrom(src io.Reader) (n int64, err error)

ReadFrom reads a Handshake from a reader

func (HandshakeMessage) WriteTo

func (t HandshakeMessage) WriteTo(dst io.Writer) (n int64, err error)

WriteTo write the header to the provided writer.

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) Int64

func (i Integer) Int64() int64

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 NewAllowedFast

func NewAllowedFast(piece uint32) Message

func NewBitField

func NewBitField(n uint64, b *roaring.Bitmap) Message

func NewChoked

func NewChoked() Message

func NewDHTPort

func NewDHTPort(p uint16) Message

func NewExtended

func NewExtended(id ExtensionNumber, encoded []byte) Message

func NewExtendedHandshake

func NewExtendedHandshake(encoded []byte) Message

func NewHaveAll

func NewHaveAll() Message

func NewHaveNone

func NewHaveNone() Message

func NewHavePiece

func NewHavePiece(p uint64) Message

func NewInterested

func NewInterested(b bool) Message

func NewKeepAlive

func NewKeepAlive() Message

func NewPiece

func NewPiece(index Integer, begin Integer, bin []byte) Message

func NewPort

func NewPort(p uint16) Message

func NewUnchoked

func NewUnchoked() 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
	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 = 0x14 // 20
)

func (MessageType) FastExtension

func (mt MessageType) FastExtension() bool

func (MessageType) String

func (i MessageType) String() string

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

PexMsg http://bittorrent.org/beps/bep_0011.html

type PexPeerFlags

type PexPeerFlags byte

PexPeerFlags flags describing peers supported functionality.

func (PexPeerFlags) Get

func (t PexPeerFlags) Get(f PexPeerFlags) bool

Get checks if the provided flags are set.

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