Skip to content

Commit

Permalink
Merge branch 'v0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowdalic committed Jan 17, 2018
2 parents bb05614 + 1a649f8 commit 0dba448
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
MiniDNS
=======

[![Build Status](https://travis-ci.org/rtreffer/minidns.svg)](https://travis-ci.org/rtreffer/minidns) [![Coverage Status](https://coveralls.io/repos/rtreffer/minidns/badge.svg)](https://coveralls.io/r/rtreffer/minidns)
[![Build Status](https://travis-ci.org/MiniDNS/minidns.svg)](https://travis-ci.org/MiniDNS/minidns) [![Coverage Status](https://coveralls.io/repos/MiniDNS/minidns/badge.svg)](https://coveralls.io/r/MiniDNS/minidns)

MiniDNS is a minimal DNS client client library for Android and Java SE. It can parse resource records (A, AAAA, NS, SRV, …) and is easy to use and extend. MiniDNS aims to be secure, modular, leightweight and as simple as possible. It also provides experimental support for DNSSEC and DANE, and is thus the ideal resolver if you want to bring DNSSEC close to your application.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,17 @@

import de.measite.minidns.util.PlatformDetection;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;

/**
Expand All @@ -41,38 +44,7 @@ public List<String> getDnsServerAddresses() {
InputStream inputStream = process.getInputStream();
LineNumberReader lnr = new LineNumberReader(
new InputStreamReader(inputStream));
String line = null;
HashSet<String> server = new HashSet<String>(6);
while ((line = lnr.readLine()) != null) {
int split = line.indexOf("]: [");
if (split == -1) {
continue;
}
String property = line.substring(1, split);
String value = line.substring(split + 4, line.length() - 1);

if (value.isEmpty()) {
continue;
}

if (property.endsWith(".dns") || property.endsWith(".dns1") ||
property.endsWith(".dns2") || property.endsWith(".dns3") ||
property.endsWith(".dns4")) {

// normalize the address

InetAddress ip = InetAddress.getByName(value);

if (ip == null) continue;

value = ip.getHostAddress();

if (value == null) continue;
if (value.length() == 0) continue;

server.add(value);
}
}
Set<String> server = parseProps(lnr, true);
if (server.size() > 0) {
List<String> res = new ArrayList<>(server.size());
res.addAll(server);
Expand All @@ -89,4 +61,54 @@ public boolean isAvailable() {
return PlatformDetection.isAndroid();
}

private static final String PROP_DELIM = "]: [";
protected static Set<String> parseProps(BufferedReader lnr, boolean logWarning) throws UnknownHostException, IOException {
String line = null;
Set<String> server = new HashSet<String>(6);

while ((line = lnr.readLine()) != null) {
int split = line.indexOf(PROP_DELIM);
if (split == -1) {
continue;
}
String property = line.substring(1, split);

int valueStart = split + PROP_DELIM.length();
int valueEnd = line.length() - 1;
if (valueEnd < valueStart) {
// This can happen if a newline sneaks in as the first character of the property value. For example
// "[propName]: [\n…]".
if (logWarning) {
LOGGER.warning("Malformed property detected: \"" + line + '"');
}
continue;
}

String value = line.substring(valueStart, valueEnd);

if (value.isEmpty()) {
continue;
}

if (property.endsWith(".dns") || property.endsWith(".dns1") ||
property.endsWith(".dns2") || property.endsWith(".dns3") ||
property.endsWith(".dns4")) {

// normalize the address

InetAddress ip = InetAddress.getByName(value);

if (ip == null) continue;

value = ip.getHostAddress();

if (value == null) continue;
if (value.length() == 0) continue;

server.add(value);
}
}

return server;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2015-2018 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 static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.net.UnknownHostException;
import java.util.Set;

import org.junit.Test;

public class AndroidUsingExecTest {

private static final String PROPS_WITH_NEWLINE = "[property.name]: [\n" +
"]\n";

@Test
public void parsePropsWithNewlineTest() throws UnknownHostException, IOException {
Reader reader = new StringReader(PROPS_WITH_NEWLINE);
BufferedReader bufferedReader = new BufferedReader(reader);

Set<String> servers = AndroidUsingExec.parseProps(bufferedReader, false);

assertTrue(servers.isEmpty());
}
}

0 comments on commit 0dba448

Please sign in to comment.