diff options
Diffstat (limited to 'src/Network/BitTorrent/Core.hs')
-rw-r--r-- | src/Network/BitTorrent/Core.hs | 88 |
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 | -- | ||
10 | module 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 | |||
23 | import Control.Applicative | ||
24 | import Data.IP | ||
25 | import Data.Hashable | ||
26 | import Data.Serialize | ||
27 | import Data.Time | ||
28 | import Data.Typeable | ||
29 | import Network.Socket (SockAddr (..), PortNumber) | ||
30 | import Text.PrettyPrint as PP hiding ((<>)) | ||
31 | import Text.PrettyPrint.Class | ||
32 | |||
33 | import Network.BitTorrent.Core.Fingerprint as Core | ||
34 | import Network.BitTorrent.Core.NodeInfo as Core | ||
35 | import Network.BitTorrent.Core.PeerId as Core | ||
36 | import Network.BitTorrent.Core.PeerAddr as Core | ||
37 | |||
38 | |||
39 | instance Pretty UTCTime where | ||
40 | pretty = PP.text . show | ||
41 | |||
42 | class (Eq a, Serialize a, Typeable a, Hashable a, Pretty a) | ||
43 | => Address a where | ||
44 | toSockAddr :: a -> SockAddr | ||
45 | fromSockAddr :: SockAddr -> Maybe a | ||
46 | |||
47 | fromAddr :: (Address a, Address b) => a -> Maybe b | ||
48 | fromAddr = fromSockAddr . toSockAddr | ||
49 | |||
50 | -- | Note that port is zeroed. | ||
51 | instance 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. | ||
57 | instance 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. | ||
63 | instance 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 | |||
70 | setPort :: PortNumber -> SockAddr -> SockAddr | ||
71 | setPort port (SockAddrInet _ h ) = SockAddrInet port h | ||
72 | setPort port (SockAddrInet6 _ f h s) = SockAddrInet6 port f h s | ||
73 | setPort _ (SockAddrUnix s ) = SockAddrUnix s | ||
74 | {-# INLINE setPort #-} | ||
75 | |||
76 | getPort :: SockAddr -> Maybe PortNumber | ||
77 | getPort (SockAddrInet p _ ) = Just p | ||
78 | getPort (SockAddrInet6 p _ _ _) = Just p | ||
79 | getPort (SockAddrUnix _ ) = Nothing | ||
80 | {-# INLINE getPort #-} | ||
81 | |||
82 | instance Address a => Address (NodeAddr a) where | ||
83 | toSockAddr NodeAddr {..} = setPort nodePort $ toSockAddr nodeHost | ||
84 | fromSockAddr sa = NodeAddr <$> fromSockAddr sa <*> getPort sa | ||
85 | |||
86 | instance Address a => Address (PeerAddr a) where | ||
87 | toSockAddr PeerAddr {..} = setPort peerPort $ toSockAddr peerHost | ||
88 | fromSockAddr sa = PeerAddr Nothing <$> fromSockAddr sa <*> getPort sa | ||