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
486
487
488
489
490
491
492
493
494
495
#![allow(clippy::too_many_arguments)]
use futures_util::FutureExt;
use libc::c_void;
use std::{
	io,
	ptr::null_mut,
	sync::{
		Arc,
		Mutex,
	},
	task::{
		Context,
		Poll,
	},
};

use crate::{
	cstr,
	dns_consts::{
		Class,
		Type,
	},
	error::Error,
	ffi,
	notify::{
		Notified,
		Notify,
	},
};

// More typesafe than raw "ffi", but still not quite done

struct ManagedService(ffi::DNSServiceRef);

unsafe impl Send for ManagedService {}
unsafe impl Sync for ManagedService {}

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

/// Keeps the service alive
#[derive(Clone)]
pub struct ServiceHandle {
	managed_service: Arc<ManagedService>,
}

impl ServiceHandle {
	fn new(raw: ffi::DNSServiceRef) -> Self {
		Self {
			managed_service: Arc::new(ManagedService(raw)),
		}
	}

	fn as_raw(&self) -> ffi::DNSServiceRef {
		self.managed_service.0
	}
}

pub(crate) trait EventedService: Unpin {
	// in various places we need to drive the underlying (possibly shared)
	// state machine, which will set other readiness events we then check.
	//
	// this underlying state machine will never complete, so there is
	// no need to return a Poll<..> result; but we do need a context to
	// drive the underlying service.
	fn poll_service(&mut self, cx: &mut Context<'_>) -> io::Result<()>;
}

/// Many places can keep the service alive, but a single active user
pub(crate) struct OwnedService {
	handle: ServiceHandle,
	processing: crate::evented::ReadProcessor,
}

impl OwnedService {
	fn new(raw: ffi::DNSServiceRef) -> io::Result<Self> {
		let fd = unsafe { ffi::DNSServiceRefSockFD(raw) };
		let handle = ServiceHandle::new(raw);
		let processing = crate::evented::ReadProcessor::new(fd)?;
		Ok(Self { handle, processing })
	}

	pub(crate) fn share(self) -> SharedService {
		let bg_fail_notify = Notify::new();
		let bg_fail_notified = bg_fail_notify.notified();
		let inner = Arc::new(Mutex::new(SharedInner {
			handle: self.handle,
			bg_error_buf: None,
			bg_failed: false,
			bg_fail_notify,
		}));
		let bg_inner = inner.clone();
		let mut processing = self.processing;

		let bg_task = futures_util::future::poll_fn(move |cx| {
			let mut inner = bg_inner.lock().unwrap();
			let raw = inner.handle.as_raw();
			let r = processing.process(cx, || {
				Error::from(unsafe { ffi::DNSServiceProcessResult(raw) })?;
				Ok(())
			});
			match r {
				Ok(()) => Poll::Pending, // run "forever"
				Err(e) => {
					inner.bg_error_buf = Some(e);
					inner.bg_failed = true;
					inner.bg_fail_notify.notify_waiters();
					Poll::Ready(()) // stop on errors
				},
			}
		});
		SharedService {
			inner,
			_bg_task_handle: Arc::new(AbortHandle(tokio::spawn(bg_task))),
			bg_fail_notified,
		}
	}

	// -----

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

	pub(crate) 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,
	) -> Result<Self, Error> {
		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(Self::new(sd_ref)?)
	}

	pub(crate) fn browse(
		flags: ffi::DNSServiceFlags,
		interface_index: u32,
		reg_type: &cstr::CStr<'_>,
		domain: &cstr::NullableCStr<'_>,
		callback: ffi::DNSServiceBrowseReply,
		context: *mut c_void,
	) -> Result<Self, Error> {
		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(Self::new(sd_ref)?)
	}

	pub(crate) 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,
	) -> Result<Self, Error> {
		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(Self::new(sd_ref)?)
	}

	pub(crate) fn query_record(
		flags: ffi::DNSServiceFlags,
		interface_index: u32,
		fullname: &cstr::CStr<'_>,
		rr_type: Type,
		rr_class: Class,
		callback: ffi::DNSServiceQueryRecordReply,
		context: *mut c_void,
	) -> Result<Self, Error> {
		let mut sd_ref: ffi::DNSServiceRef = null_mut();
		Error::from(unsafe {
			ffi::DNSServiceQueryRecord(
				&mut sd_ref,
				flags,
				interface_index,
				fullname.as_ptr(),
				rr_type.0,
				rr_class.0,
				callback,
				context,
			)
		})?;
		Ok(Self::new(sd_ref)?)
	}
}

impl EventedService for OwnedService {
	fn poll_service(&mut self, cx: &mut Context<'_>) -> io::Result<()> {
		let raw = self.handle.as_raw();
		self.processing.process(cx, || {
			Error::from(unsafe { ffi::DNSServiceProcessResult(raw) })?;
			Ok(())
		})
	}
}

