use std::net::{Ipv4Addr, Ipv6Addr};
use dns_types::protocol::types::*;
use dns_types::zones::types::*;
pub const BLOCKED_A: RecordTypeWithData = RecordTypeWithData::A {
address: Ipv4Addr::UNSPECIFIED,
};
pub const BLOCKED_AAAA: RecordTypeWithData = RecordTypeWithData::AAAA {
address: Ipv6Addr::UNSPECIFIED,
};
pub struct Metrics {
pub authoritative_hits: u64,
pub override_hits: u64,
pub blocked: u64,
pub cache_misses: u64,
pub cache_hits: u64,
pub nameserver_hits: u64,
pub nameserver_misses: u64,
}
impl Metrics {
pub fn new() -> Self {
Metrics {
authoritative_hits: 0,
override_hits: 0,
blocked: 0,
cache_misses: 0,
cache_hits: 0,
nameserver_hits: 0,
nameserver_misses: 0,
}
}
pub fn zoneresult_answer(&mut self, rrs: &[ResourceRecord], zone: &Zone, question: &Question) {
if rrs.len() == 1 {
let rtype = &rrs[0].rtype_with_data;
if (question.qtype == QueryType::Record(RecordType::A) && rtype == &BLOCKED_A)
|| (question.qtype == QueryType::Record(RecordType::AAAA) && rtype == &BLOCKED_AAAA)
{
self.blocked += 1;
return;
}
}
if zone.is_authoritative() {
self.authoritative_hits += 1;
} else {
self.override_hits += 1;
}
}
pub fn zoneresult_cname(&mut self, zone: &Zone) {
if zone.is_authoritative() {
self.authoritative_hits += 1;
} else {
self.override_hits += 1;
}
}
pub fn zoneresult_delegation(&mut self, zone: &Zone) {
if zone.is_authoritative() {
self.authoritative_hits += 1;
}
}
pub fn zoneresult_nameerror(&mut self, zone: &Zone) {
if zone.is_authoritative() {
self.authoritative_hits += 1;
}
}
pub fn cache_hit(&mut self) {
self.cache_hits += 1;
}
pub fn cache_miss(&mut self) {
self.cache_misses += 1;
}
pub fn nameserver_hit(&mut self) {
self.nameserver_hits += 1;
}
pub fn nameserver_miss(&mut self) {
self.nameserver_misses += 1;
}
}
impl Default for Metrics {
fn default() -> Self {
Self::new()
}
}