systemconfiguration-rs 0.5.3

Safe Rust bindings for Apple's SystemConfiguration framework via a Swift bridge 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
use std::{
    ffi::c_void,
    net::SocketAddr,
    sync::{Arc, Mutex},
};

use crate::{bridge, error::Result, ffi, SystemConfigurationError};

#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
/// Wraps `SCNetworkReachabilityFlags`.
pub struct ReachabilityFlags(
    /// Wraps the raw `SCNetworkReachabilityFlags` bitfield.
    pub u32,
);

impl ReachabilityFlags {
    /// Wraps a helper on `SCNetworkReachabilityFlags`.
    pub fn bits(self) -> u32 {
        self.0
    }

    /// Wraps a helper on `SCNetworkReachabilityFlags`.
    pub fn is_transient_connection(self) -> bool {
        self.0 & (1 << 0) != 0
    }

    /// Wraps a helper on `SCNetworkReachabilityFlags`.
    pub fn is_reachable(self) -> bool {
        self.0 & (1 << 1) != 0
    }

    /// Wraps a helper on `SCNetworkReachabilityFlags`.
    pub fn needs_connection(self) -> bool {
        self.0 & (1 << 2) != 0
    }

    /// Wraps a helper on `SCNetworkReachabilityFlags`.
    pub fn is_connection_on_traffic(self) -> bool {
        self.0 & (1 << 3) != 0
    }

    /// Wraps a helper on `SCNetworkReachabilityFlags`.
    pub fn needs_intervention(self) -> bool {
        self.0 & (1 << 4) != 0
    }

    /// Wraps a helper on `SCNetworkReachabilityFlags`.
    pub fn is_connection_on_demand(self) -> bool {
        self.0 & (1 << 5) != 0
    }

    /// Wraps a helper on `SCNetworkReachabilityFlags`.
    pub fn is_local_address(self) -> bool {
        self.0 & (1 << 16) != 0
    }

    /// Wraps a helper on `SCNetworkReachabilityFlags`.
    pub fn is_direct(self) -> bool {
        self.0 & (1 << 17) != 0
    }

    /// Wraps a helper on `SCNetworkReachabilityFlags`.
    pub fn is_wwan(self) -> bool {
        self.0 & (1 << 18) != 0
    }
}

impl std::fmt::Display for ReachabilityFlags {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut labels = Vec::new();
        if self.is_transient_connection() {
            labels.push("transient");
        }
        if self.is_reachable() {
            labels.push("reachable");
        }
        if self.needs_connection() {
            labels.push("needs-connection");
        }
        if self.is_connection_on_traffic() {
            labels.push("on-traffic");
        }
        if self.needs_intervention() {
            labels.push("needs-intervention");
        }
        if self.is_connection_on_demand() {
            labels.push("on-demand");
        }
        if self.is_local_address() {
            labels.push("local-address");
        }
        if self.is_direct() {
            labels.push("direct");
        }
        if self.is_wwan() {
            labels.push("wwan");
        }
        if labels.is_empty() {
            write!(f, "0x{:x}", self.bits())
        } else {
            write!(f, "{} (0x{:x})", labels.join("|"), self.bits())
        }
    }
}

struct LocalCallbackState {
    callback: Box<dyn FnMut(ReachabilityFlags)>,
}

struct SendCallbackState {
    callback: Box<dyn FnMut(ReachabilityFlags) + Send>,
}

enum RegisteredCallback {
    Local {
        _state: Box<LocalCallbackState>,
    },
    Send {
        _state: Arc<Mutex<SendCallbackState>>,
    },
}

unsafe extern "C" fn reachability_callback_local(flags: u32, info: *mut c_void) {
    if info.is_null() {
        return;
    }

    let state = unsafe { &mut *info.cast::<LocalCallbackState>() };
    (state.callback)(ReachabilityFlags(flags));
}

unsafe extern "C" fn reachability_callback_send(flags: u32, info: *mut c_void) {
    if info.is_null() {
        return;
    }

    let mutex = unsafe { &*info.cast::<Mutex<SendCallbackState>>() };
    if let Ok(mut state) = mutex.lock() {
        (state.callback)(ReachabilityFlags(flags));
    }
}

/// Wraps `SCNetworkReachabilityRef`.
pub struct Reachability {
    raw: bridge::OwnedHandle,
    callback: Option<RegisteredCallback>,
    scheduled_with_current_run_loop: bool,
    dispatch_queue_active: bool,
}

/// Alias for the `SCNetworkReachabilityRef` wrapper.
pub type NetworkReachability = Reachability;

impl Reachability {
    /// Wraps `SCReachabilityGetTypeID`.
    pub fn type_id() -> u64 {
        unsafe { ffi::network_reachability::sc_reachability_get_type_id() }
    }

