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.
Author:
Brian Wellington
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The maximum length of a message in wire format.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new Message with a random Message ID
    Message(byte[] b)
    Creates a new Message from its DNS wire format representation
    Message(int id)
    Creates a new Message with the specified Message ID
    Message(ByteBuffer byteBuffer)
    Creates a new Message from its DNS wire format representation
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addRecord(Record r, int section)
    Adds a record to a section of the Message, and adjusts the header.
    Creates a copy of this Message.
    boolean
    Determines if the given record is already present in any section.
    boolean
    findRecord(Record r, int section)
    Determines if the given record is already present in the given section.
    boolean
    findRRset(Name name, int type)
    Determines if an RRset with the given name and type is already present in any section.
    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.
    Retrieves the Header.
    Returns the OPT record from the ADDITIONAL section, if one is present.
    Returns the first record in the QUESTION section.
    int
    Returns the message's rcode (error code).
    Gets the resolver that originally received this Message from a server.
    getSection(int section)
    Returns all records in the given section, or an empty list if the section is empty.
    getSectionArray(int section)
    Deprecated.
    getSectionRRsets(int section)
    Returns an array containing all records in the given section grouped into RRsets.
    Returns the TSIG record from the ADDITIONAL section, if one is present.
    boolean
    Was this message signed by a TSIG?
    boolean
    If this message was signed by a TSIG, was the TSIG verified?
    static Message
    Creates a new Message with a random Message ID suitable for sending as a query.
    static Message
    Creates a new Message to contain a dynamic update.
    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.
    normalize(Message query, boolean throwOnIrrelevantRecord)
    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.
    int
    Returns the size of the message.
    void
    removeAllRecords(int section)
    Removes all records from a section of the Message, and adjusts the header.
    boolean
    removeRecord(Record r, int section)
    Removes a record from a section of the Message, and adjusts the header.
    sectionToString(int section)
    Converts the given section of the Message to a String.
    void
    Replaces the Header with a new one.
    void
    Sets the resolver that originally received this Message from a server.
    void
    Sets the TSIG key to sign a message.
    void
    setTSIG(TSIG key, int error, TSIGRecord querytsig)
    Sets the TSIG key and other necessary information to sign a message.
    Converts the Message to a String.
    byte[]
    Returns an array containing the wire format representation of the Message, but does not do any additional processing (e.g.
    byte[]
    toWire(int maxLength)
    Returns an array containing the wire format representation of the Message with the specified maximum length.

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • MAXLENGTH

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

    • 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

      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:
    • 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

      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. This will generate a truncated message (with the TC bit) if the message doesn't fit, and 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.
      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:
    • 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.
    • 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