java.lang.Object
org.minidns.dnsmessage.DnsMessage
A DNS message as defined by RFC 1035. The message consists of a header and 4 sections: question, answer, nameserver and addition resource record section. A message can either be parsed (
DnsMessage(byte[])) or serialized (toArray()).- See Also:
Nested Class Summary
Modifier and TypeClassDescriptionstatic final classBuilder class for constructing DNS messages.
Provides a fluent API for building DNS messages with various sections and flags configured step by step.static enumSymbolic DNS Opcode values.static enumPossible DNS response codes.private static enumThe names of the message sections.Field Summary
Modifier and TypeFieldDescriptionThe additional section.The answers section records.private longCached minimum TTL value across all answer records, or -1 if not yet computed.final booleanTrue if the server regarded the response as authentic.final booleanTrue if this is a authorative response.The Authority Section.private byte[]Cached serialized byte array, used to avoid re-serializing on repeated calls.final booleanTrue if the server should not perform DNSSEC validation before returning the result.private EdnsThe optional but very common EDNS information.private IntegerCached hash code, computed from the serialized byte representation on first call tohashCode().final intThe DNS message id.private static final LoggerLogger for the DNS message code.private DnsMessageCached normalized version of this message (id set to 0), built on first call toasNormalizedVersion().final DnsMessage.OPCODEThe DNS message opcode.final intThe position of the OPT pseudo-record in the additional section, or -1 if none.final booleanThe QR flag of the DNS message header.The question section content.final longThe receive timestamp.final booleanTrue if recursion is possible.final booleanTrue if the server should recurse.final DnsMessage.RESPONSE_CODEThe response code of this dns message.private StringCached terminal-formatted output string, built on first call toasTerminalOutput().private StringCached string representation of this message, built on first call totoString().final booleanTrue if message is truncated.Constructor Summary
ModifierConstructorDescriptionDnsMessage(byte[] data) Build a DNS Message based on a binary DNS message.privateDnsMessage(DnsMessage message) Constructs an normalized version of the given DnsMessage by setting the id to '0'.protectedDnsMessage(DnsMessage.Builder builder) Construct a DNS message from the state of the given builder.Method Summary
Modifier and TypeMethodDescriptionCreate a builder holding a copy of the content of this message.asDatagram(InetAddress address, int port) Wrap the serialized message in a UDP datagram.Retrieve the copy of this message with the message id set to 0.Format the DnsMessage object in a way suitable for terminal output.static DnsMessage.Builderbuilder()Create an empty builder for a new DNS message.(package private) intCompute the DNS header bitmapCopy the records found in the answer section into a new list.Copy the records found in the authority section into a new list.Copy the questions found in the question section.booleanCompares the serialized wire form against another message.filterAdditionalSectionBy(Class<D> type) Collect the additional section records holding the given payload type.filterAnswerSectionBy(Class<D> type) Collect the answer records holding the given payload type.filterAuthoritySectionBy(Class<D> type) Collect the authority section records holding the given payload type.filterSectionByType(boolean stopOnFirst, DnsMessage.SectionName sectionName, Class<D> type) Filters a section's records by data type.filterSectionByType(DnsMessage.SectionName sectionName, Class<D> type) Filters a section's records by data type.Collect the payloads of the answer records matching the given question.longGet the minimum TTL from all answers in seconds.getEdns()Retrieve the EDNS information of this message.getFirstOfType(DnsMessage.SectionName sectionName, Class<D> type) The first record of the given type.getFirstOfTypeFromAdditionalSection(Class<D> type) Retrieve the first additional section record holding the given payload type.getFirstOfTypeFromAnswerSection(Class<D> type) Retrieve the first answer record holding the given payload type.getFirstOfTypeFromAuthoritySection(Class<D> type) Retrieve the first authority section record holding the given payload type.Wrap the serialized message in a byte buffer.Retrieve the OPT pseudo record of this message.private static intgetOptRrPosition(List<Record<? extends Data>> additionalSection) Position of the OPT record in the additional section.Retrieve the first question of the question section.getResponseBuilder(DnsMessage.RESPONSE_CODE responseCode) Create a builder for a response to this query.inthashCode()Hashes the serialized byte representation, caching the computed value.booleanCheck if the EDNS DO (DNSSEC OK) flag is set.private byte[]Writes the message out as bytes.byte[]toArray()Generate a binary dns packet out of this message.toString()Renders the whole message, caching the result for later calls.voidwriteTo(OutputStream outputStream) Write the serialized message to the given stream, prefixed with its length as required by the DNS over TCP framing of RFC 1035 § 4.2.2.voidwriteTo(OutputStream outputStream, boolean writeLength) Write the serialized message to the given stream.
Field Details
LOGGER
Logger for the DNS message code.id
public final int idThe DNS message id.opcode
The DNS message opcode.responseCode
The response code of this dns message.qr
public final boolean qrThe QR flag of the DNS message header. Note that this will betrueif the message is a response andfalseif it is a query.- See Also:
authoritativeAnswer
public final boolean authoritativeAnswerTrue if this is a authorative response. If set, the responding nameserver is an authority for the domain name in the question section. Note that the answer section may have multiple owner names because of aliases. This flag corresponds to the name which matches the query name, or the first owner name in the query section.truncated
public final boolean truncatedTrue if message is truncated. Then TCP should be used.recursionDesired
public final boolean recursionDesiredTrue if the server should recurse.recursionAvailable
public final boolean recursionAvailableTrue if recursion is possible.authenticData
public final boolean authenticDataTrue if the server regarded the response as authentic.checkingDisabled
public final boolean checkingDisabledTrue if the server should not perform DNSSEC validation before returning the result.questions
answerSection
The answers section records. Note that it is not guaranteed that all records found in this section will be direct answers to the question in the query. If DNSSEC is used, then this section also contains the RRSIG record.This list is unmodifiable.
authoritySection
additionalSection
optRrPosition
public final int optRrPositionThe position of the OPT pseudo-record in the additional section, or -1 if none.edns
The optional but very common EDNS information. Note that this field is lazily populated.receiveTimestamp
public final long receiveTimestampThe receive timestamp. Set only if this message was created via parse. This should be used to evaluate TTLs.byteCache
private byte[] byteCacheCached serialized byte array, used to avoid re-serializing on repeated calls.toStringCache
Cached string representation of this message, built on first call totoString().terminalOutputCache
Cached terminal-formatted output string, built on first call toasTerminalOutput().answersMinTtlCache
private long answersMinTtlCacheCached minimum TTL value across all answer records, or -1 if not yet computed.normalizedVersionCache
Cached normalized version of this message (id set to 0), built on first call toasNormalizedVersion().hashCodeCache
Cached hash code, computed from the serialized byte representation on first call tohashCode().
Constructor Details
DnsMessage
Construct a DNS message from the state of the given builder.- Parameters:
builder- the builder holding the header flags and the sections.
DnsMessage
Build a DNS Message based on a binary DNS message.- Parameters:
data- The DNS message data.- Throws:
IOException- On read errors.
DnsMessage
Constructs an normalized version of the given DnsMessage by setting the id to '0'.- Parameters:
message- the message of which normalized version should be constructed.
Method Details
getOptRrPosition
toArray
public byte[] toArray()Generate a binary dns packet out of this message.- Returns:
- byte[] the binary representation.
asDatagram
Wrap the serialized message in a UDP datagram.- Parameters:
address- the address of the peer the message is addressed to.port- the UDP port of that peer, normally 53.- Returns:
- a datagram carrying the serialized message.
writeTo
Write the serialized message to the given stream, prefixed with its length as required by the DNS over TCP framing of RFC 1035 § 4.2.2.- Parameters:
outputStream- the stream to write to.- Throws:
IOException- On write errors.
writeTo
Write the serialized message to the given stream.- Parameters:
outputStream- the stream to write to.writeLength- true to prefix the message with its 16 bit length, false to write the bare message.- Throws:
IOException- On write errors.
getInByteBuffer
Wrap the serialized message in a byte buffer.- Returns:
- a buffer over a private copy of the serialized message.
serialize
private byte[] serialize()Writes the message out as bytes.- Returns:
- the message bytes
calculateHeaderBitmap
int calculateHeaderBitmap()Compute the DNS header bitmap- Returns:
- the 16 bit header field with the QR, opcode, AA, TC, RD, RA, AD and CD bits and the response code packed into it.
getQuestion
Retrieve the first question of the question section.- Returns:
- the first question.
copyQuestions
copyAnswers
Copy the records found in the answer section into a new list.- Returns:
- a copy of the answer section records.
- See Also:
copyAuthority
Copy the records found in the authority section into a new list.- Returns:
- a copy of the authority section records.
- See Also:
getEdns
Retrieve the EDNS information of this message.- Returns:
- the EDNS information built from the OPT pseudo record, or null if this message has none.
getOptPseudoRecord
isDnssecOk
public boolean isDnssecOk()Check if the EDNS DO (DNSSEC OK) flag is set.- Returns:
- true if the DO flag is set.
toString
asTerminalOutput
Format the DnsMessage object in a way suitable for terminal output. The format is loosely based on the output provided bydig.- Returns:
- This message as a String suitable for terminal output.
getAnswersFor
Collect the payloads of the answer records matching the given question.- Type Parameters:
D- the payload type the collected payloads are cast to.- Parameters:
q- the question whose name, type and class the records must match.- Returns:
- the distinct payloads of the matching answer records, or null if this message has a response code other than NO_ERROR.
getAnswersMinTtl
public long getAnswersMinTtl()Get the minimum TTL from all answers in seconds.- Returns:
- the minimum TTL from all answers in seconds.
asBuilder
Create a builder holding a copy of the content of this message.- Returns:
- a builder initialized from this message.
asNormalizedVersion
Retrieve the copy of this message with the message id set to 0.- Returns:
- a message with the content of this one but with the id zeroed, so that messages differing only in the id compare as equal.
getResponseBuilder
Create a builder for a response to this query.- Parameters:
responseCode- the status of the response to put into the header.- Returns:
- a builder with the QR flag set and with the id and the first question of this message; only usable while this message is a query.
hashCode
filterSectionByType
private <D extends Data> List<Record<D>> filterSectionByType(boolean stopOnFirst, DnsMessage.SectionName sectionName, Class<D> type) Filters a section's records by data type.- Type Parameters:
D- the data type of the records- Parameters:
stopOnFirst- true to stop after the first matchsectionName- the section to filtertype- the data class to keep- Returns:
- the matching records
filterSectionByType
private <D extends Data> List<Record<D>> filterSectionByType(DnsMessage.SectionName sectionName, Class<D> type) Filters a section's records by data type.- Type Parameters:
D- the data type of the records- Parameters:
sectionName- the section to filtertype- the data class to keep- Returns:
- the matching records
getFirstOfType
private <D extends Data> Record<D> getFirstOfType(DnsMessage.SectionName sectionName, Class<D> type) The first record of the given type.- Type Parameters:
D- the data type of the record- Parameters:
sectionName- the section to searchtype- the data class- Returns:
- the record, null if absent
filterAnswerSectionBy
Collect the answer records holding the given payload type.- Type Parameters:
D- the payload type the collected records are typed as.- Parameters:
type- the payload type to match.- Returns:
- the matching records of the answer section, in message order.
filterAuthoritySectionBy
Collect the authority section records holding the given payload type.- Type Parameters:
D- the payload type the collected records are typed as.- Parameters:
type- the payload type to match.- Returns:
- the matching records of the authority section, in message order.
filterAdditionalSectionBy
Collect the additional section records holding the given payload type.- Type Parameters:
D- the payload type the collected records are typed as.- Parameters:
type- the payload type to match.- Returns:
- the matching records of the additional section, in message order.
getFirstOfTypeFromAnswerSection
Retrieve the first answer record holding the given payload type.- Type Parameters:
D- the payload type the returned record is typed as.- Parameters:
type- the payload type to match.- Returns:
- the first matching record of the answer section, or null if there is none.
getFirstOfTypeFromAuthoritySection
Retrieve the first authority section record holding the given payload type.- Type Parameters:
D- the payload type the returned record is typed as.- Parameters:
type- the payload type to match.- Returns:
- the first matching record of the authority section, or null if there is none.
getFirstOfTypeFromAdditionalSection
Retrieve the first additional section record holding the given payload type.- Type Parameters:
D- the payload type the returned record is typed as.- Parameters:
type- the payload type to match.- Returns:
- the first matching record of the additional section, or null if there is none.
equals
builder
Create an empty builder for a new DNS message.- Returns:
- a builder with the default header flags and empty sections.