protocol

package
v0.0.0-...-c8d95c1 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package protocol implements low-level details of the BitTorrent protocol.

Index

Constants

View Source
const (
	ExtPex        uint8 = 1
	ExtMetadata   uint8 = 2
	ExtDontHave   uint8 = 3
	ExtUploadOnly uint8 = 4
)

The subtypes we negotiate for reception of extended messages. We send the subtypes requested by the peer, as required by the extension mechanism.

Variables

View Source
var ErrBadHandshake = errors.New("bad handshake")
View Source
var ErrParse = errors.New("parse error")
View Source
var ErrUnknownTorrent = errors.New("unknown torrent")

Functions

func GetBuffer

func GetBuffer(length int) []byte

GetBuffer gets a buffer suitable for storing a chunk of BitTorrent data.

func PutBuffer

func PutBuffer(buf []byte)

PutBuffer releases a buffer obtained by GetBuffer. The buffer must not be used again.

func Reader

func Reader(c net.Conn, init []byte, l *log.Logger, ch chan<- Message, done <-chan struct{})

Reader reads BitTorrent messages from c until it is closed. The parameter init contains data that is prepended to the data received from c. If l is not nil, then all messages read are logged.

func Write

func Write(w *bufio.Writer, m Message, l *log.Logger) error

Write writes a single BitTorrent message to w. If l is not nil, then the message is logged.

func Writer

func Writer(conn net.Conn, l *log.Logger, ch <-chan Message, done chan<- struct{}) error

Writer writes BitTorrent messages to conn until either conn or ch is closed. To closes done when it's done. If l is not nil, then all messages written are logged.

Types

type AllowedFast

type AllowedFast struct {
	Index uint32
}

type Bitfield

type Bitfield struct {
	Bitfield []byte
}

type Cancel

type Cancel struct {
	Index, Begin, Length uint32
}

type Choke

type Choke struct{}

type Error

type Error struct {
	Error error
}

type Extended0

type Extended0 struct {
	Version      string
	Port         uint16
	ReqQ         uint32
	IPv4         net.IP
	IPv6         net.IP
	MetadataSize uint32
	Messages     map[string]uint8
	UploadOnly   bool
	Encrypt      bool
}

type ExtendedDontHave

type ExtendedDontHave struct {
	Subtype uint8
	Index   uint32
}

type ExtendedMetadata

type ExtendedMetadata struct {
	Subtype   uint8
	Type      uint8
	Piece     uint32
	TotalSize uint32
	Data      []byte
}

type ExtendedPex

type ExtendedPex struct {
	Subtype uint8
	Added   []pex.Peer
	Dropped []pex.Peer
}

type ExtendedUnknown

type ExtendedUnknown struct {
	Subtype uint8
}

type ExtendedUploadOnly

type ExtendedUploadOnly struct {
	Subtype uint8
	Value   bool
}

type Flush

type Flush struct{}

type HandshakeResult

type HandshakeResult struct {
	// the torrent hash
	Hash hash.Hash
	// the peer's id
	Id hash.Hash
	// supported extensions
	Dht, Fast, Extended bool
}

HandshakeResult is the result of a BitTorrent handshake

func ClientHandshake

func ClientHandshake(c net.Conn, cryptoHandshake bool, infoHash hash.Hash, myid hash.Hash, cryptoOptions *crypto.Options) (conn net.Conn, result HandshakeResult, init []byte, err error)

ClientHandshake performs the client side of a BitTorrent handshake.

func ServerHandshake

func ServerHandshake(c net.Conn, hashes []hash.HashPair, cryptoOptions *crypto.Options) (conn net.Conn, result HandshakeResult, init []byte, err error)

ServerHandshake performs the server side of a BitTorrent handshake.

type Have

type Have struct {
	Index uint32
}

type HaveAll

type HaveAll struct{}

type HaveNone

type HaveNone struct{}

type Interested

type Interested struct{}

type KeepAlive

type KeepAlive struct{}

type Message

type Message interface{}

func Read

func Read(r *bufio.Reader, l *log.Logger) (Message, error)

Read reads a single BitTorrent message from r. If l is not nil, then the message is logged.

type NotInterested

type NotInterested struct{}

type Piece

type Piece struct {
	Index, Begin uint32
	Data         []byte
}

type Port

type Port struct {
	Port uint16
}

type RejectRequest

type RejectRequest struct {
	Index, Begin, Length uint32
}

type Request

type Request struct {
	Index, Begin, Length uint32
}

type SuggestPiece

type SuggestPiece struct {
	Index uint32
}

type Unchoke

type Unchoke struct{}

type Unknown

type Unknown struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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