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 core::ops::{Deref, DerefMut};
11
12use crate::op::{Message, Query};
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 /// When true, sets the DO bit in the EDNS options
29 pub edns_set_dnssec_ok: bool,
30 /// Specifies maximum request depth for DNSSEC validation.
31 pub max_request_depth: usize,
32 /// set recursion desired (or not) for any requests
33 pub recursion_desired: bool,
34 /// Randomize case of query name, and check that the response matches, for spoofing resistance.
35 #[cfg(feature = "std")]
36 pub case_randomization: bool,
37}
38
39impl Default for DnsRequestOptions {
40 fn default() -> Self {
41 #[allow(deprecated)]
42 Self {
43 max_request_depth: 26,
44 expects_multiple_responses: false,
45 use_edns: false,
46 edns_set_dnssec_ok: false,
47 recursion_desired: true,
48 #[cfg(feature = "std")]
49 case_randomization: false,
50 }
51 }
52}
53
54/// A DNS request object
55///
56/// This wraps a DNS Message for requests. It also has request options associated for controlling certain features of the DNS protocol handlers.
57#[derive(Clone, PartialEq, Eq)]
58pub struct DnsRequest {
59 message: Message,
60 options: DnsRequestOptions,
61 /// If case randomization was replied to the request, this holds the original query.
62 original_query: Option<Query>,
63}
64
65impl DnsRequest {
66 /// Returns a new DnsRequest object
67 pub fn new(message: Message, options: DnsRequestOptions) -> Self {
68 Self {
69 message,
70 options,
71 original_query: None,
72 }
73 }
74
75 /// Add the original query
76 pub fn with_original_query(mut self, original_query: Option<Query>) -> Self {
77 self.original_query = original_query;
78 self
79 }
80
81 /// Get the set of request options associated with this request
82 pub fn options(&self) -> &DnsRequestOptions {
83 &self.options
84 }
85
86 /// Unwraps the raw message
87 pub fn into_parts(self) -> (Message, DnsRequestOptions) {
88 (self.message, self.options)
89 }
90
91 /// Get the request's original query
92 pub fn original_query(&self) -> Option<&Query> {
93 self.original_query.as_ref()
94 }
95}
96
97impl Deref for DnsRequest {
98 type Target = Message;
99 fn deref(&self) -> &Self::Target {
100 &self.message
101 }
102}
103
104impl DerefMut for DnsRequest {
105 fn deref_mut(&mut self) -> &mut Self::Target {
106 &mut self.message
107 }
108}
109
110impl From<Message> for DnsRequest {
111 fn from(message: Message) -> Self {
112 Self::new(message, DnsRequestOptions::default())
113 }
114}