nexora 0.0.1

Single-threaded io_uring HTTP/1.1, HTTP/2, WebSocket, and TLS proactor client and server SDK
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
//! Static host overrides, a TTL-bounded address cache, and c-ares queries
//! driven by the shared proactor.
//!
//! [`HostResolver`] submits one `A` and one `AAAA` query per lookup, stages the
//! c-ares socket and deadline work through [`HostResolver::flush`], and routes
//! each completion into a [`DnsResolution`] staged on the [`SubsystemInbox`].
//! Overrides added through [`HostResolver::resolve_host`] and answers held in
//! [`DnsCache`] are consulted before a query is sent.

use std::{
    collections::HashMap,
    net::{IpAddr, SocketAddr},
    time::{Duration, Instant},
};

use cares::{DnsRecordType, DnsSection};
use liburing::{Completion, CompletionDispatcher, Proactor, SubmissionContext, SubsystemId};

use crate::{
    error::{Error, Result},
    inbox::{AddressFamily, DnsResolution, SubsystemInbox},
};

/// Entry capacity of the address cache owned by every [`HostResolver`].
const DEFAULT_CACHE_CAPACITY: usize = 256;

/// Socket-bridge capacity used by [`HostResolver::default_resolver`].
const DEFAULT_BRIDGE_CAPACITY: usize = 64;

/// Canonical cache and override key for a host name.
///
/// Lowercasing copies the name once so a caller's buffer is never retained.
/// Exactly one trailing dot is removed because it marks the absolute name form;
/// stripping further dots would let a malformed name alias a valid one.
fn normalize_host(host: &str) -> String {
    let lowered = host.to_ascii_lowercase();
    match lowered.strip_suffix('.') {
        Some(stripped) => stripped.to_string(),
        None => lowered,
    }
}

mod cache;
pub use cache::{DnsCache, DnsCacheEntry};

/// Static overrides, an address cache, and a proactor-driven c-ares channel.
///
/// The resolver is registered on the same ring as the client. Query completions
/// only stage a [`DnsResolution`] on the inbox; the client drive loop applies
/// the result, so a completion never mutates connection state directly.
///
/// The owner must call [`liburing::Proactor::shutdown`] before dropping this
/// value: the driver retains armed timeout operands that must outlive their
/// final completions.
pub struct HostResolver {
    subsystem: SubsystemId,
    driver: cares::ProactorResolver,
    static_hosts: HashMap<(String, u16), Vec<SocketAddr>>,
    cache: DnsCache,
    _guard: cares::LibraryGuard,
}

impl HostResolver {
    /// Creates a resolver bound to one proactor subsystem.
    ///
    /// `capacity` is the socket-bridge capacity of the c-ares driver; the
    /// address cache always holds 256 entries.
    ///
    /// # Errors
    /// Returns [`Error::InvalidState`] when `dns_subsystem` is the reserved
    /// internal slot or when `servers_csv` is empty, because c-ares treats an
    /// empty server list as a request to blank every configured server.
    /// Returns the library-initialization, option, or socket-bridge error from
    /// the underlying driver.
    ///
    /// # Panics
    /// This function does not panic.
    pub fn new(
        dns_subsystem: SubsystemId,
        options: cares::ResolverOptions,
        servers_csv: &str,
        capacity: usize,
    ) -> Result<Self> {
        if dns_subsystem == SubsystemId::INTERNAL {
            return Err(Error::InvalidState(
                "DNS subsystem id 0 is reserved for the proactor".to_string(),
            ));
        }
        if servers_csv.is_empty() {
            return Err(Error::InvalidState(
                "DNS server list must not be empty".to_string(),
            ));
        }
        let guard = cares::LibraryGuard::acquire()?;
        let driver = cares::ProactorResolver::new(dns_subsystem, options, servers_csv, capacity)?;
        Ok(Self {
            subsystem: dns_subsystem,
            driver,
            static_hosts: HashMap::new(),
            cache: DnsCache::new(DEFAULT_CACHE_CAPACITY),
            _guard: guard,
        })
    }

