summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Network/BitTorrent/Exchange/Message.hs38
-rw-r--r--src/Network/BitTorrent/Exchange/Wire.hs2
-rw-r--r--tests/Network/BitTorrent/Exchange/MessageSpec.hs12
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
211maxProtocolStringSize :: Word8 211maxProtocolNameSize :: Word8
212maxProtocolStringSize = maxBound 212maxProtocolNameSize = 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.
216newtype ProtocolString = ProtocolString BS.ByteString 216newtype 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.
222instance Default ProtocolString where 222instance Default ProtocolName where
223 def = ProtocolString "BitTorrent protocol" 223 def = ProtocolName "BitTorrent protocol"
224 224
225instance Show ProtocolString where 225instance Show ProtocolName where
226 show (ProtocolString bs) = show bs 226 show (ProtocolName bs) = show bs
227 227
228instance Pretty ProtocolString where 228instance Pretty ProtocolName where
229 pretty (ProtocolString bs) = PP.text $ BC.unpack bs 229 pretty (ProtocolName bs) = PP.text $ BC.unpack bs
230 230
231instance IsString ProtocolString where 231instance 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
237instance Serialize ProtocolString where 237instance 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--
250data Handshake = Handshake { 250data 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.
292handshakeMaxSize :: Int 292handshakeMaxSize :: Int
293handshakeMaxSize = handshakeSize maxProtocolStringSize 293handshakeMaxSize = handshakeSize maxProtocolNameSize
294 294
295-- | Handshake with default protocol string and reserved bitmask. 295-- | Handshake with default protocol string and reserved bitmask.
296defaultHandshake :: InfoHash -> PeerId -> Handshake 296defaultHandshake :: 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
102data ProtocolError 102data 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
2import Control.Applicative 2import Control.Applicative
3import Control.Exception 3import Control.Exception
4import Data.ByteString as BS 4import Data.ByteString as BS
5import Data.ByteString.Lazy as BL
6import Data.Default
7import Data.List as L 5import Data.List as L
8import Data.Set as S 6import Data.Set as S
9import Data.Serialize as S 7import Data.Serialize as S
@@ -14,7 +12,7 @@ import Test.QuickCheck
14import Data.Torrent.BitfieldSpec () 12import Data.Torrent.BitfieldSpec ()
15import Data.Torrent.InfoHashSpec () 13import Data.Torrent.InfoHashSpec ()
16import Network.BitTorrent.CoreSpec () 14import Network.BitTorrent.CoreSpec ()
17import Network.BitTorrent.Core 15import Network.BitTorrent.Core ()
18import Network.BitTorrent.Exchange.BlockSpec () 16import Network.BitTorrent.Exchange.BlockSpec ()
19import Network.BitTorrent.Exchange.Message 17import Network.BitTorrent.Exchange.Message
20 18
@@ -24,7 +22,7 @@ instance Arbitrary Extension where
24instance Arbitrary Caps where 22instance Arbitrary Caps where
25 arbitrary = toCaps <$> arbitrary 23 arbitrary = toCaps <$> arbitrary
26 24
27instance Arbitrary ProtocolString where 25instance Arbitrary ProtocolName where
28 arbitrary = fromString <$> (arbitrary `suchThat` ((200 <) . L.length)) 26 arbitrary = fromString <$> (arbitrary `suchThat` ((200 <) . L.length))
29 27
30instance Arbitrary Handshake where 28instance 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 ->