Crates.io | dominion-parser |
lib.rs | dominion-parser |
version | 0.5.0 |
source | src |
created_at | 2022-07-17 16:22:23.719346 |
updated_at | 2023-02-03 19:54:49.002855 |
description | DNS protocol parser |
homepage | |
repository | https://github.com/lopukhov/dominion |
max_upload_size | |
id | 627298 |
size | 89,099 |
DNS parser with a focus on usage of the type system to create a declarative experience when parsing or serializing DNS packets. It allows parsing and serializing whole packets or individual elements, like the header or the different questions and resource records. Not all resource records have been implemented, if some are missing that are relevant for your use case please open an issue.
use dominion_parser::DnsPacket;
const REQ: &'static [u8; 33] = include_bytes!("../assets/dns_request.bin");
fn main() {
let packet = DnsPacket::try_from(&REQ[..]).unwrap();
println!("The request was:");
println!("{:#?}", packet);
}
use dominion_parser::body::{RecordData, RecordPreamble, ResourceRecord};
use dominion_parser::header::{AuthenticData, QueryResponse, RecursionAvailable};
use dominion_parser::DnsPacket;
const REQ: &'static [u8; 33] = include_bytes!("../assets/dns_request.bin");
fn main() {
let mut res = DnsPacket::try_from(&REQ[..]).unwrap();
// Change some flags
res.header.flags.qr = QueryResponse::Response;
res.header.flags.ra = RecursionAvailable::Available;
res.header.flags.ad = AuthenticData::NotAuthentic;
// Add answer
let preamble = RecordPreamble {
name: res.questions[0].name.clone(),
rrtype: res.questions[0]
.qtype
.try_into()
.expect("QType is not a valid Type"),
class: res.questions[0].class,
ttl: 300,
rdlen: 4,
};
let data = RecordData::A("204.74.99.100".parse().unwrap());
let answer = ResourceRecord { preamble, data };
res.header.answers = 1;
res.answers.push(answer);
let res = Vec::<u8>::from(&res);
println!("=================== My Response ===================");
println!("{:?}", res);
}