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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//! Service discovery and advertising.

use std::{
    collections::{btree_map::Entry, BTreeMap},
    fmt,
};

use crate::{
    name::{DomainName, Label},
    packet::records::{PTR, SRV, TXT},
    Error,
};

pub mod advertising;
pub mod discovery;

/// Transport protocol used by an advertised service (`_tcp` or `_udp`).
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ServiceTransport {
    /// Service uses TCP.
    TCP,
    /// Anything but TCP (UDP, SCTP, etc.).
    Other,
}

impl ServiceTransport {
    fn as_str(&self) -> &str {
        match self {
            ServiceTransport::TCP => "_tcp",
            ServiceTransport::Other => "_udp",
        }
    }

    pub fn to_label(&self) -> Label {
        Label::new(self.as_str())
    }
}

/// A service type identifier.
///
/// A service type is identified by a unique name ([`Label`]), and the [`ServiceTransport`] the
/// service can be reached with.
///
/// *Instances* of a service running on a specific machine are represented by [`ServiceInstance`].
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Service {
    /// The service name, starting with an underscore.
    name: Label,
    transport: ServiceTransport,
}

impl Service {
    /// Creates a new service.
    ///
    /// # Panics
    ///
    /// Panics if `name` does not start with an underscore (`_`).
    pub fn new(name: Label, transport: ServiceTransport) -> Self {
        assert!(name.as_bytes().starts_with(b"_"));
        Self { name, transport }
    }

    pub fn from_ptr(ptr: PTR<'_>) -> Result<Self, Error> {
        let mut labels = ptr.ptrdname().labels().iter();
        let service_name = labels.next().ok_or(Error::Eof)?;
        let transport = labels.next().ok_or(Error::Eof)?;
        if labels.next().is_none() {
            // Domain missing, this is probably not a valid service.
            return Err(Error::Eof);
        }
        Ok(Service {
            name: service_name.clone(),
            transport: match transport.as_bytes() {
                b"_tcp" => ServiceTransport::TCP,
                b"_udp" => ServiceTransport::Other,
                _ => return Err(Error::InvalidValue),
            },
        })
    }

    #[inline]
    pub fn name(&self) -> &Label {
        &self.name
    }

    #[inline]
    pub fn transport(&self) -> ServiceTransport {
        self.transport
    }
}

impl fmt::Display for Service {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{}", self.name, self.transport.as_str())
    }
}

impl fmt::Debug for Service {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

/// A named instance of a [`Service`].
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ServiceInstance {
    instance_name: Label,
    service: Service,
}

impl ServiceInstance {
    /// Creates a new [`ServiceInstance`] from its components.
    ///
    /// `instance_name` can be a free-form string, typically identifying the machine the service is
    /// running on.
    ///
    /// `service_name` must start with an underscore and is an agreed-upon identifier for the
    /// service being offered.
    pub fn new(instance_name: Label, service_name: Label, transport: ServiceTransport) -> Self {
        Self {
            instance_name,
            service: Service::new(service_name, transport),
        }
    }

    pub fn from_service(instance_name: Label, service: Service) -> Self {
        Self {
            instance_name,
            service,
        }
    }

    pub fn from_ptr(ptr: PTR<'_>) -> Result<Self, Error> {
        let mut labels = ptr.ptrdname().labels().iter();
        let instance_name = labels.next().ok_or(Error::Eof)?;
        let service_name = labels.next().ok_or(Error::Eof)?;
        let transport = labels.next().ok_or(Error::Eof)?;
        if labels.next().is_none() {
            // Domain missing, this is probably not a valid service.
            return Err(Error::Eof);
        }
        Ok(ServiceInstance {
            instance_name: instance_name.clone(),
            service: Service {
                name: service_name.clone(),
                transport: match transport.as_bytes() {
                    b"_tcp" => ServiceTransport::TCP,
                    b"_udp" => ServiceTransport::Other,
                    _ => return Err(Error::InvalidValue),
                },
            },
        })
    }

    #[inline]
    pub fn instance_name(&self) -> &Label {
        &self.instance_name
    }

    #[inline]
    pub fn service(&self) -> &Service {
        &self.service
    }

    #[inline]
    pub fn service_name(&self) -> &Label {
        self.service.name()
    }

    #[inline]
    pub fn service_transport(&self) -> ServiceTransport {
        self.service.transport
    }
}

impl fmt::Display for ServiceInstance {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{}", self.instance_name, self.service,)
    }
}

