dht

package module
v0.0.0-...-6900628 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2013 License: BSD-3-Clause Imports: 20 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.

It's able to process approx 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.

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

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

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
port := rand.Intn(10000) + 40000
d, err := NewDHTNode(port, 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)

timeout := time.After(30 * time.Second)
var infoHashPeers map[InfoHash][]string
select {
case infoHashPeers = <-d.PeersRequestResults:
	break
case <-timeout:
	fmt.Printf("Could not find new peers: timed out")
	return
}
for ih, peers := range infoHashPeers {
	if len(peers) > 0 {
		fmt.Printf("peer found for infohash [%x]\n", ih)
		// Peers are encoded in binary format. Decoding example using github.com/nictuku/nettools:
		// for _, peer := range peers {
		// 	fmt.Println(DecodePeerAddress(peer))
		// }
		return
	}
}
Output:

peer found for infohash [d1c5676ae7ac98e8b19f63565905105e3c4c37a2]

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 DHTStore

type DHTStore struct {
	// The rest of the stack uses string, but that confuses the json
	// Marshaller. []byte is more correct anyway.
	Id      []byte
	Port    int
	Remotes map[string][]byte // Key: IP, Value: node ID.
	// contains filtered or unexported fields
}

DHTStore is used to persist the routing table on disk.

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