File: Internal.hs

package info (click to toggle)
haskell-dns 4.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 380 kB
  • sloc: haskell: 3,298; ansic: 46; makefile: 2
file content (52 lines) | stat: -rw-r--r-- 1,287 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
{-# LANGUAGE CPP #-}

module Network.DNS.Resolver.Internal (
      getDefaultDnsServers
    ) where

import Network.DNS.Imports

#if !defined(mingw32_HOST_OS)
#define POSIX
#else
#define WIN
#endif

#if defined(WIN)
import Foreign.C.String
import Foreign.Marshal.Alloc (allocaBytes)
#else
import Data.Char (isSpace)
#endif

getDefaultDnsServers :: FilePath -> IO [String]

#if defined(WIN)

foreign import ccall "getWindowsDefDnsServers" getWindowsDefDnsServers :: CString -> Int -> IO Word32

getDefaultDnsServers _ = do
  allocaBytes 256 $ \cString -> do
     res <- getWindowsDefDnsServers cString 256
     case res of
       0 -> split ',' <$> peekCString cString
       _ -> return [] -- TODO: Do proper error handling here.
  where
    split :: Char -> String -> [String]
    split c cs =
        let (h, t) = dropWhile (== c) <$> break (== c) cs
         in if null t
            then if null h then [] else [h]
            else if null h
            then split c t
            else h : split c t

#else

getDefaultDnsServers file = toAddresses <$> readFile file
  where
    toAddresses :: String -> [String]
    toAddresses cs = map extract (filter ("nameserver" `isPrefixOf`) (lines cs))
    extract = reverse . dropWhile isSpace . reverse . dropWhile isSpace . drop 11

#endif