2014-11-18 18:38:13 +00:00
|
|
|
package dht
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"bitbucket.org/anacrolix/go.torrent/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type peerDiscovery struct {
|
|
|
|
*peerStream
|
2014-12-09 02:00:42 +00:00
|
|
|
triedAddrs map[string]struct{}
|
|
|
|
backlog map[string]net.Addr
|
|
|
|
pending int
|
|
|
|
server *Server
|
|
|
|
infoHash string
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 03:57:53 +00:00
|
|
|
const (
|
|
|
|
parallelQueries = 100
|
|
|
|
backlogMaxLen = 10000
|
|
|
|
)
|
2014-12-09 02:00:42 +00:00
|
|
|
|
2014-11-18 18:38:13 +00:00
|
|
|
func (me *peerDiscovery) Close() {
|
|
|
|
me.peerStream.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) GetPeers(infoHash string) (*peerStream, error) {
|
|
|
|
s.mu.Lock()
|
|
|
|
startAddrs := func() (ret []net.Addr) {
|
|
|
|
for _, n := range s.closestGoodNodes(160, infoHash) {
|
|
|
|
ret = append(ret, n.addr)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}()
|
|
|
|
s.mu.Unlock()
|
|
|
|
if len(startAddrs) == 0 {
|
|
|
|
addr, err := bootstrapAddr()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-07 03:22:12 +00:00
|
|
|
startAddrs = append(startAddrs, addr)
|
|
|
|
}
|
|
|
|
disc := &peerDiscovery{
|
|
|
|
peerStream: &peerStream{
|
|
|
|
Values: make(chan peerStreamValue),
|
|
|
|
stop: make(chan struct{}),
|
2014-12-09 03:57:53 +00:00
|
|
|
values: make(chan peerStreamValue),
|
2014-12-07 03:22:12 +00:00
|
|
|
},
|
2014-12-09 02:00:42 +00:00
|
|
|
triedAddrs: make(map[string]struct{}, 500),
|
|
|
|
backlog: make(map[string]net.Addr, parallelQueries),
|
|
|
|
server: s,
|
|
|
|
infoHash: infoHash,
|
2014-12-07 03:22:12 +00:00
|
|
|
}
|
2014-12-09 03:57:53 +00:00
|
|
|
// Function ferries from values to Values until discovery is halted.
|
|
|
|
go func() {
|
|
|
|
defer close(disc.Values)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case psv := <-disc.values:
|
|
|
|
select {
|
|
|
|
case disc.Values <- psv:
|
|
|
|
case <-disc.stop:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-disc.stop:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2014-12-09 02:00:42 +00:00
|
|
|
disc.mu.Lock()
|
2014-12-09 01:14:10 +00:00
|
|
|
for _, addr := range startAddrs {
|
2014-11-18 18:38:13 +00:00
|
|
|
disc.contact(addr)
|
|
|
|
}
|
2014-12-09 02:00:42 +00:00
|
|
|
disc.mu.Unlock()
|
2014-11-18 18:38:13 +00:00
|
|
|
return disc.peerStream, nil
|
|
|
|
}
|
|
|
|
|
2014-12-09 02:00:42 +00:00
|
|
|
func (me *peerDiscovery) gotNodeAddr(addr net.Addr) {
|
|
|
|
if util.AddrPort(addr) == 0 {
|
|
|
|
// Not a contactable address.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if me.server.ipBlocked(util.AddrIP(addr)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := me.triedAddrs[addr.String()]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := me.backlog[addr.String()]; ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if me.pending >= parallelQueries {
|
2014-12-09 03:57:53 +00:00
|
|
|
if len(me.backlog) < backlogMaxLen {
|
|
|
|
me.backlog[addr.String()] = addr
|
|
|
|
}
|
2014-12-09 02:00:42 +00:00
|
|
|
} else {
|
|
|
|
me.contact(addr)
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-09 02:00:42 +00:00
|
|
|
func (me *peerDiscovery) contact(addr net.Addr) {
|
|
|
|
me.triedAddrs[addr.String()] = struct{}{}
|
|
|
|
if err := me.getPeers(addr); err != nil {
|
|
|
|
log.Printf("error sending get_peers request to %s: %s", addr, err)
|
2014-12-09 01:14:10 +00:00
|
|
|
return
|
|
|
|
}
|
2014-12-09 02:00:42 +00:00
|
|
|
me.pending++
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
|
|
|
|
2014-12-09 02:00:42 +00:00
|
|
|
func (me *peerDiscovery) transactionClosed() {
|
|
|
|
me.pending--
|
|
|
|
for key, addr := range me.backlog {
|
|
|
|
if me.pending >= parallelQueries {
|
|
|
|
break
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
2014-12-09 02:00:42 +00:00
|
|
|
delete(me.backlog, key)
|
|
|
|
me.contact(addr)
|
|
|
|
}
|
|
|
|
if me.pending == 0 {
|
|
|
|
me.Close()
|
|
|
|
return
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-09 02:00:42 +00:00
|
|
|
func (me *peerDiscovery) responseNode(node NodeInfo) {
|
|
|
|
me.gotNodeAddr(node.Addr)
|
|
|
|
}
|
|
|
|
|
2014-11-18 18:38:13 +00:00
|
|
|
func (me *peerDiscovery) closingCh() chan struct{} {
|
|
|
|
return me.peerStream.stop
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *peerDiscovery) getPeers(addr net.Addr) error {
|
|
|
|
me.server.mu.Lock()
|
|
|
|
defer me.server.mu.Unlock()
|
|
|
|
t, err := me.server.getPeers(addr, me.infoHash)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case m := <-t.Response:
|
2014-12-09 02:00:42 +00:00
|
|
|
me.mu.Lock()
|
2014-11-18 18:38:13 +00:00
|
|
|
if nodes := m.Nodes(); len(nodes) != 0 {
|
|
|
|
for _, n := range nodes {
|
|
|
|
me.responseNode(n)
|
|
|
|
}
|
|
|
|
}
|
2014-12-09 02:00:42 +00:00
|
|
|
me.mu.Unlock()
|
2014-11-18 18:38:13 +00:00
|
|
|
if vs := extractValues(m); vs != nil {
|
|
|
|
nodeInfo := NodeInfo{
|
|
|
|
Addr: t.remoteAddr,
|
|
|
|
}
|
|
|
|
id := func() string {
|
|
|
|
defer func() {
|
|
|
|
recover()
|
|
|
|
}()
|
|
|
|
return m["r"].(map[string]interface{})["id"].(string)
|
|
|
|
}()
|
|
|
|
copy(nodeInfo.ID[:], id)
|
|
|
|
select {
|
2014-12-09 03:57:53 +00:00
|
|
|
case me.peerStream.values <- peerStreamValue{
|
2014-11-18 18:38:13 +00:00
|
|
|
Peers: vs,
|
|
|
|
NodeInfo: nodeInfo,
|
|
|
|
}:
|
|
|
|
case <-me.peerStream.stop:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-me.closingCh():
|
|
|
|
}
|
|
|
|
t.Close()
|
2014-12-09 02:00:42 +00:00
|
|
|
me.mu.Lock()
|
|
|
|
me.transactionClosed()
|
|
|
|
me.mu.Unlock()
|
2014-11-18 18:38:13 +00:00
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type peerStreamValue struct {
|
|
|
|
Peers []util.CompactPeer // Peers given in get_peers response.
|
|
|
|
NodeInfo // The node that gave the response.
|
|
|
|
}
|
|
|
|
|
|
|
|
type peerStream struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
Values chan peerStreamValue
|
2014-12-09 03:57:53 +00:00
|
|
|
// Inner chan is set to nil when on close.
|
|
|
|
values chan peerStreamValue
|
2014-11-18 18:38:13 +00:00
|
|
|
stop chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *peerStream) Close() {
|
|
|
|
ps.mu.Lock()
|
2014-12-09 01:14:10 +00:00
|
|
|
defer ps.mu.Unlock()
|
2014-11-18 18:38:13 +00:00
|
|
|
select {
|
|
|
|
case <-ps.stop:
|
|
|
|
default:
|
|
|
|
close(ps.stop)
|
|
|
|
}
|
|
|
|
}
|