From 81cf5cc870a10b17a3dad34543b91290429a43aa Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan Date: Mon, 13 Jun 2016 21:28:31 +0530 Subject: [PATCH] UDP tracker: refactoring, now connect succeeds --- src/FuncTorrent/Tracker.hs | 26 ++------------------------ src/FuncTorrent/Tracker/Udp.hs | 21 ++++++++++++--------- src/FuncTorrent/Utils.hs | 8 ++++++++ 3 files changed, 22 insertions(+), 33 deletions(-) diff --git a/src/FuncTorrent/Tracker.hs b/src/FuncTorrent/Tracker.hs index 7ad3997..39ed298 100644 --- a/src/FuncTorrent/Tracker.hs +++ b/src/FuncTorrent/Tracker.hs @@ -36,37 +36,15 @@ import Network (PortNumber) import qualified FuncTorrent.Tracker.Http as HT (trackerLoop) import qualified FuncTorrent.Tracker.Udp as UT (trackerLoop) import FuncTorrent.Tracker.Types (TState(..), TrackerEventState(..), TrackerProtocol(..), TrackerMsg(..)) -import FuncTorrent.Utils (Port, toPort) +import FuncTorrent.Utils (Port, toPort, getHostname) import qualified FuncTorrent.FileSystem as FS (MsgChannel) import FuncTorrent.Peer (Peer) type MsgChannel = Chan TrackerMsg -data TrackerUrl = TrackerUrl { protocol :: TrackerProtocol - , host :: String - , port :: Port - , path :: String - } - newTracker :: IO MsgChannel newTracker = newChan -parseUrl :: String -> TrackerUrl -parseUrl url = TrackerUrl proto host port path - where proto = getTrackerType url - host = getHostname url - port = getPort url - path = getPath url - -getHostname :: String -> String -getHostname url = takeWhile (/= ':') $ drop 2 $ dropWhile (/= '/') url - -getPort :: String -> Port -getPort url = toPort . pack $ takeWhile (/= '/') $ drop 1 $ dropWhile (/= ':') $ drop 2 $ dropWhile (/= '/') url - -getPath :: String -> String -getPath url = dropWhile (/= '/') $ dropWhile (/= ':') $ drop 1 $ dropWhile (/= ':') url - runTracker :: MsgChannel -> FS.MsgChannel -> ByteString -> PortNumber -> String -> [String] -> Integer -> IO () runTracker msgChannel fsChan infohash port peerId announceList sz = do @@ -82,7 +60,7 @@ runTracker msgChannel fsChan infohash port peerId announceList sz = do runStateT (msgHandler msgChannel) initialTState return () Udp -> do - _ <- forkIO $ UT.trackerLoop host (fromIntegral port) peerId infohash fsChan initialTState + _ <- forkIO $ UT.trackerLoop turl (fromIntegral port) peerId infohash fsChan initialTState return () _ -> error "Tracker Protocol unimplemented" diff --git a/src/FuncTorrent/Tracker/Udp.hs b/src/FuncTorrent/Tracker/Udp.hs index 4e541a9..f2198a3 100644 --- a/src/FuncTorrent/Tracker/Udp.hs +++ b/src/FuncTorrent/Tracker/Udp.hs @@ -33,14 +33,13 @@ import Data.ByteString (ByteString) import qualified Data.ByteString.Char8 as BC import Data.ByteString.Lazy (fromStrict, toStrict) import Data.Word (Word16, Word32, Word64) -import Network.Socket (Socket, Family( AF_INET ), SocketType( Datagram ), defaultProtocol, SockAddr(..), socket, inet_addr, close, getAddrInfo, addrAddress, SockAddr(..)) +import Network.Socket (Socket, Family( AF_INET ), SocketType( Datagram ), defaultProtocol, SockAddr(..), socket, close, getAddrInfo, addrAddress, SockAddr(..)) import Network.Socket.ByteString (sendTo, recvFrom) import System.Random (randomIO) -import FuncTorrent.Tracker.Types (TrackerEventState(..)) -import FuncTorrent.Utils (IP, Port, toIP, toPort) +import FuncTorrent.Tracker.Types (TrackerEventState(..), TState(..)) +import FuncTorrent.Utils (IP, Port, toIP, toPort, getHostname, getPort) import qualified FuncTorrent.FileSystem as FS (MsgChannel, Stats(..), getStats) -import FuncTorrent.Tracker.Types(TState(..)) -- UDP tracker: http://bittorrent.org/beps/bep_0015.html data Action = Connect @@ -150,6 +149,7 @@ connectResponse :: Word32 -> ReaderT UDPTrackerHandle IO Word64 connectResponse tid = do h <- ask resp <- liftIO $ recvResponse h + liftIO $ print resp -- check if nbytes is at least 16 bytes long case resp of (ConnectResp tidr cid) -> @@ -205,24 +205,27 @@ startSession host port = do s <- socket AF_INET Datagram defaultProtocol addrinfos <- getAddrInfo Nothing (Just host) (Just (show port)) let (SockAddrInet p ip) = addrAddress $ head addrinfos - hostAddr <- inet_addr (show ip) putStrLn "connected to tracker" return $ UDPTrackerHandle { sock = s - , addr = (SockAddrInet (fromIntegral port) hostAddr) } + , addr = (SockAddrInet (fromIntegral port) ip) } closeSession :: UDPTrackerHandle -> IO () closeSession (UDPTrackerHandle s _ _) = close s trackerLoop :: String -> Port -> String -> ByteString -> FS.MsgChannel -> TState -> IO () -trackerLoop host port peerId infohash fschan tstate = do +trackerLoop url sport peerId infohash fschan tstate = do st' <- FS.getStats fschan st <- readMVar st' let up = FS.bytesRead st down = FS.bytesWritten st - handle <- startSession host 2710 + port = getPort url + host = getHostname url + putStrLn $ "host = " ++ (show host) ++ " port= " ++ (show port) + handle <- startSession host port flip runReaderT handle $ do t1 <- connectRequest cid <- connectResponse t1 - t2 <- announceRequest cid infohash (fromIntegral up) (fromIntegral down) (fromIntegral (left tstate)) (fromIntegral port) + liftIO $ print cid + t2 <- announceRequest cid infohash (fromIntegral up) (fromIntegral down) (fromIntegral (left tstate)) (fromIntegral sport) stats <- announceResponse t2 liftIO $ print stats diff --git a/src/FuncTorrent/Utils.hs b/src/FuncTorrent/Utils.hs index fe9b423..be4e15b 100644 --- a/src/FuncTorrent/Utils.hs +++ b/src/FuncTorrent/Utils.hs @@ -28,6 +28,8 @@ module FuncTorrent.Utils , Port , toIP , toPort + , getHostname + , getPort ) where @@ -88,3 +90,9 @@ toIP = Data.List.intercalate "." . toInt :: String -> Integer toInt = read + +getHostname :: String -> String +getHostname url = takeWhile (/= ':') $ drop 2 $ dropWhile (/= '/') url + +getPort :: String -> Port +getPort url = read $ takeWhile (/= '/') $ drop 1 $ dropWhile (/= ':') $ drop 2 $ dropWhile (/= '/') url -- 2.45.2