]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/FuncTorrent/Tracker/Udp.hs
Misc fixes to http tracker.
[functorrent.git] / src / FuncTorrent / Tracker / Udp.hs
index 2437003ec47692f5667e47dba970613823849a8b..37979c494e7840ca96a2a4aeb5945eca8edd53d6 100644 (file)
@@ -27,7 +27,7 @@ import Control.Monad (liftM)
 import Control.Concurrent.MVar (readMVar)
 import Control.Monad.Reader (ReaderT, runReaderT, ask, liftIO)
 import Data.Binary (Binary(..), encode, decode)
-import Data.Binary.Get (Get, isEmpty, getWord32be, getByteString)
+import Data.Binary.Get (Get, isEmpty, getWord32be, getWord64be, getByteString)
 import Data.Binary.Put (putWord16be, putWord64be, putWord32be, putByteString)
 import Data.ByteString (ByteString)
 import qualified Data.ByteString.Char8 as BC
@@ -93,10 +93,10 @@ instance Binary UDPRequest where
     putWord64be (fromIntegral down)
     putWord64be (fromIntegral left)
     putWord64be (fromIntegral up)
-    putWord32be $ fromIntegral (eventToInteger None)
+    putWord32be $ fromIntegral (eventToInteger event)
     putWord32be 0
-    -- key is optional, we will not send it for now
-    putWord32be $ fromIntegral (-1)
+    putWord32be 0
+    putWord32be 10
     putWord16be $ fromIntegral port
   put (ScrapeReq _ _ _) = undefined
   get = undefined
@@ -106,7 +106,7 @@ instance Binary UDPResponse where
   get = do
     a <- getWord32be -- action
     case a of
-      0 -> liftA2 ConnectResp (fromIntegral <$> getWord32be) (fromIntegral <$> getWord32be)
+      0 -> liftA2 ConnectResp (fromIntegral <$> getWord32be) (fromIntegral <$> getWord64be)
       1 -> do
         tid <- fromIntegral <$> getWord32be
         interval' <- fromIntegral <$> getWord32be
@@ -149,7 +149,6 @@ 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) ->
@@ -165,7 +164,6 @@ announceRequest :: Word64 -> ByteString -> String -> Word64 -> Word64 -> Word64
 announceRequest cid infohash peerId up down left port = do
   h <- ask
   tidi <- liftIO randomIO
-  -- connId transId infohash peerId down left up event port)
   let pkt = encode $ AnnounceReq cid tidi infohash peerId down left up None port
   liftIO $ sendRequest h (toStrict pkt)
   return tidi
@@ -195,7 +193,7 @@ getIPPortPairs = do
   if empty
     then return []
     else do
-    ip <- toIP <$> getByteString 6
+    ip <- toIP <$> getByteString 4
     port <- toPort <$> getByteString 2
     ipportpairs <- getIPPortPairs
     return $ (ip, port) : ipportpairs
@@ -206,8 +204,8 @@ startSession host port = do
   addrinfos <- getAddrInfo Nothing (Just host) (Just (show port))
   let (SockAddrInet p ip) = addrAddress $ head addrinfos
   putStrLn "connected to tracker"
-  return UDPTrackerHandle { sock = s
-                            , addr = (SockAddrInet (fromIntegral port) ip) }
+  return UDPTrackerHandle { sock = s
+                          , addr = (SockAddrInet (fromIntegral port) ip) }
   
 closeSession :: UDPTrackerHandle -> IO ()
 closeSession (UDPTrackerHandle s _ _) = close s
@@ -225,7 +223,7 @@ trackerLoop url sport peerId infohash fschan tstate = do
   flip runReaderT handle $ do
     t1 <- connectRequest
     cid <- connectResponse t1
-    liftIO $ print cid
     t2 <- announceRequest cid infohash peerId (fromIntegral up) (fromIntegral down) (fromIntegral (left tstate)) (fromIntegral sport)
     stats <- announceResponse t2
     liftIO $ print stats
+--    _ <- threadDelay $