diff options
author | Sam T <sta.cs.vsu@gmail.com> | 2013-04-17 11:41:38 +0400 |
---|---|---|
committer | Sam T <sta.cs.vsu@gmail.com> | 2013-04-17 11:41:38 +0400 |
commit | a6041430c5bc3c57e3eddcfb3f44f90db679d0bf (patch) | |
tree | e02438ad5d261bb27d7b7c81615234e0883a40eb /src/Network | |
parent | 5656a5ce55509de371d1b89edf3263fb5fa39e74 (diff) |
+ Add peer addr conversion.
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/Torrent/PeerID.hs | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/src/Network/Torrent/PeerID.hs b/src/Network/Torrent/PeerID.hs index 6298cb87..a3394b36 100644 --- a/src/Network/Torrent/PeerID.hs +++ b/src/Network/Torrent/PeerID.hs | |||
@@ -4,7 +4,11 @@ | |||
4 | -- though this module exports some other goodies for custom generation. | 4 | -- though this module exports some other goodies for custom generation. |
5 | -- | 5 | -- |
6 | module Network.Torrent.PeerID | 6 | module Network.Torrent.PeerID |
7 | ( Peer(..), PeerID (getPeerID) | 7 | ( -- ^ Peer addr |
8 | Peer(..), peerSockAddr | ||
9 | |||
10 | -- ^ Peer identification | ||
11 | , PeerID (getPeerID) | ||
8 | -- * Encoding styles | 12 | -- * Encoding styles |
9 | , azureusStyle, shadowStyle | 13 | , azureusStyle, shadowStyle |
10 | -- * Defaults | 14 | -- * Defaults |
@@ -16,6 +20,8 @@ module Network.Torrent.PeerID | |||
16 | ) where | 20 | ) where |
17 | 21 | ||
18 | import Control.Applicative | 22 | import Control.Applicative |
23 | import Data.Word | ||
24 | import Data.Bits | ||
19 | import Data.BEncode | 25 | import Data.BEncode |
20 | import Data.ByteString (ByteString) | 26 | import Data.ByteString (ByteString) |
21 | import qualified Data.ByteString as B | 27 | import qualified Data.ByteString as B |
@@ -39,12 +45,33 @@ import Network.Socket | |||
39 | version :: Version | 45 | version :: Version |
40 | version = Version [0, 10, 0, 0] [] | 46 | version = Version [0, 10, 0, 0] [] |
41 | 47 | ||
48 | |||
49 | |||
42 | data Peer = Peer { | 50 | data Peer = Peer { |
43 | peerID :: Maybe PeerID | 51 | peerID :: Maybe PeerID |
44 | , peerIP :: HostAddress | 52 | , peerIP :: HostAddress |
45 | , peerPort :: PortNumber | 53 | , peerPort :: PortNumber |
46 | } deriving Show | 54 | } deriving Show |
47 | 55 | ||
56 | -- TODO make platform independent, clarify htonl | ||
57 | -- | Convert peer info from tracker response to socket address. | ||
58 | -- Used for establish connection between peers. | ||
59 | -- | ||
60 | peerSockAddr :: Peer -> SockAddr | ||
61 | peerSockAddr = SockAddrInet <$> (g . peerPort) <*> (htonl . peerIP) | ||
62 | where | ||
63 | htonl :: Word32 -> Word32 | ||
64 | htonl d = | ||
65 | ((d .&. 0xff) `shiftL` 24) .|. | ||
66 | (((d `shiftR` 8 ) .&. 0xff) `shiftL` 16) .|. | ||
67 | (((d `shiftR` 16) .&. 0xff) `shiftL` 8) .|. | ||
68 | ((d `shiftR` 24) .&. 0xff) | ||
69 | |||
70 | g :: PortNumber -> PortNumber | ||
71 | g (PortNum x) = PortNum x -- $ (x `shiftR` 8) .|. (x `shiftL` 8) | ||
72 | |||
73 | |||
74 | |||
48 | -- | Peer identifier is exactly 20 bytes long bytestring. | 75 | -- | Peer identifier is exactly 20 bytes long bytestring. |
49 | newtype PeerID = PeerID { getPeerID :: ByteString } | 76 | newtype PeerID = PeerID { getPeerID :: ByteString } |
50 | deriving (Show, Eq, Ord, BEncodable) | 77 | deriving (Show, Eq, Ord, BEncodable) |