summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
authorDaniel Gröber <dxld@darkboxed.org>2013-12-25 19:35:43 +0000
committerSam Truzjan <pxqr.sta@gmail.com>2014-01-05 06:44:04 +0400
commita001d6a9415e61a31b8d0abcddb20ed638fd9916 (patch)
tree7d9aeacb0c1b3f051005bf69ef6ffa933648edbd /src/Network
parent66901ca70551105a6eb05c676b0dded8b1af3bfd (diff)
Use proper address family when creating Socket from (PeerAddr IP)
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/BitTorrent/Core/PeerAddr.hs25
-rw-r--r--src/Network/BitTorrent/Exchange/Wire.hs9
2 files changed, 19 insertions, 15 deletions
diff --git a/src/Network/BitTorrent/Core/PeerAddr.hs b/src/Network/BitTorrent/Core/PeerAddr.hs
index e3e09fd6..63885144 100644
--- a/src/Network/BitTorrent/Core/PeerAddr.hs
+++ b/src/Network/BitTorrent/Core/PeerAddr.hs
@@ -23,6 +23,7 @@ module Network.BitTorrent.Core.PeerAddr
23 PeerAddr(..) 23 PeerAddr(..)
24 , defaultPorts 24 , defaultPorts
25 , peerSockAddr 25 , peerSockAddr
26 , peerSocket
26 27
27 -- * Peer storage 28 -- * Peer storage
28 , PeerStore 29 , PeerStore
@@ -296,14 +297,24 @@ defaultPorts = [6881..6889]
296_resolvePeerAddr :: (IPAddress i) => PeerAddr HostName -> PeerAddr i 297_resolvePeerAddr :: (IPAddress i) => PeerAddr HostName -> PeerAddr i
297_resolvePeerAddr = undefined 298_resolvePeerAddr = undefined
298 299
299-- | Convert peer info from tracker or DHT announce query response to 300_peerSockAddr :: PeerAddr IP -> (Family, SockAddr)
300-- socket address. Usually used to intiate connection between peers. 301_peerSockAddr PeerAddr {..} =
301-- 302 case peerHost of
303 IPv4 ipv4 ->
304 (AF_INET, SockAddrInet peerPort (toHostAddress ipv4))
305 IPv6 ipv6 ->
306 (AF_INET6, SockAddrInet6 peerPort 0 (toHostAddress6 ipv6) 0)
307
302peerSockAddr :: PeerAddr IP -> SockAddr 308peerSockAddr :: PeerAddr IP -> SockAddr
303peerSockAddr PeerAddr {..} = 309peerSockAddr = snd . _peerSockAddr
304 case peerHost of 310
305 IPv4 ipv4 -> SockAddrInet peerPort (toHostAddress ipv4) 311-- | Create a socket connected to the address specified in a peerAddr
306 IPv6 ipv6 -> SockAddrInet6 peerPort 0 (toHostAddress6 ipv6) 0 312peerSocket :: SocketType -> PeerAddr IP -> IO Socket
313peerSocket socketType pa = do
314 let (family, addr) = _peerSockAddr pa
315 sock <- socket family socketType defaultProtocol
316 connect sock addr
317 return sock
307 318
308{----------------------------------------------------------------------- 319{-----------------------------------------------------------------------
309-- Peer storage 320-- Peer storage
diff --git a/src/Network/BitTorrent/Exchange/Wire.hs b/src/Network/BitTorrent/Exchange/Wire.hs
index a3b60b99..31da3f0c 100644
--- a/src/Network/BitTorrent/Exchange/Wire.hs
+++ b/src/Network/BitTorrent/Exchange/Wire.hs
@@ -518,13 +518,6 @@ initiateHandshake sock hs = do
518 sendHandshake sock hs 518 sendHandshake sock hs
519 recvHandshake sock 519 recvHandshake sock
520 520
521-- | Tries to connect to peer using reasonable default parameters.
522connectToPeer :: PeerAddr IP -> IO Socket
523connectToPeer p = do
524 sock <- socket AF_INET Stream Network.Socket.defaultProtocol
525 connect sock (peerSockAddr p)
526 return sock
527
528{----------------------------------------------------------------------- 521{-----------------------------------------------------------------------
529-- Wire 522-- Wire
530-----------------------------------------------------------------------} 523-----------------------------------------------------------------------}
@@ -648,7 +641,7 @@ reconnect = undefined
648-- 641--
649connectWire :: Handshake -> PeerAddr IP -> ExtendedCaps -> Wire () -> IO () 642connectWire :: Handshake -> PeerAddr IP -> ExtendedCaps -> Wire () -> IO ()
650connectWire hs addr extCaps wire = 643connectWire hs addr extCaps wire =
651 bracket (connectToPeer addr) close $ \ sock -> do 644 bracket (peerSocket Stream addr) close $ \ sock -> do
652 hs' <- initiateHandshake sock hs 645 hs' <- initiateHandshake sock hs
653 646
654 Prelude.mapM_ (\(t,e) -> unless t $ throwIO $ ProtocolError e) [ 647 Prelude.mapM_ (\(t,e) -> unless t $ throwIO $ ProtocolError e) [