krpc

package
v2.0.0-...-be9fe6b Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2023 License: MPL-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// These are documented in BEP 5.
	ErrorCodeGenericError  = 201
	ErrorCodeServerError   = 202
	ErrorCodeProtocolError = 203
	ErrorCodeMethodUnknown = 204
	// BEP 44
	ErrorCodeMessageValueFieldTooBig       = 205
	ErrorCodeInvalidSignature              = 206
	ErrorCodeSaltFieldTooBig               = 207
	ErrorCodeCasHashMismatched             = 301
	ErrorCodeSequenceNumberLessThanCurrent = 302
)
View Source
const (
	YQuery    = "q"
	YResponse = "r"
	YError    = "e"
)

Variables

View Source
var ErrorMethodUnknown = Error{
	Code: ErrorCodeMethodUnknown,
	Msg:  "Method Unknown",
}

Functions

This section is empty.

Types

type Bep44Return

type Bep44Return struct {
	V   bencode.Bytes `bencode:"v,omitempty"`
	K   [32]byte      `bencode:"k,omitempty"`
	Sig [64]byte      `bencode:"sig,omitempty"`
	Seq *int64        `bencode:"seq,omitempty"`
}

type Bep46Payload

type Bep46Payload struct {
	Ih metainfo.Hash `bencode:"ih"`
}

type Bep51Return

type Bep51Return struct {
	Interval *int64 `bencode:"interval,omitempty"`
	Num      *int64 `bencode:"num,omitempty"`
	// Nodes supporting this extension should always include the samples field in the response, even
	// when it is zero-length. This lets indexing nodes to distinguish nodes supporting this
	// extension from those that respond to unknown query types which contain a target field [2].
	Samples *CompactInfohashes `bencode:"samples,omitempty"`
}

BEP 51 (DHT Infohash Indexing)

type CompactIPv4NodeAddrs

type CompactIPv4NodeAddrs []NodeAddr

func (CompactIPv4NodeAddrs) ElemSize

func (CompactIPv4NodeAddrs) ElemSize() int

func (CompactIPv4NodeAddrs) Index

func (me CompactIPv4NodeAddrs) Index(x NodeAddr) int

func (CompactIPv4NodeAddrs) MarshalBencode

func (me CompactIPv4NodeAddrs) MarshalBencode() ([]byte, error)

func (CompactIPv4NodeAddrs) MarshalBinary

func (me CompactIPv4NodeAddrs) MarshalBinary() ([]byte, error)

func (CompactIPv4NodeAddrs) NodeAddrs

func (me CompactIPv4NodeAddrs) NodeAddrs() []NodeAddr

func (*CompactIPv4NodeAddrs) UnmarshalBencode

func (me *CompactIPv4NodeAddrs) UnmarshalBencode(b []byte) error

func (*CompactIPv4NodeAddrs) UnmarshalBinary

func (me *CompactIPv4NodeAddrs) UnmarshalBinary(b []byte) error

type CompactIPv4NodeInfo

type CompactIPv4NodeInfo []NodeInfo

func (CompactIPv4NodeInfo) ElemSize

func (CompactIPv4NodeInfo) ElemSize() int

func (CompactIPv4NodeInfo) MarshalBencode

func (me CompactIPv4NodeInfo) MarshalBencode() ([]byte, error)

func (CompactIPv4NodeInfo) MarshalBinary

func (me CompactIPv4NodeInfo) MarshalBinary() ([]byte, error)

func (*CompactIPv4NodeInfo) UnmarshalBencode

func (me *CompactIPv4NodeInfo) UnmarshalBencode(b []byte) error

func (*CompactIPv4NodeInfo) UnmarshalBinary

func (me *CompactIPv4NodeInfo) UnmarshalBinary(b []byte) error

type CompactIPv6NodeAddrs

type CompactIPv6NodeAddrs []NodeAddr

func (CompactIPv6NodeAddrs) ElemSize

func (CompactIPv6NodeAddrs) ElemSize() int

func (CompactIPv6NodeAddrs) Index

func (me CompactIPv6NodeAddrs) Index(x NodeAddr) int

func (CompactIPv6NodeAddrs) MarshalBencode

func (me CompactIPv6NodeAddrs) MarshalBencode() ([]byte, error)

func (CompactIPv6NodeAddrs) MarshalBinary

func (me CompactIPv6NodeAddrs) MarshalBinary() ([]byte, error)

func (CompactIPv6NodeAddrs) NodeAddrs

func (me CompactIPv6NodeAddrs) NodeAddrs() []NodeAddr

func (*CompactIPv6NodeAddrs) UnmarshalBencode

func (me *CompactIPv6NodeAddrs) UnmarshalBencode(b []byte) error

func (*CompactIPv6NodeAddrs) UnmarshalBinary

