Skip to content

Commit

Permalink
implement get_servers()
Browse files Browse the repository at this point in the history
  • Loading branch information
dimbleby committed May 25, 2024
1 parent 0b6189b commit 85daef3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ fn main() {
println!("cargo:rustc-cfg=cares1_23");
}

println!("cargo::rustc-check-cfg=cfg(cares1_24)");
if version >= 0x1_18_00 {
println!("cargo:rustc-cfg=cares1_24");
}

println!("cargo::rustc-check-cfg=cfg(cares1_29)");
if version >= 0x1_1d_00 {
println!("cargo:rustc-cfg=cares1_29");
Expand Down
9 changes: 9 additions & 0 deletions src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use crate::ptr::{query_ptr_callback, PTRResults};
use crate::query::query_callback;
use crate::soa::{query_soa_callback, SOAResult};
use crate::srv::{query_srv_callback, SRVResults};
#[cfg(cares1_24)]
use crate::string::CAresString;
use crate::txt::{query_txt_callback, TXTResults};
use crate::types::{AddressFamily, DnsClass, QueryType, Socket};
use crate::uri::{query_uri_callback, URIResults};
Expand Down Expand Up @@ -479,6 +481,13 @@ impl Channel {
}
}

/// Retrieves the list of servers in comma delimited format.
#[cfg(cares1_24)]
pub fn get_servers(&self) -> CAresString {
let servers = unsafe { c_ares_sys::ares_get_servers_csv(self.ares_channel) };
CAresString::new(servers)
}

/// Set the local IPv4 address from which to make queries.
pub fn set_local_ipv4(&mut self, ipv4: Ipv4Addr) -> &mut Self {
unsafe { c_ares_sys::ares_set_local_ip4(self.ares_channel, u32::from(ipv4)) }
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ mod query;
mod server_state_flags;
mod soa;
mod srv;
mod string;
mod txt;
mod types;
mod uri;
Expand Down Expand Up @@ -89,6 +90,7 @@ pub use crate::ptr::PTRResults;
pub use crate::server_state_flags::ServerStateFlags;
pub use crate::soa::SOAResult;
pub use crate::srv::{SRVResult, SRVResults, SRVResultsIter};
pub use crate::string::CAresString;
pub use crate::txt::{TXTResult, TXTResults, TXTResultsIter};
pub use crate::types::{AddressFamily, Socket, SOCKET_BAD};
pub use crate::uri::{URIResult, URIResults, URIResultsIter};
Expand Down
34 changes: 34 additions & 0 deletions src/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::ffi::CStr;
use std::ops::Deref;
use std::os::raw::c_char;

/// A smart pointer wrapping a string as allocated by c-ares.
pub struct CAresString {
ares_string: *mut c_char,
rust_str: &'static str,
}

impl CAresString {
#[allow(dead_code)]
pub(crate) fn new(ares_string: *mut c_char) -> Self {
let c_str = unsafe { CStr::from_ptr(ares_string) };
let rust_str = c_str.to_str().unwrap();
CAresString {
ares_string,
rust_str,
}
}
}

impl Deref for CAresString {
type Target = str;
fn deref(&self) -> &str {
self.rust_str
}
}

impl Drop for CAresString {
fn drop(&mut self) {
unsafe { c_ares_sys::ares_free_string(self.ares_string.cast()) }
}
}

0 comments on commit 85daef3

Please sign in to comment.