hickory_proto/native_tls/
tls_client_stream.rs1use std::future::Future;
11use std::net::SocketAddr;
12use std::pin::Pin;
13
14use futures_util::TryFutureExt;
15use native_tls::Certificate;
16use tokio_native_tls::TlsStream as TokioTlsStream;
17
18use crate::error::ProtoError;
19use crate::iocompat::AsyncIoStdAsTokio;
20use crate::iocompat::AsyncIoTokioAsStd;
21use crate::native_tls::TlsStreamBuilder;
22use crate::tcp::{Connect, DnsTcpStream, TcpClientStream};
23use crate::xfer::BufDnsStreamHandle;
24
25pub type TlsClientStream<S> =
29 TcpClientStream<AsyncIoTokioAsStd<TokioTlsStream<AsyncIoStdAsTokio<S>>>>;
30
31pub struct TlsClientStreamBuilder<S>(TlsStreamBuilder<S>);
33
34impl<S: DnsTcpStream> TlsClientStreamBuilder<S> {
35 pub fn new() -> Self {
37 Self(TlsStreamBuilder::new())
38 }
39
40 pub fn add_ca(&mut self, ca: Certificate) {
44 self.0.add_ca(ca);
45 }
46
47 pub fn bind_addr(&mut self, bind_addr: SocketAddr) {
49 self.0.bind_addr(bind_addr);
50 }
51
52 #[allow(clippy::type_complexity)]
60 pub fn build_with_future<F>(
61 self,
62 future: F,
63 name_server: SocketAddr,
64 dns_name: String,
65 ) -> (
66 Pin<Box<dyn Future<Output = Result<TlsClientStream<S>, ProtoError>> + Send>>,
67 BufDnsStreamHandle,
68 )
69 where
70 F: Future<Output = std::io::Result<S>> + Send + Unpin + 'static,
71 {
72 let (stream_future, sender) = self.0.build_with_future(future, name_server, dns_name);
73
74 let new_future = Box::pin(
75 stream_future
76 .map_ok(TcpClientStream::from_stream)
77 .map_err(ProtoError::from),
78 );
79
80 (new_future, sender)
81 }
82}
83
84impl<S: Connect> TlsClientStreamBuilder<S> {
85 #[allow(clippy::type_complexity)]
92 pub fn build(
93 self,
94 name_server: SocketAddr,
95 dns_name: String,
96 ) -> (
97 Pin<Box<dyn Future<Output = Result<TlsClientStream<S>, ProtoError>> + Send>>,
98 BufDnsStreamHandle,
99 ) {
100 let (stream_future, sender) = self.0.build(name_server, dns_name);
101
102 let new_future = Box::pin(
103 stream_future
104 .map_ok(TcpClientStream::from_stream)
105 .map_err(ProtoError::from),
106 );
107
108 (new_future, sender)
109 }
110}
111
112impl<S: DnsTcpStream> Default for TlsClientStreamBuilder<S> {
113 fn default() -> Self {
114 Self::new()
115 }
116}