6 releases (breaking)
0.5.0 | Feb 3, 2023 |
---|---|
0.4.1 | Aug 9, 2022 |
0.3.0 | Jul 25, 2022 |
0.2.0 | Jul 23, 2022 |
0.1.0 | Jul 17, 2022 |
#38 in #named
Used in 2 crates
(via dominion)
60KB
1K
SLoC
Dominion Parser
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.
Parsing
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);
}
Serializing
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);
}
Dependencies
~275–730KB
~17K SLoC