func (me *CompactIPv6NodeAddrs) UnmarshalBinary(b []byte) error

type CompactIPv6NodeInfo

type CompactIPv6NodeInfo []NodeInfo

func (CompactIPv6NodeInfo) ElemSize

func (CompactIPv6NodeInfo) ElemSize() int

func (CompactIPv6NodeInfo) MarshalBencode

func (me CompactIPv6NodeInfo) MarshalBencode() ([]byte, error)

func (CompactIPv6NodeInfo) MarshalBinary

func (me CompactIPv6NodeInfo) MarshalBinary() ([]byte, error)

func (*CompactIPv6NodeInfo) UnmarshalBencode

func (me *CompactIPv6NodeInfo) UnmarshalBencode(b []byte) error

func (*CompactIPv6NodeInfo) UnmarshalBinary

func (me *CompactIPv6NodeInfo) UnmarshalBinary(b []byte) error

type CompactInfohashes

type CompactInfohashes [][20]byte

func (CompactInfohashes) ElemSize

func (CompactInfohashes) ElemSize() int

func (CompactInfohashes) MarshalBencode

func (me CompactInfohashes) MarshalBencode() ([]byte, error)

func (CompactInfohashes) MarshalBinary

func (me CompactInfohashes) MarshalBinary() ([]byte, error)

func (*CompactInfohashes) UnmarshalBencode

func (me *CompactInfohashes) UnmarshalBencode(b []byte) error

func (*CompactInfohashes) UnmarshalBinary

func (me *CompactInfohashes) UnmarshalBinary(b []byte) error

type Error

type Error struct {
	Code int
	Msg  string
}

Represented as a string or list in bencode.

func (Error) Error

func (e Error) Error() string

func (Error) MarshalBencode

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

func (*Error) UnmarshalBencode

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

type ID

type ID [20]byte

func IdFromString

func IdFromString(s string) (id ID)

func RandomNodeID

func RandomNodeID() (id ID)

func (ID) Format

func (h ID) Format(f fmt.State, c rune)

func (ID) Int160

func (id ID) Int160() int160.T

func (ID) IsZero

func (id ID) IsZero() bool

func (ID) MarshalBencode

func (id ID) MarshalBencode() ([]byte, error)

func (ID) String

func (id ID) String() string

func (*ID) UnmarshalBencode

func (id *ID) UnmarshalBencode(b []byte) error

func (*ID) UnmarshalText

func (id *ID) UnmarshalText(b []byte) (err error)

type Infohash

type Infohash [20]byte

type Msg

type Msg struct {
	Q        string   `bencode:"q,omitempty"` // Query method (one of 4: "ping", "find_node", "get_peers", "announce_peer")
	A        *MsgArgs `bencode:"a,omitempty"` // named arguments sent with a query
	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
	R        *Return  `bencode:"r,omitempty"` // RESPONSE type only
	E        *Error   `bencode:"e,omitempty"` // ERROR type only
	IP       NodeAddr `bencode:"ip,omitempty"`
	ReadOnly bool     `bencode:"ro,omitempty"` // BEP 43. Sender does not respond to queries.
	// https://www.libtorrent.org/dht_extensions.html
	ClientId string `bencode:"v,omitempty"`
}

Msg represents messages that nodes in the network send to each other as specified by the protocol. They are also referred to as the KRPC messages. There are three types of messages: QUERY, RESPONSE, ERROR The message is a dictionary 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. 3 message types: QUERY, RESPONSE, ERROR

func (Msg) Error

func (m Msg) Error() *Error

This does not return an error, but (*Error)(nil) is still a non-nil error. You have been warned! This language is evil.

func (Msg) SenderID

func (m Msg) SenderID() *ID

The node ID of the source of this Msg. Returns nil if it isn't present. TODO: Can we verify Msgs more aggressively so this is guaranteed to return a valid ID for a checked Msg?

type MsgArgs

type MsgArgs struct {
	ID       ID `bencode:"id"`                  // ID of the querying Node
	InfoHash ID `bencode:"info_hash,omitempty"` // InfoHash of the torrent
	Target   ID `bencode:"target,omitempty"`    // ID of the node sought
	// Token received from an earlier get_peers query. Also used in a BEP 44 put.
	Token       string `bencode:"token,omitempty"`
	Port        *int   `bencode:"port,omitempty"`         // Sender's torrent port
	ImpliedPort bool   `bencode:"implied_port,omitempty"` // Use senders apparent DHT port
	Want        []Want `bencode:"want,omitempty"`         // Contains strings like "n4" and "n6" from BEP 32.
	NoSeed      int    `bencode:"noseed,omitempty"`       // BEP 33
	Scrape      int    `bencode:"scrape,omitempty"`       // BEP 33

	// I don't know if we should use bencode.Bytes for this. If we unmarshalled bytes that didn't
	// marshal back the same, our hashes will not match. But this might also serve to prevent abuse.
	V    interface{} `bencode:"v,omitempty"`
	Seq  *int64      `bencode:"seq,omitempty"`
	Cas  int64       `bencode:"cas,omitempty"`
	K    [32]byte    `bencode:"k,omitempty"`
	Salt []byte      `bencode:"salt,omitempty"`
	Sig  [64]byte    `bencode:"sig,omitempty"`
}

