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
//! The stub resolver.
//!
//! This module contains the type [`StubResolver`] that represents a resolver.
//! The type is also re-exported at crate level. You are encouraged to use
//! that definition.
//!
//! [`StubResolver`]: struct.StubResolver.html

use std::{io, ops};
use std::sync::Arc;
use domain_core::bits::{Dname, Message, Question, ToDname};
use domain_core::bits::query::{QueryBuilder, QueryMessage};
use domain_core::iana::Rcode;
use tokio::prelude::{Async, Future};
use tokio::prelude::future::lazy;
use tokio::runtime::Runtime;
use crate::resolver::{Resolver, SearchNames};
use super::conf::{ResolvConf, ResolvOptions};
use super::net::{ServerInfo, ServerList, ServerListCounter, ServerQuery};


//------------ StubResolver --------------------------------------------------

/// A DNS stub resolver.
///
/// This type collects all information making it possible to start DNS
/// queries. You can create a new resoler using the system’s configuration
/// using the [`new()`] associate function or using your own configuration
/// with [`from_conf()`].
///
/// Stub resolver values can be cloned relatively cheaply as they keep all
/// information behind an arc.
///
/// If you want to run a single query or lookup on a resolver synchronously,
/// you can do so simply by using the [`run()`] or [`run_with_conf()`]
/// associated functions.
///
/// [`new()`]: #method.new
/// [`from_conf()`]: #method.from_conf
/// [`query()`]: #method.query
/// [`run()`]: #method.run
/// [`run_with_conf()`]: #method.run_with_conf
#[derive(Clone, Debug)]
pub struct StubResolver(Arc<ResolverInner>);


#[derive(Debug)]
struct ResolverInner {
    /// Preferred servers.
    preferred: ServerList,

    /// Streaming servers.
    stream: ServerList,

    /// Resolver options.
    options: ResolvOptions,
}


impl StubResolver {
    /// Creates a new resolver using the system’s default configuration.
    pub fn new() -> Self {
        Self::from_conf(ResolvConf::default())
    }

    /// Creates a new resolver using the given configuraiton.
    pub fn from_conf(conf: ResolvConf) -> Self {
        StubResolver(Arc::new(ResolverInner::from_conf(conf)))
    }

    pub fn options(&self) -> &ResolvOptions {
        &self.0.options
    }
}

impl ResolverInner {
    fn from_conf(conf: ResolvConf) -> Self {
        ResolverInner {
            preferred: ServerList::from_conf(&conf, |s| {
                s.transport.is_preferred()
            }),
            stream: ServerList::from_conf(&conf, |s| {
                s.transport.is_stream()
            }),
            options: conf.options
        }
    }
}

impl StubResolver {
    /// Synchronously perform a DNS operation atop a standard resolver.
    ///
    /// This associated functions removes almost all boiler plate for the
    /// case that you want to perform some DNS operation, either a query or
    /// lookup, on a resolver using the system’s configuration and wait for
    /// the result.
    ///
    /// The only argument is a closure taking a reference to a `StubResolver`
    /// and returning a future. Whatever that future resolves to will be
    /// returned.
    pub fn run<R, F>(op: F) -> Result<R::Item, R::Error>
    where
        R: Future + Send + 'static,
        R::Item: Send + 'static,
        R::Error: Send + 'static,
        F: FnOnce(StubResolver) -> R + Send + 'static,
    {
        Self::run_with_conf(ResolvConf::default(), op)
    }

    /// Synchronously perform a DNS operation atop a configured resolver.
    ///
    /// This is like [`run()`] but also takes a resolver configuration for
    /// tailor-making your own resolver.
    ///
    /// [`run()`]: #method.run
    pub fn run_with_conf<R, F>(
        conf: ResolvConf,
        op: F
    ) -> Result<R::Item, R::Error>
    where
        R: Future + Send + 'static,
        R::Item: Send + 'static,
        R::Error: Send + 'static,
        F: FnOnce(StubResolver) -> R + Send + 'static,
    {
        let resolver = Self::from_conf(conf);
        let mut runtime = Runtime::new().unwrap(); // XXX unwrap
        let res = runtime.block_on(lazy(|| op(resolver)));
        runtime.shutdown_on_idle().wait().unwrap();
        res
    }
}

impl Resolver for StubResolver {
    type Answer = Answer;
    type Query = Query;

    fn query<N, Q>(&self, question: Q) -> Query
    where N: ToDname, Q: Into<Question<N>> {
        Query::new(self.clone(), question)
    }
}

impl SearchNames for StubResolver {
    type Iter = SearchIter;

    fn search_iter(&self) -> Self::Iter {
        SearchIter {
            resolver: self.clone(),
            pos: 0
        }
    }
}


//------------ Query ---------------------------------------------------------

#[derive(Debug)]
pub struct Query {
    /// The resolver whose configuration we are using.
    resolver: StubResolver,

    /// Are we still in the preferred server list or have gone streaming?
    preferred: bool,

    /// The number of attempts, starting with zero.
    attempt: usize,

    /// The index in the server list we currently trying.
    counter: ServerListCounter,

    /// The server query we are currently performing.
    ///
    /// If this is an error, we had to bail out before ever starting a query.
    query: Result<ServerQuery, Option<io::Error>>,

    /// The query message we currently work on.
    ///
    /// This is an option so we can take it out temporarily to manipulate it.
    message: Option<QueryMessage>,
}

