cdns_rs/lib.rs
1/*-
2 * cdns-rs - a simple sync/async DNS query library
3 *
4 * Copyright (C) 2020 Aleksandr Morozov
5 *
6 * Copyright 2025 Aleksandr Morozov
7 *
8 * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
9 * the European Commission - subsequent versions of the EUPL (the "Licence").
10 *
11 * You may not use this work except in compliance with the Licence.
12 *
13 * You may obtain a copy of the Licence at:
14 *
15 * https://joinup.ec.europa.eu/software/page/eupl
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the Licence is distributed on an "AS IS" basis, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
20 * Licence for the specific language governing permissions and limitations
21 * under the Licence.
22 */
23
24//! cdns-rs
25//!
26//! An implementation of client side DNS query library - a resolver, a set of
27//! routines that provide access to the Internet Domain Name System (DNS).
28//!
29//! This library provides both:
30//! sync and async crates to query remote DNS servers and local hosts file.
31//!
32//! This code will be considered stable only when the major part of verion number
33//! will reach v 1.x.x
34//!
35//! # Limitations
36//!
37//! - This library reads /etc/hosts and partially understands /etc/resolv.conf i.e
38//! only 'nameserver' option is parsed at the moment.
39//! - This library does not support TCP query at the moment.
40//! - The nsswitch.conf is also ignored as this crate at the moment is not pretending
41//! to be a glibc/libc replacement.
42//! - Does not support DNS-over-TLS at the moment
43//! - Does not support DNSSEC at the moment
44//! - /etc/resolve.conf is limited to 'nameserver'
45//!
46//! # Supports
47//!
48//! Supported GNU/Linux and *BSD (incl OSX, but not guaranteed)
49//!
50//! # Layout
51//!
52//! Dirs:
53//! - a_sync - contains all async code
54//! - sync - contains all sync code
55//! Files:
56//! */*:
57//! - portable.rs - contains the code which requres porting to other OS.
58//! This code usually incompatiable with different UNIX-type OSes.
59//! - error.rs - an error wrapper and mapper
60//! - tokenizer.rs - contains simple tokenizer for parsing config files.
61//! - read_only_cell.rs - an implementation of the once written cell for
62//! initialization purposes.
63//! - cfg_host_parser.rs - a common code for the /etc/host which is used by
64//! both sync/async
65//! - cfg_resolv_parser.rs - a common code for the /etc/resolv.conf which is
66//! used by sync/async
67//! - query.rs - a common code of query
68//! - query_private.rs - internal items for query construction
69//!
70//! */sync/*:
71//! - caches.rs - contains a cached in memory config files like /etc/hosts.
72//! If file was modified, the cache controller attempts to reload file during
73//! library call from main program. Sync realization of common file.
74//! - cfg_parsers.rs - contains the confgiration files parsers.
75//! - common.rs - this file contains all code related to DNS protocol i.e structures,
76//! parsing.
77//! - network.rs - a code which deals with networking part, i.e msg exchange with DNS
78//! server.
79//! - mutex.rs - a local implementaion of mutex for dafu usage in multithreading env.
80//! Derived from rust's mutex implementation.
81//! - poison.rs - a mutex poison detection derived from rust's code.
82//! - query.rs - a file which contains main logic
83//! - query_polltaps.rs - a pairing of socket with request depending on config
84//! - query_private.rs - a sync realization of the common file
85//! - request.rs - a pre-defined requests
86//! - log.rs - a log facility
87//!
88//! Features:
89//! - feature = "use_async" for asynchronious code (use cdns_rs::a_sync::{...};)
90//! - feature = "use_sync" for synchronious code (use cdns_rs::sync::{...};)
91//! - feature = "no_error_output" does not output error
92//! - feature = "custom_error_output" outputs error to defined output
93//! `no_error_output` and `custom_error_output` can not be defined simultaniously.
94//!
95//! If both `no_error_output` and `custom_error_output` are not defined then the errors
96//! are output to stderr.
97//!
98//! All features can be used simultaniously.
99//! # Usage
100//!
101//! i.e
102//! cdns-rs = {version = "0.2", default-features = false, features = ["use_sync", "no_error_output"]}
103//!
104//! By default, both `use_async`, `use_sync`, `no_error_output` features are enabled.
105//!
106//! Example:
107//! ```Rust
108//!
109//! extern crate cdns_rs;
110//!
111//! use cdns_rs::{QType, QDnsQueriesRes, QDnsQuery, QuerySetup, DnsRdata};
112//! use cdns_rs::sync::QDns;
113//! use cdns_rs::sync::caches::CACHE;
114//!
115//! fn main() {
116//!
117//! println!("Hello, world!");
118//!
119//! let resolvers = CACHE.clone_resolve_list().unwrap();
120//!
121//! let mut q = QDns::make_empty(resolvers, 1, QuerySetup::default());
122//! q.add_request(QType::PTR, "127.0.0.1");
123//!
124//! let res = q.query().into_inner();
125//!
126//! if let Some(dq) = res
127//! {
128//! for d in dq
129//! {
130//! let resp = d.move_responses();
131//!
132//! for r in resp
133//! {
134//! match r.rdata
135//! {
136//! DnsRdata::PTR{fqdn} =>
137//! {
138//! println!("{}", fqdn);
139//! },
140//! _ => {}
141//! }
142//! }
143//! }
144//! }
145//!
146//! ```
147
148
149extern crate rand;
150#[macro_use] extern crate bitflags;
151extern crate nix;
152
153//extern crate indexmap;
154
155#[cfg(feature = "use_sync")]
156extern crate byteorder;
157#[cfg(feature = "use_sync")]
158extern crate crossbeam_utils;
159#[cfg(feature = "use_sync")]
160pub mod sync;
161
162#[cfg(feature = "use_async")]
163extern crate async_recursion;
164#[cfg(feature = "use_async")]
165extern crate tokio;
166#[cfg(feature = "use_async")]
167extern crate tokio_byteorder;
168#[cfg(feature = "use_async")]
169extern crate async_trait;
170#[cfg(feature = "use_async")]
171pub mod a_sync;
172
173pub mod cfg_host_parser;
174pub mod cfg_resolv_parser;
175
176mod query_private;
177
178pub mod query;
179pub mod common;
180mod portable;
181#[macro_use] pub mod error;
182mod tokenizer;
183
184pub use error::*;
185pub use common::{QType, DnsResponsePayload, DnsRdata, DnsSoa};
186pub use query::{QDnsQueriesRes, QDnsQuery, QuerySetup, QDnsQueryRec};