summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Network/Torrent/PeerWire/Handshake.hs23
1 files changed, 18 insertions, 5 deletions
diff --git a/src/Network/Torrent/PeerWire/Handshake.hs b/src/Network/Torrent/PeerWire/Handshake.hs
index ae65933f..8177fe41 100644
--- a/src/Network/Torrent/PeerWire/Handshake.hs
+++ b/src/Network/Torrent/PeerWire/Handshake.hs
@@ -2,15 +2,18 @@
2module Network.Torrent.PeerWire.Handshake 2module Network.Torrent.PeerWire.Handshake
3 ( Handshake 3 ( Handshake
4 , handshakeMaxSize 4 , handshakeMaxSize
5 , defaultProtocol, defaultReserved, defaultHandshake 5 , defaultBTProtocol, defaultReserved, defaultHandshake
6 , handshake
6 ) where 7 ) where
7 8
8import Control.Applicative 9import Control.Applicative
9import Data.Word 10import Data.Word
10import Data.ByteString (ByteString) 11import Data.ByteString (ByteString)
11import qualified Data.ByteString as B 12import qualified Data.ByteString as B
12import Data.Serialize 13import Data.Serialize as S
13import Data.Torrent.InfoHash 14import Data.Torrent.InfoHash
15import Network
16import Network.Socket.ByteString
14import Network.Torrent.PeerID 17import Network.Torrent.PeerID
15 18
16-- | In order to establish the connection between peers we should send 'Handshake' 19-- | In order to establish the connection between peers we should send 'Handshake'
@@ -45,8 +48,8 @@ handshakeMaxSize :: Int
45handshakeMaxSize = 1 + 256 + 8 + 20 + 20 48handshakeMaxSize = 1 + 256 + 8 + 20 + 20
46 49
47-- | Default protocol string "BitTorrent protocol" as is. 50-- | Default protocol string "BitTorrent protocol" as is.
48defaultProtocol :: ByteString 51defaultBTProtocol :: ByteString
49defaultProtocol = "BitTorrent protocol" 52defaultBTProtocol = "BitTorrent protocol"
50 53
51-- | Default reserved word is 0. 54-- | Default reserved word is 0.
52defaultReserved :: Word64 55defaultReserved :: Word64
@@ -54,4 +57,14 @@ defaultReserved = 0
54 57
55-- | Length of info hash and peer id is unchecked, so it /should/ be equal 20. 58-- | Length of info hash and peer id is unchecked, so it /should/ be equal 20.
56defaultHandshake :: InfoHash -> PeerID -> Handshake 59defaultHandshake :: InfoHash -> PeerID -> Handshake
57defaultHandshake = Handshake defaultProtocol defaultReserved \ No newline at end of file 60defaultHandshake = Handshake defaultBTProtocol defaultReserved
61
62
63-- TODO check if hash the same
64-- | Handshaking with a peer specified by the second argument.
65--
66handshake :: Socket -> Handshake -> IO (Either String Handshake)
67handshake sock hs = do
68 sendAll sock (S.encode hs)
69 r <- recv sock handshakeMaxSize
70 return (S.decode r)