    /// Creates a resolver from the system resolver configuration.
    ///
    /// A temporary channel reads the platform server list because
    /// [`cares::ResolverOptions`] is not cloneable and [`cares::Resolver::new`]
    /// consumes it. The probe is dropped before the real driver is built.
    ///
    /// # Errors
    /// Returns [`Error::InvalidState`] when the system configuration lists no
    /// servers. Hermetic runners without a resolver configuration must pass
    /// explicit servers to [`Self::new`]. Otherwise the same errors as
    /// [`Self::new`] apply.
    ///
    /// # Panics
    /// This function does not panic.
    pub fn default_resolver(dns_subsystem: SubsystemId) -> Result<Self> {
        let probe_options = cares::ResolverOptions::builder().build()?;
        let probe = cares::Resolver::new(probe_options)?;
        let servers = probe.servers_csv()?;
        drop(probe);
        if servers.is_empty() {
            return Err(Error::InvalidState(
                "system resolver configuration lists no DNS servers".to_string(),
            ));
        }
        let options = cares::ResolverOptions::builder().build()?;
        Self::new(dns_subsystem, options, &servers, DEFAULT_BRIDGE_CAPACITY)
    }

    /// Returns the subsystem this resolver is registered under.
    ///
    /// # Errors
    /// This operation does not return errors.
    ///
    /// # Panics
    /// This function does not panic.
    pub fn subsystem_id(&self) -> SubsystemId {
        self.subsystem
    }

    /// Adds a static override for `host` and `port`.
    ///
    /// An empty `addrs` list removes the override instead of storing a lookup
    /// that can never succeed.
    ///
    /// # Errors
    /// This operation does not return errors.
    ///
    /// # Panics
    /// This function does not panic.
    pub fn resolve_host(&mut self, host: impl Into<String>, port: u16, addrs: Vec<IpAddr>) {
        let host: String = host.into();
        let key = (normalize_host(&host), port);
        if addrs.is_empty() {
            drop(self.static_hosts.remove(&key));
            return;
        }
        let mapped = addrs
            .into_iter()
            .map(|ip| SocketAddr::new(ip, port))
            .collect();
        drop(self.static_hosts.insert(key, mapped));
    }

    /// Removes the static override and cached answer for `host` and `port`.
    ///
    /// # Errors
    /// This operation does not return errors.
    ///
    /// # Panics
    /// This function does not panic.
    pub fn clear_static(&mut self, host: &str, port: u16) {
        drop(self.static_hosts.remove(&(normalize_host(host), port)));
        self.cache.remove(host, port);
    }

    /// Returns an address set from the overrides, an IP literal, or the cache.
    ///
    /// Lookup order is static override, IP literal, cache. An IP literal needs
    /// no DNS query, so `host` itself is returned with `port` applied.
    ///
    /// # Errors
    /// This operation does not return errors.
    ///
    /// # Panics
    /// This function does not panic.
    pub fn lookup_static_or_cache(
        &mut self,
        host: &str,
        port: u16,
        now: Instant,
    ) -> Option<Vec<SocketAddr>> {
        let key = (normalize_host(host), port);
        if let Some(addrs) = self.static_hosts.get(&key) {
            return Some(addrs.clone());
        }
        if let Ok(ip) = host.parse::<IpAddr>() {
            return Some(vec![SocketAddr::new(ip, port)]);
        }
        self.cache.lookup(host, port, now)
    }

