Package org.xbill.DNS

Class Message

java.lang.Object
org.xbill.DNS.Message
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
Update

public class Message extends Object implements Cloneable
A DNS Message. A message is the basic unit of communication between the client and server of a DNS operation. A message consists of a Header and 4 message sections.
See Also:
  • Field Details

    • MAXLENGTH

      public static final int MAXLENGTH
      The maximum length of a message in wire format.
      See Also:
    • sections

      private List<Record>[] sections
    • size

      private int size
    • tsigkey

      private TSIG tsigkey
    • generatedTsig

      private TSIGRecord generatedTsig
    • querytsig

      private TSIGRecord querytsig
    • tsigerror

      private int tsigerror
    • resolver

      private Resolver resolver
    • tsigstart

      int tsigstart
    • tsigState

      int tsigState
    • sig0start

      int sig0start
    • TSIG_UNSIGNED

      static final int TSIG_UNSIGNED
      The message was not signed
      See Also:
    • TSIG_VERIFIED

      static final int TSIG_VERIFIED
      The message was signed and verification succeeded
      See Also:
    • TSIG_INTERMEDIATE

      static final int TSIG_INTERMEDIATE
      The message was an unsigned message in multiple-message response
      See Also:
    • TSIG_SIGNED

      static final int TSIG_SIGNED
      The message was signed and no verification was attempted.
      See Also:
    • TSIG_FAILED

      static final int TSIG_FAILED
      The message was signed and verification failed, or was not signed when it should have been.
      See Also:
    • emptyRecordArray

      private static final Record[] emptyRecordArray
  • Constructor Details

    • Message

      private Message(Header header)
    • Message

      public Message(int id)
      Creates a new Message with the specified Message ID
    • Message

      public Message()
      Creates a new Message with a random Message ID
    • Message

      Message(DNSInput in) throws IOException
      Throws:
      IOException
    • Message

      public Message(byte[] b) throws IOException
      Creates a new Message from its DNS wire format representation
      Parameters:
      b - A byte array containing the DNS Message.
      Throws:
      IOException
    • Message

      public Message(ByteBuffer byteBuffer) throws IOException
      Creates a new Message from its DNS wire format representation
      Parameters:
      byteBuffer - A ByteBuffer containing the DNS Message.
      Throws:
      IOException
  • Method Details

    • newQuery

      public static Message newQuery(Record r)
      Creates a new Message with a random Message ID suitable for sending as a query.
      Parameters:
      r - A record containing the question
    • newUpdate

      public static Message newUpdate(Name zone)
      Creates a new Message to contain a dynamic update. A random Message ID and the zone are filled in.
      Parameters:
      zone - The zone to be updated
    • setHeader

      public void setHeader(Header h)
      Replaces the Header with a new one.
      See Also:
    • getHeader

      public Header getHeader()
      Retrieves the Header.
      See Also:
    • addRecord

      public void addRecord(Record r, int section)
      Adds a record to a section of the Message, and adjusts the header.
      See Also:
    • removeRecord

      public boolean removeRecord(Record r, int section)
      Removes a record from a section of the Message, and adjusts the header.
      See Also:
    • removeAllRecords

      public void removeAllRecords(int section)
      Removes all records from a section of the Message, and adjusts the header.
      See Also:
    • findRecord

      public boolean findRecord(Record r, int section)
      Determines if the given record is already present in the given section.
      See Also:
    • findRecord

      public boolean findRecord(Record r)
      Determines if the given record is already present in any section.
      See Also:
    • findRRset

      public boolean findRRset(Name name, int type, int section)
      Determines if an RRset with the given name and type is already present in the given section.
      See Also:
    • findRRset

      public boolean findRRset(Name name, int type)
      Determines if an RRset with the given name and type is already present in any section.
      See Also:
    • getQuestion

      public Record getQuestion()
      Returns the first record in the QUESTION section.
      See Also:
    • getTSIG

      public TSIGRecord getTSIG()
      Returns the TSIG record from the ADDITIONAL section, if one is present.
      See Also:
    • getGeneratedTSIG

      TSIGRecord getGeneratedTSIG()
      Gets the generated TSIGRecord. Only valid if the messages has been converted to wire format with toWire(int) before.
      Returns:
      A generated TSIG record or null.
    • isSigned

      public boolean isSigned()
      Was this message signed by a TSIG?
      See Also:
    • isVerified

      public boolean isVerified()
      If this message was signed by a TSIG, was the TSIG verified?
      See Also:
    • getOPT

      public OPTRecord getOPT()
      Returns the OPT record from the ADDITIONAL section, if one is present.
      See Also:
    • getRcode

      public int getRcode()
      Returns the message's rcode (error code). This incorporates the EDNS extended rcode.
    • getSectionArray

      @Deprecated public Record[] getSectionArray(int section)
      Deprecated.
      Returns an array containing all records in the given section, or an empty array if the section is empty.
      See Also:
    • getSection

      public List<Record> getSection(int section)
      Returns all records in the given section, or an empty list if the section is empty.
      See Also:
    • getSectionRRsets

      public List<RRset> getSectionRRsets(int section)
      Returns an array containing all records in the given section grouped into RRsets.
      See Also:
    • toWire

      void toWire(DNSOutput out)
    • sectionToWire

      private int sectionToWire(DNSOutput out, int section, Compression c, int maxLength)
    • toWire

      private boolean toWire(DNSOutput out, int maxLength)
    • toWire

      public byte[] toWire()
      Returns an array containing the wire format representation of the Message, but does not do any additional processing (e.g. OPT/TSIG records, truncation).

      Do NOT use this to actually transmit a message, use toWire(int) instead.

    • toWire

      public byte[] toWire(int maxLength)
      Returns an array containing the wire format representation of the Message with the specified maximum length. Equivalent to calling toWire(maxLength, true).

      Do NOT use this method in conjunction with TSIG.apply(Message, TSIGRecord), it produces inconsistent results! Use setTSIG(TSIG, int, TSIGRecord) instead.

      Parameters:
      maxLength - The maximum length of the message.
      Returns:
      The wire format of the message, or an empty array if the message could not be rendered into the specified length.
      See Also:
    • toWire

      public byte[] toWire(int maxLength, boolean truncate) throws MessageSizeExceededException
      Returns an array containing the wire format representation of the Message with the specified maximum length. If truncate is true it will generate a truncated message (with the TC bit) if the message doesn't fit, otherwise an exception will be thrown. It will also sign the message with the TSIG key set by a call to setTSIG(TSIG, int, TSIGRecord). This method may return an empty byte array if the message could not be rendered at all; this could happen if maxLength is smaller than a DNS header, for example.

      Do NOT use this method in conjunction with TSIG.apply(Message, TSIGRecord), it produces inconsistent results! Use setTSIG(TSIG, int, TSIGRecord) instead.

      Parameters:
      maxLength - The maximum length of the message.
      Returns:
      The wire format of the message, or an empty array if the message could not be rendered into the specified length.
      Throws:
      MessageSizeExceededException - When the message size would exceed the specified maxLength.
      See Also:
    • setTSIG

      public void setTSIG(TSIG key)
      Sets the TSIG key to sign a message.
      Parameters:
      key - The TSIG key.
      Since:
      3.5.1
    • setTSIG

      public void setTSIG(TSIG key, int error, TSIGRecord querytsig)
      Sets the TSIG key and other necessary information to sign a message.
      Parameters:
      key - The TSIG key.
      error - The value of the TSIG error field.
      querytsig - If this is a response, the TSIG from the request.
    • numBytes

      public int numBytes()
      Returns the size of the message. Only valid if the message has been converted to or from wire format.
    • sectionToString

      public String sectionToString(int section)
      Converts the given section of the Message to a String.
      See Also:
    • sectionToString

      private void sectionToString(StringBuilder sb, int i)
    • toString

      public String toString()
      Converts the Message to a String.
      Overrides:
      toString in class Object
    • clone

      public Message clone()
      Creates a copy of this Message. This is done by the Resolver before adding TSIG and OPT records, for example.
      Overrides:
      clone in class Object
      See Also:
    • setResolver

      public void setResolver(Resolver resolver)
      Sets the resolver that originally received this Message from a server.
    • getResolver

      public Optional<Resolver> getResolver()
      Gets the resolver that originally received this Message from a server.
    • isTypeAllowedInSection

      boolean isTypeAllowedInSection(int type, int section)
      Checks if a record Type is allowed within a Section.
      Returns:
      true if the type is allowed, false otherwise.
    • normalize

      public Message normalize(Message query)
      Creates a normalized copy of this message by following xNAME chains, synthesizing CNAMEs from DNAMEs if necessary, and removing illegal RRsets from Section.AUTHORITY and Section.ADDITIONAL.

      Normalization is only applied to Rcode.NOERROR and Rcode.NXDOMAIN responses.

      This method is equivalent to calling normalize(Message, boolean) with false.

      Parameters:
      query - The query that produced this message.
      Returns:
      null if the message could not be normalized or is otherwise invalid.
      Since:
      3.6
    • normalize

      public Message normalize(Message query, boolean throwOnIrrelevantRecord) throws WireParseException
      Creates a normalized copy of this message by following xNAME chains, synthesizing CNAMEs from DNAMEs if necessary, and removing illegal RRsets from Section.AUTHORITY and Section.ADDITIONAL.

      Normalization is only applied to Rcode.NOERROR and Rcode.NXDOMAIN responses.

      Parameters:
      query - The query that produced this message.
      throwOnIrrelevantRecord - If true, throw an exception instead of silently ignoring irrelevant records.
      Returns:
      null if the message could not be normalized or is otherwise invalid.
      Throws:
      WireParseException - when throwOnIrrelevantRecord is true and an invalid or irrelevant record was found.
      Since:
      3.6
    • logOrThrow

      private void logOrThrow(boolean throwOnIrrelevantRecord, String format, RRset rrset, Name sname, Message query) throws WireParseException
      Throws:
      WireParseException
    • rrsetListToRecords

      private List<Record> rrsetListToRecords(List<RRset> rrsets)
    • addAdditionalRRset

      private void addAdditionalRRset(RRset rrset, List<RRset> additionalSectionSets, List<RRset> cleanedAdditionalSection)
    • doesTypeHaveAdditionalRecords

      private boolean doesTypeHaveAdditionalRecords(int type)