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
//! The two public types `Resolver` and `Query`.
//!
//! These two are here together because `Query` needs to be able to access 
//! `Resolver`’s transport handles yet I don’t want to expose them publicly.

use std::io;
use std::sync::Arc;
use futures::{Async, Future, Poll};
use rand::random;
use tokio_core::reactor;
use ::bits::{DName, MessageBuf, Question};
use ::iana::Rcode;
use super::conf::{ResolvConf, ResolvOptions};
use super::error::Error;
use super::request::{QueryRequest, RequestMessage, TransportHandle};
use super::tcp::tcp_transport;
use super::udp::udp_transport;


//------------ Resolver ------------------------------------------------------

/// Access to a DNS 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()`]. Either function will spawn everything necessary
/// into a `tokio_core` reactor core represented by a handle.
///
/// Resolver values can be cloned relatively cheaply as they keep all
/// information behind an arc. This is may already be useful when starting
/// a query using the [`query()`] method. Since queries need their own copy
/// of resolver, the method consumes the resolver leaving it to clone it if
/// you need it later on or saving a clone if you don’t.
///
/// If you want to run a single query or lookup on a resolver synchronously,
/// you can safe yourself all the boiler plate of creating a reactor core
/// (even if this is nowhere near as dangerous as it sounds) and resolver 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 Resolver(Arc<ResolverInner>);

/// The actual resolver.
#[derive(Clone, Debug)]
struct ResolverInner {
    /// Handles to all UDP transports.
    udp: Vec<TransportHandle>,

    /// Handles to all TCP transports.
    tcp: Vec<TransportHandle>,

    /// Our resolver configuration.
    conf: ResolvConf,
}

impl Resolver {
    /// Creates a new resolver using the system’s default configuration.
    ///
    /// All the networking components of the resolver will be spawned into
    /// the reactor core referenced by `reactor`.
    pub fn new(reactor: &reactor::Handle) -> Self {
        Self::from_conf(reactor, ResolvConf::default())
    }

    /// Creates a new resolver using the given configuration.
    ///
    /// All the components of the resolver will be spawned into the reactor
    /// referenced by `reactor`.
    pub fn from_conf(reactor: &reactor::Handle, conf: ResolvConf)
                     -> Self {
        let mut udp = Vec::new();
        let mut tcp = Vec::new();

        for server in &conf.servers {
            if let Some(transport) = udp_transport(reactor, server) {
                udp.push(transport)
            }
            if let Some(transport) = tcp_transport(reactor, server) {
                tcp.push(transport)
            }
        }

        Resolver(Arc::new(ResolverInner {
            udp: udp,
            tcp: tcp,
            conf: conf
        }))
    }

    /// Trades the resolver for a DNS query.
    ///
    /// This starts a query for something that can be turned into a question.
    /// In particular, both a tripel of a domain name, record type, and class
    /// as well as a pair of domain name and record type can be turned into
    /// a question, the latter assuming the class IN.
    ///
    /// If you need to keep the resolver, clone it before calling `query()`.
    pub fn query<N, Q>(self, question: Q) -> Query
                 where N: DName, Q: Into<Question<N>> {
        Query::new(self, question)
    }

    /// Returns a reference to the list of UDP service handles.
    fn udp(&self) -> &[TransportHandle] {
        &self.0.udp
    }

    /// Returns a reference to the list of TCP service handles.
    fn tcp(&self) -> &[TransportHandle] {
        &self.0.tcp
    }

    /// Returns a reference to the configuration of this resolver.
    pub fn conf(&self) -> &ResolvConf {
        &self.0.conf
    }

    /// Returns a reference to the configuration options of this resolver.
    pub fn options(&self) -> &ResolvOptions {
        &self.0.conf.options
    }
}

/// # Shortcuts
///
impl Resolver {
    /// 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 `Resolver` 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,
                     R::Error: From<io::Error>,
                     F: FnOnce(Resolver) -> R {
        Self::run_with_conf(ResolvConf::default(), op)
    }

    /// Synchronously perform a DNS operation atop a configuredresolver.
    ///
    /// 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,
                               R::Error: From<io::Error>,
                               F: FnOnce(Resolver) -> R {
        let mut reactor = reactor::Core::new()?;
        let resolver = Self::from_conf(&reactor.handle(), conf);
        reactor.run(op(resolver))
    }
}


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

/// A DNS query.
///
/// A query is a future that resolves a DNS question for all the resource
/// records for a given domain name, record type, and class into either a
/// successful response or query error. It will follow the rules set down
/// by the resolver configuration in trying to find an upstream server that
/// replies to the question.
///
/// While you can start a query directly by calling the `Query::new()`
/// function, the resolver’s `query()` method may be more convenient.
//
//  Since a query can fail very early on when building the request message,
//  but we don’t really want to complicate things by `Query::new()` returning
//  a result, it internally consists of that `Result`. If it is `Ok()`, we
//  really do have a query to poll, otherwise we have an error to take out
//  and return.
//
//  This is okay because the early failure should be rather unlikely.
pub struct Query(Result<QueryInner, Option<Error>>);