    /// Starts the `A` and `AAAA` queries for `host` and `port`.
    ///
    /// Both queries are submitted before the driver is flushed, so the returned
    /// handles cover every query the proactor may complete. A failure while
    /// submitting the second query or flushing cancels the queries already
    /// accepted, because no caller can observe handles that were never
    /// returned. A callback may run synchronously inside submission; if the
    /// second submission then fails, a resolution for this host and port may
    /// already be staged on the inbox.
    ///
    /// # Errors
    /// Returns a resolver submission error, or the driver flush error. Queries
    /// already submitted are cancelled before the error is returned.
    ///
    /// # Panics
    /// This function does not panic.
    pub fn start_query(
        &mut self,
        proactor: &mut Proactor,
        host: &str,
        port: u16,
        inbox: &SubsystemInbox,
    ) -> Result<Vec<cares::QueryHandle>> {
        let mut handles = Vec::with_capacity(2);
        let ipv4 = self.submit_family_query(
            host,
            port,
            DnsRecordType::A,
            AddressFamily::Ipv4,
            inbox.clone(),
        )?;
        handles.push(ipv4);
        match self.submit_family_query(
            host,
            port,
            DnsRecordType::AAAA,
            AddressFamily::Ipv6,
            inbox.clone(),
        ) {
            Ok(ipv6) => handles.push(ipv6),
            Err(error) => {
                self.cancel_queries(&handles);
                return Err(error);
            }
        }
        if let Err(error) = self.driver.flush(proactor) {
            self.cancel_queries(&handles);
            return Err(Error::from(error));
        }
        Ok(handles)
    }

    /// Cancels every query in `handles`, ignoring stale handles.
    ///
    /// A handle whose query already completed is stale, and cancellation is
    /// then a no-op rather than an error the caller could act on. Cancellation
    /// retires the callback slot; c-ares exposes no per-query abort, so the
    /// channel may keep the query in flight until it completes or times out.
    ///
    /// # Errors
    /// This operation does not return errors.
    ///
    /// # Panics
    /// This function does not panic.
    pub fn cancel_queries(&mut self, handles: &[cares::QueryHandle]) {
        for handle in handles {
            // A stale handle means the query already completed, so the
            // cancellation is already satisfied.
            drop(self.driver.resolver.cancel_query(*handle));
        }
    }

    /// Stores an answer in the address cache.
    ///
    /// # Errors
    /// This operation does not return errors.
    ///
    /// # Panics
    /// This function does not panic.
    pub fn store_cache(
        &mut self,
        host: &str,
        port: u16,
        addrs: Vec<SocketAddr>,
        ttl: Duration,
        now: Instant,
    ) {
        self.cache.insert(host, port, addrs, ttl, now);
    }

    /// Borrows the address cache.
    ///
    /// # Errors
    /// This operation does not return errors.
    ///
    /// # Panics
    /// This function does not panic.
    pub fn cache(&self) -> &DnsCache {
        &self.cache
    }

    /// Mutably borrows the address cache.
    ///
    /// # Errors
    /// This operation does not return errors.
    ///
    /// # Panics
    /// This function does not panic.
    pub fn cache_mut(&mut self) -> &mut DnsCache {
        &mut self.cache
    }

    /// Stages queued c-ares socket work and the next deadline on the ring.
    ///
    /// # Errors
    /// Returns a proactor submission, timer, or channel error.
    ///
    /// # Panics
    /// This function does not panic.
    pub fn flush(&mut self, proactor: &mut Proactor) -> Result<()> {
        self.driver.flush(proactor)?;
        Ok(())
    }

    /// Stops queries and stages cancellation while leaving the shared ring open.
    /// # Errors
    /// Returns resolver or cancellation-submission errors; pending I/O stays owned.
    /// # Panics
    /// This function does not panic.
    pub fn begin_shutdown(&mut self, proactor: &mut Proactor) -> Result<()> {
        self.driver.begin_shutdown(proactor)?;
        Ok(())
    }

    /// Reports whether DNS targets or cancellation acknowledgments remain live.
    /// # Errors
    /// This function does not return errors.
    /// # Panics
    /// This function does not panic.
    #[must_use]
    pub fn has_pending_io(&self) -> bool {
        self.driver.has_pending_io()
    }

    /// Consumes one completion addressed to this resolver subsystem.
    ///
    /// Completions for other subsystems are ignored so a caller may forward
    /// every completion without filtering.
    ///
    /// # Errors
    /// Returns a driver completion-dispatch error.
    ///
    /// # Panics
    /// This function does not panic.
    pub fn on_completion(
        &mut self,
        completion: Completion,
        submissions: &mut SubmissionContext<'_>,
    ) -> Result<()> {
        self.dispatch_completion(completion, submissions)
    }

