summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-12-08 08:23:50 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-12-08 08:23:50 +0400
commit1dbd72547c0f0bbaad9e89b9cd7d7a8034342f0f (patch)
tree0f37d62557b0c474a0f7ea21b0f8d566c0a712ed /src/Network
parent18f29191f6092ba502b27a64b7b4abebeaa6dc88 (diff)
Close connection if peer send invalid protocol string
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/BitTorrent/Exchange/Wire.hs11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/Network/BitTorrent/Exchange/Wire.hs b/src/Network/BitTorrent/Exchange/Wire.hs
index b04bd378..3ec01ca1 100644
--- a/src/Network/BitTorrent/Exchange/Wire.hs
+++ b/src/Network/BitTorrent/Exchange/Wire.hs
@@ -73,11 +73,15 @@ data ChannelSide
73 | RemotePeer 73 | RemotePeer
74 deriving (Show, Eq, Enum) 74 deriving (Show, Eq, Enum)
75 75
76-- TODO pretty instance 76instance Pretty ChannelSide where
77 pretty = PP.text . show
77 78
78-- | Errors occur when a remote peer violates protocol specification. 79-- | Errors occur when a remote peer violates protocol specification.
79data ProtocolError 80data ProtocolError
80 = UnexpectedTopic InfoHash -- ^ peer replied with unexpected infohash. 81 -- | Protocol string should be 'BitTorrent Protocol' but remote
82 -- peer send a different string.
83 = InvalidProtocol ProtocolString
84 | UnexpectedTopic InfoHash -- ^ peer replied with unexpected infohash.
81 | UnexpectedPeerId PeerId -- ^ peer replied with unexpected peer id. 85 | UnexpectedPeerId PeerId -- ^ peer replied with unexpected peer id.
82 | UnknownTopic InfoHash -- ^ peer requested unknown torrent. 86 | UnknownTopic InfoHash -- ^ peer requested unknown torrent.
83 | HandshakeRefused -- ^ peer do not send an extended handshake back. 87 | HandshakeRefused -- ^ peer do not send an extended handshake back.
@@ -270,6 +274,9 @@ connectWire hs addr extCaps wire =
270 bracket (connectToPeer addr) close $ \ sock -> do 274 bracket (connectToPeer addr) close $ \ sock -> do
271 hs' <- initiateHandshake sock hs 275 hs' <- initiateHandshake sock hs
272 276
277 unless (def == hsProtocol hs') $ do
278 throwIO $ ProtocolError $ InvalidProtocol (hsProtocol hs')
279
273 unless (hsInfoHash hs == hsInfoHash hs') $ do 280 unless (hsInfoHash hs == hsInfoHash hs') $ do
274 throwIO $ ProtocolError $ UnexpectedTopic (hsInfoHash hs') 281 throwIO $ ProtocolError $ UnexpectedTopic (hsInfoHash hs')
275 282