pub struct Channel { /* private fields */ }Expand description
A channel for name service lookups.
Implementations§
Source§impl Channel
impl Channel
Sourcepub fn as_raw(&self) -> ares_channel
pub fn as_raw(&self) -> ares_channel
Returns the raw ares_channel pointer.
§Safety
The returned pointer is only valid for the lifetime of this Channel.
Callers must ensure that any use of the pointer is compatible with
concurrent access (e.g. only call thread-safe c-ares functions).
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Create a new channel for name service lookups, with default Options.
§Examples
let channel = c_ares::Channel::new().unwrap();Sourcepub fn with_options(options: Options) -> Result<Channel>
pub fn with_options(options: Options) -> Result<Channel>
Create a new channel for name service lookups, with the given Options.
§Examples
let mut options = c_ares::Options::new();
options.set_flags(c_ares::Flags::STAYOPEN)
.set_tries(2);
let channel = c_ares::Channel::with_options(options).unwrap();Sourcepub fn reinit(&mut self) -> Result<&mut Self>
pub fn reinit(&mut self) -> Result<&mut Self>
Reinitialize a channel from system configuration.
Sourcepub fn process_fd(&mut self, read_fd: Option<Socket>, write_fd: Option<Socket>)
pub fn process_fd(&mut self, read_fd: Option<Socket>, write_fd: Option<Socket>)
Handle input, output, and timeout events associated with the specified file descriptors (sockets).
Providing a value for read_fd indicates that the identified socket is readable; likewise
providing a value for write_fd indicates that the identified socket is writable. Use
None for “no action”.
Sourcepub fn process(&mut self, read_fds: &mut fd_set, write_fds: &mut fd_set)
pub fn process(&mut self, read_fds: &mut fd_set, write_fds: &mut fd_set)
Handle input and output events associated with the specified file descriptors (sockets).
Also handles timeouts associated with the Channel.
Sourcepub fn process_fds(
&mut self,
events: &[FdEvents],
flags: ProcessFlags,
) -> Result<()>
pub fn process_fds( &mut self, events: &[FdEvents], flags: ProcessFlags, ) -> Result<()>
Process events on multiple file descriptors based on the event mask associated with each
file descriptor. Recommended over calling process_fd() multiple times since it would
trigger additional logic such as timeout processing on each call.
Sourcepub fn sockets(&self) -> Sockets
pub fn sockets(&self) -> Sockets
Retrieve the set of socket descriptors which the calling application should wait on for reading and / or writing.
§Examples
let channel = c_ares::Channel::new().unwrap();
for (socket, readable, writable) in &channel.sockets() {
println!("socket {socket}: read={readable}, write={writable}");
}Sourcepub fn fds(&self, read_fds: &mut fd_set, write_fds: &mut fd_set) -> u32
pub fn fds(&self, read_fds: &mut fd_set, write_fds: &mut fd_set) -> u32
Retrieve the set of socket descriptors which the calling application should wait on for reading and / or writing.
Sourcepub fn set_servers<I, S>(&mut self, servers: I) -> Result<&mut Self>
pub fn set_servers<I, S>(&mut self, servers: I) -> Result<&mut Self>
Set the list of servers to contact, instead of the servers specified in resolv.conf or the local named.
String format is host[:port]. IPv6 addresses with ports require square brackets eg
[2001:4860:4860::8888]:53.
§Examples
let mut channel = c_ares::Channel::new().unwrap();
channel.set_servers(&["8.8.8.8", "8.8.4.4:53"]).unwrap();Sourcepub fn servers(&self) -> Vec<String>
pub fn servers(&self) -> Vec<String>
Retrieves the list of configured servers.
Each entry is in host[:port] format, matching what set_servers
accepts.
Sourcepub fn set_local_ipv4(&mut self, ipv4: Ipv4Addr) -> &mut Self
pub fn set_local_ipv4(&mut self, ipv4: Ipv4Addr) -> &mut Self
Set the local IPv4 address from which to make queries.
Sourcepub fn set_local_ipv6(&mut self, ipv6: Ipv6Addr) -> &mut Self
pub fn set_local_ipv6(&mut self, ipv6: Ipv6Addr) -> &mut Self
Set the local IPv6 address from which to make queries.
Sourcepub fn set_local_device(&mut self, device: &str) -> Result<&mut Self>
pub fn set_local_device(&mut self, device: &str) -> Result<&mut Self>
Set the local device from which to make queries.
Sourcepub fn set_sortlist<I, S>(&mut self, sortlist: I) -> Result<&mut Self>
pub fn set_sortlist<I, S>(&mut self, sortlist: I) -> Result<&mut Self>
Initializes an address sortlist configuration, so that addresses returned by
get_host_by_name() are sorted according to the sortlist.
Each element of the sortlist holds an IP-address/netmask pair. The netmask is optional but follows the address after a slash if present. For example: “130.155.160.0/255.255.240.0”, or “130.155.0.0”.
Sourcepub fn set_server_state_callback<F>(&mut self, callback: F) -> &mut Self
pub fn set_server_state_callback<F>(&mut self, callback: F) -> &mut Self
Set a callback function to be invoked whenever a query on the channel completes.
callback(server, success, flags) will be called when a query completes.
serverindicates the DNS server that was used for the query.successindicates whether the query succeeded or not.flagsis a bitmask of flags describing various aspects of the query.
Sourcepub fn set_pending_write_callback<F>(&mut self, callback: F) -> &mut Self
pub fn set_pending_write_callback<F>(&mut self, callback: F) -> &mut Self
Set a callback function to be invoked when there is potential pending data which needs to be written.
Sourcepub fn query_a<F>(&mut self, name: &str, handler: F)
pub fn query_a<F>(&mut self, name: &str, handler: F)
Initiate a single-question DNS query for the A records associated with name.
On completion, handler is called with the result.
Sourcepub fn search_a<F>(&mut self, name: &str, handler: F)
pub fn search_a<F>(&mut self, name: &str, handler: F)
Initiate a series of single-question DNS queries for the A records associated with name.
On completion, handler is called with the result.
Sourcepub fn query_aaaa<F>(&mut self, name: &str, handler: F)
pub fn query_aaaa<F>(&mut self, name: &str, handler: F)
Initiate a single-question DNS query for the AAAA records associated with name.
On completion, handler is called with the result.
Sourcepub fn search_aaaa<F>(&mut self, name: &str, handler: F)
pub fn search_aaaa<F>(&mut self, name: &str, handler: F)
Initiate a series of single-question DNS queries for the AAAA records associated with
name.
On completion, handler is called with the result.
Sourcepub fn query_caa<F>(&mut self, name: &str, handler: F)
pub fn query_caa<F>(&mut self, name: &str, handler: F)
Initiate a single-question DNS query for the CAA records associated with name.
On completion, handler is called with the result.
Sourcepub fn search_caa<F>(&mut self, name: &str, handler: F)
pub fn search_caa<F>(&mut self, name: &str, handler: F)
Initiate a series of single-question DNS queries for the CAA records associated with
name.
On completion, handler is called with the result.
Sourcepub fn query_cname<F>(&mut self, name: &str, handler: F)
pub fn query_cname<F>(&mut self, name: &str, handler: F)
Initiate a single-question DNS query for the CNAME records associated with name.
On completion, handler is called with the result.
Sourcepub fn search_cname<F>(&mut self, name: &str, handler: F)
pub fn search_cname<F>(&mut self, name: &str, handler: F)
Initiate a series of single-question DNS queries for the CNAME records associated with
name.
On completion, handler is called with the result.
Sourcepub fn query_mx<F>(&mut self, name: &str, handler: F)
pub fn query_mx<F>(&mut self, name: &str, handler: F)
Initiate a single-question DNS query for the MX records associated with name.
On completion, handler is called with the result.
Sourcepub fn search_mx<F>(&mut self, name: &str, handler: F)
pub fn search_mx<F>(&mut self, name: &str, handler: F)
Initiate a series of single-question DNS queries for the MX records associated with name.
On completion, handler is called with the result.
Sourcepub fn query_naptr<F>(&mut self, name: &str, handler: F)
pub fn query_naptr<F>(&mut self, name: &str, handler: F)
Initiate a single-question DNS query for the NAPTR records associated with name.
On completion, handler is called with the result.
Sourcepub fn search_naptr<F>(&mut self, name: &str, handler: F)
pub fn search_naptr<F>(&mut self, name: &str, handler: F)
Initiate a series of single-question DNS queries for the NAPTR records associated with
name.
On completion, handler is called with the result.
Sourcepub fn query_ns<F>(&mut self, name: &str, handler: F)
pub fn query_ns<F>(&mut self, name: &str, handler: F)
Initiate a single-question DNS query for the NS records associated with name.
On completion, handler is called with the result.
Sourcepub fn search_ns<F>(&mut self, name: &str, handler: F)
pub fn search_ns<F>(&mut self, name: &str, handler: F)
Initiate a series of single-question DNS queries for the NS records associated with name.
On completion, handler is called with the result.
Sourcepub fn query_ptr<F>(&mut self, name: &str, handler: F)
pub fn query_ptr<F>(&mut self, name: &str, handler: F)
Initiate a single-question DNS query for the PTR records associated with name.
On completion, handler is called with the result.
Sourcepub fn search_ptr<F>(&mut self, name: &str, handler: F)
pub fn search_ptr<F>(&mut self, name: &str, handler: F)
Initiate a series of single-question DNS queries for the PTR records associated with
name.
On completion, handler is called with the result.
Sourcepub fn query_soa<F>(&mut self, name: &str, handler: F)
pub fn query_soa<F>(&mut self, name: &str, handler: F)
Initiate a single-question DNS query for the SOA records associated with name.
On completion, handler is called with the result.
Sourcepub fn search_soa<F>(&mut self, name: &str, handler: F)
pub fn search_soa<F>(&mut self, name: &str, handler: F)
Initiate a series of single-question DNS queries for the SOA records associated with
name.
On completion, handler is called with the result.
Sourcepub fn query_srv<F>(&mut self, name: &str, handler: F)
pub fn query_srv<F>(&mut self, name: &str, handler: F)
Initiate a single-question DNS query for the SRV records associated with name.
On completion, handler is called with the result.
Sourcepub fn search_srv<F>(&mut self, name: &str, handler: F)
pub fn search_srv<F>(&mut self, name: &str, handler: F)
Initiate a series of single-question DNS queries for the SRV records associated with
name.
On completion, handler is called with the result.
Sourcepub fn query_txt<F>(&mut self, name: &str, handler: F)
pub fn query_txt<F>(&mut self, name: &str, handler: F)
Initiate a single-question DNS query for the TXT records associated with name.
On completion, handler is called with the result.
Sourcepub fn search_txt<F>(&mut self, name: &str, handler: F)
pub fn search_txt<F>(&mut self, name: &str, handler: F)
Initiate a series of single-question DNS queries for the TXT records associated with
name.
On completion, handler is called with the result.
Sourcepub fn query_uri<F>(&mut self, name: &str, handler: F)
pub fn query_uri<F>(&mut self, name: &str, handler: F)
Initiate a single-question DNS query for the URI records associated with name.
On completion, handler is called with the result.
Sourcepub fn search_uri<F>(&mut self, name: &str, handler: F)
pub fn search_uri<F>(&mut self, name: &str, handler: F)
Initiate a series of single-question DNS queries for the URI records associated with
name.
On completion, handler is called with the result.
Sourcepub fn get_host_by_address<F>(&mut self, address: &IpAddr, handler: F)
pub fn get_host_by_address<F>(&mut self, address: &IpAddr, handler: F)
Perform a host query by address.
On completion, handler is called with the result.
Sourcepub fn get_host_by_name<F>(
&mut self,
name: &str,
family: AddressFamily,
handler: F,
)
pub fn get_host_by_name<F>( &mut self, name: &str, family: AddressFamily, handler: F, )
Perform a host query by name.
On completion, handler is called with the result.
Sourcepub fn get_name_info<F>(
&mut self,
address: &SocketAddr,
flags: NIFlags,
handler: F,
)
pub fn get_name_info<F>( &mut self, address: &SocketAddr, flags: NIFlags, handler: F, )
Address-to-nodename translation in protocol-independent manner.
The valid values for flags are documented here.
On completion, handler is called with the result.
Sourcepub fn get_addrinfo<F>(
&mut self,
name: &str,
service: Option<&str>,
hints: &AddrInfoHints,
handler: F,
)
pub fn get_addrinfo<F>( &mut self, name: &str, service: Option<&str>, hints: &AddrInfoHints, handler: F, )
Initiate a host query by name and service.
The hints parameter controls the desired address family, socket type, protocol, and
behaviour flags.
On completion, handler is called with the result.
Sourcepub fn query<F>(
&mut self,
name: &str,
dns_class: u16,
query_type: u16,
handler: F,
)
pub fn query<F>( &mut self, name: &str, dns_class: u16, query_type: u16, handler: F, )
Initiate a single-question DNS query for name. The class and type of the query are per
the provided parameters, taking values as defined in arpa/nameser.h.
On completion, handler is called with the result.
This method is provided so that users can query DNS types for which c-ares does not
provide a parser. This is expected to be a last resort; if a suitable query_xxx() is
available, that should be preferred.
Sourcepub fn search<F>(
&mut self,
name: &str,
dns_class: u16,
query_type: u16,
handler: F,
)
pub fn search<F>( &mut self, name: &str, dns_class: u16, query_type: u16, handler: F, )
Initiate a series of single-question DNS queries for name. The class and type of the
query are per the provided parameters, taking values as defined in arpa/nameser.h.
On completion, handler is called with the result.
This method is provided so that users can search DNS types for which c-ares does not
provide a parser. This is expected to be a last resort; if a suitable search_xxx() is
available, that should be preferred.
Sourcepub fn send_dnsrec<F>(&mut self, dnsrec: &DnsRecord, handler: F) -> Result<u16>
pub fn send_dnsrec<F>(&mut self, dnsrec: &DnsRecord, handler: F) -> Result<u16>
Send a DNS query using a pre-built DnsRecord.
On completion, handler is called with a Result<DnsRecord> containing
the parsed response.
Returns the query ID on success.
§Examples
use c_ares::*;
let mut channel = Channel::new().unwrap();
let mut query = DnsRecord::new(0, DnsFlags::RD, DnsOpcode::Query, DnsRcode::NoError).unwrap();
query.query_add("example.com", DnsRecordType::A, DnsCls::IN).unwrap();
channel.send_dnsrec(&query, move |result| {
let record = result.unwrap();
for rr in record.rrs(DnsSection::Answer) {
if let Some(addr) = rr.get_addr(DnsRrKey::A_ADDR) {
println!("address: {addr}");
}
}
}).unwrap();
// ... drive the event loop ...Sourcepub fn query_dnsrec<F>(
&mut self,
name: &str,
dns_class: DnsCls,
query_type: DnsRecordType,
handler: F,
) -> Result<u16>
pub fn query_dnsrec<F>( &mut self, name: &str, dns_class: DnsCls, query_type: DnsRecordType, handler: F, ) -> Result<u16>
Initiate a DNS query for name with the given class and type, receiving
a parsed DnsRecord in the callback.
Returns the query ID on success.
§Examples
use c_ares::{Channel, DnsCls, DnsRecordType, DnsRrKey, DnsSection};
let mut channel = Channel::new().unwrap();
channel.query_dnsrec(
"example.com",
DnsCls::IN,
DnsRecordType::A,
move |result| {
let record = result.unwrap();
for rr in record.rrs(DnsSection::Answer) {
if let Some(addr) = rr.get_addr(DnsRrKey::A_ADDR) {
println!("address: {addr}");
}
}
},
).unwrap();
// ... drive the event loop with channel.sockets() / channel.process_fd() ...Sourcepub fn search_dnsrec<F>(&mut self, dnsrec: &DnsRecord, handler: F) -> Result<()>
pub fn search_dnsrec<F>(&mut self, dnsrec: &DnsRecord, handler: F) -> Result<()>
Initiate a series of DNS queries using a pre-built DnsRecord,
receiving a parsed DnsRecord in the callback.
§Examples
use c_ares::*;
let mut channel = Channel::new().unwrap();
let mut query = DnsRecord::new(0, DnsFlags::RD, DnsOpcode::Query, DnsRcode::NoError).unwrap();
query.query_add("example.com", DnsRecordType::A, DnsCls::IN).unwrap();
channel.search_dnsrec(&query, move |result| {
let record = result.unwrap();
for rr in record.rrs(DnsSection::Answer) {
if let Some(addr) = rr.get_addr(DnsRrKey::A_ADDR) {
println!("address: {addr}");
}
}
}).unwrap();
// ... drive the event loop ...Sourcepub fn cancel(&mut self)
pub fn cancel(&mut self)
Cancel all requests made on this Channel.
Callbacks will be invoked for each pending query, passing a result
Err(Error::ECANCELLED).
Sourcepub fn process_pending_write(&mut self)
pub fn process_pending_write(&mut self)
Kick c-ares to process a pending write.
Sourcepub fn timeout(&self, max_timeout: Option<Duration>) -> Option<Duration>
pub fn timeout(&self, max_timeout: Option<Duration>) -> Option<Duration>
Return the maximum time to wait before processing timeouts.
If there are pending queries, returns the time until the next timeout
fires, optionally capped at max_timeout. If there are no pending
queries, returns max_timeout (which may be None).
Sourcepub fn queue_wait_empty(&self, timeout: Option<Duration>) -> Result<()>
pub fn queue_wait_empty(&self, timeout: Option<Duration>) -> Result<()>
Block until notified that there are no longer any queries in queue, or the specified timeout has expired.
Pass None to wait indefinitely.
Sourcepub fn queue_active_queries(&self) -> usize
pub fn queue_active_queries(&self) -> usize
Retrieve the total number of active queries pending answers from servers.
Some c-ares requests may spawn multiple queries, such as
get_addrinfo() when using AddressFamily::UNSPEC, which will be
reflected in this number.
Trait Implementations§
impl Send for Channel
impl Sync for Channel
Auto Trait Implementations§
impl !RefUnwindSafe for Channel
impl !UnwindSafe for Channel
impl Freeze for Channel
impl Unpin for Channel
impl UnsafeUnpin for Channel
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more