networkframework 0.9.1

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
#![allow(clippy::missing_errors_doc, clippy::semicolon_if_nothing_returned)]

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

use crate::{
    endpoint::Endpoint,
    error::NetworkError,
    ffi,
    interface::{InterfaceType, NetworkInterface},
    interface_support::{network_interface_from_handle, network_interface_from_parts},
    parameters::ConnectionParameters,
    protocol::ProtocolOptions,
};

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

unsafe fn copied_optional_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)
}

/// Connection service class applied to new paths.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ServiceClass {
    BestEffort,
    Background,
    InteractiveVideo,
    InteractiveVoice,
    ResponsiveData,
    Signaling,
    Unknown(i32),
}

impl ServiceClass {
    const fn from_raw(raw: i32) -> Self {
        match raw {
            0 => Self::BestEffort,
            1 => Self::Background,
            2 => Self::InteractiveVideo,
            3 => Self::InteractiveVoice,
            4 => Self::ResponsiveData,
            5 => Self::Signaling,
            other => Self::Unknown(other),
        }
    }

    const fn as_raw(self) -> i32 {
        match self {
            Self::BestEffort => 0,
            Self::Background => 1,
            Self::InteractiveVideo => 2,
            Self::InteractiveVoice => 3,
            Self::ResponsiveData => 4,
            Self::Signaling => 5,
            Self::Unknown(raw) => raw,
        }
    }
}

/// Multipath policy applied to new connections.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MultipathService {
    Disabled,
    Handover,
    Interactive,
    Aggregate,
    Unknown(i32),
}

impl MultipathService {
    const fn from_raw(raw: i32) -> Self {
        match raw {
            0 => Self::Disabled,
            1 => Self::Handover,
            2 => Self::Interactive,
            3 => Self::Aggregate,
            other => Self::Unknown(other),
        }
    }

    const fn as_raw(self) -> i32 {
        match self {
            Self::Disabled => 0,
            Self::Handover => 1,
            Self::Interactive => 2,
            Self::Aggregate => 3,
            Self::Unknown(raw) => raw,
        }
    }
}

/// Policy controlling whether expired DNS answers may be used.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExpiredDnsBehavior {
    Default,
    Allow,
    Prohibit,
    Persistent,
    Unknown(i32),
}

impl ExpiredDnsBehavior {
    const fn from_raw(raw: i32) -> Self {
        match raw {
            0 => Self::Default,
            1 => Self::Allow,
            2 => Self::Prohibit,
            3 => Self::Persistent,
            other => Self::Unknown(other),
        }
    }

    const fn as_raw(self) -> i32 {
        match self {
            Self::Default => 0,
            Self::Allow => 1,
            Self::Prohibit => 2,
            Self::Persistent => 3,
            Self::Unknown(raw) => raw,
        }
    }
}

/// Mutable wrapper around `nw_protocol_stack_t`.
pub struct ProtocolStack {
    handle: *mut c_void,
}

unsafe impl Send for ProtocolStack {}
unsafe impl Sync for ProtocolStack {}

impl ProtocolStack {
    #[must_use]
    pub(crate) const unsafe fn from_raw(handle: *mut c_void) -> Self {
        Self { handle }
    }

    /// Remove every application protocol from the stack.
    pub fn clear_application_protocols(&mut self) -> &mut Self {
        unsafe { ffi::nw_shim_protocol_stack_clear_application_protocols(self.handle) };
        self
    }

    /// Copy the current application-protocol list.
    #[must_use]
    pub fn application_protocols(&self) -> Vec<ProtocolOptions> {
        let mut count = 0_usize;
        let items = unsafe {
            ffi::nw_shim_protocol_stack_copy_application_protocols(self.handle, &mut count)
        };
        if items.is_null() || count == 0 {
            return Vec::new();
        }
        let slice = unsafe { std::slice::from_raw_parts(items, count) };
        let protocols = slice
            .iter()
            .filter_map(|handle| (!handle.is_null()).then_some(unsafe { ProtocolOptions::from_raw(*handle) }))
            .collect();
        unsafe { ffi::nw_shim_free_buffer(items.cast()) };
        protocols
    }

    /// Copy the transport protocol, if one is configured.
    #[must_use]
    pub fn transport_protocol(&self) -> Option<ProtocolOptions> {
        let handle = unsafe { ffi::nw_shim_protocol_stack_copy_transport_protocol(self.handle) };
        (!handle.is_null()).then_some(unsafe { ProtocolOptions::from_raw(handle) })
    }

    /// Replace the current transport protocol.
    pub fn set_transport_protocol(&mut self, protocol: &ProtocolOptions) -> &mut Self {
        unsafe { ffi::nw_shim_protocol_stack_set_transport_protocol(self.handle, protocol.as_ptr()) };
        self
    }

    /// Copy the internet protocol, if one is configured.
    #[must_use]
    pub fn internet_protocol(&self) -> Option<ProtocolOptions> {
        let handle = unsafe { ffi::nw_shim_protocol_stack_copy_internet_protocol(self.handle) };
        (!handle.is_null()).then_some(unsafe { ProtocolOptions::from_raw(handle) })
    }
}

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

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

