diff options
author | Daniel Gröber <dxld@darkboxed.org> | 2013-12-25 19:35:43 +0000 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-05 06:44:04 +0400 |
commit | a001d6a9415e61a31b8d0abcddb20ed638fd9916 (patch) | |
tree | 7d9aeacb0c1b3f051005bf69ef6ffa933648edbd /src/Network | |
parent | 66901ca70551105a6eb05c676b0dded8b1af3bfd (diff) |
Use proper address family when creating Socket from (PeerAddr IP)
Diffstat (limited to 'src/Network')
-rw-r--r-- | src/Network/BitTorrent/Core/PeerAddr.hs | 25 | ||||
-rw-r--r-- | src/Network/BitTorrent/Exchange/Wire.hs | 9 |
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 | |||
302 | peerSockAddr :: PeerAddr IP -> SockAddr | 308 | peerSockAddr :: PeerAddr IP -> SockAddr |
303 | peerSockAddr PeerAddr {..} = | 309 | peerSockAddr = 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 | 312 | peerSocket :: SocketType -> PeerAddr IP -> IO Socket |
313 | peerSocket 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. | ||
522 | connectToPeer :: PeerAddr IP -> IO Socket | ||
523 | connectToPeer 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 | -- |
649 | connectWire :: Handshake -> PeerAddr IP -> ExtendedCaps -> Wire () -> IO () | 642 | connectWire :: Handshake -> PeerAddr IP -> ExtendedCaps -> Wire () -> IO () |
650 | connectWire hs addr extCaps wire = | 643 | connectWire 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) [ |