Rust

server

Module

The server: decode a query, resolve it, encode the response.

Contents

use utopia_dns::Server;
Rust
pub struct Server<R>

A DNS server: decodes each packet, asks the resolver, and encodes the response within what the transport carries.

  • A packet whose header decodes but whose sections do not gets FORMERR; one too short for a header gets no answer.
  • Opcodes other than QUERY get NOTIMP; a query without a question gets FORMERR.
  • A resolver error, or a response that cannot be encoded, gets SERVFAIL.

Errors go to the handlers added with Server::error. Telemetry records the decode, resolve and encode durations (dns.query.duration, seconds, by phase and responseCode) and counts queries and responses (dns.queries.total by type, dns.responses.total by type and responseCode).

Rust
use utopia_dns::message::{Message, Question, Record};
use utopia_dns::resolver::Memory;
use utopia_dns::{Handler, Protocol, Server, Zone};

let zone = Zone::new(
    "example.test",
    vec![Record::new("www.example.test", Record::TYPE_A).with_ttl(60).with_rdata("192.0.2.1")],
    Record::new("example.test", Record::TYPE_SOA)
        .with_ttl(60)
        .with_rdata("ns1.example.test hostmaster.example.test 1 7200 1800 1209600 3600"),
)?;
let server = Server::new(Memory::new(zone));

let query = Message::query(Question::new("www.example.test", Record::TYPE_A), Some(7), true);
let packet = server.handle(&query.encode(None)?, "127.0.0.1", 5300, Protocol::Udp).await;
let response = Message::decode(&packet)?;
assert_eq!(response.answers()[0].rdata(), b"192.0.2.1");
use utopia_dns::Handler;
Rust
pub trait Handler: Send + Sync + 'static

What turns a packet into the packet to answer with: implemented by Server, called by Adapters for every packet they receive.

Implemented byServer
Rust
pub type ErrorHandler = Box<dyn Fn(&Error) + Send + Sync>

An error handler.