bittorrent

package
v0.0.0-...-64d0194 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2022 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

All of the remaining messages in the protocol take the form of <length prefix><message ID><payload>.

The length prefix is a four byte big-endian value. The message ID is a single decimal byte. The payload is message dependent.

Message <length prefix> <message ID> <payload> Keep-alive 0000 0 none Choke 0001 0 none Unchoke 0001 1 none Interested 0001 2 none Not-interested 0001 3 none Have 0005 4 Piece index Bitfield 0001+X 5 Bitfield Request 0013 6 <index><begin><length> Piece 0009+X 7 <index><begin><block> Cancel 0013 8 <index><begin><length> port 0003 9 <listen-port>

Index

Constants

View Source
const (
	KeepAliveID     = 0
	ChokeID         = 0
	UnchokeID       = 1
	InterestedID    = 2
	NotInterestedID = 3
	HaveID          = 4
	BitfieldID      = 5
	RequestID       = 6
	PieceID         = 7
	CancelID        = 8
)

Variables

This section is empty.

Functions

func ParseHave

func ParseHave(msg *Message) (int, error)

ParseHave parses a HAVE message

func ParsePiece

func ParsePiece(index int, buf []byte, msg *Message) (int, error)

ParsePiece parses a PIECE message

Types

type Handshake

type Handshake struct {
	Pstr     string //protocol identifier which is always BitTorrent protocol
	InfoHash []byte
	PeerID   []byte //identify ourselves
}

Handshake represent the tcp handshake between us and the tracker.

func Deserialize

func Deserialize(r io.Reader) (*Handshake, error)

Deserialize into an handshake structure.

func NewHandshake

func NewHandshake(infoHash, peerID []byte) *Handshake

func (*Handshake) Serialize

func (h *Handshake) Serialize() []byte

* SerializeMsg the handshake structure into an array of bytes.

  1. The length of the protocol identifier, which is always 19 -> 1 byte
  2. The name of the protocol (BitTorrent protocol)
  3. Eight reserved bytes, all set to 0 -> 8 bytes
  4. The infohash to identify which file we want -> 20 bytes
  5. The Peer ID to identify ourselves -> 20 bytes 49 bytes

type Message

type Message struct {
	Payload []byte
	ID      int
}

Message represents a message sent from client to Peer

func DeserializeMsg

func DeserializeMsg(cnt []byte) (*Message, error)

func FormatRequest

func FormatRequest(index, begin, length int) *Message

FormatRequest creates a REQUEST message

func NewHaveMessage

func NewHaveMessage(idx int) *Message

NewHaveMessage generates a create have message

func NewInterestedMessage

func NewInterestedMessage() *Message

NewInterestedMessage generates a create interested message

func NewUnchokeMessage

func NewUnchokeMessage() *Message

NewUnchokeMessage generate a create unchoke message

func (*Message) SerializeMsg

func (m *Message) SerializeMsg() []byte

SerializeMsg a message

type PeerConnection

type PeerConnection struct {
	Con      net.Conn
	Peer     peers.Peer
	Bitfield bitfield.Bitfield
	Choked   bool
}

PeerConnection is a tcp connection between a Peer and us

func Dial

func Dial(p peers.Peer) (*PeerConnection, error)

Dial starts a tcp connection with the given Peer.

func (*PeerConnection) DoHandshake

func (c *PeerConnection) DoHandshake(ih, pid []byte) (*Handshake, error)

DoHandshake completes a two-way BitTorrent handshake

func (*PeerConnection) Interested

func (c *PeerConnection) Interested() error

Interested in obtaining pieces of a file the Peer has

func (*PeerConnection) Read

func (p *PeerConnection) Read() (*Message, error)

func (*PeerConnection) SendHave

func (c *PeerConnection) SendHave(idx int) error

SendHave sends a Have message to the peer

func (*PeerConnection) SendRequest

func (c *PeerConnection) SendRequest(index, begin, length int) error

SendRequest sends a Request message to the peer

func (*PeerConnection) Unchoke

func (c *PeerConnection) Unchoke() error

Unchoke Peer. Choking is a temporary refusal to upload. It is one of BitTorrent’s most powerful idea to deal with those who only download but never upload.

Jump to

Keyboard shortcuts

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