diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-16 20:19:07 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-16 20:19:07 +0400 |
commit | 0cf1c142d0e18eef05e1190d0fdaa94d2fa4df59 (patch) | |
tree | b7103d0a55c665bd738eb23ccc3784f3e8d13c18 /src/Network/BitTorrent/Core | |
parent | f393a2ec1611d2e5587f6fc97317294377c72d5d (diff) |
Add spec for AnnounceInfo encoding
Diffstat (limited to 'src/Network/BitTorrent/Core')
-rw-r--r-- | src/Network/BitTorrent/Core/PeerAddr.hs | 41 |
1 files changed, 20 insertions, 21 deletions
diff --git a/src/Network/BitTorrent/Core/PeerAddr.hs b/src/Network/BitTorrent/Core/PeerAddr.hs index 6c6056c9..88239d0b 100644 --- a/src/Network/BitTorrent/Core/PeerAddr.hs +++ b/src/Network/BitTorrent/Core/PeerAddr.hs | |||
@@ -21,11 +21,6 @@ module Network.BitTorrent.Core.PeerAddr | |||
21 | PeerAddr(..) | 21 | PeerAddr(..) |
22 | , defaultPorts | 22 | , defaultPorts |
23 | , peerSockAddr | 23 | , peerSockAddr |
24 | |||
25 | -- * IP | ||
26 | , mergeIPLists | ||
27 | , splitIPList | ||
28 | , IPAddress () | ||
29 | ) where | 24 | ) where |
30 | 25 | ||
31 | import Control.Applicative | 26 | import Control.Applicative |
@@ -36,8 +31,6 @@ import Data.BEncode.BDict (BKey) | |||
36 | import Data.ByteString.Char8 as BS8 | 31 | import Data.ByteString.Char8 as BS8 |
37 | import Data.Char | 32 | import Data.Char |
38 | import Data.Default | 33 | import Data.Default |
39 | import Data.Either | ||
40 | import Data.Foldable | ||
41 | import Data.IP | 34 | import Data.IP |
42 | import Data.List as L | 35 | import Data.List as L |
43 | import Data.List.Split | 36 | import Data.List.Split |
@@ -86,13 +79,16 @@ class IPAddress i where | |||
86 | 79 | ||
87 | instance IPAddress IPv4 where | 80 | instance IPAddress IPv4 where |
88 | toHostAddr = Left . toHostAddress | 81 | toHostAddr = Left . toHostAddress |
82 | {-# INLINE toHostAddr #-} | ||
89 | 83 | ||
90 | instance IPAddress IPv6 where | 84 | instance IPAddress IPv6 where |
91 | toHostAddr = Right . toHostAddress6 | 85 | toHostAddr = Right . toHostAddress6 |
86 | {-# INLINE toHostAddr #-} | ||
92 | 87 | ||
93 | instance IPAddress IP where | 88 | instance IPAddress IP where |
94 | toHostAddr (IPv4 ip) = toHostAddr ip | 89 | toHostAddr (IPv4 ip) = toHostAddr ip |
95 | toHostAddr (IPv6 ip) = toHostAddr ip | 90 | toHostAddr (IPv6 ip) = toHostAddr ip |
91 | {-# INLINE toHostAddr #-} | ||
96 | 92 | ||
97 | deriving instance Typeable IP | 93 | deriving instance Typeable IP |
98 | deriving instance Typeable IPv4 | 94 | deriving instance Typeable IPv4 |
@@ -100,6 +96,7 @@ deriving instance Typeable IPv6 | |||
100 | 96 | ||
101 | ipToBEncode :: Show i => i -> BValue | 97 | ipToBEncode :: Show i => i -> BValue |
102 | ipToBEncode ip = BString $ BS8.pack $ show ip | 98 | ipToBEncode ip = BString $ BS8.pack $ show ip |
99 | {-# INLINE ipToBEncode #-} | ||
103 | 100 | ||
104 | ipFromBEncode :: Read a => BValue -> BS.Result a | 101 | ipFromBEncode :: Read a => BValue -> BS.Result a |
105 | ipFromBEncode (BString (BS8.unpack -> ipStr)) | 102 | ipFromBEncode (BString (BS8.unpack -> ipStr)) |
@@ -107,17 +104,25 @@ ipFromBEncode (BString (BS8.unpack -> ipStr)) | |||
107 | | otherwise = decodingError $ "IP: " ++ ipStr | 104 | | otherwise = decodingError $ "IP: " ++ ipStr |
108 | ipFromBEncode _ = decodingError $ "IP: addr should be a bstring" | 105 | ipFromBEncode _ = decodingError $ "IP: addr should be a bstring" |
109 | 106 | ||
107 | instance Ord IP where | ||
108 | |||
110 | instance BEncode IP where | 109 | instance BEncode IP where |
111 | toBEncode = ipToBEncode | 110 | toBEncode = ipToBEncode |
111 | {-# INLINE toBEncode #-} | ||
112 | fromBEncode = ipFromBEncode | 112 | fromBEncode = ipFromBEncode |
113 | {-# INLINE fromBEncode #-} | ||
113 | 114 | ||
114 | instance BEncode IPv4 where | 115 | instance BEncode IPv4 where |
115 | toBEncode = ipToBEncode | 116 | toBEncode = ipToBEncode |
117 | {-# INLINE toBEncode #-} | ||
116 | fromBEncode = ipFromBEncode | 118 | fromBEncode = ipFromBEncode |
119 | {-# INLINE fromBEncode #-} | ||
117 | 120 | ||
118 | instance BEncode IPv6 where | 121 | instance BEncode IPv6 where |
119 | toBEncode = ipToBEncode | 122 | toBEncode = ipToBEncode |
123 | {-# INLINE toBEncode #-} | ||
120 | fromBEncode = ipFromBEncode | 124 | fromBEncode = ipFromBEncode |
125 | {-# INLINE fromBEncode #-} | ||
121 | 126 | ||
122 | instance Serialize IPv4 where | 127 | instance Serialize IPv4 where |
123 | put = putWord32host . toHostAddress | 128 | put = putWord32host . toHostAddress |
@@ -136,9 +141,14 @@ instance Serialize IPv6 where | |||
136 | -- compact list encoding. | 141 | -- compact list encoding. |
137 | data PeerAddr a = PeerAddr | 142 | data PeerAddr a = PeerAddr |
138 | { peerId :: !(Maybe PeerId) | 143 | { peerId :: !(Maybe PeerId) |
144 | |||
145 | -- | This is usually 'IPv4', 'IPv6', 'IP' or unresolved | ||
146 | -- 'HostName'. | ||
139 | , peerHost :: !a | 147 | , peerHost :: !a |
148 | |||
149 | -- | The port the peer listenning for incoming P2P sessions. | ||
140 | , peerPort :: {-# UNPACK #-} !PortNumber | 150 | , peerPort :: {-# UNPACK #-} !PortNumber |
141 | } deriving (Show, Eq, Typeable, Functor) | 151 | } deriving (Show, Eq, Ord, Typeable, Functor) |
142 | 152 | ||
143 | peer_ip_key, peer_id_key, peer_port_key :: BKey | 153 | peer_ip_key, peer_id_key, peer_port_key :: BKey |
144 | peer_ip_key = "ip" | 154 | peer_ip_key = "ip" |
@@ -160,17 +170,6 @@ instance (Typeable a, BEncode a) => BEncode (PeerAddr a) where | |||
160 | where | 170 | where |
161 | peerAddr = flip PeerAddr | 171 | peerAddr = flip PeerAddr |
162 | 172 | ||
163 | mergeIPLists :: [PeerAddr IPv4] -> Maybe [PeerAddr IPv6] -> [PeerAddr IP] | ||
164 | mergeIPLists v4 v6 = (fmap IPv4 `L.map` v4) | ||
165 | ++ (fmap IPv6 `L.map` Data.Foldable.concat v6) | ||
166 | |||
167 | splitIPList :: [PeerAddr IP] -> ([PeerAddr IPv4],[PeerAddr IPv6]) | ||
168 | splitIPList xs = partitionEithers $ toEither <$> xs | ||
169 | where | ||
170 | toEither :: PeerAddr IP -> Either (PeerAddr IPv4) (PeerAddr IPv6) | ||
171 | toEither pa@(PeerAddr _ (IPv4 _) _) = Left (ipv4 <$> pa) | ||
172 | toEither pa@(PeerAddr _ (IPv6 _) _) = Right (ipv6 <$> pa) | ||
173 | |||
174 | -- | The tracker's 'compact peer list' compatible encoding. The | 173 | -- | The tracker's 'compact peer list' compatible encoding. The |
175 | -- 'peerId' is always 'Nothing'. | 174 | -- 'peerId' is always 'Nothing'. |
176 | -- | 175 | -- |
@@ -231,8 +230,8 @@ defaultPorts = [6881..6889] | |||
231 | _resolvePeerAddr :: (IPAddress i) => PeerAddr HostName -> PeerAddr i | 230 | _resolvePeerAddr :: (IPAddress i) => PeerAddr HostName -> PeerAddr i |
232 | _resolvePeerAddr = undefined | 231 | _resolvePeerAddr = undefined |
233 | 232 | ||
234 | -- | Convert peer info from tracker response to socket address. Used | 233 | -- | Convert peer info from tracker or DHT announce query response to |
235 | -- for establish connection between peers. | 234 | -- socket address. Usually used to intiate connection between peers. |
236 | -- | 235 | -- |
237 | peerSockAddr :: PeerAddr IP -> SockAddr | 236 | peerSockAddr :: PeerAddr IP -> SockAddr |
238 | peerSockAddr PeerAddr {..} = | 237 | peerSockAddr PeerAddr {..} = |