hightower-mdns 0.1.3

A naive Rust implementation of mDNS (Multicast DNS) for advertising and discovering hostnames on a local network
Documentation
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use std::net::Ipv4Addr;

/// Build a simple mDNS A record response packet
///
/// Creates an mDNS response packet advertising the given hostname and IP address
/// with a default TTL of 120 seconds.
///
/// # Arguments
///
/// * `name` - The hostname (without domain suffix)
/// * `domain` - The domain to use
/// * `ip` - The IPv4 address to advertise
///
/// # Returns
///
/// A byte vector containing the complete mDNS response packet
pub fn build_mdns_packet(name: &str, domain: &str, ip: Ipv4Addr) -> Vec<u8> {
    build_mdns_packet_with_ttl(name, domain, ip, 120)
}

/// Build a goodbye packet (TTL = 0)
///
/// Creates an mDNS goodbye packet to announce that a service is shutting down.
/// This is the same as a regular response but with TTL set to 0.
///
/// # Arguments
///
/// * `name` - The hostname (without domain suffix)
/// * `domain` - The domain to use
/// * `ip` - The IPv4 address being removed
///
/// # Returns
///
/// A byte vector containing the complete mDNS goodbye packet
pub fn build_goodbye_packet(name: &str, domain: &str, ip: Ipv4Addr) -> Vec<u8> {
    build_mdns_packet_with_ttl(name, domain, ip, 0)
}

/// Build an mDNS A record response packet with custom TTL
fn build_mdns_packet_with_ttl(name: &str, domain: &str, ip: Ipv4Addr, ttl: u32) -> Vec<u8> {
    let mut packet = Vec::new();

    // DNS Header (12 bytes)
    packet.extend_from_slice(&[0x00, 0x00]); // Transaction ID
    packet.extend_from_slice(&[0x84, 0x00]); // Flags: Response, Authoritative
    packet.extend_from_slice(&[0x00, 0x00]); // Questions: 0
    packet.extend_from_slice(&[0x00, 0x01]); // Answer RRs: 1
    packet.extend_from_slice(&[0x00, 0x00]); // Authority RRs: 0
    packet.extend_from_slice(&[0x00, 0x00]); // Additional RRs: 0

    // QNAME: encode the hostname
    let full_name = format!("{}.{}", name, domain);
    for label in full_name.split('.') {
        packet.push(label.len() as u8);
        packet.extend_from_slice(label.as_bytes());
    }
    packet.push(0x00); // End of QNAME

    // TYPE: A (0x0001)
    packet.extend_from_slice(&[0x00, 0x01]);

    // CLASS: IN with cache-flush bit (0x8001)
    packet.extend_from_slice(&[0x80, 0x01]);

    // TTL: specified seconds
    packet.extend_from_slice(&ttl.to_be_bytes());

    // RDLENGTH: 4 (for IPv4 address)
    packet.extend_from_slice(&[0x00, 0x04]);

    // RDATA: IP address
    packet.extend_from_slice(&ip.octets());

    packet
}

/// Build an mDNS query packet
///
/// Creates an mDNS query packet to search for a specific hostname on the network.
///
/// # Arguments
///
/// * `name` - The hostname to query (without domain suffix)
/// * `domain` - The domain to use
///
/// # Returns
///
/// A byte vector containing the complete mDNS query packet
pub fn build_mdns_query(name: &str, domain: &str) -> Vec<u8> {
    let mut packet = Vec::new();

    // DNS Header (12 bytes)
    packet.extend_from_slice(&[0x00, 0x00]); // Transaction ID
    packet.extend_from_slice(&[0x00, 0x00]); // Flags: Query
    packet.extend_from_slice(&[0x00, 0x01]); // Questions: 1
    packet.extend_from_slice(&[0x00, 0x00]); // Answer RRs: 0
    packet.extend_from_slice(&[0x00, 0x00]); // Authority RRs: 0
    packet.extend_from_slice(&[0x00, 0x00]); // Additional RRs: 0

    // QNAME: encode the hostname
    let full_name = format!("{}.{}", name, domain);
    for label in full_name.split('.') {
        packet.push(label.len() as u8);
        packet.extend_from_slice(label.as_bytes());
    }
    packet.push(0x00); // End of QNAME

    // QTYPE: A (0x0001)
    packet.extend_from_slice(&[0x00, 0x01]);

    // QCLASS: IN (0x0001)
    packet.extend_from_slice(&[0x00, 0x01]);

    packet
}

