org.minidns.dnsmessage

## Class DnsMessage

- java.lang.Object
- - org.minidns.dnsmessage.DnsMessage

-     public class DnsMessage
      extends Object

  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:
    RFC 1035

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `DnsMessage.Builder` Builder class for constructing DNS messages. Provides a fluent API for building DNS messages with various sections and flags configured step by step.
    `static class` | `DnsMessage.OPCODE` Symbolic DNS Opcode values.
    `static class` | `DnsMessage.RESPONSE_CODE` Possible DNS response codes.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `List<Record<? extends Data>>` | `additionalSection` The additional section.
    `List<Record<? extends Data>>` | `answerSection` The answers section records.
    `boolean` | `authenticData` True if the server regarded the response as authentic.
    `boolean` | `authoritativeAnswer` True if this is a authorative response.
    `List<Record<? extends Data>>` | `authoritySection` The Authority Section.
    `boolean` | `checkingDisabled` True if the server should not perform DNSSEC validation before returning the result.
    `int` | `id` The DNS message id.
    `DnsMessage.OPCODE` | `opcode` The DNS message opcode.
    `int` | `optRrPosition`
    `boolean` | `qr` The QR flag of the DNS message header.
    `List<Question>` | `questions` The question section content.
    `long` | `receiveTimestamp` The receive timestamp.
    `boolean` | `recursionAvailable` True if recursion is possible.
    `boolean` | `recursionDesired` True if the server should recurse.
    `DnsMessage.RESPONSE_CODE` | `responseCode` The response code of this dns message.
    `boolean` | `truncated` True if message is truncated.

  - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `` | `DnsMessage(byte[] data)` Build a DNS Message based on a binary DNS message.
    `protected` | `DnsMessage(DnsMessage.Builder builder)`

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `DnsMessage.Builder` | `asBuilder()`
    `DatagramPacket` | `asDatagram(InetAddress address, int port)`
    `DnsMessage` | `asNormalizedVersion()`
    `String` | `asTerminalOutput()` Format the DnsMessage object in a way suitable for terminal output.
    `static DnsMessage.Builder` | `builder()`
    `(package private) int` | `calculateHeaderBitmap()`
    `List<Record<? extends Data>>` | `copyAnswers()` Copy the records found in the answer section into a new list.
    `List<Record<? extends Data>>` | `copyAuthority()` Copy the records found in the authority section into a new list.
    `List<Question>` | `copyQuestions()` Copy the questions found in the question section.
    `boolean` | `equals(Object other)`
    `<D extends Data> List<Record<D>>` | `filterAdditionalSectionBy(Class<D> type)`
    `<D extends Data> List<Record<D>>` | `filterAnswerSectionBy(Class<D> type)`
    `<D extends Data> List<Record<D>>` | `filterAuthoritySectionBy(Class<D> type)`
    `<D extends Data> Set<D>` | `getAnswersFor(Question q)`
    `long` | `getAnswersMinTtl()` Get the minimum TTL from all answers in seconds.
    `Edns` | `getEdns()`
    `<D extends Data> Record<D>` | `getFirstOfTypeFromAdditionalSection(Class<D> type)`
    `<D extends Data> Record<D>` | `getFirstOfTypeFromAnswerSection(Class<D> type)`
    `<D extends Data> Record<D>` | `getFirstOfTypeFromAuthoritySection(Class<D> type)`
    `ByteBuffer` | `getInByteBuffer()`
    `Record<OPT>` | `getOptPseudoRecord()`
    `Question` | `getQuestion()`
    `DnsMessage.Builder` | `getResponseBuilder(DnsMessage.RESPONSE_CODE responseCode)`
    `int` | `hashCode()`
    `boolean` | `isDnssecOk()` Check if the EDNS DO (DNSSEC OK) flag is set.
    `byte[]` | `toArray()` Generate a binary dns packet out of this message.
    `String` | `toString()`
    `void` | `writeTo(OutputStream outputStream)`
    `void` | `writeTo(OutputStream outputStream, boolean writeLength)`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### additionalSection

          public final List<Record<? extends Data>> additionalSection

      The additional section. It eventually contains RRs which relate to the query.

      This list is unmodifiable.

    - #### answerSection

          public final List<Record<? extends Data>> 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.

    - #### authenticData

          public final boolean authenticData

      True if the server regarded the response as authentic.

    - #### authoritativeAnswer

          public final boolean authoritativeAnswer

      True 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.
      See Also:
        RFC 1035 § 4.1.1. Header section format

    - #### authoritySection

          public final List<Record<? extends Data>> authoritySection

      The Authority Section. Note that it is not guaranteed that this section only contains nameserver records. If DNSSEC is used, then this section could also contain a NSEC(3) record.

      This list is unmodifiable.

    - #### checkingDisabled

          public final boolean checkingDisabled

      True if the server should not perform DNSSEC validation before returning the result.

    - #### id

          public final int id

      The DNS message id.

    - #### opcode

          public final DnsMessage.OPCODE opcode

      The DNS message opcode.

    - #### optRrPosition

          public final int optRrPosition

    - #### qr

          public final boolean qr

      The QR flag of the DNS message header. Note that this will be `true` if the message is a response and `false` if it is a query.
      See Also:
        RFC 1035 § 4.1.1

    - #### questions

          public final List<Question> questions

      The question section content. Usually there will be only one question.

      This list is unmodifiable.

    - #### receiveTimestamp

          public final long receiveTimestamp

      The receive timestamp. Set only if this message was created via parse. This should be used to evaluate TTLs.

    - #### recursionAvailable

          public final boolean recursionAvailable

      True if recursion is possible.

    - #### recursionDesired

          public final boolean recursionDesired

      True if the server should recurse.

    - #### responseCode

          public final DnsMessage.RESPONSE_CODE responseCode

      The response code of this dns message.

    - #### truncated

          public final boolean truncated

      True if message is truncated. Then TCP should be used.

  - ### Constructor Detail

    - #### DnsMessage

          public DnsMessage(byte[] data)
                     throws IOException

      Build a DNS Message based on a binary DNS message.
      Parameters:
        `data` - The DNS message data.
      Throws:
        `IOException` - On read errors.

    - #### DnsMessage

          protected DnsMessage(DnsMessage.Builder builder)

  - ### Method Detail

    - #### asBuilder

          public DnsMessage.Builder asBuilder()

    - #### asDatagram

          public DatagramPacket asDatagram(InetAddress address,
                                           int port)

    - #### asNormalizedVersion

          public DnsMessage asNormalizedVersion()

    - #### asTerminalOutput

          public String asTerminalOutput()

      Format the DnsMessage object in a way suitable for terminal output. The format is loosely based on the output provided by `dig`.
      Returns:
        This message as a String suitable for terminal output.

    - #### builder

          public static DnsMessage.Builder builder()

    - #### calculateHeaderBitmap

          int calculateHeaderBitmap()

    - #### copyAnswers

          public List<Record<? extends Data>> copyAnswers()

      Copy the records found in the answer section into a new list.
      Returns:
        a copy of the answer section records.
      See Also:
        `answerSection`

    - #### copyAuthority

          public List<Record<? extends Data>> copyAuthority()

      Copy the records found in the authority section into a new list.
      Returns:
        a copy of the authority section records.
      See Also:
        `authoritySection`

    - #### copyQuestions

          public List<Question> copyQuestions()

      Copy the questions found in the question section.
      Returns:
        a copy of the question section questions.
      See Also:
        `questions`

    - #### equals

          public boolean equals(Object other)

      Overrides:
        `equals` in class `Object`

    - #### filterAdditionalSectionBy

          public <D extends Data> List<Record<D>> filterAdditionalSectionBy(Class<D> type)

    - #### filterAnswerSectionBy

          public <D extends Data> List<Record<D>> filterAnswerSectionBy(Class<D> type)

    - #### filterAuthoritySectionBy

          public <D extends Data> List<Record<D>> filterAuthoritySectionBy(Class<D> type)

    - #### getAnswersFor

          public <D extends Data> Set<D> getAnswersFor(Question q)

    - #### getAnswersMinTtl

          public long getAnswersMinTtl()

      Get the minimum TTL from all answers in seconds.
      Returns:
        the minimum TTL from all answers in seconds.

    - #### getEdns

          public Edns getEdns()

    - #### getFirstOfTypeFromAdditionalSection

          public <D extends Data> Record<D> getFirstOfTypeFromAdditionalSection(Class<D> type)

    - #### getFirstOfTypeFromAnswerSection

          public <D extends Data> Record<D> getFirstOfTypeFromAnswerSection(Class<D> type)

    - #### getFirstOfTypeFromAuthoritySection

          public <D extends Data> Record<D> getFirstOfTypeFromAuthoritySection(Class<D> type)

    - #### getInByteBuffer

          public ByteBuffer getInByteBuffer()

    - #### getOptPseudoRecord

          public Record<OPT> getOptPseudoRecord()

    - #### getQuestion

          public Question getQuestion()

    - #### getResponseBuilder

          public DnsMessage.Builder getResponseBuilder(DnsMessage.RESPONSE_CODE responseCode)

    - #### hashCode

          public int hashCode()

      Overrides:
        `hashCode` in class `Object`

    - #### isDnssecOk

          public boolean isDnssecOk()

      Check if the EDNS DO (DNSSEC OK) flag is set.
      Returns:
        true if the DO flag is set.

    - #### toArray

          public byte[] toArray()

      Generate a binary dns packet out of this message.
      Returns:
        byte[] the binary representation.

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### writeTo

          public void writeTo(OutputStream outputStream)
                       throws IOException

      Throws:
        `IOException`

    - #### writeTo

          public void writeTo(OutputStream outputStream,
                              boolean writeLength)
                       throws IOException

      Throws:
        `IOException`