forked from dylanmckay/mdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.rs
163 lines (149 loc) · 4.62 KB
/
response.rs
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
use std::net;
use std::net::{IpAddr, SocketAddr};
/// A DNS response.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Response {
pub answers: Vec<Record>,
pub nameservers: Vec<Record>,
pub additional: Vec<Record>,
}
/// Any type of DNS record.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Record {
pub name: String,
pub class: dns_parser::Class,
pub ttl: u32,
pub kind: RecordKind,
}
/// A specific DNS record variant.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RecordKind {
A(net::Ipv4Addr),
AAAA(net::Ipv6Addr),
CNAME(String),
MX {
preference: u16,
exchange: String,
},
NS(String),
SRV {
priority: u16,
weight: u16,
port: u16,
target: String,
},
TXT(Vec<String>),
PTR(String),
/// A record kind that hasn't been implemented by this library yet.
Unimplemented(Vec<u8>),
}
impl Response {
pub fn from_packet(packet: &dns_parser::Packet) -> Self {
Response {
answers: packet
.answers
.iter()
.map(Record::from_resource_record)
.collect(),
nameservers: packet
.nameservers
.iter()
.map(Record::from_resource_record)
.collect(),
additional: packet
.additional
.iter()
.map(Record::from_resource_record)
.collect(),
}
}
pub fn records(&self) -> impl Iterator<Item = &Record> {
self.answers
.iter()
.chain(self.nameservers.iter())
.chain(self.additional.iter())
}
pub fn is_empty(&self) -> bool {
self.answers.is_empty() && self.nameservers.is_empty() && self.additional.is_empty()
}
pub fn ip_addr(&self) -> Option<IpAddr> {
self.records().find_map(|record| match record.kind {
RecordKind::A(addr) => Some(addr.into()),
RecordKind::AAAA(addr) => Some(addr.into()),
_ => None,
})
}
pub fn hostname(&self) -> Option<&str> {
self.records().find_map(|record| match record.kind {
RecordKind::PTR(ref host) => Some(host.as_str()),
_ => None,
})
}
pub fn port(&self) -> Option<u16> {
self.records().find_map(|record| match record.kind {
RecordKind::SRV { port, .. } => Some(port),
_ => None,
})
}
pub fn socket_address(&self) -> Option<SocketAddr> {
Some((self.ip_addr()?, self.port()?).into())
}
pub fn txt_records(&self) -> impl Iterator<Item = &str> {
self.records()
.filter_map(|record| match record.kind {
RecordKind::TXT(ref txt) => Some(txt),
_ => None,
})
.flat_map(|txt| txt.iter())
.map(|txt| txt.as_str())
}
}
impl Record {
fn from_resource_record(rr: &dns_parser::ResourceRecord) -> Self {
Record {
name: rr.name.to_string(),
class: rr.cls,
ttl: rr.ttl,
kind: RecordKind::from_rr_data(&rr.data),
}
}
}
impl RecordKind {
fn from_rr_data(data: &dns_parser::RData) -> Self {
use dns_parser::RData;
match *data {
RData::A(dns_parser::rdata::a::Record(addr)) => RecordKind::A(addr),
RData::AAAA(dns_parser::rdata::aaaa::Record(addr)) => RecordKind::AAAA(addr),
RData::CNAME(ref name) => RecordKind::CNAME(name.to_string()),
RData::MX(dns_parser::rdata::mx::Record {
preference,
ref exchange,
}) => RecordKind::MX {
preference,
exchange: exchange.to_string(),
},
RData::NS(ref name) => RecordKind::NS(name.to_string()),
RData::PTR(ref name) => RecordKind::PTR(name.to_string()),
RData::SRV(dns_parser::rdata::srv::Record {
priority,
weight,
port,
ref target,
}) => RecordKind::SRV {
priority,
weight,
port,
target: target.to_string(),
},
RData::TXT(ref txt) => RecordKind::TXT(
txt.iter()
.map(|bytes| String::from_utf8_lossy(bytes).into_owned())
.collect(),
),
RData::SOA(..) => {
RecordKind::Unimplemented("SOA record handling is not implemented".into())
}
RData::Unknown(data) => RecordKind::Unimplemented(data.to_owned()),
}
}
}