From d6c53fd0af4c395459f89a4e3fa9c5988c27ec32 Mon Sep 17 00:00:00 2001 From: Sam T Date: Mon, 8 Apr 2013 02:59:45 +0400 Subject: + mk newtype for info hash --- src/Network/Torrent/PeerWire/Handshake.hs | 12 +++++------- src/Network/Torrent/Tracker.hs | 9 ++++----- 2 files changed, 9 insertions(+), 12 deletions(-) (limited to 'src/Network/Torrent') diff --git a/src/Network/Torrent/PeerWire/Handshake.hs b/src/Network/Torrent/PeerWire/Handshake.hs index 7bdb7fef..ab255c14 100644 --- a/src/Network/Torrent/PeerWire/Handshake.hs +++ b/src/Network/Torrent/PeerWire/Handshake.hs @@ -9,7 +9,7 @@ import Data.Word import Data.ByteString (ByteString) import qualified Data.ByteString as B import Data.Serialize - +import Data.Torrent.InfoHash import Network.Torrent.PeerID -- | In order to establish the connection between peers we should send 'Handshake' @@ -18,7 +18,7 @@ import Network.Torrent.PeerID data Handshake = Handshake { hsProtocol :: ByteString -- ^ Identifier of the protocol. , hsReserved :: Word64 -- ^ Reserved bytes, rarely used. - , hsInfoHash :: ByteString -- ^ Hash from the metainfo file. + , hsInfoHash :: InfoHash -- ^ 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. @@ -29,14 +29,14 @@ instance Serialize Handshake where putWord8 (fromIntegral (B.length (hsProtocol hs))) putByteString (hsProtocol hs) putWord64be (hsReserved hs) - putByteString (hsInfoHash hs) + put (hsInfoHash hs) put (hsPeerID hs) get = do len <- getWord8 Handshake <$> getBytes (fromIntegral len) <*> getWord64be - <*> getBytes 20 + <*> get <*> get -- | Default protocol string "BitTorrent protocol" as is. @@ -48,7 +48,5 @@ 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 :: InfoHash -> PeerID -> Handshake defaultHandshake hash pid = Handshake defaultProtocol defaultReserved hash pid \ No newline at end of file diff --git a/src/Network/Torrent/Tracker.hs b/src/Network/Torrent/Tracker.hs index 72fbcb44..796db66a 100644 --- a/src/Network/Torrent/Tracker.hs +++ b/src/Network/Torrent/Tracker.hs @@ -22,6 +22,7 @@ import Data.ByteString.Char8 as BC import Data.Text as T import Data.Serialize.Get hiding (Result) import Data.URLEncoded as URL +import Data.Torrent import Network import Network.Socket @@ -31,8 +32,6 @@ import Network.Torrent.PeerID import Numeric -type Hash = ByteString - data Peer = Peer { peerID :: Maybe PeerID , peerIP :: HostAddress @@ -46,7 +45,7 @@ data Event = Started -- ^ For first request. data TRequest = TRequest { -- TODO peer here -- TODO detach announce reqAnnounce :: URI -- ^ Announce url of the torrent. - , reqInfoHash :: Hash -- ^ Hash of info part of the torrent. + , reqInfoHash :: InfoHash -- ^ Hash of info part of the torrent. , reqPeerID :: PeerID -- ^ Id of the peer doing request. () , reqPort :: PortNumber -- ^ Port to listen to for connection from other peers. , reqUploaded :: Int -- ^ # of bytes that the peer has uploaded in the swarm. @@ -155,7 +154,7 @@ instance URLEncode TRequest where encodeRequest :: TRequest -> URI encodeRequest req = URL.urlEncode req `addToURI` reqAnnounce req - `addHash` BC.unpack (reqInfoHash req) + `addHash` BC.unpack (getInfoHash (reqInfoHash req)) where addHash :: URI -> String -> URI addHash uri s = uri { uriQuery = uriQuery uri ++ "&info_hash=" ++ rfc1738Encode s } @@ -176,7 +175,7 @@ encodeRequest req = URL.urlEncode req `addToURI` reqAnnounce req defaultPorts :: [PortNumber] defaultPorts = [6881..6889] -defaultRequest :: URI -> Hash -> PeerID -> TRequest +defaultRequest :: URI -> InfoHash -> PeerID -> TRequest defaultRequest announce hash pid = TRequest { reqAnnounce = announce -- cgit v1.2.3