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
//! The crate simply parses `/etc/resolv.conf` file and creates a config object //! //! # Examples //! //! ## Parsing a config from a string //! ```rust //! extern crate resolv_conf; //! //! use std::net::{Ipv4Addr, Ipv6Addr}; //! use resolv_conf::{ScopedIp, Config, Network}; //! //! fn main() { //! let config_str = " //! options ndots:8 timeout:8 attempts:8 //! //! domain example.com //! search example.com sub.example.com //! //! nameserver 2001:4860:4860::8888 //! nameserver 2001:4860:4860::8844 //! nameserver 8.8.8.8 //! nameserver 8.8.4.4 //! //! options rotate //! options inet6 no-tld-query //! //! sortlist 130.155.160.0/255.255.240.0 130.155.0.0"; //! //! // Parse the config. //! let parsed_config = Config::parse(&config_str).expect("Failed to parse config"); //! //! // We can build configs manually as well, either directly or with Config::new() //! let expected_config = Config { //! nameservers: vec![ //! ScopedIp::V6(Ipv6Addr::new(0x2001, 0x4860, 0x4860, 0, 0, 0, 0, 0x8888), None), //! ScopedIp::V6(Ipv6Addr::new(0x2001, 0x4860, 0x4860, 0, 0, 0, 0, 0x8844), None), //! ScopedIp::V4(Ipv4Addr::new(8, 8, 8, 8)), //! ScopedIp::V4(Ipv4Addr::new(8, 8, 4, 4)), //! ], //! search: vec![String::from("example.com"), String::from("sub.example.com")], //! sortlist: vec![ //! Network::V4(Ipv4Addr::new(130, 155, 160, 0), Ipv4Addr::new(255, 255, 240, 0)), //! Network::V4(Ipv4Addr::new(130, 155, 0, 0), Ipv4Addr::new(255, 255, 0, 0)), //! ], //! debug: false, //! ndots: 8, //! timeout: 8, //! attempts: 8, //! rotate: true, //! no_check_names: false, //! inet6: true, //! ip6_bytestring: false, //! ip6_dotint: false, //! edns0: false, //! single_request: false, //! single_request_reopen: false, //! no_tld_query: true, //! use_vc: false, //! }; //! //! // We can compare configurations, since resolv_conf::Config implements Eq //! assert_eq!(parsed_config, expected_config); //! } //! ``` //! //! ## Parsing a file //! //! ```rust //! use std::io::Read; //! use std::fs::File; //! //! extern crate resolv_conf; //! //! fn main() { //! // Read the file //! let mut buf = Vec::with_capacity(4096); //! let mut f = File::open("/etc/resolv.conf").unwrap(); //! f.read_to_end(&mut buf).unwrap(); //! //! // Parse the buffer //! let cfg = resolv_conf::Config::parse(&buf).unwrap(); //! //! // Print the config //! println!("---- Parsed /etc/resolv.conf -----\n{:#?}\n", cfg); //! } //! ``` #![warn(missing_debug_implementations)] #![warn(missing_docs)] #[macro_use] extern crate quick_error; mod grammar; mod ip; mod config; pub use grammar::ParseError; pub use ip::{AddrParseError, ScopedIp, Network}; pub use config::Config;