summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/BitTorrent/Core/PeerId.hs27
1 files changed, 13 insertions, 14 deletions
diff --git a/src/Network/BitTorrent/Core/PeerId.hs b/src/Network/BitTorrent/Core/PeerId.hs
index b8780a96..beec7408 100644
--- a/src/Network/BitTorrent/Core/PeerId.hs
+++ b/src/Network/BitTorrent/Core/PeerId.hs
@@ -8,9 +8,8 @@
8-- 'PeerID' represent self assigned peer identificator. Ideally each 8-- 'PeerID' represent self assigned peer identificator. Ideally each
9-- host in the network should have unique peer id to avoid 9-- host in the network should have unique peer id to avoid
10-- collisions, therefore for peer ID generation we use good entropy 10-- collisions, therefore for peer ID generation we use good entropy
11-- source. (FIX not really) Peer ID is sent in /tracker request/, 11-- source. Peer ID is sent in /tracker request/, sent and received in
12-- sent and received in /peer handshakes/ and used in /distributed 12-- /peer handshakes/ and used in DHT queries.
13-- hash table/ queries.
14-- 13--
15{-# LANGUAGE GeneralizedNewtypeDeriving #-} 14{-# LANGUAGE GeneralizedNewtypeDeriving #-}
16module Network.BitTorrent.Core.PeerId 15module Network.BitTorrent.Core.PeerId
@@ -29,7 +28,7 @@ module Network.BitTorrent.Core.PeerId
29 -- * Decoding 28 -- * Decoding
30 , clientInfo 29 , clientInfo
31 30
32 -- ** Extra 31 -- * Extra
33 , byteStringPadded 32 , byteStringPadded
34 , defaultClientId 33 , defaultClientId
35 , defaultVersionNumber 34 , defaultVersionNumber
@@ -155,12 +154,12 @@ shadowStyle cid ver rnd = PeerId $ BL.toStrict $ BS.toLazyByteString $
155 byteStringPadded rnd 15 '0' 154 byteStringPadded rnd 15 '0'
156 155
157 156
158-- | "HS" - 2 bytes long client identifier. 157-- | 'HS'- 2 bytes long client identifier.
159defaultClientId :: ByteString 158defaultClientId :: ByteString
160defaultClientId = "HS" 159defaultClientId = "HS"
161 160
162-- | Gives exactly 4 bytes long version number for any version of the 161-- | Gives exactly 4 bytes long version number for any version of the
163-- package. Version is taken from .cabal. 162-- package. Version is taken from .cabal file.
164defaultVersionNumber :: ByteString 163defaultVersionNumber :: ByteString
165defaultVersionNumber = BS.take 4 $ BC.pack $ foldMap show $ 164defaultVersionNumber = BS.take 4 $ BC.pack $ foldMap show $
166 versionBranch version 165 versionBranch version
@@ -173,14 +172,13 @@ defaultVersionNumber = BS.take 4 $ BC.pack $ foldMap show $
173-- 172--
174-- * 6 bytes : first 6 characters from picoseconds obtained with %q. 173-- * 6 bytes : first 6 characters from picoseconds obtained with %q.
175-- 174--
176-- * 1 bytes : character '.' for readability. 175-- * 1 byte : character \'.\' for readability.
177-- 176--
178-- * 9..* bytes: number of whole seconds since the Unix epoch 177-- * 9..* bytes: number of whole seconds since the Unix epoch
179-- (!)REVERSED. 178-- (!)REVERSED.
180-- 179--
181-- Can be used both with shadow and azureus style encoding. This 180-- Can be used both with shadow and azureus style encoding. This
182-- format is used to make the ID's readable(for debugging) and more 181-- format is used to make the ID's readable for debugging purposes.
183-- or less random.
184-- 182--
185timestamp :: IO ByteString 183timestamp :: IO ByteString
186timestamp = (BC.pack . format) <$> getCurrentTime 184timestamp = (BC.pack . format) <$> getCurrentTime
@@ -189,19 +187,20 @@ timestamp = (BC.pack . format) <$> getCurrentTime
189 L.take 9 (L.reverse (formatTime defaultTimeLocale "%s" t)) 187 L.take 9 (L.reverse (formatTime defaultTimeLocale "%s" t))
190 188
191-- | Gives 15 character long random bytestring. This is more robust 189-- | Gives 15 character long random bytestring. This is more robust
192-- method for generation of random part of peer ID than timestamp. 190-- method for generation of random part of peer ID than 'timestamp'.
193entropy :: IO ByteString 191entropy :: IO ByteString
194entropy = getEntropy 15 192entropy = getEntropy 15
195 193
196-- NOTE: entropy generates incorrrect peer id 194-- NOTE: entropy generates incorrrect peer id
197 195
198-- | Here we use Azureus-style encoding with the following args: 196-- | Here we use 'azureusStyle' encoding with the following args:
199-- 197--
200-- * 'HS' for the client id. 198-- * 'HS' for the client id; ('defaultClientId')
201-- 199--
202-- * Version of the package for the version number 200-- * Version of the package for the version number;
201-- ('defaultVersionNumber')
203-- 202--
204-- * UTC time day ++ day time for the random number. 203-- * UTC time day ++ day time for the random number. ('timestamp')
205-- 204--
206genPeerId :: IO PeerId 205genPeerId :: IO PeerId
207genPeerId = azureusStyle defaultClientId defaultVersionNumber <$> timestamp 206genPeerId = azureusStyle defaultClientId defaultVersionNumber <$> timestamp