]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Filter out DHT peers with port 0
authorMatt Joiner <anacrolix@gmail.com>
Mon, 3 Aug 2015 15:20:10 +0000 (01:20 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 3 Aug 2015 15:20:10 +0000 (01:20 +1000)
Probably not necessary now we ban the nodes that send them, but it's appropriate to check here too.

client.go

index 9469096fca4752db4d6f37d4a6029d392d054980..ec3f94dcfa0457dca4c8de1c7b10915d6e0520d2 100644 (file)
--- a/client.go
+++ b/client.go
@@ -2261,24 +2261,26 @@ func (cl *Client) announceTorrentDHT(t *torrent, impliedPort bool) {
                                if !ok {
                                        break getPeers
                                }
-                               peersFoundByDHT.Add(int64(len(v.Peers)))
-                               for _, p := range v.Peers {
-                                       allAddrs[(&net.UDPAddr{
-                                               IP:   p.IP[:],
-                                               Port: int(p.Port),
-                                       }).String()] = struct{}{}
+                               addPeers := make([]Peer, 0, len(v.Peers))
+                               for _, cp := range v.Peers {
+                                       if cp.Port == 0 {
+                                               // Can't do anything with this.
+                                               continue
+                                       }
+                                       addPeers = append(addPeers, Peer{
+                                               IP:     cp.IP[:],
+                                               Port:   int(cp.Port),
+                                               Source: peerSourceDHT,
+                                       })
+                                       key := (&net.UDPAddr{
+                                               IP:   cp.IP[:],
+                                               Port: int(cp.Port),
+                                       }).String()
+                                       allAddrs[key] = struct{}{}
                                }
+                               peersFoundByDHT.Add(int64(len(addPeers)))
                                cl.mu.Lock()
-                               cl.addPeers(t, func() (ret []Peer) {
-                                       for _, cp := range v.Peers {
-                                               ret = append(ret, Peer{
-                                                       IP:     cp.IP[:],
-                                                       Port:   int(cp.Port),
-                                                       Source: peerSourceDHT,
-                                               })
-                                       }
-                                       return
-                               }())
+                               cl.addPeers(t, addPeers)
                                numPeers := len(t.Peers)
                                cl.mu.Unlock()
                                if numPeers >= torrentPeersHighWater {