Dieses kleine Tutorial beschreibt die notwendigen Schritte zum Aufsetzen eines eigenen DNS-Servers und den Zugriff über Java.
Voraussetzung
- CentOS 5.5
- Bind DNS Server 9.3.x
- DnsJava 2.0.6
DNS Server
Bind Konfiguration /etc/named.conf für die neue Zone:
...
zone "ronnyfriedland-local.de" {
type master;
file "/var/named/ronnyfriedland-local.de";
allow-update { none; };
};
...
Die Datei /var/named/ronnyfriedland-local.de für die neu angelegte Zone:
$TTL 86400
@ IN SOA ronnyfriedland-local.de. root.ronnyfriedland-local.de. (
1 ; serial
1H ; refresh
1M ; retry
7D ; expiry
1D ) ; minimum
@ IN MX 10 mail.ronnyfriedland-local.de.
@ IN NS ns1.ronnyfriedland-local.de.
@ IN NS ns2.ronnyfriedland-local.de.
@ IN A 192.168.8.5
pop IN CNAME mail
smtp IN CNAME mail
ns1 IN A 192.168.8.1
ns2 IN A 192.168.8.2
mail IN A 192.168.8.3
Testen der Konfiguration mit
host -v -c IN -t ANY ronnyfriedland-local.de 192.168.8.5
liefert das folgende Ergebnis:
Trying "ronnyfriedland-local.de" Using domain server: Name: 192.168.8.5 Address: 192.168.8.5#53 Aliases: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 48770 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 5, AUTHORITY: 0, ADDITIONAL: 3 ;; QUESTION SECTION: ;ronnyfriedland-local.de. IN ANY ;; ANSWER SECTION: ronnyfriedland-local.de. 86400 IN SOA ronnyfriedland-local.de. root.ronnyfriedland-local.de. 1 3600 60 604800 86400 ronnyfriedland-local.de. 86400 IN MX 10 mail.ronnyfriedland-local.de. ronnyfriedland-local.de. 86400 IN NS ns2.ronnyfriedland-local.de. ronnyfriedland-local.de. 86400 IN NS ns1.ronnyfriedland-local.de. ronnyfriedland-local.de. 86400 IN A 192.168.8.5 ;; ADDITIONAL SECTION: mail.ronnyfriedland-local.de. 86400 IN A 192.168.8.3 ns1.ronnyfriedland-local.de. 86400 IN A 192.168.8.1 ns2.ronnyfriedland-local.de. 86400 IN A 192.168.8.2 Received 203 bytes from 192.168.8.5#53 in 88 ms
Java Zugriff
Der Zugriff über DnsJava ist recht einfach. Ich habe dafür einen Unittest geschrieben, welcher die aktuelle Konfiguration testet.
import java.net.UnknownHostException;
import org.junit.Assert;
import org.junit.Test;
import org.xbill.DNS.ARecord;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.MXRecord;
import org.xbill.DNS.NSRecord;
import org.xbill.DNS.Record;
import org.xbill.DNS.Resolver;
import org.xbill.DNS.SimpleResolver;
import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;
public class DnsLookupTest {
private static final String IP = "192.168.8.5";
private static final String DOMAIN = "ronnyfriedland-local.de";
@Test
public void testMX() throws Exception {
Lookup lookup = getLookup(Type.MX);
Record[] records = lookup.run();
Assert.assertEquals(Lookup.SUCCESSFUL, lookup.getResult());
Assert.assertEquals(1, records.length);
Record mxRecord = records[0];
Assert.assertTrue(mxRecord instanceof MXRecord);
Assert.assertEquals("mail.ronnyfriedland-local.de.", ((MXRecord) mxRecord).getTarget().toString());
}
@Test
public void testNS() throws Exception {
Lookup lookup = getLookup(Type.NS);
Record[] records = lookup.run();
Assert.assertEquals(Lookup.SUCCESSFUL, lookup.getResult());
Assert.assertEquals(2, records.length);
Assert.assertTrue(records[0] instanceof NSRecord);
Assert.assertTrue(records[1] instanceof NSRecord);
}
@Test
public void testA() throws Exception {
Lookup lookup = getLookup(Type.A);
Record[] records = lookup.run();
Assert.assertEquals(Lookup.SUCCESSFUL, lookup.getResult());
Assert.assertEquals(1, records.length);
Record aRecord = records[0];
Assert.assertTrue(aRecord instanceof ARecord);
Assert.assertEquals(DOMAIN + ".", ((ARecord) aRecord).getName().toString());
Assert.assertEquals(IP, ((ARecord) aRecord).getAddress().getHostAddress());
}
private Lookup getLookup(int type) throws TextParseException, UnknownHostException {
Lookup lookup = new Lookup(DOMAIN, type);
Resolver resolver = new SimpleResolver(IP);
lookup.setResolver(resolver);
lookup.setCache(null); // no cache
return lookup;
}
}