Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // MaxInfoHashes is the limit of number of infohashes for which we should keep a peer list. // If this value and MaxInfoHashPeers are unchanged, after several days the used space in // RAM would approach 192MB. Large values help keeping the DHT network healthy. This // variable can only be changed before the DHT node is created with NewDHTNode. MaxInfoHashes = 16384 // MaxInfoHashPeers is the limit of number of peers to be tracked for each infohash. One // single peer contact typically consumes 6 bytes. This variable can only be changed before // the DHT node is created with NewDHTNode. MaxInfoHashPeers = 2048 )
Functions ¶
func DecodePeerAddress ¶
DecodePeerAddress transforms the binary-encoded host:port address into a human-readable format. So, "abcdef" becomes 97.98.99.100:25958.
Types ¶
type DHT ¶
type DHT struct { Logger Logger // Public channels: PeersRequestResults chan map[InfoHash][]string // key = infohash, v = slice of peers. // contains filtered or unexported fields }
DHT should be created by NewDHTNode(). It provides DHT features to a torrent client, such as finding new peers for torrent downloads without requiring a tracker.
Example ¶
ExampleDHT is a simple example that searches for a particular infohash and exits when it finds any peers. A stand-alone version can be found in the examples/ directory.
if testing.Short() { fmt.Println("Peer found for the requested infohash or the test was skipped") return } d, err := NewDHTNode(0, 100, false) if err != nil { fmt.Println(err) return } go d.DoDHT() infoHash, err := DecodeInfoHash("d1c5676ae7ac98e8b19f63565905105e3c4c37a2") if err != nil { fmt.Printf("DecodeInfoHash faiure: %v", err) return } // Give the DHT some time to "warm-up" its routing table. time.Sleep(5 * time.Second) d.PeersRequest(string(infoHash), false) var infoHashPeers map[InfoHash][]string select { case infoHashPeers = <-d.PeersRequestResults: break case <-time.After(30 * time.Second): fmt.Printf("Could not find new peers: timed out") return } for ih, peers := range infoHashPeers { if len(peers) > 0 { // Peers are encoded in binary format. Decoding example using github.com/nictuku/nettools: // for _, peer := range peers { // fmt.Println(DecodePeerAddress(peer)) // } if fmt.Sprintf("%x", ih) == "d1c5676ae7ac98e8b19f63565905105e3c4c37a2" { fmt.Println("Peer found for the requested infohash or the test was skipped") return } } }
Output: Peer found for the requested infohash or the test was skipped
func NewDHTNode ¶
func (*DHT) AddNode ¶
AddNode informs the DHT of a new node it should add to its routing table. addr is a string containing the target node's "host:port" UDP address.
func (*DHT) DoDHT ¶
func (d *DHT) DoDHT()
DoDHT is the DHT node main loop and should be run as a goroutine by the torrent client.
func (*DHT) PeersRequest ¶
PeersRequest asks the DHT to search for more peers for the infoHash provided. announce should be true if the connected peer is actively downloading this infohash, which is normally the case - unless this DHT node is just a router that doesn't downloads torrents.
type InfoHash ¶
type InfoHash string
func DecodeInfoHash ¶
DecodeInfoHash transforms a hex-encoded 20-characters string to a binary infohash.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
examples
|
|
find_infohash_and_wait
Runs a node on UDP port 11221 that attempts to collect 100 peers for an infohash, then keeps running as a passive DHT node.
|
Runs a node on UDP port 11221 that attempts to collect 100 peers for an infohash, then keeps running as a passive DHT node. |