iroh_dns_server/http/doh/
response.rs

1//! DNS Response
2
3// This module is mostly copied from
4// https://github.com/fission-codes/fission-server/blob/394de877fad021260c69fdb1edd7bb4b2f98108c/fission-core/src/dns.rs
5
6use hickory_server::proto;
7use n0_error::{Result, ensure_any};
8use serde::{Deserialize, Serialize};
9
10/// JSON representation of a DNS response
11/// See: <https://developers.google.com/speed/public-dns/docs/doh/json>
12#[derive(Debug, Serialize, Deserialize)]
13pub(crate) struct DnsResponse {
14    /// Standard DNS response code
15    #[serde(rename = "Status")]
16    pub status: u32,
17    /// Whether the response was truncated
18    #[serde(rename = "TC")]
19    pub tc: bool,
20    /// Whether recursion was desired
21    #[serde(rename = "RD")]
22    pub rd: bool,
23    /// Whether recursion was available
24    #[serde(rename = "RA")]
25    pub ra: bool,
26    /// Whether the response was validated with DNSSEC
27    #[serde(rename = "AD")]
28    pub ad: bool,
29    /// Whether the client asked to disable DNSSEC validation
30    #[serde(rename = "CD")]
31    pub cd: bool,
32    /// The questions that this request answers
33    #[serde(rename = "Question")]
34    pub question: Vec<DohQuestionJson>,
35    /// The answers to the request
36    #[serde(rename = "Answer")]
37    #[serde(skip_serializing_if = "Vec::is_empty")]
38    pub answer: Vec<DohRecordJson>,
39    /// An optional comment
40    #[serde(rename = "Comment")]
41    pub comment: Option<String>,
42    /// IP Address / scope prefix-length of the client
43    /// See: <https://tools.ietf.org/html/rfc7871>
44    pub edns_client_subnet: Option<String>,
45}
46
47impl DnsResponse {
48    /// Create a new JSON response from a DNS message
49    pub(crate) fn from_message(message: proto::op::Message) -> Result<Self> {
50        ensure_any!(
51            message.metadata.message_type == proto::op::MessageType::Response,
52            "Expected message type to be response"
53        );
54
55        let status: u32 =
56            <u16 as From<proto::op::ResponseCode>>::from(message.metadata.response_code) as u32;
57
58        let question: Vec<_> = message
59            .queries
60            .iter()
61            .map(DohQuestionJson::from_query)
62            .collect();
63
64        let answer: Vec<_> = message
65            .answers
66            .iter()
67            .map(DohRecordJson::from_record)
68            .collect::<Result<_>>()?;
69
70        Ok(DnsResponse {
71            status,
72            tc: message.metadata.truncation,
73            rd: message.metadata.recursion_desired,
74            ra: message.metadata.recursion_available,
75            ad: message.metadata.authentic_data,
76            cd: message.metadata.checking_disabled,
77            question,
78            answer,
79            comment: None,
80            edns_client_subnet: None,
81        })
82    }
83}
84
85#[derive(Debug, Serialize, Deserialize)]
86/// JSON representation of a DNS question
87pub(crate) struct DohQuestionJson {
88    /// FQDN with trailing dot
89    pub(crate) name: String,
90    /// Standard DNS RR type
91    #[serde(rename = "type")]
92    pub(crate) question_type: u16,
93}
94
95impl DohQuestionJson {
96    /// Create a new JSON question from a DNS query
97    pub(crate) fn from_query(query: &proto::op::Query) -> Self {
98        Self {
99            name: query.name().to_string(),
100            question_type: query.query_type().into(),
101        }
102    }
103}
104
105#[derive(Debug, Serialize, Deserialize)]
106/// JSON representation of a DNS record
107pub(crate) struct DohRecordJson {
108    /// FQDN with trailing dot
109    pub(crate) name: String,
110    /// Standard DNS RR type
111    #[serde(rename = "type")]
112    pub(crate) record_type: u16,
113    /// Time-to-live, in seconds
114    #[serde(rename = "TTL")]
115    pub(crate) ttl: u32,
116    /// Record data
117    pub(crate) data: String,
118}
119
120impl DohRecordJson {
121    /// Create a new JSON record from a DNS record
122    pub(crate) fn from_record(record: &proto::rr::Record) -> Result<Self> {
123        Ok(Self {
124            name: record.name.to_string(),
125            record_type: record.record_type().into(),
126            ttl: record.ttl,
127            data: record.data.to_string(),
128        })
129    }
130}