Merge pull request #3 from Flowdalic/androidagnostic

Make minidns Android agnostic
This commit is contained in:
Rene Treffer 2014-06-08 15:37:40 +02:00
commit f66c0db63f
17 changed files with 145 additions and 158 deletions

View File

@ -1,13 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.measite.minidns"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="19" />
<application/>
</manifest>

View File

@ -1,7 +1,7 @@
MiniDNS MiniDNS
------- -------
MiniDNS is a minial dns client library for android. It can parse a basic set MiniDNS is a minimal dns client library for android. It can parse a basic set
of resource records (A, AAAA, NS, SRV) and is easy to use and extend. of resource records (A, AAAA, NS, SRV) and is easy to use and extend.
This library is not intended to be used as a DNS server. You might want to This library is not intended to be used as a DNS server. You might want to

View File

@ -1,34 +1,126 @@
buildscript { apply plugin: 'java'
repositories { apply plugin: 'eclipse'
mavenCentral() apply plugin: 'maven'
} apply plugin: 'osgi'
apply plugin: 'signing'
dependencies { ext {
classpath 'com.android.tools.build:gradle:0.9.0' shortVersion = '0.1'
} isSnapshot = true
gitCommit = getGitCommit()
isReleaseVersion = !shortVersion
sonatypeCredentialsAvailable = project.hasProperty('sonatypeUsername') && project.hasProperty('sonatypePassword')
signingRequired = isReleaseVersion
sonatypeSnapshotUrl = 'https://oss.sonatype.org/content/repositories/snapshots'
sonatypeStagingUrl = 'https://oss.sonatype.org/service/local/staging/deploy/maven2'
buildDate = (new java.text.SimpleDateFormat("yyyy-MM-dd")).format(new Date())
} }
apply plugin: 'android-library' group = 'de.measite.minidns'
description = "A minimal DNS client library with support for A, AAAA, NS and SRV records"
android { sourceCompatibility = 1.7
compileSdkVersion 19 version = shortVersion
buildToolsVersion '19.0.3' if (isSnapshot) {
version += '-SNAPSHOT'
// NOTE: We are using the old folder structure to also support Eclipse }
sourceSets {
main { jar {
manifest.srcFile 'AndroidManifest.xml' manifest {
java.srcDirs = ['src'] instruction 'Implementation-GitRevision:', project.ext.gitCommit
resources.srcDirs = ['src'] }
aidl.srcDirs = ['src'] }
renderscript.srcDirs = ['src']
res.srcDirs = ['res'] gradle.taskGraph.whenReady { taskGraph ->
assets.srcDirs = ['assets'] if (signingRequired
} && taskGraph.allTasks.any { it instanceof Sign }) {
} // Use Java 6's console to read from the console (no good for a CI environment)
Console console = System.console()
// Do not abort build if lint finds errors console.printf '\n\nWe have to sign some things in this build.\n\nPlease enter your signing details.\n\n'
lintOptions { def password = console.readPassword('GnuPG Private Key Password: ')
abortOnError false
} allprojects { ext.'signing.password' = password }
console.printf '\nThanks.\n\n'
}
}
uploadArchives {
repositories {
mavenDeployer {
if (signingRequired) {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
}
repository(url: project.sonatypeStagingUrl) {
if (sonatypeCredentialsAvailable) {
authentication(userName: sonatypeUsername, password: sonatypePassword)
}
}
snapshotRepository(url: project.sonatypeSnapshotUrl) {
if (sonatypeCredentialsAvailable) {
authentication(userName: sonatypeUsername, password: sonatypePassword)
}
}
pom.project {
name 'minidns'
packaging 'jar'
inceptionYear '2014'
url 'https://github.com/rtreffer/minidns'
description project.description
issueManagement {
system 'GitHub'
url 'https://github.com/rtreffer/minidns/issues'
}
distributionManagement {
snapshotRepository {
id 'minidns.snapshot'
url project.sonatypeSnapshotUrl
}
}
scm {
url 'https://github.com/rtreffer/minidns'
connection 'scm:git:https://github.com/rtreffer/minidns.git'
developerConnection 'scm:git:https://github.com/rtreffer/minidns.git'
}
licenses {
license {
name 'The Apache Software License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
distribution 'repo'
}
}
developers {
developer {
id 'rtreffer'
name 'Rene Treffer'
}
developer {
id 'flow'
name 'Florian Schmaus'
email 'flow@geekplace.eu'
}
}
}
}
}
signing {
required { signingRequired }
sign configurations.archives
}
}
def getGitCommit() {
def dotGit = new File("$projectDir/.git")
if (!dotGit.isDirectory()) return 'non-git build'
def cmd = 'git describe --all --dirty=+'
def proc = cmd.execute()
def gitCommit = proc.text.trim()
assert !gitCommit.isEmpty()
gitCommit
} }

View File

@ -1,92 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="minidns-library" default="help">
<!-- The local.properties file is created and updated by the 'android' tool.
It contains the path to the SDK. It should *NOT* be checked into
Version Control Systems. -->
<property file="local.properties" />
<!-- The ant.properties file can be created by you. It is only edited by the
'android' tool to add properties to it.
This is the place to change some Ant specific build properties.
Here are some properties you may want to change/update:
source.dir
The name of the source directory. Default is 'src'.
out.dir
The name of the output directory. Default is 'bin'.
For other overridable properties, look at the beginning of the rules
files in the SDK, at tools/ant/build.xml
Properties related to the SDK location or the project target should
be updated using the 'android' tool with the 'update' action.
This file is an integral part of the build system for your
application and should be checked into Version Control Systems.
-->
<property file="ant.properties" />
<!-- if sdk.dir was not set from one of the property file, then
get it from the ANDROID_HOME env var.
This must be done before we load project.properties since
the proguard config can use sdk.dir -->
<property environment="env" />
<condition property="sdk.dir" value="${env.ANDROID_HOME}">
<isset property="env.ANDROID_HOME" />
</condition>
<!-- The project.properties file is created and updated by the 'android'
tool, as well as ADT.
This contains project specific properties such as project target, and library
dependencies. Lower level build properties are stored in ant.properties
(or in .classpath for Eclipse projects).
This file is an integral part of the build system for your
application and should be checked into Version Control Systems. -->
<loadproperties srcFile="project.properties" />
<!-- quick check on sdk.dir -->
<fail
message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
unless="sdk.dir"
/>
<!--
Import per project custom build rules if present at the root of the project.
This is the place to put custom intermediary targets such as:
-pre-build
-pre-compile
-post-compile (This is typically used for code obfuscation.
Compiled code location: ${out.classes.absolute.dir}
If this is not done in place, override ${out.dex.input.absolute.dir})
-post-package
-post-build
-pre-clean
-->
<import file="custom_rules.xml" optional="true" />
<!-- Import the actual build file.
To customize existing targets, there are two options:
- Customize only one target:
- copy/paste the target into this file, *before* the
<import> task.
- customize it to your needs.
- Customize the whole content of build.xml
- copy/paste the content of the rules files (minus the top node)
into this file, replacing the <import> task.
- customize to your needs.
***********************
****** IMPORTANT ******
***********************
In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
in order to avoid having your file be overridden by tools such as "android update project"
-->
<!-- version-tag: 1 -->
<import file="${sdk.dir}/tools/ant/build.xml" />
</project>

View File

View File

@ -1,2 +0,0 @@
target=android-19
android.library=true

View File

@ -14,8 +14,9 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Random; import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import android.util.Log;
import de.measite.minidns.Record.CLASS; import de.measite.minidns.Record.CLASS;
import de.measite.minidns.Record.TYPE; import de.measite.minidns.Record.TYPE;
@ -25,6 +26,8 @@ import de.measite.minidns.Record.TYPE;
*/ */
public class Client { public class Client {
private static final Logger LOGGER = Logger.getLogger(Client.class.getName());
/** /**
* The internal random class for sequence generation. * The internal random class for sequence generation.
*/ */
@ -129,18 +132,19 @@ public class Client {
message.setRecursionDesired(true); message.setRecursionDesired(true);
message.setId(random.nextInt()); message.setId(random.nextInt());
byte[] buf = message.toArray(); byte[] buf = message.toArray();
DatagramSocket socket = new DatagramSocket(); try (DatagramSocket socket = new DatagramSocket()) {
DatagramPacket packet = new DatagramPacket( DatagramPacket packet = new DatagramPacket(buf, buf.length,
buf, buf.length, InetAddress.getByName(host), port); InetAddress.getByName(host), port);
socket.setSoTimeout(timeout); socket.setSoTimeout(timeout);
socket.send(packet); socket.send(packet);
packet = new DatagramPacket(new byte[bufferSize], bufferSize); packet = new DatagramPacket(new byte[bufferSize], bufferSize);
socket.receive(packet); socket.receive(packet);
DNSMessage dnsMessage = DNSMessage.parse(packet.getData()); DNSMessage dnsMessage = DNSMessage.parse(packet.getData());
if (dnsMessage.getId() != message.getId()) { if (dnsMessage.getId() != message.getId()) {
return null; return null;
}
return dnsMessage;
} }
return dnsMessage;
} }
/** /**
@ -165,6 +169,7 @@ public class Client {
} }
} }
} catch (IOException ioe) { } catch (IOException ioe) {
LOGGER.log(Level.FINE, "IOException in query", ioe);
} }
} }
return null; return null;
@ -177,22 +182,19 @@ public class Client {
public String[] findDNS() { public String[] findDNS() {
String[] result = findDNSByReflection(); String[] result = findDNSByReflection();
if (result != null) { if (result != null) {
Log.d("minidns/client", LOGGER.fine("Got DNS servers via reflection: " + Arrays.toString(result));
"Got DNS servers via reflection: " + Arrays.toString(result));
return result; return result;
} }
result = findDNSByExec(); result = findDNSByExec();
if (result != null) { if (result != null) {
Log.d("minidns/client", LOGGER.fine("Got DNS servers via exec: " + Arrays.toString(result));
"Got DNS servers via exec: " + Arrays.toString(result));
return result; return result;
} }
// fallback for ipv4 and ipv6 connectivity // fallback for ipv4 and ipv6 connectivity
// see https://developers.google.com/speed/public-dns/docs/using // see https://developers.google.com/speed/public-dns/docs/using
Log.d("minidns/client", LOGGER.fine("No DNS found? Using fallback [8.8.8.8, [2001:4860:4860::8888]]");
"No DNS found? Using fallback [8.8.8.8, [2001:4860:4860::8888]]");
return new String[]{"8.8.8.8", "[2001:4860:4860::8888]"}; return new String[]{"8.8.8.8", "[2001:4860:4860::8888]"};
} }
@ -238,7 +240,7 @@ public class Client {
return server.toArray(new String[server.size()]); return server.toArray(new String[server.size()]);
} }
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); LOGGER.log(Level.WARNING, "Exception in findDNSByExec", e);
} }
return null; return null;
} }
@ -283,7 +285,7 @@ public class Client {
} }
} catch (Exception e) { } catch (Exception e) {
// we might trigger some problems this way // we might trigger some problems this way
e.printStackTrace(); LOGGER.log(Level.WARNING, "Exception in findDNSByReflection", e);
} }
return null; return null;
} }