krpc

package
Version: v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package krpc supplies the KRPC message used by DHT.

Index

Constants

View Source
const (
	// BEP 5
	ErrorCodeGeneric       = 201
	ErrorCodeServer        = 202
	ErrorCodeProtocol      = 203
	ErrorCodeMethodUnknown = 204

	// BEP 44
	ErrorCodeMessageValueFieldTooBig       = 205
	ErrorCodeInvalidSignature              = 206
	ErrorCodeSaltFieldTooBig               = 207
	ErrorCodeCasHashMismatched             = 301
	ErrorCodeSequenceNumberLessThanCurrent = 302
)

Predefine some error code.

Variables

This section is empty.

Functions

This section is empty.

Types

type CompactAddresses

type CompactAddresses []metainfo.Address

CompactAddresses represents a group of the compact addresses.

func (CompactAddresses) MarshalBinary

func (cas CompactAddresses) MarshalBinary() ([]byte, error)

MarshalBinary implements the interface binary.BinaryMarshaler.

func (*CompactAddresses) UnmarshalBinary

func (cas *CompactAddresses) UnmarshalBinary(b []byte) (err error)

UnmarshalBinary implements the interface binary.BinaryUnmarshaler.

type CompactIPv4Node

type CompactIPv4Node []Node

CompactIPv4Node is a set of IPv4 Nodes.

func (CompactIPv4Node) MarshalBencode

func (cn CompactIPv4Node) MarshalBencode() (b []byte, err error)

MarshalBencode implements the interface bencode.Marshaler.

func (CompactIPv4Node) MarshalBinary

func (cn CompactIPv4Node) MarshalBinary() ([]byte, error)

MarshalBinary implements the interface binary.BinaryMarshaler.

func (*CompactIPv4Node) UnmarshalBencode

func (cn *CompactIPv4Node) UnmarshalBencode(b []byte) (err error)

UnmarshalBencode implements the interface bencode.Unmarshaler.

func (*CompactIPv4Node) UnmarshalBinary

func (cn *CompactIPv4Node) UnmarshalBinary(b []byte) (err error)

UnmarshalBinary implements the interface binary.BinaryUnmarshaler.

type CompactIPv6Node

type CompactIPv6Node []Node

CompactIPv6Node is a set of IPv6 Nodes.

func (CompactIPv6Node) MarshalBencode

func (cn CompactIPv6Node) MarshalBencode() (b []byte, err error)

MarshalBencode implements the interface bencode.Marshaler.

func (CompactIPv6Node) MarshalBinary

func (cn CompactIPv6Node) MarshalBinary() ([]byte, error)

MarshalBinary implements the interface binary.BinaryMarshaler.

func (*CompactIPv6Node) UnmarshalBencode

func (cn *CompactIPv6Node) UnmarshalBencode(b []byte) (err error)

UnmarshalBencode implements the interface bencode.Unmarshaler.

func (*CompactIPv6Node) UnmarshalBinary

func (cn *CompactIPv6Node) UnmarshalBinary(b []byte) (err error)

UnmarshalBinary implements the interface binary.BinaryUnmarshaler.

type Error

type Error struct {
	Code   int
	Reason string
}

Error represents a response error.

func NewError

func NewError(code int, reason string) Error

NewError returns a new Error.

func (Error) Error

func (e Error) Error() string

func (Error) MarshalBencode

func (e Error) MarshalBencode() (ret []byte, err error)

MarshalBencode implements the interface bencode.Marshaler.

func (*Error) UnmarshalBencode

func (e *Error) UnmarshalBencode(b []byte) (err error)

UnmarshalBencode implements the interface bencode.Unmarshaler.

type Message

type Message struct {
	T string         `bencode:"t"`           // required: transaction ID
	Y string         `bencode:"y"`           // required: type of the message: q for QUERY, r for RESPONSE, e for ERROR
	Q string         `bencode:"q,omitempty"` // Query method (one of 4: "ping", "find_node", "get_peers", "announce_peer")
	A QueryArg       `bencode:"a,omitempty"` // named arguments sent with a query
	R ResponseResult `bencode:"r,omitempty"` // RESPONSE type only
	E Error          `bencode:"e,omitempty"` // ERROR type only

	RO bool `bencode:"ro,omitempty"` // BEP 43: ReadOnly
}

Message represents messages that nodes in the network send to each other as specified by the KRPC protocol, which are also referred to as the KRPC messages.

There are three types of messages: QUERY, RESPONSE, ERROR The message is a dictonary that is then "bencoded" (serialization & compression format adopted by the BitTorrent) and sent via the UDP connection to peers.

A KRPC message is a single dictionary with two keys common to every message and additional keys depending on the type of message. Every message has a key "t" with a string value representing a transaction ID. This transaction ID is generated by the querying node and is echoed in the response, so responses may be correlated with multiple queries to the same node. The transaction ID should be encoded as a short string of binary numbers, typically 2 characters are enough as they cover 2^16 outstanding queries. The other key contained in every KRPC message is "y" with a single character value describing the type of message. The value of the "y" key is one of "q" for query, "r" for response, or "e" for error.

func NewErrorMsg

func NewErrorMsg(tid string, code int, reason string) Message

