cleanup DNS

This commit is contained in:
nathan 2015-07-17 02:47:02 +02:00
parent e961f395a5
commit 448ffe15dc
9 changed files with 111 additions and 100 deletions

View File

@ -5,12 +5,14 @@ import io.netty.handler.codec.dns.DnsRecord;
import io.netty.resolver.dns.DnsNameResolverAccess; import io.netty.resolver.dns.DnsNameResolverAccess;
/** /**
* Decodes any record that simply returns a domain name, such as NS (name * server) and CNAME (canonical name) resource records. * Decodes domain names, such as NS (name * server) and CNAME (canonical name) resource records
*/ */
public class DomainDecoder implements RecordDecoder<String> { public
class DomainDecoder implements RecordDecoder<String> {
@Override @Override
public String decode(final DnsRecord record, final ByteBuf response) { public
String decode(final DnsRecord record, final ByteBuf response) {
return DnsNameResolverAccess.decodeDomainName(response); return DnsNameResolverAccess.decodeDomainName(response);
} }
} }

View File

@ -6,15 +6,14 @@ import io.netty.handler.codec.dns.DnsRecord;
import io.netty.resolver.dns.DnsNameResolverAccess; import io.netty.resolver.dns.DnsNameResolverAccess;
/** /**
* Decodes MX (mail exchanger) resource records. * Decodes MX (mail exchanger) resource records
*/ */
public class MailExchangerDecoder implements RecordDecoder<MailExchangerRecord> { public
class MailExchangerDecoder implements RecordDecoder<MailExchangerRecord> {
/**
* Returns a decoded MX (mail exchanger) resource record
*/
@Override @Override
public MailExchangerRecord decode(final DnsRecord record, final ByteBuf response) { public
MailExchangerRecord decode(final DnsRecord record, final ByteBuf response) {
int priority = response.readUnsignedShort(); int priority = response.readUnsignedShort();
String name = DnsNameResolverAccess.decodeDomainName(response); String name = DnsNameResolverAccess.decodeDomainName(response);

View File

@ -4,7 +4,8 @@ import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.DecoderException; import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.dns.DnsRecord; import io.netty.handler.codec.dns.DnsRecord;
public interface RecordDecoder<T> { public
interface RecordDecoder<T> {
T decode(final DnsRecord record, final ByteBuf response) throws DecoderException; T decode(final DnsRecord record, final ByteBuf response) throws DecoderException;
} }

View File

@ -8,17 +8,16 @@ import io.netty.resolver.dns.DnsNameResolverAccess;
/** /**
* Decodes SRV (service) resource records. * Decodes SRV (service) resource records.
*/ */
public class ServiceDecoder implements RecordDecoder<ServiceRecord> { public
class ServiceDecoder implements RecordDecoder<ServiceRecord> {
/**
* Returns a decoded SRV (service) resource record
*/
@Override @Override
public ServiceRecord decode(final DnsRecord record, final ByteBuf response) { public
ServiceRecord decode(final DnsRecord record, final ByteBuf response) {
int priority = response.readShort(); int priority = response.readShort();
int weight = response.readShort(); int weight = response.readShort();
int port = response.readUnsignedShort(); int port = response.readUnsignedShort();
String target = DnsNameResolverAccess.decodeDomainName(response); String target = DnsNameResolverAccess.decodeDomainName(response);
return new ServiceRecord(record.name(), priority, weight, port, target); return new ServiceRecord(record.name(), priority, weight, port, target);
} }

View File

@ -8,15 +8,14 @@ import io.netty.resolver.dns.DnsNameResolverAccess;
/** /**
* Decodes SOA (start of authority) resource records. * Decodes SOA (start of authority) resource records.
*/ */
public class StartOfAuthorityDecoder implements RecordDecoder<StartOfAuthorityRecord> { public
class StartOfAuthorityDecoder implements RecordDecoder<StartOfAuthorityRecord> {
/**
* Returns a decoded SOA (start of authority) resource record
*/
@Override @Override
public StartOfAuthorityRecord decode(final DnsRecord record, final ByteBuf response) { public
String mName = DnsNameResolverAccess.decodeDomainName(response); StartOfAuthorityRecord decode(final DnsRecord record, final ByteBuf response) {
String rName = DnsNameResolverAccess.decodeDomainName(response); String primaryName = DnsNameResolverAccess.decodeDomainName(response);
String personName = DnsNameResolverAccess.decodeDomainName(response);
long serial = response.readUnsignedInt(); long serial = response.readUnsignedInt();
int refresh = response.readInt(); int refresh = response.readInt();
@ -24,6 +23,6 @@ public class StartOfAuthorityDecoder implements RecordDecoder<StartOfAuthorityRe
int expire = response.readInt(); int expire = response.readInt();
long minimum = response.readUnsignedInt(); long minimum = response.readUnsignedInt();
return new StartOfAuthorityRecord(mName, rName, serial, refresh, retry, expire, minimum); return new StartOfAuthorityRecord(primaryName, personName, serial, refresh, retry, expire, minimum);
} }
} }

View File

@ -10,13 +10,12 @@ import java.util.List;
/** /**
* Decodes TXT (text) resource records. * Decodes TXT (text) resource records.
*/ */
public class TextDecoder implements RecordDecoder<List<String>> { public
class TextDecoder implements RecordDecoder<List<String>> {
/**
* Returns a decoded TXT (text) resource record
*/
@Override @Override
public List<String> decode(final DnsRecord record, final ByteBuf response) { public
List<String> decode(final DnsRecord record, final ByteBuf response) {
List<String> list = new ArrayList<String>(); List<String> list = new ArrayList<String>();
int index = response.readerIndex(); int index = response.readerIndex();
@ -28,5 +27,4 @@ public class TextDecoder implements RecordDecoder<List<String>> {
return list; return list;
} }
} }

View File

@ -20,7 +20,8 @@ package dorkbox.network.dns.record;
* responsible for accepting e-mail and a preference value for prioritizing mail * responsible for accepting e-mail and a preference value for prioritizing mail
* servers if multiple servers exist. * servers if multiple servers exist.
*/ */
public class MailExchangerRecord { public
class MailExchangerRecord {
private final int priority; private final int priority;
private final String name; private final String name;
@ -28,13 +29,12 @@ public class MailExchangerRecord {
/** /**
* Constructs an MX (mail exchanger) record. * Constructs an MX (mail exchanger) record.
* *
* @param priority * @param priority the priority of the mail exchanger, lower is more preferred
* the priority of the mail exchanger, lower is more preferred * @param name the e-mail address in the format admin.example.com, which
* @param name * represents admin@example.com
* the e-mail address in the format admin.example.com, which
* represents admin@example.com
*/ */
public MailExchangerRecord(int priority, String name) { public
MailExchangerRecord(int priority, String name) {
this.priority = priority; this.priority = priority;
this.name = name; this.name = name;
} }
@ -42,7 +42,8 @@ public class MailExchangerRecord {
/** /**
* Returns the priority of the mail exchanger, lower is more preferred. * Returns the priority of the mail exchanger, lower is more preferred.
*/ */
public int priority() { public
int priority() {
return this.priority; return this.priority;
} }
@ -50,7 +51,8 @@ public class MailExchangerRecord {
* Returns the mail exchanger (an e-mail address) in the format * Returns the mail exchanger (an e-mail address) in the format
* admin.example.com, which represents admin@example.com. * admin.example.com, which represents admin@example.com.
*/ */
public String name() { public
String name() {
return this.name; return this.name;
} }

View File

@ -20,7 +20,8 @@ package dorkbox.network.dns.record;
* and port, of servers for specified services. For example, a service "http" * and port, of servers for specified services. For example, a service "http"
* may be running on the server "example.com" on port 80. * may be running on the server "example.com" on port 80.
*/ */
public class ServiceRecord { public
class ServiceRecord {
private final int priority; private final int priority;
private final int weight; private final int weight;
@ -33,24 +34,20 @@ public class ServiceRecord {
/** /**
* Constructs an SRV (service) record. * Constructs an SRV (service) record.
* *
* @param fullPath * @param fullPath the name first read in the SRV record which contains the
* the name first read in the SRV record which contains the * service, the protocol, and the name of the server being
* service, the protocol, and the name of the server being * queried. The format is {@code _service._protocol.hostname}, or
* queried. The format is {@code _service._protocol.hostname}, or * for example {@code _http._tcp.example.com}
* for example {@code _http._tcp.example.com} * @param priority relative priority of this service, range 0-65535 (lower is
* @param priority * higher priority)
* relative priority of this service, range 0-65535 (lower is * @param weight determines how often multiple services will be used in the
* higher priority) * event they have the same priority (greater weight means
* @param weight * service is used more often)
* determines how often multiple services will be used in the * @param port the port for the service
* event they have the same priority (greater weight means * @param target the name of the host for the service
* service is used more often)
* @param port
* the port for the service
* @param target
* the name of the host for the service
*/ */
public ServiceRecord(String fullPath, int priority, int weight, int port, String target) { public
ServiceRecord(String fullPath, int priority, int weight, int port, String target) {
String[] parts = fullPath.split("\\.", 3); String[] parts = fullPath.split("\\.", 3);
this.service = parts[0]; this.service = parts[0];
this.protocol = parts[1]; this.protocol = parts[1];
@ -64,50 +61,56 @@ public class ServiceRecord {
/** /**
* Returns the priority for this service record. * Returns the priority for this service record.
*/ */
public int priority() { public
int priority() {
return this.priority; return this.priority;
} }
/** /**
* Returns the weight of this service record. * Returns the weight of this service record.
*/ */
public int weight() { public
int weight() {
return this.weight; return this.weight;
} }
/** /**
* Returns the port the service is running on. * Returns the port the service is running on.
*/ */
public int port() { public
int port() {
return this.port; return this.port;
} }
/** /**
* Returns the name for the server being queried. * Returns the name for the server being queried.
*/ */
public String name() { public
String name() {
return this.name; return this.name;
} }
/** /**
* Returns the protocol for the service being queried (i.e. "_tcp"). * Returns the protocol for the service being queried (i.e. "_tcp").
*/ */
public String protocol() { public
String protocol() {
return this.protocol; return this.protocol;
} }
/** /**
* Returns the service's name (i.e. "_http"). * Returns the service's name (i.e. "_http").
*/ */
public String service() { public
String service() {
return this.service; return this.service;
} }
/** /**
* Returns the name of the host for the service. * Returns the name of the host for the service.
*/ */
public String target() { public
String target() {
return this.target; return this.target;
} }
} }

View File

@ -19,7 +19,8 @@ package dorkbox.network.dns.record;
* Represents an SOA (start of authority) record, which defines global * Represents an SOA (start of authority) record, which defines global
* parameters for a zone (domain). There can only be one SOA record per zone. * parameters for a zone (domain). There can only be one SOA record per zone.
*/ */
public class StartOfAuthorityRecord { public
class StartOfAuthorityRecord {
private final String primaryNameServer; private final String primaryNameServer;
private final String responsiblePerson; private final String responsiblePerson;
@ -32,34 +33,34 @@ public class StartOfAuthorityRecord {
/** /**
* Constructs an SOA (start of authority) record. * Constructs an SOA (start of authority) record.
* *
* @param primaryNameServer * @param primaryNameServer any name server that will respond authoritatively for the
* any name server that will respond authoritatively for the * domain
* domain * @param responsiblePerson e-mail address of person responsible for this zone
* @param responsiblePerson * @param serial a serial number that must be incremented when changes are
* e-mail address of person responsible for this zone * made. Recommended format is YYYYMMDDnn. For example, if the
* @param serial * primary name server is changed on June 19, 2013, then the
* a serial number that must be incremented when changes are * serial would be 2013061901. If it is changed again on the same
* made. Recommended format is YYYYMMDDnn. For example, if the * day it would be 2013061902
* primary name server is changed on June 19, 2013, then the * @param refreshTime number of seconds a secondary name server waits, after getting
* serial would be 2013061901. If it is changed again on the same * a copy of the zone, before it checks the zone again for
* day it would be 2013061902 * changes
* @param refreshTime * @param retryTime number of seconds to wait after a failed refresh attempt
* number of seconds a secondary name server waits, after getting * before another attempt to refresh is made
* a copy of the zone, before it checks the zone again for * @param expireTime number of seconds secondary name server can hold information
* changes * before it is considered not authoritative
* @param retryTime * @param minimumTtl number of seconds that records in the zone are valid for (if a
* number of seconds to wait after a failed refresh attempt * record has a higher TTL, it overrides this value which is just
* before another attempt to refresh is made * a minimum)
* @param expireTime
* number of seconds secondary name server can hold information
* before it is considered not authoritative
* @param minimumTtl
* number of seconds that records in the zone are valid for (if a
* record has a higher TTL, it overrides this value which is just
* a minimum)
*/ */
public StartOfAuthorityRecord(String primaryNameServer, String responsiblePerson, long serial, int refreshTime, public
int retryTime, int expireTime, long minimumTtl) { StartOfAuthorityRecord(String primaryNameServer,
String responsiblePerson,
long serial,
int refreshTime,
int retryTime,
int expireTime,
long minimumTtl) {
this.primaryNameServer = primaryNameServer; this.primaryNameServer = primaryNameServer;
this.responsiblePerson = responsiblePerson; this.responsiblePerson = responsiblePerson;
this.serial = serial; this.serial = serial;
@ -72,35 +73,40 @@ public class StartOfAuthorityRecord {
/** /**
* Returns the primary name server. * Returns the primary name server.
*/ */
public String primaryNameServer() { public
String primaryNameServer() {
return this.primaryNameServer; return this.primaryNameServer;
} }
/** /**
* Returns the responsible person's e-mail. * Returns the responsible person's e-mail.
*/ */
public String responsiblePerson() { public
String responsiblePerson() {
return this.responsiblePerson; return this.responsiblePerson;
} }
/** /**
* Returns the zone's serial number, usually in format YYYYMMDDnn. * Returns the zone's serial number, usually in format YYYYMMDDnn.
*/ */
public long serial() { public
long serial() {
return this.serial; return this.serial;
} }
/** /**
* Returns time between refreshes for secondary name servers. * Returns time between refreshes for secondary name servers.
*/ */
public int refreshTime() { public
int refreshTime() {
return this.refreshTime; return this.refreshTime;
} }
/** /**
* Returns time between retries for failed refresh attempts. * Returns time between retries for failed refresh attempts.
*/ */
public int retryTime() { public
int retryTime() {
return this.retryTime; return this.retryTime;
} }
@ -108,7 +114,8 @@ public class StartOfAuthorityRecord {
* Returns time before information stored in secondary name servers becomes * Returns time before information stored in secondary name servers becomes
* non authoritative. * non authoritative.
*/ */
public int expireTime() { public
int expireTime() {
return this.expireTime; return this.expireTime;
} }
@ -116,7 +123,8 @@ public class StartOfAuthorityRecord {
* Returns the minimum TTL for records in the zone (if the record has a * Returns the minimum TTL for records in the zone (if the record has a
* higher TTL, that value should be used as the TTL). * higher TTL, that value should be used as the TTL).
*/ */
public long minimumTtl() { public
long minimumTtl() {
return this.minimumTtl; return this.minimumTtl;
} }