From 4296031c4c1b004ec2271c0028ab603c3f6c4e8a Mon Sep 17 00:00:00 2001 From: Sam T Date: Sun, 7 Apr 2013 21:48:28 +0400 Subject: move handshake to PeerWire --- src/Network/Torrent.hs | 2 -- src/Network/Torrent/Handshake.hs | 54 ------------------------------- src/Network/Torrent/PeerWire.hs | 5 ++- src/Network/Torrent/PeerWire/Handshake.hs | 54 +++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 57 deletions(-) delete mode 100644 src/Network/Torrent/Handshake.hs create mode 100644 src/Network/Torrent/PeerWire/Handshake.hs (limited to 'src') diff --git a/src/Network/Torrent.hs b/src/Network/Torrent.hs index a9194d3e..a37bee41 100644 --- a/src/Network/Torrent.hs +++ b/src/Network/Torrent.hs @@ -2,10 +2,8 @@ module Network.Torrent ( module Network.Torrent.PeerID , module Network.Torrent.Tracker , module Network.Torrent.PeerWire - , module Network.Torrent.Handshake ) where import Network.Torrent.PeerID import Network.Torrent.Tracker import Network.Torrent.PeerWire -import Network.Torrent.Handshake diff --git a/src/Network/Torrent/Handshake.hs b/src/Network/Torrent/Handshake.hs deleted file mode 100644 index c14fd591..00000000 --- a/src/Network/Torrent/Handshake.hs +++ /dev/null @@ -1,54 +0,0 @@ -{-# LANGUAGE OverloadedStrings #-} -module Network.Torrent.Handshake - ( Handshake - , defaultProtocol, defaultReserved, defaultHandshake - ) where - -import Control.Applicative -import Data.Word -import Data.ByteString (ByteString) -import qualified Data.ByteString as B -import Data.Serialize - -import Network.Torrent.PeerID - --- | In order to establish the connection between peers we should send 'Handshake' --- message. The 'Handshake' is a required message and must be the first message --- transmitted by the peer to the another peer. -data Handshake = Handshake { - hsProtocol :: ByteString -- ^ Identifier of the protocol. - , hsReserved :: Word64 -- ^ Reserved bytes, rarely used. - , hsInfoHash :: ByteString -- ^ Hash from the metainfo file. - -- This /should be/ same hash that is transmitted in tracker requests. - , hsPeerID :: PeerID -- ^ Peer id of the initiator. - -- This is /usually the same peer id that is transmitted in tracker requests. - } deriving (Show, Eq) - -instance Serialize Handshake where - put hs = do - putWord8 (fromIntegral (B.length (hsProtocol hs))) - putByteString (hsProtocol hs) - putWord64be (hsReserved hs) - putByteString (hsInfoHash hs) - put (hsPeerID hs) - - get = do - len <- getWord8 - Handshake <$> getBytes (fromIntegral len) - <*> getWord64be - <*> getBytes 20 - <*> get - --- | Default protocol string "BitTorrent protocol" as is. -defaultProtocol :: ByteString -defaultProtocol = "BitTorrent protocol" - --- | Default reserved word is 0. -defaultReserved :: Word64 -defaultReserved = 0 - --- | Length of info hash and peer id is unchecked, so it /should/ be equal 20. -defaultHandshake :: ByteString -- ^ Info hash string. - -> PeerID - -> Handshake -defaultHandshake hash pid = Handshake defaultProtocol defaultReserved hash pid \ No newline at end of file diff --git a/src/Network/Torrent/PeerWire.hs b/src/Network/Torrent/PeerWire.hs index b1fae9f2..27ecda79 100644 --- a/src/Network/Torrent/PeerWire.hs +++ b/src/Network/Torrent/PeerWire.hs @@ -1,8 +1,11 @@ {-# LANGUAGE DoAndIfThenElse #-} module Network.Torrent.PeerWire - ( Message(..), Block(..), BlockIx(..), + ( module Network.Torrent.PeerWire.Handshake + , Message(..), Block(..), BlockIx(..), ) where +import Network.Torrent.PeerWire.Handshake + import Control.Applicative import Data.ByteString (ByteString) import qualified Data.ByteString as B diff --git a/src/Network/Torrent/PeerWire/Handshake.hs b/src/Network/Torrent/PeerWire/Handshake.hs new file mode 100644 index 00000000..7bdb7fef --- /dev/null +++ b/src/Network/Torrent/PeerWire/Handshake.hs @@ -0,0 +1,54 @@ +{-# LANGUAGE OverloadedStrings #-} +module Network.Torrent.PeerWire.Handshake + ( Handshake + , defaultProtocol, defaultReserved, defaultHandshake + ) where + +import Control.Applicative +import Data.Word +import Data.ByteString (ByteString) +import qualified Data.ByteString as B +import Data.Serialize + +import Network.Torrent.PeerID + +-- | In order to establish the connection between peers we should send 'Handshake' +-- message. The 'Handshake' is a required message and must be the first message +-- transmitted by the peer to the another peer. +data Handshake = Handshake { + hsProtocol :: ByteString -- ^ Identifier of the protocol. + , hsReserved :: Word64 -- ^ Reserved bytes, rarely used. + , hsInfoHash :: ByteString -- ^ Hash from the metainfo file. + -- This /should be/ same hash that is transmitted in tracker requests. + , hsPeerID :: PeerID -- ^ Peer id of the initiator. + -- This is /usually the same peer id that is transmitted in tracker requests. + } deriving (Show, Eq) + +instance Serialize Handshake where + put hs = do + putWord8 (fromIntegral (B.length (hsProtocol hs))) + putByteString (hsProtocol hs) + putWord64be (hsReserved hs) + putByteString (hsInfoHash hs) + put (hsPeerID hs) + + get = do + len <- getWord8 + Handshake <$> getBytes (fromIntegral len) + <*> getWord64be + <*> getBytes 20 + <*> get + +-- | Default protocol string "BitTorrent protocol" as is. +defaultProtocol :: ByteString +defaultProtocol = "BitTorrent protocol" + +-- | Default reserved word is 0. +defaultReserved :: Word64 +defaultReserved = 0 + +-- | Length of info hash and peer id is unchecked, so it /should/ be equal 20. +defaultHandshake :: ByteString -- ^ Info hash string. + -> PeerID + -> Handshake +defaultHandshake hash pid = Handshake defaultProtocol defaultReserved hash pid \ No newline at end of file -- cgit v1.2.3