networkframework 0.13.0

Safe Rust bindings for Apple's Network.framework — modern, post-CFNetwork TCP / UDP / TLS / Bonjour networking on macOS
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//! [`Browser`] — Bonjour and application-service discovery via `nw_browser`.

#![allow(clippy::missing_errors_doc)]

use core::ffi::{c_char, c_int, c_void};
use std::ffi::{CStr, CString};
use std::sync::{Arc, Mutex};

use crate::endpoint::Endpoint;
use crate::error::{FrameworkError, NetworkError};
use crate::ffi;
use crate::interface::{InterfaceType, NetworkInterface};
use crate::parameters::ConnectionParameters;
use crate::txt_record::TxtRecord;

/// One Bonjour service that the browser has observed appearing or
/// disappearing on the network.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiscoveredService {
    /// Instance name, e.g. `"Living Room Apple TV"`.
    pub name: String,
    /// Service type with protocol, e.g. `"_airplay._tcp"`.
    pub service_type: String,
    /// DNS domain — usually `"local"`.
    pub domain: String,
}

/// Discovery event delivered to the browser closure.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BrowserEvent {
    Found(DiscoveredService),
    Lost(DiscoveredService),
}

/// Browser lifecycle states reported by `nw_browser_set_state_changed_handler`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BrowserState {
    Invalid,
    Ready,
    Failed,
    Cancelled,
    Waiting,
    Unknown(i32),
}

impl BrowserState {
    pub(crate) const fn from_raw(raw: i32) -> Self {
        match raw {
            0 => Self::Invalid,
            1 => Self::Ready,
            2 => Self::Failed,
            3 => Self::Cancelled,
            4 => Self::Waiting,
            other => Self::Unknown(other),
        }
    }
}

/// Bitflags describing how a browse result changed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BrowseResultChange(u64);

impl BrowseResultChange {
    pub const INVALID: Self = Self(0x00);
    pub const IDENTICAL: Self = Self(0x01);
    pub const RESULT_ADDED: Self = Self(0x02);
    pub const RESULT_REMOVED: Self = Self(0x04);
    pub const INTERFACE_ADDED: Self = Self(0x08);
    pub const INTERFACE_REMOVED: Self = Self(0x10);
    pub const TXT_RECORD_CHANGED: Self = Self(0x20);

    #[must_use]
    pub const fn bits(self) -> u64 {
        self.0
    }

    #[must_use]
    pub const fn contains(self, flag: Self) -> bool {
        (self.0 & flag.0) == flag.0
    }

    #[must_use]
    pub const fn from_bits(bits: u64) -> Self {
        Self(bits)
    }

    #[must_use]
    pub(crate) const fn from_raw(bits: u64) -> Self {
        Self(bits)
    }
}

/// Rich browse result metadata delivered by `nw_browser`.
pub struct BrowseResult {
    handle: *mut c_void,
}

unsafe impl Send for BrowseResult {}
unsafe impl Sync for BrowseResult {}

impl std::fmt::Debug for BrowseResult {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BrowseResult")
            .field("handle", &self.handle)
            .finish()
    }
}

impl BrowseResult {
    /// # Safety
    ///
    /// `handle` must be a valid retained `nw_browse_result_t` that remains
    /// alive for the lifetime of the returned wrapper.
    #[must_use]
    pub const unsafe fn from_raw(handle: *mut c_void) -> Self {
        Self { handle }
    }

    /// Copy the endpoint associated with this browse result.
    #[must_use]
    pub fn endpoint(&self) -> Option<Endpoint> {
        let handle = unsafe { ffi::nw_shim_browse_result_copy_endpoint(self.handle) };
        (!handle.is_null()).then_some(unsafe { Endpoint::from_raw(handle) })
    }

    /// The number of interfaces that currently advertise this result.
    #[must_use]
    pub fn interface_count(&self) -> usize {
        unsafe { ffi::nw_shim_browse_result_get_interfaces_count(self.handle) }
    }

