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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
use std::borrow::Borrow;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fmt::{self, Display};
use std::marker::Unpin;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use futures_channel::mpsc;
use futures_util::stream::{Stream, StreamExt};
use futures_util::{future::Future, ready, FutureExt};
use rand;
use rand::distributions::{Distribution, Standard};
use tracing::{debug, warn};
use crate::error::*;
use crate::op::{MessageFinalizer, MessageVerifier};
use crate::xfer::{
ignore_send, BufDnsStreamHandle, DnsClientStream, DnsRequest, DnsRequestSender, DnsResponse,
DnsResponseStream, SerialMessage, CHANNEL_BUFFER_SIZE,
};
use crate::DnsStreamHandle;
use crate::Time;
const QOS_MAX_RECEIVE_MSGS: usize = 100; struct ActiveRequest {
completion: mpsc::Sender<Result<DnsResponse, ProtoError>>,
request_id: u16,
timeout: Box<dyn Future<Output = ()> + Send + Unpin>,
verifier: Option<MessageVerifier>,
}
impl ActiveRequest {
fn new(
completion: mpsc::Sender<Result<DnsResponse, ProtoError>>,
request_id: u16,
timeout: Box<dyn Future<Output = ()> + Send + Unpin>,
verifier: Option<MessageVerifier>,
) -> Self {
Self {
completion,
request_id,
timeout,
verifier,
}
}
fn poll_timeout(&mut self, cx: &mut Context<'_>) -> Poll<()> {
self.timeout.poll_unpin(cx)
}
fn is_canceled(&self) -> bool {
self.completion.is_closed()
}
fn request_id(&self) -> u16 {
self.request_id
}
fn complete_with_error(mut self, error: ProtoError) {
ignore_send(self.completion.try_send(Err(error)));
}
}
#[must_use = "futures do nothing unless polled"]
pub struct DnsMultiplexer<S, MF>
where
S: DnsClientStream + 'static,
MF: MessageFinalizer,
{
stream: S,
timeout_duration: Duration,
stream_handle: BufDnsStreamHandle,
active_requests: HashMap<u16, ActiveRequest>,
signer: Option<Arc<MF>>,
is_shutdown: bool,
}
impl<S, MF> DnsMultiplexer<S, MF>
where
S: DnsClientStream + Unpin + 'static,
MF: MessageFinalizer,
{
#[allow(clippy::new_ret_no_self)]
pub fn new<F>(
stream: F,
stream_handle: BufDnsStreamHandle,
signer: Option<Arc<MF>>,
) -> DnsMultiplexerConnect<F, S, MF>
where
F: Future<Output = Result<S, ProtoError>> + Send + Unpin + 'static,
{
Self::with_timeout(stream, stream_handle, Duration::from_secs(5), signer)
}
pub fn with_timeout<F>(
stream: F,
stream_handle: BufDnsStreamHandle,
timeout_duration: Duration,
signer: Option<Arc<MF>>,
) -> DnsMultiplexerConnect<F, S, MF>
where
F: Future<Output = Result<S, ProtoError>> + Send + Unpin + 'static,
{
DnsMultiplexerConnect {
stream,
stream_handle: Some(stream_handle),
timeout_duration,
signer,
}
}
fn drop_cancelled(&mut self, cx: &mut Context<'_>) {
let mut canceled = HashMap::<u16, ProtoError>::new();
for (&id, ref mut active_req) in &mut self.active_requests {
if active_req.is_canceled() {
canceled.insert(id, ProtoError::from("requestor canceled"));
}
match active_req.poll_timeout(cx) {
Poll::Ready(()) => {
debug!("request timed out: {}", id);
canceled.insert(id, ProtoError::from(ProtoErrorKind::Timeout));
}
Poll::Pending => (),
}
}
for (id, error) in canceled {
if let Some(active_request) = self.active_requests.remove(&id) {
active_request.complete_with_error(error);
}
}
}
fn next_random_query_id(&self) -> Result<u16, ProtoError> {
let mut rand = rand::thread_rng();
for _ in 0..100 {
let id: u16 = Standard.sample(&mut rand); if !self.active_requests.contains_key(&id) {
return Ok(id);
}
}
Err(ProtoError::from(
"id space exhausted, consider filing an issue",
))
}
fn stream_closed_close_all(&mut self, error: ProtoError) {
if !self.active_requests.is_empty() {
warn!("stream {} error: {}", self.stream, error);
} else {
debug!("stream {} error: {}", self.stream, error);
}
for (_, active_request) in self.active_requests.drain() {
active_request.complete_with_error(error.clone());
}
}
}
#[must_use = "futures do nothing unless polled"]
pub struct DnsMultiplexerConnect<F, S, MF>
where
F: Future<Output = Result<S, ProtoError>> + Send + Unpin + 'static,
S: Stream<Item = Result<SerialMessage, ProtoError>> + Unpin,
MF: MessageFinalizer + Send + Sync + 'static,
{
stream: F,
stream_handle: Option<BufDnsStreamHandle>,
timeout_duration: Duration,
signer: Option<Arc<MF>>,
}
impl<F, S, MF> Future for DnsMultiplexerConnect<F, S, MF>
where
F: Future<Output = Result<S, ProtoError>> + Send + Unpin + 'static,
S: DnsClientStream + Unpin + 'static,
MF: MessageFinalizer + Send + Sync + 'static,
{
type Output = Result<DnsMultiplexer<S, MF>, ProtoError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let stream: S = ready!(self.stream.poll_unpin(cx))?;
Poll::Ready(Ok(DnsMultiplexer {
stream,
timeout_duration: self.timeout_duration,
stream_handle: self
.stream_handle
.take()
.expect("must not poll after complete"),
active_requests: HashMap::new(),
signer: self.signer.clone(),
is_shutdown: false,
}))
}
}
impl<S, MF> Display for DnsMultiplexer<S, MF>
where
S: DnsClientStream + 'static,
MF: MessageFinalizer + Send + Sync + 'static,
{
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(formatter, "{}", self.stream)
}
}
impl<S, MF> DnsRequestSender for DnsMultiplexer<S, MF>
where
S: DnsClientStream + Unpin + 'static,
MF: MessageFinalizer + Send + Sync + 'static,
{
fn send_message(&mut self, request: DnsRequest) -> DnsResponseStream {
if self.is_shutdown {
panic!("can not send messages after stream is shutdown")
}
if self.active_requests.len() > CHANNEL_BUFFER_SIZE {
return ProtoError::from(ProtoErrorKind::Busy).into();
}
let query_id = match self.next_random_query_id() {
Ok(id) => id,
Err(e) => return e.into(),
};
let (mut request, _) = request.into_parts();
request.set_id(query_id);
let now = match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(now) => now.as_secs(),
Err(_) => return ProtoError::from("Current time is before the Unix epoch.").into(),
};
let now = now as u32;
let mut verifier = None;
if let Some(ref signer) = self.signer {
if signer.should_finalize_message(&request) {
match request.finalize::<MF>(signer.borrow(), now) {
Ok(answer_verifier) => verifier = answer_verifier,
Err(e) => {
debug!("could not sign message: {}", e);
return e.into();
}
}
}
}
let timeout = S::Time::delay_for(self.timeout_duration);
let (complete, receiver) = mpsc::channel(CHANNEL_BUFFER_SIZE);
let active_request =
ActiveRequest::new(complete, request.id(), Box::new(timeout), verifier);
match request.to_vec() {
Ok(buffer) => {
debug!("sending message id: {}", active_request.request_id());
let serial_message = SerialMessage::new(buffer, self.stream.name_server_addr());
debug!(
"final message: {}",
serial_message
.to_message()
.expect("bizarre we just made this message")
);
match self.stream_handle.send(serial_message) {
Ok(()) => self
.active_requests
.insert(active_request.request_id(), active_request),
Err(err) => return err.into(),
};
}
Err(e) => {
debug!(
"error message id: {} error: {}",
active_request.request_id(),
e
);
return e.into();
}
}
receiver.into()
}
fn shutdown(&mut self) {
self.is_shutdown = true;
}
fn is_shutdown(&self) -> bool {
self.is_shutdown
}
}
impl<S, MF> Stream for DnsMultiplexer<S, MF>
where
S: DnsClientStream + Unpin + 'static,
MF: MessageFinalizer + Send + Sync + 'static,
{
type Item = Result<(), ProtoError>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.drop_cancelled(cx);
if self.is_shutdown && self.active_requests.is_empty() {
debug!("stream is done: {}", self);
return Poll::Ready(None);
}
let mut messages_received = 0;
for i in 0..QOS_MAX_RECEIVE_MSGS {
match self.stream.poll_next_unpin(cx) {
Poll::Ready(Some(Ok(buffer))) => {
messages_received = i;
match buffer.to_message() {
Ok(message) => match self.active_requests.entry(message.id()) {
Entry::Occupied(mut request_entry) => {
let active_request = request_entry.get_mut();
if let Some(ref mut verifier) = active_request.verifier {
ignore_send(
active_request
.completion
.try_send(verifier(buffer.bytes())),
);
} else {
ignore_send(
active_request.completion.try_send(Ok(message.into())),
);
}
}
Entry::Vacant(..) => debug!("unexpected request_id: {}", message.id()),
},
Err(e) => debug!("error decoding message: {}", e),
}
}
Poll::Ready(err) => {
let err = match err {
Some(Err(e)) => e,
None => ProtoError::from("stream closed"),
_ => unreachable!(),
};
self.stream_closed_close_all(err);
self.is_shutdown = true;
return Poll::Ready(None);
}
Poll::Pending => break,
}
}
if messages_received == QOS_MAX_RECEIVE_MSGS {
cx.waker().wake_by_ref();
}
Poll::Pending
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::op::message::NoopMessageFinalizer;
use crate::op::op_code::OpCode;
use crate::op::{Message, MessageType, Query};
use crate::rr::record_type::RecordType;
use crate::rr::{DNSClass, Name, RData, Record};
use crate::serialize::binary::BinEncodable;
use crate::xfer::StreamReceiver;
use crate::xfer::{DnsClientStream, DnsRequestOptions};
use futures_util::future;
use futures_util::stream::TryStreamExt;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
struct MockClientStream {
messages: Vec<Message>,
addr: SocketAddr,
id: Option<u16>,
receiver: Option<StreamReceiver>,
}
impl MockClientStream {
fn new(
mut messages: Vec<Message>,
addr: SocketAddr,
) -> Pin<Box<dyn Future<Output = Result<Self, ProtoError>> + Send>> {
messages.reverse(); Box::pin(future::ok(Self {
messages,
addr,
id: None,
receiver: None,
}))
}
}
impl fmt::Display for MockClientStream {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
write!(formatter, "TestClientStream")
}
}
impl Stream for MockClientStream {
type Item = Result<SerialMessage, ProtoError>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let id = if let Some(id) = self.id {
id
} else {
let serial = ready!(self
.receiver
.as_mut()
.expect("should only be polled after receiver has been set")
.poll_next_unpin(cx));
let message = serial.unwrap().to_message().unwrap();
self.id = Some(message.id());
message.id()
};
if let Some(mut message) = self.messages.pop() {
message.set_id(id);
Poll::Ready(Some(Ok(SerialMessage::new(
message.to_bytes().unwrap(),
self.addr,
))))
} else {
Poll::Pending
}
}
}
impl DnsClientStream for MockClientStream {
type Time = crate::TokioTime;
fn name_server_addr(&self) -> SocketAddr {
self.addr
}
}
async fn get_mocked_multiplexer(
mock_response: Vec<Message>,
) -> DnsMultiplexer<MockClientStream, NoopMessageFinalizer> {
let addr = SocketAddr::from(([127, 0, 0, 1], 1234));
let mock_response = MockClientStream::new(mock_response, addr);
let (handler, receiver) = BufDnsStreamHandle::new(addr);
let mut multiplexer =
DnsMultiplexer::with_timeout(mock_response, handler, Duration::from_millis(100), None)
.await
.unwrap();
multiplexer.stream.receiver = Some(receiver); multiplexer
}
fn a_query_answer() -> (DnsRequest, Vec<Message>) {
let name = Name::from_ascii("www.example.com").unwrap();
let mut msg = Message::new();
msg.add_query({
let mut query = Query::query(name.clone(), RecordType::A);
query.set_query_class(DNSClass::IN);
query
})
.set_message_type(MessageType::Query)
.set_op_code(OpCode::Query)
.set_recursion_desired(true);
let query = msg.clone();
msg.set_message_type(MessageType::Response).add_answer(
Record::new()
.set_name(name)
.set_ttl(86400)
.set_rr_type(RecordType::A)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::A(Ipv4Addr::new(93, 184, 216, 34))))
.clone(),
);
(
DnsRequest::new(query, DnsRequestOptions::default()),
vec![msg],
)
}
fn axfr_query() -> Message {
let name = Name::from_ascii("example.com").unwrap();
let mut msg = Message::new();
msg.add_query({
let mut query = Query::query(name, RecordType::AXFR);
query.set_query_class(DNSClass::IN);
query
})
.set_message_type(MessageType::Query)
.set_op_code(OpCode::Query)
.set_recursion_desired(true);
msg
}
fn axfr_response() -> Vec<Record> {
use crate::rr::rdata::*;
let origin = Name::from_ascii("example.com").unwrap();
let soa = Record::new()
.set_name(origin.clone())
.set_ttl(3600)
.set_rr_type(RecordType::SOA)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::SOA(SOA::new(
Name::parse("sns.dns.icann.org.", None).unwrap(),
Name::parse("noc.dns.icann.org.", None).unwrap(),
2015082403,
7200,
3600,
1209600,
3600,
))))
.clone();
vec![
soa.clone(),
Record::new()
.set_name(origin.clone())
.set_ttl(86400)
.set_rr_type(RecordType::NS)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::NS(
Name::parse("a.iana-servers.net.", None).unwrap(),
)))
.clone(),
Record::new()
.set_name(origin.clone())
.set_ttl(86400)
.set_rr_type(RecordType::NS)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::NS(
Name::parse("b.iana-servers.net.", None).unwrap(),
)))
.clone(),
Record::new()
.set_name(origin.clone())
.set_ttl(86400)
.set_rr_type(RecordType::A)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::A(Ipv4Addr::new(93, 184, 216, 34))))
.clone(),
Record::new()
.set_name(origin)
.set_ttl(86400)
.set_rr_type(RecordType::AAAA)
.set_dns_class(DNSClass::IN)
.set_data(Some(RData::AAAA(Ipv6Addr::new(
0x2606, 0x2800, 0x220, 0x1, 0x248, 0x1893, 0x25c8, 0x1946,
))))
.clone(),
soa,
]
}
fn axfr_query_answer() -> (DnsRequest, Vec<Message>) {
let mut msg = axfr_query();
let query = msg.clone();
msg.set_message_type(MessageType::Response)
.insert_answers(axfr_response());
(
DnsRequest::new(query, DnsRequestOptions::default()),
vec![msg],
)
}
fn axfr_query_answer_multi() -> (DnsRequest, Vec<Message>) {
let base = axfr_query();
let query = base.clone();
let mut rr = axfr_response();
let rr2 = rr.split_off(3);
let mut msg1 = base.clone();
msg1.set_message_type(MessageType::Response)
.insert_answers(rr);
let mut msg2 = base;
msg2.set_message_type(MessageType::Response)
.insert_answers(rr2);
(
DnsRequest::new(query, DnsRequestOptions::default()),
vec![msg1, msg2],
)
}
#[tokio::test]
async fn test_multiplexer_a() {
let (query, answer) = a_query_answer();
let mut multiplexer = get_mocked_multiplexer(answer).await;
let response = multiplexer.send_message(query);
let response = tokio::select! {
_ = multiplexer.next() => {
panic!("should never end")
},
r = response.try_collect::<Vec<_>>() => r.unwrap(),
};
assert_eq!(response.len(), 1);
}
#[tokio::test]
async fn test_multiplexer_axfr() {
let (query, answer) = axfr_query_answer();
let mut multiplexer = get_mocked_multiplexer(answer).await;
let response = multiplexer.send_message(query);
let response = tokio::select! {
_ = multiplexer.next() => {
panic!("should never end")
},
r = response.try_collect::<Vec<_>>() => r.unwrap(),
};
assert_eq!(response.len(), 1);
assert_eq!(response[0].answers().len(), axfr_response().len());
}
#[tokio::test]
async fn test_multiplexer_axfr_multi() {
let (query, answer) = axfr_query_answer_multi();
let mut multiplexer = get_mocked_multiplexer(answer).await;
let response = multiplexer.send_message(query);
let response = tokio::select! {
_ = multiplexer.next() => {
panic!("should never end")
},
r = response.try_collect::<Vec<_>>() => r.unwrap(),
};
assert_eq!(response.len(), 2);
assert_eq!(
response.iter().map(|m| m.answers().len()).sum::<usize>(),
axfr_response().len()
);
}
}