hickory_proto/xfer/dns_request.rs
1// Copyright 2015-2018 Benjamin Fry <benjaminfry@me.com>
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// https://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// https://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! `DnsRequest` wraps a `Message` and associates a set of `DnsRequestOptions` for specifying different transfer options.
9
10use std::ops::{Deref, DerefMut};
11
12use crate::op::Message;
13
14/// A set of options for expressing options to how requests should be treated
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
16#[non_exhaustive]
17pub struct DnsRequestOptions {
18 /// When true, the underlying DNS protocols will not return on the first response received.
19 ///
20 /// Setting this option will cause the underlying protocol to await the timeout, and then return all Responses.
21 #[deprecated]
22 pub expects_multiple_responses: bool,
23 // /// If set, then the request will terminate early if all types have been received
24 // pub expected_record_types: Option<SmallVec<[RecordType; 2]>>,
25 // TODO: add EDNS options here?
26 /// When true, will add EDNS options to the request.
27 pub use_edns: bool,
28 /// Specifies maximum request depth for DNSSEC validation.
29 pub max_request_depth: usize,
30 /// set recursion desired (or not) for any requests
31 pub recursion_desired: bool,
32}
33
34impl Default for DnsRequestOptions {
35 fn default() -> Self {
36 #[allow(deprecated)]
37 Self {
38 max_request_depth: 26,
39 expects_multiple_responses: false,
40 use_edns: false,
41 recursion_desired: true,
42 }
43 }
44}
45
46/// A DNS request object
47///
48/// This wraps a DNS Message for requests. It also has request options associated for controlling certain features of the DNS protocol handlers.
49#[derive(Clone, PartialEq, Eq)]
50pub struct DnsRequest {
51 message: Message,
52 options: DnsRequestOptions,
53}
54
55impl DnsRequest {
56 /// Returns a new DnsRequest object
57 pub fn new(message: Message, options: DnsRequestOptions) -> Self {
58 Self { message, options }
59 }
60
61 /// Get the set of request options associated with this request
62 pub fn options(&self) -> &DnsRequestOptions {
63 &self.options
64 }
65
66 /// Unwraps the raw message
67 pub fn into_parts(self) -> (Message, DnsRequestOptions) {
68 (self.message, self.options)
69 }
70}
71
72impl Deref for DnsRequest {
73 type Target = Message;
74 fn deref(&self) -> &Self::Target {
75 &self.message
76 }
77}
78
79impl DerefMut for DnsRequest {
80 fn deref_mut(&mut self) -> &mut Self::Target {
81 &mut self.message
82 }
83}
84
85impl From<Message> for DnsRequest {
86 fn from(message: Message) -> Self {
87 Self::new(message, DnsRequestOptions::default())
88 }
89}