Class DnsMessage


  • public class DnsMessage
    extends java.lang.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
    • Field Detail

      • id

        public final int id
        The DNS message id.
      • 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
      • 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
      • truncated

        public final boolean truncated
        True if message is truncated. Then TCP should be used.
      • recursionDesired

        public final boolean recursionDesired
        True if the server should recurse.
      • recursionAvailable

        public final boolean recursionAvailable
        True if recursion is possible.
      • authenticData

        public final boolean authenticData
        True if the server regarded the response as authentic.
      • checkingDisabled

        public final boolean checkingDisabled
        True if the server should not perform DNSSEC validation before returning the result.
      • questions

        public final java.util.List<Question> questions
        The question section content. Usually there will be only one question.

        This list is unmodifiable.

      • answerSection

        public final java.util.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.

      • authoritySection

        public final java.util.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.

      • additionalSection

        public final java.util.List<Record<? extends Data>> additionalSection
        The additional section. It eventually contains RRs which relate to the query.

        This list is unmodifiable.

      • optRrPosition

        public final int optRrPosition
      • receiveTimestamp

        public final long receiveTimestamp
        The receive timestamp. Set only if this message was created via parse. This should be used to evaluate TTLs.
    • Constructor Detail

      • DnsMessage

        public DnsMessage​(byte[] data)
                   throws java.io.IOException
        Build a DNS Message based on a binary DNS message.
        Parameters:
        data - The DNS message data.
        Throws:
        java.io.IOException - On read errors.
    • Method Detail

      • toArray

        public byte[] toArray()
        Generate a binary dns packet out of this message.
        Returns:
        byte[] the binary representation.
      • asDatagram

        public java.net.DatagramPacket asDatagram​(java.net.InetAddress address,
                                                  int port)
      • writeTo

        public void writeTo​(java.io.OutputStream outputStream)
                     throws java.io.IOException
        Throws:
        java.io.IOException
      • writeTo

        public void writeTo​(java.io.OutputStream outputStream,
                            boolean writeLength)
                     throws java.io.IOException
        Throws:
        java.io.IOException
      • getInByteBuffer

        public java.nio.ByteBuffer getInByteBuffer()
      • getQuestion

        public Question getQuestion()
      • copyQuestions

        public java.util.List<Question> copyQuestions()
        Copy the questions found in the question section.
        Returns:
        a copy of the question section questions.
        See Also:
        questions
      • copyAnswers

        public java.util.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 java.util.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
      • getEdns

        public Edns getEdns()
      • getOptPseudoRecord

        public Record<OPT> getOptPseudoRecord()
      • isDnssecOk

        public boolean isDnssecOk()
        Check if the EDNS DO (DNSSEC OK) flag is set.
        Returns:
        true if the DO flag is set.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • asTerminalOutput

        public java.lang.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.
      • getAnswersFor

        public <D extends Data> java.util.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.
      • asNormalizedVersion

        public DnsMessage asNormalizedVersion()
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • filterAnswerSectionBy

        public <D extends Data> java.util.List<Record<D>> filterAnswerSectionBy​(java.lang.Class<D> type)
      • filterAuthoritySectionBy

        public <D extends Data> java.util.List<Record<D>> filterAuthoritySectionBy​(java.lang.Class<D> type)
      • filterAdditionalSectionBy

        public <D extends Data> java.util.List<Record<D>> filterAdditionalSectionBy​(java.lang.Class<D> type)
      • getFirstOfTypeFromAnswerSection

        public <D extends DataRecord<D> getFirstOfTypeFromAnswerSection​(java.lang.Class<D> type)
      • getFirstOfTypeFromAuthoritySection

        public <D extends DataRecord<D> getFirstOfTypeFromAuthoritySection​(java.lang.Class<D> type)
      • getFirstOfTypeFromAdditionalSection

        public <D extends DataRecord<D> getFirstOfTypeFromAdditionalSection​(java.lang.Class<D> type)
      • equals

        public boolean equals​(java.lang.Object other)
        Overrides:
        equals in class java.lang.Object