summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-01-06 00:21:12 -0500
committerjoe <joe@jerkface.net>2017-01-06 00:21:12 -0500
commitbcd860aa8816cf52a01c313aecfdcde21fcd2c16 (patch)
tree7c3d0c1f4e8ca32505d41212cde6f4c502e8e91f /src/Network/BitTorrent
parentc02531ba4555e205d59672e6cd9b65a6c8931506 (diff)
Method to recover thisNodeId from stored table.
Diffstat (limited to 'src/Network/BitTorrent')
-rw-r--r--src/Network/BitTorrent/Address.hs3
-rw-r--r--src/Network/BitTorrent/DHT.hs2
-rw-r--r--src/Network/BitTorrent/DHT/Routing.hs33
3 files changed, 37 insertions, 1 deletions
diff --git a/src/Network/BitTorrent/Address.hs b/src/Network/BitTorrent/Address.hs
index 3d2616cc..7ef837db 100644
--- a/src/Network/BitTorrent/Address.hs
+++ b/src/Network/BitTorrent/Address.hs
@@ -56,7 +56,8 @@ module Network.BitTorrent.Address
56 56
57 -- * Node 57 -- * Node
58 -- ** Id 58 -- ** Id
59 , NodeId 59 , NodeId(..)
60 , nodeIdSize
60 , testIdBit 61 , testIdBit
61 , NodeDistance 62 , NodeDistance
62 , distance 63 , distance
diff --git a/src/Network/BitTorrent/DHT.hs b/src/Network/BitTorrent/DHT.hs
index a5b4612f..1c62a0e0 100644
--- a/src/Network/BitTorrent/DHT.hs
+++ b/src/Network/BitTorrent/DHT.hs
@@ -129,6 +129,8 @@ dht opts addr action = do
129-- 129--
130-- * "router.utorrent.com" since it is just an alias to 130-- * "router.utorrent.com" since it is just an alias to
131-- "router.bittorrent.com". 131-- "router.bittorrent.com".
132-- XXX: ignoring this advise as it resolves to a different
133-- ip address for me.
132 134
133-- | List of bootstrap nodes maintained by different bittorrent 135-- | List of bootstrap nodes maintained by different bittorrent
134-- software authors. 136-- software authors.
diff --git a/src/Network/BitTorrent/DHT/Routing.hs b/src/Network/BitTorrent/DHT/Routing.hs
index 14aec612..a4da8445 100644
--- a/src/Network/BitTorrent/DHT/Routing.hs
+++ b/src/Network/BitTorrent/DHT/Routing.hs
@@ -37,6 +37,7 @@ module Network.BitTorrent.DHT.Routing
37 , shape 37 , shape
38 , Network.BitTorrent.DHT.Routing.size 38 , Network.BitTorrent.DHT.Routing.size
39 , Network.BitTorrent.DHT.Routing.depth 39 , Network.BitTorrent.DHT.Routing.depth
40 , compatibleNodeId
40 41
41 -- * Lookup 42 -- * Lookup
42 , K 43 , K
@@ -77,6 +78,8 @@ import Data.Word
77import GHC.Generics 78import GHC.Generics
78import Text.PrettyPrint as PP hiding ((<>)) 79import Text.PrettyPrint as PP hiding ((<>))
79import Text.PrettyPrint.HughesPJClass (pPrint,Pretty) 80import Text.PrettyPrint.HughesPJClass (pPrint,Pretty)
81import qualified Data.ByteString as BS
82import Data.Bits
80 83
81import Data.Torrent 84import Data.Torrent
82import Network.BitTorrent.Address 85import Network.BitTorrent.Address
@@ -442,6 +445,36 @@ lookupBucket nid = go 0
442 | otherwise = pure bucket 445 | otherwise = pure bucket
443 go _ (Tip _ _ bucket) = pure bucket 446 go _ (Tip _ _ bucket) = pure bucket
444 447
448compatibleNodeId :: Table ip -> IO NodeId
449compatibleNodeId tbl = genBucketSample prefix br
450 where
451 br = bucketRange (L.length (shape tbl) - 1) True
452 bs = BS.pack $ take nodeIdSize $ tablePrefix tbl ++ repeat 0
453 prefix = NodeId bs
454
455tablePrefix :: Table ip -> [Word8]
456tablePrefix = map (packByte . take 8 . (++repeat False))
457 . chunksOf 8
458 . tableBits
459 where
460 packByte = foldl1' (.|.) . zipWith bitmask [7,6 .. 0]
461 bitmask ix True = bit ix
462 bitmask _ _ = 0
463
464tableBits :: Table ip -> [Bool]
465tableBits (One _ tbl) = True : tableBits tbl
466tableBits (Zero tbl _) = False : tableBits tbl
467tableBits (Tip _ _ _) = []
468
469chunksOf :: Int -> [e] -> [[e]]
470chunksOf i ls = map (take i) (build (splitter ls)) where
471 splitter :: [e] -> ([e] -> a -> a) -> a -> a
472 splitter [] _ n = n
473 splitter l c n = l `c` splitter (drop i l) c n
474
475build :: ((a -> [a] -> [a]) -> [a] -> [a]) -> [a]
476build g = g (:) []
477
445-- | Count of closest nodes in find_node request. 478-- | Count of closest nodes in find_node request.
446type K = Int 479type K = Int
447 480