summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
authorSam T <sta.cs.vsu@gmail.com>2013-04-17 11:41:38 +0400
committerSam T <sta.cs.vsu@gmail.com>2013-04-17 11:41:38 +0400
commita6041430c5bc3c57e3eddcfb3f44f90db679d0bf (patch)
treee02438ad5d261bb27d7b7c81615234e0883a40eb /src/Network
parent5656a5ce55509de371d1b89edf3263fb5fa39e74 (diff)
+ Add peer addr conversion.
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/Torrent/PeerID.hs29
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--
6module Network.Torrent.PeerID 6module 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
18import Control.Applicative 22import Control.Applicative
23import Data.Word
24import Data.Bits
19import Data.BEncode 25import Data.BEncode
20import Data.ByteString (ByteString) 26import Data.ByteString (ByteString)
21import qualified Data.ByteString as B 27import qualified Data.ByteString as B
@@ -39,12 +45,33 @@ import Network.Socket
39version :: Version 45version :: Version
40version = Version [0, 10, 0, 0] [] 46version = Version [0, 10, 0, 0] []
41 47
48
49
42data Peer = Peer { 50data 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--
60peerSockAddr :: Peer -> SockAddr
61peerSockAddr = 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.
49newtype PeerID = PeerID { getPeerID :: ByteString } 76newtype PeerID = PeerID { getPeerID :: ByteString }
50 deriving (Show, Eq, Ord, BEncodable) 77 deriving (Show, Eq, Ord, BEncodable)