summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Address.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/Address.hs')
-rw-r--r--src/Network/BitTorrent/Address.hs23
1 files changed, 21 insertions, 2 deletions
diff --git a/src/Network/BitTorrent/Address.hs b/src/Network/BitTorrent/Address.hs
index a8e12b35..2132f8f9 100644
--- a/src/Network/BitTorrent/Address.hs
+++ b/src/Network/BitTorrent/Address.hs
@@ -91,8 +91,10 @@ module Network.BitTorrent.Address
91import Control.Applicative 91import Control.Applicative
92import Control.Monad 92import Control.Monad
93import Control.Exception (onException) 93import Control.Exception (onException)
94#ifdef VERSION_bencoding
94import Data.BEncode as BE 95import Data.BEncode as BE
95import Data.BEncode.BDict (BKey) 96import Data.BEncode.BDict (BKey)
97#endif
96import Data.Bits 98import Data.Bits
97import qualified Data.ByteString as BS 99import qualified Data.ByteString as BS
98import qualified Data.ByteString.Internal as BS 100import qualified Data.ByteString.Internal as BS
@@ -204,7 +206,11 @@ instance Address a => Address (PeerAddr a) where
204 206
205-- | Peer identifier is exactly 20 bytes long bytestring. 207-- | Peer identifier is exactly 20 bytes long bytestring.
206newtype PeerId = PeerId { getPeerId :: ByteString } 208newtype PeerId = PeerId { getPeerId :: ByteString }
207 deriving (Show, Eq, Ord, BEncode, Typeable) 209 deriving ( Show, Eq, Ord, Typeable
210#ifdef VERSION_bencoding
211 , BEncode
212#endif
213 )
208 214
209peerIdLen :: Int 215peerIdLen :: Int
210peerIdLen = 20 216peerIdLen = 20
@@ -369,6 +375,7 @@ genPeerId = azureusStyle defaultClientId defaultVersionNumber <$> timestamp
369-- Port number 375-- Port number
370-----------------------------------------------------------------------} 376-----------------------------------------------------------------------}
371 377
378#ifdef VERSION_bencoding
372instance BEncode PortNumber where 379instance BEncode PortNumber where
373 toBEncode = toBEncode . fromEnum 380 toBEncode = toBEncode . fromEnum
374 fromBEncode = fromBEncode >=> portNumber 381 fromBEncode = fromBEncode >=> portNumber
@@ -378,6 +385,7 @@ instance BEncode PortNumber where
378 | 0 <= n && n <= fromIntegral (maxBound :: Word16) 385 | 0 <= n && n <= fromIntegral (maxBound :: Word16)
379 = pure $ fromIntegral n 386 = pure $ fromIntegral n
380 | otherwise = decodingError $ "PortNumber: " ++ show n 387 | otherwise = decodingError $ "PortNumber: " ++ show n
388#endif
381 389
382instance Serialize PortNumber where 390instance Serialize PortNumber where
383 get = fromIntegral <$> getWord16be 391 get = fromIntegral <$> getWord16be
@@ -417,6 +425,7 @@ deriving instance Typeable IP
417deriving instance Typeable IPv4 425deriving instance Typeable IPv4
418deriving instance Typeable IPv6 426deriving instance Typeable IPv6
419 427
428#ifdef VERSION_bencoding
420ipToBEncode :: Show i => i -> BValue 429ipToBEncode :: Show i => i -> BValue
421ipToBEncode ip = BString $ BS8.pack $ show ip 430ipToBEncode ip = BString $ BS8.pack $ show ip
422{-# INLINE ipToBEncode #-} 431{-# INLINE ipToBEncode #-}
@@ -444,6 +453,7 @@ instance BEncode IPv6 where
444 {-# INLINE toBEncode #-} 453 {-# INLINE toBEncode #-}
445 fromBEncode = ipFromBEncode 454 fromBEncode = ipFromBEncode
446 {-# INLINE fromBEncode #-} 455 {-# INLINE fromBEncode #-}
456#endif
447 457
448-- | When 'get'ing an IP it must be 'isolate'd to the appropriate 458-- | When 'get'ing an IP it must be 'isolate'd to the appropriate
449-- number of bytes since we have no other way of telling which 459-- number of bytes since we have no other way of telling which
@@ -508,6 +518,7 @@ data PeerAddr a = PeerAddr
508 , peerPort :: {-# UNPACK #-} !PortNumber 518 , peerPort :: {-# UNPACK #-} !PortNumber
509 } deriving (Show, Eq, Ord, Typeable, Functor) 519 } deriving (Show, Eq, Ord, Typeable, Functor)
510 520
521#ifdef VERSION_bencoding
511peer_ip_key, peer_id_key, peer_port_key :: BKey 522peer_ip_key, peer_id_key, peer_port_key :: BKey
512peer_ip_key = "ip" 523peer_ip_key = "ip"
513peer_id_key = "peer id" 524peer_id_key = "peer id"
@@ -527,6 +538,7 @@ instance (Typeable a, BEncode a) => BEncode (PeerAddr a) where
527 <*>! peer_port_key 538 <*>! peer_port_key
528 where 539 where
529 peerAddr = flip PeerAddr 540 peerAddr = flip PeerAddr
541#endif
530 542
531-- | The tracker's 'compact peer list' compatible encoding. The 543-- | The tracker's 'compact peer list' compatible encoding. The
532-- 'peerId' is always 'Nothing'. 544-- 'peerId' is always 'Nothing'.
@@ -642,7 +654,12 @@ peerSocket socketType pa = do
642-- Normally, /this/ node id should be saved between invocations 654-- Normally, /this/ node id should be saved between invocations
643-- of the client software. 655-- of the client software.
644newtype NodeId = NodeId ByteString 656newtype NodeId = NodeId ByteString
645 deriving (Show, Eq, Ord, BEncode, Typeable) 657 deriving (Show, Eq, Ord, Typeable
658#ifdef VERSION_bencoding
659 , BEncode
660#endif
661 )
662
646 663
647nodeIdSize :: Int 664nodeIdSize :: Int
648nodeIdSize = 20 665nodeIdSize = 20
@@ -771,12 +788,14 @@ instance Serialize a => Serialize (NodeAddr a) where
771 put NodeAddr {..} = put nodeHost >> put nodePort 788 put NodeAddr {..} = put nodeHost >> put nodePort
772 {-# INLINE put #-} 789 {-# INLINE put #-}
773 790
791#ifdef VERSION_bencoding
774-- | Torrent file compatible encoding. 792-- | Torrent file compatible encoding.
775instance BEncode a => BEncode (NodeAddr a) where 793instance BEncode a => BEncode (NodeAddr a) where
776 toBEncode NodeAddr {..} = toBEncode (nodeHost, nodePort) 794 toBEncode NodeAddr {..} = toBEncode (nodeHost, nodePort)
777 {-# INLINE toBEncode #-} 795 {-# INLINE toBEncode #-}
778 fromBEncode b = uncurry NodeAddr <$> fromBEncode b 796 fromBEncode b = uncurry NodeAddr <$> fromBEncode b
779 {-# INLINE fromBEncode #-} 797 {-# INLINE fromBEncode #-}
798#endif
780 799
781instance Hashable a => Hashable (NodeAddr a) where 800instance Hashable a => Hashable (NodeAddr a) where
782 hashWithSalt s NodeAddr {..} = hashWithSalt s (nodeHost, nodePort) 801 hashWithSalt s NodeAddr {..} = hashWithSalt s (nodeHost, nodePort)