diff options
author | Sam T <sta.cs.vsu@gmail.com> | 2013-04-17 11:13:44 +0400 |
---|---|---|
committer | Sam T <sta.cs.vsu@gmail.com> | 2013-04-17 11:13:44 +0400 |
commit | e8dd39b3c49e38a8ec7d0e1b474bf8e8170ec51c (patch) | |
tree | 57d48248c1b5809ed8fa90af29ab77008e2d9724 | |
parent | b2934baabfc7568fcfa6be5a92202c86f1a3cdc0 (diff) |
+ Add handshaking.
-rw-r--r-- | src/Network/Torrent/PeerWire/Handshake.hs | 23 |
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 @@ | |||
2 | module Network.Torrent.PeerWire.Handshake | 2 | module 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 | ||
8 | import Control.Applicative | 9 | import Control.Applicative |
9 | import Data.Word | 10 | import Data.Word |
10 | import Data.ByteString (ByteString) | 11 | import Data.ByteString (ByteString) |
11 | import qualified Data.ByteString as B | 12 | import qualified Data.ByteString as B |
12 | import Data.Serialize | 13 | import Data.Serialize as S |
13 | import Data.Torrent.InfoHash | 14 | import Data.Torrent.InfoHash |
15 | import Network | ||
16 | import Network.Socket.ByteString | ||
14 | import Network.Torrent.PeerID | 17 | import 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 | |||
45 | handshakeMaxSize = 1 + 256 + 8 + 20 + 20 | 48 | handshakeMaxSize = 1 + 256 + 8 + 20 + 20 |
46 | 49 | ||
47 | -- | Default protocol string "BitTorrent protocol" as is. | 50 | -- | Default protocol string "BitTorrent protocol" as is. |
48 | defaultProtocol :: ByteString | 51 | defaultBTProtocol :: ByteString |
49 | defaultProtocol = "BitTorrent protocol" | 52 | defaultBTProtocol = "BitTorrent protocol" |
50 | 53 | ||
51 | -- | Default reserved word is 0. | 54 | -- | Default reserved word is 0. |
52 | defaultReserved :: Word64 | 55 | defaultReserved :: 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. |
56 | defaultHandshake :: InfoHash -> PeerID -> Handshake | 59 | defaultHandshake :: InfoHash -> PeerID -> Handshake |
57 | defaultHandshake = Handshake defaultProtocol defaultReserved \ No newline at end of file | 60 | defaultHandshake = Handshake defaultBTProtocol defaultReserved |
61 | |||
62 | |||
63 | -- TODO check if hash the same | ||
64 | -- | Handshaking with a peer specified by the second argument. | ||
65 | -- | ||
66 | handshake :: Socket -> Handshake -> IO (Either String Handshake) | ||
67 | handshake sock hs = do | ||
68 | sendAll sock (S.encode hs) | ||
69 | r <- recv sock handshakeMaxSize | ||
70 | return (S.decode r) | ||