diff --git a/src/a.rs b/src/a.rs index d619e3e5f..2aff56c73 100644 --- a/src/a.rs +++ b/src/a.rs @@ -9,10 +9,8 @@ use std::ptr; use std::slice; use std::str; -use types::{ - AresError, - hostent, -}; +use error::AresError; +use types::hostent; use utils::ares_error; /// The result of a successful A lookup. diff --git a/src/aaaa.rs b/src/aaaa.rs index 4a126d088..9c09d39f1 100644 --- a/src/aaaa.rs +++ b/src/aaaa.rs @@ -9,10 +9,8 @@ use std::ptr; use std::slice; use std::str; -use types::{ - AresError, - hostent, -}; +use error::AresError; +use types::hostent; use utils::ares_error; /// The result of a successful AAAA lookup. diff --git a/src/channel.rs b/src/channel.rs index 9fe86ca6b..52ad53ba8 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -24,6 +24,7 @@ use cname::{ CNameResult, query_cname_callback, }; +use error::AresError; use flags::Flags; use host::{ HostResults, @@ -56,7 +57,6 @@ use srv::{ }; use types::{ AddressFamily, - AresError, DnsClass, IpAddr, QueryType, diff --git a/src/cname.rs b/src/cname.rs index 03ff7a6d4..b55c7f0b2 100644 --- a/src/cname.rs +++ b/src/cname.rs @@ -8,10 +8,8 @@ use std::ptr; use std::slice; use std::str; -use types::{ - AresError, - hostent, -}; +use error::AresError; +use types::hostent; use utils::ares_error; /// The result of a successful CNAME lookup. diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 000000000..cf59391cf --- /dev/null +++ b/src/error.rs @@ -0,0 +1,133 @@ +extern crate c_ares_sys; +extern crate libc; + +use std::error; +use std::ffi::CStr; +use std::fmt::{ + Display, + Error, + Formatter, +}; +use std::str; + +/// Error codes that the library might return. +#[derive(Debug, Clone, Copy)] +pub enum AresError { + /// DNS server returned answer with no data. + ENODATA = c_ares_sys::ARES_ENODATA as isize, + + /// DNS server claims query was misformatted. + EFORMERR = c_ares_sys::ARES_EFORMERR as isize, + + /// DNS server returned general failure. + ESERVFAIL = c_ares_sys::ARES_ESERVFAIL as isize, + + /// Domain name not found. + ENOTFOUND = c_ares_sys::ARES_ENOTFOUND as isize, + + /// DNS server does not implement requested operation. + ENOTIMP = c_ares_sys::ARES_ENOTIMP as isize, + + /// DNS server refused query. + EREFUSED = c_ares_sys::ARES_EREFUSED as isize, + + /// Misformatted DNS query. + EBADQUERY = c_ares_sys::ARES_EBADQUERY as isize, + + /// Misformatted domain name. + EBADNAME = c_ares_sys::ARES_EBADNAME as isize, + + /// Unsupported address family. + EBADFAMILY = c_ares_sys::ARES_EBADFAMILY as isize, + + /// Misformatted DNS reply. + EBADRESP = c_ares_sys::ARES_EBADRESP as isize, + + /// Could not contact DNS servers. + ECONNREFUSED = c_ares_sys::ARES_ECONNREFUSED as isize, + + /// Timeout while contacting DNS servers. + ETIMEOUT = c_ares_sys::ARES_ETIMEOUT as isize, + + /// End of file. + EOF = c_ares_sys::ARES_EOF as isize, + + /// Error reading file. + EFILE = c_ares_sys::ARES_EFILE as isize, + + /// Out of memory. + ENOMEM = c_ares_sys::ARES_ENOMEM as isize, + + /// Channel is being destroyed. + EDESTRUCTION = c_ares_sys::ARES_EDESTRUCTION as isize, + + /// Misformatted string. + EBADSTR = c_ares_sys::ARES_EBADSTR as isize, + + /// Illegal flags specified. + EBADFLAGS = c_ares_sys::ARES_EBADFLAGS as isize, + + /// Given hostname is not numeric. + ENONAME = c_ares_sys::ARES_ENONAME as isize, + + /// Illegal hints flags specified. + EBADHINTS = c_ares_sys::ARES_EBADHINTS as isize, + + /// c-ares library initialization not yet performed. + ENOTINITIALIZED = c_ares_sys::ARES_ENOTINITIALIZED as isize, + + /// Error loading iphlpapi.dll. + ELOADIPHLPAPI = c_ares_sys::ARES_ELOADIPHLPAPI as isize, + + /// Could not find GetNetworkParams function. + EADDRGETNETWORKPARAMS = c_ares_sys::ARES_EADDRGETNETWORKPARAMS as isize, + + /// DNS query cancelled. + ECANCELLED = c_ares_sys::ARES_ECANCELLED as isize, + + /// Unknown error. + UNKNOWN, +} + +impl Display for AresError { + fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> { + let text = match *self { + AresError::ENODATA => "ENODATA", + AresError::EFORMERR => "EFORMERR", + AresError::ESERVFAIL => "ESERVFAIL", + AresError::ENOTFOUND => "ENOTFOUND", + AresError::ENOTIMP => "ENOTIMP", + AresError::EREFUSED => "EREFUSED", + AresError::EBADQUERY => "EBADQUERY", + AresError::EBADNAME => "EBADNAME", + AresError::EBADFAMILY => "EBADFAMILY", + AresError::EBADRESP => "EBADRESP", + AresError::ECONNREFUSED => "ECONNREFUSED", + AresError::ETIMEOUT => "ETIMEOUT", + AresError::EOF => "EOF", + AresError::EFILE => "EFILE", + AresError::ENOMEM => "ENOMEM", + AresError::EDESTRUCTION => "EDESTRUCTION", + AresError::EBADSTR => "EBADSTR", + AresError::EBADFLAGS => "EBADFLAGS", + AresError::ENONAME => "ENONAME", + AresError::EBADHINTS => "EBADHINTS", + AresError::ENOTINITIALIZED => "ENOTINITIALIZED", + AresError::ELOADIPHLPAPI => "ELOADIPHLPAPI", + AresError::EADDRGETNETWORKPARAMS => "EADDRGETNETWORKPARAMS", + AresError::ECANCELLED => "ECANCELLED", + AresError::UNKNOWN => "UNKNOWN", + }; + formatter.write_str(text) + } +} + +impl error::Error for AresError { + fn description(&self) -> &str { + unsafe { + let ptr = c_ares_sys::ares_strerror(*self as libc::c_int); + let buf = CStr::from_ptr(ptr).to_bytes(); + str::from_utf8_unchecked(buf) + } + } +} diff --git a/src/host.rs b/src/host.rs index 66a29ec37..a9f2c675c 100644 --- a/src/host.rs +++ b/src/host.rs @@ -11,9 +11,9 @@ use std::net::{ use std::ptr; use std::str; +use error::AresError; use types::{ AddressFamily, - AresError, hostent, IpAddr, }; diff --git a/src/lib.rs b/src/lib.rs index 384cecb5f..1fe9df2e2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,6 +30,7 @@ mod aaaa; mod srv; mod channel; mod cname; +mod error; pub mod flags; mod host; mod mx; @@ -62,6 +63,7 @@ pub use channel::{ Options, }; pub use cname::CNameResult; +pub use error::AresError; pub use host::{ HostAddressResult, HostAliasResult, @@ -86,7 +88,6 @@ pub use ptr::{ }; pub use types::{ AddressFamily, - AresError, INVALID_FD, IpAddr, }; diff --git a/src/mx.rs b/src/mx.rs index d1f917416..50e8134b8 100644 --- a/src/mx.rs +++ b/src/mx.rs @@ -8,7 +8,7 @@ use std::ptr; use std::slice; use std::str; -use types::AresError; +use error::AresError; use utils::ares_error; /// The result of a successful MX lookup. diff --git a/src/nameinfo.rs b/src/nameinfo.rs index 206048d99..f3110f2c0 100644 --- a/src/nameinfo.rs +++ b/src/nameinfo.rs @@ -6,7 +6,7 @@ use std::marker::PhantomData; use std::mem; use std::str; -use types::AresError; +use error::AresError; use utils::ares_error; /// The result of a successful name-info lookup. diff --git a/src/naptr.rs b/src/naptr.rs index a7d42cbe2..312fb3799 100644 --- a/src/naptr.rs +++ b/src/naptr.rs @@ -8,7 +8,7 @@ use std::str; use std::ptr; use std::slice; -use types::AresError; +use error::AresError; use utils::ares_error; /// The result of a successful NAPTR lookup. diff --git a/src/ns.rs b/src/ns.rs index 9d8334b26..16a664a45 100644 --- a/src/ns.rs +++ b/src/ns.rs @@ -8,10 +8,8 @@ use std::ptr; use std::slice; use std::str; -use types::{ - AresError, - hostent, -}; +use error::AresError; +use types::hostent; use utils::ares_error; /// The result of a successful NS lookup. diff --git a/src/ptr.rs b/src/ptr.rs index debf7a65d..b38452541 100644 --- a/src/ptr.rs +++ b/src/ptr.rs @@ -8,10 +8,8 @@ use std::ptr; use std::slice; use std::str; -use types::{ - AresError, - hostent, -}; +use error::AresError; +use types::hostent; use utils::ares_error; /// The result of a successful PTR lookup. diff --git a/src/soa.rs b/src/soa.rs index 6781be10c..4d0cb9d12 100644 --- a/src/soa.rs +++ b/src/soa.rs @@ -8,7 +8,7 @@ use std::ptr; use std::slice; use std::str; -use types::AresError ; +use error::AresError ; use utils::ares_error; /// The result of a successful SOA lookup. diff --git a/src/srv.rs b/src/srv.rs index 07273d6a5..b2f0dcaaf 100644 --- a/src/srv.rs +++ b/src/srv.rs @@ -8,7 +8,7 @@ use std::str; use std::ptr; use std::slice; -use types::AresError; +use error::AresError; use utils::ares_error; /// The result of a successful SRV lookup. diff --git a/src/txt.rs b/src/txt.rs index 3433105d7..dc35d2b42 100644 --- a/src/txt.rs +++ b/src/txt.rs @@ -8,7 +8,7 @@ use std::ptr; use std::slice; use std::str; -use types::AresError; +use error::AresError; use utils::ares_error; /// The result of a successful TXT lookup. diff --git a/src/types.rs b/src/types.rs index 12397c01d..0f9a31605 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,146 +1,16 @@ extern crate c_ares_sys; extern crate libc; -use std::error; -use std::ffi::CStr; -use std::fmt::{ - Display, - Error, - Formatter, -}; use std::net::{ Ipv4Addr, Ipv6Addr, }; use std::os::unix::io; -use std::str; /// An invalid file descriptor. Use this to represent 'no action' when calling /// `process_fd()` on a channel. pub const INVALID_FD: io::RawFd = c_ares_sys::ARES_SOCKET_BAD as io::RawFd; -/// Error codes that the library might return. -#[derive(Debug, Clone, Copy)] -pub enum AresError { - /// DNS server returned answer with no data. - ENODATA = c_ares_sys::ARES_ENODATA as isize, - - /// DNS server claims query was misformatted. - EFORMERR = c_ares_sys::ARES_EFORMERR as isize, - - /// DNS server returned general failure. - ESERVFAIL = c_ares_sys::ARES_ESERVFAIL as isize, - - /// Domain name not found. - ENOTFOUND = c_ares_sys::ARES_ENOTFOUND as isize, - - /// DNS server does not implement requested operation. - ENOTIMP = c_ares_sys::ARES_ENOTIMP as isize, - - /// DNS server refused query. - EREFUSED = c_ares_sys::ARES_EREFUSED as isize, - - /// Misformatted DNS query. - EBADQUERY = c_ares_sys::ARES_EBADQUERY as isize, - - /// Misformatted domain name. - EBADNAME = c_ares_sys::ARES_EBADNAME as isize, - - /// Unsupported address family. - EBADFAMILY = c_ares_sys::ARES_EBADFAMILY as isize, - - /// Misformatted DNS reply. - EBADRESP = c_ares_sys::ARES_EBADRESP as isize, - - /// Could not contact DNS servers. - ECONNREFUSED = c_ares_sys::ARES_ECONNREFUSED as isize, - - /// Timeout while contacting DNS servers. - ETIMEOUT = c_ares_sys::ARES_ETIMEOUT as isize, - - /// End of file. - EOF = c_ares_sys::ARES_EOF as isize, - - /// Error reading file. - EFILE = c_ares_sys::ARES_EFILE as isize, - - /// Out of memory. - ENOMEM = c_ares_sys::ARES_ENOMEM as isize, - - /// Channel is being destroyed. - EDESTRUCTION = c_ares_sys::ARES_EDESTRUCTION as isize, - - /// Misformatted string. - EBADSTR = c_ares_sys::ARES_EBADSTR as isize, - - /// Illegal flags specified. - EBADFLAGS = c_ares_sys::ARES_EBADFLAGS as isize, - - /// Given hostname is not numeric. - ENONAME = c_ares_sys::ARES_ENONAME as isize, - - /// Illegal hints flags specified. - EBADHINTS = c_ares_sys::ARES_EBADHINTS as isize, - - /// c-ares library initialization not yet performed. - ENOTINITIALIZED = c_ares_sys::ARES_ENOTINITIALIZED as isize, - - /// Error loading iphlpapi.dll. - ELOADIPHLPAPI = c_ares_sys::ARES_ELOADIPHLPAPI as isize, - - /// Could not find GetNetworkParams function. - EADDRGETNETWORKPARAMS = c_ares_sys::ARES_EADDRGETNETWORKPARAMS as isize, - - /// DNS query cancelled. - ECANCELLED = c_ares_sys::ARES_ECANCELLED as isize, - - /// Unknown error. - UNKNOWN, -} - -impl Display for AresError { - fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> { - let text = match *self { - AresError::ENODATA => "ENODATA", - AresError::EFORMERR => "EFORMERR", - AresError::ESERVFAIL => "ESERVFAIL", - AresError::ENOTFOUND => "ENOTFOUND", - AresError::ENOTIMP => "ENOTIMP", - AresError::EREFUSED => "EREFUSED", - AresError::EBADQUERY => "EBADQUERY", - AresError::EBADNAME => "EBADNAME", - AresError::EBADFAMILY => "EBADFAMILY", - AresError::EBADRESP => "EBADRESP", - AresError::ECONNREFUSED => "ECONNREFUSED", - AresError::ETIMEOUT => "ETIMEOUT", - AresError::EOF => "EOF", - AresError::EFILE => "EFILE", - AresError::ENOMEM => "ENOMEM", - AresError::EDESTRUCTION => "EDESTRUCTION", - AresError::EBADSTR => "EBADSTR", - AresError::EBADFLAGS => "EBADFLAGS", - AresError::ENONAME => "ENONAME", - AresError::EBADHINTS => "EBADHINTS", - AresError::ENOTINITIALIZED => "ENOTINITIALIZED", - AresError::ELOADIPHLPAPI => "ELOADIPHLPAPI", - AresError::EADDRGETNETWORKPARAMS => "EADDRGETNETWORKPARAMS", - AresError::ECANCELLED => "ECANCELLED", - AresError::UNKNOWN => "UNKNOWN", - }; - formatter.write_str(text) - } -} - -impl error::Error for AresError { - fn description(&self) -> &str { - unsafe { - let ptr = c_ares_sys::ares_strerror(*self as libc::c_int); - let buf = CStr::from_ptr(ptr).to_bytes(); - str::from_utf8_unchecked(buf) - } - } -} - /// Address families. #[derive(Debug, Clone, Copy)] pub enum AddressFamily { diff --git a/src/utils.rs b/src/utils.rs index 09b5a9c43..1ff6a45ad 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,10 +1,8 @@ extern crate c_ares_sys; extern crate libc; -use types::{ - AddressFamily, - AresError, -}; +use error::AresError; +use types::AddressFamily; use std::mem; use std::net::{ Ipv4Addr,