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
use futures::sync::mpsc::UnboundedSender;
use futures::sync::oneshot;
use futures::{Future, IntoFuture};
use rand;
use crate::error::*;
use crate::op::{Message, MessageType, OpCode, Query};
use crate::xfer::{ignore_send, DnsRequest, DnsRequestOptions, DnsResponse, SerialMessage};
const MAX_PAYLOAD_LEN: u16 = 1500 - 40 - 8;
pub struct StreamHandle {
sender: UnboundedSender<Vec<u8>>,
}
impl StreamHandle {
pub fn new(sender: UnboundedSender<Vec<u8>>) -> Self {
StreamHandle { sender }
}
}
pub trait DnsStreamHandle: 'static + Send {
fn send(&mut self, buffer: SerialMessage) -> Result<(), ProtoError>;
}
impl DnsStreamHandle for StreamHandle {
fn send(&mut self, buffer: SerialMessage) -> Result<(), ProtoError> {
UnboundedSender::unbounded_send(&self.sender, buffer.unwrap().0)
.map_err(|e| ProtoError::from(format!("mpsc::SendError {}", e)))
}
}
#[derive(Clone)]
pub struct BasicDnsHandle {
message_sender: UnboundedSender<(DnsRequest, oneshot::Sender<Result<DnsResponse, ProtoError>>)>,
}
impl BasicDnsHandle {
pub fn new(
message_sender: UnboundedSender<(
DnsRequest,
oneshot::Sender<Result<DnsResponse, ProtoError>>,
)>,
) -> Self {
BasicDnsHandle { message_sender }
}
}
impl DnsHandle for BasicDnsHandle {
type Response = Box<Future<Item = DnsResponse, Error = ProtoError> + Send>;
fn send<R: Into<DnsRequest>>(
&mut self,
request: R,
) -> Box<Future<Item = DnsResponse, Error = ProtoError> + Send> {
let request = request.into();
let (complete, receiver) = oneshot::channel();
let message_sender: &mut _ = &mut self.message_sender;
let receiver = match UnboundedSender::unbounded_send(message_sender, (request, complete)) {
Ok(()) => receiver,
Err(e) => {
let (complete, receiver) = oneshot::channel();
ignore_send(complete.send(Err(ProtoError::from(format!(
"error sending to channel: {}",
e
)))));
receiver
}
};
Box::new(
receiver
.map_err(|c| ProtoError::from(ProtoErrorKind::Canceled(c)))
.map(IntoFuture::into_future)
.flatten(),
)
}
}
pub trait DnsHandle: 'static + Clone + Send {
type Response: Future<Item = DnsResponse, Error = ProtoError> + 'static + Send;
fn is_verifying_dnssec(&self) -> bool {
false
}
fn send<R: Into<DnsRequest>>(&mut self, request: R) -> Self::Response;
fn lookup(&mut self, query: Query, options: DnsRequestOptions) -> Self::Response {
debug!("querying: {} {:?}", query.name(), query.query_type());
let mut message: Message = Message::new();
let id: u16 = rand::random();
message.add_query(query);
message
.set_id(id)
.set_message_type(MessageType::Query)
.set_op_code(OpCode::Query)
.set_recursion_desired(true);
{
let edns = message.edns_mut();
edns.set_max_payload(MAX_PAYLOAD_LEN);
edns.set_version(0);
}
self.send(DnsRequest::new(message, options))
}
}