summaryrefslogtreecommitdiff
path: root/src/Network/BitTorrent/Tracker/RPC/UDP.hs
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2014-02-04 01:27:55 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2014-02-04 01:27:55 +0400
commit4778126b1e7adab5dc1867fbc448df699bd9dae6 (patch)
tree3aa10fce4eb261d7d4dd6324d3b68514ebfa75a9 /src/Network/BitTorrent/Tracker/RPC/UDP.hs
parent967e902b869e7d9f3143df87d4d396e5e611cfd6 (diff)
Refactor UDP tracker host addr resolution
Diffstat (limited to 'src/Network/BitTorrent/Tracker/RPC/UDP.hs')
-rw-r--r--src/Network/BitTorrent/Tracker/RPC/UDP.hs41
1 files changed, 26 insertions, 15 deletions
diff --git a/src/Network/BitTorrent/Tracker/RPC/UDP.hs b/src/Network/BitTorrent/Tracker/RPC/UDP.hs
index a3927c2c..d7b359ed 100644
--- a/src/Network/BitTorrent/Tracker/RPC/UDP.hs
+++ b/src/Network/BitTorrent/Tracker/RPC/UDP.hs
@@ -95,6 +95,28 @@ withManager :: Options -> (Manager -> IO a) -> IO a
95withManager opts = bracket (newManager opts) closeManager 95withManager opts = bracket (newManager opts) closeManager
96 96
97{----------------------------------------------------------------------- 97{-----------------------------------------------------------------------
98-- Host Addr resolution
99-----------------------------------------------------------------------}
100
101setPort :: PortNumber -> SockAddr -> SockAddr
102setPort p (SockAddrInet _ h) = SockAddrInet p h
103setPort p (SockAddrInet6 _ f h s) = SockAddrInet6 p f h s
104setPort _ addr = addr
105
106resolveURI :: URI -> IO SockAddr
107resolveURI URI { uriAuthority = Just (URIAuth {..}) } = do
108 infos <- getAddrInfo Nothing (Just uriRegName) Nothing
109 let port = fromMaybe 0 (readMaybe (L.drop 1 uriPort) :: Maybe Int)
110 case infos of
111 AddrInfo {..} : _ -> return $ setPort (fromIntegral port) addrAddress
112 _ -> fail "getTrackerAddr: unable to lookup host addr"
113resolveURI _ = fail "getTrackerAddr: hostname unknown"
114
115-- TODO caching?
116getTrackerAddr :: Manager -> URI -> IO SockAddr
117getTrackerAddr _ = resolveURI
118
119{-----------------------------------------------------------------------
98 Tokens 120 Tokens
99-----------------------------------------------------------------------} 121-----------------------------------------------------------------------}
100 122
@@ -268,20 +290,6 @@ isExpired Connection {..} = do
268maxPacketSize :: Int 290maxPacketSize :: Int
269maxPacketSize = 98 -- announce request packet 291maxPacketSize = 98 -- announce request packet
270 292
271setPort :: PortNumber -> SockAddr -> SockAddr
272setPort p (SockAddrInet _ h) = SockAddrInet p h
273setPort p (SockAddrInet6 _ f h s) = SockAddrInet6 p f h s
274setPort _ addr = addr
275
276getTrackerAddr :: URI -> IO SockAddr
277getTrackerAddr URI { uriAuthority = Just (URIAuth {..}) } = do
278 infos <- getAddrInfo Nothing (Just uriRegName) Nothing
279 let port = fromMaybe 0 (readMaybe (L.drop 1 uriPort) :: Maybe Int)
280 case infos of
281 AddrInfo {..} : _ -> return $ setPort (fromIntegral port) addrAddress
282 _ -> fail "getTrackerAddr: unable to lookup host addr"
283getTrackerAddr _ = fail "getTrackerAddr: hostname unknown"
284
285call :: Manager -> SockAddr -> ByteString -> IO ByteString 293call :: Manager -> SockAddr -> ByteString -> IO ByteString
286call Manager {..} addr arg = do 294call Manager {..} addr arg = do
287 BS.sendAllTo sock arg addr 295 BS.sendAllTo sock arg addr
@@ -315,7 +323,7 @@ transaction m tracker @ UDPTracker {..} request = do
315 tid <- genTransactionId 323 tid <- genTransactionId
316 let trans = TransactionQ cid tid request 324 let trans = TransactionQ cid tid request
317 325
318 addr <- getTrackerAddr trackerURI 326 addr <- getTrackerAddr m trackerURI
319 res <- call m addr (encode trans) 327 res <- call m addr (encode trans)
320 case decode res of 328 case decode res of
321 Right (TransactionR {..}) 329 Right (TransactionR {..})
@@ -346,6 +354,9 @@ freshConnection m tracker @ UDPTracker {..} = do
346 connId <- connectUDP m tracker 354 connId <- connectUDP m tracker
347 updateConnection connId tracker 355 updateConnection connId tracker
348 356
357getConnection :: Manager -> URI -> IO Connection
358getConnection _ = undefined
359
349announce :: Manager -> AnnounceQuery -> UDPTracker -> IO AnnounceInfo 360announce :: Manager -> AnnounceQuery -> UDPTracker -> IO AnnounceInfo
350announce m ann tracker = do 361announce m ann tracker = do
351 freshConnection m tracker 362 freshConnection m tracker