dht: github.com/anacrolix/dht Index | Files | Directories

package dht

import "github.com/anacrolix/dht"

Standard use involves creating a Server, and calling Announce on it with the details of your local torrent client and infohash of interest.


Package Files

addr.go announce.go closest_nodes.go dht.go doc.go expvar.go int160.go nodeid.go security.go server.go tokens.go transaction.go

func NodeIdSecure Uses

func NodeIdSecure(id string, ip net.IP) bool

Returns whether the node ID is considered secure. The id is the 20 raw bytes. http://www.libtorrent.org/dht_sec.html

func SecureNodeId Uses

func SecureNodeId(id []byte, ip net.IP)

Makes a node ID secure, in-place. The ID is 20 raw bytes. http://www.libtorrent.org/dht_sec.html

type Addr Uses

type Addr interface {
    UDPAddr() *net.UDPAddr
    String() string

Used internally to refer to node network addresses. String() is called a lot, and so can be optimized. Network() is not exposed, so that the interface does not satisfy net.Addr, as the underlying type must be passed to any OS-level function that take net.Addr.

func NewAddr Uses

func NewAddr(ua *net.UDPAddr) Addr

type Announce Uses

type Announce struct {
    Peers chan PeersValues
    // contains filtered or unexported fields

Maintains state for an ongoing Announce operation. An Announce is started by calling Server.Announce.

func (*Announce) Close Uses

func (a *Announce) Close()

Stop the announce.

func (*Announce) NumContacted Uses

func (a *Announce) NumContacted() int

Returns the number of distinct remote addresses the announce has queried.

type Peer Uses

type Peer struct {
    IP   net.IP
    Port int

func (*Peer) String Uses

func (p *Peer) String() string

type PeersValues Uses

type PeersValues struct {
    Peers         []Peer // Peers given in get_peers response.
    krpc.NodeInfo        // The node that gave the response.

Corresponds to the "values" key in a get_peers KRPC response. A list of peers that a node has reported as being in the swarm for a queried info hash.

type Server Uses

type Server struct {
    // contains filtered or unexported fields

A Server defines parameters for a DHT node server that is able to send queries, and respond to the ones from the network. Each node has a globally unique identifier known as the "node ID." Node IDs are chosen at random from the same 160-bit space as BitTorrent infohashes and define the behaviour of the node. Zero valued Server does not have a valid ID and thus is unable to function properly. Use `NewServer(nil)` to initialize a default node.

func NewServer Uses

func NewServer(c *ServerConfig) (s *Server, err error)

NewServer initializes a new DHT node server.

func (*Server) AddNode Uses

func (s *Server) AddNode(ni krpc.NodeInfo)

Adds directly to the node table.

func (*Server) Addr Uses

func (s *Server) Addr() net.Addr

Addr returns the listen address for the server. Packets arriving to this address are processed by the server (unless aliens are involved).

func (*Server) Announce Uses

func (s *Server) Announce(infoHash [20]byte, port int, impliedPort bool) (*Announce, error)

This is kind of the main thing you want to do with DHT. It traverses the graph toward nodes that store peers for the infohash, streaming them to the caller, and announcing the local node to each node if allowed and specified.

func (*Server) Close Uses

func (s *Server) Close()

Stops the server network activity. This is all that's required to clean-up a Server.

func (*Server) ID Uses

func (s *Server) ID() string

ID returns the 20-byte server ID. This is the ID used to communicate with the DHT network.

func (*Server) IPBlocklist Uses

func (s *Server) IPBlocklist() iplist.Ranger

func (*Server) Nodes Uses

func (s *Server) Nodes() (nis []krpc.NodeInfo)

Exports the current node table.

func (*Server) NumNodes Uses

func (s *Server) NumNodes() int

Returns how many nodes are in the node table.

func (*Server) Ping Uses

func (s *Server) Ping(node *net.UDPAddr) (*Transaction, error)

Sends a ping query to the address given.

func (*Server) SetIPBlockList Uses

func (s *Server) SetIPBlockList(list iplist.Ranger)

Packets to and from any address matching a range in the list are dropped.

func (*Server) Stats Uses

func (s *Server) Stats() (ss ServerStats)

Stats returns statistics for the server.

func (*Server) String Uses

func (s *Server) String() string

Returns a description of the Server. Python repr-style.

type ServerConfig Uses

type ServerConfig struct {
    // Listen address. Used if Conn is nil.
    Addr string

    // Set NodeId Manually. Caller must ensure that, if NodeId does not
    // conform to DHT Security Extensions, that NoSecurity is also set. This
    // should be given as a HEX string.
    NodeIdHex string

    Conn net.PacketConn
    // Don't respond to queries from other nodes.
    Passive bool
    // DHT Bootstrap nodes
    BootstrapNodes []string
    // Disable bootstrapping from global servers even if given no BootstrapNodes.
    // This creates a solitary node that awaits other nodes; it's only useful if
    // you're creating your own DHT and want to avoid accidental crossover, without
    // spoofing a bootstrap node and filling your logs with connection errors.
    NoDefaultBootstrap bool

    // Disable the DHT security extension:
    // http://www.libtorrent.org/dht_sec.html.
    NoSecurity bool
    // Initial IP blocklist to use. Applied before serving and bootstrapping
    // begins.
    IPBlocklist iplist.Ranger
    // Used to secure the server's ID. Defaults to the Conn's LocalAddr(). Set
    // to the IP that remote nodes will see, as that IP is what they'll use to
    // validate our ID.
    PublicIP net.IP

    // Hook received queries. Return true if you don't want to propagate to
    // the default handlers.
    OnQuery func(query *krpc.Msg, source net.Addr) (propagate bool)
    // Called when a peer successfully announces to us.
    OnAnnouncePeer func(infoHash metainfo.Hash, peer Peer)

ServerConfig allows to set up a configuration of the `Server` instance to be created with NewServer

type ServerStats Uses

type ServerStats struct {
    // Count of nodes in the node table that responded to our last query or
    // haven't yet been queried.
    GoodNodes int
    // Count of nodes in the node table.
    Nodes int
    // Transactions awaiting a response.
    OutstandingTransactions int
    // Individual announce_peer requests that got a success response.
    ConfirmedAnnounces int
    // Nodes that have been blocked.
    BadNodes uint

ServerStats instance is returned by Server.Stats() and stores Server metrics

type Transaction Uses

type Transaction struct {
    // contains filtered or unexported fields

Transaction keeps track of a message exchange between nodes, such as a query message and a response message.

func (*Transaction) Close Uses

func (t *Transaction) Close()

Close (abandon) the transaction.

func (*Transaction) SetResponseHandler Uses

func (t *Transaction) SetResponseHandler(f func(krpc.Msg, bool))

SetResponseHandler sets up a function to be called when the query response is available.



Package dht imports 31 packages (graph) and is imported by 6 packages. Updated 2017-04-04. Refresh now. Tools for package owners.