summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/PeerWire/Handshake.hs
diff options
context:
space:
mode:
authorSam T <sta.cs.vsu@gmail.com>2013-04-24 23:52:42 +0400
committerSam T <sta.cs.vsu@gmail.com>2013-04-24 23:52:42 +0400
commit82bea85b66304a550df074e700078c0c0c5d602a (patch)
treedff862bd03f730285c81b1cabbc6e4f647112b78 /src/Network/BitTorrent/PeerWire/Handshake.hs
parenta3b7d8442feaec251c38609712d24aea208becbb (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.hs40
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
16import Control.Applicative 17import Control.Applicative
17import Data.Word 18import Data.Word
18import Data.ByteString (ByteString) 19import Data.ByteString (ByteString)
19import qualified Data.ByteString as B 20import qualified Data.ByteString as B
21import qualified Data.ByteString.Char8 as BC
20import Data.Serialize as S 22import Data.Serialize as S
21import Data.Torrent.InfoHash 23import Data.Torrent.InfoHash
22import Network 24import Network
23import Network.Socket.ByteString 25import Network.Socket.ByteString
24 26
25import Network.BitTorrent.PeerID 27import Network.BitTorrent.PeerID
28import 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--
31data Handshake = Handshake { 36data 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
40instance Serialize Handshake where 57instance 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
73ppHandshake :: Handshake -> String
74ppHandshake 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.
56handshakeMaxSize :: Int 78handshakeMaxSize :: Int
57handshakeMaxSize = 1 + 256 + 8 + 20 + 20 79handshakeMaxSize = 1 + 256 + 8 + 20 + 20