    /// Enumerate the interfaces that currently advertise this result.
    #[must_use]
    pub fn interfaces(&self) -> Vec<NetworkInterface> {
        unsafe extern "C" fn collect(
            name: *const c_char,
            interface_type: c_int,
            index: u32,
            user_info: *mut c_void,
        ) -> c_int {
            if user_info.is_null() {
                return 0;
            }
            let interfaces = unsafe { &mut *user_info.cast::<Vec<NetworkInterface>>() };
            let name = if name.is_null() {
                String::new()
            } else {
                unsafe { CStr::from_ptr(name) }
                    .to_string_lossy()
                    .into_owned()
            };
            interfaces.push(NetworkInterface {
                name,
                interface_type: InterfaceType::from_raw(interface_type),
                index,
            });
            1
        }

        let mut interfaces = Vec::new();
        unsafe {
            ffi::nw_shim_browse_result_enumerate_interfaces(
                self.handle,
                Some(collect),
                std::ptr::addr_of_mut!(interfaces).cast(),
            )
        };
        interfaces
    }

    /// Copy the structured TXT-record object for this result.
    #[must_use]
    pub fn txt_record_object(&self) -> Option<TxtRecord> {
        let handle = unsafe { ffi::nw_shim_browse_result_copy_txt_record_object(self.handle) };
        (!handle.is_null()).then_some(unsafe { TxtRecord::from_raw(handle) })
    }
}

impl Clone for BrowseResult {
    fn clone(&self) -> Self {
        let handle = unsafe { ffi::nw_shim_retain_object(self.handle) };
        Self { handle }
    }
}

impl Drop for BrowseResult {
    fn drop(&mut self) {
        if !self.handle.is_null() {
            unsafe { ffi::nw_shim_release_object(self.handle) };
            self.handle = core::ptr::null_mut();
        }
    }
}

/// A descriptor describing what a browser should look for.
pub struct BrowseDescriptor {
    handle: *mut c_void,
}

unsafe impl Send for BrowseDescriptor {}
unsafe impl Sync for BrowseDescriptor {}

impl std::fmt::Debug for BrowseDescriptor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BrowseDescriptor")
            .field("handle", &self.handle)
            .field("include_txt_record", &self.include_txt_record())
            .finish()
    }
}

fn copied_string(ptr: *mut c_char) -> Option<String> {
    if ptr.is_null() {
        return None;
    }
    let value = unsafe { CStr::from_ptr(ptr) }
        .to_string_lossy()
        .into_owned();
    unsafe { ffi::nw_shim_free_buffer(ptr.cast()) };
    Some(value)
}

fn to_cstring(value: &str, field: &str) -> Result<CString, NetworkError> {
    CString::new(value).map_err(|e| NetworkError::InvalidArgument(format!("{field} NUL byte: {e}")))
}

impl BrowseDescriptor {
    /// Create a Bonjour browse descriptor.
    pub fn bonjour_service(service_type: &str, domain: Option<&str>) -> Result<Self, NetworkError> {
        let service_type = to_cstring(service_type, "service_type")?;
        let domain = match domain {
            Some(domain) => Some(to_cstring(domain, "domain")?),
            None => None,
        };
        let handle = unsafe {
            ffi::nw_shim_browse_descriptor_create_bonjour_service(
                service_type.as_ptr(),
                domain
                    .as_ref()
                    .map_or(core::ptr::null(), |value| value.as_ptr()),
            )
        };
        if handle.is_null() {
            return Err(NetworkError::InvalidArgument(
                "failed to create browse descriptor".into(),
            ));
        }
        Ok(Self { handle })
    }

    /// Create an application-service browse descriptor.
    pub fn application_service(name: &str) -> Result<Self, NetworkError> {
        let name = to_cstring(name, "name")?;
        let handle =
            unsafe { ffi::nw_shim_browse_descriptor_create_application_service(name.as_ptr()) };
        if handle.is_null() {
            return Err(NetworkError::InvalidArgument(
                "failed to create application-service browse descriptor".into(),
            ));
        }
        Ok(Self { handle })
    }

    /// Bonjour service type, if this is a Bonjour browse descriptor.
    #[must_use]
    pub fn bonjour_service_type(&self) -> Option<String> {
        copied_string(unsafe {
            ffi::nw_shim_browse_descriptor_copy_bonjour_service_type(self.handle)
        })
    }

    /// Bonjour service domain, if explicitly configured.
    #[must_use]
    pub fn bonjour_service_domain(&self) -> Option<String> {
        copied_string(unsafe {
            ffi::nw_shim_browse_descriptor_copy_bonjour_service_domain(self.handle)
        })
    }

    /// Enable or disable TXT-record inclusion during browsing.
    pub fn set_include_txt_record(&mut self, include_txt_record: bool) -> &mut Self {
        unsafe {
            ffi::nw_shim_browse_descriptor_set_include_txt_record(
                self.handle,
                i32::from(include_txt_record),
            );
        };
        self
    }

