async-dnssd 0.2.0

Asynchronous wrapper for DNS-SD C libraries
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
use std::os::raw::{c_int,c_void};
use std::cell::UnsafeCell;
use std::ptr::null_mut;
use std::rc::Rc;

use cstr;
use error::Error;
use ffi;

type FFIResult<R> = Result<R, Error>;

struct InnerDNSService(ffi::DNSServiceRef);

impl Drop for InnerDNSService {
	fn drop(&mut self) {
		unsafe {
			ffi::DNSServiceRefDeallocate(self.0);
		}
	}
}

impl InnerDNSService {
	fn fd(&mut self) -> c_int {
		unsafe { ffi::DNSServiceRefSockFD(self.0) }
	}

	fn process_result(&mut self) -> FFIResult<()> {
		Error::from(unsafe {
			ffi::DNSServiceProcessResult(self.0)
		})
	}

	fn enumerate_domains(
		flags: ffi::DNSServiceFlags,
		interface_index: u32,
		callback: ffi::DNSServiceDomainEnumReply,
		context: *mut c_void
	) -> FFIResult<InnerDNSService> {
		let mut sd_ref : ffi::DNSServiceRef = null_mut();
		Error::from(unsafe {
			ffi::DNSServiceEnumerateDomains(&mut sd_ref, flags, interface_index, callback, context)
		})?;
		Ok(InnerDNSService(sd_ref))
	}

	fn register(
		flags: ffi::DNSServiceFlags,
		interface_index: u32,
		name: &cstr::NullableCStr,
		reg_type: &cstr::CStr,
		domain: &cstr::NullableCStr,
		host: &cstr::NullableCStr,
		port: u16,
		txt: &[u8],
		callback: ffi::DNSServiceRegisterReply,
		context: *mut c_void
	) -> FFIResult<InnerDNSService> {
		let txt_len = txt.len();
		assert!(txt_len < (1 << 16));
		let txt_len = txt_len as u16;
		let txt_record = txt.as_ptr();

		let mut sd_ref : ffi::DNSServiceRef = null_mut();
		Error::from(unsafe {
			ffi::DNSServiceRegister(
				&mut sd_ref,
				flags,
				interface_index,
				name.as_ptr(),
				reg_type.as_ptr(),
				domain.as_ptr(),
				host.as_ptr(),
				port,
				txt_len,
				txt_record,
				callback,
				context
			)
		})?;
		Ok(InnerDNSService(sd_ref))
	}

	fn browse(
		flags: ffi::DNSServiceFlags,
		interface_index: u32,
		reg_type: &cstr::CStr,
		domain: &cstr::NullableCStr,
		callback: ffi::DNSServiceBrowseReply,
		context: *mut c_void
	) -> FFIResult<InnerDNSService> {
		let mut sd_ref : ffi::DNSServiceRef = null_mut();
		Error::from(unsafe {
			ffi::DNSServiceBrowse(
				&mut sd_ref,
				flags,
				interface_index,
				reg_type.as_ptr(),
				domain.as_ptr(),
				callback,
				context
			)
		})?;
		Ok(InnerDNSService(sd_ref))
	}

	fn resolve(
		flags: ffi::DNSServiceFlags,
		interface_index: u32,
		name: &cstr::CStr,
		reg_type: &cstr::CStr,
		domain: &cstr::CStr,
		callback: ffi::DNSServiceResolveReply,
		context: *mut c_void
	) -> FFIResult<InnerDNSService> {
		let mut sd_ref : ffi::DNSServiceRef = null_mut();
		Error::from(unsafe {
			ffi::DNSServiceResolve(
				&mut sd_ref,
				flags,
				interface_index,
				name.as_ptr(),
				reg_type.as_ptr(),
				domain.as_ptr(),
				callback,
				context
			)
		})?;
		Ok(InnerDNSService(sd_ref))
	}

	fn create_connection() -> FFIResult<InnerDNSService> {
		let mut sd_ref : ffi::DNSServiceRef = null_mut();
		Error::from(unsafe {
			ffi::DNSServiceCreateConnection(&mut sd_ref)
		})?;
		Ok(InnerDNSService(sd_ref))
	}

