summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/Torrent/PeerID.hs30
1 files changed, 25 insertions, 5 deletions
diff --git a/src/Network/Torrent/PeerID.hs b/src/Network/Torrent/PeerID.hs
index f0ce790c..d0687f59 100644
--- a/src/Network/Torrent/PeerID.hs
+++ b/src/Network/Torrent/PeerID.hs
@@ -48,11 +48,16 @@ instance URLShow PeerID where
48 urlShow = BC.unpack . getPeerID 48 urlShow = BC.unpack . getPeerID
49 49
50 50
51-- | Azureus-style encoding: 51-- | Azureus-style encoding have the following layout:
52--
52-- * 1 byte : '-' 53-- * 1 byte : '-'
54--
53-- * 2 bytes: client id 55-- * 2 bytes: client id
56--
54-- * 4 bytes: version number 57-- * 4 bytes: version number
58--
55-- * 1 byte : '-' 59-- * 1 byte : '-'
60--
56-- * 12 bytes: random number 61-- * 12 bytes: random number
57-- 62--
58azureusStyle :: ByteString -- ^ 2 character client ID, padded with 'H'. 63azureusStyle :: ByteString -- ^ 2 character client ID, padded with 'H'.
@@ -66,9 +71,12 @@ azureusStyle cid ver rnd = PeerID $ BL.toStrict $ B.toLazyByteString $
66 B.char8 '-' <> 71 B.char8 '-' <>
67 byteStringPadded rnd 12 '0' 72 byteStringPadded rnd 12 '0'
68 73
69-- | Shadow-style encoding: 74-- | Shadow-style encoding have the following layout:
75--
70-- * 1 byte : client id. 76-- * 1 byte : client id.
77--
71-- * 0-4 bytes: version number. If less than 4 then padded with '-' char. 78-- * 0-4 bytes: version number. If less than 4 then padded with '-' char.
79--
72-- * 15 bytes : random number. If length is less than 15 then padded with '0' char. 80-- * 15 bytes : random number. If length is less than 15 then padded with '0' char.
73-- 81--
74shadowStyle :: Char -- ^ Client ID. 82shadowStyle :: Char -- ^ Client ID.
@@ -91,9 +99,13 @@ defaultVersionNumber :: ByteString
91defaultVersionNumber = B.take 4 (BC.pack (foldMap show (versionBranch version))) 99defaultVersionNumber = B.take 4 (BC.pack (foldMap show (versionBranch version)))
92 100
93-- | Gives 15 characters long decimal timestamp such that: 101-- | Gives 15 characters long decimal timestamp such that:
102--
94-- * 6 bytes : first 6 characters from picoseconds obtained with %q. 103-- * 6 bytes : first 6 characters from picoseconds obtained with %q.
104--
95-- * 1 bytes : character '.' for readability. 105-- * 1 bytes : character '.' for readability.
106--
96-- * 9..* bytes: number of whole seconds since the Unix epoch (!)REVERSED. 107-- * 9..* bytes: number of whole seconds since the Unix epoch (!)REVERSED.
108--
97-- Can be used both with shadow and azureus style encoding. This format is 109-- Can be used both with shadow and azureus style encoding. This format is
98-- used to make the ID's readable(for debugging) and more or less random. 110-- used to make the ID's readable(for debugging) and more or less random.
99-- 111--
@@ -104,17 +116,25 @@ timestampByteString = (BC.pack . format) <$> getCurrentTime
104 take 9 (reverse (formatTime defaultTimeLocale "%s" t)) 116 take 9 (reverse (formatTime defaultTimeLocale "%s" t))
105 117
106-- | Here we use Azureus-style encoding with the following args: 118-- | Here we use Azureus-style encoding with the following args:
119--
107-- * 'HS' for the client id. 120-- * 'HS' for the client id.
121--
108-- * Version of the package for the version number 122-- * Version of the package for the version number
123--
109-- * UTC time day ++ day time for the random number. 124-- * UTC time day ++ day time for the random number.
110-- 125--
111newPeerID :: IO PeerID 126newPeerID :: IO PeerID
112newPeerID = azureusStyle defaultClientID defaultVersionNumber 127newPeerID = azureusStyle defaultClientID defaultVersionNumber
113 <$> timestampByteString 128 <$> timestampByteString
114 129
115-- | length < size: Complete bytestring by given charaters. 130-- | Pad bytestring so it's becomes exactly request length. Conversion is done
116-- length = size: Output bytestring as is. 131-- like so:
117-- length > size: Drop last (length - size) charaters from a given bytestring. 132--
133-- * length < size: Complete bytestring by given charaters.
134--
135-- * length = size: Output bytestring as is.
136--
137-- * length > size: Drop last (length - size) charaters from a given bytestring.
118-- 138--
119byteStringPadded :: ByteString -- ^ bytestring to be padded. 139byteStringPadded :: ByteString -- ^ bytestring to be padded.
120 -> Int -- ^ size of result builder. 140 -> Int -- ^ size of result builder.