    /// Whether TXT-record inclusion is enabled.
    #[must_use]
    pub fn include_txt_record(&self) -> bool {
        unsafe { ffi::nw_shim_browse_descriptor_get_include_txt_record(self.handle) != 0 }
    }

    /// Application-service name, if this is an application-service descriptor.
    #[must_use]
    pub fn application_service_name(&self) -> Option<String> {
        copied_string(unsafe {
            ffi::nw_shim_browse_descriptor_copy_application_service_name(self.handle)
        })
    }

    #[must_use]
    pub(crate) const fn as_ptr(&self) -> *mut c_void {
        self.handle
    }
}

impl Clone for BrowseDescriptor {
    fn clone(&self) -> Self {
        let handle = unsafe { ffi::nw_shim_retain_object(self.handle) };
        Self { handle }
    }
}

impl Drop for BrowseDescriptor {
    fn drop(&mut self) {
        if !self.handle.is_null() {
            unsafe { ffi::nw_shim_release_object(self.handle) };
            self.handle = core::ptr::null_mut();
        }
    }
}

type Cb = Mutex<Box<dyn FnMut(BrowserEvent) + Send + 'static>>;
type ResultsCb = Mutex<
    Box<
        dyn FnMut(Option<BrowseResult>, Option<BrowseResult>, BrowseResultChange, bool)
            + Send
            + 'static,
    >,
>;
type StateCb = Mutex<Box<dyn FnMut(BrowserState, Option<FrameworkError>) + Send + 'static>>;

/// RAII guard for a running `nw_browser`. Drop to stop receiving
/// discovery callbacks.
#[allow(clippy::type_complexity)]
pub struct Browser {
    handle: *mut c_void,
    _callback: Arc<Cb>,
    state_callback: Option<Arc<StateCb>>,
}

unsafe impl Send for Browser {}
unsafe impl Sync for Browser {}

impl std::fmt::Debug for Browser {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Browser")
            .field("handle", &self.handle)
            .field("has_state_callback", &self.state_callback.is_some())
            .finish_non_exhaustive()
    }
}

impl Browser {
    /// Copy the active browse descriptor.
    #[must_use]
    pub fn browse_descriptor(&self) -> Option<BrowseDescriptor> {
        let handle = unsafe { ffi::nw_shim_browser_copy_browse_descriptor(self.handle) };
        (!handle.is_null()).then_some(BrowseDescriptor { handle })
    }

    /// Copy the browser's current parameters snapshot.
    #[must_use]
    pub fn parameters(&self) -> Option<ConnectionParameters> {
        let handle = unsafe { ffi::nw_shim_browser_copy_parameters(self.handle) };
        (!handle.is_null()).then_some(unsafe { ConnectionParameters::from_raw(handle) })
    }

    #[must_use]
    pub(crate) const fn as_ptr(&self) -> *mut c_void {
        self.handle
    }

    /// Receive browser state updates.
    pub fn set_state_changed_handler<F>(&mut self, callback: F)
    where
        F: FnMut(BrowserState, Option<FrameworkError>) + Send + 'static,
    {
        let callback: Box<dyn FnMut(BrowserState, Option<FrameworkError>) + Send + 'static> =
            Box::new(callback);
        let arc = Arc::new(Mutex::new(callback));
        let raw = Arc::into_raw(arc.clone()).cast::<c_void>().cast_mut();
        unsafe {
            ffi::nw_shim_browser_set_state_changed_handler(
                self.handle,
                Some(state_trampoline),
                raw,
            );
        };
        self.state_callback = Some(arc);
    }
}

impl Drop for Browser {
    fn drop(&mut self) {
        if !self.handle.is_null() {
            unsafe { ffi::nw_shim_browser_stop(self.handle) };
            self.handle = core::ptr::null_mut();
        }
    }
}

/// RAII guard for a running `nw_browser` that delivers rich browse-result objects.
#[allow(clippy::type_complexity)]
pub struct BrowseResultsBrowser {
    handle: *mut c_void,
    _callback: Arc<ResultsCb>,
    state_callback: Option<Arc<StateCb>>,
}

unsafe impl Send for BrowseResultsBrowser {}
unsafe impl Sync for BrowseResultsBrowser {}

impl std::fmt::Debug for BrowseResultsBrowser {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BrowseResultsBrowser")
            .field("handle", &self.handle)
            .field("has_state_callback", &self.state_callback.is_some())
            .finish_non_exhaustive()
    }
}

