pub struct Header { /* private fields */ }
Expand description
The first part of the header of a DNS message.
This type represents the information contained in the first four octets of the header: the message ID, opcode, rcode, and the various flags. It keeps those four octets in wire representation, i.e., in network byte order. The data is layed out like this:
1 1 1 1 1 1
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ID |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR| Opcode |AA|TC|RD|RA|Z |AD|CD| RCODE |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Methods are available for accessing each of these fields. For more information on the fields, see these methods in the section Field Access below.
You can create owned values via the new
method or
the Default
trait. However, more often the type will
be used via a reference into the octets of an actual message. The
functions for_message_slice
and
for_message_slice_mut
create such
references from an octets slice.
The basic structure and most of the fields re defined in RFC 1035, except for the AD and CD flags, which are defined in RFC 4035.
Implementations§
source§impl Header
impl Header
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new header.
The new header has all fields as either zero or false. Thus, the
opcode will be Opcode::Query
and the response code will be
Rcode::NoError
.
sourcepub fn for_message_slice(s: &[u8]) -> &Header
pub fn for_message_slice(s: &[u8]) -> &Header
Creates a header reference from an octets slice of a message.
Panics
This function panics if the slice is less than four octets long.
sourcepub fn for_message_slice_mut(s: &mut [u8]) -> &mut Header
pub fn for_message_slice_mut(s: &mut [u8]) -> &mut Header
Creates a mutable header reference from an octets slice of a message.
Panics
This function panics if the slice is less than four octets long.
source§impl Header
impl Header
sourcepub fn id(self) -> u16
pub fn id(self) -> u16
Returns the value of the ID field.
The ID field is an identifier chosen by whoever created a query and is copied into a response by a server. It allows matching incoming responses to their queries.
When choosing an ID for an outgoing message, make sure it is random
to avoid spoofing by guessing the message ID. If std
support
is enabled, the method
set_random_id
can be used for this purpose.
sourcepub fn set_random_id(&mut self)
pub fn set_random_id(&mut self)
Sets the value of the ID field to a randomly chosen number.
sourcepub fn opcode(self) -> Opcode
pub fn opcode(self) -> Opcode
Returns the value of the Opcode field.
This field specifies the kind of query a message contains. See
the Opcode
type for more information on the possible values and
their meaning. Normal queries have the variant Opcode::Query
which is also the default value when creating a new header.
sourcepub fn set_opcode(&mut self, opcode: Opcode)
pub fn set_opcode(&mut self, opcode: Opcode)
Sets the value of the opcode field.
sourcepub fn flags(self) -> Flags
pub fn flags(self) -> Flags
Returns all flags contained in the header.
This is a virtual field composed of all the flag bits that are present
in the header. The returned Flags
type can be useful when you’re
working with all flags, rather than a single one, which can be easily
obtained from the header directly.
sourcepub fn z(self) -> bool
pub fn z(self) -> bool
Returns whether the reserved bit is set.
This bit must be false
in all queries and responses.