Documentation ¶
Overview ¶
Package DHT implements a DHT for use with the BitTorrent protocol, described in BEP 5: http://www.bittorrent.org/beps/bep_0005.html.
Standard use involves creating a NewServer, and calling Announce on it with the details of your local torrent client and infohash of interest.
Index ¶
- Constants
- func NodeIdSecure(id string, ip net.IP) bool
- func SecureNodeId(id []byte, ip net.IP)
- type Announce
- type CompactIPv4NodeInfo
- type KRPCError
- type Msg
- type NodeInfo
- type Peer
- type PeersValues
- type Return
- type Server
- func (s *Server) AddNode(ni NodeInfo)
- func (s *Server) Addr() net.Addr
- func (s *Server) Announce(infoHash string, port int, impliedPort bool) (*Announce, error)
- func (s *Server) Close()
- func (s *Server) ID() string
- func (s *Server) IPBlocklist() iplist.Ranger
- func (s *Server) Nodes() (nis []NodeInfo)
- func (s *Server) NumNodes() int
- func (s *Server) Ping(node *net.UDPAddr) (*Transaction, error)
- func (s *Server) SetIPBlockList(list iplist.Ranger)
- func (s *Server) Stats() (ss ServerStats)
- func (s *Server) String() string
- type ServerConfig
- type ServerStats
- type Transaction
Constants ¶
const CompactIPv4NodeInfoLen = 26
The size in bytes of a NodeInfo in its compact binary representation.
Variables ¶
This section is empty.
Functions ¶
func NodeIdSecure ¶
Returns whether the node ID is considered secure. The id is the 20 raw bytes. http://www.libtorrent.org/dht_sec.html
func SecureNodeId ¶
Makes a node ID secure, in-place. The ID is 20 raw bytes. http://www.libtorrent.org/dht_sec.html
Types ¶
type Announce ¶
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) NumContacted ¶
Returns the number of distinct remote addresses the announce has queried.
type CompactIPv4NodeInfo ¶
type CompactIPv4NodeInfo []NodeInfo
func (CompactIPv4NodeInfo) MarshalBencode ¶
func (me CompactIPv4NodeInfo) MarshalBencode() (ret []byte, err error)
func (*CompactIPv4NodeInfo) UnmarshalBencode ¶
func (me *CompactIPv4NodeInfo) UnmarshalBencode(_b []byte) (err error)
type KRPCError ¶
Represented as a string or list in bencode.
func (KRPCError) MarshalBencode ¶
func (*KRPCError) UnmarshalBencode ¶
type Msg ¶
type Msg struct { Q string `bencode:"q,omitempty"` A *struct { ID string `bencode:"id"` InfoHash string `bencode:"info_hash"` Target string `bencode:"target"` } `bencode:"a,omitempty"` T string `bencode:"t"` Y string `bencode:"y"` R *Return `bencode:"r,omitempty"` E *KRPCError `bencode:"e,omitempty"` }
The unmarshalled KRPC dict message.
type NodeInfo ¶
type NodeInfo struct { ID [20]byte Addr dHTAddr }
func (*NodeInfo) PutCompact ¶
Writes the node info to its compact binary representation in b. See CompactNodeInfoLen.
func (*NodeInfo) UnmarshalCompactIPv4 ¶
type PeersValues ¶
type PeersValues struct { Peers []Peer // Peers given in get_peers response. 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 Return ¶
type Return struct { ID string `bencode:"id"` Nodes CompactIPv4NodeInfo `bencode:"nodes,omitempty"` Token string `bencode:"token"` Values []util.CompactPeer `bencode:"values,omitempty"` }
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func (*Server) 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 ¶
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 ¶
func (s *Server) Close()
Stops the server network activity. This is all that's required to clean-up a Server.
func (*Server) ID ¶
Returns the 20-byte server ID. This is the ID used to communicate with the DHT network.
func (*Server) IPBlocklist ¶
func (*Server) Ping ¶
func (s *Server) Ping(node *net.UDPAddr) (*Transaction, error)
Sends a ping query to the address given.
func (*Server) SetIPBlockList ¶
Packets to and from any address matching a range in the list are dropped.
type ServerConfig ¶
type ServerConfig struct { Addr string // Listen address. Used if Conn is nil. Conn net.PacketConn // Don't respond to queries from other nodes. Passive bool // DHT Bootstrap nodes BootstrapNodes []string // 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(). PublicIP net.IP }
type ServerStats ¶
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 }
type Transaction ¶
type Transaction struct {
// contains filtered or unexported fields
}
func (*Transaction) SetResponseHandler ¶
func (t *Transaction) SetResponseHandler(f func(Msg))
Set a function to be called with the response.