use std::{
cmp::min,
pin::Pin,
slice::Iter,
sync::Arc,
time::{Duration, Instant},
};
use futures_util::stream::Stream;
use crate::{
dns_lru::MAX_TTL,
lookup_ip::LookupIpIter,
name_server::{ConnectionProvider, NameServerPool},
proto::{
op::Query,
rr::{
rdata::{self, A, AAAA, NS, PTR},
RData, Record,
},
xfer::{DnsRequest, DnsResponse},
DnsHandle, ProtoError, RetryDnsHandle,
},
};
#[cfg(feature = "dnssec-ring")]
use crate::proto::dnssec::{DnssecDnsHandle, Proven};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Lookup {
query: Query,
records: Arc<[Record]>,
valid_until: Instant,
}
impl Lookup {
pub fn from_rdata(query: Query, rdata: RData) -> Self {
let record = Record::from_rdata(query.name().clone(), MAX_TTL, rdata);
Self::new_with_max_ttl(query, Arc::from([record]))
}
pub fn new_with_max_ttl(query: Query, records: Arc<[Record]>) -> Self {
let valid_until = Instant::now() + Duration::from_secs(u64::from(MAX_TTL));
Self {
query,
records,
valid_until,
}
}
pub fn new_with_deadline(query: Query, records: Arc<[Record]>, valid_until: Instant) -> Self {
Self {
query,
records,
valid_until,
}
}
pub fn query(&self) -> &Query {
&self.query
}
pub fn iter(&self) -> LookupIter<'_> {
LookupIter(self.records.iter())
}
#[cfg(feature = "dnssec-ring")]
pub fn dnssec_iter(&self) -> DnssecIter<'_> {
DnssecIter(self.dnssec_record_iter())
}
pub fn record_iter(&self) -> LookupRecordIter<'_> {
LookupRecordIter(self.records.iter())
}
#[cfg(feature = "dnssec-ring")]
pub fn dnssec_record_iter(&self) -> DnssecLookupRecordIter<'_> {
DnssecLookupRecordIter(self.records.iter())
}
pub fn valid_until(&self) -> Instant {
self.valid_until
}
#[doc(hidden)]
pub fn is_empty(&self) -> bool {
self.records.is_empty()
}
pub(crate) fn len(&self) -> usize {
self.records.len()
}
pub fn records(&self) -> &[Record] {
self.records.as_ref()
}
pub(crate) fn append(&self, other: Self) -> Self {
let mut records = Vec::with_capacity(self.len() + other.len());
records.extend_from_slice(&self.records);
records.extend_from_slice(&other.records);
let valid_until = min(self.valid_until(), other.valid_until());
Self::new_with_deadline(self.query.clone(), Arc::from(records), valid_until)
}
pub fn extend_records(&mut self, other: Vec<Record>) {
let mut records = Vec::with_capacity(self.len() + other.len());
records.extend_from_slice(&self.records);
records.extend(other);
self.records = Arc::from(records);
}
}
pub struct LookupIter<'a>(Iter<'a, Record>);
impl<'a> Iterator for LookupIter<'a> {
type Item = &'a RData;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(Record::data)
}
}
#[cfg(feature = "dnssec-ring")]
pub struct DnssecIter<'a>(DnssecLookupRecordIter<'a>);
#[cfg(feature = "dnssec-ring")]
impl<'a> Iterator for DnssecIter<'a> {
type Item = Proven<&'a RData>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|r| r.map(Record::data))
}
}
pub struct LookupRecordIter<'a>(Iter<'a, Record>);
impl<'a> Iterator for LookupRecordIter<'a> {
type Item = &'a Record;
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
}
#[cfg(feature = "dnssec-ring")]
pub struct DnssecLookupRecordIter<'a>(Iter<'a, Record>);
#[cfg(feature = "dnssec-ring")]
impl<'a> Iterator for DnssecLookupRecordIter<'a> {
type Item = Proven<&'a Record>;
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(Proven::from)
}
}
impl IntoIterator for Lookup {
type Item = RData;
type IntoIter = LookupIntoIter;
fn into_iter(self) -> Self::IntoIter {
LookupIntoIter {
records: Arc::clone(&self.records),
index: 0,
}
}
}
pub struct LookupIntoIter {
records: Arc<[Record]>,
index: usize,
}
impl Iterator for LookupIntoIter {
type Item = RData;
fn next(&mut self) -> Option<Self::Item> {
let rdata = self.records.get(self.index).map(Record::data);
self.index += 1;
rdata.cloned()
}
}
#[derive(Clone)]
#[doc(hidden)]
pub enum LookupEither<P: ConnectionProvider + Send> {
Retry(RetryDnsHandle<NameServerPool<P>>),
#[cfg(feature = "dnssec-ring")]
Secure(DnssecDnsHandle<RetryDnsHandle<NameServerPool<P>>>),
}
impl<P: ConnectionProvider> DnsHandle for LookupEither<P> {
type Response = Pin<Box<dyn Stream<Item = Result<DnsResponse, ProtoError>> + Send>>;
fn is_verifying_dnssec(&self) -> bool {
match self {
Self::Retry(c) => c.is_verifying_dnssec(),
#[cfg(feature = "dnssec-ring")]
Self::Secure(c) => c.is_verifying_dnssec(),
}
}
fn send<R: Into<DnsRequest> + Unpin + Send + 'static>(&self, request: R) -> Self::Response {
match self {
Self::Retry(c) => c.send(request),
#[cfg(feature = "dnssec-ring")]
Self::Secure(c) => c.send(request),
}
}
}
#[derive(Debug, Clone)]
pub struct SrvLookup(Lookup);
impl SrvLookup {
pub fn iter(&self) -> SrvLookupIter<'_> {
SrvLookupIter(self.0.iter())
}
pub fn query(&self) -> &Query {
self.0.query()
}
pub fn ip_iter(&self) -> LookupIpIter<'_> {
LookupIpIter(self.0.iter())
}
pub fn as_lookup(&self) -> &Lookup {
&self.0
}
}
impl From<Lookup> for SrvLookup {
fn from(lookup: Lookup) -> Self {
Self(lookup)
}
}
pub struct SrvLookupIter<'i>(LookupIter<'i>);
impl<'i> Iterator for SrvLookupIter<'i> {
type Item = &'i rdata::SRV;
fn next(&mut self) -> Option<Self::Item> {
let iter: &mut _ = &mut self.0;
iter.find_map(|rdata| match rdata {
RData::SRV(data) => Some(data),
_ => None,
})
}
}
impl IntoIterator for SrvLookup {
type Item = rdata::SRV;
type IntoIter = SrvLookupIntoIter;
fn into_iter(self) -> Self::IntoIter {
SrvLookupIntoIter(self.0.into_iter())
}
}
pub struct SrvLookupIntoIter(LookupIntoIter);
impl Iterator for SrvLookupIntoIter {
type Item = rdata::SRV;
fn next(&mut self) -> Option<Self::Item> {
let iter: &mut _ = &mut self.0;
iter.find_map(|rdata| match rdata {
RData::SRV(data) => Some(data),
_ => None,
})
}
}
macro_rules! lookup_type {
($l:ident, $i:ident, $ii:ident, $r:path, $t:path) => {
#[derive(Debug, Clone)]
pub struct $l(Lookup);
impl $l {
#[doc = stringify!(Returns an iterator over the records that match $r)]
pub fn iter(&self) -> $i<'_> {
$i(self.0.iter())
}
pub fn query(&self) -> &Query {
self.0.query()
}
pub fn valid_until(&self) -> Instant {
self.0.valid_until()
}
pub fn as_lookup(&self) -> &Lookup {
&self.0
}
}
impl From<Lookup> for $l {
fn from(lookup: Lookup) -> Self {
$l(lookup)
}
}
impl From<$l> for Lookup {
fn from(revlookup: $l) -> Self {
revlookup.0
}
}
pub struct $i<'i>(LookupIter<'i>);
impl<'i> Iterator for $i<'i> {
type Item = &'i $t;
fn next(&mut self) -> Option<Self::Item> {
let iter: &mut _ = &mut self.0;
iter.find_map(|rdata| match rdata {
$r(data) => Some(data),
_ => None,
})
}
}
impl IntoIterator for $l {
type Item = $t;
type IntoIter = $ii;
fn into_iter(self) -> Self::IntoIter {
$ii(self.0.into_iter())
}
}
pub struct $ii(LookupIntoIter);
impl Iterator for $ii {
type Item = $t;
fn next(&mut self) -> Option<Self::Item> {
let iter: &mut _ = &mut self.0;
iter.find_map(|rdata| match rdata {
$r(data) => Some(data),
_ => None,
})
}
}
};
}
lookup_type!(
ReverseLookup,
ReverseLookupIter,
ReverseLookupIntoIter,
RData::PTR,
PTR
);
lookup_type!(Ipv4Lookup, Ipv4LookupIter, Ipv4LookupIntoIter, RData::A, A);
lookup_type!(
Ipv6Lookup,
Ipv6LookupIter,
Ipv6LookupIntoIter,
RData::AAAA,
AAAA
);
lookup_type!(
MxLookup,
MxLookupIter,
MxLookupIntoIter,
RData::MX,
rdata::MX
);
lookup_type!(
TlsaLookup,
TlsaLookupIter,
TlsaLookupIntoIter,
RData::TLSA,
rdata::TLSA
);
lookup_type!(
TxtLookup,
TxtLookupIter,
TxtLookupIntoIter,
RData::TXT,
rdata::TXT
);
lookup_type!(
CertLookup,
CertLookupIter,
CertLookupIntoIter,
RData::CERT,
rdata::CERT
);
lookup_type!(
SoaLookup,
SoaLookupIter,
SoaLookupIntoIter,
RData::SOA,
rdata::SOA
);
lookup_type!(NsLookup, NsLookupIter, NsLookupIntoIter, RData::NS, NS);
#[cfg(test)]
mod tests {
use std::str::FromStr;
use std::sync::Arc;
#[cfg(feature = "dnssec-ring")]
use crate::proto::op::Query;
use crate::proto::rr::{Name, RData, Record};
use super::*;
#[test]
fn test_lookup_into_iter_arc() {
let mut lookup = LookupIntoIter {
records: Arc::from([
Record::from_rdata(
Name::from_str("www.example.com.").unwrap(),
80,
RData::A(A::new(127, 0, 0, 1)),
),
Record::from_rdata(
Name::from_str("www.example.com.").unwrap(),
80,
RData::A(A::new(127, 0, 0, 2)),
),
]),
index: 0,
};
assert_eq!(lookup.next().unwrap(), RData::A(A::new(127, 0, 0, 1)));
assert_eq!(lookup.next().unwrap(), RData::A(A::new(127, 0, 0, 2)));
assert_eq!(lookup.next(), None);
}
#[test]
#[cfg(feature = "dnssec-ring")]
fn test_dnssec_lookup() {
use hickory_proto::dnssec::Proof;
let mut a1 = Record::from_rdata(
Name::from_str("www.example.com.").unwrap(),
80,
RData::A(A::new(127, 0, 0, 1)),
);
a1.set_proof(Proof::Secure);
let mut a2 = Record::from_rdata(
Name::from_str("www.example.com.").unwrap(),
80,
RData::A(A::new(127, 0, 0, 2)),
);
a2.set_proof(Proof::Insecure);
let lookup = Lookup {
query: Query::default(),
records: Arc::from([a1.clone(), a2.clone()]),
valid_until: Instant::now(),
};
let mut lookup = lookup.dnssec_iter();
assert_eq!(
*lookup.next().unwrap().require(Proof::Secure).unwrap(),
*a1.data()
);
assert_eq!(
*lookup.next().unwrap().require(Proof::Insecure).unwrap(),
*a2.data()
);
assert_eq!(lookup.next(), None);
}
}