messages

package
v0.0.0-...-0dc6626 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Choke messageKinds = iota
	Unchoke
	Interested
	Uninterested
	Have
	Bitfield
	Request
	Piece
	Cancel
	Port
	Keepalive
	Handshake
	// Not part of the BT protocol; used here to mean an incomplete "piece" message split into blocks
	Fragment
)

Variables

View Source
var (
	ErrUnsupportedProtocol = errors.New("Peer protocol unsupported")
	ErrBadInfoHash         = errors.New("Peer handshake contained invalid info hash")
	ErrMessageTimedOut     = errors.New("Message timeout expired")
)

Functions

func CreateBitfield

func CreateBitfield(bitfield []byte) []byte
<len=0001+X><id=5><bitfield>

"The bitfield message may only be sent immediately after the handshaking sequence is completed, and before any other messages are sent. It is optional, and need not be sent if a client has no pieces.

The bitfield message is variable length, where X is the length of the bitfield. The payload is a bitfield representing the pieces that have been successfully downloaded. The high bit in the first byte corresponds to piece index 0. Bits that are cleared indicated a missing piece, and set bits indicate a valid and available piece. Spare bits at the end are set to zero."

func CreateChoke

func CreateChoke() []byte

func CreateHandshake

func CreateHandshake(infoHash []byte, peerId []byte) []byte

<pstrlen><pstr><reserved><info_hash><peer_id>

func CreateHave

func CreateHave(pieceNum uint32) []byte

have: <len=0005><id=4><piece index> "The have message is fixed length. The payload is the zero-based index of a piece that has just been successfully downloaded and verified via the hash."

func CreateInterested

func CreateInterested() []byte

<len=0001><id=2>

func CreatePiece

func CreatePiece(pieceNum, offset uint32, block []byte) []byte

<len=0009+X><id=7><index><begin><block>

func CreateRequest

func CreateRequest(pieceNum, offset, length uint32) []byte

<len=0013><id=6><index><begin><length>

func CreateUnchoke

func CreateUnchoke() []byte

Types

type PeerMessage

type PeerMessage struct {
	Kind        messageKinds
	TotalSize   int
	PeerId      []byte
	PieceNum    int
	Bitfield    []byte
	BlockOffset int
	BlockSize   int
	BlockData   []byte
}

TODO: Move away from single type for all messages

func ParseMultiMessage

func ParseMultiMessage(buf, infoHash []byte) ([]PeerMessage, error)

parseMultiMessage will parse a chunk of bytes for multiple sequential messages until empty

Jump to

Keyboard shortcuts

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