summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-12-29 08:18:36 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-12-29 08:18:36 +0400
commit2c30e11e4ed379afb7b3fc5c35de9913085f090e (patch)
tree2b4c4774050fab860a2f6c65efb5288f621ed751 /src/Network
parent1e1ab84ec903aac67b5d1f337cfb290975fc77e1 (diff)
Add hashable instances for all address types
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/BitTorrent/Core.hs10
-rw-r--r--src/Network/BitTorrent/Core/PeerAddr.hs15
2 files changed, 24 insertions, 1 deletions
diff --git a/src/Network/BitTorrent/Core.hs b/src/Network/BitTorrent/Core.hs
index 2ddee517..5717e4de 100644
--- a/src/Network/BitTorrent/Core.hs
+++ b/src/Network/BitTorrent/Core.hs
@@ -9,7 +9,10 @@
9-- 9--
10module Network.BitTorrent.Core 10module Network.BitTorrent.Core
11 ( module Core 11 ( module Core
12
13 -- * Address class
12 , Address (..) 14 , Address (..)
15 , fromAddr
13 16
14 -- * Re-exports from Data.IP 17 -- * Re-exports from Data.IP
15 , IPv4 18 , IPv4
@@ -19,6 +22,7 @@ module Network.BitTorrent.Core
19 22
20import Control.Applicative 23import Control.Applicative
21import Data.IP 24import Data.IP
25import Data.Hashable
22import Data.Serialize 26import Data.Serialize
23import Data.Time 27import Data.Time
24import Data.Typeable 28import Data.Typeable
@@ -35,10 +39,14 @@ import Network.BitTorrent.Core.PeerAddr as Core
35instance Pretty UTCTime where 39instance Pretty UTCTime where
36 pretty = PP.text . show 40 pretty = PP.text . show
37 41
38class (Eq a, Serialize a, Typeable a, Pretty a) => Address a where 42class (Eq a, Serialize a, Typeable a, Hashable a, Pretty a)
43 => Address a where
39 toSockAddr :: a -> SockAddr 44 toSockAddr :: a -> SockAddr
40 fromSockAddr :: SockAddr -> Maybe a 45 fromSockAddr :: SockAddr -> Maybe a
41 46
47fromAddr :: (Address a, Address b) => a -> Maybe b
48fromAddr = fromSockAddr . toSockAddr
49
42-- | Note that port is zeroed. 50-- | Note that port is zeroed.
43instance Address IPv4 where 51instance Address IPv4 where
44 toSockAddr = SockAddrInet 0 . toHostAddress 52 toSockAddr = SockAddrInet 0 . toHostAddress
diff --git a/src/Network/BitTorrent/Core/PeerAddr.hs b/src/Network/BitTorrent/Core/PeerAddr.hs
index 4905f910..d634716c 100644
--- a/src/Network/BitTorrent/Core/PeerAddr.hs
+++ b/src/Network/BitTorrent/Core/PeerAddr.hs
@@ -176,6 +176,17 @@ instance Pretty IP where
176 pretty = PP.text . show 176 pretty = PP.text . show
177 {-# INLINE pretty #-} 177 {-# INLINE pretty #-}
178 178
179instance Hashable IPv4 where
180 hashWithSalt = hashUsing toHostAddress
181 {-# INLINE hashWithSalt #-}
182
183instance Hashable IPv6 where
184 hashWithSalt s a = hashWithSalt s (toHostAddress6 a)
185
186instance Hashable IP where
187 hashWithSalt s (IPv4 h) = hashWithSalt s h
188 hashWithSalt s (IPv6 h) = hashWithSalt s h
189
179{----------------------------------------------------------------------- 190{-----------------------------------------------------------------------
180-- Peer addr 191-- Peer addr
181-----------------------------------------------------------------------} 192-----------------------------------------------------------------------}
@@ -267,6 +278,10 @@ instance Pretty a => Pretty (PeerAddr a) where
267 where 278 where
268 paddr = pretty peerHost <> ":" <> text (show peerPort) 279 paddr = pretty peerHost <> ":" <> text (show peerPort)
269 280
281instance Hashable a => Hashable (PeerAddr a) where
282 hashWithSalt s PeerAddr {..} =
283 s `hashWithSalt` peerId `hashWithSalt` peerHost `hashWithSalt` peerPort
284
270-- | Ports typically reserved for bittorrent P2P listener. 285-- | Ports typically reserved for bittorrent P2P listener.
271defaultPorts :: [PortNumber] 286defaultPorts :: [PortNumber]
272defaultPorts = [6881..6889] 287defaultPorts = [6881..6889]