Skip to main content

iris_core/protocols/stream/dns/
mod.rs

1//! DNS transaction parsing.
2
3pub mod parser;
4mod transaction;
5
6pub use self::transaction::*;
7
8use serde::Serialize;
9
10/// Parsed DNS transaction contents.
11///
12/// A DNS transaction consists of a query and a response.
13#[derive(Clone, Debug, Serialize)]
14pub struct Dns {
15    /// DNS transaction ID.
16    pub transaction_id: u16,
17    /// DNS Query.
18    pub query: Option<DnsQuery>,
19    /// DNS Response.
20    pub response: Option<DnsResponse>,
21}
22
23impl Dns {
24    /// Returns the DNS query domain name, or `""` if no query was observed in the transaction.
25    pub fn query_domain(&self) -> &str {
26        if let Some(query) = &self.query {
27            if !query.queries.is_empty() {
28                return &query.queries[0];
29            }
30        }
31        ""
32    }
33
34    /// Returns a string representation of the answers
35    pub fn answers(&self) -> String {
36        if let Some(resp) = &self.response {
37            if !resp.answers.is_empty() {
38                return serde_json::to_string(&resp.answers).unwrap_or(String::new());
39            }
40        }
41        String::new()
42    }
43
44    /// Returns a string representation of the response nameservers
45    pub fn nameservers(&self) -> String {
46        if let Some(resp) = &self.response {
47            if !resp.nameservers.is_empty() {
48                return serde_json::to_string(&resp.nameservers).unwrap_or(String::new());
49            }
50        }
51        String::new()
52    }
53
54    /// Returns a string representation of the response additionals
55    pub fn additionals(&self) -> String {
56        if let Some(resp) = &self.response {
57            if !resp.additionals.is_empty() {
58                return serde_json::to_string(&resp.additionals).unwrap_or(String::new());
59            }
60        }
61        String::new()
62    }
63
64    /// Returns a string representation of the response
65    pub fn response(&self) -> String {
66        if let Some(resp) = &self.response {
67            return serde_json::to_string(&resp).unwrap_or(String::new());
68        }
69        String::new()
70    }
71}