Crates.io | rusdig |
lib.rs | rusdig |
version | |
source | src |
created_at | 2024-10-19 17:33:26.569714 |
updated_at | 2024-12-08 14:20:56.417006 |
description | Lightweight DNS message parsing and encoding library written in Rust |
homepage | |
repository | https://github.com/Kek5chen/rusdig |
max_upload_size | |
id | 1415603 |
Cargo.toml error: | TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
A lightweight DNS message parsing and encoding library written in Rust. This library allows you to create DNS queries, parse DNS responses, and handle various DNS record types such as A, AAAA, CNAME, NS, MX, TXT, and SRV.
DNS Message Construction: Easily build DNS queries for a given hostname and record type.
DNS Message Parsing: Parse raw DNS response bytes into structured queries, answers, and authority sections.
Record Type Support: Built-in support for common DNS record types:
Robust Error Handling:
Comprehensive error types (DNSParseError
) to handle invalid data, decoding issues, and malformed responses.
Name Encoding and Decoding: Encode and decode DNS names, including compressed names, with recursion protection to avoid infinite loops.
std::net::UdpSocket
) if you intend to resolve hostnames via a DNS server. This library focuses on message construction and parsing; it does not itself handle I/O.
Constructing a Query:
use rusdig::{Query, RecordType};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a DNS query for an A record for example.com
let query = Query::for_name("example.com", RecordType::A);
let query_bytes = query.as_bytes()?;
// Send `query_bytes` to a DNS server via UDP (not shown here)
Ok(())
}
Parsing a Response:
use rusdig::{Query, DNSParseError};
fn parse_dns_response(response_bytes: &[u8]) -> Result<(), DNSParseError> {
let response = Query::from_bytes(response_bytes)?;
// Check if the response is successful
if response.flags.successful() {
for answer in &response.resource_answers {
println!("Name: {}", answer.name);
println!("Type: {:?}", answer.entry_type());
println!("TTL: {}", answer.time_to_live);
// Extract data based on record type
match answer.entry_type() {
rusdig::RecordType::A => {
let ipv4 = answer.data_as_ipv4()?;
println!("IPv4: {}", ipv4);
}
rusdig::RecordType::AAAA => {
let ipv6 = answer.data_as_ipv6()?;
println!("IPv6: {}", ipv6);
}
rusdig::RecordType::TXT => {
let text = answer.data_as_text_lossy()?;
println!("TXT Data: {}", text);
}
_ => println!("Unsupported or unhandled record type"),
}
println!();
}
} else {
eprintln!("DNS query was not successful");
}
Ok(())
}
Contributions are welcome! Feel free to open issues or submit pull requests with improvements, bug fixes, or additional features.
This project is licensed under the MIT License.