use dashmap::DashMap;
use std::net::IpAddr;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, Instant, SystemTime};
use crate::network::types::{
AttributedHostname, AttributionSource, Connection, ConnectionKey, Protocol,
};
const MAX_DOMAINS_PER_IP: usize = 4;
pub(crate) const DEFAULT_FRESH_WINDOW: Duration = Duration::from_secs(10);
pub(crate) const DEFAULT_RETENTION: Duration = Duration::from_secs(600);
pub(crate) const DEFAULT_MAX_ENTRIES: usize = 8192;
const MAX_PENDING_PER_IP: usize = 256;
#[derive(Debug, Clone)]
pub(crate) struct AttributedDomain {
pub domain: String,
pub observed_at: Instant,
pub source: AttributionSource,
}
#[derive(Debug, Clone, Copy)]
struct PendingEntry {
conn_key: ConnectionKey,
enrolled_at: Instant,
}
#[derive(Debug)]
pub(crate) struct DnsAttributionCache {
map: DashMap<IpAddr, Vec<AttributedDomain>>,
pending: DashMap<IpAddr, Vec<PendingEntry>>,
map_len: AtomicUsize,
max_entries: usize,
fresh_window: Duration,
retention: Duration,
}
impl Default for DnsAttributionCache {
fn default() -> Self {
Self::new(DEFAULT_MAX_ENTRIES, DEFAULT_FRESH_WINDOW, DEFAULT_RETENTION)
}
}
impl DnsAttributionCache {
pub(crate) fn new(max_entries: usize, fresh_window: Duration, retention: Duration) -> Self {
Self {
map: DashMap::new(),
pending: DashMap::new(),
map_len: AtomicUsize::new(0),
max_entries,
fresh_window,
retention,
}
}
pub(crate) fn record(&self, query_name: &str, ips: &[IpAddr], source: AttributionSource) {
let domain = query_name.trim().trim_end_matches('.');
if domain.is_empty() || ips.is_empty() {
return;
}
let now = Instant::now();
for ip in ips {
if ip.is_unspecified() || ip.is_loopback() {
continue;
}
self.map
.entry(*ip)
.and_modify(|entries| {
if let Some(existing) = entries.iter_mut().find(|d| d.domain == domain) {
existing.observed_at = now;
existing.source = source;
} else {
entries.push(AttributedDomain {
domain: domain.to_string(),
observed_at: now,
source,
});
}
if entries.len() > MAX_DOMAINS_PER_IP {
entries.sort_by_key(|d| std::cmp::Reverse(d.observed_at));
entries.truncate(MAX_DOMAINS_PER_IP);
}
})
.or_insert_with(|| {
self.map_len.fetch_add(1, Ordering::Relaxed);
vec![AttributedDomain {
domain: domain.to_string(),
observed_at: now,
source,
}]
});
}
if self.map_len.load(Ordering::Relaxed) > self.max_entries {
self.prune(now);
}
}
pub(crate) fn lookup(&self, ip: IpAddr) -> Option<(AttributedDomain, bool)> {
let entries = self.map.get(&ip)?;
let now = Instant::now();
let freshest = entries
.iter()
.filter(|d| now.saturating_duration_since(d.observed_at) <= self.retention)
.max_by_key(|d| d.observed_at)?
.clone();
let fresh = now.saturating_duration_since(freshest.observed_at) <= self.fresh_window;
Some((freshest, fresh))
}
pub(crate) fn attribute(&self, conn: &mut Connection, key: ConnectionKey) {
if conn.protocol == Protocol::Arp || conn.attributed_hostname.is_some() {
return;
}
if conn.authoritative_hostname().is_some() {
return;
}
if is_unattributable_protocol(conn) {
return;
}
let ip = conn.remote_addr.ip();
if let Some((att, fresh)) = self.lookup(ip)
&& fresh
{
conn.attributed_hostname = Some(to_attributed_hostname(att));
return;
}
self.enroll_pending(ip, key);
if let Some((att, fresh)) = self.lookup(ip)
&& fresh
{
conn.attributed_hostname = Some(to_attributed_hostname(att));
}
}
fn enroll_pending(&self, ip: IpAddr, conn_key: ConnectionKey) {
let now = Instant::now();
self.pending
.entry(ip)
.and_modify(|v| {
if v.iter().all(|e| e.conn_key != conn_key) && v.len() < MAX_PENDING_PER_IP {
v.push(PendingEntry {
conn_key,
enrolled_at: now,
});
}
})
.or_insert_with(|| {
vec![PendingEntry {
conn_key,
enrolled_at: now,
}]
});
}
pub(crate) fn forget_pending(&self, ip: IpAddr, conn_key: ConnectionKey) {
let now_empty = self.pending.get_mut(&ip).map(|mut entry| {
entry.retain(|e| e.conn_key != conn_key);
entry.is_empty()
});
if matches!(now_empty, Some(true)) {
self.pending.remove(&ip);
}
}
pub(crate) fn record_and_drain_pending(
&self,
query_name: &str,
ips: &[IpAddr],
source: AttributionSource,
) -> Vec<ConnectionKey> {
self.record(query_name, ips, source);
let now = Instant::now();
let mut keys = Vec::new();
for ip in ips {
if ip.is_unspecified() || ip.is_loopback() {
continue;
}
if let Some((_, waiters)) = self.pending.remove(ip) {
for entry in waiters {
if now.saturating_duration_since(entry.enrolled_at) <= self.fresh_window {
keys.push(entry.conn_key);
}
}
}
}
keys
}
pub(crate) fn prune_pending(&self, now: Instant) {
self.pending.retain(|_ip, entries| {
entries.retain(|e| now.saturating_duration_since(e.enrolled_at) <= self.fresh_window);
!entries.is_empty()
});
}
pub(crate) fn cleanup_tick(&self, now: Instant) {
self.prune(now);
self.prune_pending(now);
}
pub(crate) fn prune(&self, now: Instant) {
self.map.retain(|_ip, entries| {
entries.retain(|d| now.saturating_duration_since(d.observed_at) <= self.retention);
!entries.is_empty()
});
if self.map.len() > self.max_entries {
let target = self.max_entries - self.max_entries / 8;
let mut ages: Vec<(IpAddr, Instant)> = self
.map
.iter()
.map(|kv| {
let freshest = kv
.value()
.iter()
.map(|d| d.observed_at)
.max()
.unwrap_or(now);
(*kv.key(), freshest)
})
.collect();
let to_drop = ages.len().saturating_sub(target);
if to_drop > 0 {
ages.select_nth_unstable_by_key(to_drop - 1, |(_, t)| *t);
for (ip, _) in ages.into_iter().take(to_drop) {
self.map.remove(&ip);
}
}
}
self.map_len.store(self.map.len(), Ordering::Relaxed);
}
pub(crate) fn clear(&self) {
self.map.clear();
self.pending.clear();
self.map_len.store(0, Ordering::Relaxed);
}
}
fn is_unattributable_protocol(conn: &Connection) -> bool {
use crate::network::types::ApplicationProtocol;
let Some(dpi) = &conn.dpi_info else {
return false;
};
matches!(
dpi.application,
ApplicationProtocol::Dns(_)
| ApplicationProtocol::Mdns(_)
| ApplicationProtocol::Llmnr(_)
| ApplicationProtocol::Dhcp(_)
| ApplicationProtocol::Ssdp(_)
| ApplicationProtocol::NetBios(_)
)
}
fn to_attributed_hostname(att: AttributedDomain) -> AttributedHostname {
let now = SystemTime::now();
AttributedHostname {
name: att.domain,
source: att.source,
observed_at: now.checked_sub(att.observed_at.elapsed()).unwrap_or(now),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::network::types::{Protocol, ProtocolState, TcpState};
use std::net::{Ipv4Addr, SocketAddr};
use std::thread::sleep;
fn ip(a: u8, b: u8, c: u8, d: u8) -> IpAddr {
IpAddr::V4(Ipv4Addr::new(a, b, c, d))
}
fn key_of(conn: &Connection) -> ConnectionKey {
ConnectionKey::from_connection(conn)
}
fn tcp_connection() -> Connection {
Connection::new(
Protocol::Tcp,
"192.168.1.10:54321".parse().unwrap(),
"1.2.3.4:443".parse().unwrap(),
ProtocolState::Tcp(TcpState::Established),
)
}
#[test]
fn fresh_lookup_after_record() {
let cache = DnsAttributionCache::default();
cache.record("foo.com", &[ip(1, 2, 3, 4)], AttributionSource::CapturedDns);
let (att, fresh) = cache.lookup(ip(1, 2, 3, 4)).expect("hit");
assert_eq!(att.domain, "foo.com");
assert!(fresh);
}
#[test]
fn lookup_past_fresh_window_is_not_fresh() {
let cache = DnsAttributionCache::new(
DEFAULT_MAX_ENTRIES,
Duration::from_millis(20),
Duration::from_secs(60),
);
cache.record("foo.com", &[ip(1, 2, 3, 4)], AttributionSource::CapturedDns);
sleep(Duration::from_millis(40));
let (att, fresh) = cache.lookup(ip(1, 2, 3, 4)).expect("hit");
assert_eq!(att.domain, "foo.com");
assert!(!fresh, "should be stale");
}
#[test]
fn lookup_past_retention_is_miss() {
let cache = DnsAttributionCache::new(
DEFAULT_MAX_ENTRIES,
Duration::from_millis(5),
Duration::from_millis(20),
);
cache.record("foo.com", &[ip(1, 2, 3, 4)], AttributionSource::CapturedDns);
sleep(Duration::from_millis(40));
assert!(cache.lookup(ip(1, 2, 3, 4)).is_none());
}
#[test]
fn most_recent_wins_for_shared_ip() {
let cache = DnsAttributionCache::default();
cache.record(
"first.com",
&[ip(1, 1, 1, 1)],
AttributionSource::CapturedDns,
);
sleep(Duration::from_millis(5));
cache.record(
"second.com",
&[ip(1, 1, 1, 1)],
AttributionSource::CapturedDns,
);
let (att, _fresh) = cache.lookup(ip(1, 1, 1, 1)).expect("hit");
assert_eq!(att.domain, "second.com");
}
#[test]
fn record_dedupes_same_domain() {
let cache = DnsAttributionCache::default();
for _ in 0..10 {
cache.record("foo.com", &[ip(1, 2, 3, 4)], AttributionSource::CapturedDns);
}
let entries = cache.map.get(&ip(1, 2, 3, 4)).unwrap();
assert_eq!(entries.len(), 1);
}
#[test]
fn ipv6_path() {
let cache = DnsAttributionCache::default();
let v6 = "2606:4700:4700::1111".parse::<IpAddr>().unwrap();
cache.record("dns.cloudflare.com", &[v6], AttributionSource::CapturedDns);
let (att, fresh) = cache.lookup(v6).expect("hit");
assert_eq!(att.domain, "dns.cloudflare.com");
assert!(fresh);
}
#[test]
fn loopback_and_unspecified_ignored() {
let cache = DnsAttributionCache::default();
cache.record(
"lo.example",
&[ip(127, 0, 0, 1), ip(0, 0, 0, 0), ip(8, 8, 8, 8)],
AttributionSource::CapturedDns,
);
assert!(cache.lookup(ip(127, 0, 0, 1)).is_none());
assert!(cache.lookup(ip(0, 0, 0, 0)).is_none());
assert!(cache.lookup(ip(8, 8, 8, 8)).is_some());
}
#[test]
fn cap_enforced_via_prune() {
let cache = DnsAttributionCache::new(16, Duration::from_secs(60), Duration::from_secs(60));
for i in 0..200u32 {
let octets = i.to_be_bytes();
cache.record(
"name.example",
&[ip(10, octets[1], octets[2], octets[3])],
AttributionSource::CapturedDns,
);
}
assert!(
cache.map.len() <= 16,
"cache exceeded cap: {}",
cache.map.len()
);
}
#[test]
fn hard_cap_prunes_to_watermark() {
let cache = DnsAttributionCache::new(16, Duration::from_secs(60), Duration::from_secs(60));
for i in 0..17u32 {
let octets = i.to_be_bytes();
cache.record(
"name.example",
&[ip(10, octets[1], octets[2], octets[3])],
AttributionSource::CapturedDns,
);
}
assert_eq!(cache.map.len(), 14);
}
#[test]
fn attribute_tags_fresh_connection() {
let cache = DnsAttributionCache::default();
cache.record(
"youtube.com",
&[ip(142, 250, 1, 1)],
AttributionSource::CapturedDns,
);
let local: SocketAddr = "192.168.1.10:54321".parse().unwrap();
let remote: SocketAddr = "142.250.1.1:443".parse().unwrap();
let mut conn = Connection::new(Protocol::Udp, local, remote, ProtocolState::Udp);
let key = key_of(&conn);
cache.attribute(&mut conn, key);
let att = conn.attributed_hostname.expect("attribution applied");
assert_eq!(att.name, "youtube.com");
assert_eq!(att.source, AttributionSource::CapturedDns);
}
#[test]
fn attribute_stamps_dns_observation_time() {
let cache = DnsAttributionCache::default();
cache.record("foo.com", &[ip(1, 2, 3, 4)], AttributionSource::CapturedDns);
sleep(Duration::from_millis(50));
let local: SocketAddr = "192.168.1.10:54321".parse().unwrap();
let remote: SocketAddr = "1.2.3.4:443".parse().unwrap();
let mut conn = Connection::new(Protocol::Udp, local, remote, ProtocolState::Udp);
let key = key_of(&conn);
cache.attribute(&mut conn, key);
let att = conn.attributed_hostname.expect("attribution applied");
let age = att.observed_at.elapsed().expect("wall clock went forward");
assert!(age >= Duration::from_millis(40), "age too small: {age:?}");
}
#[test]
fn attribute_first_write_wins() {
let cache = DnsAttributionCache::default();
cache.record("a.com", &[ip(1, 2, 3, 4)], AttributionSource::CapturedDns);
let mut conn = tcp_connection();
let key = key_of(&conn);
cache.attribute(&mut conn, key);
cache.record("b.com", &[ip(1, 2, 3, 4)], AttributionSource::CapturedDns);
cache.attribute(&mut conn, key);
assert_eq!(conn.attributed_hostname.unwrap().name, "a.com");
}
#[test]
fn attribute_skips_dns_self_attribution() {
use crate::network::types::{ApplicationProtocol, DnsInfo, DpiInfo};
let cache = DnsAttributionCache::default();
cache.record(
"resolver.example",
&[ip(8, 8, 8, 8)],
AttributionSource::CapturedDns,
);
let local: SocketAddr = "192.168.1.10:54321".parse().unwrap();
let remote: SocketAddr = "8.8.8.8:53".parse().unwrap();
let mut conn = Connection::new(Protocol::Udp, local, remote, ProtocolState::Udp);
conn.dpi_info = Some(DpiInfo {
application: ApplicationProtocol::Dns(DnsInfo {
query_name: Some("foo.com".into()),
query_type: None,
response_ips: vec![],
is_response: false,
txid: 0x1234,
rcode: None,
nodata: None,
}),
});
let key = key_of(&conn);
cache.attribute(&mut conn, key);
assert!(
conn.attributed_hostname.is_none(),
"DNS connection must not be self-attributed"
);
}
#[test]
fn attribute_skips_local_discovery_protocols() {
use crate::network::types::{
ApplicationProtocol, DhcpInfo, DhcpMessageType, DpiInfo, LlmnrInfo, MdnsInfo,
NetBiosInfo, NetBiosOpcode, NetBiosService, SsdpInfo, SsdpMethod,
};
let cache = DnsAttributionCache::default();
cache.record(
"should-not-be-applied.example",
&[ip(1, 2, 3, 4)],
AttributionSource::CapturedDns,
);
let local: SocketAddr = "192.168.1.10:5353".parse().unwrap();
let remote: SocketAddr = "1.2.3.4:5353".parse().unwrap();
let make_conn = |app: ApplicationProtocol| {
let mut c = Connection::new(Protocol::Udp, local, remote, ProtocolState::Udp);
c.dpi_info = Some(DpiInfo { application: app });
c
};
let cases: Vec<(&str, ApplicationProtocol)> = vec![
(
"mdns",
ApplicationProtocol::Mdns(MdnsInfo {
query_name: None,
query_type: None,
is_response: false,
response_ips: vec![],
}),
),
(
"llmnr",
ApplicationProtocol::Llmnr(LlmnrInfo {
query_name: None,
query_type: None,
is_response: false,
response_ips: vec![],
txid: 0,
}),
),
(
"dhcp",
ApplicationProtocol::Dhcp(DhcpInfo {
message_type: DhcpMessageType::Discover,
hostname: None,
client_mac: None,
}),
),
(
"ssdp",
ApplicationProtocol::Ssdp(SsdpInfo {
method: SsdpMethod::MSearch,
service_type: None,
}),
),
(
"netbios",
ApplicationProtocol::NetBios(NetBiosInfo {
service: NetBiosService::NameService,
opcode: NetBiosOpcode::Query,
name: None,
transaction_id: 0x1234,
is_response: false,
response_status: None,
}),
),
];
for (label, app) in cases {
let mut conn = make_conn(app);
let key = key_of(&conn);
cache.attribute(&mut conn, key);
assert!(
conn.attributed_hostname.is_none(),
"{} connections must not be attributed",
label
);
}
}
#[test]
fn attribute_skips_when_sni_present() {
use crate::network::types::{ApplicationProtocol, DpiInfo, HttpsInfo, TlsInfo};
let cache = DnsAttributionCache::default();
cache.record(
"attribution.example",
&[ip(1, 2, 3, 4)],
AttributionSource::CapturedDns,
);
let mut conn = tcp_connection();
conn.dpi_info = Some(DpiInfo {
application: ApplicationProtocol::Https(HttpsInfo {
tls_info: Some(TlsInfo {
version: None,
sni: Some("sni.example".into()),
alpn: vec![],
cipher_suite: None,
}),
}),
});
let key = key_of(&conn);
cache.attribute(&mut conn, key);
assert!(
conn.attributed_hostname.is_none(),
"SNI-bearing connections should short-circuit attribution"
);
}
#[test]
fn attribute_proceeds_when_sni_partial() {
use crate::network::types::{ApplicationProtocol, DpiInfo, HttpsInfo, TlsInfo};
let cache = DnsAttributionCache::default();
cache.record(
"real.example",
&[ip(1, 2, 3, 4)],
AttributionSource::CapturedDns,
);
let mut conn = tcp_connection();
conn.dpi_info = Some(DpiInfo {
application: ApplicationProtocol::Https(HttpsInfo {
tls_info: Some(TlsInfo {
version: None,
sni: Some("partial[PARTIAL]".into()),
alpn: vec![],
cipher_suite: None,
}),
}),
});
let key = key_of(&conn);
cache.attribute(&mut conn, key);
assert_eq!(
conn.attributed_hostname.as_ref().map(|a| a.name.as_str()),
Some("real.example"),
"[PARTIAL] SNI is not authoritative; attribution should still apply"
);
}
#[test]
fn attribute_skips_when_not_fresh() {
let cache = DnsAttributionCache::new(
DEFAULT_MAX_ENTRIES,
Duration::from_millis(10),
Duration::from_secs(60),
);
cache.record("foo.com", &[ip(1, 2, 3, 4)], AttributionSource::CapturedDns);
sleep(Duration::from_millis(40));
let mut conn = tcp_connection();
let key = key_of(&conn);
cache.attribute(&mut conn, key);
assert!(
conn.attributed_hostname.is_none(),
"stale entry must not be applied"
);
}
#[test]
fn attribute_enrolls_on_miss_then_drain_attributes() {
let cache = DnsAttributionCache::default();
let mut conn = tcp_connection();
let key = key_of(&conn);
cache.attribute(&mut conn, key);
assert!(conn.attributed_hostname.is_none());
assert_eq!(cache.pending.len(), 1);
let drained = cache.record_and_drain_pending(
"late.example",
&[ip(1, 2, 3, 4)],
AttributionSource::CapturedDns,
);
assert_eq!(drained.len(), 1);
assert_eq!(drained[0], key);
assert!(
cache.pending.is_empty(),
"pending entry should be removed on drain"
);
cache.attribute(&mut conn, key);
assert_eq!(
conn.attributed_hostname.as_ref().map(|a| a.name.as_str()),
Some("late.example")
);
}
#[test]
fn forget_pending_clears_dead_keys() {
let cache = DnsAttributionCache::default();
let mut conn = tcp_connection();
let key = key_of(&conn);
cache.attribute(&mut conn, key);
assert_eq!(cache.pending.len(), 1);
cache.forget_pending(conn.remote_addr.ip(), key);
assert!(
cache.pending.is_empty(),
"forget_pending should drop the IP entry once empty"
);
let drained = cache.record_and_drain_pending(
"x.example",
&[ip(1, 2, 3, 4)],
AttributionSource::CapturedDns,
);
assert!(drained.is_empty());
}
#[test]
fn pending_ttl_drops_expired_enrollments_on_drain() {
let cache = DnsAttributionCache::default();
let mut conn = tcp_connection();
let key = key_of(&conn);
cache.attribute(&mut conn, key);
if let Some(mut entry) = cache.pending.get_mut(&conn.remote_addr.ip()) {
for e in entry.iter_mut() {
e.enrolled_at = Instant::now() - cache.fresh_window - Duration::from_secs(1);
}
}
let drained = cache.record_and_drain_pending(
"stale.example",
&[ip(1, 2, 3, 4)],
AttributionSource::CapturedDns,
);
assert!(drained.is_empty(), "stale enrollments must not be surfaced");
}
#[test]
fn prune_pending_drops_expired_enrollments() {
let cache = DnsAttributionCache::default();
let mut conn = tcp_connection();
let key = key_of(&conn);
cache.attribute(&mut conn, key);
assert_eq!(cache.pending.len(), 1);
if let Some(mut entry) = cache.pending.get_mut(&conn.remote_addr.ip()) {
for e in entry.iter_mut() {
e.enrolled_at = Instant::now() - cache.fresh_window - Duration::from_secs(1);
}
}
cache.prune_pending(Instant::now());
assert!(cache.pending.is_empty());
}
#[test]
fn enroll_pending_dedupes_same_key() {
let cache = DnsAttributionCache::default();
let mut conn = tcp_connection();
let key = key_of(&conn);
for _ in 0..5 {
cache.attribute(&mut conn, key);
}
let entry = cache.pending.get(&conn.remote_addr.ip()).unwrap();
assert_eq!(entry.len(), 1);
}
#[test]
fn empty_inputs_are_noops() {
let cache = DnsAttributionCache::default();
cache.record("", &[ip(1, 2, 3, 4)], AttributionSource::CapturedDns);
cache.record("foo.com", &[], AttributionSource::CapturedDns);
assert!(cache.map.is_empty());
}
}