summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Peer.hs
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-05-03 09:36:02 +0400
committerSam T <pxqr.sta@gmail.com>2013-05-03 09:36:02 +0400
commit6ac50d6e0323094ea0fd6ff37520698b94fa9d41 (patch)
treed3af77c8a496aed6733f070759b9142f59edd85a /src/Network/BitTorrent/Peer.hs
parentf53531b65171e3b64d733aa0ab8c7fed71325464 (diff)
~ Reorganize modules.
All peer location & identification & information stuff should be placed in Network.BitTorrent.Peer now.
Diffstat (limited to 'src/Network/BitTorrent/Peer.hs')
-rw-r--r--src/Network/BitTorrent/Peer.hs52
1 files changed, 6 insertions, 46 deletions
diff --git a/src/Network/BitTorrent/Peer.hs b/src/Network/BitTorrent/Peer.hs
index e16329d2..ff32feb5 100644
--- a/src/Network/BitTorrent/Peer.hs
+++ b/src/Network/BitTorrent/Peer.hs
@@ -5,52 +5,12 @@
5-- Stability : experimental 5-- Stability : experimental
6-- Portability : non-portable 6-- Portability : non-portable
7-- 7--
8-- Just convenient reexports for peer related modules.
9--
8module Network.BitTorrent.Peer 10module Network.BitTorrent.Peer
9 ( Peer(..) 11 ( module P
10 , peerSockAddr, connectToPeer
11 , ppPeer
12 ) where 12 ) where
13 13
14import Control.Applicative 14import Network.BitTorrent.Peer.Addr as P
15import Data.Word 15import Network.BitTorrent.Peer.ClientInfo as P
16import Data.Bits 16import Network.BitTorrent.Peer.ID as P \ No newline at end of file
17import Network
18import Network.Socket
19
20import Network.BitTorrent.PeerID
21import Network.BitTorrent.PeerWire.ClientInfo
22
23
24data Peer = Peer {
25 peerID :: Maybe PeerID
26 , peerIP :: HostAddress
27 , peerPort :: PortNumber
28 } deriving Show
29
30-- TODO make platform independent, clarify htonl
31-- | Convert peer info from tracker response to socket address.
32-- Used for establish connection between peers.
33--
34peerSockAddr :: Peer -> SockAddr
35peerSockAddr = SockAddrInet <$> (g . peerPort) <*> (htonl . peerIP)
36 where
37 htonl :: Word32 -> Word32
38 htonl d =
39 ((d .&. 0xff) `shiftL` 24) .|.
40 (((d `shiftR` 8 ) .&. 0xff) `shiftL` 16) .|.
41 (((d `shiftR` 16) .&. 0xff) `shiftL` 8) .|.
42 ((d `shiftR` 24) .&. 0xff)
43
44 g :: PortNumber -> PortNumber
45 g = id
46
47-- | Tries to connect to peer using reasonable default parameters.
48connectToPeer :: Peer -> IO Socket
49connectToPeer p = do
50 sock <- socket AF_INET Stream Network.Socket.defaultProtocol
51 connect sock (peerSockAddr p)
52 return sock
53
54ppPeer :: Peer -> String
55ppPeer p = maybe "" (++ " at ") ((ppClientInfo . clientInfo) <$> peerID p)
56 ++ show (peerSockAddr p)