summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/DHT
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/DHT')
-rw-r--r--src/Network/BitTorrent/DHT/Token.hs18
1 files changed, 17 insertions, 1 deletions
diff --git a/src/Network/BitTorrent/DHT/Token.hs b/src/Network/BitTorrent/DHT/Token.hs
index 08079b75..756b5a98 100644
--- a/src/Network/BitTorrent/DHT/Token.hs
+++ b/src/Network/BitTorrent/DHT/Token.hs
@@ -21,6 +21,9 @@
21module Network.BitTorrent.DHT.Token 21module Network.BitTorrent.DHT.Token
22 ( -- * Token 22 ( -- * Token
23 Token 23 Token
24 , maxInterval
25 , toPaddedByteString
26 , fromPaddedByteString
24 27
25 -- * Session tokens 28 -- * Session tokens
26 , TokenMap 29 , TokenMap
@@ -74,7 +77,19 @@ instance Show Token where
74 77
75-- | Meaningless token, for testing purposes only. 78-- | Meaningless token, for testing purposes only.
76instance Default Token where 79instance Default Token where
77 def = Token "0xdeadbeef" 80 def = makeToken (0::Int) 0
81
82-- | Prepend token with 0x20 bytes to fill the available width.
83--
84-- If n > 8, then this will also guarantee a nonzero token, which is useful for
85-- Tox ping-id values for announce responses.
86toPaddedByteString :: Int -> Token -> BS.ByteString
87toPaddedByteString n (Token bs) = BS.append (BS.replicate (n - BS.length bs) 0x20) bs
88
89fromPaddedByteString :: Int -> BS.ByteString -> Token
90fromPaddedByteString n bs = Token $ BS.drop (n - len) bs
91 where
92 len = BS.length tok where Token tok = def
78 93
79-- | The secret value used as salt. 94-- | The secret value used as salt.
80type Secret = Int 95type Secret = Int
@@ -85,6 +100,7 @@ makeToken :: Hashable a => a -> Secret -> Token
85makeToken n s = Token $ toBS $ hashWithSalt s n 100makeToken n s = Token $ toBS $ hashWithSalt s n
86 where 101 where
87 toBS = toStrict . toLazyByteString . int64BE . fromIntegral 102 toBS = toStrict . toLazyByteString . int64BE . fromIntegral
103{-# INLINE makeToken #-}
88 104
89-- | Constant space 'Node' to 'Token' map based on the secret value. 105-- | Constant space 'Node' to 'Token' map based on the secret value.
90data TokenMap = TokenMap 106data TokenMap = TokenMap