diff options
author | Sam T <sta.cs.vsu@gmail.com> | 2013-04-24 23:52:42 +0400 |
---|---|---|
committer | Sam T <sta.cs.vsu@gmail.com> | 2013-04-24 23:52:42 +0400 |
commit | 82bea85b66304a550df074e700078c0c0c5d602a (patch) | |
tree | dff862bd03f730285c81b1cabbc6e4f647112b78 /src/Network/BitTorrent/PeerWire/Handshake.hs | |
parent | a3b7d8442feaec251c38609712d24aea208becbb (diff) |
+ Add handshake pprint.
Move Peer to separated module since otherwise we have recursive
module dependencies.
Diffstat (limited to 'src/Network/BitTorrent/PeerWire/Handshake.hs')
-rw-r--r-- | src/Network/BitTorrent/PeerWire/Handshake.hs | 40 |
1 files changed, 31 insertions, 9 deletions
diff --git a/src/Network/BitTorrent/PeerWire/Handshake.hs b/src/Network/BitTorrent/PeerWire/Handshake.hs index 6ce37887..a80728aa 100644 --- a/src/Network/BitTorrent/PeerWire/Handshake.hs +++ b/src/Network/BitTorrent/PeerWire/Handshake.hs | |||
@@ -11,30 +11,47 @@ module Network.BitTorrent.PeerWire.Handshake | |||
11 | , handshakeMaxSize | 11 | , handshakeMaxSize |
12 | , defaultBTProtocol, defaultReserved, defaultHandshake | 12 | , defaultBTProtocol, defaultReserved, defaultHandshake |
13 | , handshake | 13 | , handshake |
14 | , ppHandshake | ||
14 | ) where | 15 | ) where |
15 | 16 | ||
16 | import Control.Applicative | 17 | import Control.Applicative |
17 | import Data.Word | 18 | import Data.Word |
18 | import Data.ByteString (ByteString) | 19 | import Data.ByteString (ByteString) |
19 | import qualified Data.ByteString as B | 20 | import qualified Data.ByteString as B |
21 | import qualified Data.ByteString.Char8 as BC | ||
20 | import Data.Serialize as S | 22 | import Data.Serialize as S |
21 | import Data.Torrent.InfoHash | 23 | import Data.Torrent.InfoHash |
22 | import Network | 24 | import Network |
23 | import Network.Socket.ByteString | 25 | import Network.Socket.ByteString |
24 | 26 | ||
25 | import Network.BitTorrent.PeerID | 27 | import Network.BitTorrent.PeerID |
28 | import Network.BitTorrent.PeerWire.ClientInfo | ||
26 | 29 | ||
27 | 30 | ||
28 | -- | In order to establish the connection between peers we should send 'Handshake' | 31 | -- | In order to establish the connection between peers we should send |
29 | -- message. The 'Handshake' is a required message and must be the first message | 32 | -- 'Handshake' message. The 'Handshake' is a required message and |
30 | -- transmitted by the peer to the another peer. | 33 | -- must be the first message transmitted by the peer to the another |
34 | -- peer. | ||
35 | -- | ||
31 | data Handshake = Handshake { | 36 | data Handshake = Handshake { |
32 | hsProtocol :: ByteString -- ^ Identifier of the protocol. | 37 | -- ^ Identifier of the protocol. |
33 | , hsReserved :: Word64 -- ^ Reserved bytes, rarely used. | 38 | hsProtocol :: ByteString |
34 | , hsInfoHash :: InfoHash -- ^ Hash from the metainfo file. | 39 | |
35 | -- This /should be/ same hash that is transmitted in tracker requests. | 40 | -- ^ Reserved bytes used to specify supported BEP's. |
36 | , hsPeerID :: PeerID -- ^ Peer id of the initiator. | 41 | , hsReserved :: Word64 |
37 | -- This is /usually the same peer id that is transmitted in tracker requests. | 42 | |
43 | -- ^ Info hash of the info part of the metainfo file. that is | ||
44 | -- transmitted in tracker requests. Info hash of the initiator | ||
45 | -- handshake and response handshake should match, otherwise | ||
46 | -- initiator should break the connection. | ||
47 | -- | ||
48 | , hsInfoHash :: InfoHash | ||
49 | |||
50 | -- ^ Peer id of the initiator. This is usually the same peer id | ||
51 | -- that is transmitted in tracker requests. | ||
52 | -- | ||
53 | , hsPeerID :: PeerID | ||
54 | |||
38 | } deriving (Show, Eq) | 55 | } deriving (Show, Eq) |
39 | 56 | ||
40 | instance Serialize Handshake where | 57 | instance Serialize Handshake where |
@@ -52,6 +69,11 @@ instance Serialize Handshake where | |||
52 | <*> get | 69 | <*> get |
53 | <*> get | 70 | <*> get |
54 | 71 | ||
72 | -- TODO add reserved bits info | ||
73 | ppHandshake :: Handshake -> String | ||
74 | ppHandshake hs = BC.unpack (hsProtocol hs) ++ " " | ||
75 | ++ ppClientInfo (clientInfo (hsPeerID hs)) | ||
76 | |||
55 | -- | Maximum size of handshake message in bytes. | 77 | -- | Maximum size of handshake message in bytes. |
56 | handshakeMaxSize :: Int | 78 | handshakeMaxSize :: Int |
57 | handshakeMaxSize = 1 + 256 + 8 + 20 + 20 | 79 | handshakeMaxSize = 1 + 256 + 8 + 20 + 20 |