2014-11-18 18:38:13 +00:00
|
|
|
package dht
|
|
|
|
|
2015-01-29 03:17:27 +00:00
|
|
|
// get_peers and announce_peers.
|
|
|
|
|
2014-11-18 18:38:13 +00:00
|
|
|
import (
|
2014-12-26 06:21:48 +00:00
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
2015-01-29 03:20:21 +00:00
|
|
|
"bitbucket.org/anacrolix/go.torrent/logonce"
|
|
|
|
|
2014-12-19 23:10:22 +00:00
|
|
|
"bitbucket.org/anacrolix/go.torrent/util"
|
2014-12-20 02:02:12 +00:00
|
|
|
"bitbucket.org/anacrolix/sync"
|
2014-12-19 23:10:22 +00:00
|
|
|
"github.com/willf/bloom"
|
2014-11-18 18:38:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type peerDiscovery struct {
|
|
|
|
*peerStream
|
2015-01-29 03:20:21 +00:00
|
|
|
triedAddrs *bloom.BloomFilter
|
|
|
|
pending int
|
|
|
|
server *Server
|
|
|
|
infoHash string
|
|
|
|
numContacted int
|
|
|
|
announcePort int
|
|
|
|
announcePortImplied bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pd *peerDiscovery) NumContacted() int {
|
|
|
|
pd.mu.Lock()
|
|
|
|
defer pd.mu.Unlock()
|
|
|
|
return pd.numContacted
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 03:20:21 +00:00
|
|
|
func (s *Server) Announce(infoHash string, port int, impliedPort bool) (*peerDiscovery, error) {
|
2014-11-18 18:38:13 +00:00
|
|
|
s.mu.Lock()
|
2014-12-26 06:21:48 +00:00
|
|
|
startAddrs := func() (ret []dHTAddr) {
|
2014-11-18 18:38:13 +00:00
|
|
|
for _, n := range s.closestGoodNodes(160, infoHash) {
|
|
|
|
ret = append(ret, n.addr)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}()
|
|
|
|
s.mu.Unlock()
|
|
|
|
if len(startAddrs) == 0 {
|
2014-12-19 23:09:11 +00:00
|
|
|
addrs, err := bootstrapAddrs()
|
2014-11-18 18:38:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-19 23:09:11 +00:00
|
|
|
for _, addr := range addrs {
|
2014-12-26 06:21:48 +00:00
|
|
|
startAddrs = append(startAddrs, newDHTAddr(addr))
|
2014-12-19 23:09:11 +00:00
|
|
|
}
|
2014-12-07 03:22:12 +00:00
|
|
|
}
|
|
|
|
disc := &peerDiscovery{
|
|
|
|
peerStream: &peerStream{
|
2015-01-29 03:20:21 +00:00
|
|
|
Values: make(chan peerStreamValue, 100),
|
2014-12-07 03:22:12 +00:00
|
|
|
stop: make(chan struct{}),
|
2014-12-09 03:57:53 +00:00
|
|
|
values: make(chan peerStreamValue),
|
2014-12-07 03:22:12 +00:00
|
|
|
},
|
2015-01-29 03:20:21 +00:00
|
|
|
triedAddrs: bloom.NewWithEstimates(1000, 0.5),
|
|
|
|
server: s,
|
|
|
|
infoHash: infoHash,
|
|
|
|
announcePort: port,
|
|
|
|
announcePortImplied: impliedPort,
|
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 06:24:05 +00:00
|
|
|
for i, addr := range startAddrs {
|
|
|
|
if i != 0 {
|
|
|
|
time.Sleep(time.Millisecond)
|
|
|
|
}
|
2014-12-20 02:02:12 +00:00
|
|
|
disc.mu.Lock()
|
2014-11-18 18:38:13 +00:00
|
|
|
disc.contact(addr)
|
2014-12-20 02:02:12 +00:00
|
|
|
disc.mu.Unlock()
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
2015-01-29 03:20:21 +00:00
|
|
|
return disc, nil
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
|
|
|
|
2014-12-26 06:21:48 +00:00
|
|
|
func (me *peerDiscovery) gotNodeAddr(addr dHTAddr) {
|
2014-12-09 02:00:42 +00:00
|
|
|
if util.AddrPort(addr) == 0 {
|
|
|
|
// Not a contactable address.
|
|
|
|
return
|
|
|
|
}
|
2014-12-19 23:10:22 +00:00
|
|
|
if me.triedAddrs.Test([]byte(addr.String())) {
|
2014-12-09 02:00:42 +00:00
|
|
|
return
|
|
|
|
}
|
2014-12-19 23:10:22 +00:00
|
|
|
if me.server.ipBlocked(util.AddrIP(addr)) {
|
2014-12-09 02:00:42 +00:00
|
|
|
return
|
|
|
|
}
|
2014-12-19 23:10:22 +00:00
|
|
|
me.contact(addr)
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
|
|
|
|
2014-12-26 06:21:48 +00:00
|
|
|
func (me *peerDiscovery) contact(addr dHTAddr) {
|
2015-01-29 03:20:21 +00:00
|
|
|
me.numContacted++
|
2014-12-19 23:10:22 +00:00
|
|
|
me.triedAddrs.Add([]byte(addr.String()))
|
2014-12-09 02:00:42 +00:00
|
|
|
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--
|
|
|
|
if me.pending == 0 {
|
2014-12-20 02:02:12 +00:00
|
|
|
me.close()
|
2014-12-09 02:00:42 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-01-29 03:20:21 +00:00
|
|
|
func (me *peerDiscovery) announcePeer(to dHTAddr, token string) {
|
|
|
|
err := me.server.announcePeer(to, me.infoHash, me.announcePort, token, me.announcePortImplied)
|
|
|
|
if err != nil {
|
|
|
|
logonce.Stderr.Printf("error announcing peer: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-26 06:21:48 +00:00
|
|
|
func (me *peerDiscovery) getPeers(addr dHTAddr) error {
|
2014-11-18 18:38:13 +00:00
|
|
|
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:
|
2015-01-29 03:17:27 +00:00
|
|
|
// Register suggested nodes closer to the target info-hash.
|
2014-12-09 02:00:42 +00:00
|
|
|
me.mu.Lock()
|
2014-12-20 02:02:12 +00:00
|
|
|
for _, n := range m.Nodes() {
|
|
|
|
me.responseNode(n)
|
2014-11-18 18:38:13 +00:00
|
|
|
}
|
2014-12-09 02:00:42 +00:00
|
|
|
me.mu.Unlock()
|
2015-01-29 03:20:21 +00:00
|
|
|
|
|
|
|
if vs := m.Values(); vs != nil {
|
2014-11-18 18:38:13 +00:00
|
|
|
nodeInfo := NodeInfo{
|
|
|
|
Addr: t.remoteAddr,
|
|
|
|
}
|
2015-01-29 03:20:21 +00:00
|
|
|
copy(nodeInfo.ID[:], m.ID())
|
2014-11-18 18:38:13 +00:00
|
|
|
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:
|
|
|
|
}
|
|
|
|
}
|
2015-01-29 03:20:21 +00:00
|
|
|
|
|
|
|
if at, ok := m.AnnounceToken(); ok {
|
|
|
|
me.announcePeer(addr, at)
|
|
|
|
}
|
2014-11-18 18:38:13 +00:00
|
|
|
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.
|
|
|
|
}
|
|
|
|
|
2015-01-29 03:17:27 +00:00
|
|
|
// TODO: This was to be the shared publicly accessible part returned by DHT
|
|
|
|
// functions that stream peers. Possibly not necessary anymore.
|
2014-11-18 18:38:13 +00:00
|
|
|
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-12-20 02:02:12 +00:00
|
|
|
ps.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *peerStream) close() {
|
2014-11-18 18:38:13 +00:00
|
|
|
select {
|
|
|
|
case <-ps.stop:
|
|
|
|
default:
|
|
|
|
close(ps.stop)
|
|
|
|
}
|
|
|
|
}
|