Daniel Sanchez 0333d3256c
Dns discovery (#15)
* Updated vendor source

* Added dns discovery method

* Use latest Go in check workflow

* Use host for nameserver

* Use reference instead of move Host
2022-10-25 11:25:23 +02:00

48 lines
1.5 KiB
Rust

// std
use std::ffi::{CStr, CString};
use std::time::Duration;
// crates
use multiaddr::Multiaddr;
use url::{Host, Url};
// internal
use crate::general::JsonResponse;
use crate::Result;
/// RetrieveNodes returns a list of multiaddress given a url to a DNS discoverable ENR tree.
/// The nameserver can optionally be specified to resolve the enrtree url. Otherwise uses the default system dns.
pub fn waku_dns_discovery(
url: &Url,
server: Option<&Host>,
timeout: Option<Duration>,
) -> Result<Vec<Multiaddr>> {
let result = unsafe {
CStr::from_ptr(waku_sys::waku_dns_discovery(
CString::new(url.to_string())
.expect("CString should build properly from a valid Url")
.into_raw(),
CString::new(
server
.map(|host| host.to_string())
.unwrap_or_else(|| "".to_string()),
)
.expect("CString should build properly from a String nameserver")
.into_raw(),
timeout
.map(|timeout| {
timeout
.as_millis()
.try_into()
.expect("Duration as milliseconds should fit in a i32")
})
.unwrap_or(0),
))
}
.to_str()
.expect("Response should always succeed to load to a &str");
let response: JsonResponse<Vec<Multiaddr>> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
response.into()
}