    /// Submits one query whose completion stages a [`DnsResolution`].
    ///
    /// # Errors
    /// Returns a resolver submission error.
    fn submit_family_query(
        &mut self,
        host: &str,
        port: u16,
        record_type: DnsRecordType,
        family: AddressFamily,
        inbox: SubsystemInbox,
    ) -> Result<cares::QueryHandle> {
        let name = host.to_string();
        let handle =
            self.driver
                .resolver
                .submit_query(host, record_type, move |_ctx, outcome| {
                    inbox.dns_resolved(resolution_from_outcome(&name, port, family, &outcome));
                })?;
        Ok(handle)
    }

    /// Dispatches a completion to the c-ares driver.
    ///
    /// # Errors
    /// Returns a driver completion-dispatch error.
    fn dispatch_completion(
        &mut self,
        completion: Completion,
        submissions: &mut SubmissionContext<'_>,
    ) -> Result<()> {
        if completion.operation_id.subsystem_id() != self.subsystem {
            return Ok(());
        }
        self.driver.dispatch(completion, submissions)?;
        Ok(())
    }
}

impl CompletionDispatcher for HostResolver {
    fn dispatch(
        &mut self,
        completion: Completion,
        submissions: &mut SubmissionContext<'_>,
    ) -> liburing::Result<()> {
        match self.dispatch_completion(completion, submissions) {
            Ok(()) => Ok(()),
            Err(_) => Err(liburing::UringError::InvalidConfig("DNS resolver failure")),
        }
    }
}

/// Converts one c-ares query outcome into a resolution for the inbox.
///
/// Records with an unmodeled type are skipped. The reported TTL is the minimum
/// across every answer record, including the CNAME chain, so a cached entry
/// never outlives the shortest-lived record it depends on.
fn resolution_from_outcome(
    host: &str,
    port: u16,
    family: AddressFamily,
    outcome: &cares::QueryOutcome<'_>,
) -> DnsResolution {
    let Some(record) = outcome.record else {
        return DnsResolution::failure(host, port, family, outcome.status.message());
    };
    let wanted = match family {
        AddressFamily::Ipv4 => DnsRecordType::A,
        AddressFamily::Ipv6 => DnsRecordType::AAAA,
    };
    let mut addrs = Vec::new();
    let mut minimum_ttl: Option<u32> = None;
    for index in 0..record.rr_count(DnsSection::Answer) {
        let Ok(rr) = record.rr(DnsSection::Answer, index) else {
            continue;
        };
        // DNSSEC answers mix unmodeled types such as RRSIG into the section; an
        // unknown type must not discard the address records beside it.
        let Ok(record_type) = rr.record_type() else {
            continue;
        };
        let ttl = rr.ttl();
        minimum_ttl = Some(minimum_ttl.map_or(ttl, |current| current.min(ttl)));
        if record_type != wanted {
            continue;
        }
        let address = match family {
            AddressFamily::Ipv4 => match rr.as_a() {
                Ok(record) => SocketAddr::new(IpAddr::V4(record.address), port),
                Err(_) => continue,
            },
            AddressFamily::Ipv6 => match rr.as_aaaa() {
                Ok(record) => {
                    if record.address.is_unicast_link_local() {
                        continue;
                    }
                    SocketAddr::new(IpAddr::V6(record.address), port)
                }
                Err(_) => continue,
            },
        };
        if !addrs.contains(&address) {
            addrs.push(address);
        }
    }
    if addrs.is_empty() {
        return DnsResolution::failure(
            host,
            port,
            family,
            "no usable A/AAAA records in the answer",
        );
    }
    let ttl = match minimum_ttl {
        Some(seconds) => Duration::from_secs(u64::from(seconds)),
        None => Duration::ZERO,
    };
    DnsResolution::success(host, port, family, addrs, ttl)
}

#[cfg(test)]
mod tests {
    use std::net::{Ipv4Addr, Ipv6Addr};

    use cares::{
        ARecord, AaaaRecord, AresStatus, CnameRecord, DnsClass, DnsRecord, QueryOutcome,
        types::{DnsFlags, DnsOpcode, DnsRcode},
    };

    use super::*;