impl Query {
    /// Starts a new query.
    pub fn new<N, Q>(resolv: Resolver, question: Q) -> Self
               where N: DName, Q: Into<Question<N>> {
        let message = match RequestMessage::new(question, resolv.conf()) {
            Ok(message) => message,
            Err(err) => return Query(Err(Some(err.into())))
        };
        Query(Ok(QueryInner::new(resolv, message)))
    }
}


//--- Future

impl Future for Query {
    type Item = MessageBuf;
    type Error = Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match self.0 {
            Ok(ref mut inner) => inner.poll(),
            Err(ref mut err) => {
                match err.take() {
                    Some(err) => Err(err),
                    None => panic!("polling a resolved Query")
                }
            }
        }
    }
}


//------------ QueryInner ----------------------------------------------------

struct QueryInner {
    /// The resolver we are working with.
    resolver: Resolver,

    /// The request we are currently processing.
    request: QueryRequest,

    /// Are we on datagram track?
    ///
    /// A query can be in one of two *tracks,* datagram track or stream
    /// track. Unless the config states that we should use the stream
    /// track only, we will start out in datagram track and only switch to
    /// the stream track if we receive a truncated response.
    dgram: bool,

    /// The index of the service we started at.
    ///
    /// The index references either `resolver.udp` or `resolver.tcp`,
    /// depending on the value of `dgram`. We need to keep the index we
    /// started at because of the rotate option which makes us start at a
    /// random service.
    start_index: usize,

    /// The index of the service we currently are using.
    curr_index: usize,

    /// The how-many-th attempt this is, starting at attempt 0.
    attempt: usize,
}


impl QueryInner {
    fn new(resolver: Resolver, message: RequestMessage) -> Self {
        let dgram = !resolver.options().use_vc;
        let (index, request) = Self::start(&resolver, dgram, message);
        QueryInner {
            resolver: resolver,
            request: request,
            dgram: dgram,
            start_index: index,
            curr_index: index,
            attempt: 0
        }
    }

    /// Processes a response received from a request.
    ///
    /// This will either resolve the future or switch to stream mode and
    /// continue.
    fn response(&mut self, response: MessageBuf, message: RequestMessage)
                -> Poll<MessageBuf, Error> {
        if response.header().tc() && self.dgram
                && !self.resolver.options().ign_tc {
            self.start_stream(message)
        }
        else if response.header().rcode() != Rcode::NoError {
            Err(response.header().rcode().into())
        }
        else {
            Ok(Async::Ready(response))
        }
    }

    /// Processes an error received from a request.
    ///
    /// Proceeds to the next request or errors out.
    fn error(&mut self, _error: Error, message: RequestMessage)
             -> Poll<MessageBuf, Error> {
        self.curr_index = (self.curr_index + 1) % self.track().len();
        if self.curr_index == self.start_index {
            self.attempt += 1;
            if self.attempt == self.resolver.conf().attempts {
                return Err(Error::Timeout)
            }
            let (index, request) = Self::start(&self.resolver,
                                               self.dgram, message);
            self.start_index = index;
            self.curr_index = index;
            self.request = request;
        }
        else {
            self.request = QueryRequest::new(message,
                                             &self.track()[self.curr_index]);
        }
        self.poll()
    }

    /// Switches to stream mode and starts the first request.
    fn start_stream(&mut self, message: RequestMessage)
                    -> Poll<MessageBuf, Error> {
        self.dgram = false;
        let (index, request) = Self::start(&self.resolver, false,
                                           message);
        self.start_index = index;
        self.curr_index = index;
        self.request = request;
        self.poll()
    }

    /// Determines the start index and request for a new attempt.
    fn start(resolver: &Resolver, dgram: bool, message: RequestMessage)
             -> (usize, QueryRequest) {
        let track = if dgram { resolver.udp() }
                    else { resolver.tcp() };
        let index = if resolver.options().rotate {
            random::<usize>() % track.len()
        }
        else { 0 };
        let request = QueryRequest::new(message, &track[index]);
        (index, request)
    }

    /// Returns the current track.
    ///
    /// This is either the UDP or TCP service handles of the resolver,
    /// depending on whether we are in datagram or stream mode.
    fn track(&self) -> &[TransportHandle] {
        if self.dgram {
            self.resolver.udp()
        }
        else {
            self.resolver.tcp()
        }
    }
}


//--- Future

impl Future for QueryInner {
    type Item = MessageBuf;
    type Error = Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match self.request.poll() {
            Ok(Async::NotReady) => Ok(Async::NotReady),
            Ok(Async::Ready((response, message))) => {
                self.response(response, message)
            }
            Err((error, message)) => {
                self.error(error, message)
            }
        }
    }
}