java.lang.Object
org.minidns.dnsmessage.DnsMessage.Builder
Enclosing class:
DnsMessage

public static final class DnsMessage.Builderextends Object
Builder class for constructing DNS messages.
Provides a fluent API for building DNS messages with various sections and flags configured step by step.
  • Field Details

    • id

      private int id
      The DNS message ID.
    • opcode

      private DnsMessage.OPCODE opcode
      The DNS message opcode.
    • responseCode

      private DnsMessage.RESPONSE_CODE responseCode
      The DNS response code.
    • query

      private boolean query
      True if this is a response (QR flag), false if it is a query.
    • authoritativeAnswer

      private boolean authoritativeAnswer
      True if the response is authoritative.
    • truncated

      private boolean truncated
      True if the message was truncated.
    • recursionDesired

      private boolean recursionDesired
      True if recursion was requested.
    • recursionAvailable

      private boolean recursionAvailable
      True if the server supports recursion.
    • authenticData

      private boolean authenticData
      True if the server considers the data authentic (DNSSEC).
    • checkingDisabled

      private boolean checkingDisabled
      True if DNSSEC validation should be skipped.
    • receiveTimestamp

      private long receiveTimestamp
      The timestamp when the message was received, or -1 if not set.
    • questions

      private List<Question> questions
      The question section records.
    • answerSection

      private List<Record<? extends Data>> answerSection
      The answer section records.
    • authoritySection

      private List<Record<? extends Data>> authoritySection
      The authority section records.
    • additionalSection

      private List<Record<? extends Data>> additionalSection
      The additional section records.
    • ednsBuilder

      private Edns.Builder ednsBuilder
      Builder for the EDNS OPT pseudo-record, or null if not used.
  • Constructor Details

    • Builder

      private Builder()
      Creates a new empty Builder.
    • Builder

      private Builder(DnsMessage message)
      Creates a Builder initialized from an existing DNS message.
      Parameters:
      message - the DNS message to copy values from
  • Method Details

    • setId

      public DnsMessage.Builder setId(int id)
      Set the current DNS message id.
      Parameters:
      id - The new DNS message id.
      Returns:
      a reference to this builder.
    • setOpcode

      public DnsMessage.Builder setOpcode(DnsMessage.OPCODE opcode)
      Set the opcode of the DNS message header.
      Parameters:
      opcode - the operation this message requests.
      Returns:
      a reference to this builder.
    • setResponseCode

      public DnsMessage.Builder setResponseCode(DnsMessage.RESPONSE_CODE responseCode)
      Set the response code of the DNS message header.
      Parameters:
      responseCode - the status of the DNS exchange to report.
      Returns:
      a reference to this builder.
    • setQrFlag

      public DnsMessage.Builder setQrFlag(boolean query)
      Set the QR flag. Note that this will be true if the message is a response and false if it is a query.
      Parameters:
      query - The new QR flag status.
      Returns:
      a reference to this builder.
    • setAuthoritativeAnswer

      public DnsMessage.Builder setAuthoritativeAnswer(boolean authoritativeAnswer)
      Set the authoritative answer flag.
      Parameters:
      authoritativeAnswer - Tge new authoritative answer value.
      Returns:
      a reference to this builder.
    • setTruncated

      public DnsMessage.Builder setTruncated(boolean truncated)
      Set the truncation bit on this DNS message.
      Parameters:
      truncated - The new truncated bit status.
      Returns:
      a reference to this builder.
    • setRecursionDesired

      public DnsMessage.Builder setRecursionDesired(boolean recursionDesired)
      Set the recursion desired flag on this message.
      Parameters:
      recursionDesired - The new recursion setting.
      Returns:
      a reference to this builder.
    • setRecursionAvailable

      public DnsMessage.Builder setRecursionAvailable(boolean recursionAvailable)
      Set the recursion available flog from this DNS message.
      Parameters:
      recursionAvailable - The new recursion available status.
      Returns:
      a reference to this builder.
    • setAuthenticData

      public DnsMessage.Builder setAuthenticData(boolean authenticData)
      Set the authentic data flag on this DNS message.
      Parameters:
      authenticData - The new authentic data flag value.
      Returns:
      a reference to this builder.
    • setCheckingDisabled

      public DnsMessage.Builder setCheckingDisabled(boolean checkingDisabled)
      /** Change the check status of this packet.
      Parameters:
      checkingDisabled - The new check disabled value.
      Returns:
      a reference to this builder.
    • copyFlagsFrom

      public void copyFlagsFrom(DnsMessage dnsMessage)
      Copy the header flags of the given message.
      Parameters:
      dnsMessage - the message to copy the QR, TC, RD, RA, AD and CD flags from; its AD bit also becomes the AA flag.
    • setReceiveTimestamp

      public DnsMessage.Builder setReceiveTimestamp(long receiveTimestamp)
      Set the time at which the message was received.
      Parameters:
      receiveTimestamp - the receive time in milliseconds since the epoch, or -1 if the message was not received.
      Returns:
      a reference to this builder.
    • addQuestion

      public DnsMessage.Builder addQuestion(Question question)
      Append a question to the question section.
      Parameters:
      question - the question to append.
      Returns:
      a reference to this builder.
    • setQuestions

      public DnsMessage.Builder setQuestions(List<Question> questions)
      Set the question part of this message.
      Parameters:
      questions - questions this message asks, replacing any already set
      Returns:
      a reference to this builder.
    • setQuestion

      public DnsMessage.Builder setQuestion(Question question)
      Set the question part of this message.
      Parameters:
      question - the single question this message asks
      Returns:
      a reference to this builder.
    • addAnswer

      public DnsMessage.Builder addAnswer(Record<? extends Data> answer)
      Append a record to the answer section.
      Parameters:
      answer - the record to append to the answer section.
      Returns:
      a reference to this builder.
    • addAnswers

      public DnsMessage.Builder addAnswers(Collection<Record<? extends Data>> records)
      Append records to the answer section.
      Parameters:
      records - the records to append to the answer section.
      Returns:
      a reference to this builder.
    • setAnswers

      public DnsMessage.Builder setAnswers(Collection<Record<? extends Data>> records)
      Replace the answer section by a copy of the given records.
      Parameters:
      records - the records the new answer section consists of.
      Returns:
      a reference to this builder.
    • getAnswers

      public List<Record<? extends Data>> getAnswers()
      Retrieve the answer section records.
      Returns:
      the records of the answer section, or an empty list if none have been set.
    • addNameserverRecords

      public DnsMessage.Builder addNameserverRecords(Record<? extends Data> record)
      Append a record to the authority section.
      Parameters:
      record - the record to append to the authority section.
      Returns:
      a reference to this builder.
    • setNameserverRecords

      public DnsMessage.Builder setNameserverRecords(Collection<Record<? extends Data>> records)
      Replace the authority section by a copy of the given records.
      Parameters:
      records - the records the new authority section consists of.
      Returns:
      a reference to this builder.
    • setAdditionalResourceRecords

      public DnsMessage.Builder setAdditionalResourceRecords(Collection<Record<? extends Data>> records)
      Replace the additional section by a copy of the given records.
      Parameters:
      records - the records the new additional section consists of.
      Returns:
      a reference to this builder.
    • addAdditionalResourceRecord

      public DnsMessage.Builder addAdditionalResourceRecord(Record<? extends Data> record)
      Append a record to the additional section.
      Parameters:
      record - the record to append to the additional section.
      Returns:
      a reference to this builder.
    • addAdditionalResourceRecords

      public DnsMessage.Builder addAdditionalResourceRecords(List<Record<? extends Data>> records)
      Append records to the additional section.
      Parameters:
      records - the records to append to the additional section.
      Returns:
      a reference to this builder.
    • getAdditionalResourceRecords

      public List<Record<? extends Data>> getAdditionalResourceRecords()
      Retrieve the additional section records.
      Returns:
      the records of the additional section, or an empty list if none have been set.
    • getEdnsBuilder

      public Edns.Builder getEdnsBuilder()
      Get the Edns builder. If no builder has been set so far, then a new one will be created.

      The EDNS record can be used to announce the supported size of UDP payload as well as additional flags.

      Note that some networks and firewalls are known to block big UDP payloads. 1280 should be a reasonable value, everything below 512 is treated as 512 and should work on all networks.

      Returns:
      a EDNS builder.
    • build

      public DnsMessage build()
      Build the DNS message from the state of this builder.
      Returns:
      a message holding the header flags and the sections of this builder.
    • writeToStringBuilder

      private void writeToStringBuilder(StringBuilder sb)
      Appends this record to the string builder.
      Parameters:
      sb - the builder to append to
    • toString

      public String toString()
      Renders the builder's accumulated state as a readable message summary.
      Overrides:
      toString in class Object