Skip to content

Commit

Permalink
loop send udp query
Browse files Browse the repository at this point in the history
  • Loading branch information
valesteban committed Oct 26, 2023
1 parent 8d97b47 commit ba0e871
Showing 1 changed file with 35 additions and 34 deletions.
69 changes: 35 additions & 34 deletions src/resolver/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ use futures_util::{future::Future,future};
use super::resolver_error::ResolverError;
use std::sync:: {Mutex,Arc};
use crate::client::client_connection::ClientConnectionType;

use crate::client::udp_connection::ClientUDPConnection;
use crate::client::tcp_connection::ClientTCPConnection;
//Future returned fron AsyncResolver when performing a lookup with rtype A
pub struct LookupIpFutureStub {
name: DomainName, // cache: DnsCache,
query_answer: Arc<std::sync::Mutex<Pin<Box<dyn futures_util::Future<Output = Result<DnsMessage, ResolverError>> + Send>>>>,
cache: DnsCache,
conn: ClientConnectionType,
name_servers: Vec<(ClientUDPConnection, ClientTCPConnection)>,
waker: Option<Waker>,
}

Expand All @@ -44,7 +47,7 @@ impl Future for LookupIpFutureStub{

let referenced_query = Arc::clone(&self.query_answer); //same as self.query.clone()
tokio::spawn(
lookup_stub(self.name.clone(),self.cache.clone(),self.conn.clone(),self.waker.clone(),referenced_query));
lookup_stub(self.name.clone(),self.cache.clone(),self.name_servers.clone(),self.waker.clone(),referenced_query));
println!(" [return pending]");
return Poll::Pending;
},
Expand All @@ -61,15 +64,15 @@ impl LookupIpFutureStub{
pub fn lookup(
name: DomainName,
cache:DnsCache,
conn: ClientConnectionType,
name_servers: Vec<(ClientUDPConnection, ClientTCPConnection)>,
) -> Self {
println!("[LOOKUP CREATE FUTURE]");

Self {
name: name,
query_answer: Arc::new(Mutex::new(future::err(ResolverError::Message("Empty")).boxed())), //FIXME: cambiar a otro tipo el error/inicio
cache: cache,
conn: conn,
name_servers: name_servers,
waker: None,
}

Expand All @@ -79,7 +82,7 @@ impl LookupIpFutureStub{
pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y que se le pase ahi un parametro strategy que diga si son los pasos o si funciona como stub
name: DomainName,
mut cache: DnsCache,
conn: ClientConnectionType,
name_servers: Vec<(ClientUDPConnection, ClientTCPConnection)>,
waker: Option<Waker>,
referenced_query:Arc<std::sync::Mutex<Pin<Box<dyn futures_util::Future<Output = Result<DnsMessage, ResolverError>> + Send>>>>,
) {
Expand Down Expand Up @@ -113,32 +116,27 @@ pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y
// return Ok(new_query);
}

// Create Server failure query //FIXME:
let mut response = new_query.clone().to_owned();
response.get_header().set_rcode(2);

//loop
for (conn_udp,conn_tcp) in name_servers.iter() {

let result_response = conn_udp.send(new_query.clone());
match result_response {
Ok(response_ok) => {
response = response_ok;
break;
},
Err(_) => (),
};
}

println!("[] {:?}",response);

//FIXME:
let response_result: Result<DnsMessage, ResolverError> = match conn {
ClientConnectionType::TCP(client_conn) => {
match client_conn.send(new_query) {
Err(_) => Err(ResolverError::Message("Error: Receiving DNS message")),
Ok(val) => {
Ok(val)
},
}
}

ClientConnectionType::UDP(client_conn) => {
match client_conn.send(new_query) {
Err(_) => Err(ResolverError::Message("Error: Receiving DNS message")),
Ok(val) => {
Ok(val)},
}
}
};
println!("[] {:?}",response_result);

let mut future_query = referenced_query.lock().unwrap();
*future_query = future::ready(response_result).boxed(); // TODO: check if it workingas expected
// let mut future_query = referenced_query.lock().unwrap();
// *future_query = future::ready(response_result).boxed(); // TODO: check if it workingas expected

//wake up task
if let Some(waker) = waker {
Expand Down Expand Up @@ -169,14 +167,17 @@ mod async_resolver_test {
let cache = DnsCache::new();
let waker = None;

let google_server: IpAddr = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8));
let timeout: Duration = Duration::from_secs(20);

let client_udp = ClientUDPConnection::new(google_server, timeout);
let conn = ClientConnectionType::UDP(client_udp);
let query = Arc::new(Mutex::new(future::err(ResolverError::Message("Empty")).boxed()));

// Create vect of name servers
let google_server:IpAddr = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8));
let timeout: Duration = Duration::from_secs(20);

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

lookup_stub(name, cache, conn, waker,query).await;
let name_servers = vec![(conn_udp,conn_tcp)];
lookup_stub(name, cache, name_servers, waker,query).await;
// println!("[Test Result ] {:?}", result);
}

Expand Down

0 comments on commit ba0e871

Please sign in to comment.