impl BrowseResultsBrowser {
    /// Copy the active browse descriptor.
    #[must_use]
    pub fn browse_descriptor(&self) -> Option<BrowseDescriptor> {
        let handle = unsafe { ffi::nw_shim_browser_copy_browse_descriptor(self.handle) };
        (!handle.is_null()).then_some(BrowseDescriptor { handle })
    }

    /// Copy the browser's current parameters snapshot.
    #[must_use]
    pub fn parameters(&self) -> Option<ConnectionParameters> {
        let handle = unsafe { ffi::nw_shim_browser_copy_parameters(self.handle) };
        (!handle.is_null()).then_some(unsafe { ConnectionParameters::from_raw(handle) })
    }

    /// Receive browser state updates.
    pub fn set_state_changed_handler<F>(&mut self, callback: F)
    where
        F: FnMut(BrowserState, Option<FrameworkError>) + Send + 'static,
    {
        let callback: Box<dyn FnMut(BrowserState, Option<FrameworkError>) + Send + 'static> =
            Box::new(callback);
        let arc = Arc::new(Mutex::new(callback));
        let raw = Arc::into_raw(arc.clone()).cast::<c_void>().cast_mut();
        unsafe {
            ffi::nw_shim_browser_set_state_changed_handler(
                self.handle,
                Some(state_trampoline),
                raw,
            );
        };
        self.state_callback = Some(arc);
    }
}

impl Drop for BrowseResultsBrowser {
    fn drop(&mut self) {
        if !self.handle.is_null() {
            unsafe { ffi::nw_shim_browser_stop(self.handle) };
            self.handle = core::ptr::null_mut();
        }
    }
}

unsafe extern "C" fn found_trampoline(
    name: *const c_char,
    service_type: *const c_char,
    domain: *const c_char,
    user_info: *mut c_void,
) {
    invoke(user_info, name, service_type, domain, true);
}

unsafe extern "C" fn lost_trampoline(
    name: *const c_char,
    service_type: *const c_char,
    domain: *const c_char,
    user_info: *mut c_void,
) {
    invoke(user_info, name, service_type, domain, false);
}

unsafe extern "C" fn state_trampoline(state: c_int, error: *mut c_void, user_info: *mut c_void) {
    if user_info.is_null() {
        return;
    }
    let callback = unsafe { &*user_info.cast::<StateCb>() };
    let Ok(mut guard) = callback.lock() else {
        return;
    };
    let error = (!error.is_null()).then_some(unsafe { FrameworkError::from_raw(error) });
    guard(BrowserState::from_raw(state), error);
}

unsafe extern "C" fn result_trampoline(
    old_result: *mut c_void,
    new_result: *mut c_void,
    changes: u64,
    batch_complete: c_int,
    user_info: *mut c_void,
) {
    if user_info.is_null() {
        return;
    }
    let callback = unsafe { &*user_info.cast::<ResultsCb>() };
    let Ok(mut guard) = callback.lock() else {
        return;
    };
    let old_result =
        (!old_result.is_null()).then_some(unsafe { BrowseResult::from_raw(old_result) });
    let new_result =
        (!new_result.is_null()).then_some(unsafe { BrowseResult::from_raw(new_result) });
    guard(
        old_result,
        new_result,
        BrowseResultChange::from_bits(changes),
        batch_complete != 0,
    );
}

unsafe fn invoke(
    user_info: *mut c_void,
    name: *const c_char,
    service_type: *const c_char,
    domain: *const c_char,
    is_found: bool,
) {
    if user_info.is_null() {
        return;
    }
    let arc_ptr = user_info.cast::<Cb>();
    let svc = DiscoveredService {
        name: cstr_to_string(name),
        service_type: cstr_to_string(service_type),
        domain: cstr_to_string(domain),
    };
    let event = if is_found {
        BrowserEvent::Found(svc)
    } else {
        BrowserEvent::Lost(svc)
    };
    let Ok(mut guard) = (unsafe { &*arc_ptr }).lock() else {
        return;
    };
    guard(event);
}

unsafe fn cstr_to_string(p: *const c_char) -> String {
    if p.is_null() {
        return String::new();
    }
    unsafe { CStr::from_ptr(p) }.to_string_lossy().into_owned()
}

