trust_dns_client/
https_client_connection.rs

1// Copyright 2015-2018 Benjamin Fry <benjaminfry@me.com>
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! UDP based DNS client connection for Client impls
9
10use std::marker::PhantomData;
11use std::net::SocketAddr;
12use std::sync::Arc;
13
14use rustls::ClientConfig;
15use trust_dns_proto::https::{HttpsClientConnect, HttpsClientStream, HttpsClientStreamBuilder};
16use trust_dns_proto::tcp::Connect;
17
18use crate::client::{ClientConnection, Signer};
19
20/// UDP based DNS Client connection
21///
22/// Use with `trust_dns_client::client::Client` impls
23#[derive(Clone)]
24pub struct HttpsClientConnection<T> {
25    name_server: SocketAddr,
26    bind_addr: Option<SocketAddr>,
27    dns_name: String,
28    client_config: Arc<ClientConfig>,
29    marker: PhantomData<T>,
30}
31
32impl<T> HttpsClientConnection<T> {
33    /// Creates a new client connection.
34    ///
35    /// *Note* this has side affects of starting the listening event_loop. Expect this to change in
36    /// the future.
37    ///
38    /// # Arguments
39    ///
40    /// * `name_server` - IP and Port for the remote DNS resolver
41    /// * `dns_name` - The DNS name, Subject Public Key Info (SPKI) name, as associated to a certificate
42    /// * `client_config` - The TLS config
43    #[allow(clippy::new_ret_no_self)]
44    pub fn new(
45        name_server: SocketAddr,
46        dns_name: String,
47        client_config: Arc<ClientConfig>,
48    ) -> Self {
49        Self::new_with_bind_addr(name_server, None, dns_name, client_config)
50    }
51
52    /// Creates a new client connection with a specified source address.
53    ///
54    /// *Note* this has side affects of starting the listening event_loop. Expect this to change in
55    /// the future.
56    ///
57    /// # Arguments
58    ///
59    /// * `name_server` - IP and Port for the remote DNS resolver
60    /// * `bind_addr` - IP and port to connect from
61    /// * `dns_name` - The DNS name, Subject Public Key Info (SPKI) name, as associated to a certificate
62    /// * `client_config` - The TLS config    
63    #[allow(clippy::new_ret_no_self)]
64    pub fn new_with_bind_addr(
65        name_server: SocketAddr,
66        bind_addr: Option<SocketAddr>,
67        dns_name: String,
68        client_config: Arc<ClientConfig>,
69    ) -> Self {
70        Self {
71            name_server,
72            bind_addr,
73            dns_name,
74            client_config,
75            marker: PhantomData,
76        }
77    }
78}
79
80impl<T> ClientConnection for HttpsClientConnection<T>
81where
82    T: Connect,
83{
84    type Sender = HttpsClientStream;
85    type SenderFuture = HttpsClientConnect<T>;
86
87    fn new_stream(
88        &self,
89        // TODO: maybe signer needs to be applied in https...
90        _signer: Option<Arc<Signer>>,
91    ) -> Self::SenderFuture {
92        // TODO: maybe signer needs to be applied in https...
93        let mut https_builder =
94            HttpsClientStreamBuilder::with_client_config(Arc::clone(&self.client_config));
95        if let Some(bind_addr) = self.bind_addr {
96            https_builder.bind_addr(bind_addr);
97        }
98        https_builder.build(self.name_server, self.dns_name.clone())
99    }
100}