]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add DHT nodes from metainfo when added to Client
authorMatt Joiner <anacrolix@gmail.com>
Wed, 24 Feb 2016 10:56:50 +0000 (21:56 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 24 Feb 2016 10:56:50 +0000 (21:56 +1100)
client.go
client_test.go

index cff3e5624b411b95177115b68a2bd54219a9f6c6..3a15bb3ab3b8977cfb0e0e2b3d3a20883ba13ef6 100644 (file)
--- a/client.go
+++ b/client.go
@@ -2526,6 +2526,9 @@ func (me *Client) AddMagnet(uri string) (T Torrent, err error) {
 
 func (me *Client) AddTorrent(mi *metainfo.MetaInfo) (T Torrent, err error) {
        T, _, err = me.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
+       var ss []string
+       missinggo.CastSlice(&ss, mi.Nodes)
+       me.AddDHTNodes(ss)
        return
 }
 
@@ -2534,10 +2537,27 @@ func (me *Client) AddTorrentFromFile(filename string) (T Torrent, err error) {
        if err != nil {
                return
        }
-       T, _, err = me.AddTorrentSpec(TorrentSpecFromMetaInfo(mi))
-       return
+       return me.AddTorrent(mi)
 }
 
 func (me *Client) DHT() *dht.Server {
        return me.dHT
 }
+
+func (me *Client) AddDHTNodes(nodes []string) {
+       for _, n := range nodes {
+               hmp := missinggo.SplitHostPort(n)
+               ip := net.ParseIP(hmp.Host)
+               if ip == nil {
+                       log.Printf("won't add DHT node with bad IP: %q", hmp.Host)
+                       continue
+               }
+               ni := dht.NodeInfo{
+                       Addr: dht.NewAddr(&net.UDPAddr{
+                               IP:   ip,
+                               Port: hmp.Port,
+                       }),
+               }
+               me.DHT().AddNode(ni)
+       }
+}
index 05c81a6d5198905b3d7fbea641423f869cc95def..25a82d28e9808eb85c9a38401392dc63ba5c5654 100644 (file)
@@ -755,9 +755,13 @@ func TestAddTorrentPiecesNotAlreadyCompleted(t *testing.T) {
        testAddTorrentPriorPieceCompletion(t, false)
 }
 
-func TestAddIssue65Torrent(t *testing.T) {
+func TestAddMetainfoWithNodes(t *testing.T) {
        cfg := TestingConfig
        cfg.NoDHT = false
+       // For now, we want to just jam the nodes into the table, without
+       // verifying them first. Also the DHT code doesn't support mixing secure
+       // and insecure nodes if security is enabled (yet).
+       cfg.DHTConfig.NoSecurity = true
        cl, err := NewClient(&cfg)
        require.NoError(t, err)
        defer cl.Close()