Documentation
¶
Index ¶
- Constants
- Variables
- func PeerCompactNodeInfo(addr *net.UDPAddr, id ID) []byte
- type Bucket
- type BucketSet
- type DHT
- type FindNodeResponse
- type ID
- func (id ID) BucketIndex(root ID, limit int) int
- func (id ID) Bytes() []byte
- func (id ID) Distance(other ID) (result ID)
- func (id ID) Equals(other ID) bool
- func (id ID) GreaterThan(other ID) bool
- func (id ID) Hex() string
- func (id ID) Int() *big.Int
- func (id ID) LesserThan(other ID) bool
- func (id ID) String() string
- type IDArray
- type IncomingQueryPacket
- type IncomingReplyPacket
- type KRPC
- type Peer
- func NewPeer(dht *DHT, addr *net.UDPAddr, id ID) *Peer
- func NewPeerFromAddr(dht *DHT, addr *net.UDPAddr, id ID) (*Peer, error)
- func NewPeerFromCompactAddressPort(dht *DHT, info []byte) (*Peer, error)
- func NewPeerFromCompactNodeInfo(dht *DHT, info []byte) (*Peer, error)
- func NewPeerFromIP(dht *DHT, ip string, port int, id ID) (*Peer, error)
- func (peer *Peer) Addr() *net.UDPAddr
- func (peer *Peer) BucketIndex(limit int) int
- func (peer *Peer) CompactNodeInfo() []byte
- func (peer *Peer) DHT() *DHT
- func (peer *Peer) DistanceFrom(other *Peer) ID
- func (peer *Peer) DistanceFromRoot() ID
- func (peer *Peer) Equals(other *Peer) bool
- func (peer *Peer) FindNode(key ID, closestPeers chan *FindNodeResponse)
- func (peer *Peer) Hex() string
- func (peer *Peer) ID() ID
- func (peer *Peer) IsAlive() (bool, bool)
- func (peer *Peer) Ping()
- func (peer *Peer) String() string
- type PeerArray
- type PeerState
- type QueryPacket
- type ReplyPacket
- type Request
- type Response
- type TreeMap
Constants ¶
const ( // Alpha represents the redundancy factor used in the DHT. Alpha int = 3 // K represents the maximum capacity of a Bucket K int = 20 // KeySize for a typical node ID (bits) KeySize int = 160 // Freshness represents the minutes during which a node is considered fresh. Freshness time.Duration = 10 * time.Minute // Timeout delay for requests. 80% of peers should reply within 20%. Timeout time.Duration = 20 * time.Second )
const (
CompactNodeInfoLength = IDLength + 6
)
const ( // IDLength in bytes IDLength int = (KeySize / 8) )
Variables ¶
var ErrBucketMustSplit = errors.New("bucket is full and must be split")
ErrBucketMustSplit is the error returned by a bucket when it's full and needs a split.
Functions ¶
Types ¶
type Bucket ¶
type Bucket struct {
// contains filtered or unexported fields
}
A Bucket is a collection of nodes grouped by their distance from the local peer's ID. The index int here is a reference to the precision-bit at which the nodes it contains are considered as "close" to the local peer.
func NewBucket ¶
NewBucket returns an instance of the Bucket struct and ties it to the parent bucket set. The index int refers to the Kademlia kBucket index that will assigned to the newly created instance.
type BucketSet ¶
type BucketSet struct {
// contains filtered or unexported fields
}
A BucketSet is the basis of the Kademlia routing table. It represents a binary tree of kBuckets structured to cover the entire range of the KeySize space.
func NewBucketSet ¶
NewBucketSet returns a new instance of the BucketSet struct, and ties it to a DHT instance. The local Peer will automatically be inserted.
func (*BucketSet) ClosestPeersFrom ¶
ClosestPeersFrom returns the Alpha peers which are the closest to a given key
func (*BucketSet) Contains ¶
Contains returns a value indicating whether the bucket set contains a peer with a given HEX value.
func (*BucketSet) Insert ¶
Insert attempts to insert a Peer instance in the right kBucket. If the Bucket is full and requests splitting, the method will proceed and and make another attempt.
func (*BucketSet) InsertRange ¶
InsertRange is a handy method to insert more than one peer a time.
type DHT ¶
type DHT struct {
// contains filtered or unexported fields
}
DHT represents a Kadmelia-inspired Distributed Hash Table.
type FindNodeResponse ¶
Structure of a FindNode RPC-reply.
type ID ¶
ID represents a node ID.
func (ID) BucketIndex ¶
BucketIndex returns the index of the kBucket where the ID is located or should be inserted. It's determined using the formula index = Log2(distance) where d is the distance
func (ID) Equals ¶
Equals compares two IDs and returns a value indicating whether they are equal or not.
func (ID) GreaterThan ¶
GreaterThan compares two IDs and returns a value indicating whether an ID is greater than another
func (ID) LesserThan ¶
LesserThan compares two IDs and returns a value indicating whether an ID is lesser than another.
type IDArray ¶
type IDArray []ID
IDArray is a simple type aliasing to refer to an array of IDs.
type IncomingQueryPacket ¶
type IncomingQueryPacket struct {
QueryPacket // Anonymous field
Source *net.UDPAddr
}
type IncomingReplyPacket ¶
type IncomingReplyPacket struct {
ReplyPacket
Source *net.UDPAddr
}
type KRPC ¶
type KRPC struct {
// contains filtered or unexported fields
}
KRPC server listening on UDP
func (*KRPC) Start ¶
func (krpc *KRPC) Start(incomingQueries chan *IncomingQueryPacket) (err error)
Start listening on the network.
type Peer ¶
type Peer struct {
// contains filtered or unexported fields
}
A peer is a contact on the network.
func NewPeerFromAddr ¶
NewPeerFromAddr creates a new peer from a UDP address
func NewPeerFromCompactAddressPort ¶
NewPeerFromCompactAddressPort creates a new peer from a KRPC compact address format. Contact information for peers is encoded as a 6-byte string. Also known as "Compact IP-address/port info" the 4-byte IP address is in network byte order with the 2 byte port in network byte order concatenated onto the end.
func NewPeerFromCompactNodeInfo ¶
Contact information for nodes is encoded as a 26-byte string. Also known as "Compact node info" the 20-byte Node ID in network byte order has the compact IP-address/port info concatenated to the end.
func NewPeerFromIP ¶
NewPeerFromIP creates a new peer from an ip and port
func (*Peer) BucketIndex ¶
BucketIndex returns the index of the kBucket where the ID is located or should be inserted. It's determined using the formula i = Log2(d) where d is the distance and i is the index we're looking for.
func (*Peer) CompactNodeInfo ¶
Returns a CompactNodeInfo representation
func (*Peer) DistanceFrom ¶
The distance represented as an ID between this Peer and another.
func (*Peer) DistanceFromRoot ¶
func (*Peer) FindNode ¶
func (peer *Peer) FindNode(key ID, closestPeers chan *FindNodeResponse)
Sends a find_node RPC request to the current Peer
func (*Peer) Hex ¶
Returns a unique HEX value for this peer. Most Kademlia implementation will only hash the ID. We chose to include the host and the port to allow multiple nodes to live on the machine.
type PeerArray ¶
type PeerArray []*Peer
Simple type aliasing to represent an array of Peers
func (PeerArray) CompactInfo ¶
type PeerState ¶
type PeerState struct {
// contains filtered or unexported fields
}
Represents a peer item stored in the Tree Map.
type QueryPacket ¶
type QueryPacket struct {
Args map[string]interface{} `bencode:"a"`
QueryMethod string `bencode:"q"`
PacketType string `bencode:"y"`
Transaction string `bencode:"t"`
}
The content of a KRPC request UDP packet.
func NewFindNodeQuery ¶
func NewFindNodeQuery(queryingNodeID, targetNodeID ID) *QueryPacket
Find node is used to find the contact information for the node with targetNodeID
func NewGetPeersQuery ¶
func NewGetPeersQuery(queryingNodeID, infoHash ID) *QueryPacket
Get peers associated with a torrent infohash
func NewQuery ¶
func NewQuery(method string, args map[string]interface{}) (query *QueryPacket)
Returns a message instance fit for a KRPC query.
type ReplyPacket ¶
type ReplyPacket struct {
NamedReturns map[string]interface{} `bencode:"r"`
PacketType string `bencode:"y"`
Transaction string `bencode:"t"`
}
The content of an RPC reponse UDP packet
func NewFindNodeReply ¶
func NewFindNodeReply(transaction string, localPeerID ID, peers []*Peer) *ReplyPacket
func NewPingReply ¶
func NewPingReply(transaction string, localPeerID ID) *ReplyPacket
func NewReplyPacket ¶
func NewReplyPacket(transaction string, args map[string]interface{}) *ReplyPacket
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
func NewRequest ¶
func NewRequest(dht *DHT, dest *Peer, message *QueryPacket, responseChan chan *IncomingReplyPacket) *Request
func (*Request) TransactionID ¶
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
func NewResponse ¶
func NewResponse(dht *DHT, dest *Peer, message *ReplyPacket) *Response
type TreeMap ¶
type TreeMap struct {
// contains filtered or unexported fields
}
A data structure used to keep track of the peers discovered during an ID lookup. Peers discovered are stored and sorted by distance from a given key. PeersCloserThan and UnqueriedPeers are there to feed different rounds of a FIND_NODE-RPC with peers to query.