	fn query_record(
		flags: ffi::DNSServiceFlags,
		interface_index: u32,
		fullname: &cstr::CStr,
		rr_type: u16,
		rr_class: u16,
		callback: ffi::DNSServiceQueryRecordReply,
		context: *mut c_void
	) -> FFIResult<InnerDNSService> {
		let mut sd_ref : ffi::DNSServiceRef = null_mut();
		Error::from(unsafe {
			ffi::DNSServiceQueryRecord(
				&mut sd_ref,
				flags,
				interface_index,
				fullname.as_ptr(),
				rr_type,
				rr_class,
				callback,
				context
			)
		})?;
		Ok(InnerDNSService(sd_ref))
	}
}

#[derive(Clone)]
pub struct DNSService(Rc<UnsafeCell<InnerDNSService>>);

impl DNSService {
	fn get(&self) -> &mut InnerDNSService {
		use std::mem::transmute;
		// Rc means it cannot be shared across threads.
		// we need to make sure our callbacks don't ever come back
		// here, otherwise there is no way for "re-entrance".
		let r = self.0.get();
		unsafe { transmute::<*mut InnerDNSService, &mut InnerDNSService>(r) }
	}

	fn new(s: FFIResult<InnerDNSService>) -> FFIResult<DNSService> {
		s.map(|s| DNSService(Rc::new(UnsafeCell::new(s))))
	}

	pub fn fd(&self) -> c_int {
		self.get().fd()
	}

	pub fn process_result(&self) -> FFIResult<()> {
		self.get().process_result()
	}

	pub fn enumerate_domains(
		flags: ffi::DNSServiceFlags,
		interface_index: u32,
		callback: ffi::DNSServiceDomainEnumReply,
		context: *mut c_void
	) -> FFIResult<DNSService> {
		Self::new(
			InnerDNSService::enumerate_domains(flags, interface_index, callback, context)
		)
	}

	pub fn register(
		flags: ffi::DNSServiceFlags,
		interface_index: u32,
		name: &cstr::NullableCStr,
		reg_type: &cstr::CStr,
		domain: &cstr::NullableCStr,
		host: &cstr::NullableCStr,
		port: u16,
		txt: &[u8],
		callback: ffi::DNSServiceRegisterReply,
		context: *mut c_void
	) -> FFIResult<DNSService> {
		Self::new(
			InnerDNSService::register(flags, interface_index, name, reg_type, domain, host, port, txt, callback, context)
		)
	}

	pub fn add_record(
		&self,
		flags: ffi::DNSServiceFlags,
		rr_type: u16,
		rdata: &[u8],
		ttl: u32
	) -> FFIResult<DNSRecord> {
		Ok(DNSRecord(
			InnerDNSRecord::add_record(self, flags, rr_type, rdata, ttl)?
		))
	}

	pub fn get_default_txt_record(&self) -> DNSRecord {
		const RR_TYPE_TXT : u16 = 16;
		DNSRecord(
			InnerDNSRecord(self.clone(), null_mut(), RR_TYPE_TXT)
		)
	}

	pub fn browse(
		flags: ffi::DNSServiceFlags,
		interface_index: u32,
		reg_type: &cstr::CStr,
		domain: &cstr::NullableCStr,
		callback: ffi::DNSServiceBrowseReply,
		context: *mut c_void
	) -> FFIResult<DNSService> {
		Self::new(
			InnerDNSService::browse(flags, interface_index, reg_type, domain, callback, context)
		)
	}

	pub fn resolve(
		flags: ffi::DNSServiceFlags,
		interface_index: u32,
		name: &cstr::CStr,
		reg_type: &cstr::CStr,
		domain: &cstr::CStr,
		callback: ffi::DNSServiceResolveReply,
		context: *mut c_void
	) -> FFIResult<DNSService> {
		Self::new(
			InnerDNSService::resolve(flags, interface_index, name, reg_type, domain, callback, context)
		)
	}

	pub fn register_record(&self,
		flags: ffi::DNSServiceFlags,
		interface_index: u32,
		fullname: &cstr::CStr,
		rr_type: u16,
		rr_class: u16,
		rdata: &[u8],
		ttl: u32,
		callback: ffi::DNSServiceRegisterRecordReply,
		context: *mut c_void
	) -> FFIResult<DNSRecord> {
		Ok(DNSRecord(
			InnerDNSRecord::register_record(
				self,
				flags,
				interface_index,
				fullname,
				rr_type,
				rr_class,
				rdata,
				ttl,
				callback,
				context
			)?
		))
	}

	pub fn create_connection() -> FFIResult<DNSService> {
		Self::new(
			InnerDNSService::create_connection()
		)
	}

