diff options
-rw-r--r-- | src/Network/BitTorrent/Exchange/Message.hs | 38 | ||||
-rw-r--r-- | src/Network/BitTorrent/Exchange/Wire.hs | 2 | ||||
-rw-r--r-- | tests/Network/BitTorrent/Exchange/MessageSpec.hs | 12 |
3 files changed, 25 insertions, 27 deletions
diff --git a/src/Network/BitTorrent/Exchange/Message.hs b/src/Network/BitTorrent/Exchange/Message.hs index b3100269..070a0e42 100644 --- a/src/Network/BitTorrent/Exchange/Message.hs +++ b/src/Network/BitTorrent/Exchange/Message.hs | |||
@@ -40,7 +40,7 @@ module Network.BitTorrent.Exchange.Message | |||
40 | , Caps | 40 | , Caps |
41 | 41 | ||
42 | -- * Handshake | 42 | -- * Handshake |
43 | , ProtocolString | 43 | , ProtocolName |
44 | , Handshake(..) | 44 | , Handshake(..) |
45 | , defaultHandshake | 45 | , defaultHandshake |
46 | , handshakeSize | 46 | , handshakeSize |
@@ -208,48 +208,48 @@ instance Capabilities Caps where | |||
208 | Handshake | 208 | Handshake |
209 | -----------------------------------------------------------------------} | 209 | -----------------------------------------------------------------------} |
210 | 210 | ||
211 | maxProtocolStringSize :: Word8 | 211 | maxProtocolNameSize :: Word8 |
212 | maxProtocolStringSize = maxBound | 212 | maxProtocolNameSize = maxBound |
213 | 213 | ||
214 | -- | The protocol name is used to identify to the local peer which | 214 | -- | The protocol name is used to identify to the local peer which |
215 | -- version of BTP the remote peer uses. | 215 | -- version of BTP the remote peer uses. |
216 | newtype ProtocolString = ProtocolString BS.ByteString | 216 | newtype ProtocolName = ProtocolName BS.ByteString |
217 | deriving (Eq, Ord, Typeable) | 217 | deriving (Eq, Ord, Typeable) |
218 | 218 | ||
219 | -- | In BTP/1.0 the name is 'BitTorrent protocol'. If this string is | 219 | -- | In BTP/1.0 the name is 'BitTorrent protocol'. If this string is |
220 | -- different from the local peers own protocol name, then the | 220 | -- different from the local peers own protocol name, then the |
221 | -- connection is to be dropped. | 221 | -- connection is to be dropped. |
222 | instance Default ProtocolString where | 222 | instance Default ProtocolName where |
223 | def = ProtocolString "BitTorrent protocol" | 223 | def = ProtocolName "BitTorrent protocol" |
224 | 224 | ||
225 | instance Show ProtocolString where | 225 | instance Show ProtocolName where |
226 | show (ProtocolString bs) = show bs | 226 | show (ProtocolName bs) = show bs |
227 | 227 | ||
228 | instance Pretty ProtocolString where | 228 | instance Pretty ProtocolName where |
229 | pretty (ProtocolString bs) = PP.text $ BC.unpack bs | 229 | pretty (ProtocolName bs) = PP.text $ BC.unpack bs |
230 | 230 | ||
231 | instance IsString ProtocolString where | 231 | instance IsString ProtocolName where |
232 | fromString str | 232 | fromString str |
233 | | L.length str <= fromIntegral maxProtocolStringSize | 233 | | L.length str <= fromIntegral maxProtocolNameSize |
234 | = ProtocolString (fromString str) | 234 | = ProtocolName (fromString str) |
235 | | otherwise = error $ "fromString: ProtocolString too long: " ++ str | 235 | | otherwise = error $ "fromString: ProtocolName too long: " ++ str |
236 | 236 | ||
237 | instance Serialize ProtocolString where | 237 | instance Serialize ProtocolName where |
238 | put (ProtocolString bs) = do | 238 | put (ProtocolName bs) = do |
239 | putWord8 $ fromIntegral $ BS.length bs | 239 | putWord8 $ fromIntegral $ BS.length bs |
240 | putByteString bs | 240 | putByteString bs |
241 | 241 | ||
242 | get = do | 242 | get = do |
243 | len <- getWord8 | 243 | len <- getWord8 |
244 | bs <- getByteString $ fromIntegral len | 244 | bs <- getByteString $ fromIntegral len |
245 | return (ProtocolString bs) | 245 | return (ProtocolName bs) |
246 | 246 | ||
247 | -- | Handshake message is used to exchange all information necessary | 247 | -- | Handshake message is used to exchange all information necessary |
248 | -- to establish connection between peers. | 248 | -- to establish connection between peers. |
249 | -- | 249 | -- |
250 | data Handshake = Handshake { | 250 | data Handshake = Handshake { |
251 | -- | Identifier of the protocol. This is usually equal to 'def'. | 251 | -- | Identifier of the protocol. This is usually equal to 'def'. |
252 | hsProtocol :: ProtocolString | 252 | hsProtocol :: ProtocolName |
253 | 253 | ||
254 | -- | Reserved bytes used to specify supported BEP's. | 254 | -- | Reserved bytes used to specify supported BEP's. |
255 | , hsReserved :: Caps | 255 | , hsReserved :: Caps |
@@ -290,7 +290,7 @@ handshakeSize n = 1 + fromIntegral n + 8 + 20 + 20 | |||
290 | 290 | ||
291 | -- | Maximum size of handshake message in bytes. | 291 | -- | Maximum size of handshake message in bytes. |
292 | handshakeMaxSize :: Int | 292 | handshakeMaxSize :: Int |
293 | handshakeMaxSize = handshakeSize maxProtocolStringSize | 293 | handshakeMaxSize = handshakeSize maxProtocolNameSize |
294 | 294 | ||
295 | -- | Handshake with default protocol string and reserved bitmask. | 295 | -- | Handshake with default protocol string and reserved bitmask. |
296 | defaultHandshake :: InfoHash -> PeerId -> Handshake | 296 | defaultHandshake :: InfoHash -> PeerId -> Handshake |
diff --git a/src/Network/BitTorrent/Exchange/Wire.hs b/src/Network/BitTorrent/Exchange/Wire.hs index fe4086bc..e0e652ec 100644 --- a/src/Network/BitTorrent/Exchange/Wire.hs +++ b/src/Network/BitTorrent/Exchange/Wire.hs | |||
@@ -102,7 +102,7 @@ instance Pretty ChannelSide where | |||
102 | data ProtocolError | 102 | data ProtocolError |
103 | -- | Protocol string should be 'BitTorrent Protocol' but remote | 103 | -- | Protocol string should be 'BitTorrent Protocol' but remote |
104 | -- peer send a different string. | 104 | -- peer send a different string. |
105 | = InvalidProtocol ProtocolString | 105 | = InvalidProtocol ProtocolName |
106 | | UnexpectedTopic InfoHash -- ^ peer replied with unexpected infohash. | 106 | | UnexpectedTopic InfoHash -- ^ peer replied with unexpected infohash. |
107 | | UnexpectedPeerId PeerId -- ^ peer replied with unexpected peer id. | 107 | | UnexpectedPeerId PeerId -- ^ peer replied with unexpected peer id. |
108 | | UnknownTopic InfoHash -- ^ peer requested unknown torrent. | 108 | | UnknownTopic InfoHash -- ^ peer requested unknown torrent. |
diff --git a/tests/Network/BitTorrent/Exchange/MessageSpec.hs b/tests/Network/BitTorrent/Exchange/MessageSpec.hs index 5d332eaa..6a794ce2 100644 --- a/tests/Network/BitTorrent/Exchange/MessageSpec.hs +++ b/tests/Network/BitTorrent/Exchange/MessageSpec.hs | |||
@@ -2,8 +2,6 @@ module Network.BitTorrent.Exchange.MessageSpec (spec) where | |||
2 | import Control.Applicative | 2 | import Control.Applicative |
3 | import Control.Exception | 3 | import Control.Exception |
4 | import Data.ByteString as BS | 4 | import Data.ByteString as BS |
5 | import Data.ByteString.Lazy as BL | ||
6 | import Data.Default | ||
7 | import Data.List as L | 5 | import Data.List as L |
8 | import Data.Set as S | 6 | import Data.Set as S |
9 | import Data.Serialize as S | 7 | import Data.Serialize as S |
@@ -14,7 +12,7 @@ import Test.QuickCheck | |||
14 | import Data.Torrent.BitfieldSpec () | 12 | import Data.Torrent.BitfieldSpec () |
15 | import Data.Torrent.InfoHashSpec () | 13 | import Data.Torrent.InfoHashSpec () |
16 | import Network.BitTorrent.CoreSpec () | 14 | import Network.BitTorrent.CoreSpec () |
17 | import Network.BitTorrent.Core | 15 | import Network.BitTorrent.Core () |
18 | import Network.BitTorrent.Exchange.BlockSpec () | 16 | import Network.BitTorrent.Exchange.BlockSpec () |
19 | import Network.BitTorrent.Exchange.Message | 17 | import Network.BitTorrent.Exchange.Message |
20 | 18 | ||
@@ -24,7 +22,7 @@ instance Arbitrary Extension where | |||
24 | instance Arbitrary Caps where | 22 | instance Arbitrary Caps where |
25 | arbitrary = toCaps <$> arbitrary | 23 | arbitrary = toCaps <$> arbitrary |
26 | 24 | ||
27 | instance Arbitrary ProtocolString where | 25 | instance Arbitrary ProtocolName where |
28 | arbitrary = fromString <$> (arbitrary `suchThat` ((200 <) . L.length)) | 26 | arbitrary = fromString <$> (arbitrary `suchThat` ((200 <) . L.length)) |
29 | 27 | ||
30 | instance Arbitrary Handshake where | 28 | instance Arbitrary Handshake where |
@@ -85,12 +83,12 @@ spec = do | |||
85 | byteLength (stats msg) `shouldBe` | 83 | byteLength (stats msg) `shouldBe` |
86 | fromIntegral (BS.length (S.encode (msg :: Message))) | 84 | fromIntegral (BS.length (S.encode (msg :: Message))) |
87 | 85 | ||
88 | describe "ProtocolString" $ do | 86 | describe "ProtocolName" $ do |
89 | it "fail to construct invalid string" $ do | 87 | it "fail to construct invalid string" $ do |
90 | let str = L.replicate 500 'x' | 88 | let str = L.replicate 500 'x' |
91 | evaluate (fromString str :: ProtocolString) | 89 | evaluate (fromString str :: ProtocolName) |
92 | `shouldThrow` | 90 | `shouldThrow` |
93 | errorCall ("fromString: ProtocolString too long: " ++ str) | 91 | errorCall ("fromString: ProtocolName too long: " ++ str) |
94 | 92 | ||
95 | describe "Handshake" $ do | 93 | describe "Handshake" $ do |
96 | it "properly serialized" $ property $ \ hs -> | 94 | it "properly serialized" $ property $ \ hs -> |