diff options
author | joe <joe@jerkface.net> | 2017-01-06 00:21:12 -0500 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-01-06 00:21:12 -0500 |
commit | bcd860aa8816cf52a01c313aecfdcde21fcd2c16 (patch) | |
tree | 7c3d0c1f4e8ca32505d41212cde6f4c502e8e91f /src/Network | |
parent | c02531ba4555e205d59672e6cd9b65a6c8931506 (diff) |
Method to recover thisNodeId from stored table.
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/BitTorrent/Address.hs | 3 | ||||
-rw-r--r-- | src/Network/BitTorrent/DHT.hs | 2 | ||||
-rw-r--r-- | src/Network/BitTorrent/DHT/Routing.hs | 33 |
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 | |||
77 | import GHC.Generics | 78 | import GHC.Generics |
78 | import Text.PrettyPrint as PP hiding ((<>)) | 79 | import Text.PrettyPrint as PP hiding ((<>)) |
79 | import Text.PrettyPrint.HughesPJClass (pPrint,Pretty) | 80 | import Text.PrettyPrint.HughesPJClass (pPrint,Pretty) |
81 | import qualified Data.ByteString as BS | ||
82 | import Data.Bits | ||
80 | 83 | ||
81 | import Data.Torrent | 84 | import Data.Torrent |
82 | import Network.BitTorrent.Address | 85 | import 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 | ||
448 | compatibleNodeId :: Table ip -> IO NodeId | ||
449 | compatibleNodeId 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 | |||
455 | tablePrefix :: Table ip -> [Word8] | ||
456 | tablePrefix = 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 | |||
464 | tableBits :: Table ip -> [Bool] | ||
465 | tableBits (One _ tbl) = True : tableBits tbl | ||
466 | tableBits (Zero tbl _) = False : tableBits tbl | ||
467 | tableBits (Tip _ _ _) = [] | ||
468 | |||
469 | chunksOf :: Int -> [e] -> [[e]] | ||
470 | chunksOf 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 | |||
475 | build :: ((a -> [a] -> [a]) -> [a] -> [a]) -> [a] | ||
476 | build g = g (:) [] | ||
477 | |||
445 | -- | Count of closest nodes in find_node request. | 478 | -- | Count of closest nodes in find_node request. |
446 | type K = Int | 479 | type K = Int |
447 | 480 | ||