/// Parse an mDNS query packet and extract the queried name
///
/// Parses an incoming mDNS query packet and extracts the hostname being queried.
///
/// # Arguments
///
/// * `packet` - The raw mDNS packet data
///
/// # Returns
///
/// The queried hostname as a string, or `None` if the packet is invalid or not a query
pub fn parse_mdns_query(packet: &[u8]) -> Option<String> {
    if packet.len() < 12 {
        return None;
    }

    // Check if it's a query (QR bit should be 0)
    if packet[2] & 0x80 != 0 {
        return None;
    }

    // Get question count
    let question_count = u16::from_be_bytes([packet[4], packet[5]]);
    if question_count == 0 {
        return None;
    }

    // Parse the first question's QNAME
    let mut pos = 12;
    let mut name_parts = Vec::new();

    while pos < packet.len() {
        let len = packet[pos] as usize;
        if len == 0 {
            break;
        }

        pos += 1;
        if pos + len > packet.len() {
            return None;
        }

        if let Ok(label) = std::str::from_utf8(&packet[pos..pos + len]) {
            name_parts.push(label.to_string());
        } else {
            return None;
        }

        pos += len;
    }

    if name_parts.is_empty() {
        None
    } else {
        Some(name_parts.join("."))
    }
}

/// Parse an mDNS response/announcement packet and extract hostname and IP address
///
/// Parses an incoming mDNS response or announcement packet and extracts the hostname
/// and IPv4 address from the first A record.
///
/// # Arguments
///
/// * `packet` - The raw mDNS packet data
///
/// # Returns
///
/// A tuple of (hostname, IP address), or `None` if the packet is invalid, not a response,
/// or doesn't contain a valid A record
pub fn parse_mdns_response(packet: &[u8]) -> Option<(String, Ipv4Addr)> {
    if packet.len() < 12 {
        return None;
    }

    // Check if it's a response (QR bit should be 1)
    if packet[2] & 0x80 == 0 {
        return None;
    }

    // Get answer count
    let answer_count = u16::from_be_bytes([packet[6], packet[7]]);
    if answer_count == 0 {
        return None;
    }

    // Skip questions section
    let mut pos = 12;
    let question_count = u16::from_be_bytes([packet[4], packet[5]]);

    for _ in 0..question_count {
        while pos < packet.len() && packet[pos] != 0 {
            let len = packet[pos] as usize;
            pos += 1 + len;
        }
        pos += 1;
        pos += 4; // Skip QTYPE and QCLASS
    }

    if pos >= packet.len() {
        return None;
    }

    // Parse hostname from first answer
    let mut name_parts = Vec::new();

    while pos < packet.len() {
        let len = packet[pos] as usize;

        // Handle DNS compression pointer
        if len >= 0xC0 {
            if pos + 1 >= packet.len() {
                return None;
            }
            let offset = (u16::from_be_bytes([packet[pos] & 0x3F, packet[pos + 1]])) as usize;
            let mut offset_pos = offset;
            while offset_pos < packet.len() {
                let offset_len = packet[offset_pos] as usize;
                if offset_len == 0 || offset_len >= 0xC0 {
                    break;
                }
                offset_pos += 1;
                if offset_pos + offset_len > packet.len() {
                    return None;
                }
                if let Ok(label) = std::str::from_utf8(&packet[offset_pos..offset_pos + offset_len]) {
                    name_parts.push(label.to_string());
                }
                offset_pos += offset_len;
            }
            pos += 2; // Skip the compression pointer
            break;
        }

        if len == 0 {
            pos += 1; // Skip the null terminator
            break;
        }

        pos += 1;
        if pos + len > packet.len() {
            return None;
        }

        if let Ok(label) = std::str::from_utf8(&packet[pos..pos + len]) {
            name_parts.push(label.to_string());
        } else {
            return None;
        }

        pos += len;
    }

    if name_parts.is_empty() {
        return None;
    }

    // Now we're at the TYPE field
    if pos + 10 > packet.len() {
        return None;
    }

    // Read TYPE
    let rtype = u16::from_be_bytes([packet[pos], packet[pos + 1]]);
    pos += 2;

    // Read CLASS
    pos += 2;

    // Read TTL
    pos += 4;

    // Read RDLENGTH
    let rdlength = u16::from_be_bytes([packet[pos], packet[pos + 1]]);
    pos += 2;

    // Check if it's an A record and has correct length
    if rtype != 1 || rdlength != 4 {
        return None;
    }

    // Read the IP address
    if pos + 4 > packet.len() {
        return None;
    }

    let ip = Ipv4Addr::new(packet[pos], packet[pos + 1], packet[pos + 2], packet[pos + 3]);

    Some((name_parts.join("."), ip))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_build_mdns_packet() {
        let ip = Ipv4Addr::new(192, 168, 1, 50);
        let packet = build_mdns_packet("testhost", "local", ip);

        // Verify header
        assert_eq!(packet[2], 0x84); // Response flag
        assert_eq!(packet[7], 0x01); // 1 answer

        // Verify the name is encoded
        assert!(packet.len() > 12); // Header + data

        // Verify IP address is in the packet
        let ip_octets = ip.octets();
        let packet_len = packet.len();
        assert_eq!(&packet[packet_len - 4..], &ip_octets);
    }

    #[test]
    fn test_parse_mdns_query() {
        // Build a simple query packet for "testhost.local"
        let mut packet = vec![
            0x00, 0x00, // Transaction ID
            0x00, 0x00, // Flags: Query
            0x00, 0x01, // Questions: 1
            0x00, 0x00, // Answer RRs: 0
            0x00, 0x00, // Authority RRs: 0
            0x00, 0x00, // Additional RRs: 0
        ];

        // QNAME: testhost.local
        packet.push(8); // length of "testhost"
        packet.extend_from_slice(b"testhost");
        packet.push(5); // length of "local"
        packet.extend_from_slice(b"local");
        packet.push(0); // End of QNAME

        let parsed = parse_mdns_query(&packet);
        assert_eq!(parsed, Some("testhost.local".to_string()));
    }

    #[test]
    fn test_parse_mdns_response() {
        // Response packets should return None
        let packet = vec![
            0x00, 0x00, // Transaction ID
            0x84, 0x00, // Flags: Response
            0x00, 0x00, // Questions: 0
            0x00, 0x01, // Answer RRs: 1
            0x00, 0x00, // Authority RRs: 0
            0x00, 0x00, // Additional RRs: 0
        ];

        let parsed = parse_mdns_query(&packet);
        assert_eq!(parsed, None);
    }

    #[test]
    fn test_build_mdns_query() {
        let packet = build_mdns_query("testhost", "local");

        // Verify header
        assert_eq!(packet[2], 0x00); // Query flag
        assert_eq!(packet[3], 0x00); // Query flag
        assert_eq!(packet[5], 0x01); // 1 question

        // Verify it contains the name
        assert!(packet.len() > 12);
    }

    #[test]
    fn test_build_goodbye_packet() {
        let ip = Ipv4Addr::new(192, 168, 1, 50);
        let packet = build_goodbye_packet("testhost", "local", ip);

        // Verify header
        assert_eq!(packet[2], 0x84); // Response flag
        assert_eq!(packet[7], 0x01); // 1 answer

        // Find TTL position (after name encoding)
        // Name is "testhost.local":
        //   1 byte length (8) + 8 bytes "testhost"
        //   1 byte length (5) + 5 bytes "local"
        //   1 byte null = 16 bytes total
        // After header (12 bytes) + name (16 bytes) + TYPE (2) + CLASS (2) = 32 bytes
        // TTL starts at position 32
        let ttl_pos = 32;
        let ttl = u32::from_be_bytes([
            packet[ttl_pos],
            packet[ttl_pos + 1],
            packet[ttl_pos + 2],
            packet[ttl_pos + 3],
        ]);
        assert_eq!(ttl, 0); // TTL should be 0 for goodbye packet

        // Verify IP address is still in the packet
        let ip_octets = ip.octets();
        let packet_len = packet.len();
        assert_eq!(&packet[packet_len - 4..], &ip_octets);
    }
}