	pub fn query_record(
		flags: ffi::DNSServiceFlags,
		interface_index: u32,
		fullname: &cstr::CStr,
		rr_type: u16,
		rr_class: u16,
		callback: ffi::DNSServiceQueryRecordReply,
		context: *mut c_void
	) -> FFIResult<DNSService> {
		Self::new(
			InnerDNSService::query_record(flags, interface_index, fullname, rr_type, rr_class, callback, context)
		)
	}
}

struct InnerDNSRecord(DNSService, ffi::DNSRecordRef, u16);

impl Drop for InnerDNSRecord {
	fn drop(&mut self) {
		if null_mut() != self.1 {
			unsafe {
				ffi::DNSServiceRemoveRecord(
					self.get_service().0,
					self.1,
					0 /* no flags */
				);
			}
		}
	}
}

impl InnerDNSRecord {
	fn get_service(&self) -> &mut InnerDNSService {
		self.0.get()
	}

	fn rr_type(&self) -> u16 {
		self.2
	}

	// only valid when `service` was created through "register"
	fn add_record(
		service: &DNSService,
		flags: ffi::DNSServiceFlags,
		rr_type: u16,
		rdata: &[u8],
		ttl: u32
	) -> FFIResult<InnerDNSRecord> {
		let rd_len = rdata.len();
		assert!(rd_len < (1 << 16));
		let rd_len = rd_len as u16;
		let rdata = rdata.as_ptr();

		let mut record_ref: ffi::DNSRecordRef = null_mut();
		Error::from(unsafe {
			ffi::DNSServiceAddRecord(
				service.get().0,
				&mut record_ref,
				flags,
				rr_type,
				rd_len,
				rdata,
				ttl
			)
		})?;
		Ok(InnerDNSRecord(service.clone(), record_ref, rr_type))
	}

	// only valid when `service` was created through "create_connection"
	fn register_record(
		service: &DNSService,
		flags: ffi::DNSServiceFlags,
		interface_index: u32,
		fullname: &cstr::CStr,
		rr_type: u16,
		rr_class: u16,
		rdata: &[u8],
		ttl: u32,
		callback: ffi::DNSServiceRegisterRecordReply,
		context: *mut c_void
	) -> FFIResult<InnerDNSRecord> {
		let rd_len = rdata.len();
		assert!(rd_len < (1 << 16));
		let rd_len = rd_len as u16;
		let rdata = rdata.as_ptr();

		let mut record_ref: ffi::DNSRecordRef = null_mut();
		Error::from(unsafe {
			ffi::DNSServiceRegisterRecord(
				service.get().0,
				&mut record_ref,
				flags,
				interface_index,
				fullname.as_ptr(),
				rr_type,
				rr_class,
				rd_len,
				rdata,
				ttl,
				callback,
				context
			)
		})?;
		Ok(InnerDNSRecord(service.clone(), record_ref, rr_type))
	}

	fn update_record(
		&self,
		flags: ffi::DNSServiceFlags,
		rdata: &[u8],
		ttl: u32
	) -> FFIResult<()> {
		let rd_len = rdata.len();
		assert!(rd_len < (1 << 16));
		let rd_len = rd_len as u16;
		let rdata = rdata.as_ptr();

		Error::from(unsafe {
			ffi::DNSServiceUpdateRecord(
				self.get_service().0,
				self.1,
				flags,
				rd_len,
				rdata,
				ttl
			)
		})
	}

	fn keep(mut self) {
		self.1 = null_mut();
	}
}

pub struct DNSRecord(InnerDNSRecord);

impl DNSRecord {
	pub fn rr_type(&self) -> u16 {
		self.0.rr_type()
	}

	pub fn update_record(
		&self,
		flags: ffi::DNSServiceFlags,
		rdata: &[u8],
		ttl: u32
	) -> FFIResult<()> {
		self.0.update_record(flags, rdata, ttl)
	}

	// keep "forever" (until service is dropped)
	pub fn keep(self) {
		self.0.keep()
	}
}

pub fn reconfirm_record(
	flags: ffi::DNSServiceFlags,
	interface_index: u32,
	fullname: &cstr::CStr,
	rr_type: u16,
	rr_class: u16,
	rdata: &[u8]
) {
	let rd_len = rdata.len();
	assert!(rd_len < (1 << 16));
	let rd_len = rd_len as u16;
	let rdata = rdata.as_ptr();

	unsafe {
		ffi::DNSServiceReconfirmRecord(
			flags,
			interface_index,
			fullname.as_ptr(),
			rr_type,
			rr_class,
			rd_len,
			rdata
		);
	}
}