diff options
Diffstat (limited to 'src/Network/BitTorrent/DHT')
-rw-r--r-- | src/Network/BitTorrent/DHT/Token.hs | 18 |
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 @@ | |||
21 | module Network.BitTorrent.DHT.Token | 21 | module 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. |
76 | instance Default Token where | 79 | instance 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. | ||
86 | toPaddedByteString :: Int -> Token -> BS.ByteString | ||
87 | toPaddedByteString n (Token bs) = BS.append (BS.replicate (n - BS.length bs) 0x20) bs | ||
88 | |||
89 | fromPaddedByteString :: Int -> BS.ByteString -> Token | ||
90 | fromPaddedByteString 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. |
80 | type Secret = Int | 95 | type Secret = Int |
@@ -85,6 +100,7 @@ makeToken :: Hashable a => a -> Secret -> Token | |||
85 | makeToken n s = Token $ toBS $ hashWithSalt s n | 100 | makeToken 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. |
90 | data TokenMap = TokenMap | 106 | data TokenMap = TokenMap |