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
//! Low level interface for interacting with `TXTRecordRef`.
use crate::Result;
use bonjour_sys::{
TXTRecordContainsKey, TXTRecordCreate, TXTRecordDeallocate, TXTRecordGetBytesPtr,
TXTRecordGetCount, TXTRecordGetItemAtIndex, TXTRecordGetLength, TXTRecordGetValuePtr,
TXTRecordRef, TXTRecordRemoveValue, TXTRecordSetValue,
};
use libc::{c_char, c_uchar, c_void};
use std::ffi::CString;
use std::{fmt, mem, ptr};
use super::bonjour_util;
/// Wraps the `ManagedTXTRecordRef` type from the raw Bonjour bindings.
///
/// `zeroconf::TxtRecord` provides the cross-platform bindings for this functionality.
pub struct ManagedTXTRecordRef(TXTRecordRef);
impl ManagedTXTRecordRef {
/// Creates a new empty TXT record
///
/// # Safety
/// This function is unsafe because it calls a C function.
pub unsafe fn new() -> Self {
let mut record: TXTRecordRef = unsafe { mem::zeroed() };
unsafe { TXTRecordCreate(&mut record, 0, ptr::null_mut()) };
Self(record)
}
/// Delegate function for [`TXTRecordGetBytes()`].
///
/// [`TXTRecordGetBytes()`]: https://developer.apple.com/documentation/dnssd/1804717-txtrecordgetbytesptr?language=objc
///
/// # Safety
/// This function is unsafe because it makes no guarantees about the raw pointer arguments
pub unsafe fn get_bytes_ptr(&self) -> *const c_void {
unsafe { TXTRecordGetBytesPtr(&self.0) }
}
/// Delegate function for [`TXTRecordGetLength()`].
///
/// [`TXTRecordGetLength()`]: https://developer.apple.com/documentation/dnssd/1804720-txtrecordgetlength?language=objc
///
/// # Safety
/// This function is unsafe because it makes no guarantees about the raw pointer arguments
pub unsafe fn get_length(&self) -> u16 {
unsafe { TXTRecordGetLength(&self.0) }
}
/// Delegate function for [`TXTRecordRemoveValue()`].
///
/// # Safety
/// This function is unsafe because it makes no guarantees about `key` and `key` is
/// dereferenced. `key` is expected to be a non-null `*const c_char`.
///
/// [`TXTRecordRemoveValue()`]: https://developer.apple.com/documentation/dnssd/1804721-txtrecordremovevalue?language=objc
pub unsafe fn remove_value(&mut self, key: *const c_char) -> Result<()> {
bonjour_util::sys_exec(
|| unsafe { TXTRecordRemoveValue(&mut self.0, key) },
"could not remove TXT record value",
)
}
/// Delegate function for [`TXTRecordSetValue`].
///
/// # Safety
/// This function is unsafe because it makes no guarantees about it's rew pointer arguments
/// that are dereferenced.
///
/// [`TXTRecordSetValue`]: https://developer.apple.com/documentation/dnssd/1804723-txtrecordsetvalue?language=objc
pub unsafe fn set_value(
&mut self,
key: *const c_char,
value_size: u8,
value: *const c_void,
) -> Result<()> {
bonjour_util::sys_exec(
|| unsafe { TXTRecordSetValue(&mut self.0, key, value_size, value) },
"could not set TXT record value",
)
}
/// Delegate function for [`TXTRecordContainsKey`].
///
/// # Safety
/// This function is unsafe because it makes no guarantees about it's rew pointer arguments
/// that are dereferenced.
///
/// [`TXTRecordContainsKey`]: https://developer.apple.com/documentation/dnssd/1804705-txtrecordcontainskey?language=objc
pub unsafe fn contains_key(&self, key: *const c_char) -> bool {
unsafe { TXTRecordContainsKey(self.get_length(), self.get_bytes_ptr(), key) == 1 }
}
/// Delegate function for [`TXTRecordGetCount`].
///
/// [`TXTRecordGetCount`]: https://developer.apple.com/documentation/dnssd/1804706-txtrecordgetcount?language=objc
///
/// # Safety
/// This function is unsafe because it makes no guarantees about it's raw pointer arguments
pub unsafe fn get_count(&self) -> u16 {
unsafe { _get_count(self.get_length(), self.get_bytes_ptr()) }
}
/// Delegate function for [`TXTRecordGetItemAtIndex`].
///
/// # Safety
/// This function is unsafe because it makes no guarantees about it's raw pointer arguments
/// that are dereferenced.
///
/// [`TXTRecordGetItemAtIndex`]: https://developer.apple.com/documentation/dnssd/1804708-txtrecordgetitematindex?language=objc
pub unsafe fn get_item_at_index(
&self,
item_index: u16,
key_buf_len: u16,
key: *mut c_char,
value_len: *mut u8,
value: *mut *const c_void,
) -> Result<()> {
unsafe {
_get_item_at_index(
self.get_length(),
self.get_bytes_ptr(),
item_index,
key_buf_len,
key,
value_len,
value,
)
}
}
/// Delegate function for [`TXTRecordGetValuePtr`].
///
/// # Safety
/// This function is unsafe because it makes no guarantees about it's raw pointer arguments
/// that are dereferenced.
///
/// [`TXTRecordGetValuePtr`]: https://developer.apple.com/documentation/dnssd/1804709-txtrecordgetvalueptr?language=objc
pub unsafe fn get_value_ptr(&self, key: *mut c_char, value_len: &mut u8) -> *const c_void {
unsafe { TXTRecordGetValuePtr(self.get_length(), self.get_bytes_ptr(), key, value_len) }
}
/// Returns a copy of the TXT record.
///
/// # Safety
/// This function is unsafe because it makes no guarantees about the raw pointer arguments
pub unsafe fn clone(&self) -> Self {
unsafe { Self::clone_raw(self.get_bytes_ptr() as *const c_uchar, self.get_length()) }
.expect("could not clone TXT record")
}
pub(crate) unsafe fn clone_raw(raw: *const c_uchar, size: u16) -> Result<Self> {
let chars = c_string!(alloc(size as usize)).into_raw() as *mut c_uchar;
unsafe { ptr::copy(raw, chars, size as usize) };
let chars = unsafe { CString::from_raw(chars as *mut c_char) };
let mut record = unsafe { Self::new() };
for i in 0..unsafe { _get_count(size, chars.as_ptr() as *const c_void) } {
let key = c_string!(alloc(256));
let mut value_len: u8 = 0;
let mut value: *const c_void = ptr::null_mut();
unsafe {
_get_item_at_index(
size,
chars.as_ptr() as *const c_void,
i,
256,
key.as_ptr() as *mut c_char,
&mut value_len,
&mut value,
)
}?;
unsafe { record.set_value(key.as_ptr() as *mut c_char, value_len, value) }?;
}
Ok(record)
}
}
impl Drop for ManagedTXTRecordRef {
fn drop(&mut self) {
unsafe { TXTRecordDeallocate(&mut self.0) };
}
}
impl fmt::Debug for ManagedTXTRecordRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("ManagedTXTRecordRef").finish()
}
}
unsafe impl Send for ManagedTXTRecordRef {}
unsafe fn _get_count(length: u16, data: *const c_void) -> u16 {
unsafe { TXTRecordGetCount(length, data) }
}
unsafe fn _get_item_at_index(
length: u16,
data: *const c_void,
item_index: u16,
key_buf_len: u16,
key: *mut c_char,
value_len: *mut u8,
value: *mut *const c_void,
) -> Result<()> {
bonjour_util::sys_exec(
|| unsafe {
TXTRecordGetItemAtIndex(length, data, item_index, key_buf_len, key, value_len, value)
},
"could get item at index for TXT record",
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ffi::c_str;
#[test]
fn set_value_success() {
let mut record = unsafe { ManagedTXTRecordRef::new() };
let key = c_string!("foo");
let value = c_string!("bar");
let value_size = mem::size_of_val(&value) as u8;
unsafe {
record
.set_value(
key.as_ptr() as *const c_char,
value_size,
value.as_ptr() as *const c_void,
)
.unwrap();
}
let mut value_len: u8 = 0;
let result = unsafe {
c_str::raw_to_str(
record.get_value_ptr(key.as_ptr() as *mut c_char, &mut value_len) as *const c_char,
)
};
assert_eq!(result, "bar");
}
#[test]
fn set_value_null_success() {
let mut record = unsafe { ManagedTXTRecordRef::new() };
let key = c_string!("foo");
let value_size = 0;
unsafe {
record
.set_value(key.as_ptr() as *const c_char, value_size, ptr::null())
.unwrap();
}
let mut value_len: u8 = 0;
let result = unsafe { record.get_value_ptr(key.as_ptr() as *mut c_char, &mut value_len) };
assert!(result.is_null());
}
#[test]
fn remove_value_success() {
let mut record = unsafe { ManagedTXTRecordRef::new() };
let key = c_string!("foo");
let value = c_string!("bar");
let value_size = mem::size_of_val(&value) as u8;
unsafe {
record
.set_value(
key.as_ptr() as *const c_char,
value_size,
value.as_ptr() as *const c_void,
)
.unwrap();
record.remove_value(key.as_ptr() as *const c_char).unwrap();
}
let mut value_len: u8 = 0;
let result = unsafe { record.get_value_ptr(key.as_ptr() as *mut c_char, &mut value_len) };
assert!(result.is_null());
}
#[test]
#[should_panic]
fn remove_value_missing_key_panics() {
let mut record = unsafe { ManagedTXTRecordRef::new() };
let key = c_string!("foo");
unsafe {
record.remove_value(key.as_ptr() as *const c_char).unwrap();
}
}
#[test]
fn contains_key_success() {
let mut record = unsafe { ManagedTXTRecordRef::new() };
let key = c_string!("foo");
let value = c_string!("bar");
let value_size = mem::size_of_val(&value) as u8;
unsafe {
record
.set_value(
key.as_ptr() as *const c_char,
value_size,
value.as_ptr() as *const c_void,
)
.unwrap();
}
let no_val = c_string!("baz");
unsafe {
assert!(record.contains_key(key.as_ptr() as *const c_char));
assert!(!record.contains_key(no_val.as_ptr() as *const c_char));
}
}
#[test]
fn get_count_success() {
let mut record = unsafe { ManagedTXTRecordRef::new() };
let key = c_string!("foo");
let value = c_string!("bar");
let value_size = mem::size_of_val(&value) as u8;
unsafe {
record
.set_value(
key.as_ptr() as *const c_char,
value_size,
value.as_ptr() as *const c_void,
)
.unwrap();
}
assert_eq!(unsafe { record.get_count() }, 1);
}
#[test]
fn get_item_at_index() {
let mut record = unsafe { ManagedTXTRecordRef::new() };
let key = c_string!("foo");
let value = c_string!("bar");
let value_size = mem::size_of_val(&value) as u8;
unsafe {
record
.set_value(
key.as_ptr() as *const c_char,
value_size,
value.as_ptr() as *const c_void,
)
.unwrap();
}
let key = c_string!(alloc(256));
let mut value_len: u8 = 0;
let mut value: *const c_void = ptr::null_mut();
unsafe {
record
.get_item_at_index(
0,
256,
key.as_ptr() as *mut c_char,
&mut value_len,
&mut value,
)
.unwrap();
let key = c_str::raw_to_str(key.as_ptr() as *const c_char);
let value = c_str::raw_to_str(value as *const c_char);
assert_eq!(key, "foo");
assert_eq!(value, "bar");
}
}
}