]> git.rkrishnan.org Git - functorrent.git/commitdiff
UDP tracker: refactoring, now connect succeeds
authorRamakrishnan Muthukrishnan <ram@acano.com>
Mon, 13 Jun 2016 15:58:31 +0000 (21:28 +0530)
committerRamakrishnan Muthukrishnan <ram@acano.com>
Mon, 13 Jun 2016 15:58:31 +0000 (21:28 +0530)
src/FuncTorrent/Tracker.hs
src/FuncTorrent/Tracker/Udp.hs
src/FuncTorrent/Utils.hs

index 7ad3997ffe1010ef6c4a7ee4c197119072d2b2ff..39ed2982f51bed44181ce73fca3eb7217330c4f9 100644 (file)
@@ -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 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
 
 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
 
 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
 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
       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"
       return ()
     _ ->
       error "Tracker Protocol unimplemented"
index 4e541a932b2e17c5c20a8cb20cc48b7065689889..f2198a3361b51351051be7de46112c3428b38c1e 100644 (file)
@@ -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 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 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 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
 
 -- 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
 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) ->
   -- 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
   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
   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 ()
   
 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
   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
   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
     stats <- announceResponse t2
     liftIO $ print stats
index fe9b423eeff6f53542b1712339e595c089c298f6..be4e15b55ac9ec518b14a3ab8e0933a85bc3ea28 100644 (file)
@@ -28,6 +28,8 @@ module FuncTorrent.Utils
        , Port
        , toIP
        , toPort
        , Port
        , toIP
        , toPort
+       , getHostname
+       , getPort
        )
        where
 
        )
        where
 
@@ -88,3 +90,9 @@ toIP = Data.List.intercalate "." .
 
 toInt :: String -> Integer
 toInt = read
 
 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