    /// Wraps `SCReachabilityCreateWithName`.
    pub fn with_name(name: &str) -> Result<Self> {
        let name = bridge::cstring(name, "sc_reachability_create_with_name")?;
        let raw =
            unsafe { ffi::network_reachability::sc_reachability_create_with_name(name.as_ptr()) };
        let raw = bridge::owned_handle_or_last("sc_reachability_create_with_name", raw)?;
        Ok(Self {
            raw,
            callback: None,
            scheduled_with_current_run_loop: false,
            dispatch_queue_active: false,
        })
    }

    /// Wraps `SCReachabilityCreateWithAddress`.
    pub fn with_address(address: SocketAddr) -> Result<Self> {
        let storage = socket_addr_to_bytes(address);
        let raw = unsafe {
            ffi::network_reachability::sc_reachability_create_with_address(
                storage.as_ptr(),
                isize::try_from(storage.len()).expect("socket address length exceeded isize"),
            )
        };
        let raw = bridge::owned_handle_or_last("sc_reachability_create_with_address", raw)?;
        Ok(Self {
            raw,
            callback: None,
            scheduled_with_current_run_loop: false,
            dispatch_queue_active: false,
        })
    }

    /// Wraps `SCReachabilityCreateWithAddressPair`.
    pub fn with_address_pair(
        local_address: Option<SocketAddr>,
        remote_address: Option<SocketAddr>,
    ) -> Result<Self> {
        let local = local_address.map(socket_addr_to_bytes);
        let remote = remote_address.map(socket_addr_to_bytes);
        let raw = unsafe {
            ffi::network_reachability::sc_reachability_create_with_address_pair(
                local.as_ref().map_or(std::ptr::null(), Vec::as_ptr),
                local.as_ref().map_or(0, |value| {
                    isize::try_from(value.len()).expect("socket address length exceeded isize")
                }),
                remote.as_ref().map_or(std::ptr::null(), Vec::as_ptr),
                remote.as_ref().map_or(0, |value| {
                    isize::try_from(value.len()).expect("socket address length exceeded isize")
                }),
            )
        };
        let raw = bridge::owned_handle_or_last("sc_reachability_create_with_address_pair", raw)?;
        Ok(Self {
            raw,
            callback: None,
            scheduled_with_current_run_loop: false,
            dispatch_queue_active: false,
        })
    }

    /// Wraps `SCReachabilityGetFlags`.
    pub fn flags(&self) -> Result<ReachabilityFlags> {
        let mut flags = 0_u32;
        let ok = unsafe {
            ffi::network_reachability::sc_reachability_get_flags(self.raw.as_ptr(), &mut flags)
        };
        bridge::bool_result("sc_reachability_get_flags", ok)?;
        Ok(ReachabilityFlags(flags))
    }

    /// Wraps a helper on `SCNetworkReachabilityRef`.
    pub fn set_callback<F>(&mut self, callback: F) -> Result<()>
    where
        F: FnMut(ReachabilityFlags) + 'static,
    {
        if self.dispatch_queue_active {
            return Err(SystemConfigurationError::null(
                "sc_reachability_set_callback",
                "dispatch queues require callbacks registered via Reachability::set_callback_send; clear the dispatch queue first",
            ));
        }

        let mut callback = Box::new(LocalCallbackState {
            callback: Box::new(callback),
        });
        self.set_registered_callback(
            Some(reachability_callback_local),
            std::ptr::from_mut(&mut *callback).cast::<c_void>(),
            Some(RegisteredCallback::Local { _state: callback }),
        )
    }

    /// Wraps a helper on `SCNetworkReachabilityRef`.
    pub fn set_callback_send<F>(&mut self, callback: F) -> Result<()>
    where
        F: FnMut(ReachabilityFlags) + Send + 'static,
    {
        let callback = Arc::new(Mutex::new(SendCallbackState {
            callback: Box::new(callback),
        }));
        self.set_registered_callback(
            Some(reachability_callback_send),
            Arc::as_ptr(&callback).cast_mut().cast::<c_void>(),
            Some(RegisteredCallback::Send { _state: callback }),
        )
    }

    /// Wraps a helper on `SCNetworkReachabilityRef`.
    pub fn clear_callback(&mut self) -> Result<()> {
        if self.dispatch_queue_active {
            self.clear_dispatch_queue()?;
        }
        self.set_registered_callback(None, std::ptr::null_mut(), None)
    }

    /// Wraps `SCReachabilityScheduleWithRunLoopCurrent`.
    pub fn schedule_with_run_loop_current(&mut self) -> Result<()> {
        let ok = unsafe {
            ffi::network_reachability::sc_reachability_schedule_with_run_loop_current(
                self.raw.as_ptr(),
            )
        };
        bridge::bool_result("sc_reachability_schedule_with_run_loop_current", ok)?;
        self.scheduled_with_current_run_loop = true;
        Ok(())
    }

