Skip to content

Commit

Permalink
Merge branch 'refactor' of https://github.com/niclabs/dns-rust into r…
Browse files Browse the repository at this point in the history
…efactor
  • Loading branch information
justRkive committed Nov 2, 2023
2 parents 57555ca + 755883c commit 4b192e1
Showing 1 changed file with 16 additions and 40 deletions.
56 changes: 16 additions & 40 deletions src/resolver/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,17 @@ impl Future for LookupIpFutureStub{
type Output = Result<DnsMessage, ResolverError>;

fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
println!("[POLL FUTURE]");

let query = self.query_answer.lock().unwrap().as_mut().poll(cx) ;

match query {
Poll::Pending => {
println!(" [return pending]");
return Poll::Pending;
},
Poll::Ready(Err(_)) => {
println!(" [ready empty]");
self.waker = Some(cx.waker().clone());

let referenced_query = Arc::clone(&self.query_answer); //same as self.query.clone()
let referenced_query = Arc::clone(&self.query_answer);
tokio::spawn(
lookup_stub(
self.name.clone(),
Expand All @@ -54,11 +51,10 @@ impl Future for LookupIpFutureStub{
self.waker.clone(),
referenced_query,
self.config.clone()));
println!(" [return pending]");

return Poll::Pending;
},
Poll::Ready(Ok(ip_addr)) => {
println!(" [return ready]");
return Poll::Ready(Ok(ip_addr));
}
}
Expand All @@ -72,7 +68,6 @@ impl LookupIpFutureStub {
config: ResolverConfig,
cache: DnsCache
) -> Self {
println!("[LOOKUP CREATE FUTURE]");

Self {
name: name,
Expand All @@ -93,7 +88,6 @@ pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y
referenced_query:Arc<std::sync::Mutex<Pin<Box<dyn futures_util::Future<Output = Result<DnsMessage, ResolverError>> + Send>>>>,
config: ResolverConfig,
) {
println!("[LOOKUP STUB]");

// Create random generator
let mut rng = thread_rng();
Expand All @@ -112,15 +106,14 @@ pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y
);

if let Some(cache_lookup) = cache.get(name.clone(), Rtype::A) {
println!("[LOOKUP STUB] cached data {:?}",cache_lookup);

//Add Answer
// Add Answer
let answer: Vec<ResourceRecord> = cache_lookup
.iter()
.map(|rr_cache_value| rr_cache_value.get_resource_record())
.collect::<Vec<ResourceRecord>>();
new_query.set_answer(answer);
// return Ok(new_query);

}

// Create Server failure query
Expand All @@ -130,10 +123,8 @@ pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y
let mut retry_count = 0;

for (conn_udp,conn_tcp) in name_servers.iter() {
println!("[LOOKUP STUB] retry {}",retry_count);

if retry_count > config.get_retry() {
println!("[LOOKUP STUB] max tries");
break;
}

Expand Down Expand Up @@ -163,26 +154,23 @@ pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y
}
}
_ => continue,
}
}

retry_count = retry_count + 1;
}

// Wake up task
if let Some(waker) = waker {
println!(" [wake up task]");
waker.wake();
}
let mut future_query = referenced_query.lock().unwrap();
*future_query = future::ready(Ok(response.clone())).boxed();
println!("[save answer]")

}


#[cfg(test)]
mod async_resolver_test {
use crate::dns_cache::cache_data;
// use tokio::runtime::Runtime;
use crate::message::rdata::a_rdata::ARdata;
use crate::message::rdata::Rdata;
Expand Down Expand Up @@ -221,40 +209,28 @@ mod async_resolver_test {

// TODO: test poll (not shure)

// TODO: lookup_stub test

// TODO: lookup_stub test save query in lookup struct

// TODO: lookup_stub numer of retries

#[ignore]
#[tokio::test]
async fn lookup_stub_max_tries(){

let domain_name = DomainName::new_from_string("example.com".to_string());
let timeout = Duration::from_secs(2);

let mut config: ResolverConfig = ResolverConfig::default();
let non_existent_server:IpAddr = IpAddr::V4(Ipv4Addr::new(44, 44, 1, 81));

let conn_udp:ClientUDPConnection = ClientUDPConnection::new(non_existent_server, timeout);
let conn_tcp:ClientTCPConnection = ClientTCPConnection::new(non_existent_server, timeout);
config.set_name_servers(vec![(conn_udp,conn_tcp)]);
config.set_retry(1);

let cache = DnsCache::new();
let waker = None;

let query = Arc::new(Mutex::new(future::err(ResolverError::Message("Empty")).boxed()));

// Create vector of name servers
let non_existente_server:IpAddr = IpAddr::V4(Ipv4Addr::new(234,1 ,4, 44));
let timeout: Duration = Duration::from_secs(20);

let conn_udp:ClientUDPConnection = ClientUDPConnection::new(non_existente_server, timeout);
let conn_tcp:ClientTCPConnection = ClientTCPConnection::new(non_existente_server, timeout);

let name_servers = vec![(conn_udp,conn_tcp)];

lookup_stub(domain_name, cache, name_servers, waker,query,config).await;

//FIXME: is not working

let response_future = LookupIpFutureStub::lookup(domain_name, config, cache).await;
println!("response_future {:?}",response_future);

assert_eq!(response_future.is_ok(), true);
assert_eq!(response_future.unwrap().get_header().get_ancount(), 0);
// assert_eq!(response_future.unwrap().get_header().get_rcode() , 2); //FIXME:
}


Expand Down

0 comments on commit 4b192e1

Please sign in to comment.