    /// Builds a mixed answer: duplicated A records, a second A record with the
    /// shortest address TTL, a usable AAAA record, a link-local AAAA record,
    /// and a CNAME carrying the overall minimum TTL.
    fn mixed_answer() -> Result<DnsRecord> {
        let mut record = DnsRecord::new(
            0,
            DnsFlags::QR | DnsFlags::RD,
            DnsOpcode::Query,
            DnsRcode::NoError,
        )?;
        for (address, ttl) in [
            (Ipv4Addr::new(192, 0, 2, 1), 30),
            (Ipv4Addr::new(192, 0, 2, 1), 30),
            (Ipv4Addr::new(192, 0, 2, 2), 20),
        ] {
            let mut rr = record.rr_add(
                DnsSection::Answer,
                "example.test",
                DnsRecordType::A,
                DnsClass::IN,
                ttl,
            )?;
            rr.set(&ARecord { address })?;
        }
        let mut rr = record.rr_add(
            DnsSection::Answer,
            "example.test",
            DnsRecordType::AAAA,
            DnsClass::IN,
            60,
        )?;
        rr.set(&AaaaRecord {
            address: Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 1),
        })?;
        let mut rr = record.rr_add(
            DnsSection::Answer,
            "example.test",
            DnsRecordType::AAAA,
            DnsClass::IN,
            10,
        )?;
        rr.set(&AaaaRecord {
            address: Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1),
        })?;
        let mut rr = record.rr_add(
            DnsSection::Answer,
            "alias.example.test",
            DnsRecordType::CNAME,
            DnsClass::IN,
            5,
        )?;
        rr.set(&CnameRecord {
            cname: String::from("example.test"),
        })?;
        Ok(record)
    }

    #[test]
    fn resolution_keeps_only_the_requested_family() -> Result<()> {
        let record = mixed_answer()?;
        let outcome = QueryOutcome {
            status: AresStatus::Success,
            timeouts: 0,
            record: Some(record.as_ref()),
        };
        let ipv4 = resolution_from_outcome("example.test", 443, AddressFamily::Ipv4, &outcome);
        assert!(ipv4.is_usable());
        assert_eq!(ipv4.family, AddressFamily::Ipv4);
        assert_eq!(ipv4.host, "example.test");
        assert_eq!(ipv4.port, 443);
        assert_eq!(
            ipv4.addrs,
            vec![
                SocketAddr::from(([192, 0, 2, 1], 443)),
                SocketAddr::from(([192, 0, 2, 2], 443)),
            ]
        );
        // The CNAME's TTL is the minimum across the whole answer.
        assert_eq!(ipv4.ttl, Duration::from_secs(5));
        let ipv6 = resolution_from_outcome("example.test", 443, AddressFamily::Ipv6, &outcome);
        assert!(ipv6.is_usable());
        assert_eq!(ipv6.family, AddressFamily::Ipv6);
        assert_eq!(
            ipv6.addrs,
            vec![SocketAddr::from((
                Ipv6Addr::new(0x2001, 0x0db8, 0, 0, 0, 0, 0, 1),
                443,
            ))]
        );
        assert_eq!(ipv6.ttl, Duration::from_secs(5));
        Ok(())
    }

    #[test]
    fn resolution_reports_failures_for_nodata_and_errors() -> Result<()> {
        let empty = DnsRecord::new(0, DnsFlags::QR, DnsOpcode::Query, DnsRcode::NoError)?;
        let nodata = QueryOutcome {
            status: AresStatus::Success,
            timeouts: 0,
            record: Some(empty.as_ref()),
        };
        let resolution = resolution_from_outcome("example.test", 443, AddressFamily::Ipv6, &nodata);
        assert!(!resolution.is_usable());
        assert!(resolution.addrs.is_empty());
        assert!(resolution.error.is_some());
        let not_found = QueryOutcome {
            status: AresStatus::NotFound,
            timeouts: 1,
            record: None,
        };
        let resolution =
            resolution_from_outcome("example.test", 443, AddressFamily::Ipv4, &not_found);
        assert!(!resolution.is_usable());
        assert!(
            resolution
                .error
                .as_deref()
                .is_some_and(|message| !message.is_empty())
        );
        Ok(())
    }
}