diff options
Diffstat (limited to 'src/Network/BitTorrent/Address.hs')
-rw-r--r-- | src/Network/BitTorrent/Address.hs | 23 |
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 | |||
91 | import Control.Applicative | 91 | import Control.Applicative |
92 | import Control.Monad | 92 | import Control.Monad |
93 | import Control.Exception (onException) | 93 | import Control.Exception (onException) |
94 | #ifdef VERSION_bencoding | ||
94 | import Data.BEncode as BE | 95 | import Data.BEncode as BE |
95 | import Data.BEncode.BDict (BKey) | 96 | import Data.BEncode.BDict (BKey) |
97 | #endif | ||
96 | import Data.Bits | 98 | import Data.Bits |
97 | import qualified Data.ByteString as BS | 99 | import qualified Data.ByteString as BS |
98 | import qualified Data.ByteString.Internal as BS | 100 | import 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. |
206 | newtype PeerId = PeerId { getPeerId :: ByteString } | 208 | newtype 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 | ||
209 | peerIdLen :: Int | 215 | peerIdLen :: Int |
210 | peerIdLen = 20 | 216 | peerIdLen = 20 |
@@ -369,6 +375,7 @@ genPeerId = azureusStyle defaultClientId defaultVersionNumber <$> timestamp | |||
369 | -- Port number | 375 | -- Port number |
370 | -----------------------------------------------------------------------} | 376 | -----------------------------------------------------------------------} |
371 | 377 | ||
378 | #ifdef VERSION_bencoding | ||
372 | instance BEncode PortNumber where | 379 | instance 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 | ||
382 | instance Serialize PortNumber where | 390 | instance Serialize PortNumber where |
383 | get = fromIntegral <$> getWord16be | 391 | get = fromIntegral <$> getWord16be |
@@ -417,6 +425,7 @@ deriving instance Typeable IP | |||
417 | deriving instance Typeable IPv4 | 425 | deriving instance Typeable IPv4 |
418 | deriving instance Typeable IPv6 | 426 | deriving instance Typeable IPv6 |
419 | 427 | ||
428 | #ifdef VERSION_bencoding | ||
420 | ipToBEncode :: Show i => i -> BValue | 429 | ipToBEncode :: Show i => i -> BValue |
421 | ipToBEncode ip = BString $ BS8.pack $ show ip | 430 | ipToBEncode 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 | ||
511 | peer_ip_key, peer_id_key, peer_port_key :: BKey | 522 | peer_ip_key, peer_id_key, peer_port_key :: BKey |
512 | peer_ip_key = "ip" | 523 | peer_ip_key = "ip" |
513 | peer_id_key = "peer id" | 524 | peer_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. |
644 | newtype NodeId = NodeId ByteString | 656 | newtype 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 | ||
647 | nodeIdSize :: Int | 664 | nodeIdSize :: Int |
648 | nodeIdSize = 20 | 665 | nodeIdSize = 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. |
775 | instance BEncode a => BEncode (NodeAddr a) where | 793 | instance 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 | ||
781 | instance Hashable a => Hashable (NodeAddr a) where | 800 | instance Hashable a => Hashable (NodeAddr a) where |
782 | hashWithSalt s NodeAddr {..} = hashWithSalt s (nodeHost, nodePort) | 801 | hashWithSalt s NodeAddr {..} = hashWithSalt s (nodeHost, nodePort) |