diff --git a/src/resolver/lookup.rs b/src/resolver/lookup.rs index a59e6a6b..88efb092 100644 --- a/src/resolver/lookup.rs +++ b/src/resolver/lookup.rs @@ -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> + Send>>>>, cache: DnsCache, - conn: ClientConnectionType, + name_servers: Vec<(ClientUDPConnection, ClientTCPConnection)>, waker: Option, } @@ -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; }, @@ -61,7 +64,7 @@ impl LookupIpFutureStub{ pub fn lookup( name: DomainName, cache:DnsCache, - conn: ClientConnectionType, + name_servers: Vec<(ClientUDPConnection, ClientTCPConnection)>, ) -> Self { println!("[LOOKUP CREATE FUTURE]"); @@ -69,7 +72,7 @@ impl LookupIpFutureStub{ 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, } @@ -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, referenced_query:Arc> + Send>>>>, ) { @@ -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 = 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 { @@ -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); }