NewErrorMsg return a ERROR message.

func NewQueryMsg

func NewQueryMsg(tid, method string, arg QueryArg) Message

NewQueryMsg return a QUERY message.

func NewResponseMsg

func NewResponseMsg(tid string, data ResponseResult) Message

NewResponseMsg return a RESPONSE message.

func (Message) ID

func (m Message) ID() metainfo.Hash

ID returns the QID or RID.

Notice: it will panic if the the message is not a query or response.

func (Message) IsError

func (m Message) IsError() bool

IsError reports whether the message is an ERROR.

func (Message) IsQuery

func (m Message) IsQuery() bool

IsQuery reports whether the message is an QUERY.

func (Message) IsResponse

func (m Message) IsResponse() bool

IsResponse reports whether the message is an RESPONSE.

func (Message) QID

func (m Message) QID() metainfo.Hash

QID returns the value named "id" in "a", that's, the query arguments.

Return the ZERO value instead if no "id".

func (Message) RID

func (m Message) RID() metainfo.Hash

RID returns the value named "id" in "r".

Return the ZERO value instead if no "id".

type Node

type Node struct {
	ID   metainfo.Hash
	Addr metainfo.Address
}

Node represents a node information.

func NewNode

func NewNode(id metainfo.Hash, ip net.IP, port int) Node

NewNode returns a new Node.

func NewNodeByUDPAddr

func NewNodeByUDPAddr(id metainfo.Hash, addr *net.UDPAddr) (n Node)

NewNodeByUDPAddr returns a new Node with the id and the UDP address.

func (Node) Equal

func (n Node) Equal(o Node) bool

Equal reports whether n is equal to o.

func (Node) MarshalBinary

func (n Node) MarshalBinary() (data []byte, err error)

MarshalBinary implements the interface binary.BinaryMarshaler.

func (Node) String

func (n Node) String() string

func (*Node) UnmarshalBinary

func (n *Node) UnmarshalBinary(b []byte) error

UnmarshalBinary implements the interface binary.BinaryUnmarshaler.

func (Node) WriteBinary

func (n Node) WriteBinary(w io.Writer) (m int, err error)

WriteBinary is the same as MarshalBinary, but writes the result into w instead of returning.

type QueryArg

type QueryArg struct {
	// ID is used to identify a querying node.
	ID metainfo.Hash `bencode:"id"` // BEP 5

	// Target is used to identify the node sought by the queryer.
	//
	// find_node
	Target metainfo.Hash `bencode:"target,omitempty"` // BEP 5

	// InfoHash is the infohash of the torrent file.
	//
	// get_peers, announce_peer
	InfoHash metainfo.Hash `bencode:"info_hash,omitempty"` // BEP 5

	// Port is the port on where sender is listening to allow others
	// to download the torrent.
	//
	// announce_peer
	Port uint16 `bencode:"port,omitempty"` // BEP 5

	// Token is the received one from an earlier "get_peers" query.
	//
	// announce_peer
	Token string `bencode:"token,omitempty"` // BEP 5

	// ImpliedPort is used by the senders to apparent DHT port
	// to improve NAT support.
	//
	// announce_peer
	ImpliedPort bool `bencode:"implied_port,omitempty"` // BEP 5

	// Wants is only used to represent to expect "nodes" for "n4"
	// or "nodes6" for "n6".
	//
	// Notice: It only governs the presence of the "nodes" and "nodes6"
	// parameters, not the interpretation of "values".
	//
	// find_node, get_peers
	Wants []Want `bencode:"want,omitempty"` // BEP 32
}

QueryArg represents the arguments used by the QUERY message.

func (QueryArg) ContainsWant

func (a QueryArg) ContainsWant(w Want) bool

ContainsWant reports whether the request contains the given Want.

func (QueryArg) GetPort

func (a QueryArg) GetPort(port int) uint16

GetPort returns the real port of the peer.

type ResponseResult

type ResponseResult struct {
	// ID is used to indentify the queried node, that's, the response node.
	ID metainfo.Hash `bencode:"id"` // BEP 5

	// Nodes is a string containing the compact node information for the list
	// of the ipv4 target node, or the K(8) closest good nodes in routing table
	// of the requested ipv4 target.
	//
	// find_node
	Nodes CompactIPv4Node `bencode:"nodes,omitempty"` // BEP 5

	// Nodes6 is a string containing the compact node information for the list
	// of the ipv6 target node, or the K(8) closest good nodes in routing table
	// of the requested ipv6 target.
	//
	// find_node
	Nodes6 CompactIPv6Node `bencode:"nodes6,omitempty"` // BEP 32

	// Token is used for future "announce_peer".
	//
	// get_peers
	Token string `bencode:"token,omitempty"` // BEP 5

	// Values is a list of the torrent peers.
	//
	// get_peers
	Values CompactAddresses `bencode:"values,omitempty"` // BEP 5
}

ResponseResult represents the results used by the RESPONSE message.

type Want

type Want string

Want represents the type of nodes that the request wants.

BEP 32

const (
	WantNodes  Want = "n4"
	WantNodes6 Want = "n6"
)

Predefine some wants.

BEP 32

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL