This project implements a High-Performance DNS (Domain Name System) server in Rust. The server listens for DNS queries, processes them, and responds appropriately.
git clone https://github.com/JeninSutradhar/Rust-Dns-Resolver-Server
cd Rust-Dns-Resolver-Server
cargo build --release
cargo run --release
dig @127.0.0.1 -p 8080 example.com
dig @127.0.0.1 -p 8080 yahoo.com
Received query: Question { name: "example.com", qtype: A }
Answer: A { domain: "example.com", addr: 93.184.216.34, ttl: 3600 }
fn main() -> Result<(), Box<dyn Error>> {
// Bind to the UDP socket
let socket = UdpSocket::bind(("0.0.0.0", 8080))?; // Change the address and port here
// Continuously handle incoming queries
loop {
match dns::handle_query(&socket) {
Ok(_) => {},
Err(e) => eprintln!("An error occurred: {}", e),
}
}
}
The main.rs file is the entry point of the application. It sets up a UDP socket that listens for incoming DNS queries on a specified address and port. The main function enters an infinite loop, continuously calling the handle_query function to process incoming requests. Any errors encountered during this process are logged to the console.
The byte_packet_buffer.rs module provides a buffer implementation for reading and writing raw bytes. This buffer is used to parse DNS packets from incoming data and to serialize DNS packets for outgoing responses. It includes methods for reading and writing various data types, such as strings, integers, and byte arrays.
The dns_packet.rs module defines the structure of a DNS packet. It includes definitions for the DNS header, questions, answers, and other sections of a DNS message. This module also provides methods for serializing and deserializing DNS packets, making it easy to convert between raw bytes and structured data.
The dns_record.rs module defines the various types of DNS records, such as A, NS, and CNAME records. It includes methods for reading and writing these records to and from the byte buffer. This module also provides functionality for handling the different record types and their associated data.
The dns.rs module contains the core logic for handling DNS packets. It includes functions for reading and writing DNS packets, processing queries, and generating responses. The handle_query function reads an incoming DNS query from the UDP socket, parses it, and constructs an appropriate response. If a recursive lookup is necessary, the function performs the lookup and includes the results in the response.