type NodeAddr

type NodeAddr struct {
	IP   net.IP
	Port int
}

This will be deprecated in favour of NodeAddrPort.

func (NodeAddr) Equal

func (me NodeAddr) Equal(x NodeAddr) bool

func (*NodeAddr) FromAddrPort

func (me *NodeAddr) FromAddrPort(f netip.AddrPort)

func (*NodeAddr) FromUDPAddr

func (me *NodeAddr) FromUDPAddr(ua *net.UDPAddr)

func (NodeAddr) MarshalBencode

func (me NodeAddr) MarshalBencode() ([]byte, error)

func (NodeAddr) MarshalBinary

func (me NodeAddr) MarshalBinary() ([]byte, error)

func (NodeAddr) String

func (me NodeAddr) String() string

A zero Port is taken to mean no port provided, per BEP 7.

func (NodeAddr) ToNodeAddrPort

func (me NodeAddr) ToNodeAddrPort() NodeAddrPort

func (NodeAddr) UDP

func (me NodeAddr) UDP() *net.UDPAddr

func (*NodeAddr) UnmarshalBencode

func (me *NodeAddr) UnmarshalBencode(b []byte) (err error)

func (*NodeAddr) UnmarshalBinary

func (me *NodeAddr) UnmarshalBinary(b []byte) error

type NodeAddrPort

type NodeAddrPort struct {
	netip.AddrPort
}

This is a comparable replacement for NodeAddr.

func (NodeAddrPort) Compare

func (l NodeAddrPort) Compare(r NodeAddrPort) int

func (NodeAddrPort) IP

func (me NodeAddrPort) IP() net.IP

func (NodeAddrPort) ToNodeAddr

func (me NodeAddrPort) ToNodeAddr() NodeAddr

func (NodeAddrPort) UDP

func (me NodeAddrPort) UDP() *net.UDPAddr

type NodeInfo

type NodeInfo struct {
	ID   ID
	Addr NodeAddr
}

func RandomNodeInfo

func RandomNodeInfo(ipLen int) (ni NodeInfo)

func (NodeInfo) MarshalBinary

func (ni NodeInfo) MarshalBinary() ([]byte, error)

func (NodeInfo) String

func (me NodeInfo) String() string

func (NodeInfo) ToNodeInfoAddrPort

func (me NodeInfo) ToNodeInfoAddrPort() NodeInfoAddrPort

func (*NodeInfo) UnmarshalBinary

func (ni *NodeInfo) UnmarshalBinary(b []byte) error

type NodeInfoAddrPort

type NodeInfoAddrPort struct {
	ID   ID
	Addr NodeAddrPort
}

This is a comparable replacement for NodeInfo.

type Return

type Return struct {
	// All returns are supposed to contain an ID, but what if they don't?
	ID ID `bencode:"id"` // ID of the queried (and responding) node

	// K closest nodes to the requested target. Included in responses to queries that imply
	// traversal, for example get_peers, find_nodes, get, sample_infohashes.
	Nodes  CompactIPv4NodeInfo `bencode:"nodes,omitempty"`
	Nodes6 CompactIPv6NodeInfo `bencode:"nodes6,omitempty"`

	Token  *string    `bencode:"token,omitempty"`  // Token for future announce_peer or put (BEP 44)
	Values []NodeAddr `bencode:"values,omitempty"` // Torrent peers

	// BEP 33 (scrapes)
	BFsd *ScrapeBloomFilter `bencode:"BFsd,omitempty"`
	BFpe *ScrapeBloomFilter `bencode:"BFpe,omitempty"`

	Bep51Return

	// BEP 44
	Bep44Return
}

func (Return) ForAllNodes

func (r Return) ForAllNodes(f func(NodeInfo))

type ScrapeBloomFilter

type ScrapeBloomFilter [256]byte

func (*ScrapeBloomFilter) AddIp

func (me *ScrapeBloomFilter) AddIp(ip net.IP)

Note that if you intend for an IP to be in the IPv4 space, you might want to trim it to 4 bytes with IP.To4.

func (*ScrapeBloomFilter) EstimateCount

func (me *ScrapeBloomFilter) EstimateCount() float64

type Want

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

Jump to

Keyboard shortcuts

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