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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//! Single-threaded, threadless, completion-driven `c-ares` bindings.
//!
//! [`Resolver`] owns one `c-ares` channel compiled with `CARES_THREADS=OFF` and is
//! `!Send + !Sync`. Shared-ring callers forward socket and timer completions from a
//! proactor through [`Resolver::on_cqe`]; standalone callers drive the channel with
//! [`Resolver::wait_completions`], [`Resolver::poll_completions`], or
//! [`Resolver::resolve`].
//!
//! Completion callbacks run synchronously on the driving thread, either from inside
//! an `ares_process_fds` call or from the submission call itself. A callback receives
//! a [`ChannelCtx`] that allows follow-up submissions and per-query cancellation on
//! the same channel, but cannot recursively drive the resolver.
mod library;
pub mod callbacks;
pub mod error;
pub mod options;
pub mod proactor;
pub mod record;
pub mod record_rr;
pub mod resolver;
pub mod socket_bridge;
pub mod timer;
pub mod types;
pub use cares_macros::*;
pub use error::{AresError, AresStatus, Result};
pub use library::LibraryGuard;
pub use options::{ResolverBuilder, ResolverOptions};
pub use proactor::ProactorResolver;
pub use record::{DnsRecord, DnsRecordRef};
pub use record_rr::{
ARecord, AaaaRecord, CaaRecord, CnameRecord, HttpsRecord, MxRecord, NaptrRecord, NsRecord,
OptRecord, PtrRecord, SoaRecord, SrvRecord, SvcbRecord, TlsaRecord, TxtRecord, UriRecord,
};
pub use resolver::{
ChannelCtx, HostOutcome, NameInfoOutcome, PollOutcome, QueryOutcome, Resolver, ResolverCqe,
ServerStateEvent,
};
pub use types::{DnsClass, DnsRecordType, DnsSection, HostEntry, NameInfo};
/// Identifies one in-flight query within a [`Resolver`].
///
/// Handles are generational, so a retired handle never addresses a newer occupant.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct QueryHandle {
pub(crate) index: u32,
pub(crate) generation: u32,
pub(crate) resolver_id: u64,
}
impl QueryHandle {
/// Returns the callback-table slot index.
pub fn index(&self) -> u32 {
self.index
}
/// Returns the slot generation current when this handle was issued.
pub fn generation(&self) -> u32 {
self.generation
}
}
/// Private machinery shared by the resolver and its callback trampolines.
///
/// Owns callback slots and channel state shared across the FFI boundary.
mod support {
use std::{
cell::{Cell, RefCell},
ffi::{CStr, CString},
panic::{AssertUnwindSafe, catch_unwind},
ptr,
rc::{Rc, Weak},
};
use cares_sys as sys;
use libc::{c_char, c_int, c_void};
use crate::{
callbacks::{
CallbackSlot, CallbackState, dns_trampoline, host_trampoline, nameinfo_trampoline,
},
error::{AresError, AresStatus, Result},
record::{DnsRecord, DnsRecordRef},
resolver::{
ChannelCtx, HostOutcome, NameInfoOutcome, QueryHandle, QueryOutcome, ServerStateEvent,
},
socket_bridge::SharedChannelState,
types::{DnsClass, DnsRecordType, HostEntry, NameInfo, ServerStateFlags},
};
pub(crate) type QueryFn = Box<dyn for<'cb> FnOnce(Result<DnsRecordRef<'cb>>, usize) + 'static>;
pub(crate) type HostFn = Box<dyn FnOnce(Result<HostEntry>, usize) + 'static>;
pub(crate) type NameInfoFn = Box<dyn FnOnce(Result<NameInfo>, usize) + 'static>;
pub(crate) type ServerStateCallback = Option<Rc<dyn Fn(ServerStateEvent)>>;
/// Type-erased callback slot held by one table entry.
pub(crate) enum SlotKind {
Query(Rc<CallbackSlot<QueryFn>>),
Host(Rc<CallbackSlot<HostFn>>),
NameInfo(Rc<CallbackSlot<NameInfoFn>>),
}
impl SlotKind {
fn is_completed(&self) -> bool {
match self {
Self::Query(slot) => slot.is_completed(),
Self::Host(slot) => slot.is_completed(),
Self::NameInfo(slot) => slot.is_completed(),
}
}
/// Creates the strong reference handed to c-ares as its `arg` pointer.
fn raw_arg(&self) -> *mut c_void {
match self {
Self::Query(slot) => Rc::into_raw(Rc::clone(slot)) as *mut c_void,
Self::Host(slot) => Rc::into_raw(Rc::clone(slot)) as *mut c_void,
Self::NameInfo(slot) => Rc::into_raw(Rc::clone(slot)) as *mut c_void,
}
}
/// Drops the closure uninvoked; a later C callback only marks the slot complete.
fn take(&self) {
match self {
Self::Query(slot) => drop(slot.take()),
Self::Host(slot) => drop(slot.take()),
Self::NameInfo(slot) => drop(slot.take()),
}
}
}
struct Entry {
generation: u32,
slot: SlotKind,
/// Clone handed to `ares_send_dnsrec`, kept alive until the callback fires.
_record: Option<DnsRecord>,
}
/// Generational slot table with a free list.
#[derive(Default)]
pub(crate) struct CallbackTable {
entries: Vec<Option<Entry>>,
/// Retired index and the generation the next occupant must use.
free: Vec<(u32, u32)>,
}
impl CallbackTable {
fn insert(
&mut self,
slot: SlotKind,
record: Option<DnsRecord>,
resolver_id: u64,
) -> Result<QueryHandle> {
if let Some((index, generation)) = self.free.pop() {
self.entries[index as usize] = Some(Entry {
generation,
slot,
_record: record,
});
return Ok(QueryHandle {
index,
generation,
resolver_id,
});
}
let index = u32::try_from(self.entries.len())
.map_err(|_| AresError::invalid_input("too many in-flight queries"))?;
self.entries.push(Some(Entry {
generation: 1,
slot,
_record: record,
}));
Ok(QueryHandle {
index,
generation: 1,
resolver_id,
})
}
fn get(&self, handle: QueryHandle) -> Option<&Entry> {
self.entries
.get(handle.index as usize)
.and_then(|entry| entry.as_ref())
.filter(|entry| entry.generation == handle.generation)
}
fn retire(&mut self, index: u32) {
let taken = self
.entries
.get_mut(index as usize)
.and_then(|entry| entry.take());
if let Some(entry) = taken {
self.free.push((index, entry.generation.wrapping_add(1)));
}
}
pub(crate) fn take_slot(&mut self, handle: QueryHandle) -> bool {
match self.get(handle) {
Some(entry) => {
entry.slot.take();
true
}
None => false,
}
}
/// Retires every slot whose callback already ran.
pub(crate) fn sweep(&mut self) {
for index in 0..self.entries.len() {
if self.entries[index]
.as_ref()
.is_some_and(|entry| entry.slot.is_completed())
{
self.retire(index as u32);
}
}
}
pub(crate) fn active_count(&self) -> usize {
self.entries
.iter()
.filter(|entry| {
entry
.as_ref()
.is_some_and(|entry| !entry.slot.is_completed())
})
.count()
}
}
/// Mutable state shared with callbacks and [`ChannelCtx`].
pub(crate) struct ResolverHost {
/// Raw channel pointer; null once the channel is destroyed.
pub(crate) channel: Cell<*mut sys::ares_channel_t>,
/// Channel-wide panic and nesting state owned by the trampolines.
pub(crate) state: Rc<CallbackState>,
pub(crate) resolver_id: u64,
/// Socket table shared with the resolver, needed for deferred notifications.
pub(crate) sockets: Rc<SharedChannelState>,
pub(crate) table: RefCell<CallbackTable>,
/// Set while an FFI call that may invoke user code is on the stack.
pub(crate) driving: Cell<bool>,
pub(crate) stopping: Cell<bool>,
pub(crate) server_state_cb: RefCell<ServerStateCallback>,
/// Self-reference for upgrades inside callbacks, without a strong cycle.
pub(crate) self_weak: Weak<ResolverHost>,
}
impl ResolverHost {
pub(crate) fn is_poisoned(&self) -> bool {
self.state.is_poisoned() || self.sockets.readiness_sink_failed()
}
/// Returns the live channel, rejecting a poisoned or destroyed resolver.
pub(crate) fn ready(&self) -> Result<*mut sys::ares_channel_t> {
if self.is_poisoned() {
return Err(AresError::CallbackPanicked);
}
let channel = self.channel.get();
if channel.is_null() {
Err(AresError::from_status(AresStatus::NotInitialized))
} else {
Ok(channel)
}
}
}
/// Marks the resolver as driving user code; nesting restores the previous value so a
/// re-entrant submission inside a callback cannot clear the outer FFI call's flag.
pub(crate) struct DrivingGuard<'a> {
host: &'a ResolverHost,
previous: bool,
}
impl<'a> DrivingGuard<'a> {
pub(crate) fn enter(host: &'a ResolverHost) -> Self {
Self {
host,
previous: host.driving.replace(true),
}
}
}
impl Drop for DrivingGuard<'_> {
fn drop(&mut self) {
self.host.driving.set(self.previous);
}
}
/// Splits a callback result into the status and optional value reported to users.
fn outcome_parts<T>(result: Result<T>) -> (AresStatus, Option<T>) {
match result {
Ok(value) => (AresStatus::Success, Some(value)),
Err(error) => (error.status().unwrap_or(AresStatus::Unknown(-1)), None),
}
}
pub(crate) fn query_slot<F>(host: &ResolverHost, callback: F) -> SlotKind
where
F: for<'cb> FnOnce(&ChannelCtx<'cb>, QueryOutcome<'cb>) + 'static,
{
SlotKind::Query(CallbackSlot::new(
Rc::clone(&host.state),
wrap_query_callback(host.self_weak.clone(), callback),
))
}
pub(crate) fn host_slot<F>(host: &ResolverHost, callback: F) -> SlotKind
where
F: FnOnce(&ChannelCtx<'_>, HostOutcome) + 'static,
{
SlotKind::Host(CallbackSlot::new(
Rc::clone(&host.state),
wrap_host_callback(host.self_weak.clone(), callback),
))
}
pub(crate) fn nameinfo_slot<F>(host: &ResolverHost, callback: F) -> SlotKind
where
F: FnOnce(&ChannelCtx<'_>, NameInfoOutcome) + 'static,
{
SlotKind::NameInfo(CallbackSlot::new(
Rc::clone(&host.state),
wrap_nameinfo_callback(host.self_weak.clone(), callback),
))
}
/// Generates an owned wrapper around a `FnOnce(&ChannelCtx, Outcome)` callback.
macro_rules! wrap_value_callback {
($name:ident, $value:ty, $outcome:ident, $field:ident, $fn_ty:ident) => {
pub(crate) fn $name<F>(weak: Weak<ResolverHost>, callback: F) -> $fn_ty
where
F: FnOnce(&ChannelCtx<'_>, $outcome) + 'static,
{
Box::new(move |result: Result<$value>, timeouts: usize| {
let Some(host) = weak.upgrade() else { return };
let (status, value) = outcome_parts(result);
callback(
&ChannelCtx {
host: host.as_ref(),
},
$outcome {
status,
timeouts,
$field: value,
},
);
})
}
};
}
wrap_value_callback!(wrap_host_callback, HostEntry, HostOutcome, host, HostFn);
wrap_value_callback!(
wrap_nameinfo_callback,
NameInfo,
NameInfoOutcome,
nameinfo,
NameInfoFn
);
fn wrap_query_callback<F>(weak: Weak<ResolverHost>, callback: F) -> QueryFn
where
F: for<'cb> FnOnce(&ChannelCtx<'cb>, QueryOutcome<'cb>) + 'static,
{
Box::new(move |result: Result<DnsRecordRef<'_>>, timeouts: usize| {
let Some(host) = weak.upgrade() else { return };
let (status, record) = outcome_parts(result);
let ctx = ChannelCtx {
host: host.as_ref(),
};
callback(
&ctx,
QueryOutcome {
status,
timeouts,
record,
},
);
})
}
/// The FFI entry point that starts one registered submission.
pub(crate) enum Submission<'a> {
Query {
name: CString,
record_type: DnsRecordType,
},
Record {
raw: *const sys::ares_dns_record_t,
},
HostName {
name: CString,
family: c_int,
},
HostAddress {
address: &'a [u8],
family: c_int,
},
NameInfo {
sockaddr: &'a [u8],
flags: c_int,
},
}
pub(crate) fn submit_query_on<F>(
host: &ResolverHost,
name: &str,
record_type: DnsRecordType,
callback: F,
) -> Result<QueryHandle>
where
F: for<'cb> FnOnce(&ChannelCtx<'cb>, QueryOutcome<'cb>) + 'static,
{
if name.is_empty() {
return Err(AresError::invalid_input("DNS name must not be empty"));
}
let name = CString::new(name)?;
dispatch(
host,
query_slot(host, callback),
None,
Submission::Query { name, record_type },
reclaim::<QueryFn>,
)
}
/// Registers `slot`, starts the submission, and reclaims the slot when c-ares reports
/// a failure without delivering a callback.
pub(crate) fn dispatch(
host: &ResolverHost,
slot: SlotKind,
record: Option<DnsRecord>,
submission: Submission<'_>,
reclaim: unsafe fn(*mut c_void),
) -> Result<QueryHandle> {
if host.stopping.get() {
return Err(AresError::from_status(AresStatus::Cancelled));
}
let channel = host.ready()?;
let arg = slot.raw_arg();
let handle = match host
.table
.borrow_mut()
.insert(slot, record, host.resolver_id)
{
Ok(handle) => handle,
Err(error) => {
// SAFETY: the raw reference was never handed to c-ares.
unsafe { reclaim(arg) };
return Err(error);
}
};
let status = {
let _driving = DrivingGuard::enter(host);
host.sockets
.with_process_scope(|| -> Result<sys::ares_status_t> {
match submission {
Submission::Query { name, record_type } => {
// SAFETY: the channel is live, `name` is NUL-terminated for the
// call, and `arg` is a strong reference created by `raw_arg`.
Ok(unsafe {
sys::ares_query_dnsrec(
channel,
name.as_ptr(),
sys::ares_dns_class_t(DnsClass::IN.as_raw()),
sys::ares_dns_rec_type_t(record_type.as_raw()),
Some(dns_trampoline::<QueryFn>),
arg,
ptr::null_mut(),
)
})
}
Submission::Record { raw } => {
// SAFETY: the channel is live, `raw` points at the clone
// retained by the table entry, and `arg` is a strong reference.
Ok(unsafe {
sys::ares_send_dnsrec(
channel,
raw,
Some(dns_trampoline::<QueryFn>),
arg,
ptr::null_mut(),
)
})
}
Submission::HostName { name, family } => {
// SAFETY: the channel is live, `name` is NUL-terminated for the
// call, and `arg` is a strong reference to the slot.
unsafe {
sys::ares_gethostbyname(
channel,
name.as_ptr(),
family,
Some(host_trampoline::<HostFn>),
arg,
);
}
Ok(sys::ares_status_t::ARES_SUCCESS)
}
Submission::HostAddress { address, family } => {
let length = c_int::try_from(address.len())
.map_err(|_| AresError::invalid_input("address is too long"))?;
// SAFETY: the channel is live, `address` is readable for
// `length` bytes, and `arg` is a strong reference to the slot.
unsafe {
sys::ares_gethostbyaddr(
channel,
address.as_ptr().cast(),
length,
family,
Some(host_trampoline::<HostFn>),
arg,
);
}
Ok(sys::ares_status_t::ARES_SUCCESS)
}
Submission::NameInfo { sockaddr, flags } => {
let length = u32::try_from(sockaddr.len()).map_err(|_| {
AresError::invalid_input("socket address is too long")
})?;
let storage = crate::resolver::aligned_sockaddr(sockaddr);
// SAFETY: the channel is live, `storage` is aligned and readable
// for `length` bytes, and `arg` is a strong reference to the slot.
unsafe {
sys::ares_getnameinfo(
channel,
(&raw const storage).cast::<libc::sockaddr>(),
length,
flags,
Some(nameinfo_trampoline::<NameInfoFn>),
arg,
);
}
Ok(sys::ares_status_t::ARES_SUCCESS)
}
}
})?
};
if host.is_poisoned() || host.sockets.readiness_sink_failed() {
return Err(AresError::CallbackPanicked);
}
let completed = host
.table
.borrow()
.get(handle)
.is_some_and(|entry| entry.slot.is_completed());
if !completed && !AresStatus::from(status).is_success() {
// SAFETY: c-ares failed without delivering a callback, so the trampoline never
// reconstructed the raw reference.
unsafe { reclaim(arg) };
host.table.borrow_mut().retire(handle.index);
return Err(AresError::from_status(status));
}
Ok(handle)
}
/// # Safety
/// `arg` must be a pointer produced by `Rc::into_raw` for a slot of type `F` whose
/// ownership was never transferred to a c-ares callback.
pub(crate) unsafe fn reclaim<F>(arg: *mut c_void) {
// SAFETY: the caller guarantees the pointer came from Rc::into_raw for this slot
// type and was not consumed by a trampoline.
drop(unsafe { Rc::from_raw(arg.cast::<CallbackSlot<F>>()) });
}
/// Forwards one server health transition to the registered Rust closure.
///
/// # Safety
/// `data` must be non-null and point to the registered `ResolverHost` allocation,
/// which must remain alive throughout this callback and every callback issued
/// during channel destruction. If non-null, `server_string` must point to a
/// readable NUL-terminated C string valid for the duration of this call. The
/// callback must execute on the channel owner thread without an exclusive
/// reference to the host.
pub(crate) unsafe extern "C" fn server_state_trampoline(
server_string: *const c_char,
success: sys::ares_bool_t,
flags: c_int,
data: *mut c_void,
) {
// SAFETY: `data` is the `Rc<ResolverHost>` allocation registered once with this
// channel; the resolver keeps it alive until after the final ares_destroy call.
let host = unsafe { &*data.cast::<ResolverHost>() };
if host.is_poisoned() {
return;
}
let server = if server_string.is_null() {
None
} else {
// SAFETY: c-ares passes a NUL-terminated string valid for the call.
Some(
unsafe { CStr::from_ptr(server_string) }
.to_string_lossy()
.into_owned(),
)
};
let event = ServerStateEvent {
server,
success: success == sys::ares_bool_t::ARES_TRUE,
flags: ServerStateFlags::from_bits_retain(flags),
};
let callback = match host.server_state_cb.try_borrow() {
Ok(borrowed) => borrowed.as_ref().cloned(),
Err(_) => None,
};
let Some(callback) = callback else { return };
if let Err(payload) = catch_unwind(AssertUnwindSafe(|| callback(event))) {
host.state.poison(payload);
}
}
}