    /// Wraps `SCReachabilityUnscheduleFromRunLoopCurrent`.
    pub fn unschedule_from_run_loop_current(&mut self) -> Result<()> {
        let ok = unsafe {
            ffi::network_reachability::sc_reachability_unschedule_from_run_loop_current(
                self.raw.as_ptr(),
            )
        };
        bridge::bool_result("sc_reachability_unschedule_from_run_loop_current", ok)?;
        self.scheduled_with_current_run_loop = false;
        Ok(())
    }

    /// Wraps `SCReachabilitySetDispatchQueueGlobal`.
    pub fn set_dispatch_queue_global(&mut self) -> Result<()> {
        if matches!(self.callback, Some(RegisteredCallback::Local { .. })) {
            return Err(SystemConfigurationError::null(
                "sc_reachability_set_dispatch_queue_global",
                "dispatch queues require callbacks registered via Reachability::set_callback_send",
            ));
        }

        let ok = unsafe {
            ffi::network_reachability::sc_reachability_set_dispatch_queue_global(self.raw.as_ptr())
        };
        bridge::bool_result("sc_reachability_set_dispatch_queue_global", ok)?;
        self.dispatch_queue_active = true;
        Ok(())
    }

    /// Wraps `SCReachabilityClearDispatchQueue`.
    pub fn clear_dispatch_queue(&mut self) -> Result<()> {
        let ok = unsafe {
            ffi::network_reachability::sc_reachability_clear_dispatch_queue(self.raw.as_ptr())
        };
        bridge::bool_result("sc_reachability_clear_dispatch_queue", ok)?;
        self.dispatch_queue_active = false;
        Ok(())
    }

    fn set_registered_callback(
        &mut self,
        callback: ffi::network_reachability::ReachabilityCallback,
        info: *mut c_void,
        registered: Option<RegisteredCallback>,
    ) -> Result<()> {
        let ok = unsafe {
            ffi::network_reachability::sc_reachability_set_callback(
                self.raw.as_ptr(),
                callback,
                info,
            )
        };
        bridge::bool_result("sc_reachability_set_callback", ok)?;
        self.callback = registered;
        Ok(())
    }
}

impl Drop for Reachability {
    fn drop(&mut self) {
        if self.dispatch_queue_active {
            let _ = unsafe {
                ffi::network_reachability::sc_reachability_clear_dispatch_queue(self.raw.as_ptr())
            };
        }
        if self.scheduled_with_current_run_loop {
            let _ = unsafe {
                ffi::network_reachability::sc_reachability_unschedule_from_run_loop_current(
                    self.raw.as_ptr(),
                )
            };
        }
        if self.callback.is_some() {
            let _ = unsafe {
                ffi::network_reachability::sc_reachability_set_callback(
                    self.raw.as_ptr(),
                    None,
                    std::ptr::null_mut(),
                )
            };
        }
    }
}

fn socket_addr_to_bytes(address: SocketAddr) -> Vec<u8> {
    match address {
        SocketAddr::V4(address) => {
            let mut storage: libc::sockaddr_in = unsafe { std::mem::zeroed() };
            storage.sin_len = u8::try_from(std::mem::size_of::<libc::sockaddr_in>())
                .expect("sockaddr_in length exceeds u8");
            storage.sin_family = u8::try_from(libc::AF_INET).expect("AF_INET exceeds u8");
            storage.sin_port = address.port().to_be();
            storage.sin_addr = libc::in_addr {
                s_addr: u32::from_ne_bytes(address.ip().octets()),
            };
            unsafe {
                std::slice::from_raw_parts(
                    std::ptr::from_ref(&storage).cast::<u8>(),
                    std::mem::size_of::<libc::sockaddr_in>(),
                )
                .to_vec()
            }
        }
        SocketAddr::V6(address) => {
            let mut storage: libc::sockaddr_in6 = unsafe { std::mem::zeroed() };
            storage.sin6_len = u8::try_from(std::mem::size_of::<libc::sockaddr_in6>())
                .expect("sockaddr_in6 length exceeds u8");
            storage.sin6_family = u8::try_from(libc::AF_INET6).expect("AF_INET6 exceeds u8");
            storage.sin6_port = address.port().to_be();
            storage.sin6_flowinfo = address.flowinfo();
            storage.sin6_scope_id = address.scope_id();
            storage.sin6_addr = libc::in6_addr {
                s6_addr: address.ip().octets(),
            };
            unsafe {
                std::slice::from_raw_parts(
                    std::ptr::from_ref(&storage).cast::<u8>(),
                    std::mem::size_of::<libc::sockaddr_in6>(),
                )
                .to_vec()
            }
        }
    }
}