- All Implemented Interfaces:
Serializable, CharSequence, Comparable<DnsName>
DnsLabel) and is subject to certain restrictions (see for example RFC 3696 § 2.). Instances of this class can be created by using from(String).
This class holds three representations of a DNS name: ACE, raw ACE and IDN. ACE (ASCII Compatible Encoding), which can be accessed via ace, represents mostly the data that got send over the wire. But since DNS names are case insensitive, the ACE value is normalized to lower case. You can use getRawAce() to get the raw ACE data that was received, which possibly includes upper case characters. The IDN (Internationalized Domain Name), that is the DNS name as it should be shown to the user, can be retrieved using asIdn().
- See Also:
Field Summary
Modifier and TypeFieldDescriptionfinal StringThe DNS name in ASCII Compatible Encoding (ACE).private byte[]Cached wire format of the name.private StringCached domain part of the name.private intCached hash code, 0 until computed.private StringCached host part of the name.private StringCached internationalized form of the name.static final DnsNameThe reverse lookup zone for IPv4 addresses.static final DnsNameIP6_ARPA.private static final StringRegex matching label separator dots.private DnsLabel[]The labels in reverse order.(package private) static final intstatic final intLargest number of labels a DNS name may carry.private final StringThe DNS name in raw format, i.e.private byte[]Cached wire format including the root label.private DnsLabel[]Cached labels of the name.static final DnsNameThe root name, written as a lone dot.private StringCached printable form of the name.private static final longSerial version of the serialized form.private intSizestatic booleanWhether or not the DNS name is validated on construction.Constructor Summary
Method Summary
Modifier and TypeMethodDescriptionasIdn()Decodes the ACE form into Unicode, caching the result.intOrders names by their ACE form.booleanCompares two names by their encoded bytes.static DnsNamefrom(CharSequence name) Wraps a name given in presentation format.static DnsNamefrom(CharSequence child, DnsName parent) Places one text label directly under the given parent name.static DnsNameWraps a name given in presentation format.static DnsNameBuilds a name from labels supplied top-level first.static DnsNamePlaces a two-label run under the given parent name.static DnsNamePlaces one label directly under the given parent name.static DnsNameJoins any number of names into a single one with a dot between them.static DnsNameCreate a DNS name by "concatenating" the child under the parent name.getAce()Return the ACE (ASCII Compatible Encoding) version of this DNS name.byte[]getBytes()Serialize a domain name under IDN rules.The domain part of the name, in ACE form.The host part of the name, in ACE form.Fetches the label at the far left of this name.getLabel(int labelNum) Fetches one label by its index in the reversed array.intCounts the labels of this name, forcing them to be parsed first.DnsLabel[]Get a copy of the labels of this DNS name.private static DnsLabel[]Splits an ACE name into its labels.Return the parent of this DNS label.Returns the raw ACE version of this DNS name.byte[]Binary form built from the raw labels rather than the IDN-normalised ones, so uppercase letters received over the wire are preserved.DnsLabel[]Get a copy of the raw labels of this DNS name.inthashCode()Hashes the encoded bytes, caching the result.booleanTests whether this name sits below the given parent at any depth.booleanisDirectChildOf(DnsName parent) Tests whether exactly one label of this name sits above the given parent.booleanTests whether this name is the root, empty or a lone dot.private static StringlabelsToString(DnsLabel[] labels, int stringLength) Joins the labels into one name.private static DnsNameParse a domain name starting at the given offset.static DnsNameparse(DataInputStream dis, byte[] data) Parse a domain name starting at the current offset and moving the input stream pointer past this domain name (even if cross references occur).private voidComputes the cached wire form of the name.private voidComputes the cached host and domain parts.private voidComputes the cached labels of the name.intsize()Computes the encoded length of this name, caching the result.stripToLabels(int labelCount) Discards labels to the left, keeping only the rightmost few.private static byte[]Encodes the labels in the wire format.toString()Renders this name in dotted presentation format.private voidChecks the name against the DNS length limit.voidWrite the binary domain name to the stream, length-prefixing each label and terminating with the root byte.Methods inherited from class SafeCharSequence
charAt, length, subSequence, toSafeStringModifier and TypeMethodDescriptionfinal charcharAt(int index) Returns one character of the safe string form.final intlength()Counts the characters of the safe string form.final CharSequencesubSequence(int start, int end) Returns a safe substring of the safe string form.Return a safe string representation.Methods inherited from interface CharSequence
chars, codePoints, getChars, isEmpty
Field Details
serialVersionUID
private static final long serialVersionUIDSerial version of the serialized form.- See Also:
LABEL_SEP_REGEX
Regex matching label separator dots.MAX_DNSNAME_LENGTH_IN_OCTETS
MAX_LABELS
public static final int MAX_LABELSLargest number of labels a DNS name may carry.- See Also:
ROOT
The root name, written as a lone dot.IN_ADDR_ARPA
The reverse lookup zone for IPv4 addresses.IP6_ARPA
IP6_ARPA.VALIDATE
public static boolean VALIDATEWhether or not the DNS name is validated on construction.ace
The DNS name in ASCII Compatible Encoding (ACE).rawAce
bytes
private transient byte[] bytesCached wire format of the name.rawBytes
private transient byte[] rawBytesCached wire format including the root label.idn
Cached internationalized form of the name.domainpart
Cached domain part of the name.hostpart
Cached host part of the name.labels
The labels in reverse order.rawLabels
Cached labels of the name.hashCode
private transient int hashCodeCached hash code, 0 until computed.size
private int sizeSizesafeToStringRepresentation
Cached printable form of the name.
Constructor Details
DnsName
DnsName
Builds a name from a string in either form.- Parameters:
name- the name to parseinAce- true if the name is already ACE encoded
DnsName
Builds a name from its labels.- Parameters:
rawLabels- the labels of the namevalidateMaxDnsnameLength- true to check the total length
Method Details
labelsToString
validateMaxDnsnameLengthInOctets
private void validateMaxDnsnameLengthInOctets()Checks the name against the DNS length limit.- Throws:
IllegalArgumentException- if the name is too long
writeToStream
Write the binary domain name to the stream, length-prefixing each label and terminating with the root byte.- Parameters:
os- the stream to write the binary domain name to- Throws:
IOException- if the stream rejects or fails the write
getBytes
public byte[] getBytes()Serialize a domain name under IDN rules.- Returns:
- The binary domain name representation.
getRawBytes
public byte[] getRawBytes()Binary form built from the raw labels rather than the IDN-normalised ones, so uppercase letters received over the wire are preserved.- Returns:
- a copy of the binary domain name built from the raw labels
setBytesIfRequired
private void setBytesIfRequired()Computes the cached wire form of the name.toBytes
Encodes the labels in the wire format.- Parameters:
labels- the labels to encode- Returns:
- the encoded name
setLabelsIfRequired
private void setLabelsIfRequired()Computes the cached labels of the name.getLabels
getAce
Return the ACE (ASCII Compatible Encoding) version of this DNS name. Note that this method may return a String containing null bytes. Those Strings are notoriously difficult to handle from a security perspective. Therefore it is recommended to usetoString()instead, which will return a sanitized String.- Returns:
- the ACE version of this DNS name.
getRawAce
Returns the raw ACE version of this DNS name. That is, the version as it was received over the wire. Most notably, this version may include uppercase letters. Please refer togetAce()for a discussion of the security implications when working with the ACE representation of a DNS name.- Returns:
- the raw ACE version of this DNS name.
- See Also:
asIdn
Decodes the ACE form into Unicode, caching the result.- Returns:
- the Unicode form of this name, decoded from its ACE representation
getDomainpart
The domain part of the name, in ACE form.- Returns:
- the domain part, ACE encoded
getHostpart
The host part of the name, in ACE form.- Returns:
- the host part, ACE encoded
getHostpartLabel
Fetches the label at the far left of this name.- Returns:
- the leftmost label of this name, which is its host part
setHostnameAndDomainpartIfRequired
private void setHostnameAndDomainpartIfRequired()Computes the cached host and domain parts.size
public int size()Computes the encoded length of this name, caching the result.- Returns:
- the length in octets this name occupies in wire format, counting each label's length prefix and the terminating root byte
toString
Renders this name in dotted presentation format.- Specified by:
toStringin interfaceCharSequence- Overrides:
toStringin classObject
from
Wraps a name given in presentation format.- Parameters:
name- the domain name in presentation format, which is converted to ACE- Returns:
- the DNS name wrapping that presentation form
from
from
Create a DNS name by "concatenating" the child under the parent name. The child can also be seen as the "left" part of the resulting DNS name and the parent is the "right" part.For example using "i.am.the.child" as child and "of.this.parent.example" as parent, will result in a DNS name: "i.am.the.child.of.this.parent.example".
- Parameters:
child- the child DNS name.parent- the parent DNS name.- Returns:
- the resulting of DNS name.
from
Places one text label directly under the given parent name.- Parameters:
child- the text of the single label to place directly under parentparent- the DNS name supplying the labels to the right of the child- Returns:
- the DNS name formed by placing child under parent
from
from
Places a two-label run under the given parent name.- Parameters:
grandchild- the label to place directly under childchild- the label to place between grandchild and parentparent- the DNS name supplying the labels to the right of the child- Returns:
- the DNS name formed by placing grandchild under child under parent
from
from
parse
Parse a domain name starting at the current offset and moving the input stream pointer past this domain name (even if cross references occur).- Parameters:
dis- The input stream.data- The raw data (for cross references).- Returns:
- The domain name string.
- Throws:
IOException- Should never happen.
parse
private static DnsName parse(byte[] data, int offset, HashSet<Integer> jumps) throws IllegalStateException Parse a domain name starting at the given offset.- Parameters:
data- The raw data.offset- the index into data at which this domain name startsjumps- The list of jumps (by now).- Returns:
- The parsed domain name.
- Throws:
IllegalStateException- if the compression pointers form a cycle.
compareTo
Orders names by their ACE form.- Specified by:
compareToin interfaceComparable<DnsName>
equals
hashCode
isDirectChildOf
Tests whether exactly one label of this name sits above the given parent.- Parameters:
parent- the candidate ancestor name to compare this name against- Returns:
- true if this name carries exactly one label more than parent and shares its remaining labels
isChildOf
Tests whether this name sits below the given parent at any depth.- Parameters:
parent- the candidate ancestor name to test this name against- Returns:
- true if this name ends with parent's labels at any depth, so it may be a grandchild or further down
getLabelCount
public int getLabelCount()Counts the labels of this name, forcing them to be parsed first.- Returns:
- the number of labels in this name, where the root label on its own counts as zero
getLabels
Get a copy of the labels of this DNS name. The resulting array will contain the labels in reverse order, that is, the top-level domain will be at res[0].- Returns:
- an array of the labels in reverse order.
getLabel
Fetches one label by its index in the reversed array.- Parameters:
labelNum- the index into the reversed label array, so 0 is the top-level domain label- Returns:
- the label at that index of the reversed array
getRawLabels
Get a copy of the raw labels of this DNS name. The resulting array will contain the labels in reverse order, that is, the top-level domain will be at res[0].- Returns:
- an array of the raw labels in reverse order.
stripToLabels
Discards labels to the left, keeping only the rightmost few.- Parameters:
labelCount- how many of the rightmost labels to keep, from 0 for the root up togetLabelCount()- Returns:
- a name built from that many labels, this name itself when it is already that short, and the root at 0
- Throws:
IllegalArgumentException- if labelCount exceeds this name's label count
getParent
Return the parent of this DNS label. Will return the root label if this label itself is the root label (because there is no parent of root).For example:
"foo.bar.org".getParent() == "bar.org"".".getParent() == "."
- Returns:
- the parent of this DNS label.
isRootLabel
public boolean isRootLabel()Tests whether this name is the root, empty or a lone dot.- Returns:
- true if this name is the root, written either as an empty string or as a lone dot