impl ConnectionParameters {
    /// Whether the framework should prefer a direct path before trying configured proxies.
    #[must_use]
    pub fn prefer_no_proxy(&self) -> bool {
        unsafe { ffi::nw_shim_parameters_get_prefer_no_proxy(self.as_ptr()) != 0 }
    }

    /// Require a specific interface for path evaluation, or clear the requirement.
    pub fn require_interface(
        &mut self,
        interface: Option<&NetworkInterface>,
    ) -> Result<&mut Self, NetworkError> {
        match interface {
            Some(interface) => {
                let name = to_cstring(&interface.name, "interface.name")?;
                unsafe {
                    ffi::nw_shim_parameters_require_interface(
                        self.as_ptr(),
                        name.as_ptr(),
                        interface.interface_type.as_raw(),
                        interface.index,
                    )
                };
            }
            None => unsafe {
                ffi::nw_shim_parameters_require_interface(self.as_ptr(), core::ptr::null(), 0, 0)
            },
        }
        Ok(self)
    }

    /// Copy the currently required interface, if any.
    #[must_use]
    pub fn required_interface(&self) -> Option<NetworkInterface> {
        let mut name = core::ptr::null_mut();
        let mut interface_type: c_int = 0;
        let mut index = 0_u32;
        let found = unsafe {
            ffi::nw_shim_parameters_copy_required_interface(
                self.as_ptr(),
                &mut name,
                &mut interface_type,
                &mut index,
            )
        };
        if found == 0 {
            return None;
        }
        let name = unsafe { copied_optional_string(name) }.unwrap_or_default();
        Some(network_interface_from_parts(name, interface_type, index))
    }

    /// Add an interface to the prohibited-interface list.
    pub fn prohibit_interface(
        &mut self,
        interface: &NetworkInterface,
    ) -> Result<&mut Self, NetworkError> {
        let name = to_cstring(&interface.name, "interface.name")?;
        unsafe {
            ffi::nw_shim_parameters_prohibit_interface(
                self.as_ptr(),
                name.as_ptr(),
                interface.interface_type.as_raw(),
                interface.index,
            )
        };
        Ok(self)
    }

    /// Remove every prohibited interface.
    pub fn clear_prohibited_interfaces(&mut self) -> &mut Self {
        unsafe { ffi::nw_shim_parameters_clear_prohibited_interfaces(self.as_ptr()) };
        self
    }

    /// Copy the prohibited-interface list.
    #[must_use]
    pub fn prohibited_interfaces(&self) -> Vec<NetworkInterface> {
        let mut count = 0_usize;
        let items = unsafe { ffi::nw_shim_parameters_copy_prohibited_interfaces(self.as_ptr(), &mut count) };
        if items.is_null() || count == 0 {
            return Vec::new();
        }
        let slice = unsafe { std::slice::from_raw_parts(items, count) };
        let interfaces = slice
            .iter()
            .filter_map(|handle| unsafe { network_interface_from_handle(*handle) })
            .collect();
        unsafe { ffi::nw_shim_free_buffer(items.cast()) };
        interfaces
    }

    /// Add an interface type to the prohibited-interface-type list.
    pub fn prohibit_interface_type(&mut self, interface_type: InterfaceType) -> &mut Self {
        unsafe {
            ffi::nw_shim_parameters_prohibit_interface_type(self.as_ptr(), interface_type.as_raw())
        };
        self
    }

    /// Remove every prohibited interface type.
    pub fn clear_prohibited_interface_types(&mut self) -> &mut Self {
        unsafe { ffi::nw_shim_parameters_clear_prohibited_interface_types(self.as_ptr()) };
        self
    }

    /// Copy the prohibited interface types.
    #[must_use]
    pub fn prohibited_interface_types(&self) -> Vec<InterfaceType> {
        let mut count = 0_usize;
        let items = unsafe {
            ffi::nw_shim_parameters_copy_prohibited_interface_types(self.as_ptr(), &mut count)
        };
        if items.is_null() || count == 0 {
            return Vec::new();
        }
        let slice = unsafe { std::slice::from_raw_parts(items, count) };
        let types = slice.iter().copied().map(InterfaceType::from_raw).collect();
        unsafe { ffi::nw_shim_free_buffer(items.cast()) };
        types
    }

    /// Reuse the local address when binding.
    pub fn set_reuse_local_address(&mut self, reuse_local_address: bool) -> &mut Self {
        unsafe {
            ffi::nw_shim_parameters_set_reuse_local_address(
                self.as_ptr(),
                c_int::from(reuse_local_address),
            )
        };
        self
    }

    /// Whether the local address may be reused when binding.
    #[must_use]
    pub fn reuse_local_address(&self) -> bool {
        unsafe { ffi::nw_shim_parameters_get_reuse_local_address(self.as_ptr()) != 0 }
    }

