feat(question): name SVCB/LOC/NAPTR record types in logs #120

Merged
razvandimescu merged 2 commits from feat/named-record-types into main 2026-04-19 13:08:55 +08:00
Showing only changes of commit 5725f94ff3 - Show all commits

View File

@@ -1,129 +1,66 @@
use crate::buffer::BytePacketBuffer; use crate::buffer::BytePacketBuffer;
use crate::Result; use crate::Result;
#[derive(PartialEq, Eq, Debug, Clone, Hash, Copy)] macro_rules! define_qtypes {
pub enum QueryType { ( $( $variant:ident = $num:literal, $str:literal ),* $(,)? ) => {
UNKNOWN(u16), #[derive(PartialEq, Eq, Debug, Clone, Hash, Copy)]
A, // 1 pub enum QueryType {
NS, // 2 UNKNOWN(u16),
CNAME, // 5 $( $variant, )*
SOA, // 6 }
PTR, // 12
MX, // 15 impl QueryType {
TXT, // 16 pub fn to_num(&self) -> u16 {
AAAA, // 28 match *self {
LOC, // 29 QueryType::UNKNOWN(x) => x,
SRV, // 33 $( QueryType::$variant => $num, )*
NAPTR, // 35 }
DS, // 43 }
RRSIG, // 46
NSEC, // 47 pub fn from_num(num: u16) -> QueryType {
DNSKEY, // 48 match num {
NSEC3, // 50 $( $num => QueryType::$variant, )*
OPT, // 41 (EDNS0 pseudo-type) _ => QueryType::UNKNOWN(num),
SVCB, // 64 }
HTTPS, // 65 }
pub fn as_str(&self) -> &'static str {
match self {
QueryType::UNKNOWN(_) => "UNKNOWN",
$( QueryType::$variant => $str, )*
}
}
pub fn parse_str(s: &str) -> Option<QueryType> {
match s.to_ascii_uppercase().as_str() {
$( $str => Some(QueryType::$variant), )*
_ => None,
}
}
}
};
} }
impl QueryType { define_qtypes! {
pub fn to_num(&self) -> u16 { A = 1, "A",
match *self { NS = 2, "NS",
QueryType::UNKNOWN(x) => x, CNAME = 5, "CNAME",
QueryType::A => 1, SOA = 6, "SOA",
QueryType::NS => 2, PTR = 12, "PTR",
QueryType::CNAME => 5, MX = 15, "MX",
QueryType::SOA => 6, TXT = 16, "TXT",
QueryType::PTR => 12, AAAA = 28, "AAAA",
QueryType::MX => 15, LOC = 29, "LOC",
QueryType::TXT => 16, SRV = 33, "SRV",
QueryType::AAAA => 28, NAPTR = 35, "NAPTR",
QueryType::LOC => 29, OPT = 41, "OPT",
QueryType::SRV => 33, DS = 43, "DS",
QueryType::NAPTR => 35, RRSIG = 46, "RRSIG",
QueryType::OPT => 41, NSEC = 47, "NSEC",
QueryType::DS => 43, DNSKEY = 48, "DNSKEY",
QueryType::RRSIG => 46, NSEC3 = 50, "NSEC3",
QueryType::NSEC => 47, SVCB = 64, "SVCB",
QueryType::DNSKEY => 48, HTTPS = 65, "HTTPS",
QueryType::NSEC3 => 50,
QueryType::SVCB => 64,
QueryType::HTTPS => 65,
}
}
pub fn from_num(num: u16) -> QueryType {
match num {
1 => QueryType::A,
2 => QueryType::NS,
5 => QueryType::CNAME,
6 => QueryType::SOA,
12 => QueryType::PTR,
15 => QueryType::MX,
16 => QueryType::TXT,
28 => QueryType::AAAA,
29 => QueryType::LOC,
33 => QueryType::SRV,
35 => QueryType::NAPTR,
41 => QueryType::OPT,
43 => QueryType::DS,
46 => QueryType::RRSIG,
47 => QueryType::NSEC,
48 => QueryType::DNSKEY,
50 => QueryType::NSEC3,
64 => QueryType::SVCB,
65 => QueryType::HTTPS,
_ => QueryType::UNKNOWN(num),
}
}
pub fn as_str(&self) -> &'static str {
match self {
QueryType::A => "A",
QueryType::NS => "NS",
QueryType::CNAME => "CNAME",
QueryType::SOA => "SOA",
QueryType::PTR => "PTR",
QueryType::MX => "MX",
QueryType::TXT => "TXT",
QueryType::AAAA => "AAAA",
QueryType::LOC => "LOC",
QueryType::SRV => "SRV",
QueryType::NAPTR => "NAPTR",
QueryType::OPT => "OPT",
QueryType::DS => "DS",
QueryType::RRSIG => "RRSIG",
QueryType::NSEC => "NSEC",
QueryType::DNSKEY => "DNSKEY",
QueryType::NSEC3 => "NSEC3",
QueryType::SVCB => "SVCB",
QueryType::HTTPS => "HTTPS",
QueryType::UNKNOWN(_) => "UNKNOWN",
}
}
pub fn parse_str(s: &str) -> Option<QueryType> {
match s.to_ascii_uppercase().as_str() {
"A" => Some(QueryType::A),
"NS" => Some(QueryType::NS),
"CNAME" => Some(QueryType::CNAME),
"SOA" => Some(QueryType::SOA),
"PTR" => Some(QueryType::PTR),
"MX" => Some(QueryType::MX),
"TXT" => Some(QueryType::TXT),
"AAAA" => Some(QueryType::AAAA),
"LOC" => Some(QueryType::LOC),
"SRV" => Some(QueryType::SRV),
"NAPTR" => Some(QueryType::NAPTR),
"DS" => Some(QueryType::DS),
"RRSIG" => Some(QueryType::RRSIG),
"DNSKEY" => Some(QueryType::DNSKEY),
"NSEC" => Some(QueryType::NSEC),
"NSEC3" => Some(QueryType::NSEC3),
"SVCB" => Some(QueryType::SVCB),
"HTTPS" => Some(QueryType::HTTPS),
_ => None,
}
}
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]