diff options
author | Sam T <sta.cs.vsu@gmail.com> | 2013-04-08 02:59:45 +0400 |
---|---|---|
committer | Sam T <sta.cs.vsu@gmail.com> | 2013-04-08 02:59:45 +0400 |
commit | d6c53fd0af4c395459f89a4e3fa9c5988c27ec32 (patch) | |
tree | b291b42f9248d3e96cced7185eabf2e1be6dcd1a /src/Network/Torrent | |
parent | 4296031c4c1b004ec2271c0028ab603c3f6c4e8a (diff) |
+ mk newtype for info hash
Diffstat (limited to 'src/Network/Torrent')
-rw-r--r-- | src/Network/Torrent/PeerWire/Handshake.hs | 12 | ||||
-rw-r--r-- | src/Network/Torrent/Tracker.hs | 9 |
2 files changed, 9 insertions, 12 deletions
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 | |||
9 | import Data.ByteString (ByteString) | 9 | import Data.ByteString (ByteString) |
10 | import qualified Data.ByteString as B | 10 | import qualified Data.ByteString as B |
11 | import Data.Serialize | 11 | import Data.Serialize |
12 | 12 | import Data.Torrent.InfoHash | |
13 | import Network.Torrent.PeerID | 13 | import Network.Torrent.PeerID |
14 | 14 | ||
15 | -- | In order to establish the connection between peers we should send 'Handshake' | 15 | -- | In order to establish the connection between peers we should send 'Handshake' |
@@ -18,7 +18,7 @@ import Network.Torrent.PeerID | |||
18 | data Handshake = Handshake { | 18 | data Handshake = Handshake { |
19 | hsProtocol :: ByteString -- ^ Identifier of the protocol. | 19 | hsProtocol :: ByteString -- ^ Identifier of the protocol. |
20 | , hsReserved :: Word64 -- ^ Reserved bytes, rarely used. | 20 | , hsReserved :: Word64 -- ^ Reserved bytes, rarely used. |
21 | , hsInfoHash :: ByteString -- ^ Hash from the metainfo file. | 21 | , hsInfoHash :: InfoHash -- ^ Hash from the metainfo file. |
22 | -- This /should be/ same hash that is transmitted in tracker requests. | 22 | -- This /should be/ same hash that is transmitted in tracker requests. |
23 | , hsPeerID :: PeerID -- ^ Peer id of the initiator. | 23 | , hsPeerID :: PeerID -- ^ Peer id of the initiator. |
24 | -- This is /usually the same peer id that is transmitted in tracker requests. | 24 | -- This is /usually the same peer id that is transmitted in tracker requests. |
@@ -29,14 +29,14 @@ instance Serialize Handshake where | |||
29 | putWord8 (fromIntegral (B.length (hsProtocol hs))) | 29 | putWord8 (fromIntegral (B.length (hsProtocol hs))) |
30 | putByteString (hsProtocol hs) | 30 | putByteString (hsProtocol hs) |
31 | putWord64be (hsReserved hs) | 31 | putWord64be (hsReserved hs) |
32 | putByteString (hsInfoHash hs) | 32 | put (hsInfoHash hs) |
33 | put (hsPeerID hs) | 33 | put (hsPeerID hs) |
34 | 34 | ||
35 | get = do | 35 | get = do |
36 | len <- getWord8 | 36 | len <- getWord8 |
37 | Handshake <$> getBytes (fromIntegral len) | 37 | Handshake <$> getBytes (fromIntegral len) |
38 | <*> getWord64be | 38 | <*> getWord64be |
39 | <*> getBytes 20 | 39 | <*> get |
40 | <*> get | 40 | <*> get |
41 | 41 | ||
42 | -- | Default protocol string "BitTorrent protocol" as is. | 42 | -- | Default protocol string "BitTorrent protocol" as is. |
@@ -48,7 +48,5 @@ defaultReserved :: Word64 | |||
48 | defaultReserved = 0 | 48 | defaultReserved = 0 |
49 | 49 | ||
50 | -- | Length of info hash and peer id is unchecked, so it /should/ be equal 20. | 50 | -- | Length of info hash and peer id is unchecked, so it /should/ be equal 20. |
51 | defaultHandshake :: ByteString -- ^ Info hash string. | 51 | defaultHandshake :: InfoHash -> PeerID -> Handshake |
52 | -> PeerID | ||
53 | -> Handshake | ||
54 | defaultHandshake hash pid = Handshake defaultProtocol defaultReserved hash pid \ No newline at end of file | 52 | 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 | |||
22 | import Data.Text as T | 22 | import Data.Text as T |
23 | import Data.Serialize.Get hiding (Result) | 23 | import Data.Serialize.Get hiding (Result) |
24 | import Data.URLEncoded as URL | 24 | import Data.URLEncoded as URL |
25 | import Data.Torrent | ||
25 | 26 | ||
26 | import Network | 27 | import Network |
27 | import Network.Socket | 28 | import Network.Socket |
@@ -31,8 +32,6 @@ import Network.Torrent.PeerID | |||
31 | 32 | ||
32 | import Numeric | 33 | import Numeric |
33 | 34 | ||
34 | type Hash = ByteString | ||
35 | |||
36 | data Peer = Peer { | 35 | data Peer = Peer { |
37 | peerID :: Maybe PeerID | 36 | peerID :: Maybe PeerID |
38 | , peerIP :: HostAddress | 37 | , peerIP :: HostAddress |
@@ -46,7 +45,7 @@ data Event = Started -- ^ For first request. | |||
46 | 45 | ||
47 | data TRequest = TRequest { -- TODO peer here -- TODO detach announce | 46 | data TRequest = TRequest { -- TODO peer here -- TODO detach announce |
48 | reqAnnounce :: URI -- ^ Announce url of the torrent. | 47 | reqAnnounce :: URI -- ^ Announce url of the torrent. |
49 | , reqInfoHash :: Hash -- ^ Hash of info part of the torrent. | 48 | , reqInfoHash :: InfoHash -- ^ Hash of info part of the torrent. |
50 | , reqPeerID :: PeerID -- ^ Id of the peer doing request. () | 49 | , reqPeerID :: PeerID -- ^ Id of the peer doing request. () |
51 | , reqPort :: PortNumber -- ^ Port to listen to for connection from other peers. | 50 | , reqPort :: PortNumber -- ^ Port to listen to for connection from other peers. |
52 | , reqUploaded :: Int -- ^ # of bytes that the peer has uploaded in the swarm. | 51 | , reqUploaded :: Int -- ^ # of bytes that the peer has uploaded in the swarm. |
@@ -155,7 +154,7 @@ instance URLEncode TRequest where | |||
155 | 154 | ||
156 | encodeRequest :: TRequest -> URI | 155 | encodeRequest :: TRequest -> URI |
157 | encodeRequest req = URL.urlEncode req `addToURI` reqAnnounce req | 156 | encodeRequest req = URL.urlEncode req `addToURI` reqAnnounce req |
158 | `addHash` BC.unpack (reqInfoHash req) | 157 | `addHash` BC.unpack (getInfoHash (reqInfoHash req)) |
159 | where | 158 | where |
160 | addHash :: URI -> String -> URI | 159 | addHash :: URI -> String -> URI |
161 | addHash uri s = uri { uriQuery = uriQuery uri ++ "&info_hash=" ++ rfc1738Encode s } | 160 | addHash uri s = uri { uriQuery = uriQuery uri ++ "&info_hash=" ++ rfc1738Encode s } |
@@ -176,7 +175,7 @@ encodeRequest req = URL.urlEncode req `addToURI` reqAnnounce req | |||
176 | defaultPorts :: [PortNumber] | 175 | defaultPorts :: [PortNumber] |
177 | defaultPorts = [6881..6889] | 176 | defaultPorts = [6881..6889] |
178 | 177 | ||
179 | defaultRequest :: URI -> Hash -> PeerID -> TRequest | 178 | defaultRequest :: URI -> InfoHash -> PeerID -> TRequest |
180 | defaultRequest announce hash pid = | 179 | defaultRequest announce hash pid = |
181 | TRequest { | 180 | TRequest { |
182 | reqAnnounce = announce | 181 | reqAnnounce = announce |