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
#![allow(non_snake_case)]
use crate::raw::{ICWWiFiClient, INSString, ICWInterface, INSArray, NSString, INSError, INSSet, ICWNetwork, ICWChannel};
/// "A wrapper around the entire Wi-Fi subsystem that you use to access interfaces and set up event
/// notifications."
pub struct CWWiFiClient {
raw: crate::raw::CWWiFiClient
}
impl CWWiFiClient {
/// "The shared Wi-Fi client object."
pub fn sharedWiFiClient() -> CWWiFiClient {
unsafe { CWWiFiClient { raw: crate::raw::CWWiFiClient::sharedWiFiClient() } }
}
/// "Initializes a Wi-Fi client object."
/// You probably want to use [CWWiFiClient::sharedWiFiClient].
pub fn init() -> CWWiFiClient {
unsafe {
CWWiFiClient {
raw: crate::raw::CWWiFiClient(
crate::raw::CWWiFiClient::init(
&crate::raw::CWWiFiClient::alloc()
)
)
}
}
}
/// "Returns the default Wi-Fi interface."
pub fn interface(&self) -> CWInterface {
unsafe { CWInterface { raw: self.raw.interface() } }
}
/// "Returns the Wi-Fi interface with the given name."
/// TODO: Test what happens when an invalid interface name is passed.
pub fn interface_with_name(&self, name: &str) -> CWInterface {
let out_name = &format!("{}\0", name);
unsafe { CWInterface { raw: self.raw.interfaceWithName_(crate::raw::NSString(crate::raw::NSString::stringWithUTF8String(out_name.as_bytes()))) } }
}
/// "Returns all available Wi-Fi interfaces."
pub fn interfaces(&self) -> Vec<CWInterface> {
let mut final_interfaces = vec![];
unsafe {
let interfaces = self.raw.interfaces();
let arr_len = <crate::raw::NSArray as INSArray<crate::raw::CWInterface>>::count(&interfaces);
for i in 0..arr_len {
final_interfaces.push(CWInterface { raw: crate::raw::CWInterface(<crate::raw::NSArray as INSArray<crate::raw::CWInterface>>::objectAtIndex_(&interfaces, i)) } );
}
}
return final_interfaces;
}
/// "Returns the list of the names of available Wi-Fi interfaces."
pub fn interfaceNames(&self) -> Vec<String> {
let mut final_interface_names = vec![];
unsafe {
let interface_names = crate::raw::CWWiFiClient::interfaceNames();
let arr_len = <crate::raw::NSArray as INSArray<crate::raw::NSString>>::count(&interface_names);
for i in 0..arr_len {
let nsstring = crate::raw::NSString(<crate::raw::NSArray as INSArray<crate::raw::NSString>>::objectAtIndex_(&interface_names, i));
let cstring = std::ffi::CStr::from_ptr(nsstring.UTF8String());
let new_utf8 = cstring.to_str().unwrap();
let safe_utf8 = String::from(new_utf8);
final_interface_names.push(safe_utf8);
}
}
return final_interface_names;
}
}
/// "Encapsulates an IEEE 802.11 interface."
pub struct CWInterface {
raw: crate::raw::CWInterface
}
impl CWInterface {
/// "Scans for networks."
///
/// Scanning more than once every 10 seconds leads to an error.
pub fn scanForNetworksWithName(&self, name: Option<String>) -> Result<Vec<CWNetwork>, ()> {
let mut final_networks = vec![];
unsafe {
if let Some(name) = name {
let modified_name = &format!("{}\0", name);
let network_name = crate::raw::NSString(crate::raw::NSString::stringWithUTF8String(modified_name.as_bytes()));
let potential_error = &mut crate::raw::NSError::alloc() as *mut crate::raw::NSError;
let networks = self.raw.scanForNetworksWithName_error_(network_name, potential_error);
if potential_error.as_ref().unwrap().code() != 0 {
// TODO: proper error codes!
println!("ERROR CODE #{}", potential_error.as_ref().unwrap().code());
return Err(());
}
let networks_nsarr = <crate::raw::NSSet as INSSet<crate::raw::CWNetwork>>::allObjects(&networks);
let arr_len = <crate::raw::NSArray as INSArray<crate::raw::CWNetwork>>::count(&networks_nsarr);
for i in 0..arr_len {
let instance = crate::raw::CWNetwork(<crate::raw::NSArray as INSArray<crate::raw::CWNetwork>>::objectAtIndex_(&networks_nsarr, i));
final_networks.push(CWNetwork { raw: instance });
}
}
else {
let potential_error = &mut crate::raw::NSError::alloc() as *mut crate::raw::NSError;
let networks = self.raw.scanForNetworks_error_(potential_error);
if potential_error.as_ref().unwrap().code() != 0 {
// TODO: proper error codes!
println!("ERROR CODE #{}", potential_error.as_ref().unwrap().code());
return Err(());
}
let networks_nsarr = <crate::raw::NSSet as INSSet<crate::raw::CWNetwork>>::allObjects(&networks);
let arr_len = <crate::raw::NSArray as INSArray<crate::raw::CWNetwork>>::count(&networks_nsarr);
for i in 0..arr_len {
let instance = crate::raw::CWNetwork(<crate::raw::NSArray as INSArray<crate::raw::CWNetwork>>::objectAtIndex_(&networks_nsarr, i));
final_networks.push(CWNetwork { raw: instance });
}
}
}
return Ok(final_networks);
}
/// "Disassociates from the current network."
pub fn disassociate(&self) {
unsafe { self.raw.disassociate() }
}
}
/// "Encapsulates an IEEE 802.11 network, providing read-only accessors to various properties of the
/// network."
pub struct CWNetwork {
raw: crate::raw::CWNetwork
}
impl CWNetwork {
/// "Method for determining which security types a network supports."
pub fn supportsSecurity(&self, security: CWSecurity) -> bool {
unsafe { return self.raw.supportsSecurity_(security as i64); }
}
/// "Method for determining which PHY modes a network supports."
pub fn supportsPHYMode(&self, mode: CWPHYMode) -> bool {
unsafe { return self.raw.supportsPHYMode_(mode as i64); }
}
/// "The beacon interval (ms) for the network."
pub fn beaconInterval(&self) -> i64 {
unsafe { return self.raw.beaconInterval(); }
}
/// "The basic service set identifier (BSSID) for the network."
///
/// This value is not typically returned. Getting it to work is finicky. Try googling 'bssid
/// CoreWLAN macOS' and pray.
///
/// Further notes:
///
/// Afaik if the following are true this should return a valid value:
/// - CoreLocation::CLLocationManager::requestAlwaysAuthorization()
/// - Executable is signed
///
/// I've been unable to test/reproduce this.
pub fn bssid(&self) -> Option<String> {
unsafe {
// SAFTEY: This block checks if a string or null pointer was returned manually. It's not
// clean, it's not the best soulution. It works. This shouldn't cause issues, but if it
// can, or you have a better soulution, please open an issue immediately.
// attempt to grab the bssid string
let nsstring = self.raw.bssid();
// get a pointer to our theoretical string
let raw_val = &nsstring as *const NSString;
// convert it to a pointer to a u8 (this is the danger)
let to_u8 = raw_val as *const u8;
// check if the value stored there = 0
if to_u8.as_ref().unwrap() == &0 {
// then there's not a string!
return None;
}
// otherwise there is a string, ignore the pointer nonsense and carry on
let cstring = std::ffi::CStr::from_ptr(nsstring.UTF8String());
return Some(cstring.to_str().unwrap().clone().to_string());
}
}
/// "The country code (ISO/IEC 3166-1:1997) for the network."
///
/// Requesting this information also requires location services permissions. See
/// [CWNetwork::bssid] for how you might get this information.
pub fn countryCode(&self) -> Option<String> {
unsafe {
// SAFTEY: This block checks if a string or null pointer was returned manually. It's not
// clean, it's not the best soulution. It works. This shouldn't cause issues, but if it
// can, or you have a better soulution, please open an issue immediately.
let nsstring = self.raw.countryCode();
// get a pointer to our theoretical string
let raw_val = &nsstring as *const NSString;
// convert it to a pointer to a u8 (this is the danger)
let to_u8 = raw_val as *const u8;
// check if the value stored there = 0
if to_u8.as_ref().unwrap() == &0 {
// then there's not a string!
return None;
}
let cstring = std::ffi::CStr::from_ptr(nsstring.UTF8String());
let new_utf8 = cstring.to_str().unwrap();
return Some(String::from(new_utf8));
}
}
/// "The network is an IBSS network."
///
/// IBSS networks are essentially peer-to-peer networks.
pub fn ibss(&self) -> bool {
unsafe { return self.raw.ibss(); }
}
/// "The aggregate noise measurement (dBm) for the network."
pub fn noiseMeasurement(&self) -> i64 {
unsafe { return self.raw.noiseMeasurement(); }
}
/// "The aggregate received signal strength indication (RSSI) measurement (dBm) for the
/// network."
pub fn rssiValue(&self) -> i64 {
unsafe { return self.raw.rssiValue(); }
}
/// "The service set identifier (SSID) for the network."
pub fn ssid(&self) -> String {
unsafe {
let nsstring = self.raw.ssid();
let cstring = std::ffi::CStr::from_ptr(nsstring.UTF8String());
let new_utf8 = cstring.to_str().unwrap();
// SAFTEY: There is no promise on the lifetime of the returned NSString, so we create a
// new string using the data while we know it's good.
return String::from(new_utf8);
}
}
/// "The channel for the network."
pub fn wlanChannel(&self) -> CWChannel {
let (number, width, band);
unsafe {
let raw_channel = self.raw.wlanChannel();
number = raw_channel.channelNumber();
// The OS has "Unknown" values for both these enums, and so should never return an
// invalid value to us.
width = CWChannelWidth::try_from_i64(raw_channel.channelWidth()).unwrap();
band = CWChannelBand::try_from_i64(raw_channel.channelBand()).unwrap();
}
return CWChannel { number, width, band };
}
}
#[repr(i64)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
/// "CoreWLAN security types."
pub enum CWSecurity {
/// "Open System authentication."
None = 0,
/// "WEP security."
WEP = 1,
/// "WPA Personal authentication."
WPAPersonal = 2,
/// "WPA/WPA2 Personal authentication."
WPAPersonalMixed = 3,
/// "WPA2 Personal authentication."
WPA2Personal = 4,
/// "Personal authentication."
Personal = 5,
/// "Dynamic WEP security."
DynamicWEP = 6,
/// "WPA Enterprise authentication."
WPAEnterprise = 7,
/// "WPA/WPA2 Enterprise authentication."
WPAEnterpriseMixed = 8,
/// "WPA2 Enterprise authentication."
WPA2Enterprise = 9,
/// "Enterprise authentication."
Enterprise = 10,
/// "WPA3 Personal authentication."
WPA3Personal = 11,
/// "WPA3 Enterprise authentication."
WPA3Enterprise = 12,
/// "WPA3 Transition (WPA3/WPA2 Personal) authentication."
WPA3Transition = 13,
/// "Unknown security type."
Unknown = 9223372036854775807,
}
#[repr(i64)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
/// "CoreWLAN physical layer modes."
pub enum CWPHYMode {
/// "No specified mode."
None = 0,
/// "IEEE 802.11a PHY."
M11a = 1,
/// "IEEE 802.11b PHY."
M11b = 2,
/// "IEEE 802.11g PHY."
M11g = 3,
/// "IEEE 802.11n PHY."
M11n = 4,
/// "IEEE 802.11ac PHY."
M11ac = 5,
// Labeled weird in Apple's documentation, maybe normal, maybe not?
M11ax = 6,
}
pub struct CWChannel {
// ???
pub number: i64,
/// Specifies the width of this channel in MHz
pub width: CWChannelWidth,
/// Specifies the 2.4 or 5GHz band
pub band: CWChannelBand,
}
#[repr(i64)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
/// "The channel width."
pub enum CWChannelWidth {
/// "Unknown channel width."
Unknown = 0,
/// "20MHz channel width."
W20MHz = 1,
/// "40MHz channel width."
W40MHz = 2,
/// "80MHz channel width."
W80MHz = 3,
/// "160MHz channel width."
W160MHz = 4,
}
impl CWChannelWidth {
pub fn try_from_i64(data: i64) -> Option<CWChannelWidth> {
match data {
x if x == Self::Unknown as i64 => Some(Self::Unknown),
x if x == Self::W20MHz as i64 => Some(Self::W20MHz),
x if x == Self::W40MHz as i64 => Some(Self::W40MHz),
x if x == Self::W80MHz as i64 => Some(Self::W80MHz),
x if x == Self::W160MHz as i64 => Some(Self::W160MHz),
_ => None
}
}
}
#[repr(i64)]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
/// "The channel band."
pub enum CWChannelBand {
/// "Unknown channel band."
Unknown = 0,
/// "2.4GHz channel band."
B2GHz = 1,
/// "5GHz channel band."
B5GHz = 2,
}
impl CWChannelBand {
pub fn try_from_i64(data: i64) -> Option<CWChannelBand> {
match data {
x if x == Self::Unknown as i64 => Some(Self::Unknown),
x if x == Self::B2GHz as i64 => Some(Self::B2GHz),
x if x == Self::B5GHz as i64 => Some(Self::B5GHz),
_ => None
}
}
}