turn on hash verification of every downloaded piece
authorRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Fri, 24 Jul 2015 18:54:24 +0000 (00:24 +0530)
committerRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Fri, 24 Jul 2015 18:54:24 +0000 (00:24 +0530)
src/FuncTorrent/Peer.hs

index b193e43633a4c34ed246fc146a41316053057d61..0bce055a9327b5c4ad8cbc658e82510fbf509acf 100644 (file)
@@ -214,13 +214,14 @@ msgLoop pState pieceStatus | meInterested pState == False &&
                                Nothing -> putStrLn "Nothing to download"
                                Just workPiece -> do
                                  let pLen = len (pieceStatus ! workPiece)
+                                 putStrLn $ "piece length = " ++ show pLen
                                  pBS <- downloadPiece (handle pState) workPiece pLen
-                                 -- if not $ verifyHash pBS (hash (pieceStatus ! workPiece))
-                                 --  then
-                                 --  putStrLn $ "Hash mismatch: " ++ show (hash (pieceStatus ! workPiece)) ++ " vs " ++ show (take 20 (SHA1.hash pBS))
-                                 --  else do
-                                 writeFileAtOffset "/tmp/download.file" (workPiece * pLen) pBS
-                                 msgLoop pState (adjust (\pieceData -> pieceData { state = Have }) workPiece pieceStatus)
+                                 if not $ verifyHash pBS (hash (pieceStatus ! workPiece))
+                                   then
+                                   putStrLn $ "Hash mismatch: " ++ show (hash (pieceStatus ! workPiece)) ++ " vs " ++ show (take 20 (SHA1.hash pBS))
+                                   else do
+                                   writeFileAtOffset "/tmp/download.file" (workPiece * pLen) pBS
+                                   msgLoop pState (adjust (\pieceData -> pieceData { state = Have }) workPiece pieceStatus)
                           | otherwise = do
                               msg <- getMsg (handle pState)
                               putStrLn $ "<-- " ++ show msg ++ "from peer: " ++ show (peer pState)