From aa5ea9e2049c741140773d2adf0f0daea236d913 Mon Sep 17 00:00:00 2001 From: joe Date: Sat, 5 Aug 2017 23:45:31 -0400 Subject: Implemented Tox's announce handler. --- src/Network/BitTorrent/DHT/Token.hs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/Network') 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 @@ module Network.BitTorrent.DHT.Token ( -- * Token Token + , maxInterval + , toPaddedByteString + , fromPaddedByteString -- * Session tokens , TokenMap @@ -74,7 +77,19 @@ instance Show Token where -- | Meaningless token, for testing purposes only. instance Default Token where - def = Token "0xdeadbeef" + def = makeToken (0::Int) 0 + +-- | Prepend token with 0x20 bytes to fill the available width. +-- +-- If n > 8, then this will also guarantee a nonzero token, which is useful for +-- Tox ping-id values for announce responses. +toPaddedByteString :: Int -> Token -> BS.ByteString +toPaddedByteString n (Token bs) = BS.append (BS.replicate (n - BS.length bs) 0x20) bs + +fromPaddedByteString :: Int -> BS.ByteString -> Token +fromPaddedByteString n bs = Token $ BS.drop (n - len) bs + where + len = BS.length tok where Token tok = def -- | The secret value used as salt. type Secret = Int @@ -85,6 +100,7 @@ makeToken :: Hashable a => a -> Secret -> Token makeToken n s = Token $ toBS $ hashWithSalt s n where toBS = toStrict . toLazyByteString . int64BE . fromIntegral +{-# INLINE makeToken #-} -- | Constant space 'Node' to 'Token' map based on the secret value. data TokenMap = TokenMap -- cgit v1.2.3