summaryrefslogtreecommitdiff
path: root/src/Network/Torrent/PeerWire/Handshake.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/Torrent/PeerWire/Handshake.hs')
-rw-r--r--src/Network/Torrent/PeerWire/Handshake.hs77
1 files changed, 0 insertions, 77 deletions
diff --git a/src/Network/Torrent/PeerWire/Handshake.hs b/src/Network/Torrent/PeerWire/Handshake.hs
deleted file mode 100644
index 733d5785..00000000
--- a/src/Network/Torrent/PeerWire/Handshake.hs
+++ /dev/null
@@ -1,77 +0,0 @@
1-- |
2-- Copyright : (c) Sam T. 2013
3-- License : MIT
4-- Maintainer : pxqr.sta@gmail.com
5-- Stability : experimental
6-- Portability : portable
7--
8{-# LANGUAGE OverloadedStrings #-}
9module Network.Torrent.PeerWire.Handshake
10 ( Handshake
11 , handshakeMaxSize
12 , defaultBTProtocol, defaultReserved, defaultHandshake
13 , handshake
14 ) where
15
16import Control.Applicative
17import Data.Word
18import Data.ByteString (ByteString)
19import qualified Data.ByteString as B
20import Data.Serialize as S
21import Data.Torrent.InfoHash
22import Network
23import Network.Socket.ByteString
24import Network.Torrent.PeerID
25
26-- | In order to establish the connection between peers we should send 'Handshake'
27-- message. The 'Handshake' is a required message and must be the first message
28-- transmitted by the peer to the another peer.
29data Handshake = Handshake {
30 hsProtocol :: ByteString -- ^ Identifier of the protocol.
31 , hsReserved :: Word64 -- ^ Reserved bytes, rarely used.
32 , hsInfoHash :: InfoHash -- ^ Hash from the metainfo file.
33 -- This /should be/ same hash that is transmitted in tracker requests.
34 , hsPeerID :: PeerID -- ^ Peer id of the initiator.
35 -- This is /usually the same peer id that is transmitted in tracker requests.
36 } deriving (Show, Eq)
37
38instance Serialize Handshake where
39 put hs = do
40 putWord8 (fromIntegral (B.length (hsProtocol hs)))
41 putByteString (hsProtocol hs)
42 putWord64be (hsReserved hs)
43 put (hsInfoHash hs)
44 put (hsPeerID hs)
45
46 get = do
47 len <- getWord8
48 Handshake <$> getBytes (fromIntegral len)
49 <*> getWord64be
50 <*> get
51 <*> get
52
53-- | Maximum size of handshake message in bytes.
54handshakeMaxSize :: Int
55handshakeMaxSize = 1 + 256 + 8 + 20 + 20
56
57-- | Default protocol string "BitTorrent protocol" as is.
58defaultBTProtocol :: ByteString
59defaultBTProtocol = "BitTorrent protocol"
60
61-- | Default reserved word is 0.
62defaultReserved :: Word64
63defaultReserved = 0
64
65-- | Length of info hash and peer id is unchecked, so it /should/ be equal 20.
66defaultHandshake :: InfoHash -> PeerID -> Handshake
67defaultHandshake = Handshake defaultBTProtocol defaultReserved
68
69
70-- TODO check if hash the same
71-- | Handshaking with a peer specified by the second argument.
72--
73handshake :: Socket -> Handshake -> IO (Either String Handshake)
74handshake sock hs = do
75 sendAll sock (S.encode hs)
76 r <- recv sock handshakeMaxSize
77 return (S.decode r)