Skip to content

Commit

Permalink
Add support to read /etc/resolv.conf on Unixoid systems
Browse files Browse the repository at this point in the history
  • Loading branch information
mar-v-in committed Feb 23, 2016
1 parent 819b740 commit d72b7f5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions minidns-core/src/main/java/de/measite/minidns/DNSClient.java
Expand Up @@ -14,6 +14,7 @@
import de.measite.minidns.dnsserverlookup.AndroidUsingReflection;
import de.measite.minidns.dnsserverlookup.DNSServerLookupMechanism;
import de.measite.minidns.dnsserverlookup.HardcodedDNSServerAddresses;
import de.measite.minidns.dnsserverlookup.UnixUsingEtcResolvConf;
import de.measite.minidns.record.OPT;

import java.io.IOException;
Expand All @@ -35,6 +36,7 @@ public class DNSClient extends AbstractDNSClient {
addDnsServerLookupMechanism(AndroidUsingExec.INSTANCE);
addDnsServerLookupMechanism(AndroidUsingReflection.INSTANCE);
addDnsServerLookupMechanism(HardcodedDNSServerAddresses.INSTANCE);
addDnsServerLookupMechanism(UnixUsingEtcResolvConf.INSTANCE);
}

private boolean askForDnssec = false;
Expand Down
@@ -0,0 +1,73 @@
/*
* Copyright 2015 the original author or authors
*
* This software is licensed under the Apache License, Version 2.0,
* the GNU Lesser General Public License version 2 or later ("LGPL")
* and the WTFPL.
* You may choose either license to govern your use of this software only
* upon the condition that you accept all of the terms of either
* the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
*/
package de.measite.minidns.dnsserverlookup;

import de.measite.minidns.util.PlatformDetection;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class UnixUsingEtcResolvConf extends AbstractDNSServerLookupMechanism {

public static final DNSServerLookupMechanism INSTANCE = new UnixUsingEtcResolvConf();
public static final int PRIORITY = 2000;

private static final String RESOLV_CONF_FILE = "/etc/resolv.conf";
private static final String NAMESERVER_PREFIX = "nameserver ";

protected UnixUsingEtcResolvConf() {
super(UnixUsingEtcResolvConf.class.getSimpleName(), PRIORITY);
}

@Override
public String[] getDnsServerAddresses() {
if (PlatformDetection.isAndroid()) {
// Don't rely on resolv.conf when on Android
return null;
}

File file = new File(RESOLV_CONF_FILE);
if (!file.exists()) {
// Not very unixoid systems
return null;
}

List<String> servers = new ArrayList<>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
while ((line = reader.readLine()) != null) {
if (line.startsWith(NAMESERVER_PREFIX)) {
servers.add(line.substring(NAMESERVER_PREFIX.length()));
}
}
} catch (IOException e) {
return null;
} finally {
if (reader != null) try {
reader.close();
} catch (IOException ignored) {
// continue
}
}

if (servers.isEmpty()) {
return null;
}
return servers.toArray(new String[servers.size()]);
}
}

0 comments on commit d72b7f5

Please sign in to comment.