Skip to content
Permalink
Browse files

Adds ResolverConfigTest (#51)

* Use src/main/resources

* Add initial ResolverConfig tests

* Remove Win95/98/ME winipcfg parsing

* Exclude NameServiceDescriptor service file from JDK9 profile

* Use try-with-resources for findResolvConf

* Revert method addition in Name
  • Loading branch information...
kingle authored and ibauersachs committed Jun 4, 2019
1 parent ebccd5a commit 1f6bcc260372fc79294a579e1e5a419758a16129
@@ -4,4 +4,5 @@
.idea/
*.iml
target/
.DS_Store

27 pom.xml
@@ -193,24 +193,6 @@
</dependencies>

<profiles>
<profile>
<id>spi-up-to-java8</id>
<activation>
<jdk>(,1.8]</jdk>
</activation>
<build>
<resources>
<resource>
<directory>org/xbill/DNS/spi/</directory>
<targetPath>META-INF</targetPath>
<includes>
<include>services/</include>
</includes>
</resource>
</resources>
</build>
</profile>

<profile>
<!-- JDK9 removed the Nameservice SPI as per bug 8134577. There's no replacement. -->
<id>no-spi-on-java9</id>
@@ -242,6 +224,15 @@
</configuration>
</plugin>
</plugins>

<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>META-INF/services/sun.net.spi.nameservice.NameServiceDescriptor</exclude>
</excludes>
</resource>
</resources>
</build>
</profile>
</profiles>
@@ -323,7 +323,7 @@ else if (s.equals("."))

/**
* Create a new name from a constant string. This should only be used when
the name is known to be good - that is, when it is constant.
* the name is known to be good - that is, when it is constant.
* @param s The string to be converted
* @throws IllegalArgumentException The name is invalid.
*/
@@ -4,7 +4,10 @@

import java.io.*;
import java.lang.reflect.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Stream;

/**
* A class that tries to locate name servers and the search path to
@@ -17,7 +20,7 @@
* (which are resolved using Java's built in DNS support).
* <LI>The sun.net.dns.ResolverConfiguration class is queried.
* <LI>On Unix, /etc/resolv.conf is parsed.
* <LI>On Windows, ipconfig/winipcfg is called and its output parsed. This
* <LI>On Windows, ipconfig is called and its output parsed. This
* may fail for non-English versions on Windows.
* <LI>"localhost" is used as the nameserver, and the search path is empty.
* </UL>
@@ -30,9 +33,11 @@
* @author <a href="mailto:yannick@meudal.net">Yannick Meudal</a>
* @author <a href="mailto:arnt@gulbrandsen.priv.no">Arnt Gulbrandsen</a>
*/

public class ResolverConfig {

public static final String DNS_SERVER_PROP = "dns.server";
public static final String DNS_SEARCH_PROP = "dns.search";

private String [] servers = null;
private Name [] searchlist = null;
private int ndots = -1;
@@ -53,12 +58,7 @@
String OS = System.getProperty("os.name");
String vendor = System.getProperty("java.vendor");
if (OS.contains("Windows")) {
if (OS.contains("95") ||
OS.contains("98") ||
OS.contains("ME"))
find95();
else
findNT();
findWin();
} else if (OS.contains("NetWare")) {
findNetware();
} else if (vendor.contains("Android")) {
@@ -129,21 +129,21 @@
* Servers are defined by dns.server=server1,server2...
* The search path is defined by dns.search=domain1,domain2...
*/
private boolean
boolean
findProperty() {
String prop;
List<String> lserver = new ArrayList<>(0);
List<Name> lsearch = new ArrayList<>(0);
StringTokenizer st;

prop = System.getProperty("dns.server");
prop = System.getProperty(DNS_SERVER_PROP);
if (prop != null) {
st = new StringTokenizer(prop, ",");
while (st.hasMoreTokens())
addServer(st.nextToken(), lserver);
}

prop = System.getProperty("dns.search");
prop = System.getProperty(DNS_SEARCH_PROP);
if (prop != null) {
st = new StringTokenizer(prop, ",");
while (st.hasMoreTokens())
@@ -209,21 +209,13 @@
* "nameserver" lines specify servers. "domain" and "search" lines
* define the search path.
*/
private void
findResolvConf(String file) {
InputStream in;
try {
in = new FileInputStream(file);
}
catch (FileNotFoundException e) {
return;
}
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr);
boolean
findResolvConf(InputStream in) {
List<String> lserver = new ArrayList<>(0);
List<Name> lsearch = new ArrayList<>(0);
int lndots = -1;
try {
try (InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader(isr)){
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith("nameserver")) {
@@ -258,29 +250,44 @@ else if(line.startsWith("options")) {
}
}
}
br.close();
}
catch (IOException e) {
return false;
}

if (lserver.isEmpty()) {
return false;
}

configureFromLists(lserver, lsearch);
configureNdots(lndots);
return true;
}

private void
boolean
findUnix() {
findResolvConf("/etc/resolv.conf");
try (InputStream in = Files.newInputStream(Paths.get("/etc/resolv.conf"))) {
return findResolvConf(in);
}
catch (Exception e) {
return false;
}
}

private void
boolean
findNetware() {
findResolvConf("sys:/etc/resolv.cfg");
try (InputStream in = Files.newInputStream(Paths.get("sys:/etc/resolv.cfg"))) {
return findResolvConf(in);
}
catch (Exception e) {
return false;
}
}

/**
* Parses the output of winipcfg or ipconfig.
* Parses the output of ipconfig.
*/
private void
boolean
findWin(InputStream in, Locale locale) {
String packageName = ResolverConfig.class.getPackage().getName();
String resPackageName = packageName + ".windows.DNSServer";
@@ -355,71 +362,62 @@ else if(line.startsWith("options")) {
readingServers = true;
}
}

if (lserver.isEmpty()) {
return false;
}
configureFromLists(lserver, lsearch);
return true;
}
catch (IOException e) {
return false;
}
}

private void
private boolean
findWin(InputStream in) {
boolean found;
String property = "org.xbill.DNS.windows.parse.buffer";
final int defaultBufSize = 8 * 1024;
int bufSize = Integer.getInteger(property, defaultBufSize);
BufferedInputStream b = new BufferedInputStream(in, bufSize);
b.mark(bufSize);
findWin(b, null);
found = findWin(b, null);
if (servers == null) {
try {
b.reset();
}
catch (IOException e) {
return;
return false;
}
findWin(b, new Locale("", ""));
}
}

/**
* Calls winipcfg and parses the result to find servers and a search path.
*/
private void
find95() {
String s = "winipcfg.out";
try {
Process p;
p = Runtime.getRuntime().exec("winipcfg /all /batch " + s);
p.waitFor();
File f = new File(s);
findWin(new FileInputStream(f));
new File(s).delete();
}
catch (Exception e) {
found = findWin(b, new Locale("", ""));
}
return found;
}

/**
* Calls ipconfig and parses the result to find servers and a search path.
*/
private void
findNT() {
boolean
findWin() {
boolean found;
try {
Process p;
p = Runtime.getRuntime().exec("ipconfig /all");
findWin(p.getInputStream());
found = findWin(p.getInputStream());
p.destroy();
}
catch (Exception e) {
return false;
}
return found;
}

/**
* Parses the output of getprop, which is the only way to get DNS
* info on Android. getprop might disappear in future releases, so
* this code comes with a use-by date.
*/
private void
boolean
findAndroid() {
// This originally looked for all lines containing .dns; but
// http://code.google.com/p/android/issues/detail?id=2207#c73
@@ -444,10 +442,14 @@ else if(line.startsWith("options")) {
!lserver.contains(v))
lserver.add(v);
}
} catch ( Exception e ) {
// ignore resolutely
} catch (Exception e) {
return false;
}
if (lserver.isEmpty()) {
return false;
}
configureFromLists(lserver, lsearch);
return true;
}

/** Returns all located servers */

0 comments on commit 1f6bcc2

Please sign in to comment.
You can’t perform that action at this time.