summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Exchange
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/Exchange')
-rw-r--r--src/Network/BitTorrent/Exchange/Message.hs38
-rw-r--r--src/Network/BitTorrent/Exchange/Wire.hs2
2 files changed, 20 insertions, 20 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.