struct AbortHandle(tokio::task::JoinHandle<()>);

impl Drop for AbortHandle {
	fn drop(&mut self) {
		self.0.abort();
	}
}

struct SharedInner {
	// protect ffi calls
	handle: ServiceHandle,
	// forward error from background task
	bg_error_buf: Option<io::Error>,
	// but we can extract error only once, so remember it failed
	bg_failed: bool,
	//
	bg_fail_notify: Notify,
}

#[derive(Clone)]
pub(crate) struct SharedService {
	inner: Arc<Mutex<SharedInner>>,
	// make sure we kill the background task once all users are gone
	_bg_task_handle: Arc<AbortHandle>,
	bg_fail_notified: Notified,
}

impl EventedService for SharedService {
	fn poll_service(&mut self, cx: &mut Context<'_>) -> io::Result<()> {
		// service is run in background task; just make sure there wasn't
		// an error yet and to get notified of future errors.
		let mut inner = self.inner.lock().unwrap();
		if let Some(e) = inner.bg_error_buf.take() {
			return Err(e);
		}
		if inner.bg_failed {
			return Err(io::Error::new(io::ErrorKind::NotConnected, "service gone"));
		}
		// should be pending, because we just checked for errors:
		let _ = self.bg_fail_notified.poll_unpin(cx);
		Ok(())
	}
}

impl SharedService {
	pub(crate) fn get_default_txt_record(self) -> DNSRecord {
		DNSRecord {
			service: self,
			raw: DNSRecordRef(null_mut()),
			rr_type: Type::TXT,
		}
	}

	// only valid when `service` was created through "register"
	//
	// still needs a SharedService to synchronize ffi calls
	pub(crate) fn add_record(
		self,
		flags: ffi::DNSServiceFlags,
		rr_type: Type,
		rdata: &[u8],
		ttl: u32,
	) -> Result<DNSRecord, Error> {
		let rd_len = rdata.len();
		assert!(rd_len < (1 << 16));
		let rd_len = rd_len as u16;
		let rdata = rdata.as_ptr();

		let inner = self.inner.lock().unwrap();

		let mut record_ref: ffi::DNSRecordRef = null_mut();
		Error::from(unsafe {
			ffi::DNSServiceAddRecord(
				inner.handle.as_raw(),
				&mut record_ref,
				flags,
				rr_type.0,
				rd_len,
				rdata,
				ttl,
			)
		})?;

		drop(inner);

		Ok(DNSRecord {
			service: self,
			raw: DNSRecordRef(record_ref),
			rr_type,
		})
	}

	pub(crate) fn create_connection() -> Result<Self, Error> {
		let mut sd_ref: ffi::DNSServiceRef = null_mut();
		Error::from(unsafe { ffi::DNSServiceCreateConnection(&mut sd_ref) })?;
		Ok(OwnedService::new(sd_ref)?.share())
	}

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

		let inner = self.inner.lock().unwrap();

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

		drop(inner);

		Ok(DNSRecord {
			service: self,
			raw: DNSRecordRef(record_ref),
			rr_type,
		})
	}
}

// so we don't have to unsafe impl for whole `DNSRecord`
//
// can only be used in combination with service handle, which is protected by mutex
struct DNSRecordRef(ffi::DNSRecordRef);

unsafe impl Sync for DNSRecordRef {}
unsafe impl Send for DNSRecordRef {}

pub(crate) struct DNSRecord {
	service: SharedService,
	raw: DNSRecordRef,
	rr_type: Type,
}

impl Drop for DNSRecord {
	fn drop(&mut self) {
		if !self.raw.0.is_null() {
			let inner = self.service.inner.lock().unwrap();
			unsafe {
				ffi::DNSServiceRemoveRecord(
					inner.handle.as_raw(),
					self.raw.0,
					0, // no flags
				);
			}
		}
	}
}

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

		let inner = self.service.inner.lock().unwrap();

		Error::from(unsafe {
			ffi::DNSServiceUpdateRecord(
				inner.handle.as_raw(),
				self.raw.0,
				flags,
				rd_len,
				rdata,
				ttl,
			)
		})
	}

	pub(crate) fn rr_type(&self) -> Type {
		self.rr_type
	}

	// keep "forever" (until service is dropped)
	pub(crate) fn keep(mut self) {
		self.raw.0 = null_mut();
	}
}

pub fn reconfirm_record(
	flags: ffi::DNSServiceFlags,
	interface_index: u32,
	fullname: &cstr::CStr<'_>,
	rr_type: Type,
	rr_class: Class,
	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.0,
			rr_class.0,
			rd_len,
			rdata,
		);
	}
}