2 releases

0.1.1 Sep 21, 2022
0.1.0 Sep 12, 2022

#24 in #dns-lookup

Download history 8/week @ 2024-02-19 19/week @ 2024-02-26 6/week @ 2024-03-04 13/week @ 2024-03-11 5/week @ 2024-03-18 5/week @ 2024-03-25 65/week @ 2024-04-01

89 downloads per month
Used in 3 crates (2 directly)

MIT/Apache

57KB
1K SLoC

dns-protocol

This crate provides a no_std implementation of the DNS protocol.

In order to make it trivial for others to build implementations of the DNS protocol, this crate provides a sans-I/O implementation of the protocol. This means that it doesn't provide any I/O functionality, but instead provides a way to parse and serialize DNS messages.

In addition, this crate is not only no_std, but also alloc-free. This means that it can be used in environments where alloc is not available, such as embedded systems. It also has no unsafe code.

However, there is a catch. Since this system does not allocate, the user is responsible for providing a buffer to parse DNS messages into. This means that the user must know the maximum size of a DNS message that they will be parsing. This is a reasonable assumption, since DNS messages are limited to 512 bytes in the common case.

License

Dual licensed under the MIT and Apache 2.0 licenses.


lib.rs:

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 the std library for use in Error types. Disable this feature to use on no_std targets.

Dependencies

~175–315KB