summaryrefslogtreecommitdiff
path: root/standalone/android/haskell-patches/dns_use-android-net.dns1-command-instead-of-resolv.conf.patch
blob: 962a6420756c0e32455160849241b1044d13292f (plain)
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
53
54
55
56
57
58
59
60
61
62
63
From 087f1ae5e17f0e6d7c9f6b4092a5bb5bb6f5bf60 Mon Sep 17 00:00:00 2001
From: dummy <dummy@example.com>
Date: Thu, 16 Oct 2014 02:59:11 +0000
Subject: [PATCH] port

---
 Network/DNS/Resolver.hs | 13 ++++++++-----
 dns.cabal               |  1 +
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/Network/DNS/Resolver.hs b/Network/DNS/Resolver.hs
index 5721e03..c4400d1 100644
--- a/Network/DNS/Resolver.hs
+++ b/Network/DNS/Resolver.hs
@@ -19,7 +19,7 @@ module Network.DNS.Resolver (
   ) where
 
 import Control.Applicative ((<$>), (<*>), pure)
-import Control.Exception (bracket)
+import Control.Exception (bracket, catch, IOException)
 import qualified Data.ByteString.Char8 as BS
 import Data.Char (isSpace)
 import Data.List (isPrefixOf)
@@ -32,6 +32,7 @@ import Network.Socket (AddrInfoFlag(..), AddrInfo(..), defaultHints, getAddrInfo
 import Prelude hiding (lookup)
 import System.Random (getStdRandom, randomR)
 import System.Timeout (timeout)
+import System.Process
 
 #if mingw32_HOST_OS == 1
 import Network.Socket (send)
@@ -130,10 +131,12 @@ makeResolvSeed conf = ResolvSeed <$> addr
   where
     addr = case resolvInfo conf of
         RCHostName numhost -> makeAddrInfo numhost
-        RCFilePath file -> toAddr <$> readFile file >>= makeAddrInfo
-    toAddr cs = let l:_ = filter ("nameserver" `isPrefixOf`) $ lines cs
-                in extract l
-    extract = reverse . dropWhile isSpace . reverse . dropWhile isSpace . drop 11
+        RCFilePath file -> do
+		-- Android has no /etc/resolv.conf; use getprop command.
+		ls <- catch (lines <$> readProcess "getprop" ["net.dns1"] []) (const (return []) :: IOException -> IO [String])
+		makeAddrInfo $ case ls of
+			[] -> "8.8.8.8" -- google public dns as a fallback only
+			(l:_) -> l
 
 makeAddrInfo :: HostName -> IO AddrInfo
 makeAddrInfo addr = do
diff --git a/dns.cabal b/dns.cabal
index ceaf5f4..cd15e61 100644
--- a/dns.cabal
+++ b/dns.cabal
@@ -37,6 +37,7 @@ Library
                       , network >= 2.3
                       , random
                       , resourcet
+                      , process
   else
     Build-Depends:      base >= 4 && < 5
                       , attoparsec
-- 
2.1.1