Package org.xbill.DNS
Class Message
java.lang.Object
org.xbill.DNS.Message
- All Implemented Interfaces:
Cloneable
- Direct Known Subclasses:
Update
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
FieldsModifier and TypeFieldDescriptionstatic final int
The maximum length of a message in wire format. -
Constructor Summary
ConstructorsConstructorDescriptionMessage()
Creates a new Message with a random Message IDMessage
(byte[] b) Creates a new Message from its DNS wire format representationMessage
(int id) Creates a new Message with the specified Message IDMessage
(ByteBuffer byteBuffer) Creates a new Message from its DNS wire format representation -
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a record to a section of the Message, and adjusts the header.clone()
Creates a copy of this Message.boolean
findRecord
(Record r) 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
Determines if an RRset with the given name and type is already present in any section.boolean
Determines if an RRset with the given name and type is already present in the given section.Retrieves the Header.getOPT()
Returns the OPT record from the ADDITIONAL section, if one is present.Returns the first record in the QUESTION section.int
getRcode()
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.Record[]
getSectionArray
(int section) Deprecated.getSectionRRsets
(int section) Returns an array containing all records in the given section grouped into RRsets.getTSIG()
Returns the TSIG record from the ADDITIONAL section, if one is present.boolean
isSigned()
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 fromSection.AUTHORITY
andSection.ADDITIONAL
.Creates a normalized copy of this message by following xNAME chains, synthesizing CNAMEs from DNAMEs if necessary, and removing illegal RRsets fromSection.AUTHORITY
andSection.ADDITIONAL
.int
numBytes()
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
setResolver
(Resolver resolver) 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.toString()
Converts the Message to a String.byte[]
toWire()
Returns an array containing the wire format representation of theMessage
, 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.
-
Field Details
-
MAXLENGTH
public static final int MAXLENGTHThe 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
Creates a new Message from its DNS wire format representation- Parameters:
b
- A byte array containing the DNS Message.- Throws:
IOException
-
Message
Creates a new Message from its DNS wire format representation- Parameters:
byteBuffer
- A ByteBuffer containing the DNS Message.- Throws:
IOException
-
-
Method Details
-
newQuery
Creates a new Message with a random Message ID suitable for sending as a query.- Parameters:
r
- A record containing the question
-
newUpdate
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
Replaces the Header with a new one.- See Also:
-
getHeader
Retrieves the Header.- See Also:
-
addRecord
Adds a record to a section of the Message, and adjusts the header.- See Also:
-
removeRecord
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
Determines if the given record is already present in the given section.- See Also:
-
findRecord
Determines if the given record is already present in any section.- See Also:
-
findRRset
Determines if an RRset with the given name and type is already present in the given section.- See Also:
-
findRRset
Determines if an RRset with the given name and type is already present in any section.- See Also:
-
getQuestion
Returns the first record in the QUESTION section.- See Also:
-
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
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.usegetSection(int)
Returns an array containing all records in the given section, or an empty array if the section is empty.- See Also:
-
getSection
Returns all records in the given section, or an empty list if the section is empty.- See Also:
-
getSectionRRsets
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 theMessage
, 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 tosetTSIG(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! UsesetTSIG(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
Sets the TSIG key to sign a message.- Parameters:
key
- The TSIG key.- Since:
- 3.5.1
-
setTSIG
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
Converts the given section of the Message to a String.- See Also:
-
toString
Converts the Message to a String. -
clone
Creates a copy of this Message. This is done by the Resolver before adding TSIG and OPT records, for example. -
setResolver
Sets the resolver that originally received this Message from a server. -
getResolver
Gets the resolver that originally received this Message from a server. -
normalize
Creates a normalized copy of this message by following xNAME chains, synthesizing CNAMEs from DNAMEs if necessary, and removing illegal RRsets fromSection.AUTHORITY
andSection.ADDITIONAL
.Normalization is only applied to
Rcode.NOERROR
andRcode.NXDOMAIN
responses.This method is equivalent to calling
normalize(Message, boolean)
withfalse
.- 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
Creates a normalized copy of this message by following xNAME chains, synthesizing CNAMEs from DNAMEs if necessary, and removing illegal RRsets fromSection.AUTHORITY
andSection.ADDITIONAL
.Normalization is only applied to
Rcode.NOERROR
andRcode.NXDOMAIN
responses.- Parameters:
query
- The query that produced this message.throwOnIrrelevantRecord
- Iftrue
, throw an exception instead of silently ignoring irrelevant records.- Returns:
null
if the message could not be normalized or is otherwise invalid.- Throws:
WireParseException
- whenthrowOnIrrelevantRecord
istrue
and an invalid or irrelevant record was found.- Since:
- 3.6
-
getSection(int)