]> git.rkrishnan.org Git - functorrent.git/blob - src/FuncTorrent/Utils.hs
WIP: UDP tracker, compiles
[functorrent.git] / src / FuncTorrent / Utils.hs
1 {-
2  - Copyright (C) 2015-2016 Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
3  -
4  - This file is part of FuncTorrent.
5  -
6  - FuncTorrent is free software; you can redistribute it and/or modify
7  - it under the terms of the GNU General Public License as published by
8  - the Free Software Foundation; either version 3 of the License, or
9  - (at your option) any later version.
10  -
11  - FuncTorrent is distributed in the hope that it will be useful,
12  - but WITHOUT ANY WARRANTY; without even the implied warranty of
13  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  - GNU General Public License for more details.
15  -
16  - You should have received a copy of the GNU General Public License
17  - along with FuncTorrent; if not,  see <http://www.gnu.org/licenses/>
18  -}
19
20 module FuncTorrent.Utils
21        ( createDummyFile
22        , writeFileAtOffset
23        , readFileAtOffset
24        , splitNum
25        , splitN
26        , verifyHash
27        , IP
28        , Port
29        , toIP
30        , toPort
31        )
32        where
33
34 import Prelude hiding (writeFile, take)
35
36 import qualified Crypto.Hash.SHA1 as SHA1 (hash)
37 import Control.Exception.Base (IOException, try)
38 import Data.ByteString (ByteString, writeFile, hPut, hGet, take)
39 import qualified Data.ByteString.Base16 as B16 (encode)
40 import qualified Data.ByteString.Char8 as BC
41 import Data.List (intercalate)
42 import System.IO (Handle, hSeek, SeekMode(..))
43 import System.Directory (doesFileExist)
44
45 type IP = String
46 type Port = Integer
47
48 splitN :: Int -> BC.ByteString -> [BC.ByteString]
49 splitN n bs | BC.null bs = []
50             | otherwise = BC.take n bs : splitN n (BC.drop n bs)
51
52 splitNum :: Integer -> Integer -> [Integer]
53 splitNum n d | n == 0 = []
54              | n < d = [n]
55              | otherwise = d : splitNum (n - d) d
56
57 createDummyFile :: FilePath -> Int -> IO (Either IOException ())
58 createDummyFile path size = do
59   dfe <- doesFileExist path
60   if not dfe
61     then do
62     try $ writeFile path (BC.replicate size '\0')
63     else
64     return $ Right ()
65
66 -- write into a file at a specific offet
67 writeFileAtOffset :: Handle -> Integer -> ByteString -> IO ()
68 writeFileAtOffset h offset block = do
69   hSeek h AbsoluteSeek offset
70   hPut h block
71
72 readFileAtOffset :: Handle -> Integer -> Integer -> IO ByteString
73 readFileAtOffset h offset len = do
74   hSeek h AbsoluteSeek offset
75   hGet h (fromInteger len)
76
77 verifyHash :: ByteString -> ByteString -> Bool
78 verifyHash bs pieceHash =
79   take 20 (SHA1.hash bs) == pieceHash
80
81 toPort :: ByteString -> Port
82 toPort = read . ("0x" ++) . BC.unpack . B16.encode
83
84 toIP :: ByteString -> IP
85 toIP = Data.List.intercalate "." .
86        map (show . toInt . ("0x" ++) . BC.unpack) .
87        splitN 2 . B16.encode
88
89 toInt :: String -> Integer
90 toInt = read