impl fmt::Debug for ServiceInstance {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

/// Describes how a [`ServiceInstance`] can be reached, and supplies service metadata.
pub struct InstanceDetails {
    host: DomainName,
    port: u16,
    txt: TxtRecords,
}

impl InstanceDetails {
    pub fn new(host: DomainName, port: u16) -> Self {
        Self {
            host,
            port,
            txt: TxtRecords::new(),
        }
    }

    /// Parses an [`SRV`] record containing instance details.
    pub fn from_srv(srv: &SRV<'_>) -> Result<Self, Error> {
        Ok(Self {
            host: srv.target().clone(),
            port: srv.port(),
            txt: TxtRecords::new(),
        })
    }

    /// Returns the [`DomainName`] on which the service can be found.
    #[inline]
    pub fn host(&self) -> &DomainName {
        &self.host
    }

    /// Returns the port on which the service is listening.
    #[inline]
    pub fn port(&self) -> u16 {
        self.port
    }

    #[inline]
    pub fn txt_records(&self) -> &TxtRecords {
        &self.txt
    }

    #[inline]
    pub fn txt_records_mut(&mut self) -> &mut TxtRecords {
        &mut self.txt
    }
}

/// List of `key=value` records stored in a DNS-SD TXT record of a service instance.
#[derive(Debug)]
pub struct TxtRecords {
    // keys are lowercased
    // FIXME this should keep the original order
    map: BTreeMap<String, TxtRecord>,
}

#[derive(Debug)]
struct TxtRecord {
    key: String,
    value: Option<Vec<u8>>,
}

impl TxtRecords {
    pub fn new() -> Self {
        Self {
            map: BTreeMap::new(),
        }
    }

    pub fn from_txt(txt: &TXT<'_>) -> Self {
        let mut map = BTreeMap::new();

        for entry in txt.entries() {
            let mut split = entry.splitn(2, |&b| b == b'=');
            let key = split.next().unwrap();
            let key = match String::from_utf8(key.to_vec()) {
                Ok(key) => key,
                Err(e) => {
                    log::debug!("non-ASCII TXT key: {}", e);
                    continue;
                }
            };
            let entry = map.entry(key.to_ascii_lowercase());
            if let Entry::Occupied(_) = entry {
                log::debug!("TXT key '{}' already occupied, ignoring", entry.key());
            }

            match split.next() {
                Some(value) => {
                    entry.or_insert(TxtRecord {
                        key,
                        value: Some(value.to_vec()),
                    });
                }
                None => {
                    // boolean flag
                    entry.or_insert(TxtRecord { key, value: None });
                }
            }
        }

        Self { map }
    }

    /// Adds a TXT record with no value.
    pub fn add_flag(&mut self, key: String) {
        self.map
            .insert(key.to_ascii_lowercase(), TxtRecord { key, value: None });
    }

    /// Returns an iterator over all key-value pairs.
    pub fn iter(&self) -> impl Iterator<Item = (&str, TxtRecordValue<'_>)> {
        self.map.iter().map(|(_, rec)| match &rec.value {
            Some(v) => (rec.key.as_str(), TxtRecordValue::Value(&v)),
            None => (rec.key.as_str(), TxtRecordValue::NoValue),
        })
    }

    pub fn get(&self, key: &str) -> Option<TxtRecordValue<'_>> {
        self.map
            .get(&key.to_ascii_lowercase())
            .map(|rec| match &rec.value {
                Some(v) => TxtRecordValue::Value(v),
                None => TxtRecordValue::NoValue,
            })
    }

    pub fn is_empty(&self) -> bool {
        self.map.is_empty()
    }
}

impl fmt::Display for TxtRecords {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for (i, rec) in self.map.values().enumerate() {
            if i != 0 {
                f.write_str(" ")?;
            }

            f.write_str(&rec.key)?;
            match &rec.value {
                Some(v) => {
                    f.write_str("=")?;
                    v.escape_ascii().fmt(f)?;
                }
                None => {}
            }
        }
        Ok(())
    }
}

pub enum TxtRecordValue<'a> {
    NoValue,
    Value(&'a [u8]),
}

impl<'a> fmt::Debug for TxtRecordValue<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NoValue => f.write_str("-"),
            Self::Value(v) => match std::str::from_utf8(v) {
                Ok(s) => s.fmt(f),
                Err(_) => {
                    for byte in *v {
                        byte.escape_ascii().fmt(f)?;
                    }
                    Ok(())
                }
            },
        }
    }
}