/// Start browsing with an explicit descriptor and optional parameters.
pub fn start_browser_with_descriptor<F>(
    descriptor: &BrowseDescriptor,
    parameters: Option<&ConnectionParameters>,
    callback: F,
) -> Result<Browser, NetworkError>
where
    F: FnMut(BrowserEvent) + Send + 'static,
{
    let boxed: Box<dyn FnMut(BrowserEvent) + Send + 'static> = Box::new(callback);
    let arc: Arc<Cb> = Arc::new(Mutex::new(boxed));
    let raw = Arc::into_raw(arc.clone()).cast::<c_void>().cast_mut();
    let handle = unsafe {
        ffi::nw_shim_browser_start_with_descriptor(
            descriptor.as_ptr(),
            parameters.map_or(core::ptr::null_mut(), ConnectionParameters::as_ptr),
            found_trampoline,
            lost_trampoline,
            raw,
        )
    };
    if handle.is_null() {
        unsafe { Arc::from_raw(raw.cast::<Cb>()) };
        return Err(NetworkError::ListenFailed);
    }
    Ok(Browser {
        handle,
        _callback: arc,
        state_callback: None,
    })
}

/// Start browsing with rich browse-result objects and change metadata.
pub fn start_browser_results_with_descriptor<F>(
    descriptor: &BrowseDescriptor,
    parameters: Option<&ConnectionParameters>,
    callback: F,
) -> Result<BrowseResultsBrowser, NetworkError>
where
    F: FnMut(Option<BrowseResult>, Option<BrowseResult>, BrowseResultChange, bool) + Send + 'static,
{
    let arc: Arc<ResultsCb> = Arc::new(Mutex::new(Box::new(callback)));
    let raw = Arc::into_raw(arc.clone()).cast::<c_void>().cast_mut();
    let handle = unsafe {
        ffi::nw_shim_browser_start_results_with_descriptor(
            descriptor.as_ptr(),
            parameters.map_or(core::ptr::null_mut(), ConnectionParameters::as_ptr),
            Some(result_trampoline),
            raw,
        )
    };
    if handle.is_null() {
        unsafe { Arc::from_raw(raw.cast::<ResultsCb>()) };
        return Err(NetworkError::ListenFailed);
    }
    Ok(BrowseResultsBrowser {
        handle,
        _callback: arc,
        state_callback: None,
    })
}

/// Start browsing for Bonjour services of `service_type` in `domain`.
pub fn start_browser<F>(
    service_type: &str,
    domain: Option<&str>,
    callback: F,
) -> Result<Browser, NetworkError>
where
    F: FnMut(BrowserEvent) + Send + 'static,
{
    let descriptor = BrowseDescriptor::bonjour_service(service_type, domain)?;
    start_browser_with_descriptor(&descriptor, None, callback)
}

/// RAII guard for a running Bonjour service advertisement. Drop to
/// stop publishing the service to the local network.
pub struct BonjourAdvertiser {
    handle: *mut c_void,
}

unsafe impl Send for BonjourAdvertiser {}
unsafe impl Sync for BonjourAdvertiser {}

impl std::fmt::Debug for BonjourAdvertiser {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("BonjourAdvertiser")
            .field("handle", &self.handle)
            .finish()
    }
}

impl Drop for BonjourAdvertiser {
    fn drop(&mut self) {
        if !self.handle.is_null() {
            unsafe { ffi::nw_shim_bonjour_advertise_stop(self.handle) };
            self.handle = core::ptr::null_mut();
        }
    }
}

/// Publish a Bonjour service on the local network.
pub fn advertise_bonjour_service(
    service_type: &str,
    service_name: &str,
    domain: Option<&str>,
    port: u16,
) -> Result<BonjourAdvertiser, NetworkError> {
    let svc_type = CString::new(service_type)
        .map_err(|e| NetworkError::InvalidArgument(format!("service_type NUL: {e}")))?;
    let svc_name = CString::new(service_name)
        .map_err(|e| NetworkError::InvalidArgument(format!("service_name NUL: {e}")))?;
    let dom = match domain {
        Some(d) => Some(
            CString::new(d)
                .map_err(|e| NetworkError::InvalidArgument(format!("domain NUL: {e}")))?,
        ),
        None => None,
    };
    let mut status: core::ffi::c_int = 0;
    let handle = unsafe {
        ffi::nw_shim_bonjour_advertise_start(
            svc_type.as_ptr(),
            svc_name.as_ptr(),
            dom.as_ref().map_or(core::ptr::null(), |c| c.as_ptr()),
            port,
            &mut status,
        )
    };
    if status != ffi::NW_OK || handle.is_null() {
        return Err(crate::error::from_status(status));
    }
    Ok(BonjourAdvertiser { handle })
}