1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
use std::fmt::{Debug, Display};
use std::net::SocketAddr;
use std::pin::Pin;
use std::task::{Context, Poll};
use futures::channel::mpsc::{TrySendError, UnboundedSender};
use futures::channel::oneshot::{self, Receiver, Sender};
use futures::{ready, Future, Stream};
use log::{debug, warn};
use crate::error::*;
use crate::Time;
mod dns_exchange;
pub mod dns_handle;
pub mod dns_multiplexer;
pub mod dns_request;
pub mod dns_response;
#[cfg(feature = "dnssec")]
pub mod dnssec_dns_handle;
pub mod retry_dns_handle;
mod serial_message;
pub use self::dns_exchange::{
DnsExchange, DnsExchangeBackground, DnsExchangeConnect, DnsExchangeSend,
};
pub use self::dns_handle::{DnsHandle, DnsStreamHandle, StreamHandle};
pub use self::dns_multiplexer::{
DnsMultiplexer, DnsMultiplexerConnect, DnsMultiplexerSerialResponse,
};
pub use self::dns_request::{DnsRequest, DnsRequestOptions};
pub use self::dns_response::DnsResponse;
#[cfg(feature = "dnssec")]
pub use self::dnssec_dns_handle::DnssecDnsHandle;
pub use self::retry_dns_handle::RetryDnsHandle;
pub use self::serial_message::SerialMessage;
fn ignore_send<M, E: Debug>(result: Result<M, E>) {
if let Err(error) = result {
warn!("error notifying wait, possible future leak: {:?}", error);
}
}
pub trait DnsClientStream:
Stream<Item = Result<SerialMessage, ProtoError>> + Display + Send
{
fn name_server_addr(&self) -> SocketAddr;
}
#[derive(Clone)]
pub struct BufStreamHandle {
sender: UnboundedSender<SerialMessage>,
}
impl BufStreamHandle {
pub fn new(sender: UnboundedSender<SerialMessage>) -> Self {
BufStreamHandle { sender }
}
pub fn unbounded_send(&self, msg: SerialMessage) -> Result<(), TrySendError<SerialMessage>> {
self.sender.unbounded_send(msg)
}
}
pub struct BufDnsStreamHandle {
name_server: SocketAddr,
sender: BufStreamHandle,
}
impl BufDnsStreamHandle {
pub fn new(name_server: SocketAddr, sender: BufStreamHandle) -> Self {
BufDnsStreamHandle {
name_server,
sender,
}
}
}
impl DnsStreamHandle for BufDnsStreamHandle {
fn send(&mut self, buffer: SerialMessage) -> Result<(), ProtoError> {
let name_server: SocketAddr = self.name_server;
let sender: &mut _ = &mut self.sender;
sender
.sender
.unbounded_send(SerialMessage::new(buffer.unwrap().0, name_server))
.map_err(|e| ProtoError::from(format!("mpsc::SendError {}", e)))
}
}
pub struct DnsRequestStreamHandle<F>
where
F: Future<Output = Result<DnsResponse, ProtoError>> + Send,
{
sender: UnboundedSender<OneshotDnsRequest<F>>,
}
impl<F> DnsRequestStreamHandle<F>
where
F: Future<Output = Result<DnsResponse, ProtoError>> + Send,
{
pub fn new(sender: UnboundedSender<OneshotDnsRequest<F>>) -> Self {
DnsRequestStreamHandle { sender }
}
pub fn unbounded_send(
&self,
msg: OneshotDnsRequest<F>,
) -> Result<(), TrySendError<OneshotDnsRequest<F>>> {
self.sender.unbounded_send(msg)
}
}
impl<F> Clone for DnsRequestStreamHandle<F>
where
F: Future<Output = Result<DnsResponse, ProtoError>> + Send,
{
fn clone(&self) -> Self {
DnsRequestStreamHandle {
sender: self.sender.clone(),
}
}
}
pub trait DnsRequestSender: Stream<Item = Result<(), ProtoError>> + Send + Unpin + 'static {
type DnsResponseFuture: Future<Output = Result<DnsResponse, ProtoError>>
+ 'static
+ Send
+ Unpin;
fn send_message<TE: Time>(
&mut self,
message: DnsRequest,
cx: &mut Context,
) -> Self::DnsResponseFuture;
fn error_response<TE: Time>(error: ProtoError) -> Self::DnsResponseFuture;
fn shutdown(&mut self);
fn is_shutdown(&self) -> bool;
}
pub struct BufDnsRequestStreamHandle<F>
where
F: Future<Output = Result<DnsResponse, ProtoError>> + Send,
{
sender: DnsRequestStreamHandle<F>,
}
impl<F> BufDnsRequestStreamHandle<F>
where
F: Future<Output = Result<DnsResponse, ProtoError>> + Send,
{
pub fn new(sender: DnsRequestStreamHandle<F>) -> Self {
BufDnsRequestStreamHandle { sender }
}
}
impl<F> Clone for BufDnsRequestStreamHandle<F>
where
F: Future<Output = Result<DnsResponse, ProtoError>> + Send,
{
fn clone(&self) -> Self {
BufDnsRequestStreamHandle {
sender: self.sender.clone(),
}
}
}
macro_rules! try_oneshot {
($expr:expr) => {{
use std::result::Result;
match $expr {
Result::Ok(val) => val,
Result::Err(err) => {
return OneshotDnsResponseReceiver::Err(Some(ProtoError::from(err)))
}
}
}};
($expr:expr,) => {
$expr?
};
}
impl<F> DnsHandle for BufDnsRequestStreamHandle<F>
where
F: Future<Output = Result<DnsResponse, ProtoError>> + Send + Unpin + 'static,
{
type Response = OneshotDnsResponseReceiver<F>;
fn send<R: Into<DnsRequest>>(&mut self, request: R) -> Self::Response {
let request: DnsRequest = request.into();
debug!("enqueueing message: {:?}", request.queries());
let (request, oneshot) = OneshotDnsRequest::oneshot(request);
try_oneshot!(self.sender.unbounded_send(request).map_err(|_| {
debug!("unable to enqueue message");
ProtoError::from("could not send request")
}));
OneshotDnsResponseReceiver::Receiver(oneshot)
}
}
pub struct OneshotDnsRequest<F>
where
F: Future<Output = Result<DnsResponse, ProtoError>> + Send,
{
dns_request: DnsRequest,
sender_for_response: Sender<F>,
}
impl<F> OneshotDnsRequest<F>
where
F: Future<Output = Result<DnsResponse, ProtoError>> + Send,
{
fn oneshot(dns_request: DnsRequest) -> (OneshotDnsRequest<F>, oneshot::Receiver<F>) {
let (sender_for_response, receiver) = oneshot::channel();
(
OneshotDnsRequest {
dns_request,
sender_for_response,
},
receiver,
)
}
fn unwrap(self) -> (DnsRequest, OneshotDnsResponse<F>) {
(
self.dns_request,
OneshotDnsResponse(self.sender_for_response),
)
}
}
struct OneshotDnsResponse<F>(oneshot::Sender<F>)
where
F: Future<Output = Result<DnsResponse, ProtoError>> + Send;
impl<F> OneshotDnsResponse<F>
where
F: Future<Output = Result<DnsResponse, ProtoError>> + Send,
{
fn send_response(self, serial_response: F) -> Result<(), F> {
self.0.send(serial_response)
}
}
pub enum OneshotDnsResponseReceiver<F>
where
F: Future<Output = Result<DnsResponse, ProtoError>> + Send + Unpin,
{
Receiver(Receiver<F>),
Received(F),
Err(Option<ProtoError>),
}
impl<F> Future for OneshotDnsResponseReceiver<F>
where
F: Future<Output = Result<DnsResponse, ProtoError>> + Send + Unpin,
{
type Output = <F as Future>::Output;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
loop {
*self = match *self.as_mut() {
OneshotDnsResponseReceiver::Receiver(ref mut receiver) => {
let receiver = Pin::new(receiver);
let future = ready!(receiver
.poll(cx)
.map_err(|_| ProtoError::from("receiver was canceled")))?;
OneshotDnsResponseReceiver::Received(future)
}
OneshotDnsResponseReceiver::Received(ref mut future) => {
let future = Pin::new(future);
return future.poll(cx);
}
OneshotDnsResponseReceiver::Err(ref mut err) => {
return Poll::Ready(Err(err
.take()
.expect("futures should not be polled after complete")))
}
};
}
}
}