Crate simple_dns
source · [−]Expand description
Simple DNS
Pure Rust implementation to work with DNS packets
You can parse or write a DNS packet by using Packet or PacketBuf structs
Packet
Packet holds references for the original data and it is more suitable for situations where you need to manipulate the packet before generating the final bytes buffer
use simple_dns::*;
use simple_dns::rdata::*;
let question = Question::new(Name::new_unchecked("_srv._udp.local"), QTYPE::TXT, QCLASS::IN, false);
let resource = ResourceRecord::new(Name::new_unchecked("_srv._udp.local"), CLASS::IN, 10, RData::A(A { address: 10 }));
let mut packet = Packet::new_query(1, false);
packet.questions.push(question);
packet.additional_records.push(resource);
let bytes = packet.build_bytes_vec();
assert!(bytes.is_ok());
// Same as above, but Names are compressed
let bytes = packet.build_bytes_vec_compressed();
assert!(bytes.is_ok());
It doesn’t matter what order the resources are added, the packet will be built only when build_bytes_vec
is called
To parse the contents of a buffer into a packet, you need call call Packet::parse
use simple_dns::Packet;
let bytes = b"\x00\x03\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x06\x67\x6f\x6f\x67\x6c\x65\x03\x63\x6f\x6d\x00\x00\x01\x00\x01";
let packet = Packet::parse(&bytes[..]);
assert!(packet.is_ok());
PacketBuf
PacketBuf holds an internal buffer that is populated right when a resource is added.
It DOES matter the order in which the resources are added
use simple_dns::*;
use simple_dns::rdata::*;
let question = Question::new(Name::new_unchecked("_srv._udp.local"), QTYPE::TXT, QCLASS::IN, false);
let resource = ResourceRecord::new(Name::new_unchecked("_srv._udp.local"), CLASS::IN, 10, RData::A(A { address: 10 }));
let mut packet = PacketBuf::new(PacketHeader::new_query(1, false), true);
assert!(packet.add_answer(&resource).is_ok());
assert!(packet.add_question(&question).is_err()); //This will fail, since an answer is already added
It is possible to create a PacketBuf from a buffer by calling PacketBuf::from, but be aware that this will clone the contents from the buffer
DNS Packet Parser/Builder
The Packet structure provides parsing e building of a DNS packet, it aims to be fully compliant with the RFCs bellow:
Other Resource Records defined by other RFCs that are not in this list will be implemented over time
Modules
Contains RData implementations
Structs
CharacterString is expressed in one or two ways:
A Name represents a domain-name, which consists of character strings separated by dots.
Each section of a name is called label
ex: google.com
consists of two labels google
and com
Represents a DNS message packet
Owned version of Packet
that contains a internal buffer.
This struct fills the internal buffer on the fly, because of this, it imposes some constraints.
You have to build the packet in order.
ex: It is not possible to add a question after an answer
Contains general information about the packet
Question represents a query in the DNS Packet
Iterate over the questions of a PacketBuf
If a question is not valid, the iterator will stop
Resource Records are used to represent the answer, authority, and additional sections in DNS packets.
Enums
Possible CLASS values for a Resource in a DNS packet
Each value is described according to its own RFC
Possible OPCODE values for a DNS packet, use to specify the type of operation.
RFC 1035: A four bit field that specifies kind of query in this message.
This value is set by the originator of a query and copied into the response.
Possible QCLASS values for a Question in a DNS packet
Each value is described according to its own RFC
Possible QTYPE values for a Question in a DNS packet
Each value is described according to its own RFC
Possible RCODE values for a DNS packet
RFC 1035 Response code - this 4 bit field is set as part of responses.
The values have the following interpretation
Error types for SimpleDns
Possible TYPE values in DNS Resource Records
Each value is described according to its own RFC
Type Definitions
Alias type for Result<T, SimpleDnsError>;