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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*-
* cdns-rs - a simple sync/async DNS query library
* Copyright (C) 2020  Aleksandr Morozov, RELKOM s.r.o
* Copyright (C) 2021  Aleksandr Morozov
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

use std::collections::LinkedList;
use std::fmt;
use std::net::IpAddr;
use std::time::Duration;

use crate::error::*;
use crate::{write_error, internal_error, internal_error_map};

use super::query::{QDns, QDnsName, QuerySetup, QueryMode};
use super::common::{QType, DnsRdata};


/// Resolves the FQDN to a list of an IPv6 addresses.
/// 
/// # Arguments
/// 
/// * `fqdn` - a domain which is needed to be resolved
/// 
/// # Returns
/// 
/// * [CDnsResult] with vector of [IpAddr]
/// 
/// * [CDnsResult] with error
pub async
fn resolve_aaaa<C>(fqdn: C) -> CDnsResult<Vec<IpAddr>>
where C: AsRef<str>
{
    let res = 
        QDns::query_optional(
            QDnsName::from(fqdn.as_ref()), 
            QType::AAAA, 
            QuerySetup::new(
                false, 
                false, 
                QueryMode::ForceNxQueryAll, 
                Some(Duration::from_secs(3))
            )
        ).await?;

    let dns_qs = 
        match res.into_inner()
        {
            Some(r) => r,
            None => return Ok(Vec::new()),
        };

    let mut ret_list: Vec<IpAddr> = Vec::with_capacity(dns_qs.len());

    for dq in dns_qs
    {
        let ok_res = dq.collect_ok_responses();

        for okr in ok_res
        {
            match okr
            {
                DnsRdata::AAAA{ ip } =>
                {
                    ret_list.push(IpAddr::from(ip));
                },
                _ => {} // skip rest
            }
        }
    }

    return Ok(ret_list);
}

/// Resolves the FQDN to a list of an IPv4 addresses.
/// 
/// # Arguments
/// 
/// * `fqdn` - a domain which is needed to be resolved
/// 
/// # Returns
/// 
/// * [CDnsResult] with vector of [IpAddr]
/// 
/// * [CDnsResult] with error
pub async
fn resolve_a<C>(fqdn: C) -> CDnsResult<Vec<IpAddr>>
where C: AsRef<str>
{
    let res = 
        QDns::query_optional(
            QDnsName::from(fqdn.as_ref()), 
            QType::A, 
            QuerySetup::new(
                false, 
                false, 
                QueryMode::ForceNxQueryAll, 
                Some(Duration::from_secs(3))
            )
        ).await?;

    let dns_qs = 
        match res.into_inner()
        {
            Some(r) => r,
            None => return Ok(Vec::new()),
        };

    let mut ret_list: Vec<IpAddr> = Vec::with_capacity(dns_qs.len());

    for dq in dns_qs
    {
        let ok_res = dq.collect_ok_responses();

        for okr in ok_res
        {
            match okr
            {
                DnsRdata::A{ ip } =>
                {
                    ret_list.push(IpAddr::from(ip));
                },
                _ => {} // skip rest
            }
        }
    }

    return Ok(ret_list);
}

#[derive(Debug)]
pub struct DnsMxResolved
{
    pub preference: u16,
    pub exchange_fqdn: String, 
    pub ip: IpAddr
}

impl fmt::Display for DnsMxResolved
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result 
    {
        write!(f, "{} {} {}", self.preference, self.exchange_fqdn, self.ip)    
    }
}

/// Searches for the MX records of the provided domain.
/// 
/// Does not take into account the preference.
/// 
/// The reply (on request) timeout is 3 seconds.
/// 
/// # Arguments
/// 
/// * `domain` - a domain which is required to check.
/// 
/// * `aaaa` - if set to true then AAAA will also be resolved.
/// 
/// # Returns
/// 
/// * [CDnsResult] with a pair of FQDN, IP address the FQDN points to
/// 
/// * [CDnsResult] with an error message
pub async 
fn resolve_mx<D>(domain: D, aaaa: bool) -> CDnsResult<Option<Vec<DnsMxResolved>>>
where D: AsRef<str>
{
    let mut ret_list: LinkedList<DnsMxResolved> = LinkedList::new();

    let res = 
        QDns::query_optional(
            QDnsName::from(domain.as_ref()), 
            QType::MX, 
            QuerySetup::new(
                false, 
                false, 
                QueryMode::ForceNxQueryAll, 
                Some(Duration::from_secs(3))
            )
        ).await?;

    let dns_qs = 
        match res.into_inner()
        {
            Some(r) => r,
            None => return Ok(None),
        };

    for dq in dns_qs
    {
        let ok_res = dq.collect_ok_responses();

        for okr in ok_res
        {
            match okr
            {
                DnsRdata::MX{ preference, exchange } =>
                {
                    // use preference to insert the record in order
                    // exchange sould be CNAME, but may be an IP address if set up incorectly
                    // check if it really FQDN, not IP

                    // send new request to resolve fqdn
                    match resolve_a(&exchange).await
                    {
                        Ok(ip_list) =>
                        {
                            for ip in ip_list
                            {
                                ret_list.push_back(
                                    DnsMxResolved
                                    {
                                        preference: preference,
                                        exchange_fqdn: exchange.clone(),
                                        ip: ip
                                    }
                                );
                            }
                        },
                        Err(e) => 
                        {
                            write_error!("{}", e);
                            continue;
                        }
                    }

                    // check if AAAA is needed
                    if aaaa == true
                    {
                        match resolve_aaaa(&exchange).await
                        {
                            Ok(ip_list) =>
                            {
                                for ip in ip_list
                                {
                                    ret_list.push_back(
                                        DnsMxResolved
                                        {
                                            preference: preference,
                                            exchange_fqdn: exchange.clone(),
                                            ip: ip
                                        }
                                    );
                                }
                            },
                            Err(e) => 
                            {
                                write_error!("{}", e);
                                continue;
                            }
                        }
                    }

                },
                _ => {} // skip rest
            }
        }
    }

    return Ok(Some(ret_list.into_iter().map(|d| d).collect()));
}


#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_mx_resolve()
{
    let fqdn_ip = resolve_mx("protonmail.com", true).await;

    assert_eq!(fqdn_ip.is_err(), false);

    let fqdn_ip = fqdn_ip.unwrap();

    assert_eq!(fqdn_ip.is_some(), true);

    let dnsips = fqdn_ip.unwrap();

    for di in dnsips
    {
        println!("{}", di);
    }
    
}