resolv_conf/
lib.rs

1//! The crate simply parses `/etc/resolv.conf` file and creates a config object
2//!
3//! # Examples
4//!
5//! ## Parsing a config from a string
6//! ```rust
7//! extern crate resolv_conf;
8//!
9//! use std::net::{Ipv4Addr, Ipv6Addr};
10//! use resolv_conf::{ScopedIp, Config, Network};
11//!
12//! fn main() {
13//!     let config_str = "
14//! options ndots:8 timeout:8 attempts:8
15//!
16//! domain example.com
17//! search example.com sub.example.com
18//!
19//! nameserver 2001:4860:4860::8888
20//! nameserver 2001:4860:4860::8844
21//! nameserver 8.8.8.8
22//! nameserver 8.8.4.4
23//!
24//! options rotate
25//! options inet6 no-tld-query
26//!
27//! sortlist 130.155.160.0/255.255.240.0 130.155.0.0";
28//!
29//!     // Parse the config.
30//!     let parsed_config = Config::parse(&config_str).expect("Failed to parse config");
31//!
32//!     // We can build configs manually as well, either directly or with Config::new()
33//!     let mut expected_config = Config::new();
34//!     expected_config.nameservers = vec![
35//!         ScopedIp::V6(Ipv6Addr::new(0x2001, 0x4860, 0x4860, 0, 0, 0, 0, 0x8888), None),
36//!         ScopedIp::V6(Ipv6Addr::new(0x2001, 0x4860, 0x4860, 0, 0, 0, 0, 0x8844), None),
37//!         ScopedIp::V4(Ipv4Addr::new(8, 8, 8, 8)),
38//!         ScopedIp::V4(Ipv4Addr::new(8, 8, 4, 4)),
39//!     ];
40//!     expected_config.sortlist = vec![
41//!         Network::V4(Ipv4Addr::new(130, 155, 160, 0), Ipv4Addr::new(255, 255, 240, 0)),
42//!         Network::V4(Ipv4Addr::new(130, 155, 0, 0), Ipv4Addr::new(255, 255, 0, 0)),
43//!     ];
44//!     expected_config.debug = false;
45//!     expected_config.ndots = 8;
46//!     expected_config.timeout = 8;
47//!     expected_config.attempts = 8;
48//!     expected_config.rotate = true;
49//!     expected_config.no_check_names = false;
50//!     expected_config.inet6 = true;
51//!     expected_config.ip6_bytestring = false;
52//!     expected_config.ip6_dotint = false;
53//!     expected_config.edns0 = false;
54//!     expected_config.single_request = false;
55//!     expected_config.single_request_reopen = false;
56//!     expected_config.no_tld_query = true;
57//!     expected_config.use_vc = false;
58//!     expected_config.set_domain(String::from("example.com"));
59//!     expected_config.set_search(vec![
60//!         String::from("example.com"),
61//!         String::from("sub.example.com")
62//!     ]);
63//!
64//!     // We can compare configurations, since resolv_conf::Config implements Eq
65//!     assert_eq!(parsed_config, expected_config);
66//! }
67//! ```
68//!
69//! ## Parsing a file
70//!
71//! ```rust
72//! use std::io::Read;
73//! use std::fs::File;
74//!
75//! extern crate resolv_conf;
76//!
77//! fn main() {
78//!     // Read the file
79//!     let mut buf = Vec::with_capacity(4096);
80//!     let mut f = File::open("/etc/resolv.conf").unwrap();
81//!     f.read_to_end(&mut buf).unwrap();
82//!
83//!     // Parse the buffer
84//!     let cfg = resolv_conf::Config::parse(&buf).unwrap();
85//!
86//!     // Print the config
87//!     println!("---- Parsed /etc/resolv.conf -----\n{:#?}\n", cfg);
88//! }
89//! ```
90
91#![warn(missing_debug_implementations)]
92#![warn(missing_docs)]
93
94#[macro_use]
95extern crate quick_error;
96#[cfg(feature = "system")]
97extern crate hostname;
98
99mod grammar;
100mod ip;
101mod config;
102
103pub use grammar::ParseError;
104pub use ip::{AddrParseError, Network, ScopedIp};
105pub use config::{Config, DomainIter, Lookup, Family};