Package org.microproxy.dns
Class DnsMessage
java.lang.Object
org.microproxy.dns.DnsMessage
A DNS message (RFC 1035 section 4.1): its header, questions and resource records, decoded from
the wire format and encoded back. Records keep their RDATA as bytes, with embedded names expanded,
and
DnsMessage.ResourceRecord.data() reads the RDATA of the common types (A, AAAA, NS, CNAME, PTR,
DNAME, MX, SRV, TXT, SOA) as a typed DnsMessage.Rdata.
Messages are immutable: the with methods return changed copies. encode()
compresses the questions' and records' owner names; names inside RDATA are written as they are.
DnsMessage answer = DnsMessage.decode(bytes);
List<DnsMessage.ResourceRecord> blocked = answer.answers().stream()
.map(r -> r.type() == DnsMessage.A && r.name().equals("ads.example")
? r.withData(DnsMessage.Address.of("0.0.0.0")) : r)
.toList();
byte[] rewritten = answer.withAnswers(blocked).encode();
Names are given and shown in presentation format without the final dot (
www.example.com, . for the root), with \., \\ and \DDD escapes
for bytes that need them.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordAn address, the data of A (IPv4) and AAAA (IPv6) records.static final recordThe data of an MX record.static final recordA domain name, the data of NS, CNAME, PTR and DNAME records.static final recordRDATA of a type without a typed view, kept as bytes.static final recordA question: the name, type and class asked about.static interfaceThe RDATA of a record, read as its type.static final recordA resource record.static final recordThe data of an SOA record.static final recordThe data of an SRV record (RFC 2782).static final recordThe data of a TXT record: its character strings, read as UTF-8 (useDnsMessage.ResourceRecord.rdata()for strings of other bytes). -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intType A: an IPv4 address.static final intType AAAA: an IPv6 address.static final intType CNAME: the canonical name of an alias.static final intType HTTPS: HTTPS service binding (RFC 9460).static final intClass IN: the Internet.static final intType MX: a mail exchange.static final intType NS: a name server.static final intType PTR: a domain name pointer.static final intType SOA: the start of a zone of authority.static final intType SRV: a service location.static final intType TXT: text strings. -
Method Summary
Modifier and TypeMethodDescriptionReturns the additional section, an EDNS OPT record included.answers()Returns the answer section.Returns the authority section.static DnsMessagedecode(byte[] data) Decodes a message from its wire format, following compression pointers.byte[]encode()Encodes the message, compressing the questions' and records' owner names.intflags()Returns the header's flags: QR, opcode, AA, TC, RD, RA, Z, AD, CD and RCODE, as 16 bits.intid()Returns the message ID.booleanReturns whether the answer is authoritative (AA).booleanReturns whether recursion is available (RA).booleanReturns whether recursion is desired (RD).booleanReturns whether the message is a response (QR).booleanReturns whether the message was truncated (TC).static DnsMessageof(int id, int flags, List<DnsMessage.Question> questions, List<DnsMessage.ResourceRecord> answers, List<DnsMessage.ResourceRecord> authorities, List<DnsMessage.ResourceRecord> additionals) A message made of its parts.intopcode()Returns the opcode: 0 for a standard query.static DnsMessageA query with recursion desired, for one name and type of class IN, without EDNS.Returns the question section.intrcode()Returns the RCODE: 0 NOERROR, 2 SERVFAIL, 3 NXDOMAIN, ....static StringrcodeName(int rcode) The name of an RCODE.render()The message asdigshows it: the header, then each section.reply(List<DnsMessage.ResourceRecord> answers) An answer to this query: a response with the same ID, opcode, questions and recursion desired bit, recursion available, the RCODE NOERROR, andanswers.toString()static intThe type a mnemonic names.static StringtypeName(int type) The mnemonic of a type.withAdditionals(List<DnsMessage.ResourceRecord> additionals) This message with another additional section.withAnswers(List<DnsMessage.ResourceRecord> answers) This message with other answers.withAuthorities(List<DnsMessage.ResourceRecord> authorities) This message with another authority section.withFlags(int flags) This message with other header flags (QR, opcode, AA, TC, RD, RA, Z, AD, CD and RCODE).withId(int id) This message with another ID.withQuestions(List<DnsMessage.Question> questions) This message with other questions.withRcode(int rcode) This message with another RCODE (3 for NXDOMAIN, ...).
-
Field Details
-
A
public static final int AType A: an IPv4 address.- See Also:
-
NS
public static final int NSType NS: a name server.- See Also:
-
CNAME
public static final int CNAMEType CNAME: the canonical name of an alias.- See Also:
-
SOA
public static final int SOAType SOA: the start of a zone of authority.- See Also:
-
PTR
public static final int PTRType PTR: a domain name pointer.- See Also:
-
MX
public static final int MXType MX: a mail exchange.- See Also:
-
TXT
public static final int TXTType TXT: text strings.- See Also:
-
AAAA
public static final int AAAAType AAAA: an IPv6 address.- See Also:
-
SRV
public static final int SRVType SRV: a service location.- See Also:
-
HTTPS
public static final int HTTPSType HTTPS: HTTPS service binding (RFC 9460).- See Also:
-
IN
public static final int INClass IN: the Internet.- See Also:
-
-
Method Details
-
decode
Decodes a message from its wire format, following compression pointers.- Parameters:
data- the message- Returns:
- the message
- Throws:
IllegalArgumentException- if the bytes are not a well-formed DNS message
-
query
A query with recursion desired, for one name and type of class IN, without EDNS.- Parameters:
id- the message IDname- the nametype- the type (A, ...)- Returns:
- the query
- Throws:
IllegalArgumentException- if the name is not a valid domain name
-
of
public static DnsMessage of(int id, int flags, List<DnsMessage.Question> questions, List<DnsMessage.ResourceRecord> answers, List<DnsMessage.ResourceRecord> authorities, List<DnsMessage.ResourceRecord> additionals) A message made of its parts.- Parameters:
id- the message ID, 16 bitsflags- the header's flags, 16 bits (seeflags())questions- the question sectionanswers- the answer sectionauthorities- the authority sectionadditionals- the additional section- Returns:
- the message
-
reply
An answer to this query: a response with the same ID, opcode, questions and recursion desired bit, recursion available, the RCODE NOERROR, andanswers.- Parameters:
answers- the answer section- Returns:
- the response
-
encode
public byte[] encode()Encodes the message, compressing the questions' and records' owner names.- Returns:
- the wire format
- Throws:
IllegalArgumentException- if a section has more than 65535 entries
-
id
public int id()Returns the message ID.- Returns:
- the message ID
-
flags
public int flags()Returns the header's flags: QR, opcode, AA, TC, RD, RA, Z, AD, CD and RCODE, as 16 bits.- Returns:
- the header's flags: QR, opcode, AA, TC, RD, RA, Z, AD, CD and RCODE, as 16 bits
-
rcode
public int rcode()Returns the RCODE: 0 NOERROR, 2 SERVFAIL, 3 NXDOMAIN, ....- Returns:
- the RCODE: 0 NOERROR, 2 SERVFAIL, 3 NXDOMAIN, ...
-
opcode
public int opcode()Returns the opcode: 0 for a standard query.- Returns:
- the opcode: 0 for a standard query
-
isResponse
public boolean isResponse()Returns whether the message is a response (QR).- Returns:
- whether the message is a response (QR)
-
isAuthoritative
public boolean isAuthoritative()Returns whether the answer is authoritative (AA).- Returns:
- whether the answer is authoritative (AA)
-
isTruncated
public boolean isTruncated()Returns whether the message was truncated (TC).- Returns:
- whether the message was truncated (TC)
-
isRecursionDesired
public boolean isRecursionDesired()Returns whether recursion is desired (RD).- Returns:
- whether recursion is desired (RD)
-
isRecursionAvailable
public boolean isRecursionAvailable()Returns whether recursion is available (RA).- Returns:
- whether recursion is available (RA)
-
questions
Returns the question section.- Returns:
- the question section
-
answers
Returns the answer section.- Returns:
- the answer section
-
authorities
Returns the authority section.- Returns:
- the authority section
-
additionals
Returns the additional section, an EDNS OPT record included.- Returns:
- the additional section, an EDNS OPT record included
-
withId
This message with another ID.- Parameters:
id- the ID, 16 bits- Returns:
- the copy
-
withFlags
This message with other header flags (QR, opcode, AA, TC, RD, RA, Z, AD, CD and RCODE).- Parameters:
flags- the flags, 16 bits- Returns:
- the copy
-
withRcode
This message with another RCODE (3 for NXDOMAIN, ...).- Parameters:
rcode- the RCODE, 4 bits- Returns:
- the copy
-
withQuestions
This message with other questions.- Parameters:
questions- the question section- Returns:
- the copy
-
withAnswers
This message with other answers.- Parameters:
answers- the answer section- Returns:
- the copy
-
withAuthorities
This message with another authority section.- Parameters:
authorities- the authority section- Returns:
- the copy
-
withAdditionals
This message with another additional section.- Parameters:
additionals- the additional section- Returns:
- the copy
-
typeName
The mnemonic of a type.- Parameters:
type- the type- Returns:
A,AAAA, ..., orTYPE<n>for a type without one (RFC 3597)
-
type
The type a mnemonic names.- Parameters:
name-A,aaaa, ..., orTYPE<n>- Returns:
- the type
- Throws:
IllegalArgumentException- for a name this class does not know
-
rcodeName
The name of an RCODE.- Parameters:
rcode- the RCODE- Returns:
NOERROR,NXDOMAIN, ..., orRCODE<n>
-
render
The message asdigshows it: the header, then each section.- Returns:
- the text, ending with a newline
-
toString
-