day-part-zeroconf 0.1.0

Native DNS-SD discovery and advertisement for Day apps
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
414
415
416
// Copyright © The Daybrite Project
// SPDX-License-Identifier: MPL-2.0
//! Windows 10+ DNS-SD, using dnsapi.dll (no Bonjour installation).
use super::*;
use crate::windows_names::{escape, instance};
use std::{
    ffi::c_void,
    net::{Ipv4Addr, Ipv6Addr, SocketAddrV6},
    ptr,
    sync::atomic::AtomicU32,
};
use windows_sys::Win32::NetworkManagement::Dns::*;
const PENDING: u32 = 9506;
fn hostname() -> Result<Vec<u16>> {
    use windows_sys::Win32::System::SystemInformation::{
        ComputerNameDnsHostname, GetComputerNameExW,
    };
    let mut text = vec![0u16; 256];
    let mut len = text.len() as u32;
    // SAFETY: the native call receives a writable buffer and its length in UTF-16 units.
    if unsafe { GetComputerNameExW(ComputerNameDnsHostname, text.as_mut_ptr(), &mut len) } == 0 {
        return Err(Error::Unavailable("GetComputerNameExW".into()));
    }
    text.truncate(len as usize);
    text.extend(".local".encode_utf16());
    text.push(0);
    Ok(text)
}
fn wide(s: &str) -> Vec<u16> {
    s.encode_utf16().chain(Some(0)).collect()
}
unsafe fn string(p: *const u16) -> String {
    if p.is_null() {
        return String::new();
    }
    let mut n = 0;
    unsafe {
        while n < 65536 && *p.add(n) != 0 {
            n += 1
        }
        String::from_utf16_lossy(std::slice::from_raw_parts(p, n))
    }
}
fn error(code: u32) -> Error {
    if code == 5 {
        Error::PermissionDenied
    } else {
        Error::Native {
            code: code.into(),
            message: "Windows DNS-SD".into(),
        }
    }
}
struct Context {
    sink: Sink,
    request: Request,
    done: AtomicBool,
    status: AtomicU32,
    registering: AtomicBool,
    diagnostics: Option<mpsc::SyncSender<String>>,
}
unsafe extern "system" fn browse_callback(
    status: u32,
    data: *const c_void,
    records: *const DNS_RECORDW,
) {
    unsafe {
        let c = &*data.cast::<Context>();
        if let Some(tx) = &c.diagnostics {
            let _ = tx.try_send(format!("Windows browse callback status={status}"));
        }
        if status == 1223 || status == 995 {
            if !records.is_null() {
                DnsFree(records.cast(), DnsFreeRecordList)
            }
            c.done.store(true, Ordering::Release);
            return;
        }
        if status != 0 {
            emit(&c.sink, Event::Error(error(status)));
        } else if let Request::Browse(kind, interface) = &c.request {
            let mut p = records;
            while let Some(r) = p.as_ref() {
                if r.wType == DNS_TYPE_PTR {
                    let target = string(r.Data.PTR.pNameHost);
                    if let Some(tx) = &c.diagnostics {
                        let _ =
                            tx.try_send(format!("Windows PTR target={target:?} ttl={}", r.dwTtl));
                    }
                    let target = target.trim_end_matches('.');
                    if kind == ENUMERATION_TYPE {
                        if r.dwTtl > 0
                            && let Ok(t) = validate_type(&target.to_ascii_lowercase())
                        {
                            emit(&c.sink, Event::ServiceType(t));
                        }
                    } else {
                        if let Some(name) = instance(target, kind) {
                            let id = ServiceId {
                                name,
                                service_type: kind.clone(),
                                domain: "local.".into(),
                                interface: *interface,
                            };
                            emit(
                                &c.sink,
                                if r.dwTtl == 0 {
                                    Event::Removed(id)
                                } else {
                                    Event::Found(id)
                                },
                            );
                        }
                    }
                }
                p = r.pNext;
            }
        }
        if !records.is_null() {
            DnsFree(records.cast(), DnsFreeRecordList)
        }
    }
}
unsafe fn service(c: &Context, s: &DNS_SERVICE_INSTANCE) -> Service {
    unsafe {
        let id = match &c.request {
            Request::Resolve(id) => id.clone(),
            Request::Advertise(a) => {
                let full = string(s.pszInstanceName);
                ServiceId {
                    name: instance(&full, &a.service_type).unwrap_or_else(|| a.name.clone()),
                    service_type: a.service_type.clone(),
                    domain: "local.".into(),
                    interface: a.interface,
                }
            }
            _ => unreachable!(),
        };
        let mut addresses = vec![];
        if let Some(ip) = s.ip4Address.as_ref() {
            addresses.push(SocketAddr::new(
                Ipv4Addr::from(ip.to_ne_bytes()).into(),
                s.wPort,
            ))
        }
        if let Some(ip) = s.ip6Address.as_ref() {
            let ip = Ipv6Addr::from(ip.IP6Byte);
            let scope = if ip.is_unicast_link_local() {
                s.dwInterfaceIndex
            } else {
                0
            };
            addresses.push(SocketAddr::V6(SocketAddrV6::new(ip, s.wPort, 0, scope)))
        }
        let mut txt = TxtRecord::default();
        if !s.keys.is_null() && !s.values.is_null() {
            for i in 0..s.dwPropertyCount.min(1024) as usize {
                let key = string(*s.keys.add(i));
                let p = *s.values.add(i);
                txt.0.push((
                    key,
                    if p.is_null() {
                        None
                    } else {
                        Some(string(p).into_bytes())
                    },
                ));
            }
        }
        Service {
            id,
            hostname: string(s.pszHostName),
            port: s.wPort,
            addresses,
            txt,
        }
    }
}
unsafe extern "system" fn resolve_callback(
    status: u32,
    data: *const c_void,
    instance: *const DNS_SERVICE_INSTANCE,
) {
    unsafe {
        let c = &*data.cast::<Context>();
        if status == 0 {
            if let Some(s) = instance.as_ref() {
                emit(&c.sink, Event::Resolved(service(c, s)));
            }
        } else if status != 1223 && status != 995 {
            emit(&c.sink, Event::Error(error(status)));
        }
        if !instance.is_null() {
            DnsServiceFreeInstance(instance)
        }
        c.status.store(status, Ordering::Release);
        c.done.store(true, Ordering::Release);
    }
}
unsafe extern "system" fn register_callback(
    status: u32,
    data: *const c_void,
    instance: *const DNS_SERVICE_INSTANCE,
) {
    unsafe {
        let c = &*data.cast::<Context>();
        if status == 0 && c.registering.load(Ordering::Acquire) {
            if let Some(s) = instance.as_ref() {
                emit(&c.sink, Event::Registered(service(c, s).id));
            }
        } else if status != 0 && status != 1223 && status != 995 {
            emit(&c.sink, Event::Error(error(status)));
        }
        if !instance.is_null() {
            DnsServiceFreeInstance(instance)
        }
        c.status.store(status, Ordering::Release);
        c.done.store(true, Ordering::Release);
    }
}
struct Handle {
    stop: Arc<AtomicBool>,
    worker: Option<thread::JoinHandle<()>>,
}
impl Drop for Handle {
    fn drop(&mut self) {
        self.stop.store(true, Ordering::Release);
        if let Some(w) = self.worker.take() {
            let _ = w.join();
        }
    }
}
pub fn capabilities() -> Capabilities {
    Capabilities {
        native: true,
        browse_all: true,
        binary_txt: false,
        key_only_txt: false,
        interface_selection: true,
    }
}
pub fn start(request: Request, sink: Sink) -> Result<Box<dyn Native>> {
    if let Request::Advertise(a) = &request {
        for (_, v) in &a.txt.0 {
            if v.as_ref()
                .is_none_or(|v| v.contains(&0) || std::str::from_utf8(v).is_err())
            {
                return Err(Error::Unsupported);
            }
        }
    }
    let stop = Arc::new(AtomicBool::new(false));
    let stopping = stop.clone();
    let worker = thread::Builder::new()
        .name("dns-sd".into())
        .spawn(move || unsafe {
            // Context, request, query, cancel handle and instance stay alive through the final callback.
            // In particular, DnsServiceBrowseCancel is asynchronous; its return is not a lifetime fence.
            let (diagnostics, diagnostic_rx) = mpsc::sync_channel(32);
            let mut c = Box::new(Context {
                sink,
                request,
                done: AtomicBool::new(false),
                status: AtomicU32::new(PENDING),
                registering: AtomicBool::new(true),
                diagnostics: trace_enabled().then_some(diagnostics),
            });
            let data = (&mut *c as *mut Context).cast();
            let mut cancel = DNS_SERVICE_CANCEL::default();
            match &c.request {
                Request::Browse(kind, interface) => {
                    // Follow the Windows API's documented query form (no terminal root dot).
                    let query = wide(&format!("{kind}.local"));
                    let req = DNS_SERVICE_BROWSE_REQUEST {
                        Version: 1,
                        InterfaceIndex: *interface,
                        QueryName: query.as_ptr(),
                        Anonymous: DNS_SERVICE_BROWSE_REQUEST_0 {
                            pBrowseCallback: Some(browse_callback),
                        },
                        pQueryContext: data,
                    };
                    let code = DnsServiceBrowse(&req, &mut cancel) as u32;
                    if code != PENDING {
                        emit(&c.sink, Event::Error(error(code)));
                        return;
                    }
                    while !stopping.load(Ordering::Acquire) {
                        for message in diagnostic_rx.try_iter() {
                            trace(message);
                        }
                        thread::sleep(Duration::from_millis(30));
                    }
                    let code = DnsServiceBrowseCancel(&cancel);
                    if code != 0 {
                        emit(&c.sink, Event::Error(error(code as u32)));
                    }
                    while !c.done.load(Ordering::Acquire) {
                        thread::sleep(Duration::from_millis(10));
                    }
                }
                Request::Resolve(id) => {
                    let mut query = wide(&format!(
                        "{}.{}.{}",
                        escape(&id.name),
                        id.service_type,
                        id.domain.trim_end_matches('.')
                    ));
                    let req = DNS_SERVICE_RESOLVE_REQUEST {
                        Version: 1,
                        InterfaceIndex: id.interface,
                        QueryName: query.as_mut_ptr(),
                        pResolveCompletionCallback: Some(resolve_callback),
                        pQueryContext: data,
                    };
                    let code = DnsServiceResolve(&req, &mut cancel) as u32;
                    if code != PENDING {
                        emit(&c.sink, Event::Error(error(code)));
                        return;
                    }
                    let mut cancelling = false;
                    while !c.done.load(Ordering::Acquire) {
                        if stopping.load(Ordering::Acquire) && !cancelling {
                            DnsServiceResolveCancel(&cancel);
                            cancelling = true;
                        }
                        thread::sleep(Duration::from_millis(10));
                    }
                }
                Request::Advertise(a) => {
                    let name = wide(&format!("{}.{}.local", escape(&a.name), a.service_type));
                    let host = match hostname() {
                        Ok(host) => host,
                        Err(e) => {
                            emit(&c.sink, Event::Error(e));
                            return;
                        }
                    };
                    let keys: Vec<_> = a.txt.0.iter().map(|(k, _)| wide(k)).collect();
                    let values: Vec<_> = a
                        .txt
                        .0
                        .iter()
                        .map(|(_, v)| v.as_ref().map(|v| wide(std::str::from_utf8(v).unwrap())))
                        .collect();
                    let kp: Vec<_> = keys.iter().map(|k| k.as_ptr()).collect();
                    let vp: Vec<_> = values
                        .iter()
                        .map(|v| v.as_ref().map_or(ptr::null(), |v| v.as_ptr()))
                        .collect();
                    let instance = DnsServiceConstructInstance(
                        name.as_ptr(),
                        host.as_ptr(),
                        ptr::null(),
                        ptr::null(),
                        a.port,
                        0,
                        0,
                        kp.len() as u32,
                        kp.as_ptr(),
                        vp.as_ptr(),
                    );
                    if instance.is_null() {
                        emit(
                            &c.sink,
                            Event::Error(Error::Unavailable("DnsServiceConstructInstance".into())),
                        );
                        return;
                    }
                    let req = DNS_SERVICE_REGISTER_REQUEST {
                        Version: 1,
                        InterfaceIndex: a.interface,
                        pServiceInstance: instance,
                        pRegisterCompletionCallback: Some(register_callback),
                        pQueryContext: data,
                        ..Default::default()
                    };
                    let code = DnsServiceRegister(&req, &mut cancel);
                    if code != PENDING {
                        DnsServiceFreeInstance(instance);
                        emit(&c.sink, Event::Error(error(code)));
                        return;
                    }
                    let mut cancelling = false;
                    while !c.done.load(Ordering::Acquire) {
                        if stopping.load(Ordering::Acquire) && !cancelling {
                            DnsServiceRegisterCancel(&cancel);
                            cancelling = true;
                        }
                        thread::sleep(Duration::from_millis(10));
                    }
                    if c.status.load(Ordering::Acquire) == 0 {
                        while !stopping.load(Ordering::Acquire) {
                            thread::sleep(Duration::from_millis(30));
                        }
                        c.registering.store(false, Ordering::Release);
                        c.done.store(false, Ordering::Release);
                        let code = DnsServiceDeRegister(&req, &mut cancel);
                        if code == PENDING {
                            while !c.done.load(Ordering::Acquire) {
                                thread::sleep(Duration::from_millis(10));
                            }
                        } else if code != 0 {
                            emit(&c.sink, Event::Error(error(code)));
                        }
                    }
                    DnsServiceFreeInstance(instance);
                }
            }
        })
        .map_err(|e| Error::Unavailable(e.to_string()))?;
    Ok(Box::new(Handle {
        stop,
        worker: Some(worker),
    }))
}