impl Query {
    fn new<N, Q>(resolver: StubResolver, question: Q) -> Self
    where N: ToDname, Q: Into<Question<N>> {
        let mut message = QueryBuilder::new(question);
        message.set_rd(true);
        let message = message.freeze();
        let (preferred, counter) = if resolver.options().use_vc {
            (false, resolver.0.stream.counter(resolver.options().rotate))
        }
        else {
            (true, resolver.0.preferred.counter(resolver.options().rotate))
        };
        let mut res = Query {
            resolver,
            preferred,
            attempt: 0,
            counter,
            query: Err(None),
            message: Some(message)
        };
        res.query = match res.start_query() {
            Some(query) => Ok(query),
            None => Err(Some(no_servers_error()))
        };
        res
    }

    /// Starts a new query for the current server.
    ///
    /// Prepares the query message and then starts the server query. Returns
    /// `None` if a query cannot be started because 
    fn start_query(&mut self) -> Option<ServerQuery> {
        let mut message = self.message.take().unwrap().unfreeze();
        let (message, res) = {
            let info = match self.current_server() {
                Some(info) => info,
                None => return None
            };
            info.prepare_message(&mut message);
            let message = message.freeze();
            let res = ServerQuery::new(message.clone(), info);
            (message, res)
        };
        self.message = Some(message);
        Some(res)
    }

    /// Returns the info for the current server.
    fn current_server(&self) -> Option<&ServerInfo> {
        let list = if self.preferred { &self.resolver.0.preferred }
                   else { &self.resolver.0.stream };
        self.counter.info(list)
    }


    fn switch_to_stream(&mut self) -> bool {
        self.preferred = false;
        self.attempt = 0;
        self.counter = self.resolver.0.stream.counter(
            self.resolver.options().rotate
        );
        match self.start_query() {
            Some(query) => {
                self.query = Ok(query);
                true
            }
            None => {
                self.query = Err(None);
                false
            }
        }
    }

    fn next_server(&mut self) {
        self.counter.next();
        if let Some(query) = self.start_query() {
            self.query = Ok(query);
            return;
        }
        self.attempt += 1;
        if self.attempt >= self.resolver.options().attempts {
            self.query = Err(Some(giving_up_error()));
            return;
        }
        self.counter = if self.preferred {
            self.resolver.0.preferred.counter(self.resolver.options().rotate)
        }
        else {
            self.resolver.0.stream.counter(self.resolver.options().rotate)
        };
        self.query = match self.start_query() {
            Some(query) => Ok(query),
            None => Err(Some(giving_up_error()))
        }
    }
}

impl Future for Query {
    type Item = Answer;
    type Error = io::Error;

    fn poll(&mut self) -> Result<Async<Self::Item>, Self::Error> {
        let answer = {
            let query = match self.query {
                Ok(ref mut query) => query,
                Err(ref mut err) => {
                    let err = err.take();
                    match err {
                        Some(err) => return Err(err),
                        None => panic!("polled a resolved future")
                    }
                }
            };
            match query.poll() {
                Ok(Async::NotReady) => return Ok(Async::NotReady),
                Ok(Async::Ready(answer)) => Some(answer),
                Err(_) => None,
            }
        };
        match answer {
            Some(answer) => {
                if answer.header().rcode() == Rcode::FormErr
                    && self.current_server().unwrap().does_edns()
                {
                    // FORMERR with EDNS: turn off EDNS and try again.
                    self.current_server().unwrap().disable_edns();
                    self.query = Ok(self.start_query().unwrap());
                }
                else if answer.header().rcode() == Rcode::ServFail {
                    // SERVFAIL: go to next server.
                    self.next_server();
                }
                else if answer.header().tc() && self.preferred
                    && !self.resolver.options().ign_tc
                {
                    // Truncated. If we can, switch to stream transports.
                    if !self.switch_to_stream() {
                        return Ok(Async::Ready(answer))
                    }
                }
                else {
                    // I guess we have an answer ...
                    self.query = Err(None); // Make it panic if polled again.
                    return Ok(Async::Ready(answer));
                }
            }
            None => {
                self.next_server();
            }
        }
        self.poll()
    }
}


//------------ Answer --------------------------------------------------------

/// The answer to a question.
///
/// This type is a wrapper around the DNS [`Message`] containing the answer
/// that provides some additional information.
#[derive(Clone, Debug)]
pub struct Answer {
    message: Message,
}

impl Answer {
    /// Returns whether the answer is a final answer to be returned.
    pub fn is_final(&self) -> bool {
        (self.message.header().rcode() == Rcode::NoError
            || self.message.header().rcode() == Rcode::NXDomain)
        && !self.message.header().tc()
    }

    /// Returns whether the answer is truncated.
    pub fn is_truncated(&self) -> bool {
        self.message.header().tc()
    }

    pub fn into_message(self) -> Message {
        self.message
    }
}

impl From<Message> for Answer {
    fn from(message: Message) -> Self {
        Answer { message }
    }
}

impl ops::Deref for Answer {
    type Target = Message;

    fn deref(&self) -> &Self::Target {
        &self.message
    }
}

impl AsRef<Message> for Answer {
    fn as_ref(&self) -> &Message {
        &self.message
    }
}


//------------ SearchIter ----------------------------------------------------

#[derive(Clone, Debug)]
pub struct SearchIter {
    resolver: StubResolver,
    pos: usize,
}

impl Iterator for SearchIter {
    type Item = Dname;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(res) = self.resolver.options().search.get(self.pos) {
            self.pos += 1;
            Some(res.clone())
        }
        else {
            None
        }
    }
}


//------------ Making Errors -------------------------------------------------
//
// Because we want to use io::Error and creating them is tedious, we have some
// friendly helpers for that.

fn no_servers_error() -> io::Error {
    io::Error::new(io::ErrorKind::NotFound, "no servers available")
}

fn giving_up_error() -> io::Error {
    io::Error::new(io::ErrorKind::TimedOut, "timed out")
}