dht

package module
v0.0.0-...-4fb33ad Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2013 License: BSD-3-Clause Imports: 22 Imported by: 0

README

This is a golang Kademlia/Bittorrent DHT library that implements BEP 5.

It's typically used by a torrent client such as Taipei-Torrent, but it could also be used by a standalone DHT routers, or for other more creative purposes.

The DHT performs well and supports the most important features despite its simple API.

A multi-node deployment is able to process more than 3000 incoming packets per second in a single core of a very old AMD Athlon(tm) 64 Processor 3700+, when the optional rate-limiting feature is disabled.

Performance stats

By default, if left running for several days the DHT node should use approx. 200MB of RAM. This can be adjusted by decreasing MaxInfoHashes and MaxInfoHashPeers accordingly.

For usage details, see the online documentation at: http://go.pkgdoc.org/github.com/nictuku/dht

A full example is at: find_infohash_and_wait

Build Status

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
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

func DecodePeerAddress(x string) string

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 NewDHTNode(port, numTargetPeers int, storeEnabled bool) (node *DHT, err error)

func (*DHT) AddNode

func (d *DHT) AddNode(addr string)

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

func (d *DHT) PeersRequest(ih string, announce bool)

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.

func (*DHT) Port

func (d *DHT) Port() int

Port returns the port number assigned to the DHT. This is useful when when initialising the DHT with port 0, i.e. automatic port assignment, in order to retrieve the actual port number used.

type InfoHash

type InfoHash string

func DecodeInfoHash

func DecodeInfoHash(x string) (b InfoHash, err error)

DecodeInfoHash transforms a hex-encoded 20-characters string to a binary infohash.

type Logger

type Logger interface {
	GetPeers(*net.UDPAddr, string, InfoHash)
}

Logger allows the DHT client to attach hooks for certain RPCs so it can log interesting events any way it wants.

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.

Jump to

Keyboard shortcuts

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