dht: github.com/shiyanhui/dht Index | Files

package dht

import "github.com/shiyanhui/dht"

Package dht implements the bittorrent dht protocol. For more information see http://www.bittorrent.org/beps/bep_0005.html.

Index

Package Files

bencode.go bitmap.go bitmap_xorfast.go blacklist.go container.go dht.go krpc.go peerwire.go routingtable.go util.go

Constants

const (
    // StandardMode follows the standard protocol
    StandardMode = iota
    // CrawlMode for crawling the dht network.
    CrawlMode
)
const (
    // REQUEST represents request message type
    REQUEST = iota
    // DATA represents data message type
    DATA
    // REJECT represents reject message type
    REJECT
)
const (
    // BLOCK is 2 ^ 14
    BLOCK = 16384
    // MaxMetadataSize represents the max medata it can accept
    MaxMetadataSize = BLOCK * 1000
    // EXTENDED represents it is a extended message
    EXTENDED = 20
    // HANDSHAKE represents handshake bit
    HANDSHAKE = 0
)

Variables

var (
    // ErrNotReady is the error when DHT is not initialized.
    ErrNotReady = errors.New("dht is not ready")
    // ErrOnGetPeersResponseNotSet is the error that config
    // OnGetPeersResponseNotSet is not set when call dht.GetPeers.
    ErrOnGetPeersResponseNotSet = errors.New("OnGetPeersResponse is not set")
)

func Decode Uses

func Decode(data []byte) (result interface{}, err error)

Decode decodes a bencoded string to string, int, list or map.

func DecodeDict Uses

func DecodeDict(data []byte, start int) (
    result interface{}, index int, err error)

DecodeDict decodes a map value.

func DecodeInt Uses

func DecodeInt(data []byte, start int) (
    result interface{}, index int, err error)

DecodeInt decodes int value in the data.

func DecodeList Uses

func DecodeList(data []byte, start int) (
    result interface{}, index int, err error)

DecodeList decodes a list value.

func DecodeString Uses

func DecodeString(data []byte, start int) (
    result interface{}, index int, err error)

DecodeString decodes a string in the data. It returns a tuple (decoded result, the end position, error).

func Encode Uses

func Encode(data interface{}) string

Encode encodes a string, int, dict or list value to a bencoded string.

func EncodeDict Uses

func EncodeDict(data map[string]interface{}) string

EncodeDict encodes a dict value.

func EncodeInt Uses

func EncodeInt(data int) string

EncodeInt encodes a int value.

func EncodeList Uses

func EncodeList(data []interface{}) string

EncodeList encodes a list value.

func EncodeString Uses

func EncodeString(data string) string

EncodeString encodes a string value.

func ParseKey Uses

func ParseKey(data map[string]interface{}, key string, t string) error

ParseKey parses the key in dict data. `t` is type of the keyed value. It's one of "int", "string", "map", "list".

func ParseKeys Uses

func ParseKeys(data map[string]interface{}, pairs [][]string) error

ParseKeys parses keys. It just wraps ParseKey.

type Config Uses

type Config struct {
    // in mainline dht, k = 8
    K   int
    // for crawling mode, we put all nodes in one bucket, so KBucketSize may
    // not be K
    KBucketSize int
    // candidates are udp, udp4, udp6
    Network string
    // format is `ip:port`
    Address string
    // the prime nodes through which we can join in dht network
    PrimeNodes []string
    // the kbucket expired duration
    KBucketExpiredAfter time.Duration
    // the node expired duration
    NodeExpriedAfter time.Duration
    // how long it checks whether the bucket is expired
    CheckKBucketPeriod time.Duration
    // peer token expired duration
    TokenExpiredAfter time.Duration
    // the max transaction id
    MaxTransactionCursor uint64
    // how many nodes routing table can hold
    MaxNodes int
    // callback when got get_peers request
    OnGetPeers func(string, string, int)
    // callback when receive get_peers response
    OnGetPeersResponse func(string, *Peer)
    // callback when got announce_peer request
    OnAnnouncePeer func(string, string, int)
    // blcoked ips
    BlockedIPs []string
    // blacklist size
    BlackListMaxSize int
    // StandardMode or CrawlMode
    Mode int
    // the times it tries when send fails
    Try int
    // the size of packet need to be dealt with
    PacketJobLimit int
    // the size of packet handler
    PacketWorkerLimit int
    // the nodes num to be fresh in a kbucket
    RefreshNodeNum int
}

Config represents the configure of dht.

func NewCrawlConfig Uses

func NewCrawlConfig() *Config

NewCrawlConfig returns a config in crawling mode.

func NewStandardConfig Uses

func NewStandardConfig() *Config

NewStandardConfig returns a Config pointer with default values.

type DHT Uses

type DHT struct {
    *Config

    Ready bool
    // contains filtered or unexported fields
}

DHT represents a DHT node.

func New Uses

func New(config *Config) *DHT

New returns a DHT pointer. If config is nil, then config will be set to the default config.

func (*DHT) GetPeers Uses

func (dht *DHT) GetPeers(infoHash string) error

GetPeers returns peers who have announced having infoHash.

func (*DHT) IsCrawlMode Uses

func (dht *DHT) IsCrawlMode() bool

IsCrawlMode returns whether mode is CrawlMode.

func (*DHT) IsStandardMode Uses

func (dht *DHT) IsStandardMode() bool

IsStandardMode returns whether mode is StandardMode.

func (*DHT) Run Uses

func (dht *DHT) Run()

Run starts the dht.

type Peer Uses

type Peer struct {
    IP   net.IP
    Port int
    // contains filtered or unexported fields
}

Peer represents a peer contact.

func (*Peer) CompactIPPortInfo Uses

func (p *Peer) CompactIPPortInfo() string

CompactIPPortInfo returns "Compact node info". See http://www.bittorrent.org/beps/bep_0005.html.

type Request Uses

type Request struct {
    InfoHash []byte
    IP       string
    Port     int
}

Request represents the request context.

type Response Uses

type Response struct {
    Request
    MetadataInfo []byte
}

Response contains the request context and the metadata info.

type Wire Uses

type Wire struct {
    // contains filtered or unexported fields
}

Wire represents the wire protocol.

func NewWire Uses

func NewWire(blackListSize, requestQueueSize, workerQueueSize int) *Wire

NewWire returns a Wire pointer.

- blackListSize: the blacklist size
- requestQueueSize: the max requests it can buffers
- workerQueueSize: the max goroutine downloading workers

func (*Wire) Request Uses

func (wire *Wire) Request(infoHash []byte, ip string, port int)

Request pushes the request to the queue.

func (*Wire) Response Uses

func (wire *Wire) Response() <-chan Response

Response returns a chan of Response.

func (*Wire) Run Uses

func (wire *Wire) Run()

Run starts the peer wire protocol.

Package dht imports 21 packages (graph) and is imported by 1 packages. Updated 2019-03-23. Refresh now. Tools for package owners.