summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Core.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Network/BitTorrent/Core.hs')
-rw-r--r--src/Network/BitTorrent/Core.hs88
1 files changed, 0 insertions, 88 deletions
diff --git a/src/Network/BitTorrent/Core.hs b/src/Network/BitTorrent/Core.hs
deleted file mode 100644
index b9b3c065..00000000
--- a/src/Network/BitTorrent/Core.hs
+++ /dev/null
@@ -1,88 +0,0 @@
1-- |
2-- Copyright : (c) Sam Truzjan 2013
3-- License : BSD3
4-- Maintainer : pxqr.sta@gmail.com
5-- Stability : experimental
6-- Portability : portable
7--
8-- Re-export every @Network.BitTorrent.Core.*@ module.
9--
10module Network.BitTorrent.Core
11 ( module Core
12
13 -- * Address class
14 , Address (..)
15 , fromAddr
16
17 -- * Re-exports from Data.IP
18 , IPv4
19 , IPv6
20 , IP (..)
21 ) where
22
23import Control.Applicative
24import Data.IP
25import Data.Hashable
26import Data.Serialize
27import Data.Time
28import Data.Typeable
29import Network.Socket (SockAddr (..), PortNumber)
30import Text.PrettyPrint as PP hiding ((<>))
31import Text.PrettyPrint.Class
32
33import Network.BitTorrent.Core.Fingerprint as Core
34import Network.BitTorrent.Core.NodeInfo as Core
35import Network.BitTorrent.Core.PeerId as Core
36import Network.BitTorrent.Core.PeerAddr as Core
37
38
39instance Pretty UTCTime where
40 pretty = PP.text . show
41
42class (Eq a, Serialize a, Typeable a, Hashable a, Pretty a)
43 => Address a where
44 toSockAddr :: a -> SockAddr
45 fromSockAddr :: SockAddr -> Maybe a
46
47fromAddr :: (Address a, Address b) => a -> Maybe b
48fromAddr = fromSockAddr . toSockAddr
49
50-- | Note that port is zeroed.
51instance Address IPv4 where
52 toSockAddr = SockAddrInet 0 . toHostAddress
53 fromSockAddr (SockAddrInet _ h) = Just (fromHostAddress h)
54 fromSockAddr _ = Nothing
55
56-- | Note that port is zeroed.
57instance Address IPv6 where
58 toSockAddr h = SockAddrInet6 0 0 (toHostAddress6 h) 0
59 fromSockAddr (SockAddrInet6 _ _ h _) = Just (fromHostAddress6 h)
60 fromSockAddr _ = Nothing
61
62-- | Note that port is zeroed.
63instance Address IP where
64 toSockAddr (IPv4 h) = toSockAddr h
65 toSockAddr (IPv6 h) = toSockAddr h
66 fromSockAddr sa =
67 IPv4 <$> fromSockAddr sa
68 <|> IPv6 <$> fromSockAddr sa
69
70setPort :: PortNumber -> SockAddr -> SockAddr
71setPort port (SockAddrInet _ h ) = SockAddrInet port h
72setPort port (SockAddrInet6 _ f h s) = SockAddrInet6 port f h s
73setPort _ (SockAddrUnix s ) = SockAddrUnix s
74{-# INLINE setPort #-}
75
76getPort :: SockAddr -> Maybe PortNumber
77getPort (SockAddrInet p _ ) = Just p
78getPort (SockAddrInet6 p _ _ _) = Just p
79getPort (SockAddrUnix _ ) = Nothing
80{-# INLINE getPort #-}
81
82instance Address a => Address (NodeAddr a) where
83 toSockAddr NodeAddr {..} = setPort nodePort $ toSockAddr nodeHost
84 fromSockAddr sa = NodeAddr <$> fromSockAddr sa <*> getPort sa
85
86instance Address a => Address (PeerAddr a) where
87 toSockAddr PeerAddr {..} = setPort peerPort $ toSockAddr peerHost
88 fromSockAddr sa = PeerAddr Nothing <$> fromSockAddr sa <*> getPort sa