Skip to main content

hickory_net/xfer/
dns_handle.rs

1// Copyright 2015-2018 Benjamin Fry <benjaminfry@me.com>
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// https://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// https://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! `DnsHandle` types perform conversions of the raw DNS messages before sending the messages on the specified streams.
9
10use futures_util::stream::Stream;
11use tracing::debug;
12
13use crate::{
14    error::NetError,
15    proto::op::{DnsRequest, DnsRequestOptions, DnsResponse, Query, SerialMessage},
16    runtime::RuntimeProvider,
17};
18
19/// Implementations of Sinks for sending DNS messages
20pub trait DnsStreamHandle: 'static + Send {
21    /// Sends a message to the Handle for delivery to the server.
22    fn send(&mut self, buffer: SerialMessage) -> Result<(), NetError>;
23}
24
25/// A trait for implementing high level functions of DNS.
26pub trait DnsHandle: 'static + Clone + Send + Sync + Unpin {
27    /// The associated response from the response stream, this should resolve to the Response messages
28    type Response: Stream<Item = Result<DnsResponse, NetError>> + Send + Unpin + 'static;
29
30    /// The asynchronous runtime in use.
31    type Runtime: RuntimeProvider;
32
33    /// Only returns true if and only if this DNS handle is validating DNSSEC.
34    ///
35    /// If the DnsHandle impl is wrapping other clients, then the correct option is to delegate the question to the wrapped client.
36    fn is_verifying_dnssec(&self) -> bool {
37        false
38    }
39
40    /// Allow for disabling EDNS
41    fn is_using_edns(&self) -> bool {
42        true
43    }
44
45    /// Send a message via the channel in the client
46    ///
47    /// # Arguments
48    ///
49    /// * `request` - the fully constructed Message to send, note that most implementations of
50    ///   will most likely be required to rewrite the QueryId, do no rely on that as
51    ///   being stable.
52    fn send(&self, request: DnsRequest) -> Self::Response;
53
54    /// A *classic* DNS query
55    ///
56    /// This is identical to `query`, but instead takes a `Query` object.
57    ///
58    /// # Arguments
59    ///
60    /// * `query` - the query to lookup
61    /// * `options` - options to use when constructing the message
62    fn lookup(&self, query: Query, options: DnsRequestOptions) -> Self::Response {
63        debug!("querying: {} {:?}", query.name(), query.query_type());
64        self.send(DnsRequest::from_query(query, options))
65    }
66}