Expand description
An implementation of the DNS protocol, sans I/O.
This crate implements the Domain System Protocol, used for namespace lookups among other things. It is intended to be used in conjunction with a transport layer, such as UDP, TCP or HTTPS The goal of this crate is to provide a runtime and protocol-agnostic implementation of the DNS protocol, so that it can be used in a variety of contexts.
This crate is not only no_std
, it does not use an allocator as well. This means that it
can be used on embedded systems that do not have allocators, and that it does no allocation
of its own. However, this comes with a catch: the user is expected to provide their own
buffers for the various operations. This is done to avoid the need for an allocator, and
to allow the user to control the memory usage of the library.
This crate is also #![forbid(unsafe_code)]
, and is intended to remain so.
§Example
use dns_protocol::{Message, Question, ResourceRecord, ResourceType, Flags};
use std::net::UdpSocket;
// Allocate a buffer for the message.
let mut buf = vec![0; 1024];
// Create a message. This is a query for the A record of example.com.
let mut questions = [
Question::new(
"example.com",
ResourceType::A,
0,
)
];
let mut answers = [ResourceRecord::default()];
let message = Message::new(0x42, Flags::default(), &mut questions, &mut answers, &mut [], &mut []);
// Serialize the message into the buffer
assert!(message.space_needed() <= buf.len());
let len = message.write(&mut buf)?;
// Write the buffer to the socket.
let socket = UdpSocket::bind("localhost:0")?;
socket.send_to(&buf[..len], "1.2.3.4:53")?;
// Read new data from the socket.
let data_len = socket.recv(&mut buf)?;
// Parse the data as a message.
let message = Message::read(
&buf[..data_len],
&mut questions,
&mut answers,
&mut [],
&mut [],
)?;
// Read the answer from the message.
let answer = message.answers()[0];
println!("Answer Data: {:?}", answer.data());
§Features
std
(enabled by default) - Enables thestd
library for use inError
types. Disable this feature to use onno_std
targets.
Structs§
- Flags
- The flags associated with a DNS message.
- Header
- The header for a DNS query.
- Invalid
Code - The given value is not a valid code.
- Label
- A DNS name.
- Message
- The message for a DNS query.
- Question
- The question in a DNS query.
- Resource
Record - A resource record in a DNS query.
Enums§
- Error
- An error that may occur while using the DNS protocol.
- Label
Segment - A DNS-compatible label segment.
- Message
Type - Whether a message is a query or a reply.
- Opcode
- The operation code for the query.
- Resource
Type - The resource types that a question can ask for.
- Response
Code - The response code for a query.