summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/DHT
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/DHT
parentc02531ba4555e205d59672e6cd9b65a6c8931506 (diff)
Method to recover thisNodeId from stored table.
Diffstat (limited to 'src/Network/BitTorrent/DHT')
-rw-r--r--src/Network/BitTorrent/DHT/Routing.hs33
1 files changed, 33 insertions, 0 deletions
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