dht

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

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

Go to latest
Published: Nov 15, 2014 License: BSD-3-Clause Imports: 18 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 5000 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. 30MB of RAM. This can be adjusted by changing MaxInfoHashes and MaxInfoHashPeers accordingly.

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

A full example is at: find_infohash_and_wait

Build Status

Google+ Community

We have a Taipei-Torrent Google+ Community for the parent project Taipei Torrent and also this DHT library.

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultConfig = NewConfig()

Functions

func RegisterFlags

func RegisterFlags(c *Config)

Registers Config fields as command line flags. If c is nil, DefaultConfig is used.

Types

type Config

type Config struct {
	// IP Address to listen on.  If left blank, one is chosen automatically.
	Address string
	// UDP port the DHT node should listen on. If zero, it picks a random port.
	Port int
	// Number of peers that DHT will try to find for each infohash being searched. This might
	// later be moved to a per-infohash option. Default value: 5.
	NumTargetPeers int
	// Maximum number of nodes to store in the routing table. Default value: 100.
	MaxNodes int
	// How often to ping nodes in the network to see if they are reachable. Default value: 15 min.
	CleanupPeriod time.Duration
	// Maximum packets per second to be processed. Disabled if negative. Default value: 100.
	RateLimit int64
	// MaxInfoHashes is the limit of number of infohashes for which we should keep a peer list.
	// If this and MaxInfoHashPeers are unchanged, it should consume around 25 MB of RAM. Larger
	// values help keeping the DHT network healthy. Default value: 2048.
	MaxInfoHashes int
	// MaxInfoHashPeers is the limit of number of peers to be tracked for each infohash. A
	// single peer contact typically consumes 6 bytes. Default value: 256.
	MaxInfoHashPeers int
	// Node ID. A random Node ID is generated if this is left blank.
	NodeID string
}

Config for the DHT Node. Use NewConfig to create a configuration with default values.

func NewConfig

func NewConfig() *Config

Creates a *Config populated with default values.

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 New(). 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 := New(nil)
if err != nil {
	fmt.Println(err)
	return
}
go d.Run()

infoHash, err := DecodeInfoHash("d1c5676ae7ac98e8b19f63565905105e3c4c37a2")
if err != nil {
	fmt.Printf("DecodeInfoHash faiure: %v", err)
	return
}

tick := time.Tick(time.Second)

var infoHashPeers map[InfoHash][]string
M:
for {
	select {
	case <-tick:
		// Repeat the request until a result appears, querying nodes that haven't been
		// consulted before and finding close-by candidates for the infohash.
		d.PeersRequest(infoHash, false)
	case infoHashPeers = <-d.PeersRequestResults:
		break M
	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 New

func New(config *Config) (node *DHT, err error)

New creates a DHT node. If config is nil, DefaultConfig will be used. Changing the config after calling this function has no effect.

This method replaces NewDHTNode.

func (*DHT) AddNode

func (d *DHT) AddNode(addr, id 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. You may optionally provide the node ID of the node. The node ID should be in binary format, not hex. Specify a node ID of "" if you don't know what the node ID is.

func (*DHT) ForceAddNode

func (d *DHT) ForceAddNode(addr, id string)

Add a node to the DHT even if the routing table is already full. Arguments are the same as for AddNode().

func (*DHT) PeersRequest

func (d *DHT) PeersRequest(ih InfoHash, 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.

func (*DHT) Run

func (d *DHT) Run() error

Run starts a DHT node. It bootstraps a routing table, if necessary, and listens for incoming DHT requests.

func (*DHT) Stop

func (d *DHT) Stop()

Stop the DHT node.

func (*DHT) VisitNodes

func (d *DHT) VisitNodes(f VisitNodeFunc) error

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.

func (InfoHash) String

func (ih InfoHash) String() string

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.

type VisitNodeFunc

type VisitNodeFunc func(address string, nodeID []byte) error

Called by DHT.VisitNodes(). address is in "host:port" format. You can short-circuit the visitation process by returning a non-nil error value, which is passed through and returned from VisitNodes().

Directories

Path Synopsis
examples
find_infohash_and_wait
Runs a node on a random UDP port that attempts to collect 10 peers for an infohash, then keeps running as a passive DHT node.
Runs a node on a random UDP port that attempts to collect 10 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