1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
|
-- |
-- Copyright : (c) Sam T. 2013
-- License : MIT
-- Maintainer : pxqr.sta@gmail.com
-- Stability : experimental
-- Portability : non-portable
--
-- This modules provides three datatypes related to a peer as a host:
--
-- * 'PeerID' represent self assigned peer identificator. Ideally
-- each host in the network should have unique peer id to avoid
-- collisions, therefor for peer ID generation we use good entropy
-- source. (FIX not really) Peer ID is sent in /tracker request/,
-- sent and received in /peer handshakes/ and used in /distributed
-- hash table/ queries.
--
-- * 'PeerAddr' is used to represent peer location. Currently it's
-- just peer IP and peer port but this might be changed later.
--
-- * 'ClientInfo' is used to identify the client implementation and
-- version which also contained in 'Peer'. For exsample first
-- 6 bytes of peer id of this this library are @-HS0100-@ while
-- for mainline we have @M4-3-6--@.
-- We could extract this info and print in human frienly form: this
-- is useful for debugging and logging. For more information see:
-- <http://bittorrent.org/beps/bep_0020.html>
-- NOTE: Do _not_ use this information to control client
-- capabilities (such as supported enchancements), this should be
-- done using 'Network.BitTorrent.Extension'!
--
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE RecordWildCards #-}
{-# OPTIONS -fno-warn-orphans #-}
module Network.BitTorrent.Peer
( -- * Peer identificators
PeerID (getPeerID), ppPeerID
-- ** Encoding styles
, azureusStyle, shadowStyle
-- ** Defaults
, defaultClientID, defaultVersionNumber
-- ** Generation
, newPeerID, timestampByteString
-- ** Extra
, byteStringPadded
-- * Peer address
, PeerAddr(..)
, peerSockAddr
, connectToPeer, forkListener
, ppPeer
-- * Client version detection
-- ** Info
, ClientInfo(..), clientInfo, ppClientInfo, unknownClient
-- ** Version
, ClientVersion, ppClientVersion
-- ** Implementation
, ClientImpl(..), ppClientImpl
) where
import Control.Applicative
import Control.Concurrent
import Control.Exception
import Data.BEncode
import Data.Bits
import Data.Word
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Lazy.Builder as B
import Data.Foldable (foldMap)
import Data.Monoid ((<>))
import Data.Serialize
import Data.URLEncoded
import Data.Version (Version(Version), versionBranch)
import Data.Time.Clock (getCurrentTime)
import Data.Time.Format (formatTime)
import Text.PrettyPrint (text, Doc, (<+>))
import System.Locale (defaultTimeLocale)
import Network hiding (accept)
import Network.Socket
-- TODO we have linker error here, so manual hardcoded version for a
-- while.
-- import Paths_network_bittorrent (version)
version :: Version
version = Version [0, 10, 0, 0] []
{-----------------------------------------------------------------------
Peer identification
-----------------------------------------------------------------------}
-- | Peer identifier is exactly 20 bytes long bytestring.
newtype PeerID = PeerID { getPeerID :: ByteString }
deriving (Show, Eq, Ord, BEncodable)
instance Serialize PeerID where
put = putByteString . getPeerID
get = PeerID <$> getBytes 20
instance URLShow PeerID where
urlShow = BC.unpack . getPeerID
-- | Format peer id in human readable form.
ppPeerID :: PeerID -> Doc
ppPeerID = text . BC.unpack . getPeerID
-- | Azureus-style encoding have the following layout:
--
-- * 1 byte : '-'
--
-- * 2 bytes: client id
--
-- * 4 bytes: version number
--
-- * 1 byte : '-'
--
-- * 12 bytes: random number
--
azureusStyle :: ByteString -- ^ 2 character client ID, padded with 'H'.
-> ByteString -- ^ Version number, padded with 'X'.
-> ByteString -- ^ Random number, padded with '0'.
-> PeerID -- ^ Azureus-style encoded peer ID.
azureusStyle cid ver rnd = PeerID $ BL.toStrict $ B.toLazyByteString $
B.char8 '-' <>
byteStringPadded cid 2 'H' <>
byteStringPadded ver 4 'X' <>
B.char8 '-' <>
byteStringPadded rnd 12 '0'
-- | Shadow-style encoding have the following layout:
--
-- * 1 byte : client id.
--
-- * 0-4 bytes: version number. If less than 4 then padded with
-- '-' char.
--
-- * 15 bytes : random number. If length is less than 15 then
-- padded with '0' char.
--
shadowStyle :: Char -- ^ Client ID.
-> ByteString -- ^ Version number.
-> ByteString -- ^ Random number.
-> PeerID -- ^ Shadow style encoded peer ID.
shadowStyle cid ver rnd = PeerID $ BL.toStrict $ B.toLazyByteString $
B.char8 cid <>
byteStringPadded ver 4 '-' <>
byteStringPadded rnd 15 '0'
-- | "HS" - 2 bytes long client identifier.
defaultClientID :: ByteString
defaultClientID = "HS"
-- | Gives exactly 4 bytes long version number for any version of the
-- package. Version is taken from .cabal.
defaultVersionNumber :: ByteString
defaultVersionNumber = B.take 4 $ BC.pack $ foldMap show $
versionBranch version
-- | Gives 15 characters long decimal timestamp such that:
--
-- * 6 bytes : first 6 characters from picoseconds obtained with %q.
--
-- * 1 bytes : character '.' for readability.
--
-- * 9..* bytes: number of whole seconds since the Unix epoch
-- (!)REVERSED.
--
-- Can be used both with shadow and azureus style encoding. This
-- format is used to make the ID's readable(for debugging) and more
-- or less random.
--
timestampByteString :: IO ByteString
timestampByteString = (BC.pack . format) <$> getCurrentTime
where
format t = take 6 (formatTime defaultTimeLocale "%q" t) ++ "." ++
take 9 (reverse (formatTime defaultTimeLocale "%s" t))
-- | Here we use Azureus-style encoding with the following args:
--
-- * 'HS' for the client id.
--
-- * Version of the package for the version number
--
-- * UTC time day ++ day time for the random number.
--
newPeerID :: IO PeerID
newPeerID = azureusStyle defaultClientID defaultVersionNumber
<$> timestampByteString
-- | Pad bytestring so it's becomes exactly request length. Conversion
-- is done like so:
--
-- * length < size: Complete bytestring by given charaters.
--
-- * length = size: Output bytestring as is.
--
-- * length > size: Drop last (length - size) charaters from a
-- given bytestring.
--
byteStringPadded :: ByteString -- ^ bytestring to be padded.
-> Int -- ^ size of result builder.
-> Char -- ^ character used for padding.
-> B.Builder
byteStringPadded bs s c =
B.byteString (B.take s bs) <>
B.byteString (BC.replicate padLen c)
where
padLen = s - min (B.length bs) s
{-----------------------------------------------------------------------
Client detection
-----------------------------------------------------------------------}
-- | All known client versions.
data ClientImpl =
IUnknown
| IAres
| IArctic
| IAvicora
| IBitPump
| IAzureus
| IBitBuddy
| IBitComet
| IBitflu
| IBTG
| IBitRocket
| IBTSlave
| IBittorrentX
| IEnhancedCTorrent
| ICTorrent
| IDelugeTorrent
| IPropagateDataClient
| IEBit
| IElectricSheep
| IFoxTorrent
| IGSTorrent
| IHalite
| IlibHSbittorrent
| IHydranode
| IKGet
| IKTorrent
| ILH_ABC
| ILphant
| ILibtorrent
| ILibTorrent
| ILimeWire
| IMonoTorrent
| IMooPolice
| IMiro
| IMoonlightTorrent
| INetTransport
| IPando
| IqBittorrent
| IQQDownload
| IQt4TorrentExample
| IRetriever
| IShareaza
| ISwiftbit
| ISwarmScope
| ISymTorrent
| Isharktorrent
| ITorrentDotNET
| ITransmission
| ITorrentstorm
| ITuoTu
| IuLeecher
| IuTorrent
| IVagaa
| IBitLet
| IFireTorrent
| IXunlei
| IXanTorrent
| IXtorrent
| IZipTorrent
deriving (Show, Eq, Ord)
parseImpl :: ByteString -> ClientImpl
parseImpl = f . BC.unpack
where
f "AG" = IAres
f "A~" = IAres
f "AR" = IArctic
f "AV" = IAvicora
f "AX" = IBitPump
f "AZ" = IAzureus
f "BB" = IBitBuddy
f "BC" = IBitComet
f "BF" = IBitflu
f "BG" = IBTG
f "BR" = IBitRocket
f "BS" = IBTSlave
f "BX" = IBittorrentX
f "CD" = IEnhancedCTorrent
f "CT" = ICTorrent
f "DE" = IDelugeTorrent
f "DP" = IPropagateDataClient
f "EB" = IEBit
f "ES" = IElectricSheep
f "FT" = IFoxTorrent
f "GS" = IGSTorrent
f "HL" = IHalite
f "HS" = IlibHSbittorrent
f "HN" = IHydranode
f "KG" = IKGet
f "KT" = IKTorrent
f "LH" = ILH_ABC
f "LP" = ILphant
f "LT" = ILibtorrent
f "lt" = ILibTorrent
f "LW" = ILimeWire
f "MO" = IMonoTorrent
f "MP" = IMooPolice
f "MR" = IMiro
f "MT" = IMoonlightTorrent
f "NX" = INetTransport
f "PD" = IPando
f "qB" = IqBittorrent
f "QD" = IQQDownload
f "QT" = IQt4TorrentExample
f "RT" = IRetriever
f "S~" = IShareaza
f "SB" = ISwiftbit
f "SS" = ISwarmScope
f "ST" = ISymTorrent
f "st" = Isharktorrent
f "SZ" = IShareaza
f "TN" = ITorrentDotNET
f "TR" = ITransmission
f "TS" = ITorrentstorm
f "TT" = ITuoTu
f "UL" = IuLeecher
f "UT" = IuTorrent
f "VG" = IVagaa
f "WT" = IBitLet
f "WY" = IFireTorrent
f "XL" = IXunlei
f "XT" = IXanTorrent
f "XX" = IXtorrent
f "ZT" = IZipTorrent
f _ = IUnknown
-- | Format client implementation info in human readable form.
ppClientImpl :: ClientImpl -> Doc
ppClientImpl = text . tail . show
-- | Used to represent not recognized implementation
unknownImpl :: ClientImpl
unknownImpl = IUnknown
-- TODO use Data.Version
-- | Raw version of client, normally extracted from peer id.
type ClientVersion = ByteString
-- | Format client implementation version in human readable form.
ppClientVersion :: ClientVersion -> Doc
ppClientVersion = text . BC.unpack
unknownVersion :: ClientVersion
unknownVersion = "0000"
-- | All useful infomation that can be obtained from a peer
-- identifier.
data ClientInfo = ClientInfo {
ciImpl :: ClientImpl
, ciVersion :: ClientVersion
} deriving (Show, Eq, Ord)
-- | Format client implementation in human readable form.
ppClientInfo :: ClientInfo -> Doc
ppClientInfo ClientInfo {..} =
ppClientImpl ciImpl <+> "version" <+> ppClientVersion ciVersion
-- | Unrecognized client implementation.
unknownClient :: ClientInfo
unknownClient = ClientInfo unknownImpl unknownVersion
-- | Tries to extract meaningful information from peer ID bytes. If
-- peer id uses unknown coding style then client info returned is
-- 'unknownClient'.
--
clientInfo :: PeerID -> ClientInfo
clientInfo pid = either (const unknownClient) id $
runGet getCI (getPeerID pid)
where -- TODO other styles
getCI = do
_ <- getWord8
ClientInfo <$> (parseImpl <$> getByteString 2) <*> getByteString 4
{-
-- code used for generation; remove it later on
mkEnumTyDef :: NM -> String
mkEnumTyDef = unlines . map (" | I" ++) . nub . map snd
mkPars :: NM -> String
mkPars = unlines . map (\(code, impl) -> " f \"" ++ code ++ "\" = " ++ "I" ++ impl)
type NM = [(String, String)]
nameMap :: NM
nameMap =
[ ("AG", "Ares")
, ("A~", "Ares")
, ("AR", "Arctic")
, ("AV", "Avicora")
, ("AX", "BitPump")
, ("AZ", "Azureus")
, ("BB", "BitBuddy")
, ("BC", "BitComet")
, ("BF", "Bitflu")
, ("BG", "BTG")
, ("BR", "BitRocket")
, ("BS", "BTSlave")
, ("BX", "BittorrentX")
, ("CD", "EnhancedCTorrent")
, ("CT", "CTorrent")
, ("DE", "DelugeTorrent")
, ("DP", "PropagateDataClient")
, ("EB", "EBit")
, ("ES", "ElectricSheep")
, ("FT", "FoxTorrent")
, ("GS", "GSTorrent")
, ("HL", "Halite")
, ("HS", "libHSnetwork_bittorrent")
, ("HN", "Hydranode")
, ("KG", "KGet")
, ("KT", "KTorrent")
, ("LH", "LH_ABC")
, ("LP", "Lphant")
, ("LT", "Libtorrent")
, ("lt", "LibTorrent")
, ("LW", "LimeWire")
, ("MO", "MonoTorrent")
, ("MP", "MooPolice")
, ("MR", "Miro")
, ("MT", "MoonlightTorrent")
, ("NX", "NetTransport")
, ("PD", "Pando")
, ("qB", "qBittorrent")
, ("QD", "QQDownload")
, ("QT", "Qt4TorrentExample")
, ("RT", "Retriever")
, ("S~", "Shareaza")
, ("SB", "Swiftbit")
, ("SS", "SwarmScope")
, ("ST", "SymTorrent")
, ("st", "sharktorrent")
, ("SZ", "Shareaza")
, ("TN", "TorrentDotNET")
, ("TR", "Transmission")
, ("TS", "Torrentstorm")
, ("TT", "TuoTu")
, ("UL", "uLeecher")
, ("UT", "uTorrent")
, ("VG", "Vagaa")
, ("WT", "BitLet")
, ("WY", "FireTorrent")
, ("XL", "Xunlei")
, ("XT", "XanTorrent")
, ("XX", "Xtorrent")
, ("ZT", "ZipTorrent")
]
-}
{-----------------------------------------------------------------------
Peer address
-----------------------------------------------------------------------}
-- | Peer address info normally extracted from peer list or peer
-- compact list encoding.
data PeerAddr = PeerAddr {
peerID :: Maybe PeerID
, peerIP :: HostAddress
, peerPort :: PortNumber
} deriving (Show, Eq, Ord)
-- TODO check semantic of ord and eq instances
instance BEncodable PortNumber where
toBEncode = toBEncode . fromEnum
fromBEncode b = toEnum <$> fromBEncode b
instance BEncodable PeerAddr where
toBEncode (PeerAddr pid pip pport) = fromAssocs
[ "peer id" -->? pid
, "ip" --> pip
, "port" --> pport
]
fromBEncode (BDict d) =
PeerAddr <$> d >--? "peer id"
<*> d >-- "ip"
<*> d >-- "port"
fromBEncode _ = decodingError "PeerAddr"
-- TODO make platform independent, clarify htonl
-- | Convert peer info from tracker response to socket address. Used
-- for establish connection between peers.
--
peerSockAddr :: PeerAddr -> SockAddr
peerSockAddr = SockAddrInet <$> (g . peerPort) <*> (htonl . peerIP)
where
htonl :: Word32 -> Word32
htonl d =
((d .&. 0xff) `shiftL` 24) .|.
(((d `shiftR` 8 ) .&. 0xff) `shiftL` 16) .|.
(((d `shiftR` 16) .&. 0xff) `shiftL` 8) .|.
((d `shiftR` 24) .&. 0xff)
g :: PortNumber -> PortNumber
g = id
-- | Tries to connect to peer using reasonable default parameters.
connectToPeer :: PeerAddr -> IO Socket
connectToPeer p = do
sock <- socket AF_INET Stream Network.Socket.defaultProtocol
connect sock (peerSockAddr p)
return sock
forkListener :: ((PeerAddr, Socket) -> IO ()) -> IO PortNumber
forkListener action = do
sock <- socket AF_INET Stream defaultProtocol
bindSocket sock (SockAddrInet 0 0)
listen sock 1
addr <- getSocketName sock
case addr of
SockAddrInet port _ -> do
forkIO (loop sock)
return port
_ -> do
throwIO $ userError "listener: impossible happened"
where
loop sock = do
(conn, addr) <- accept sock
case addr of
SockAddrInet port host ->
action (PeerAddr Nothing host port, conn)
_ -> return ()
loop sock
-- | Pretty print peer address in human readable form.
ppPeer :: PeerAddr -> Doc
ppPeer p @ PeerAddr {..} = case peerID of
Just pid -> ppClientInfo (clientInfo pid) <+> "at" <+> paddr
Nothing -> paddr
where
paddr = text (show (peerSockAddr p))
|