rsip_dns/records/
srv_record.rs

1use super::SrvDomain;
2use rsip::{Domain, Port, Transport};
3
4/// Simple struct that holds the SRV record details (domain and srv entries)
5#[derive(Debug, Clone)]
6pub struct SrvRecord {
7    pub entries: Vec<SrvEntry>,
8    pub domain: SrvDomain,
9}
10
11/// Simple struct that resembles the SRV record entries
12#[derive(Debug, Clone)]
13pub struct SrvEntry {
14    pub priority: u16,
15    pub weight: u16,
16    pub port: Port,
17    pub target: Domain,
18}
19
20impl SrvRecord {
21    pub fn targets(&self) -> Vec<Domain> {
22        self.entries.iter().map(|s| s.target.clone()).collect::<Vec<Domain>>()
23    }
24
25    pub fn domains_with_ports(&self) -> Vec<(Domain, Port)> {
26        self.entries.iter().map(|s| (s.target.clone(), s.port)).collect::<Vec<_>>()
27    }
28
29    pub fn transport(&self) -> Transport {
30        self.domain.transport()
31    }
32
33    pub fn sorted(mut self) -> Self {
34        use std::cmp::Reverse;
35
36        self.entries.sort_by_key(|b| Reverse(b.total_weight()));
37        self
38    }
39}
40
41impl SrvEntry {
42    pub fn total_weight(&self) -> u16 {
43        (10000 - self.priority) + self.weight
44    }
45}
46
47impl IntoIterator for SrvRecord {
48    type Item = SrvEntry;
49    type IntoIter = std::vec::IntoIter<Self::Item>;
50
51    fn into_iter(self) -> Self::IntoIter {
52        self.entries.into_iter()
53    }
54}
55
56#[cfg(feature = "test-utils")]
57impl testing_utils::Randomize for SrvDomain {
58    fn random() -> Self {
59        use testing_utils::Randomize;
60
61        SrvDomain {
62            domain: Randomize::random(),
63            protocol: Randomize::random(),
64            secure: bool::random(),
65        }
66    }
67}
68
69#[cfg(feature = "test-utils")]
70impl testing_utils::Randomize for SrvEntry {
71    fn random() -> Self {
72        use testing_utils::Randomize;
73
74        let secure = bool::random();
75        let transport = match secure {
76            true => Transport::Tls,
77            _ => Transport::random(),
78        };
79        Self {
80            priority: testing_utils::rand_num_from(0..10),
81            weight: testing_utils::rand_num_from(0..100),
82            port: Randomize::random(),
83            target: format!("_sip._{}.{}", transport.to_string().to_lowercase(), Domain::random())
84                .into(),
85        }
86    }
87}
88
89/*
90#[cfg(feature = "test-utils")]
91impl crate::Randomize for SrvRecord {
92    fn random() -> Self {
93        use crate::Randomize;
94
95        (2..5)
96            .map(|_| SrvEntry::random())
97            .collect::<Vec<_>>()
98            .into()
99    }
100}*/