aboutsummaryrefslogtreecommitdiff
path: root/standalone/android/haskell-patches/dns_use-android-net.dns1-command-instead-of-resolv.conf.patch
blob: f58688edca2fbc3c1b7bd1347320ddf37c56f5ad (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
64
65
66
From 99f349066fc960bfa60b4e369bb21431c87d9b59 Mon Sep 17 00:00:00 2001
From: dummy <dummy@example.com>
Date: Tue, 14 Oct 2014 03:54:57 +0000
Subject: [PATCH] use android net.dns1 command instead of resolv.conf file

Android has no /etc/resolv.conf. Some might have /system/etc/resolv.conf,
but even that does not seem likely.

This is likely a little slow, but is at least fine for git-annex's uses,
since it only uses this library for occasional SRV lookups.
---
 Network/DNS/Resolver.hs |   11 +++++++++--
 dns.cabal               |    1 +
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/Network/DNS/Resolver.hs b/Network/DNS/Resolver.hs
index 9e8342b..4c6c380 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 Data.Char (isSpace)
 import Data.List (isPrefixOf)
 import Data.Maybe (fromMaybe)
@@ -33,6 +33,7 @@ import Network.Socket (AddrInfoFlag(..), AddrInfo(..), SockAddr(..), PortNumber(
 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)
@@ -133,7 +134,13 @@ makeResolvSeed conf = ResolvSeed <$> addr
     addr = case resolvInfo conf of
         RCHostName numhost -> makeAddrInfo numhost Nothing
         RCHostPort numhost mport -> makeAddrInfo numhost $ Just mport
-        RCFilePath file -> toAddr <$> readFile file >>= \i -> makeAddrInfo i Nothing
+        RCFilePath file -> do
+		-- Android has no /etc/resolv.conf; use getprop command.
+		ls <- catch (lines <$> readProcess "getprop" ["net.dns1"] []) (const (return []) :: IOException -> IO [String])
+		let addr = case ls of
+			[] -> "8.8.8.8" -- google public dns as a fallback only
+			(l:_) -> l
+		makeAddrInfo addr Nothing
     toAddr cs = let l:_ = filter ("nameserver" `isPrefixOf`) $ lines cs
                 in extract l
     extract = reverse . dropWhile isSpace . reverse . dropWhile isSpace . drop 11
diff --git a/dns.cabal b/dns.cabal
index fd7d7a3..5ad8a84 100644
--- a/dns.cabal
+++ b/dns.cabal
@@ -38,6 +38,7 @@ Library
                       , network >= 2.3
                       , random
                       , resourcet
+                      , process
   else
     Build-Depends:      base >= 4 && < 5
                       , attoparsec
-- 
1.7.10.4