1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
-- |
-- Module : Network.BitTorrent.Core.PeerAddr
-- Copyright : (c) Sam Truzjan 2013
-- (c) Daniel Gröber 2013
-- License : BSD3
-- Maintainer : pxqr.sta@gmail.com
-- Stability : provisional
-- Portability : portable
--
-- 'PeerAddr' is used to represent peer address. Currently it's
-- just peer IP and peer port but this might change in future.
--
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS -fno-warn-orphans #-} -- for PortNumber instances
module Network.BitTorrent.Core.PeerAddr
( -- * Peer address
PeerAddr(..)
, defaultPorts
, peerSockAddr
-- * Peer storage
, PeerStore
, Network.BitTorrent.Core.PeerAddr.lookup
, Network.BitTorrent.Core.PeerAddr.insert
) where
import Control.Applicative
import Control.Monad
import Data.Aeson (ToJSON, FromJSON)
import Data.BEncode as BS
import Data.BEncode.BDict (BKey)
import Data.ByteString.Char8 as BS8
import Data.Char
import Data.Default
import Data.Hashable
import Data.HashMap.Strict as HM
import Data.IP
import Data.List as L
import Data.List.Split
import Data.Maybe
import Data.Monoid
import Data.Serialize as S
import Data.String
import Data.Typeable
import Data.Word
import Network.Socket
import Text.PrettyPrint as PP hiding ((<>))
import Text.PrettyPrint.Class
import Text.Read (readMaybe)
import qualified Text.ParserCombinators.ReadP as RP
import Data.Torrent.InfoHash
import Network.BitTorrent.Core.PeerId
{-----------------------------------------------------------------------
-- Port number
-----------------------------------------------------------------------}
deriving instance ToJSON PortNumber
deriving instance FromJSON PortNumber
instance BEncode PortNumber where
toBEncode = toBEncode . fromEnum
fromBEncode = fromBEncode >=> portNumber
where
portNumber :: Integer -> BS.Result PortNumber
portNumber n
| 0 <= n && n <= fromIntegral (maxBound :: Word16)
= pure $ fromIntegral n
| otherwise = decodingError $ "PortNumber: " ++ show n
instance Serialize PortNumber where
get = fromIntegral <$> getWord16be
{-# INLINE get #-}
put = putWord16be . fromIntegral
{-# INLINE put #-}
instance Hashable PortNumber where
hashWithSalt s = hashWithSalt s . fromEnum
{-# INLINE hashWithSalt #-}
instance Pretty PortNumber where
pretty = PP.int . fromEnum
{-# INLINE pretty #-}
{-----------------------------------------------------------------------
-- IP addr
-----------------------------------------------------------------------}
class IPAddress i where
toHostAddr :: i -> Either HostAddress HostAddress6
instance IPAddress IPv4 where
toHostAddr = Left . toHostAddress
{-# INLINE toHostAddr #-}
instance IPAddress IPv6 where
toHostAddr = Right . toHostAddress6
{-# INLINE toHostAddr #-}
instance IPAddress IP where
toHostAddr (IPv4 ip) = toHostAddr ip
toHostAddr (IPv6 ip) = toHostAddr ip
{-# INLINE toHostAddr #-}
deriving instance Typeable IP
deriving instance Typeable IPv4
deriving instance Typeable IPv6
ipToBEncode :: Show i => i -> BValue
ipToBEncode ip = BString $ BS8.pack $ show ip
{-# INLINE ipToBEncode #-}
ipFromBEncode :: Read a => BValue -> BS.Result a
ipFromBEncode (BString (BS8.unpack -> ipStr))
| Just ip <- readMaybe (ipStr) = pure ip
| otherwise = decodingError $ "IP: " ++ ipStr
ipFromBEncode _ = decodingError $ "IP: addr should be a bstring"
instance BEncode IP where
toBEncode = ipToBEncode
{-# INLINE toBEncode #-}
fromBEncode = ipFromBEncode
{-# INLINE fromBEncode #-}
instance BEncode IPv4 where
toBEncode = ipToBEncode
{-# INLINE toBEncode #-}
fromBEncode = ipFromBEncode
{-# INLINE fromBEncode #-}
instance BEncode IPv6 where
toBEncode = ipToBEncode
{-# INLINE toBEncode #-}
fromBEncode = ipFromBEncode
{-# INLINE fromBEncode #-}
-- | When 'get'ing an IP it must be 'isolate'd to the appropriate
-- number of bytes since we have no other way of telling which
-- address type we are trying to parse
instance Serialize IP where
put (IPv4 ip) = put ip
put (IPv6 ip) = put ip
get = do
n <- remaining
case n of
4 -> IPv4 <$> get
16 -> IPv6 <$> get
_ -> fail "Wrong number of bytes remaining to parse IP"
instance Serialize IPv4 where
put = putWord32host . toHostAddress
get = fromHostAddress <$> getWord32host
instance Serialize IPv6 where
put ip = put $ toHostAddress6 ip
get = fromHostAddress6 <$> get
instance Pretty IPv4 where
pretty = PP.text . show
{-# INLINE pretty #-}
instance Pretty IPv6 where
pretty = PP.text . show
{-# INLINE pretty #-}
instance Pretty IP where
pretty = PP.text . show
{-# INLINE pretty #-}
{-----------------------------------------------------------------------
-- Peer addr
-----------------------------------------------------------------------}
-- TODO check semantic of ord and eq instances
-- | Peer address info normally extracted from peer list or peer
-- compact list encoding.
data PeerAddr a = PeerAddr
{ peerId :: !(Maybe PeerId)
-- | This is usually 'IPv4', 'IPv6', 'IP' or unresolved
-- 'HostName'.
, peerHost :: !a
-- | The port the peer listenning for incoming P2P sessions.
, peerPort :: {-# UNPACK #-} !PortNumber
} deriving (Show, Eq, Ord, Typeable, Functor)
peer_ip_key, peer_id_key, peer_port_key :: BKey
peer_ip_key = "ip"
peer_id_key = "peer id"
peer_port_key = "port"
-- | The tracker's 'announce response' compatible encoding.
instance (Typeable a, BEncode a) => BEncode (PeerAddr a) where
toBEncode PeerAddr {..} = toDict $
peer_ip_key .=! peerHost
.: peer_id_key .=? peerId
.: peer_port_key .=! peerPort
.: endDict
fromBEncode = fromDict $ do
peerAddr <$>! peer_ip_key
<*>? peer_id_key
<*>! peer_port_key
where
peerAddr = flip PeerAddr
-- | The tracker's 'compact peer list' compatible encoding. The
-- 'peerId' is always 'Nothing'.
--
-- For more info see: <http://www.bittorrent.org/beps/bep_0023.html>
--
-- TODO: test byte order
instance (Serialize a) => Serialize (PeerAddr a) where
put PeerAddr {..} = put peerHost >> put peerPort
get = PeerAddr Nothing <$> get <*> get
-- | @127.0.0.1:6881@
instance Default (PeerAddr IPv4) where
def = "127.0.0.1:6881"
-- | Example:
--
-- @peerPort \"127.0.0.1:6881\" == 6881@
--
instance IsString (PeerAddr IPv4) where
fromString str
| [hostAddrStr, portStr] <- splitWhen (== ':') str
, Just hostAddr <- readMaybe hostAddrStr
, Just portNum <- toEnum <$> readMaybe portStr
= PeerAddr Nothing hostAddr portNum
| otherwise = error $ "fromString: unable to parse (PeerAddr IPv4): " ++ str
readsIPv6_port :: String -> [((IPv6, PortNumber), String)]
readsIPv6_port = RP.readP_to_S $ do
ip <- RP.char '[' *> (RP.readS_to_P reads) <* RP.char ']'
_ <- RP.char ':'
port <- toEnum <$> read <$> (RP.many1 $ RP.satisfy isDigit) <* RP.eof
return (ip,port)
instance IsString (PeerAddr IPv6) where
fromString str
| [((ip,port),"")] <- readsIPv6_port str =
PeerAddr Nothing ip port
| otherwise = error $ "fromString: unable to parse (PeerAddr IPv6): " ++ str
instance IsString (PeerAddr IP) where
fromString str
| '[' `L.elem` str = IPv6 <$> fromString str
| otherwise = IPv4 <$> fromString str
-- | fingerprint + "at" + dotted.host.inet.addr:port
-- TODO: instances for IPv6, HostName
instance Pretty a => Pretty (PeerAddr a) where
pretty PeerAddr {..}
| Just pid <- peerId = pretty (fingerprint pid) <+> "at" <+> paddr
| otherwise = paddr
where
paddr = pretty peerHost <> ":" <> text (show peerPort)
-- | Ports typically reserved for bittorrent P2P listener.
defaultPorts :: [PortNumber]
defaultPorts = [6881..6889]
_resolvePeerAddr :: (IPAddress i) => PeerAddr HostName -> PeerAddr i
_resolvePeerAddr = undefined
-- | Convert peer info from tracker or DHT announce query response to
-- socket address. Usually used to intiate connection between peers.
--
peerSockAddr :: PeerAddr IP -> SockAddr
peerSockAddr PeerAddr {..} =
case peerHost of
IPv4 ipv4 -> SockAddrInet peerPort (toHostAddress ipv4)
IPv6 ipv6 -> SockAddrInet6 peerPort 0 (toHostAddress6 ipv6) 0
{-----------------------------------------------------------------------
-- Peer storage
-----------------------------------------------------------------------}
-- TODO use more memory efficient representation
-- | Storage used to keep track a set of known peers in client,
-- tracker or DHT sessions.
newtype PeerStore ip = PeerStore (HashMap InfoHash [PeerAddr ip])
-- | Empty store.
instance Default (PeerStore a) where
def = PeerStore HM.empty
{-# INLINE def #-}
-- | Monoid under union operation.
instance Eq a => Monoid (PeerStore a) where
mempty = def
{-# INLINE mempty #-}
mappend (PeerStore a) (PeerStore b) =
PeerStore (HM.unionWith L.union a b)
{-# INLINE mappend #-}
-- | Can be used to store peers between invocations of the client
-- software.
instance Serialize (PeerStore a) where
get = undefined
put = undefined
-- | Used in 'get_peers' DHT queries.
lookup :: InfoHash -> PeerStore a -> [PeerAddr a]
lookup ih (PeerStore m) = fromMaybe [] $ HM.lookup ih m
-- | Used in 'announce_peer' DHT queries.
insert :: Eq a => InfoHash -> PeerAddr a -> PeerStore a -> PeerStore a
insert ih a (PeerStore m) = PeerStore (HM.insertWith L.union ih [a] m)
|