    /// Set or clear the local endpoint used for new connections.
    pub fn set_local_endpoint(&mut self, local_endpoint: Option<&Endpoint>) -> &mut Self {
        unsafe {
            ffi::nw_shim_parameters_set_local_endpoint(
                self.as_ptr(),
                local_endpoint.map_or(core::ptr::null_mut(), Endpoint::as_ptr),
            )
        };
        self
    }

    /// Copy the currently configured local endpoint.
    #[must_use]
    pub fn local_endpoint(&self) -> Option<Endpoint> {
        let handle = unsafe { ffi::nw_shim_parameters_copy_local_endpoint(self.as_ptr()) };
        (!handle.is_null()).then_some(unsafe { Endpoint::from_raw(handle) })
    }

    /// Allow or disallow peer-to-peer transports such as AWDL.
    pub fn set_include_peer_to_peer(&mut self, include_peer_to_peer: bool) -> &mut Self {
        unsafe {
            ffi::nw_shim_parameters_set_include_peer_to_peer(
                self.as_ptr(),
                c_int::from(include_peer_to_peer),
            )
        };
        self
    }

    /// Whether peer-to-peer transports are allowed.
    #[must_use]
    pub fn include_peer_to_peer(&self) -> bool {
        unsafe { ffi::nw_shim_parameters_get_include_peer_to_peer(self.as_ptr()) != 0 }
    }

    /// Enable or disable TCP Fast Open.
    pub fn set_fast_open_enabled(&mut self, fast_open_enabled: bool) -> &mut Self {
        unsafe {
            ffi::nw_shim_parameters_set_fast_open_enabled(
                self.as_ptr(),
                c_int::from(fast_open_enabled),
            )
        };
        self
    }

    /// Whether TCP Fast Open is enabled.
    #[must_use]
    pub fn fast_open_enabled(&self) -> bool {
        unsafe { ffi::nw_shim_parameters_get_fast_open_enabled(self.as_ptr()) != 0 }
    }

    /// Set the service class for new flows.
    pub fn set_service_class(&mut self, service_class: ServiceClass) -> &mut Self {
        unsafe { ffi::nw_shim_parameters_set_service_class(self.as_ptr(), service_class.as_raw()) };
        self
    }

    /// Current service class.
    #[must_use]
    pub fn service_class(&self) -> ServiceClass {
        ServiceClass::from_raw(unsafe { ffi::nw_shim_parameters_get_service_class(self.as_ptr()) })
    }

    /// Set the multipath policy.
    pub fn set_multipath_service(&mut self, multipath_service: MultipathService) -> &mut Self {
        unsafe {
            ffi::nw_shim_parameters_set_multipath_service(
                self.as_ptr(),
                multipath_service.as_raw(),
            )
        };
        self
    }

    /// Current multipath policy.
    #[must_use]
    pub fn multipath_service(&self) -> MultipathService {
        MultipathService::from_raw(unsafe {
            ffi::nw_shim_parameters_get_multipath_service(self.as_ptr())
        })
    }

    /// Copy the default protocol stack for these parameters.
    #[must_use]
    pub fn default_protocol_stack(&self) -> Option<ProtocolStack> {
        let handle = unsafe { ffi::nw_shim_parameters_copy_default_protocol_stack(self.as_ptr()) };
        (!handle.is_null()).then_some(unsafe { ProtocolStack::from_raw(handle) })
    }

    /// Restrict new paths to local interfaces only.
    pub fn set_local_only(&mut self, local_only: bool) -> &mut Self {
        unsafe { ffi::nw_shim_parameters_set_local_only(self.as_ptr(), c_int::from(local_only)) };
        self
    }

    /// Whether new paths are restricted to local interfaces only.
    #[must_use]
    pub fn local_only(&self) -> bool {
        unsafe { ffi::nw_shim_parameters_get_local_only(self.as_ptr()) != 0 }
    }

    /// Control whether expired DNS answers may be reused.
    pub fn set_expired_dns_behavior(&mut self, expired_dns_behavior: ExpiredDnsBehavior) -> &mut Self {
        unsafe {
            ffi::nw_shim_parameters_set_expired_dns_behavior(
                self.as_ptr(),
                expired_dns_behavior.as_raw(),
            )
        };
        self
    }

    /// Current expired-DNS policy.
    #[must_use]
    pub fn expired_dns_behavior(&self) -> ExpiredDnsBehavior {
        ExpiredDnsBehavior::from_raw(unsafe {
            ffi::nw_shim_parameters_get_expired_dns_behavior(self.as_ptr())
        })
    }

    /// Require DNSSEC validation for endpoint resolution.
    pub fn set_requires_dnssec_validation(&mut self, requires_dnssec_validation: bool) -> &mut Self {
        unsafe {
            ffi::nw_shim_parameters_set_requires_dnssec_validation(
                self.as_ptr(),
                c_int::from(requires_dnssec_validation),
            )
        };
        self
    }

    /// Whether DNSSEC validation is required.
    #[must_use]
    pub fn requires_dnssec_validation(&self) -> bool {
        unsafe { ffi::nw_shim_parameters_requires_dnssec_validation(self.as_ptr()) != 0 }
    }
}