From d51524752bbe1679e8f9ad043f336288a5364149 Mon Sep 17 00:00:00 2001 From: valesteban Date: Mon, 27 Nov 2023 19:49:17 -0300 Subject: [PATCH 001/137] add test with qtype CH --- src/async_resolver.rs | 25 +++++++++++++++++++++++++ src/async_resolver/lookup.rs | 22 ++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index 4d3fbc6f..1f7ed9c2 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -410,6 +410,31 @@ mod async_resolver_test { assert!(!ip_addresses[0].is_unspecified()); } + #[tokio::test] + async fn lookup_ip_ch() { + let mut resolver = AsyncResolver::new(ResolverConfig::default()); + let domain_name = "example.com"; + let transport_protocol = "UDP"; + let qclass = "CH"; + let ip_addresses = resolver.lookup_ip(domain_name, transport_protocol,qclass).await; + println!("RESPONSE : {:?}", ip_addresses); + + assert!(ip_addresses.is_err()); + } + + #[tokio::test] + async fn lookup_ch() { + let mut resolver = AsyncResolver::new(ResolverConfig::default()); + let domain_name = "example.com"; + let transport_protocol = "UDP"; + let qtype = "NS"; + let qclass = "CH"; + let ip_addresses = resolver.lookup(domain_name, transport_protocol,qtype,qclass).await; + println!("RESPONSE : {:?}", ip_addresses); + + assert!(ip_addresses.is_err()); + } + #[tokio::test] async fn host_name_to_host_address_translation_ch() { let mut resolver = AsyncResolver::new(ResolverConfig::default()); diff --git a/src/async_resolver/lookup.rs b/src/async_resolver/lookup.rs index 5f9f7f29..5c5c4b60 100644 --- a/src/async_resolver/lookup.rs +++ b/src/async_resolver/lookup.rs @@ -363,6 +363,28 @@ mod async_resolver_test { } + #[tokio::test] + async fn lookup_stub_ch_response() { + let domain_name = DomainName::new_from_string("example.com".to_string()); + let waker = None; + let query = Arc::new(Mutex::new(future::err(ResolverError::EmptyQuery).boxed())); + + 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); + + let config = ResolverConfig::default(); + let record_type = Qtype::A; + let record_class = Qclass::CH; + let name_servers = vec![(conn_udp,conn_tcp)]; + let response = lookup_stub(domain_name,record_type,record_class, name_servers, waker,query,config).await.unwrap(); + + + assert_eq!(response.get_header().get_qr(),true); + assert_ne!(response.get_answer().len(),0); + } #[tokio::test] //se cae, y debería caerse, pero se cae con todos los max retiries y no solo con 0 //FIXME: async fn lookup_stub_max_tries_0() { From 68d6b742a2959eebb07ab892eb07ad7cd4092af5 Mon Sep 17 00:00:00 2001 From: valesteban Date: Tue, 28 Nov 2023 12:18:31 -0300 Subject: [PATCH 002/137] add test to fix --- src/async_resolver.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index 1f7ed9c2..4bc39049 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -93,7 +93,7 @@ impl AsyncResolver { self.config.set_protocol(transport_protocol_struct); let response = self.inner_lookup(domain_name_struct,Qtype::A,Qclass::from_str_to_qclass(qclass)).await; - + let result_rrs = self.parse_dns_msg(response); if let Ok(rrs) = result_rrs { let rrs_iter = rrs.into_iter(); @@ -422,6 +422,19 @@ mod async_resolver_test { assert!(ip_addresses.is_err()); } + #[ignore] //FIXME: + #[tokio::test] + async fn lookup_ip_qclass_any() { + let mut resolver = AsyncResolver::new(ResolverConfig::default()); + let domain_name = "example.com"; + let transport_protocol = "UDP"; + let qclass = "ANY"; + let ip_addresses = resolver.lookup_ip(domain_name, transport_protocol,qclass).await; + println!("RESPONSE : {:?}", ip_addresses); + + // assert!(ip_addresses.is_err()); + } + #[tokio::test] async fn lookup_ch() { let mut resolver = AsyncResolver::new(ResolverConfig::default()); From 18124abf9fc765b77ccc1ee4ed15b561cde414be Mon Sep 17 00:00:00 2001 From: valesteban Date: Tue, 28 Nov 2023 12:24:24 -0300 Subject: [PATCH 003/137] create unimplemented function for saving in cache negative answers --- src/async_resolver.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index 4bc39049..bffdedbc 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -325,11 +325,27 @@ impl AsyncResolver { self.cache.add(rr.get_name(), rr.clone()); } }); + } } + /// [RFC 1123]: https://datatracker.ietf.org/doc/html/rfc1123#section-6.1.2.2 + /// + /// 6.1.3.3 Efficient Resource Usage + /// + /// (4) All DNS name servers and resolvers SHOULD cache + /// negative responses that indicate the specified name, or + /// data of the specified type, does not exist, as + /// described in [DNS:2]. + /// + /// Stores the data of negative answers in the cache. + fn save_negative_answers(&mut self){ + unimplemented!(); + } } + + // Getters impl AsyncResolver { // Gets the cache from the struct From 773f9acec80ca733af8f05c425531c2d16ff0d18 Mon Sep 17 00:00:00 2001 From: valesteban Date: Tue, 28 Nov 2023 12:34:27 -0300 Subject: [PATCH 004/137] create test for negative answer function --- src/async_resolver.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index bffdedbc..0de79647 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -339,8 +339,15 @@ impl AsyncResolver { /// described in [DNS:2]. /// /// Stores the data of negative answers in the cache. - fn save_negative_answers(&mut self){ - unimplemented!(); + fn save_negative_answers(&mut self, response: DnsMessage){ + println!("[Save negative answers]"); + let qname = response.get_question().get_qname(); + let qtype = response.get_question().get_qtype(); + let qclass = response.get_question().get_qclass(); + println!("Qname: {:?} {:?} {:?}", qname,qtype,qclass); + + // self.cache.add(...); + } } @@ -1608,4 +1615,28 @@ mod async_resolver_test { assert_eq!(resolver.get_cache().get_size(), 2); } + #[test] + fn save_cache_negative_answer(){ + let mut resolver = AsyncResolver::new(ResolverConfig::default()); + resolver.cache.set_max_size(1); + + let domain_name = DomainName::new_from_string("example.com".to_string()); + + // Create dns response + let dns_response = + DnsMessage::new_query_message( + domain_name, + Qtype::A, + Qclass::IN, + 0, + false, + 1); + + resolver.save_negative_answers(dns_response.clone()); + + assert_eq!(dns_response.get_answer().len(), 0); + // assert_eq!(resolver.get_cache().get_size(), 1); + + } + } \ No newline at end of file From f006375f67472aab786d2ff3b45c3d6868ac0b3b Mon Sep 17 00:00:00 2001 From: valesteban Date: Tue, 28 Nov 2023 15:36:27 -0300 Subject: [PATCH 005/137] fix test in lookup --- src/async_resolver/lookup.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/async_resolver/lookup.rs b/src/async_resolver/lookup.rs index 5c5c4b60..3eb10615 100644 --- a/src/async_resolver/lookup.rs +++ b/src/async_resolver/lookup.rs @@ -383,7 +383,7 @@ mod async_resolver_test { assert_eq!(response.get_header().get_qr(),true); - assert_ne!(response.get_answer().len(),0); + assert_eq!(response.get_answer().len(),0); } #[tokio::test] //se cae, y debería caerse, pero se cae con todos los max retiries y no solo con 0 //FIXME: async fn lookup_stub_max_tries_0() { From ae5e35c017b703b4fb034374dc489138a842010d Mon Sep 17 00:00:00 2001 From: valesteban Date: Tue, 28 Nov 2023 15:37:36 -0300 Subject: [PATCH 006/137] progress in negative catching saves RR SOA if answer do not exist --- src/async_resolver.rs | 44 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index 0de79647..ef6c0f1f 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -15,6 +15,7 @@ use crate::message::class_qclass::Qclass; use crate::message::resource_record::ResourceRecord; use crate::async_resolver::{config::ResolverConfig,lookup::LookupFutureStub}; use crate::message::rdata::Rdata; +use crate::message::type_rtype::Rtype; use crate::client::client_connection::ConnectionProtocol; use crate::async_resolver::resolver_error::ResolverError; use crate:: message::type_qtype::Qtype; @@ -329,7 +330,7 @@ impl AsyncResolver { } } - /// [RFC 1123]: https://datatracker.ietf.org/doc/html/rfc1123#section-6.1.2.2 + /// [RFC 1123]: https://datatracker.ietf.org/doc/html/rfc1123#section-6.1.3.3 /// /// 6.1.3.3 Efficient Resource Usage /// @@ -338,13 +339,50 @@ impl AsyncResolver { /// data of the specified type, does not exist, as /// described in [DNS:2]. /// + /// [RFC 1034]: https://datatracker.ietf.org/doc/html/rfc1034#section-4.3.4 + /// + /// 4.3.4. Negative response caching (Optional) + /// + /// The DNS provides an optional service which allows name servers to + /// distribute, and resolvers to cache, negative results with TTLs. For + /// example, a name server can distribute a TTL along with a name error + /// indication, and a resolver receiving such information is allowed to + /// assume that the name does not exist during the TTL period without + /// consulting authoritative data. Similarly, a resolver can make a query + /// with a QTYPE which matches multiple types, and cache the fact that some + /// of the types are not present. + /// + /// The method is that a name server may add an SOA RR to the additional + /// section of a response when that response is authoritative. The SOA must + /// be that of the zone which was the source of the authoritative data in + /// the answer section, or name error if applicable. The MINIMUM field of + /// the SOA controls the length of time that the negative result may be + /// cached. + /// /// Stores the data of negative answers in the cache. fn save_negative_answers(&mut self, response: DnsMessage){ - println!("[Save negative answers]"); + //FIXME: + println!("[Save negative answers]"); let qname = response.get_question().get_qname(); let qtype = response.get_question().get_qtype(); let qclass = response.get_question().get_qclass(); println!("Qname: {:?} {:?} {:?}", qname,qtype,qclass); + + let additionals = response.get_additional(); + let answer = response.get_answer(); + let aa = response.get_header().get_aa(); + println!("Additionals len: {:?}", additionals.len()); + + // If not existence RR for query, add SOA to cache + if additionals.len() > 0 && answer.len() == 0 && aa == true{ + additionals.iter() + .for_each(|rr| { + if rr.get_rtype() == Rtype::SOA { + self.cache.add(qname.clone(), rr.clone()); + } + }); + println!("[Additional:] {:?}", additionals); + } // self.cache.add(...); @@ -1620,7 +1658,7 @@ mod async_resolver_test { let mut resolver = AsyncResolver::new(ResolverConfig::default()); resolver.cache.set_max_size(1); - let domain_name = DomainName::new_from_string("example.com".to_string()); + let domain_name = DomainName::new_from_string("nonexistent.uchile.cl".to_string()); // Create dns response let dns_response = From c6a3e1de09ef4fdaa278c5c42f239d237ad5b08e Mon Sep 17 00:00:00 2001 From: Ephy Date: Wed, 29 Nov 2023 03:20:06 -0300 Subject: [PATCH 007/137] add integration test section 6.2.1 - 6.2.2 --- tests/integration_test.rs | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 tests/integration_test.rs diff --git a/tests/integration_test.rs b/tests/integration_test.rs new file mode 100644 index 00000000..3cc81768 --- /dev/null +++ b/tests/integration_test.rs @@ -0,0 +1,53 @@ +use std::{net::IpAddr, str::FromStr}; + +use dns_rust::{async_resolver::{config::ResolverConfig, AsyncResolver, resolver_error::ResolverError}, message::{resource_record::ResourceRecord, rdata::Rdata}}; + + + +// TODO: Change params type to intoDomainName +async fn query_response(domain_name: &str, qtype: &str) -> Result, ResolverError>{ + + let config = ResolverConfig::default(); + let mut resolver = AsyncResolver::new(config); + + let response = resolver.lookup(domain_name, "UDP", qtype).await; + + response +} + +/// 6.2.1 Query test Qtype = A +#[tokio::test] +async fn query_a_type() { + let response = query_response("example.com", "A").await; + + if let Ok(rrs) = response { + assert_eq!(rrs.iter().count(), 1); + let rdata = rrs[0].get_rdata(); + if let Rdata::A(ip) = rdata { + assert_eq!(ip.get_address(), IpAddr::from_str("93.184.216.34").unwrap()); + } else { + panic!("Error parsing response"); + } + } else { + panic!("No response") + } +} + +/// 6.2.2 Query normal Qtype = * +#[tokio::test] +async fn query_all_type() { + let response = query_response("example.com", "ANY").await; + if let Ok(rrs) = response { + assert_eq!(rrs.iter().count(), 2); + } else { + panic!("No response") + } +} + +// TODO: 6.2.3 Query normal Qtype = MX + +// TODO: 6.2.4 Query normal Qtype = NS + +// TODO: 6.2.5 Dominio mal escrito Qtype = A + + From 7c936b5790bed38ff668dcda44712da24c383945 Mon Sep 17 00:00:00 2001 From: Ephy Date: Wed, 29 Nov 2023 03:25:28 -0300 Subject: [PATCH 008/137] update query response --- tests/integration_test.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 3cc81768..02605362 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -10,7 +10,11 @@ async fn query_response(domain_name: &str, qtype: &str) -> Result Date: Wed, 29 Nov 2023 15:41:53 -0300 Subject: [PATCH 009/137] fix bug in method add_additional --- src/message.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/message.rs b/src/message.rs index e5b79aca..18dea281 100644 --- a/src/message.rs +++ b/src/message.rs @@ -612,7 +612,7 @@ impl DnsMessage { let mut msg_additionals = self.get_additional(); msg_additionals.append(&mut additionals); - self.set_answer(msg_additionals); + self.set_additional(msg_additionals); } @@ -1330,7 +1330,7 @@ mod message_test { dns_query_message.add_additionals(new_additional); //since the new additional is added to the answer lets check if something was added - assert_eq!(dns_query_message.get_answer().len(), 1); + assert_eq!(dns_query_message.get_additional().len(), 1); } //ToDo: Revisar @@ -1493,4 +1493,5 @@ mod message_test { let result = dns_query_message.check_op_code().unwrap_err(); assert_eq!(result, "IQuery not Implemented"); } + } From 760ab6d02fe1d2c1b0e23ab3d9e61ecca15a9f76 Mon Sep 17 00:00:00 2001 From: valesteban Date: Wed, 29 Nov 2023 15:44:51 -0300 Subject: [PATCH 010/137] add method save_negative_answers and test save_cache_negative_answer --- src/async_resolver.rs | 52 +++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index ef6c0f1f..d4ef7535 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -328,6 +328,8 @@ impl AsyncResolver { }); } + + self.save_negative_answers( response); } /// [RFC 1123]: https://datatracker.ietf.org/doc/html/rfc1123#section-6.1.3.3 @@ -361,17 +363,11 @@ impl AsyncResolver { /// /// Stores the data of negative answers in the cache. fn save_negative_answers(&mut self, response: DnsMessage){ - //FIXME: - println!("[Save negative answers]"); + let qname = response.get_question().get_qname(); - let qtype = response.get_question().get_qtype(); - let qclass = response.get_question().get_qclass(); - println!("Qname: {:?} {:?} {:?}", qname,qtype,qclass); - let additionals = response.get_additional(); let answer = response.get_answer(); let aa = response.get_header().get_aa(); - println!("Additionals len: {:?}", additionals.len()); // If not existence RR for query, add SOA to cache if additionals.len() > 0 && answer.len() == 0 && aa == true{ @@ -381,11 +377,8 @@ impl AsyncResolver { self.cache.add(qname.clone(), rr.clone()); } }); - println!("[Additional:] {:?}", additionals); } - // self.cache.add(...); - } } @@ -410,6 +403,7 @@ mod async_resolver_test { use crate::message::class_qclass::Qclass; use crate::message::rdata::Rdata; use crate::message::rdata::a_rdata::ARdata; + use crate::message::rdata::soa_rdata::SoaRdata; use crate::message::resource_record::ResourceRecord; use crate:: message::type_qtype::Qtype; use crate::message::type_rtype::Rtype; @@ -1658,10 +1652,32 @@ mod async_resolver_test { let mut resolver = AsyncResolver::new(ResolverConfig::default()); resolver.cache.set_max_size(1); - let domain_name = DomainName::new_from_string("nonexistent.uchile.cl".to_string()); + let domain_name = DomainName::new_from_string("banana.exaple".to_string()); + let mname = DomainName::new_from_string("a.root-servers.net.".to_string()); + let rname = DomainName::new_from_string("nstld.verisign-grs.com.".to_string()); + let serial = 2023112900; + let refresh = 1800; + let retry = 900; + let expire = 604800; + let minimum = 86400; + + //Create RR type SOA + let mut soa_rdata = SoaRdata::new(); + soa_rdata.set_mname(mname); + soa_rdata.set_rname(rname); + soa_rdata.set_serial(serial); + soa_rdata.set_refresh(refresh); + soa_rdata.set_retry(retry); + soa_rdata.set_expire(expire); + soa_rdata.set_minimum(minimum); + + + let rdata = Rdata::SOA(soa_rdata); + let mut rr = ResourceRecord::new(rdata); + rr.set_name(domain_name.clone()); // Create dns response - let dns_response = + let mut dns_response = DnsMessage::new_query_message( domain_name, Qtype::A, @@ -1669,11 +1685,19 @@ mod async_resolver_test { 0, false, 1); - + let mut new_header = dns_response.get_header(); + new_header.set_aa(true); + dns_response.set_header(new_header); + + // Save RR type SOA in Additional section of response + dns_response.add_additionals(vec![rr]); + resolver.save_negative_answers(dns_response.clone()); + // println!("[RRR] {:?}", dns_response); assert_eq!(dns_response.get_answer().len(), 0); - // assert_eq!(resolver.get_cache().get_size(), 1); + assert_eq!(dns_response.get_additional().len(), 1); + assert_eq!(resolver.get_cache().get_size(), 1); } From 9984d9ee96cccd6bfeba64a8c8aafde727abc118 Mon Sep 17 00:00:00 2001 From: Ephyy Date: Wed, 29 Nov 2023 15:56:56 -0300 Subject: [PATCH 011/137] change docker compose --- docker-compose.yml | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 40a2abde..9cdb0099 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,13 +1,7 @@ version: "4" services: - resolver: - build: . - image: dns-rust:latest - ports: - - "58396:58396" - network_mode: "dns" - client: - container_name: client + dns_rust: + container_name: dns_rust build: . image: dns-rust:latest test: From c34a30a69c74d308c77941fdb0b8e32b0264a28e Mon Sep 17 00:00:00 2001 From: Ephyy Date: Wed, 29 Nov 2023 15:57:20 -0300 Subject: [PATCH 012/137] update main client --- src/main.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index c2d4ec63..a68ffc04 100644 --- a/src/main.rs +++ b/src/main.rs @@ -99,9 +99,11 @@ pub async fn main() { let conn = ClientTCPConnection::new(addr.unwrap(), Duration::from_secs(10)); let mut client = Client::new(conn); - let mut _response = client.query(DomainName::new_from_string(client_args.host_name.clone()), client_args.qtype.as_str(), client_args.qclass.as_str()); + let response = client.query(DomainName::new_from_string(client_args.host_name.clone()), client_args.qtype.as_str(), client_args.qclass.as_str()); - //response.print_dns_message() + if let Ok(mut resp) = response { + resp.print_dns_message() + } } Commands::Resolver(resolver_args) => { From 842a73ec0cec4eb8c83618dbb3f2376d993073d3 Mon Sep 17 00:00:00 2001 From: valesteban Date: Wed, 29 Nov 2023 15:59:52 -0300 Subject: [PATCH 013/137] test get negative cached answer in inner_lookup --- src/async_resolver.rs | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index d4ef7535..cad3120d 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -1700,5 +1700,53 @@ mod async_resolver_test { assert_eq!(resolver.get_cache().get_size(), 1); } + + #[tokio::test] + async fn inner_lookup_negative_answer_in_cache(){ + let mut resolver = AsyncResolver::new(ResolverConfig::default()); + let mut cache = resolver.get_cache(); + cache.set_max_size(9); + + let domain_name = DomainName::new_from_string("banana.exaple".to_string()); + + //Create RR type SOA + let mname = DomainName::new_from_string("a.root-servers.net.".to_string()); + let rname = DomainName::new_from_string("nstld.verisign-grs.com.".to_string()); + let serial = 2023112900; + let refresh = 1800; + let retry = 900; + let expire = 604800; + let minimum = 86400; + + let mut soa_rdata = SoaRdata::new(); + soa_rdata.set_mname(mname); + soa_rdata.set_rname(rname); + soa_rdata.set_serial(serial); + soa_rdata.set_refresh(refresh); + soa_rdata.set_retry(retry); + soa_rdata.set_expire(expire); + soa_rdata.set_minimum(minimum); + + let rdata = Rdata::SOA(soa_rdata); + let mut rr = ResourceRecord::new(rdata); + rr.set_name(domain_name.clone()); + cache.add(domain_name.clone(), rr); + + // Add negative answer to cache + resolver.cache = cache; + + let qtype = Qtype::A; + let qclass = Qclass::IN; + let response = resolver.inner_lookup(domain_name,qtype,qclass).await; + + println!("RESULT {:?}", response); + // assert_eq!(resolver.get_cache().get_size(), 1); + // assert!(response.is_ok()); + // response.unwrap(); + // assert_eq!(response.get_answer().len(),0); + // assert_eq!(response.get_header().get_rcode(), Rcode::NXDomain); + + + } } \ No newline at end of file From bff69ed81670e2ff7623c212020206f1bb342b8d Mon Sep 17 00:00:00 2001 From: valesteban Date: Wed, 29 Nov 2023 16:30:25 -0300 Subject: [PATCH 014/137] add function for negative caching --- src/dns_cache.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/dns_cache.rs b/src/dns_cache.rs index 3dab01b6..3768d18e 100644 --- a/src/dns_cache.rs +++ b/src/dns_cache.rs @@ -59,6 +59,22 @@ impl DnsCache { self.set_size(self.get_size() + 1); } + /// Add negative resource record type SOA to cache for negative answers + pub fn add_negative_answer(&mut self, domain_name: DomainName, rtype: Rtype, resource_record:ResourceRecord) { + + // see cache space + if self.get_size() >= self.max_size { + self.remove_oldest_used(); + } + + let rr_cache = RRCache::new(resource_record); + let mut cache_data = self.get_cache(); + cache_data.add_to_cache_data(rtype, domain_name, rr_cache); + self.set_cache(cache_data); + self.set_size(self.get_size() + 1); + + } + /// Removes an element from cache pub fn remove(&mut self, domain_name: DomainName, rtype: Rtype) { let mut cache_data = self.get_cache(); From 159f829cd5f447ecb80f6be8c7302022a399de9c Mon Sep 17 00:00:00 2001 From: valesteban Date: Wed, 29 Nov 2023 16:31:23 -0300 Subject: [PATCH 015/137] fix save negative answer in cache saves SOA for qtupe in cache --- src/async_resolver.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index cad3120d..5f2e0ef5 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -365,6 +365,7 @@ impl AsyncResolver { fn save_negative_answers(&mut self, response: DnsMessage){ let qname = response.get_question().get_qname(); + let qtype = response.get_question().get_qtype(); let additionals = response.get_additional(); let answer = response.get_answer(); let aa = response.get_header().get_aa(); @@ -374,7 +375,8 @@ impl AsyncResolver { additionals.iter() .for_each(|rr| { if rr.get_rtype() == Rtype::SOA { - self.cache.add(qname.clone(), rr.clone()); + let rtype = Qtype::to_rtype(qtype); + self.cache.add_negative_answer(qname.clone(),rtype ,rr.clone()); } }); } @@ -1694,10 +1696,11 @@ mod async_resolver_test { resolver.save_negative_answers(dns_response.clone()); - // println!("[RRR] {:?}", dns_response); + let qtype_search = Rtype::A; assert_eq!(dns_response.get_answer().len(), 0); assert_eq!(dns_response.get_additional().len(), 1); assert_eq!(resolver.get_cache().get_size(), 1); + assert!(resolver.get_cache().get_cache().get_cache_data().get(&qtype_search).is_some()) } @@ -1745,8 +1748,7 @@ mod async_resolver_test { // response.unwrap(); // assert_eq!(response.get_answer().len(),0); // assert_eq!(response.get_header().get_rcode(), Rcode::NXDomain); - - + } } \ No newline at end of file From bb1d6ffcfb0068ea6cacc56524c2fd988807e8a5 Mon Sep 17 00:00:00 2001 From: valesteban Date: Wed, 29 Nov 2023 17:17:25 -0300 Subject: [PATCH 016/137] look negative answer in cache in inner_lookup if SOA rr is saved for other rtype in cache, corresponds to a negative caching --- src/async_resolver.rs | 59 ++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index 5f2e0ef5..4a36d367 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -1,4 +1,5 @@ use std::net::IpAddr; +use std::vec; pub mod config; pub mod lookup; @@ -167,9 +168,9 @@ impl AsyncResolver { // Cache lookup // Search in cache only if its available if self.config.is_cache_enabled() { - if let Some(cache_lookup) = self.cache.clone().get(domain_name.clone(), Qtype::to_rtype(qtype)) { - println!("[Cached Data]"); - + let rtype_saved = Qtype::to_rtype(qtype); + if let Some(cache_lookup) = self.cache.clone().get(domain_name.clone(), rtype_saved) { + // Create random generator let mut rng = thread_rng(); @@ -185,13 +186,27 @@ impl AsyncResolver { false, query_id ); - - // Add Answer - let answer: Vec = cache_lookup - .iter() - .map(|rr_cache_value| rr_cache_value.get_resource_record()) - .collect::>(); - new_query.set_answer(answer); + + // Get RR from cache + for rr_cache_value in cache_lookup.iter() { + let rr = rr_cache_value.get_resource_record(); + + // Get negative answer + if rtype_saved != rr.get_rtype() { + println!("ADD ADITIONAL NEGATIVE ANSWER SOA"); + let additionals: Vec = vec![rr]; + new_query.add_additionals(additionals); + let mut new_header = new_query.get_header(); + new_header.set_rcode(3); + new_query.set_header(new_header); + } + else { //FIXME: change to alg RFC 1034-1035 + println!("ADD ANSWER CACHE"); + let answer: Vec = vec![rr]; + new_query.set_answer(answer); + } + } + return Ok(new_query) } } @@ -1708,6 +1723,7 @@ mod async_resolver_test { async fn inner_lookup_negative_answer_in_cache(){ let mut resolver = AsyncResolver::new(ResolverConfig::default()); let mut cache = resolver.get_cache(); + let qtype = Qtype::A; cache.set_max_size(9); let domain_name = DomainName::new_from_string("banana.exaple".to_string()); @@ -1733,21 +1749,24 @@ mod async_resolver_test { let rdata = Rdata::SOA(soa_rdata); let mut rr = ResourceRecord::new(rdata); rr.set_name(domain_name.clone()); - cache.add(domain_name.clone(), rr); // Add negative answer to cache + let mut cache = resolver.get_cache(); + cache.set_max_size(9); + let rtype = Qtype::to_rtype(qtype); + cache.add_negative_answer(domain_name.clone(),rtype ,rr.clone()); resolver.cache = cache; - let qtype = Qtype::A; + assert_eq!(resolver.get_cache().get_size(), 1); + let qclass = Qclass::IN; - let response = resolver.inner_lookup(domain_name,qtype,qclass).await; - - println!("RESULT {:?}", response); - // assert_eq!(resolver.get_cache().get_size(), 1); - // assert!(response.is_ok()); - // response.unwrap(); - // assert_eq!(response.get_answer().len(),0); - // assert_eq!(response.get_header().get_rcode(), Rcode::NXDomain); + let response = resolver.inner_lookup(domain_name,qtype,qclass).await.unwrap(); + + + assert_eq!(resolver.get_cache().get_size(), 1); + assert_eq!(response.get_answer().len(), 0); + assert_eq!(response.get_additional().len(), 1); + assert_eq!(response.get_header().get_rcode(), 3); } From a9664f57de5caee095554bcba21883777e7c32e4 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Thu, 30 Nov 2023 19:41:54 -0300 Subject: [PATCH 017/137] docs for HostData --- src/dns_cache/cache_data/host_data.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/dns_cache/cache_data/host_data.rs b/src/dns_cache/cache_data/host_data.rs index 04170663..f3bad3f8 100644 --- a/src/dns_cache/cache_data/host_data.rs +++ b/src/dns_cache/cache_data/host_data.rs @@ -1,11 +1,14 @@ use chrono::{Utc, DateTime}; - use crate::{rr_cache::RRCache, domain_name::DomainName, message::rdata::Rdata}; use std::{collections::HashMap, net::IpAddr}; -///type to define the name of the host - ///struct to define the host data +/// This struct saves the data associated with a host in the cache. +/// +/// Given a single `DomainName`, it groups all data associated with it +/// a `Vec` inside a `HashMap>`. +/// This means, all the cache data associated with a single host +/// of an specific `Rtype`. #[derive(Clone, Debug)] pub struct HostData { pub host_hash: HashMap>, From ff20c216fb78cd1e189fd5594e2391097fc95e25 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Thu, 30 Nov 2023 19:42:14 -0300 Subject: [PATCH 018/137] style in docs --- src/dns_cache/cache_data/host_data.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/dns_cache/cache_data/host_data.rs b/src/dns_cache/cache_data/host_data.rs index f3bad3f8..3700c4e0 100644 --- a/src/dns_cache/cache_data/host_data.rs +++ b/src/dns_cache/cache_data/host_data.rs @@ -2,7 +2,6 @@ use chrono::{Utc, DateTime}; use crate::{rr_cache::RRCache, domain_name::DomainName, message::rdata::Rdata}; use std::{collections::HashMap, net::IpAddr}; -///struct to define the host data /// This struct saves the data associated with a host in the cache. /// /// Given a single `DomainName`, it groups all data associated with it From dd4c117ab5797beb0d51b0041e7e9778e10d9d45 Mon Sep 17 00:00:00 2001 From: Ephy Date: Mon, 4 Dec 2023 02:15:01 -0300 Subject: [PATCH 019/137] add ns query integration tests --- tests/integration_test.rs | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 02605362..7be8cc40 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -1,6 +1,6 @@ use std::{net::IpAddr, str::FromStr}; -use dns_rust::{async_resolver::{config::ResolverConfig, AsyncResolver, resolver_error::ResolverError}, message::{resource_record::ResourceRecord, rdata::Rdata}}; +use dns_rust::{async_resolver::{config::ResolverConfig, AsyncResolver, resolver_error::ResolverError}, message::{resource_record::ResourceRecord, rdata::Rdata}, domain_name::DomainName}; @@ -30,7 +30,7 @@ async fn query_a_type() { if let Rdata::A(ip) = rdata { assert_eq!(ip.get_address(), IpAddr::from_str("93.184.216.34").unwrap()); } else { - panic!("Error parsing response"); + panic!("No ip address"); } } else { panic!("No response") @@ -42,7 +42,7 @@ async fn query_a_type() { async fn query_all_type() { let response = query_response("example.com", "ANY").await; if let Ok(rrs) = response { - assert_eq!(rrs.iter().count(), 2); + assert_eq!(rrs.len(), 2); } else { panic!("No response") } @@ -50,7 +50,34 @@ async fn query_all_type() { // TODO: 6.2.3 Query normal Qtype = MX + // TODO: 6.2.4 Query normal Qtype = NS +#[tokio::test] +async fn query_ns_type() { + let response = query_response("example.com", "NS").await; + if let Ok(rrs) = response { + assert_eq!(rrs.len(), 2); + + if let Rdata::NS(ns1) = rrs[0].get_rdata() { + assert_eq!( + ns1.get_nsdname(), + DomainName::new_from_str("a.iana-servers.net")) + } else { + panic!("First record is not type NS"); + } + + if let Rdata::NS(ns) = rrs[1].get_rdata() { + assert_eq!( + ns.get_nsdname(), + DomainName::new_from_str("b.iana-servers.net")) + } else { + panic!("Second record is not type NS"); + } + + } else { + panic!("No response received") + } +} // TODO: 6.2.5 Dominio mal escrito Qtype = A From e84828d57a545082cfe7843b4c8dcf3bdb56abdc Mon Sep 17 00:00:00 2001 From: Ephyy Date: Mon, 4 Dec 2023 16:27:09 -0300 Subject: [PATCH 020/137] add no resource and mistyped name integration test --- tests/integration_test.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 7be8cc40..79c617a0 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -48,10 +48,13 @@ async fn query_all_type() { } } -// TODO: 6.2.3 Query normal Qtype = MX +// TODO: 6.2.3 Query Qtype = MX +async fn query_mx_type() { + unimplemented!(); +} -// TODO: 6.2.4 Query normal Qtype = NS +// 6.2.4 Query Qtype = NS #[tokio::test] async fn query_ns_type() { let response = query_response("example.com", "NS").await; @@ -79,6 +82,19 @@ async fn query_ns_type() { } } -// TODO: 6.2.5 Dominio mal escrito Qtype = A +/// 6.2.5 Mistyped host name Qtype = A +#[tokio::test] +async fn mistyped_host_name() { + let response = query_response("exampllee.com", "A").await; + assert!(response.is_err()); +} + +/// No record test +#[tokio::test] +async fn no_resource_available() { + let response = query_response("example.com", "CNAME").await; + assert!(response.is_err()); +} + From b56e489b13cbefa2040abef0d895f71bec3be259 Mon Sep 17 00:00:00 2001 From: Ephyy Date: Mon, 4 Dec 2023 16:32:46 -0300 Subject: [PATCH 021/137] add mx query integration test --- tests/integration_test.rs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 79c617a0..d7cb9cb1 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -48,9 +48,30 @@ async fn query_all_type() { } } -// TODO: 6.2.3 Query Qtype = MX +/// 6.2.3 Query Qtype = MX +#[tokio::test] async fn query_mx_type() { - unimplemented!(); + let response = query_response("example.com", "MX").await; + + if let Ok(rrs) = response { + assert_eq!(rrs.len(), 1); + + if let Rdata::MX(mxdata) = rrs[0].get_rdata() { + assert_eq!( + mxdata.get_exchange(), + DomainName::new_from_str("")); + + assert_eq!( + mxdata.get_preference(), + 0 + ) + } else { + panic!("Record is not MX type"); + } + + } else { + panic!("No response received") + } } @@ -66,7 +87,7 @@ async fn query_ns_type() { ns1.get_nsdname(), DomainName::new_from_str("a.iana-servers.net")) } else { - panic!("First record is not type NS"); + panic!("First record is not NS"); } if let Rdata::NS(ns) = rrs[1].get_rdata() { @@ -74,7 +95,7 @@ async fn query_ns_type() { ns.get_nsdname(), DomainName::new_from_str("b.iana-servers.net")) } else { - panic!("Second record is not type NS"); + panic!("Second record is not NS"); } } else { From 66c04f8c8cb6dbc601d4bfc58a0f47a409f3568e Mon Sep 17 00:00:00 2001 From: Ephy Date: Thu, 14 Dec 2023 02:33:44 -0300 Subject: [PATCH 022/137] test: update integration_test.rs --- tests/integration_test.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration_test.rs b/tests/integration_test.rs index d7cb9cb1..b8423e21 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -41,6 +41,7 @@ async fn query_a_type() { #[tokio::test] async fn query_all_type() { let response = query_response("example.com", "ANY").await; + if let Ok(rrs) = response { assert_eq!(rrs.len(), 2); } else { From 4d302afe3cfa9577bd518be558f54faae8ad9dc4 Mon Sep 17 00:00:00 2001 From: Ephy Date: Thu, 14 Dec 2023 02:34:48 -0300 Subject: [PATCH 023/137] test: add inner lookup cache test cases --- src/async_resolver.rs | 41 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index 4d3fbc6f..f03522b6 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -524,9 +524,9 @@ mod async_resolver_test { } } - /// Test lookup cache + /// Test inner lookup cache #[tokio::test] - async fn lookup_cache() { + async fn inner_lookup_cache_available() { let mut resolver = AsyncResolver::new(ResolverConfig::default()); resolver.cache.set_max_size(1); @@ -537,7 +537,40 @@ mod async_resolver_test { resolver.cache.add(domain_name, resource_record); - let _response = resolver.lookup("example.com", "UDP", "A","IN").await; + let domain_name = DomainName::new_from_string("example.com".to_string()); + let response = resolver.inner_lookup(domain_name, Qtype::A, Qclass::IN).await; + + if let Ok(msg) = response { + assert_eq!(msg.get_header().get_aa(), false); + } else { + panic!("No response from cache"); + } + } + + /// Test inner lookup without cache + #[tokio::test] + async fn inner_lookup_with_no_cache() { + let mut config = ResolverConfig::default(); + config.set_cache_enabled(false); + + let mut resolver = AsyncResolver::new(config); + resolver.cache.set_max_size(1); + + let domain_name = DomainName::new_from_string("example.com".to_string()); + let a_rdata = ARdata::new_from_addr(IpAddr::from_str("93.184.216.34").unwrap()); + let a_rdata = Rdata::A(a_rdata); + let resource_record = ResourceRecord::new(a_rdata); + + resolver.cache.add(domain_name, resource_record); + + let domain_name = DomainName::new_from_string("example.com".to_string()); + let response = resolver.inner_lookup(domain_name, Qtype::A, Qclass::IN).await; + + if let Ok(msg) = response { + assert_eq!(msg.get_header().get_aa(), false); + } else { + panic!("No response from nameserver"); + } } /// Test cache data @@ -546,7 +579,7 @@ mod async_resolver_test { let mut resolver = AsyncResolver::new(ResolverConfig::default()); resolver.cache.set_max_size(1); assert_eq!(resolver.cache.is_empty(), true); - let _response = resolver.lookup("example.com", "UDP", "A","IN").await; + let _response = resolver.lookup("example.com", "UDP", "A", "IN").await; assert_eq!(resolver.cache.is_cached(DomainName::new_from_str("example.com"), Rtype::A), true); // TODO: Test special cases from RFC From de3c3395cc4e1fd8c605cea1eb798b3362b83a0e Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Thu, 14 Dec 2023 20:10:01 -0300 Subject: [PATCH 024/137] add: creation_time for cache --- src/rr_cache.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/rr_cache.rs b/src/rr_cache.rs index e1a42c10..026dec0a 100644 --- a/src/rr_cache.rs +++ b/src/rr_cache.rs @@ -2,14 +2,16 @@ use crate::message::resource_record::ResourceRecord; use chrono::prelude::*; #[derive(Clone,PartialEq,Debug)] -// An structs that represents one element in the dns cache. +/// An structs that represents one element in the dns cache. pub struct RRCache { - // Resource Records of the domain name + /// Resource Records of the domain name resource_record: ResourceRecord, - // Mean of response time of the ip address + /// Mean of response time of the ip address response_time: u32, - // Last use of the rr + /// Last use of the rr last_use: DateTime, + /// Time of creation of the `RRCache` in the Resolver's cache. + creation_time: DateTime, } impl RRCache { @@ -28,6 +30,7 @@ impl RRCache { resource_record: resource_record, response_time: 5000, last_use: Utc::now(), + creation_time: Utc::now(), }; rr_cache From 93520154593ed00064a65fc28a038a4aad08a46c Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Thu, 14 Dec 2023 20:10:46 -0300 Subject: [PATCH 025/137] Add get_creation_time method to RRCache --- src/rr_cache.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/rr_cache.rs b/src/rr_cache.rs index 026dec0a..ec2f22fa 100644 --- a/src/rr_cache.rs +++ b/src/rr_cache.rs @@ -53,6 +53,11 @@ impl RRCache { pub fn get_last_use(&self) -> DateTime { self.last_use } + + // Gets the creation time of the domain in cache + pub fn get_creation_time(&self) -> DateTime { + self.creation_time + } } // Setters From 71a23965266c5b0f2cd43414f1ddc9c021c742c6 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Fri, 15 Dec 2023 18:00:18 -0300 Subject: [PATCH 026/137] add timeout cache and test --- src/dns_cache/cache_data/host_data.rs | 52 +++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/dns_cache/cache_data/host_data.rs b/src/dns_cache/cache_data/host_data.rs index 3700c4e0..09c8b4ec 100644 --- a/src/dns_cache/cache_data/host_data.rs +++ b/src/dns_cache/cache_data/host_data.rs @@ -210,6 +210,22 @@ impl HostData{ } } + /// For each domain name, it removes the RRCache past its TTL. + fn timeout_rr_cache(&mut self) { + let mut new_hash = HashMap::>::new(); + let data = self.get_host_hash(); + let current_time = Utc::now(); + for (domain_name, rr_cache_vec) in data.into_iter() { + let filtered_rr_cache_vec: Vec = rr_cache_vec + .into_iter() + .filter(|rr_cache| rr_cache.get_absolute_ttl() > current_time) + .collect(); + + new_hash.insert(domain_name, filtered_rr_cache_vec); + } + self.set_host_hash(new_hash); + } + } ///setter and getter for the host data @@ -466,4 +482,40 @@ mod host_data_test{ assert_eq!(2500, rr_cache.get_response_time()) } + + #[test] + fn timeout_rr_cache() { + use std::{thread, time}; + let mut host_data = HostData::new(); + let a_rdata = Rdata::A(ARdata::new()); + + let mut resource_record_valid = ResourceRecord::new(a_rdata.clone()); + resource_record_valid.set_ttl(1000); + let rr_cache_valid = RRCache::new(resource_record_valid); + + let mut resource_record_invalid = ResourceRecord::new(a_rdata); + resource_record_invalid.set_ttl(4); + let rr_cache_invalid = RRCache::new(resource_record_invalid); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("uchile.cl")); + + host_data.add_to_host_data(domain_name.clone(), rr_cache_valid); + host_data.add_to_host_data(domain_name.clone(), rr_cache_invalid); + + assert_eq!(host_data.get_host_hash().len(), 1); + if let Some(rr_cache_vec) = host_data.get_host_hash().get(&domain_name) { + assert_eq!(rr_cache_vec.len(), 2); + } + + println!("Before timeout: {:?}", Utc::now()); + thread::sleep(time::Duration::from_secs(5)); + println!("After timeout: {:?}", Utc::now()); + host_data.timeout_rr_cache(); + + assert_eq!(host_data.get_host_hash().len(), 1); + if let Some(rr_cache_vec) = host_data.get_host_hash().get(&domain_name) { + assert_eq!(rr_cache_vec.len(), 1); + } + } } From eb74ab365fb36d36468a91066ef0ad9c2f5ceaee Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Fri, 15 Dec 2023 18:00:53 -0300 Subject: [PATCH 027/137] add get_absolut_ttl() --- src/rr_cache.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/rr_cache.rs b/src/rr_cache.rs index ec2f22fa..882be294 100644 --- a/src/rr_cache.rs +++ b/src/rr_cache.rs @@ -35,6 +35,13 @@ impl RRCache { rr_cache } + + pub fn get_absolute_ttl(&self) -> DateTime { + let ttl = self.resource_record.get_ttl(); + let creation_time = self.creation_time; + + creation_time + chrono::Duration::seconds(ttl as i64) + } } // Getters From 326cf1162e27c67b6345c91eab432eceec7dae26 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Mon, 18 Dec 2023 15:08:19 -0300 Subject: [PATCH 028/137] first commit proving the new branch --- src/client.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/client.rs b/src/client.rs index 6e84c7c8..a2d3a3e4 100644 --- a/src/client.rs +++ b/src/client.rs @@ -176,6 +176,7 @@ mod client_test { use crate::domain_name::DomainName; use super::{Client, tcp_connection::ClientTCPConnection, client_connection::ClientConnection, udp_connection::ClientUDPConnection}; + //cambio prueba #[test] fn udp_client_query() { //create connection From 761f72218dafe35c3b425cae8a2b142af6f802ba Mon Sep 17 00:00:00 2001 From: Ephyy Date: Mon, 18 Dec 2023 20:27:18 -0300 Subject: [PATCH 029/137] test: update integration_test.rs --- tests/integration_test.rs | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/tests/integration_test.rs b/tests/integration_test.rs index b8423e21..3a3e1f1a 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -5,14 +5,14 @@ use dns_rust::{async_resolver::{config::ResolverConfig, AsyncResolver, resolver_ // TODO: Change params type to intoDomainName -async fn query_response(domain_name: &str, qtype: &str) -> Result, ResolverError>{ +async fn query_response(domain_name: &str, protocol: &str, qtype: &str) -> Result, ResolverError>{ let config = ResolverConfig::default(); let mut resolver = AsyncResolver::new(config); let response = resolver.lookup( domain_name, - "UDP", + protocol, qtype, "IN").await; @@ -22,7 +22,7 @@ async fn query_response(domain_name: &str, qtype: &str) -> Result Date: Mon, 18 Dec 2023 20:30:46 -0300 Subject: [PATCH 030/137] test: update integration_test.rs warning --- tests/integration_test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 3a3e1f1a..62a07172 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -41,7 +41,7 @@ async fn query_a_type() { #[tokio::test] #[should_panic] async fn query_all_type() { - let response = query_response("example.com", "TCP", "ANY").await; + let __response = query_response("example.com", "TCP", "ANY").await; } /// 6.2.3 Query Qtype = MX From 5e8fc093d34adb594eb2f68994037c59686c2d5d Mon Sep 17 00:00:00 2001 From: Ephyy Date: Mon, 18 Dec 2023 20:40:05 -0300 Subject: [PATCH 031/137] test: update integration_test.rs, qtype=any test --- tests/integration_test.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 62a07172..fad5dd26 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -39,9 +39,11 @@ async fn query_a_type() { /// 6.2.2 Query normal Qtype = * #[tokio::test] -#[should_panic] async fn query_all_type() { - let __response = query_response("example.com", "TCP", "ANY").await; + let udp_response = query_response("example.com", "UDP", "ANY").await; + let tcp_response = query_response("example.com", "TCP", "ANY").await; + assert!(udp_response.is_err()); + assert!(tcp_response.is_err()); } /// 6.2.3 Query Qtype = MX From 1620371e628cef78ef9db3a978776ace64395a30 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Tue, 19 Dec 2023 12:52:30 -0300 Subject: [PATCH 032/137] add test qtype A client UDP --- src/client.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index a2d3a3e4..072b6a19 100644 --- a/src/client.rs +++ b/src/client.rs @@ -176,7 +176,6 @@ mod client_test { use crate::domain_name::DomainName; use super::{Client, tcp_connection::ClientTCPConnection, client_connection::ClientConnection, udp_connection::ClientUDPConnection}; - //cambio prueba #[test] fn udp_client_query() { //create connection @@ -207,6 +206,30 @@ mod client_test { } } + #[test] + fn udp_client_qtype_a() { + //create connection + let server_addr: IpAddr = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); + let timeout: Duration = Duration::from_secs(2); + + let conn_udp:ClientUDPConnection = ClientUDPConnection::new(server_addr, timeout); + let mut udp_client = Client::new(conn_udp); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("test.test2.com.")); + + // sends query, qtype A + let qtype = "A"; + let qclass= "IN"; + let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let answers = response.get_answer(); + for answer in answers { + let a_rdata = answer.get_rdata(); + // Check if the answer is A type + assert!(matches!(a_rdata, Rdata::A(_a_rdata))) + } + } + #[test] fn tcp_client_query() { //FIXME: From 193bf583266cbf787ae5f0a9b0b50272a6f17d19 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Tue, 19 Dec 2023 13:01:30 -0300 Subject: [PATCH 033/137] add test qtype NS client UDP --- src/client.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/client.rs b/src/client.rs index 072b6a19..31baba8e 100644 --- a/src/client.rs +++ b/src/client.rs @@ -230,6 +230,30 @@ mod client_test { } } + #[test] + fn udp_client_qtype_ns() { + //create connection + let server_addr: IpAddr = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); + let timeout: Duration = Duration::from_secs(2); + + let conn_udp:ClientUDPConnection = ClientUDPConnection::new(server_addr, timeout); + let mut udp_client = Client::new(conn_udp); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("test.test2.com.")); + + // sends query, qtype NS + let qtype = "NS"; + let qclass= "IN"; + let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let answers = response.get_answer(); + for answer in answers { + let ns_rdata = answer.get_rdata(); + // Check if the answer is A type + assert!(matches!(ns_rdata, Rdata::NS(_ns_rdata))) + } + } + #[test] fn tcp_client_query() { //FIXME: From e2fbd6790dd72b126d4d1bc0ea89f9a41681a15b Mon Sep 17 00:00:00 2001 From: valesteban Date: Tue, 19 Dec 2023 13:43:34 -0300 Subject: [PATCH 034/137] add auxiliadd auxiliary function for sending queries UDO or TCP depending in teh connection made --- src/async_resolver/lookup.rs | 44 +++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/src/async_resolver/lookup.rs b/src/async_resolver/lookup.rs index 3eb10615..294fcbd7 100644 --- a/src/async_resolver/lookup.rs +++ b/src/async_resolver/lookup.rs @@ -192,27 +192,19 @@ pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y q new_header.set_rcode(2); new_header.set_qr(true); response.set_header(new_header); + //FIXME: let mut result_dns_msg = Ok(response.clone()); let mut retry_count = 0; - for (conn_udp,conn_tcp) in name_servers.iter() { + for connections in name_servers.iter() { if retry_count > config.get_retry() { break; } - - match config.get_protocol() { - ConnectionProtocol::UDP => { - let result_response = conn_udp.send(new_query.clone()); - result_dns_msg = parse_response(result_response); - } - ConnectionProtocol::TCP => { - let result_response = conn_tcp.send(new_query.clone()); - result_dns_msg = parse_response(result_response); - } - _ => continue, - } + + result_dns_msg = send_query_resolver_by_protocol(config.get_protocol(),new_query.clone(), result_dns_msg.clone(), connections); + retry_count = retry_count + 1; } @@ -238,6 +230,32 @@ pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y q result_dns_msg } +/// Sends a DNS query to a resolver using the specified connection protocol. +/// +/// This function takes a DNS query, a result containing a DNS message, +/// and connection information. Depending on the specified protocol (UDP or TCP), +/// it sends the query using the corresponding connection and updates the result +/// with the parsed response. +fn send_query_resolver_by_protocol(protocol: ConnectionProtocol,query:DnsMessage,mut result_dns_msg: Result, connections: &(ClientUDPConnection , ClientTCPConnection)) +-> Result{ + match protocol{ + ConnectionProtocol::UDP => { + let result_response = connections.0.send(query.clone()); + result_dns_msg = parse_response(result_response); + } + ConnectionProtocol::TCP => { + let result_response = connections.1.send(query.clone()); + result_dns_msg = parse_response(result_response); + } + _ => {}, + } + + result_dns_msg +} + + + + /// Parse the received response datagram to a `DnsMessage`. /// /// [RFC 1035]: https://datatracker.ietf.org/doc/html/rfc1035#section-7.3 From bd8008583d2b8c3bf0c8d70de72267dc4e6a7fd6 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Tue, 19 Dec 2023 15:11:46 -0300 Subject: [PATCH 035/137] add test qtype CNAME client UDP --- src/client.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/client.rs b/src/client.rs index 31baba8e..208040ae 100644 --- a/src/client.rs +++ b/src/client.rs @@ -253,7 +253,30 @@ mod client_test { assert!(matches!(ns_rdata, Rdata::NS(_ns_rdata))) } } + + #[test] + fn udp_client_qtype_cname() { + //create connection + let server_addr: IpAddr = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); + let timeout: Duration = Duration::from_secs(2); + + let conn_udp:ClientUDPConnection = ClientUDPConnection::new(server_addr, timeout); + let mut udp_client = Client::new(conn_udp); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("test.test2.com.")); + // sends query, qtype CNAME + let qtype = "CNAME"; + let qclass= "IN"; + let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let answers = response.get_answer(); + for answer in answers { + let cname_rdata = answer.get_rdata(); + // Check if the answer is A type + assert!(matches!(cname_rdata, Rdata::CNAME(_cname_rdata))) + } + } #[test] fn tcp_client_query() { //FIXME: From f0e67c53dd5db1ff68a12f02d7e57ac96844fd7f Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Tue, 19 Dec 2023 15:26:12 -0300 Subject: [PATCH 036/137] add test qtype SOA client UDP --- src/client.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/client.rs b/src/client.rs index 208040ae..20fba148 100644 --- a/src/client.rs +++ b/src/client.rs @@ -277,6 +277,31 @@ mod client_test { assert!(matches!(cname_rdata, Rdata::CNAME(_cname_rdata))) } } + + #[test] + fn udp_client_qtype_soa() { + //create connection + let server_addr: IpAddr = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); + let timeout: Duration = Duration::from_secs(2); + + let conn_udp:ClientUDPConnection = ClientUDPConnection::new(server_addr, timeout); + let mut udp_client = Client::new(conn_udp); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("test.test2.com.")); + + // sends query, qtype SOA + let qtype = "SOA"; + let qclass= "IN"; + let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let answers = response.get_answer(); + for answer in answers { + let soa_rdata = answer.get_rdata(); + // Check if the answer is A type + assert!(matches!(soa_rdata, Rdata::SOA(_soa_rdata))) + } + } + #[test] fn tcp_client_query() { //FIXME: From 30430a387cffc4fd8dc854935e9efb58362ba83d Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Tue, 19 Dec 2023 15:29:40 -0300 Subject: [PATCH 037/137] add test qtype MX client UDP --- src/client.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index 20fba148..edcd42b3 100644 --- a/src/client.rs +++ b/src/client.rs @@ -301,7 +301,30 @@ mod client_test { assert!(matches!(soa_rdata, Rdata::SOA(_soa_rdata))) } } - + + #[test] + fn udp_client_qtype_mx(){ + //create connection + let server_addr: IpAddr = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); + let timeout: Duration = Duration::from_secs(2); + + let conn_udp:ClientUDPConnection = ClientUDPConnection::new(server_addr, timeout); + let mut udp_client = Client::new(conn_udp); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("test.test2.com.")); + + // sends query, qtype MX + let qtype = "MX"; + let qclass= "IN"; + let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let answers = response.get_answer(); + for answer in answers { + let mx_rdata = answer.get_rdata(); + // Check if the answer is A type + assert!(matches!(mx_rdata, Rdata::MX(_mx_rdata))) + } + } #[test] fn tcp_client_query() { //FIXME: From 59d861bf2f43cc2c5695bd6b793a21223abec4e8 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Tue, 19 Dec 2023 15:33:15 -0300 Subject: [PATCH 038/137] add test qtype PTR client UDP --- src/client.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/client.rs b/src/client.rs index edcd42b3..98a07f2f 100644 --- a/src/client.rs +++ b/src/client.rs @@ -325,6 +325,30 @@ mod client_test { assert!(matches!(mx_rdata, Rdata::MX(_mx_rdata))) } } + + #[test] + fn udp_client_qtype_ptr(){ + //create connection + let server_addr: IpAddr = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); + let timeout: Duration = Duration::from_secs(2); + + let conn_udp:ClientUDPConnection = ClientUDPConnection::new(server_addr, timeout); + let mut udp_client = Client::new(conn_udp); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("test.test2.com.")); + + // sends query, qtype PTR + let qtype = "PTR"; + let qclass= "IN"; + let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let answers = response.get_answer(); + for answer in answers { + let ptr_rdata = answer.get_rdata(); + // Check if the answer is A type + assert!(matches!(ptr_rdata, Rdata::PTR(_ptr_rdata))) + } + } #[test] fn tcp_client_query() { //FIXME: From 4bf1e7b1a5e8c88a5d4bb84ce6137ac11be6c21b Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Tue, 19 Dec 2023 15:35:57 -0300 Subject: [PATCH 039/137] Fix typo in documentation of tests qtype --- src/client.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/client.rs b/src/client.rs index 98a07f2f..289ed36f 100644 --- a/src/client.rs +++ b/src/client.rs @@ -249,7 +249,7 @@ mod client_test { let answers = response.get_answer(); for answer in answers { let ns_rdata = answer.get_rdata(); - // Check if the answer is A type + // Check if the answer is NS type assert!(matches!(ns_rdata, Rdata::NS(_ns_rdata))) } } @@ -273,7 +273,7 @@ mod client_test { let answers = response.get_answer(); for answer in answers { let cname_rdata = answer.get_rdata(); - // Check if the answer is A type + // Check if the answer is CNAME type assert!(matches!(cname_rdata, Rdata::CNAME(_cname_rdata))) } } @@ -297,7 +297,7 @@ mod client_test { let answers = response.get_answer(); for answer in answers { let soa_rdata = answer.get_rdata(); - // Check if the answer is A type + // Check if the answer is SOA type assert!(matches!(soa_rdata, Rdata::SOA(_soa_rdata))) } } @@ -321,7 +321,7 @@ mod client_test { let answers = response.get_answer(); for answer in answers { let mx_rdata = answer.get_rdata(); - // Check if the answer is A type + // Check if the answer is MX type assert!(matches!(mx_rdata, Rdata::MX(_mx_rdata))) } } @@ -345,10 +345,11 @@ mod client_test { let answers = response.get_answer(); for answer in answers { let ptr_rdata = answer.get_rdata(); - // Check if the answer is A type + // Check if the answer is PTR type assert!(matches!(ptr_rdata, Rdata::PTR(_ptr_rdata))) } } + #[test] fn tcp_client_query() { //FIXME: From 141167667e71892beac05592f9a024a1f6227c19 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Tue, 19 Dec 2023 15:36:58 -0300 Subject: [PATCH 040/137] add test qtype TXT client UDP --- src/client.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/client.rs b/src/client.rs index 289ed36f..0ca1b939 100644 --- a/src/client.rs +++ b/src/client.rs @@ -350,6 +350,29 @@ mod client_test { } } + #[test] + fn udp_client_qtype_txt(){ + //create connection + let server_addr: IpAddr = IpAddr::V4(Ipv4Addr::new(172, 18, 0, 1)); + let timeout: Duration = Duration::from_secs(2); + + let conn_udp:ClientUDPConnection = ClientUDPConnection::new(server_addr, timeout); + let mut udp_client = Client::new(conn_udp); + + let mut domain_name = DomainName::new(); + + // sends query, qtype TXT + domain_name.set_name(String::from("test.test2.com.")); + let qtype = "TXT"; + let qclass= "IN"; + let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let answers = response.get_answer(); + for answer in answers { + let txt_rdata = answer.get_rdata(); + // Check if the answer is TXT type + assert!(matches!(txt_rdata, Rdata::TXT(_txt_rdata))) + } + } #[test] fn tcp_client_query() { //FIXME: From ddcbdd253813db0a43eeecd20a6111ab83ecf1c8 Mon Sep 17 00:00:00 2001 From: valesteban Date: Tue, 19 Dec 2023 16:24:59 -0300 Subject: [PATCH 041/137] add timeout between queries sent --- src/async_resolver/lookup.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/async_resolver/lookup.rs b/src/async_resolver/lookup.rs index 294fcbd7..ab7179ba 100644 --- a/src/async_resolver/lookup.rs +++ b/src/async_resolver/lookup.rs @@ -6,6 +6,8 @@ use crate::client::client_connection::ClientConnection; use crate::message::class_qclass::Qclass; use crate::message::type_qtype::Qtype; use futures_util::{FutureExt,task::Waker}; +use std::thread; +use std::time::Duration; use std::pin::Pin; use std::task::{Poll,Context}; use rand::{thread_rng, Rng}; @@ -62,7 +64,8 @@ impl Future for LookupFutureStub { self.config.get_name_servers(), self.waker.clone(), referenced_query, - self.config.clone())); + self.config.clone()) + ); return Poll::Pending; }, @@ -202,10 +205,15 @@ pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y q if retry_count > config.get_retry() { break; } - - result_dns_msg = send_query_resolver_by_protocol(config.get_protocol(),new_query.clone(), result_dns_msg.clone(), connections); - retry_count = retry_count + 1; + //FIXME: try make async + let delay_duration = Duration::from_secs(6); + thread::sleep(delay_duration); + + result_dns_msg = send_query_resolver_by_protocol(config.get_protocol(),new_query.clone(), result_dns_msg.clone(), connections); + if result_dns_msg.is_err(){ + retry_count = retry_count + 1; + } } // Wake up task From 282dd8c97fc933099f7135582384755eff7b3b1c Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Tue, 19 Dec 2023 16:25:56 -0300 Subject: [PATCH 042/137] add test qtype TSIG client UDP --- src/client.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/client.rs b/src/client.rs index 0ca1b939..fc9d7b5f 100644 --- a/src/client.rs +++ b/src/client.rs @@ -350,6 +350,30 @@ mod client_test { } } + #[test] + fn udp_client_qtype_tsig(){ + //create connection + let server_addr: IpAddr = IpAddr::V4(Ipv4Addr::new(172, 18, 0, 1)); + let timeout: Duration = Duration::from_secs(2); + + let conn_udp:ClientUDPConnection = ClientUDPConnection::new(server_addr, timeout); + let mut udp_client = Client::new(conn_udp); + + let mut domain_name = DomainName::new(); + + // sends query, qtype TSIG + domain_name.set_name(String::from("test.test2.com.")); + let qtype = "TSIG"; + let qclass= "IN"; + let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let answers = response.get_answer(); + for answer in answers { + let tsig_rdata = answer.get_rdata(); + // Check if the answer is TSIG type + assert!(matches!(tsig_rdata, Rdata::HINFO(_tsig_rdata))) + } + } + #[test] fn udp_client_qtype_txt(){ //create connection From ed691d32ea10b9cea2f8a2b3ba781ad07c2eee64 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Tue, 19 Dec 2023 16:26:45 -0300 Subject: [PATCH 043/137] add test qtype HINFO client UDP --- src/client.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/client.rs b/src/client.rs index fc9d7b5f..0e42c7b6 100644 --- a/src/client.rs +++ b/src/client.rs @@ -373,6 +373,30 @@ mod client_test { assert!(matches!(tsig_rdata, Rdata::HINFO(_tsig_rdata))) } } + + #[test] + fn udp_client_qtype_hinfo(){ + //create connection + let server_addr: IpAddr = IpAddr::V4(Ipv4Addr::new(172, 18, 0, 1)); + let timeout: Duration = Duration::from_secs(2); + + let conn_udp:ClientUDPConnection = ClientUDPConnection::new(server_addr, timeout); + let mut udp_client = Client::new(conn_udp); + + let mut domain_name = DomainName::new(); + + // sends query, qtype HINFO + domain_name.set_name(String::from("test.test2.com.")); + let qtype = "HINFO"; + let qclass= "IN"; + let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let answers = response.get_answer(); + for answer in answers { + let hinfo_rdata = answer.get_rdata(); + // Check if the answer is HINFO type + assert!(matches!(hinfo_rdata, Rdata::HINFO(_hinfo_rdata))) + } + } #[test] fn udp_client_qtype_txt(){ From 8281393be5d5d51048a334eb50ebb96afc4e95bb Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Tue, 19 Dec 2023 16:48:11 -0300 Subject: [PATCH 044/137] Fix bug of server_addr in tests qtypes TXT, HINFO, TSIG --- src/client.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/client.rs b/src/client.rs index 0e42c7b6..cf9fad7f 100644 --- a/src/client.rs +++ b/src/client.rs @@ -353,7 +353,7 @@ mod client_test { #[test] fn udp_client_qtype_tsig(){ //create connection - let server_addr: IpAddr = IpAddr::V4(Ipv4Addr::new(172, 18, 0, 1)); + let server_addr: IpAddr = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); let timeout: Duration = Duration::from_secs(2); let conn_udp:ClientUDPConnection = ClientUDPConnection::new(server_addr, timeout); @@ -373,11 +373,11 @@ mod client_test { assert!(matches!(tsig_rdata, Rdata::HINFO(_tsig_rdata))) } } - + #[test] fn udp_client_qtype_hinfo(){ //create connection - let server_addr: IpAddr = IpAddr::V4(Ipv4Addr::new(172, 18, 0, 1)); + let server_addr: IpAddr = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); let timeout: Duration = Duration::from_secs(2); let conn_udp:ClientUDPConnection = ClientUDPConnection::new(server_addr, timeout); @@ -401,7 +401,7 @@ mod client_test { #[test] fn udp_client_qtype_txt(){ //create connection - let server_addr: IpAddr = IpAddr::V4(Ipv4Addr::new(172, 18, 0, 1)); + let server_addr: IpAddr = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); let timeout: Duration = Duration::from_secs(2); let conn_udp:ClientUDPConnection = ClientUDPConnection::new(server_addr, timeout); From d34e92322da736a26de686d557f05d93ace53959 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Tue, 19 Dec 2023 16:55:15 -0300 Subject: [PATCH 045/137] Fix dominio use in qtypes test and udp_client_query test, now example.com is used --- src/client.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/client.rs b/src/client.rs index cf9fad7f..d7efde15 100644 --- a/src/client.rs +++ b/src/client.rs @@ -188,7 +188,7 @@ mod client_test { let mut domain_name = DomainName::new(); // sends query - domain_name.set_name(String::from("test.test2.com.")); + domain_name.set_name(String::from("example.com")); let qtype = "A"; let qclass= "IN"; let response = udp_client.query(domain_name, qtype, qclass).unwrap(); @@ -216,7 +216,7 @@ mod client_test { let mut udp_client = Client::new(conn_udp); let mut domain_name = DomainName::new(); - domain_name.set_name(String::from("test.test2.com.")); + domain_name.set_name(String::from("example.com")); // sends query, qtype A let qtype = "A"; @@ -240,7 +240,7 @@ mod client_test { let mut udp_client = Client::new(conn_udp); let mut domain_name = DomainName::new(); - domain_name.set_name(String::from("test.test2.com.")); + domain_name.set_name(String::from("example.com")); // sends query, qtype NS let qtype = "NS"; @@ -264,7 +264,7 @@ mod client_test { let mut udp_client = Client::new(conn_udp); let mut domain_name = DomainName::new(); - domain_name.set_name(String::from("test.test2.com.")); + domain_name.set_name(String::from("example.com")); // sends query, qtype CNAME let qtype = "CNAME"; @@ -288,7 +288,7 @@ mod client_test { let mut udp_client = Client::new(conn_udp); let mut domain_name = DomainName::new(); - domain_name.set_name(String::from("test.test2.com.")); + domain_name.set_name(String::from("example.com")); // sends query, qtype SOA let qtype = "SOA"; @@ -312,7 +312,7 @@ mod client_test { let mut udp_client = Client::new(conn_udp); let mut domain_name = DomainName::new(); - domain_name.set_name(String::from("test.test2.com.")); + domain_name.set_name(String::from("example.com")); // sends query, qtype MX let qtype = "MX"; @@ -336,7 +336,7 @@ mod client_test { let mut udp_client = Client::new(conn_udp); let mut domain_name = DomainName::new(); - domain_name.set_name(String::from("test.test2.com.")); + domain_name.set_name(String::from("example.com")); // sends query, qtype PTR let qtype = "PTR"; @@ -362,7 +362,7 @@ mod client_test { let mut domain_name = DomainName::new(); // sends query, qtype TSIG - domain_name.set_name(String::from("test.test2.com.")); + domain_name.set_name(String::from("example.com")); let qtype = "TSIG"; let qclass= "IN"; let response = udp_client.query(domain_name, qtype, qclass).unwrap(); @@ -386,7 +386,7 @@ mod client_test { let mut domain_name = DomainName::new(); // sends query, qtype HINFO - domain_name.set_name(String::from("test.test2.com.")); + domain_name.set_name(String::from("example.com")); let qtype = "HINFO"; let qclass= "IN"; let response = udp_client.query(domain_name, qtype, qclass).unwrap(); @@ -410,7 +410,7 @@ mod client_test { let mut domain_name = DomainName::new(); // sends query, qtype TXT - domain_name.set_name(String::from("test.test2.com.")); + domain_name.set_name(String::from("example.com")); let qtype = "TXT"; let qclass= "IN"; let response = udp_client.query(domain_name, qtype, qclass).unwrap(); From 3c72c4837d12e35d143876a7f5413abd2003b6d6 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Tue, 19 Dec 2023 17:08:40 -0300 Subject: [PATCH 046/137] Fix unwrap() into a match in qtypes tests and udp_client_query() test --- src/client.rs | 50 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/src/client.rs b/src/client.rs index d7efde15..875d9ee5 100644 --- a/src/client.rs +++ b/src/client.rs @@ -191,7 +191,10 @@ mod client_test { domain_name.set_name(String::from("example.com")); let qtype = "A"; let qclass= "IN"; - let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let response = match udp_client.query(domain_name, qtype, qclass) { + Ok(value) => value, + Err(error) => panic!("Error al consultar: {:?}", error), + }; let expected_ip: [u8; 4] = [93, 184, 216, 34]; let answers = response.get_answer(); @@ -221,7 +224,10 @@ mod client_test { // sends query, qtype A let qtype = "A"; let qclass= "IN"; - let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let response = match udp_client.query(domain_name, qtype, qclass) { + Ok(value) => value, + Err(error) => panic!("Error al consultar: {:?}", error), + }; let answers = response.get_answer(); for answer in answers { let a_rdata = answer.get_rdata(); @@ -245,7 +251,10 @@ mod client_test { // sends query, qtype NS let qtype = "NS"; let qclass= "IN"; - let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let response = match udp_client.query(domain_name, qtype, qclass) { + Ok(value) => value, + Err(error) => panic!("Error al consultar: {:?}", error), + }; let answers = response.get_answer(); for answer in answers { let ns_rdata = answer.get_rdata(); @@ -269,7 +278,10 @@ mod client_test { // sends query, qtype CNAME let qtype = "CNAME"; let qclass= "IN"; - let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let response = match udp_client.query(domain_name, qtype, qclass) { + Ok(value) => value, + Err(error) => panic!("Error al consultar: {:?}", error), + }; let answers = response.get_answer(); for answer in answers { let cname_rdata = answer.get_rdata(); @@ -293,7 +305,10 @@ mod client_test { // sends query, qtype SOA let qtype = "SOA"; let qclass= "IN"; - let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let response = match udp_client.query(domain_name, qtype, qclass) { + Ok(value) => value, + Err(error) => panic!("Error al consultar: {:?}", error), + }; let answers = response.get_answer(); for answer in answers { let soa_rdata = answer.get_rdata(); @@ -317,7 +332,10 @@ mod client_test { // sends query, qtype MX let qtype = "MX"; let qclass= "IN"; - let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let response = match udp_client.query(domain_name, qtype, qclass) { + Ok(value) => value, + Err(error) => panic!("Error al consultar: {:?}", error), + }; let answers = response.get_answer(); for answer in answers { let mx_rdata = answer.get_rdata(); @@ -341,7 +359,10 @@ mod client_test { // sends query, qtype PTR let qtype = "PTR"; let qclass= "IN"; - let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let response = match udp_client.query(domain_name, qtype, qclass) { + Ok(value) => value, + Err(error) => panic!("Error al consultar: {:?}", error), + }; let answers = response.get_answer(); for answer in answers { let ptr_rdata = answer.get_rdata(); @@ -365,7 +386,10 @@ mod client_test { domain_name.set_name(String::from("example.com")); let qtype = "TSIG"; let qclass= "IN"; - let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let response = match udp_client.query(domain_name, qtype, qclass) { + Ok(value) => value, + Err(error) => panic!("Error al consultar: {:?}", error), + }; let answers = response.get_answer(); for answer in answers { let tsig_rdata = answer.get_rdata(); @@ -389,7 +413,10 @@ mod client_test { domain_name.set_name(String::from("example.com")); let qtype = "HINFO"; let qclass= "IN"; - let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let response = match udp_client.query(domain_name, qtype, qclass) { + Ok(value) => value, + Err(error) => panic!("Error al consultar: {:?}", error), + }; let answers = response.get_answer(); for answer in answers { let hinfo_rdata = answer.get_rdata(); @@ -413,7 +440,10 @@ mod client_test { domain_name.set_name(String::from("example.com")); let qtype = "TXT"; let qclass= "IN"; - let response = udp_client.query(domain_name, qtype, qclass).unwrap(); + let response = match udp_client.query(domain_name, qtype, qclass) { + Ok(value) => value, + Err(error) => panic!("Error al consultar: {:?}", error), + }; let answers = response.get_answer(); for answer in answers { let txt_rdata = answer.get_rdata(); From ffd818dc63d73df42fe5ee4f5dcd0fe05fb87a79 Mon Sep 17 00:00:00 2001 From: Ephyy Date: Wed, 20 Dec 2023 11:35:18 -0300 Subject: [PATCH 047/137] update question.rs, remove print --- src/message/question.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/message/question.rs b/src/message/question.rs index 8f5473b9..32d1d32a 100644 --- a/src/message/question.rs +++ b/src/message/question.rs @@ -76,7 +76,6 @@ impl Question { let (qname, bytes_without_name) = domain_name_result.unwrap(); - println!("{}", bytes_without_name.len()); if bytes_without_name.len() < 4 { return Err("Format Error"); } From 3f5417fb920102c9c9721b924aa1f3ee18055601 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Wed, 20 Dec 2023 16:33:41 -0300 Subject: [PATCH 048/137] Fix error in qtypes tests of spanish message in panic, updated to English --- src/client.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/client.rs b/src/client.rs index 875d9ee5..d0e6effe 100644 --- a/src/client.rs +++ b/src/client.rs @@ -193,7 +193,7 @@ mod client_test { let qclass= "IN"; let response = match udp_client.query(domain_name, qtype, qclass) { Ok(value) => value, - Err(error) => panic!("Error al consultar: {:?}", error), + Err(error) => panic!("Error in the response: {:?}", error), }; let expected_ip: [u8; 4] = [93, 184, 216, 34]; @@ -226,7 +226,7 @@ mod client_test { let qclass= "IN"; let response = match udp_client.query(domain_name, qtype, qclass) { Ok(value) => value, - Err(error) => panic!("Error al consultar: {:?}", error), + Err(error) => panic!("Error in the response: {:?}", error), }; let answers = response.get_answer(); for answer in answers { @@ -253,7 +253,7 @@ mod client_test { let qclass= "IN"; let response = match udp_client.query(domain_name, qtype, qclass) { Ok(value) => value, - Err(error) => panic!("Error al consultar: {:?}", error), + Err(error) => panic!("Error in the response: {:?}", error), }; let answers = response.get_answer(); for answer in answers { @@ -280,7 +280,7 @@ mod client_test { let qclass= "IN"; let response = match udp_client.query(domain_name, qtype, qclass) { Ok(value) => value, - Err(error) => panic!("Error al consultar: {:?}", error), + Err(error) => panic!("Error in the response: {:?}", error), }; let answers = response.get_answer(); for answer in answers { @@ -307,7 +307,7 @@ mod client_test { let qclass= "IN"; let response = match udp_client.query(domain_name, qtype, qclass) { Ok(value) => value, - Err(error) => panic!("Error al consultar: {:?}", error), + Err(error) => panic!("Error in the response: {:?}", error), }; let answers = response.get_answer(); for answer in answers { @@ -334,7 +334,7 @@ mod client_test { let qclass= "IN"; let response = match udp_client.query(domain_name, qtype, qclass) { Ok(value) => value, - Err(error) => panic!("Error al consultar: {:?}", error), + Err(error) => panic!("Error in the response: {:?}", error), }; let answers = response.get_answer(); for answer in answers { @@ -361,7 +361,7 @@ mod client_test { let qclass= "IN"; let response = match udp_client.query(domain_name, qtype, qclass) { Ok(value) => value, - Err(error) => panic!("Error al consultar: {:?}", error), + Err(error) => panic!("Error in the response: {:?}", error), }; let answers = response.get_answer(); for answer in answers { @@ -388,7 +388,7 @@ mod client_test { let qclass= "IN"; let response = match udp_client.query(domain_name, qtype, qclass) { Ok(value) => value, - Err(error) => panic!("Error al consultar: {:?}", error), + Err(error) => panic!("Error in the response: {:?}", error), }; let answers = response.get_answer(); for answer in answers { @@ -415,7 +415,7 @@ mod client_test { let qclass= "IN"; let response = match udp_client.query(domain_name, qtype, qclass) { Ok(value) => value, - Err(error) => panic!("Error al consultar: {:?}", error), + Err(error) => panic!("Error in the response: {:?}", error), }; let answers = response.get_answer(); for answer in answers { @@ -442,7 +442,7 @@ mod client_test { let qclass= "IN"; let response = match udp_client.query(domain_name, qtype, qclass) { Ok(value) => value, - Err(error) => panic!("Error al consultar: {:?}", error), + Err(error) => panic!("Error in the response: {:?}", error), }; let answers = response.get_answer(); for answer in answers { From 6ed6f0b4af8bba78ec87626aeec5360abb94db8d Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Thu, 21 Dec 2023 16:35:46 -0300 Subject: [PATCH 049/137] add test qtype A inner_lookup in async_resolver --- src/async_resolver.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index 4a36d367..9d0bf140 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -441,7 +441,7 @@ mod async_resolver_test { } #[tokio::test] - async fn inner_lookup() { + async fn inner_lookup_qtype_a() { // Create a new resolver with default values let mut resolver = AsyncResolver::new(ResolverConfig::default()); let domain_name = DomainName::new_from_string("example.com".to_string()); @@ -449,10 +449,19 @@ mod async_resolver_test { let record_class = Qclass::IN; let response = resolver.inner_lookup(domain_name,qtype,record_class).await; - //FIXME: add assert - assert!(response.is_ok()); - } - + let response = match response { + Ok(val) => val, + Err(error) => panic!("Error in the response: {:?}", error), + }; + //analize if the response has the correct type according with the qtype + let answers = response.get_answer(); + for answer in answers { + let a_rdata = answer.get_rdata(); + // Check if the answer is A type + assert!(matches!(a_rdata, Rdata::A(_a_rdata))) + } + } + #[tokio::test] async fn inner_lookup_ns() { // Create a new resolver with default values From 2710afa16a8b6419c12be9bf80fb79d309c00119 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Thu, 21 Dec 2023 16:40:54 -0300 Subject: [PATCH 050/137] add test qtype NS inner_lookup in async_resolver --- src/async_resolver.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index 9d0bf140..858ab4b1 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -461,6 +461,29 @@ mod async_resolver_test { assert!(matches!(a_rdata, Rdata::A(_a_rdata))) } } + + + #[tokio::test] + async fn inner_lookup_qtype_ns() { + // Create a new resolver with default values + let mut resolver = AsyncResolver::new(ResolverConfig::default()); + let domain_name = DomainName::new_from_string("example.com".to_string()); + let qtype = Qtype::NS; + let record_class = Qclass::IN; + let response = resolver.inner_lookup(domain_name,qtype,record_class).await; + + let response = match response { + Ok(val) => val, + Err(error) => panic!("Error in the response: {:?}", error), + }; + //analize if the response has the correct type according with the qtype + let answers = response.get_answer(); + for answer in answers { + let ns_rdata = answer.get_rdata(); + // Check if the answer is NS type + assert!(matches!(ns_rdata, Rdata::NS(_ns_rdata))) + } + } #[tokio::test] async fn inner_lookup_ns() { From 50a0258cb8bfb234469bcf25dbee73b0f846b479 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Thu, 21 Dec 2023 16:48:30 -0300 Subject: [PATCH 051/137] add test qtype MX inner_lookup in async_resolver --- src/async_resolver.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index 858ab4b1..48b5b47a 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -484,7 +484,29 @@ mod async_resolver_test { assert!(matches!(ns_rdata, Rdata::NS(_ns_rdata))) } } - + + #[tokio::test] + async fn inner_lookup_qtype_mx() { + // Create a new resolver with default values + let mut resolver = AsyncResolver::new(ResolverConfig::default()); + let domain_name = DomainName::new_from_string("example.com".to_string()); + let qtype = Qtype::MX; + let record_class = Qclass::IN; + let response = resolver.inner_lookup(domain_name,qtype,record_class).await; + + let response = match response { + Ok(val) => val, + Err(error) => panic!("Error in the response: {:?}", error), + }; + //analize if the response has the correct type according with the qtype + let answers = response.get_answer(); + for answer in answers { + let mx_rdata = answer.get_rdata(); + // Check if the answer is MX type + assert!(matches!(mx_rdata, Rdata::MX(_mx_rdata))) + } + } + #[tokio::test] async fn inner_lookup_ns() { // Create a new resolver with default values From d1e656e361b131ed8c4e83bf1673979c96acbf17 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Thu, 21 Dec 2023 16:49:59 -0300 Subject: [PATCH 052/137] add test qtype PTR inner_lookup in async_resolver --- src/async_resolver.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index 48b5b47a..2d46f64b 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -507,6 +507,28 @@ mod async_resolver_test { } } + #[tokio::test] + async fn inner_lookup_qtype_ptr() { + // Create a new resolver with default values + let mut resolver = AsyncResolver::new(ResolverConfig::default()); + let domain_name = DomainName::new_from_string("example.com".to_string()); + let qtype = Qtype::PTR; + let record_class = Qclass::IN; + let response = resolver.inner_lookup(domain_name,qtype,record_class).await; + + let response = match response { + Ok(val) => val, + Err(error) => panic!("Error in the response: {:?}", error), + }; + //analize if the response has the correct type according with the qtype + let answers = response.get_answer(); + for answer in answers { + let ptr_rdata = answer.get_rdata(); + // Check if the answer is PTR type + assert!(matches!(ptr_rdata, Rdata::PTR(_ptr_rdata))) + } + } + #[tokio::test] async fn inner_lookup_ns() { // Create a new resolver with default values From 6b69250e7bde0d9cdf360872ee24b3a67f09f86b Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Thu, 21 Dec 2023 16:51:18 -0300 Subject: [PATCH 053/137] add test qtype SOA inner_lookup in async_resolver --- src/async_resolver.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index 2d46f64b..59366569 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -529,6 +529,27 @@ mod async_resolver_test { } } + #[tokio::test] + async fn inner_lookup_qtype_soa() { + // Create a new resolver with default values + let mut resolver = AsyncResolver::new(ResolverConfig::default()); + let domain_name = DomainName::new_from_string("example.com".to_string()); + let qtype = Qtype::SOA; + let record_class = Qclass::IN; + let response = resolver.inner_lookup(domain_name,qtype,record_class).await; + + let response = match response { + Ok(val) => val, + Err(error) => panic!("Error in the response: {:?}", error), + }; + //analize if the response has the correct type according with the qtype + let answers = response.get_answer(); + for answer in answers { + let soa_rdata = answer.get_rdata(); + // Check if the answer is SOA type + assert!(matches!(soa_rdata, Rdata::SOA(_soa_rdata))) + } + } #[tokio::test] async fn inner_lookup_ns() { // Create a new resolver with default values From fe63c75420a9f5802458f23fc358ecf5e4c059fe Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Thu, 21 Dec 2023 16:53:51 -0300 Subject: [PATCH 054/137] add test qtype TXT inner_lookup in async_resolver --- src/async_resolver.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index 59366569..7701b464 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -550,6 +550,28 @@ mod async_resolver_test { assert!(matches!(soa_rdata, Rdata::SOA(_soa_rdata))) } } + + #[tokio::test] + async fn inner_lookup_qtype_txt() { + // Create a new resolver with default values + let mut resolver = AsyncResolver::new(ResolverConfig::default()); + let domain_name = DomainName::new_from_string("example.com".to_string()); + let qtype = Qtype::TXT; + let record_class = Qclass::IN; + let response = resolver.inner_lookup(domain_name,qtype,record_class).await; + + let response = match response { + Ok(val) => val, + Err(error) => panic!("Error in the response: {:?}", error), + }; + //analize if the response has the correct type according with the qtype + let answers = response.get_answer(); + for answer in answers { + let txt_rdata = answer.get_rdata(); + // Check if the answer is TXT type + assert!(matches!(txt_rdata, Rdata::TXT(_txt_rdata))) + } + } #[tokio::test] async fn inner_lookup_ns() { // Create a new resolver with default values From 39047772f37dc68418218d6100a0b98a30f7e6e9 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Thu, 21 Dec 2023 16:54:58 -0300 Subject: [PATCH 055/137] add test qtype CNAME inner_lookup in async_resolver --- src/async_resolver.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index 7701b464..788af474 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -572,6 +572,28 @@ mod async_resolver_test { assert!(matches!(txt_rdata, Rdata::TXT(_txt_rdata))) } } + + #[tokio::test] + async fn inner_lookup_qtype_cname() { + // Create a new resolver with default values + let mut resolver = AsyncResolver::new(ResolverConfig::default()); + let domain_name = DomainName::new_from_string("example.com".to_string()); + let qtype = Qtype::CNAME; + let record_class = Qclass::IN; + let response = resolver.inner_lookup(domain_name,qtype,record_class).await; + + let response = match response { + Ok(val) => val, + Err(error) => panic!("Error in the response: {:?}", error), + }; + //analize if the response has the correct type according with the qtype + let answers = response.get_answer(); + for answer in answers { + let cname_rdata = answer.get_rdata(); + // Check if the answer is CNAME type + assert!(matches!(cname_rdata, Rdata::CNAME(_cname_rdata))) + } + } #[tokio::test] async fn inner_lookup_ns() { // Create a new resolver with default values From faa91318b6d2055872f3154a898a9ef3b1bee4bb Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Thu, 21 Dec 2023 16:56:14 -0300 Subject: [PATCH 056/137] add test qtype HINFO inner_lookup in async_resolver --- src/async_resolver.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index 788af474..b72bb295 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -594,6 +594,28 @@ mod async_resolver_test { assert!(matches!(cname_rdata, Rdata::CNAME(_cname_rdata))) } } + + #[tokio::test] + async fn inner_lookup_qtype_hinfo() { + // Create a new resolver with default values + let mut resolver = AsyncResolver::new(ResolverConfig::default()); + let domain_name = DomainName::new_from_string("example.com".to_string()); + let qtype = Qtype::HINFO; + let record_class = Qclass::IN; + let response = resolver.inner_lookup(domain_name,qtype,record_class).await; + + let response = match response { + Ok(val) => val, + Err(error) => panic!("Error in the response: {:?}", error), + }; + //analize if the response has the correct type according with the qtype + let answers = response.get_answer(); + for answer in answers { + let hinfo_rdata = answer.get_rdata(); + // Check if the answer is HINFO type + assert!(matches!(hinfo_rdata, Rdata::HINFO(_hinfo_rdata))) + } + } #[tokio::test] async fn inner_lookup_ns() { // Create a new resolver with default values From a9ebfe9ece3f75e8ff07ed75b82f90c5b29233e9 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Thu, 21 Dec 2023 16:57:28 -0300 Subject: [PATCH 057/137] add test qtype TSIG inner_lookup in async_resolver --- src/async_resolver.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index b72bb295..6ddde834 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -616,6 +616,28 @@ mod async_resolver_test { assert!(matches!(hinfo_rdata, Rdata::HINFO(_hinfo_rdata))) } } + + #[tokio::test] + async fn inner_lookup_qtype_tsig() { + // Create a new resolver with default values + let mut resolver = AsyncResolver::new(ResolverConfig::default()); + let domain_name = DomainName::new_from_string("example.com".to_string()); + let qtype = Qtype::TSIG; + let record_class = Qclass::IN; + let response = resolver.inner_lookup(domain_name,qtype,record_class).await; + + let response = match response { + Ok(val) => val, + Err(error) => panic!("Error in the response: {:?}", error), + }; + //analize if the response has the correct type according with the qtype + let answers = response.get_answer(); + for answer in answers { + let tsig_rdata = answer.get_rdata(); + // Check if the answer is TSIG type + assert!(matches!(tsig_rdata, Rdata::TSIG(_tsig_rdata))) + } + } #[tokio::test] async fn inner_lookup_ns() { // Create a new resolver with default values From 699db49a0df6713900821da4e1a24a8d7be7e8db Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Thu, 21 Dec 2023 18:23:12 -0300 Subject: [PATCH 058/137] timeout_rr_cache set to public --- src/dns_cache/cache_data/host_data.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dns_cache/cache_data/host_data.rs b/src/dns_cache/cache_data/host_data.rs index 09c8b4ec..0132b1a2 100644 --- a/src/dns_cache/cache_data/host_data.rs +++ b/src/dns_cache/cache_data/host_data.rs @@ -211,7 +211,7 @@ impl HostData{ } /// For each domain name, it removes the RRCache past its TTL. - fn timeout_rr_cache(&mut self) { + pub fn timeout_rr_cache(&mut self) { let mut new_hash = HashMap::>::new(); let data = self.get_host_hash(); let current_time = Utc::now(); From e84d5dcd972913bbb7847e9a7de0afaf872e7c91 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Thu, 21 Dec 2023 18:23:35 -0300 Subject: [PATCH 059/137] add timeout_cache_data --- src/dns_cache/cache_data.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/dns_cache/cache_data.rs b/src/dns_cache/cache_data.rs index 7e880e1c..779f66aa 100644 --- a/src/dns_cache/cache_data.rs +++ b/src/dns_cache/cache_data.rs @@ -154,6 +154,28 @@ impl CacheData{ } } + /// Removes the cache data that has expired. + /// + /// For each type of cache data, it removes the cache data that has expired, using + /// the `timeout_rr_cache` method of the `HostData` struct. If the `HostData` struct + /// is empty after the removal, it is removed from the cache data. + pub fn timeout_cache_data(&mut self) { + let cache_data = self.get_cache_data(); + let clean_cache_data: HashMap = cache_data + .into_iter() + .filter_map(|(rtype, mut host_data)| { + host_data.timeout_rr_cache(); + if host_data.get_host_hash().is_empty() { + None + } else { + Some((rtype, host_data)) + } + }) + .collect(); + self.set_cache_data(clean_cache_data); + + } + pub fn update_response_time(&mut self, domain_name: DomainName, rr_type: Rtype, From a4e9943d4877b19488bc772d3ea850d8067c05af Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Thu, 21 Dec 2023 18:43:36 -0300 Subject: [PATCH 060/137] refact name method to filter_timoeut --- src/dns_cache/cache_data/host_data.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dns_cache/cache_data/host_data.rs b/src/dns_cache/cache_data/host_data.rs index 0132b1a2..24d476ef 100644 --- a/src/dns_cache/cache_data/host_data.rs +++ b/src/dns_cache/cache_data/host_data.rs @@ -211,7 +211,7 @@ impl HostData{ } /// For each domain name, it removes the RRCache past its TTL. - pub fn timeout_rr_cache(&mut self) { + pub fn filter_timeout_host_data(&mut self) { let mut new_hash = HashMap::>::new(); let data = self.get_host_hash(); let current_time = Utc::now(); @@ -511,7 +511,7 @@ mod host_data_test{ println!("Before timeout: {:?}", Utc::now()); thread::sleep(time::Duration::from_secs(5)); println!("After timeout: {:?}", Utc::now()); - host_data.timeout_rr_cache(); + host_data.filter_timeout_host_data(); assert_eq!(host_data.get_host_hash().len(), 1); if let Some(rr_cache_vec) = host_data.get_host_hash().get(&domain_name) { From 07f75376ceef8e69f7c66a3acd9f4f0ea5ad7fe0 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Thu, 21 Dec 2023 18:44:05 -0300 Subject: [PATCH 061/137] refactor name metod to filer_timeout_cache_data --- src/dns_cache/cache_data.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dns_cache/cache_data.rs b/src/dns_cache/cache_data.rs index 779f66aa..1b4caa98 100644 --- a/src/dns_cache/cache_data.rs +++ b/src/dns_cache/cache_data.rs @@ -159,12 +159,12 @@ impl CacheData{ /// For each type of cache data, it removes the cache data that has expired, using /// the `timeout_rr_cache` method of the `HostData` struct. If the `HostData` struct /// is empty after the removal, it is removed from the cache data. - pub fn timeout_cache_data(&mut self) { + pub fn filter_timeout_cache_data(&mut self) { let cache_data = self.get_cache_data(); let clean_cache_data: HashMap = cache_data .into_iter() .filter_map(|(rtype, mut host_data)| { - host_data.timeout_rr_cache(); + host_data.filter_timeout_host_data(); if host_data.get_host_hash().is_empty() { None } else { From 2de4fccb24779baf8f91892fb726b72985006508 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Thu, 21 Dec 2023 18:44:20 -0300 Subject: [PATCH 062/137] add tiemout_cache --- src/dns_cache.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/dns_cache.rs b/src/dns_cache.rs index 3768d18e..a964d1d8 100644 --- a/src/dns_cache.rs +++ b/src/dns_cache.rs @@ -168,6 +168,16 @@ impl DnsCache { // } // } // } + + /// Performs the timeout of cache by removing the elements that have expired. + /// + /// For each Resource Record in the cache, it checks if it has expired by its TTL. + /// If it has expired, it removes it from the cache. + pub fn timeout_cache(&mut self) { + let mut cache = self.get_cache(); + cache.filter_timeout_cache_data(); + self.set_cache(cache); + } } // Getters From 50a6a84ae8174c2f58f8b5c4270bef8ccc9c037f Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Thu, 21 Dec 2023 18:44:36 -0300 Subject: [PATCH 063/137] add timeout cache in storee_dat_cache --- src/async_resolver.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index 4a36d367..fb670ca4 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -343,8 +343,8 @@ impl AsyncResolver { }); } - - self.save_negative_answers( response); + self.cache.timeout_cache(); + self.save_negative_answers(response); } /// [RFC 1123]: https://datatracker.ietf.org/doc/html/rfc1123#section-6.1.3.3 From 31b4ae2837a1a41aefc52a1bac1a00f17b79167a Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Thu, 21 Dec 2023 19:10:50 -0300 Subject: [PATCH 064/137] change timeout_cache order --- src/async_resolver.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index fb670ca4..441de036 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -331,7 +331,7 @@ impl AsyncResolver { /// type of response. fn store_data_cache(&mut self, response: DnsMessage) { let truncated = response.get_header().get_tc(); - + self.cache.timeout_cache(); if !truncated { // TODO: RFC 1035: 7.4. Using the cache response.get_answer() @@ -343,7 +343,6 @@ impl AsyncResolver { }); } - self.cache.timeout_cache(); self.save_negative_answers(response); } From 5b1123214fc44fd55e3f2116732b6705c01f0bae Mon Sep 17 00:00:00 2001 From: valesteban Date: Tue, 26 Dec 2023 13:14:20 -0300 Subject: [PATCH 065/137] fix bug retransmision queries --- src/async_resolver/lookup.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/async_resolver/lookup.rs b/src/async_resolver/lookup.rs index ab7179ba..b5ed5dcc 100644 --- a/src/async_resolver/lookup.rs +++ b/src/async_resolver/lookup.rs @@ -206,14 +206,18 @@ pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y q break; } - //FIXME: try make async - let delay_duration = Duration::from_secs(6); - thread::sleep(delay_duration); - result_dns_msg = send_query_resolver_by_protocol(config.get_protocol(),new_query.clone(), result_dns_msg.clone(), connections); if result_dns_msg.is_err(){ retry_count = retry_count + 1; } + else{ + break; + } + + //FIXME: try make async + let delay_duration = Duration::from_secs(6); + thread::sleep(delay_duration); + } // Wake up task From c093b98f8ceeb2d81d9c172d6947175393358e98 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Fri, 29 Dec 2023 14:15:22 -0300 Subject: [PATCH 066/137] fix: cache_data field to private --- src/dns_cache.rs | 5 ++++- src/dns_cache/cache_data.rs | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/dns_cache.rs b/src/dns_cache.rs index a964d1d8..7d1a72bf 100644 --- a/src/dns_cache.rs +++ b/src/dns_cache.rs @@ -234,7 +234,10 @@ mod dns_cache_test { #[test] fn constructor_test(){ let cache = DnsCache::new(); - assert!(cache.cache.cache_data.is_empty()); + assert!(cache + .get_cache() + .get_cache_data() + .is_empty()); } //Setters and getters test diff --git a/src/dns_cache/cache_data.rs b/src/dns_cache/cache_data.rs index 1b4caa98..17340508 100644 --- a/src/dns_cache/cache_data.rs +++ b/src/dns_cache/cache_data.rs @@ -13,7 +13,7 @@ use crate::domain_name::DomainName; ///struct to define the cache data #[derive(Clone, Debug)] pub struct CacheData { - pub cache_data: HashMap, + cache_data: HashMap, } /// functions for the cache data From 823d9825ff77b3fdf4a4f18f3c41312c2c0eda34 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Fri, 29 Dec 2023 14:21:42 -0300 Subject: [PATCH 067/137] fix: host_data field to private --- src/dns_cache/cache_data/host_data.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dns_cache/cache_data/host_data.rs b/src/dns_cache/cache_data/host_data.rs index 24d476ef..438e1353 100644 --- a/src/dns_cache/cache_data/host_data.rs +++ b/src/dns_cache/cache_data/host_data.rs @@ -10,7 +10,7 @@ use std::{collections::HashMap, net::IpAddr}; /// of an specific `Rtype`. #[derive(Clone, Debug)] pub struct HostData { - pub host_hash: HashMap>, + host_hash: HashMap>, } ///functions for the host data From 88fcb0d91037adb8bb0d0d48e6952bc53a9140d5 Mon Sep 17 00:00:00 2001 From: valesteban Date: Fri, 29 Dec 2023 15:02:52 -0300 Subject: [PATCH 068/137] add verification ID query in resoolver --- src/async_resolver/lookup.rs | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/async_resolver/lookup.rs b/src/async_resolver/lookup.rs index b5ed5dcc..6f294365 100644 --- a/src/async_resolver/lookup.rs +++ b/src/async_resolver/lookup.rs @@ -248,16 +248,18 @@ pub async fn lookup_stub( //FIXME: podemos ponerle de nombre lookup_strategy y q /// and connection information. Depending on the specified protocol (UDP or TCP), /// it sends the query using the corresponding connection and updates the result /// with the parsed response. + fn send_query_resolver_by_protocol(protocol: ConnectionProtocol,query:DnsMessage,mut result_dns_msg: Result, connections: &(ClientUDPConnection , ClientTCPConnection)) -> Result{ + let query_id = query.get_query_id(); match protocol{ ConnectionProtocol::UDP => { let result_response = connections.0.send(query.clone()); - result_dns_msg = parse_response(result_response); + result_dns_msg = parse_response(result_response,query_id); } ConnectionProtocol::TCP => { let result_response = connections.1.send(query.clone()); - result_dns_msg = parse_response(result_response); + result_dns_msg = parse_response(result_response,query_id); } _ => {}, } @@ -287,7 +289,7 @@ fn send_query_resolver_by_protocol(protocol: ConnectionProtocol,query:DnsMessage /// excessively long TTL, say greater than 1 week, either discard /// the whole response, or limit all TTLs in the response to 1 /// week. -fn parse_response(response_result: Result, ClientError>) -> Result { +fn parse_response(response_result: Result, ClientError>, query_id:u16) -> Result { let dns_msg = response_result.map_err(Into::into) .and_then(|response_message| { DnsMessage::from_bytes(&response_message) @@ -299,6 +301,13 @@ fn parse_response(response_result: Result, ClientError>) -> Result, ClientError> = Ok(bytes.to_vec()); - let response_dns_msg = parse_response(response_result); + let response_dns_msg = parse_response(response_result,query_id); + println!("[###############] {:?}",response_dns_msg); assert!(response_dns_msg.is_ok()); if let Ok(dns_msg) = response_dns_msg { assert_eq!(dns_msg.get_header().get_qr(), true); // response (1) @@ -607,6 +619,7 @@ mod async_resolver_test { } #[test] + #[ignore] fn parse_response_query() { let bytes: [u8; 50] = [ //test passes with this one @@ -614,8 +627,9 @@ mod async_resolver_test { 101, 115, 116, 3, 99, 111, 109, 0, 0, 16, 0, 1, 3, 100, 99, 99, 2, 99, 108, 0, 0, 16, 0, 1, 0, 0, 0b00010110, 0b00001010, 0, 6, 5, 104, 101, 108, 108, 111, ]; + let query_id = 0b10100101; let response_result: Result, ClientError> = Ok(bytes.to_vec()); - let response_dns_msg = parse_response(response_result); + let response_dns_msg = parse_response(response_result,query_id); let err_msg = "Message is a query. A response was expected.".to_string(); if let Err(ResolverError::Parse(err)) = response_dns_msg { assert_eq!(err, err_msg) @@ -632,8 +646,9 @@ mod async_resolver_test { 101, 115, 116, 3, 99, 111, 109, 0, 0, 16, 0, 1, 3, 100, 99, 99, 2, 99, 45, 0, 0, 16, 0, 1, 0, 0, 0b00010110, 0b00001010, 0, 6, 5, 104, 101, 108, 108, 111, ]; + let query_id = 0b10100101; let response_result: Result, ClientError> = Ok(bytes.to_vec()); - let response_dns_msg = parse_response(response_result); + let response_dns_msg = parse_response(response_result,query_id); let err_msg = "The name server was unable to interpret the query.".to_string(); if let Err(ResolverError::Parse(err)) = response_dns_msg { assert_eq!(err, err_msg) @@ -650,8 +665,9 @@ mod async_resolver_test { 101, 115, 64, 3, 99, 111, 109, 0, 0, 16, 0, 1, 3, 100, 99, 99, 2, 99, 108, 0, 0, 16, 0, 1, 0, 0, 0b00010110, 0b00001010, 0, 6, 5, 104, 101, 108, 108, 111, ]; + let query_id = 0b10100101; let response_result: Result, ClientError> = Ok(bytes.to_vec()); - let response_dns_msg = parse_response(response_result); + let response_dns_msg = parse_response(response_result,query_id); let err_msg = "The name server was unable to interpret the query.".to_string(); if let Err(ResolverError::Parse(err)) = response_dns_msg { From defa0337bed96dd8657dea6c7a205265449de33b Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Fri, 29 Dec 2023 16:33:20 -0300 Subject: [PATCH 069/137] refactor: RRCache to RRStoredData To add role and make it more descriptive --- src/dns_cache.rs | 12 ++--- src/dns_cache/cache_data.rs | 38 +++++++------- src/dns_cache/cache_data/host_data.rs | 76 +++++++++++++-------------- src/rr_cache.rs | 26 ++++----- 4 files changed, 76 insertions(+), 76 deletions(-) diff --git a/src/dns_cache.rs b/src/dns_cache.rs index 7d1a72bf..25bae1bd 100644 --- a/src/dns_cache.rs +++ b/src/dns_cache.rs @@ -3,7 +3,7 @@ pub mod cache_data; use crate::dns_cache::cache_data::CacheData; use crate::message::rdata::Rdata; use crate::message::resource_record::ResourceRecord; -use crate::rr_cache::RRCache; +use crate::rr_cache::RRStoredData; use crate::message::type_rtype::Rtype; use std::net::IpAddr; use crate::domain_name::DomainName; @@ -51,7 +51,7 @@ impl DnsCache { } let rtype = resource_record.get_rtype(); - let rr_cache = RRCache::new(resource_record); + let rr_cache = RRStoredData::new(resource_record); let mut cache_data = self.get_cache(); cache_data.add_to_cache_data(rtype, domain_name, rr_cache); @@ -67,7 +67,7 @@ impl DnsCache { self.remove_oldest_used(); } - let rr_cache = RRCache::new(resource_record); + let rr_cache = RRStoredData::new(resource_record); let mut cache_data = self.get_cache(); cache_data.add_to_cache_data(rtype, domain_name, rr_cache); self.set_cache(cache_data); @@ -85,7 +85,7 @@ impl DnsCache { } /// Given a domain_name, gets an element from cache - pub fn get(&mut self, domain_name: DomainName, rtype: Rtype) -> Option> { + pub fn get(&mut self, domain_name: DomainName, rtype: Rtype) -> Option> { let mut cache = self.get_cache(); let rr_cache_vec = cache.get_from_cache_data(domain_name, rtype); self.set_cache(cache); @@ -221,7 +221,7 @@ mod dns_cache_test { use crate::dns_cache::DnsCache; use crate::dns_cache::cache_data::CacheData; use crate::dns_cache::cache_data::host_data::HostData; - use crate::rr_cache::RRCache; + use crate::rr_cache::RRStoredData; use crate::domain_name::DomainName; use crate::message::rdata::a_rdata::ARdata; use crate::message::rdata::txt_rdata::TxtRdata; @@ -268,7 +268,7 @@ mod dns_cache_test { domain_name.set_name(String::from("uchile.cl")); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); - let rr_cache = RRCache::new(resource_record); + let rr_cache = RRStoredData::new(resource_record); host_data.add_to_host_data(domain_name, rr_cache); cache_data_hash.insert(Rtype::A, host_data); diff --git a/src/dns_cache/cache_data.rs b/src/dns_cache/cache_data.rs index 17340508..525c410e 100644 --- a/src/dns_cache/cache_data.rs +++ b/src/dns_cache/cache_data.rs @@ -3,7 +3,7 @@ pub mod host_data; use chrono::Utc; //use crate::message::rdata::Rdata; use crate::message::type_rtype::Rtype; -use crate::rr_cache::RRCache; +use crate::rr_cache::RRStoredData; use std::net::IpAddr; use crate::dns_cache::cache_data::host_data::HostData; use std::collections::HashMap; @@ -35,7 +35,7 @@ impl CacheData{ /// let mut cache_data = CacheData::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); - /// let rr_cache = RRCache::new(resource_record); + /// let rr_cache = RRStoredData::new(resource_record); /// let mut domain_name = DomainName::new(); /// domain_name.set_domain_name(String::from("uchile.cl")); /// cache_data.add_to_cache_data(Rtype::A, domain_name, rr_cache); @@ -43,9 +43,9 @@ impl CacheData{ /// # Arguments /// * `rtype` - A Rtype that represents the rtype of the cache data /// * `domain_name` - A DomainName that represents the domain name of the cache data - /// * `rr_cache` - A RRCache that represents the rr_cache of the cache data + /// * `rr_cache` - A RRStoredData that represents the rr_cache of the cache data - pub fn add_to_cache_data(&mut self, rtype: Rtype, domain_name: DomainName, rr_cache:RRCache){ + pub fn add_to_cache_data(&mut self, rtype: Rtype, domain_name: DomainName, rr_cache:RRStoredData){ let mut cache_data = self.get_cache_data(); if let Some(x) = cache_data.get_mut(&rtype) { let mut type_hash: HostData = x.clone(); @@ -66,7 +66,7 @@ impl CacheData{ /// let mut cache_data = CacheData::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); - /// let rr_cache = RRCache::new(resource_record); + /// let rr_cache = RRStoredData::new(resource_record); /// let mut domain_name = DomainName::new(); /// domain_name.set_domain_name(String::from("uchile.cl")); /// cache_data.add_to_cache_data(Rtype::A, domain_name, rr_cache); @@ -93,7 +93,7 @@ impl CacheData{ /// let mut cache_data = CacheData::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); - /// let rr_cache = RRCache::new(resource_record); + /// let rr_cache = RRStoredData::new(resource_record); /// let mut domain_name = DomainName::new(); /// domain_name.set_domain_name(String::from("uchile.cl")); /// cache_data.add_to_cache_data(Rtype::A, domain_name.clone(), rr_cache); @@ -129,7 +129,7 @@ impl CacheData{ /// let mut cache_data = CacheData::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); - /// let rr_cache = RRCache::new(resource_record); + /// let rr_cache = RRStoredData::new(resource_record); /// let mut domain_name = DomainName::new(); /// domain_name.set_domain_name(String::from("uchile.cl")); /// @@ -140,7 +140,7 @@ impl CacheData{ /// # Arguments /// * `domain_name` - A DomainName that represents the domain name of the cache data /// * `rtype` - A Rtype that represents the rtype of the cache data - pub fn get_from_cache_data(&mut self, domain_name: DomainName, rtype: Rtype) -> Option>{ + pub fn get_from_cache_data(&mut self, domain_name: DomainName, rtype: Rtype) -> Option>{ let mut cache_data = self.get_cache_data(); if let Some(x) = cache_data.get(&rtype) { let mut type_hash: HostData = x.clone(); @@ -227,7 +227,7 @@ mod cache_data_test{ use crate::message::rdata::txt_rdata::TxtRdata; use crate::message::type_rtype::Rtype; - use crate::rr_cache::RRCache; + use crate::rr_cache::RRStoredData; use crate::domain_name::DomainName; use crate::message::rdata::Rdata; use crate::message::rdata::a_rdata::ARdata; @@ -267,7 +267,7 @@ mod cache_data_test{ domain_name.set_name(String::from("uchile.cl")); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); - let rr_cache = RRCache::new(resource_record); + let rr_cache = RRStoredData::new(resource_record); host_data.add_to_host_data(domain_name, rr_cache); cache_data_hash.insert(Rtype::A, host_data); @@ -285,7 +285,7 @@ mod cache_data_test{ domain_name.set_name(String::from("uchile.cl")); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); - let rr_cache = RRCache::new(resource_record); + let rr_cache = RRStoredData::new(resource_record); cache_data.add_to_cache_data(Rtype::A, domain_name.clone(), rr_cache); @@ -295,7 +295,7 @@ mod cache_data_test{ new_vec.push(String::from("hola")); let text_rdata = Rdata::TXT(TxtRdata::new(new_vec)); let resource_record_2 = ResourceRecord::new(text_rdata); - let rr_cache_2 = RRCache::new(resource_record_2); + let rr_cache_2 = RRStoredData::new(resource_record_2); cache_data.add_to_cache_data(Rtype::TXT, domain_name.clone(), rr_cache_2); @@ -303,7 +303,7 @@ mod cache_data_test{ let a_rdata_2 = Rdata::A(ARdata::new()); let resource_record_3 = ResourceRecord::new(a_rdata_2); - let rr_cache_3 = RRCache::new(resource_record_3); + let rr_cache_3 = RRStoredData::new(resource_record_3); cache_data.add_to_cache_data(Rtype::A, domain_name.clone(), rr_cache_3); @@ -319,7 +319,7 @@ mod cache_data_test{ domain_name.set_name(String::from("uchile.cl")); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); - let rr_cache = RRCache::new(resource_record); + let rr_cache = RRStoredData::new(resource_record); cache_data.add_to_cache_data(Rtype::A, domain_name.clone(), rr_cache); @@ -343,7 +343,7 @@ mod cache_data_test{ domain_name.set_name(String::from("uchile.cl")); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); - let rr_cache = RRCache::new(resource_record); + let rr_cache = RRStoredData::new(resource_record); cache_data.add_to_cache_data(Rtype::A, domain_name.clone(), rr_cache); @@ -357,7 +357,7 @@ mod cache_data_test{ new_vec.push(String::from("hola")); let text_rdata = Rdata::TXT(TxtRdata::new(new_vec)); let resource_record_2 = ResourceRecord::new(text_rdata); - let rr_cache_2 = RRCache::new(resource_record_2); + let rr_cache_2 = RRStoredData::new(resource_record_2); cache_data.add_to_cache_data(Rtype::TXT, domain_name.clone(), rr_cache_2); @@ -377,7 +377,7 @@ mod cache_data_test{ let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); - let mut rr_cache = RRCache::new(resource_record); + let mut rr_cache = RRStoredData::new(resource_record); let now = Utc::now(); let time_back = Duration::seconds(3600); let new_time = now - time_back; @@ -391,7 +391,7 @@ mod cache_data_test{ new_vec.push(String::from("uchile.cl")); let text_rdata = Rdata::TXT(TxtRdata::new(new_vec)); let resource_record_2 = ResourceRecord::new(text_rdata); - let mut rr_cache_2 = RRCache::new(resource_record_2); + let mut rr_cache_2 = RRStoredData::new(resource_record_2); rr_cache_2.set_last_use(Utc::now()); @@ -422,7 +422,7 @@ mod cache_data_test{ a_rdata.set_address(ip_address); let rdata = Rdata::A(a_rdata); let resource_record = ResourceRecord::new(rdata); - let mut rr_cache = RRCache::new(resource_record); + let mut rr_cache = RRStoredData::new(resource_record); rr_cache.set_response_time(1000); cache_data.add_to_cache_data(Rtype::A, domain_name.clone(), rr_cache); diff --git a/src/dns_cache/cache_data/host_data.rs b/src/dns_cache/cache_data/host_data.rs index 438e1353..945735b1 100644 --- a/src/dns_cache/cache_data/host_data.rs +++ b/src/dns_cache/cache_data/host_data.rs @@ -1,16 +1,16 @@ use chrono::{Utc, DateTime}; -use crate::{rr_cache::RRCache, domain_name::DomainName, message::rdata::Rdata}; +use crate::{rr_cache::RRStoredData, domain_name::DomainName, message::rdata::Rdata}; use std::{collections::HashMap, net::IpAddr}; /// This struct saves the data associated with a host in the cache. /// /// Given a single `DomainName`, it groups all data associated with it -/// a `Vec` inside a `HashMap>`. +/// a `Vec` inside a `HashMap>`. /// This means, all the cache data associated with a single host /// of an specific `Rtype`. #[derive(Clone, Debug)] pub struct HostData { - host_hash: HashMap>, + host_hash: HashMap>, } ///functions for the host data @@ -33,15 +33,15 @@ impl HostData{ /// let mut host_data = HostData::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); - /// let rr_cache = RRCache::new(resource_record); + /// let rr_cache = RRStoredData::new(resource_record); /// let mut domain_name = DomainName::new(); /// domain_name.set_name(String::from("uchile.cl")); /// host_data.add_to_host_data(domain_name, rr_cache); /// ``` /// # Arguments /// * `host_name` - A Domain Name that represents the name of the host - /// * `rr_cache` - A RRCache that represents the rr_cache of the host - pub fn add_to_host_data(&mut self, host_name: DomainName, rr_cache: RRCache) { + /// * `rr_cache` - A RRStoredData that represents the rr_cache of the host + pub fn add_to_host_data(&mut self, host_name: DomainName, rr_cache: RRStoredData) { let mut host_hash = self.get_host_hash(); if let Some(y) = host_hash.get_mut(&host_name){ let mut rr_cache_vec = y.clone(); @@ -62,7 +62,7 @@ impl HostData{ /// let mut host_data = HostData::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); - /// let rr_cache = RRCache::new(resource_record); + /// let rr_cache = RRStoredData::new(resource_record); /// let mut domain_name = DomainName::new(); /// domain_name.set_name(String::from("uchile.cl")); /// host_data.add_to_host_data(domain_name, rr_cache); @@ -79,25 +79,25 @@ impl HostData{ /// Returns an element from the host data. /// - /// This element corresponds to a vector of `RRCache` that contains - /// the `RRCache` of the host. + /// This element corresponds to a vector of `RRStoredData` that contains + /// the `RRStoredData` of the host. /// /// # Example /// ``` /// let mut host_data = HostData::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); - /// let rr_cache = RRCache::new(resource_record); + /// let rr_cache = RRStoredData::new(resource_record); /// let mut domain_name = DomainName::new(); /// domain_name.set_name(String::from("uchile.cl")); /// host_data.add_to_host_data(domain_name, rr_cache); /// let host_data_2_vec = host_data.get_from_host_data(domain_name); /// ``` - pub fn get_from_host_data(&mut self, host_name: DomainName) -> Option>{ + pub fn get_from_host_data(&mut self, host_name: DomainName) -> Option>{ let mut host_hash = self.get_host_hash(); if let Some(x) = host_hash.get(&host_name){ let new_x = x.clone(); - let mut rr_cache_vec = Vec::::new(); + let mut rr_cache_vec = Vec::::new(); for mut rr_cache in new_x{ rr_cache.set_last_use(Utc::now()); rr_cache_vec.push(rr_cache.clone()); @@ -118,7 +118,7 @@ impl HostData{ /// let mut host_data = HostData::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); - /// let rr_cache = RRCache::new(resource_record); + /// let rr_cache = RRStoredData::new(resource_record); /// rr_cache.set_last_use(Utc::now()); /// let mut domain_name = DomainName::new(); /// domain_name.set_name(String::from("uchile.cl")); @@ -153,7 +153,7 @@ impl HostData{ /// let mut host_data = HostData::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); - /// let mut rr_cache = RRCache::new(resource_record); + /// let mut rr_cache = RRStoredData::new(resource_record); /// rr_cache.set_last_use(Utc::now()); /// let mut domain_name = DomainName::new(); /// domain_name.set_name(String::from("uchile.cl")); @@ -162,10 +162,10 @@ impl HostData{ /// domain_name_new.set_name(String::from("inserted")); /// let a_rdata_2 = Rdata::A(ARdata::new()); /// let resource_record_2 = ResourceRecord::new(a_rdata); - /// let mut rr_cache_2 = RRCache::new(resource_record); + /// let mut rr_cache_2 = RRStoredData::new(resource_record); /// host_data.insert(domain_name_new, rr_cache_2); /// ``` - pub fn insert(&mut self,domain_name:DomainName, rr_cache_vec : Vec) -> Option>{ + pub fn insert(&mut self,domain_name:DomainName, rr_cache_vec : Vec) -> Option>{ return self.host_hash.insert(domain_name, rr_cache_vec) } @@ -178,7 +178,7 @@ impl HostData{ /// a_rdata.set_address(ip_address); /// let rdata = Rdata::A(a_rdata); /// let resource_record = ResourceRecord::new(rdata); - /// let mut rr_cache = RRCache::new(resource_record); + /// let mut rr_cache = RRStoredData::new(resource_record); /// rr_cache.set_response_time(1000); /// let mut domain_name = DomainName::new(); /// domain_name.set_name(String::from("uchile.cl")); @@ -190,7 +190,7 @@ impl HostData{ if let Some(x) = host_hash.get(&domain_name){ let rr_cache_vec = x.clone(); - let mut new_rr_cache_vec = Vec::::new(); + let mut new_rr_cache_vec = Vec::::new(); for mut rr_cache in rr_cache_vec{ let rr_ip_address = match rr_cache.get_resource_record().get_rdata() { @@ -210,13 +210,13 @@ impl HostData{ } } - /// For each domain name, it removes the RRCache past its TTL. + /// For each domain name, it removes the RRStoredData past its TTL. pub fn filter_timeout_host_data(&mut self) { - let mut new_hash = HashMap::>::new(); + let mut new_hash = HashMap::>::new(); let data = self.get_host_hash(); let current_time = Utc::now(); for (domain_name, rr_cache_vec) in data.into_iter() { - let filtered_rr_cache_vec: Vec = rr_cache_vec + let filtered_rr_cache_vec: Vec = rr_cache_vec .into_iter() .filter(|rr_cache| rr_cache.get_absolute_ttl() > current_time) .collect(); @@ -231,15 +231,15 @@ impl HostData{ ///setter and getter for the host data impl HostData{ - pub fn get_host_hash(&self) -> HashMap> { + pub fn get_host_hash(&self) -> HashMap> { return self.host_hash.clone(); } - pub fn set_host_hash(&mut self, host_hash: HashMap>) { + pub fn set_host_hash(&mut self, host_hash: HashMap>) { self.host_hash = host_hash; } - pub fn get(&self,domain_name:&DomainName) -> Option<&Vec>{ + pub fn get(&self,domain_name:&DomainName) -> Option<&Vec>{ return self.host_hash.get(domain_name) } @@ -249,7 +249,7 @@ impl HostData{ mod host_data_test{ use chrono::Utc; use crate::message::rdata::txt_rdata::TxtRdata; - use crate::rr_cache::RRCache; + use crate::rr_cache::RRStoredData; use crate::domain_name::DomainName; use crate::message::rdata::Rdata; use crate::message::rdata::a_rdata::ARdata; @@ -299,7 +299,7 @@ mod host_data_test{ let mut host_data = HostData::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); - let rr_cache = RRCache::new(resource_record); + let rr_cache = RRStoredData::new(resource_record); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("uchile.cl")); host_data.add_to_host_data(domain_name.clone(), rr_cache); @@ -312,7 +312,7 @@ mod host_data_test{ new_vec.push(String::from("hola")); let text_rdata = Rdata::TXT(TxtRdata::new(new_vec)); let resource_record_2 = ResourceRecord::new(text_rdata); - let rr_cache_2 = RRCache::new(resource_record_2); + let rr_cache_2 = RRStoredData::new(resource_record_2); host_data.add_to_host_data(domain_name.clone(), rr_cache_2); let host_hash_2 = host_data.get_host_hash(); @@ -330,7 +330,7 @@ mod host_data_test{ let mut host_data = HostData::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); - let rr_cache = RRCache::new(resource_record); + let rr_cache = RRStoredData::new(resource_record); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("uchile.cl")); host_data.add_to_host_data(domain_name.clone(), rr_cache); @@ -352,7 +352,7 @@ mod host_data_test{ let mut host_data = HostData::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); - let rr_cache = RRCache::new(resource_record); + let rr_cache = RRStoredData::new(resource_record); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("uchile.cl")); host_data.add_to_host_data(domain_name.clone(), rr_cache); @@ -372,7 +372,7 @@ mod host_data_test{ let mut host_data = HostData::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); - let rr_cache = RRCache::new(resource_record); + let rr_cache = RRStoredData::new(resource_record); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("uchile.cl")); host_data.add_to_host_data(domain_name.clone(), rr_cache); @@ -394,7 +394,7 @@ mod host_data_test{ let mut host_data = HostData::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); - let mut rr_cache = RRCache::new(resource_record); + let mut rr_cache = RRStoredData::new(resource_record); rr_cache.set_response_time(1234433455); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("uchile.cl")); @@ -413,7 +413,7 @@ mod host_data_test{ let mut host_data = HostData::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); - let mut rr_cache = RRCache::new(resource_record); + let mut rr_cache = RRStoredData::new(resource_record); rr_cache.set_last_use(Utc::now()); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("expected")); @@ -422,7 +422,7 @@ mod host_data_test{ new_vec.push(String::from("uchile.cl")); let text_rdata = Rdata::TXT(TxtRdata::new(new_vec)); let resource_record_2 = ResourceRecord::new(text_rdata); - let mut rr_cache_2 = RRCache::new(resource_record_2); + let mut rr_cache_2 = RRStoredData::new(resource_record_2); rr_cache_2.set_last_use(Utc::now()); host_data.add_to_host_data(domain_name.clone(), rr_cache_2); @@ -439,7 +439,7 @@ mod host_data_test{ let mut host_data = HostData::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); - let mut rr_cache = RRCache::new(resource_record); + let mut rr_cache = RRStoredData::new(resource_record); rr_cache.set_response_time(12); let mut domain_name = DomainName::new(); @@ -449,7 +449,7 @@ mod host_data_test{ domain_name_new.set_name(String::from("inserted")); let a_rdata_2 = Rdata::A(ARdata::new()); let resource_record_2 = ResourceRecord::new(a_rdata_2); - let rr_cache_2 = RRCache::new(resource_record_2); + let rr_cache_2 = RRStoredData::new(resource_record_2); let mut rr_vec = Vec::new(); rr_vec.push(rr_cache_2); @@ -467,7 +467,7 @@ mod host_data_test{ a_rdata.set_address(ip_address); let rdata = Rdata::A(a_rdata); let resource_record = ResourceRecord::new(rdata); - let mut rr_cache = RRCache::new(resource_record); + let mut rr_cache = RRStoredData::new(resource_record); rr_cache.set_response_time(1000); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("uchile.cl")); @@ -491,11 +491,11 @@ mod host_data_test{ let mut resource_record_valid = ResourceRecord::new(a_rdata.clone()); resource_record_valid.set_ttl(1000); - let rr_cache_valid = RRCache::new(resource_record_valid); + let rr_cache_valid = RRStoredData::new(resource_record_valid); let mut resource_record_invalid = ResourceRecord::new(a_rdata); resource_record_invalid.set_ttl(4); - let rr_cache_invalid = RRCache::new(resource_record_invalid); + let rr_cache_invalid = RRStoredData::new(resource_record_invalid); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("uchile.cl")); diff --git a/src/rr_cache.rs b/src/rr_cache.rs index 882be294..f443d30c 100644 --- a/src/rr_cache.rs +++ b/src/rr_cache.rs @@ -3,30 +3,30 @@ use chrono::prelude::*; #[derive(Clone,PartialEq,Debug)] /// An structs that represents one element in the dns cache. -pub struct RRCache { +pub struct RRStoredData { /// Resource Records of the domain name resource_record: ResourceRecord, /// Mean of response time of the ip address response_time: u32, /// Last use of the rr last_use: DateTime, - /// Time of creation of the `RRCache` in the Resolver's cache. + /// Time of creation of the `RRStoredData` in the Resolver's cache. creation_time: DateTime, } -impl RRCache { - // Creates a new RRCache struct +impl RRStoredData { + // Creates a new RRStoredData struct // // # Examples // ''' - // let rr_cache = RRCache::new(); + // let rr_cache = RRStoredData::new(); // // assert_eq!(rr_cache.resource_records.len(), 0); // assert_eq!(rr_cache.response_time, 5); // ''' // pub fn new(resource_record: ResourceRecord) -> Self { - let rr_cache = RRCache { + let rr_cache = RRStoredData { resource_record: resource_record, response_time: 5000, last_use: Utc::now(), @@ -45,7 +45,7 @@ impl RRCache { } // Getters -impl RRCache { +impl RRStoredData { // Gets the resource record from the domain cache pub fn get_resource_record(&self) -> ResourceRecord { self.resource_record.clone() @@ -68,7 +68,7 @@ impl RRCache { } // Setters -impl RRCache { +impl RRStoredData { // Sets the resource record attribute with new value pub fn set_resource_record(&mut self, resource_record: ResourceRecord) { self.resource_record = resource_record; @@ -91,7 +91,7 @@ mod rr_cache_test { use crate::message::rdata::Rdata; use crate::message::type_rtype::Rtype; use crate::message::resource_record::ResourceRecord; - use crate::rr_cache::RRCache; + use crate::rr_cache::RRStoredData; use std::net::IpAddr; use chrono::prelude::*; @@ -106,7 +106,7 @@ mod rr_cache_test { let mut resource_record = ResourceRecord::new(rdata); resource_record.set_type_code(Rtype::A); - let rr_cache = RRCache::new(resource_record); + let rr_cache = RRStoredData::new(resource_record); assert_eq!(Rtype::from_rtype_to_int(rr_cache.resource_record.get_rtype()), 1); assert_eq!(rr_cache.response_time, 5000); @@ -123,7 +123,7 @@ mod rr_cache_test { let mut resource_record = ResourceRecord::new(rdata.clone()); resource_record.set_type_code(Rtype::A); - let mut rr_cache = RRCache::new(resource_record); + let mut rr_cache = RRStoredData::new(resource_record); assert_eq!(Rtype::from_rtype_to_int(rr_cache.resource_record.get_rtype()), 1); @@ -152,7 +152,7 @@ mod rr_cache_test { let mut resource_record = ResourceRecord::new(rdata); resource_record.set_type_code(Rtype::A); - let mut rr_cache = RRCache::new(resource_record); + let mut rr_cache = RRStoredData::new(resource_record); assert_eq!(rr_cache.get_response_time(), 5000); @@ -172,7 +172,7 @@ mod rr_cache_test { let mut resource_record = ResourceRecord::new(rdata); resource_record.set_type_code(Rtype::A); - let mut rr_cache = RRCache::new(resource_record); + let mut rr_cache = RRStoredData::new(resource_record); let now = Utc::now(); From 5eef096b4d1f42c628158d2b5fd65eecc89974d1 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Fri, 29 Dec 2023 17:24:22 -0300 Subject: [PATCH 070/137] rafactor: host_hash field to domain_names_data --- src/dns_cache/cache_data.rs | 4 +- src/dns_cache/cache_data/host_data.rs | 120 +++++++++++++------------- 2 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/dns_cache/cache_data.rs b/src/dns_cache/cache_data.rs index 525c410e..1ac10f10 100644 --- a/src/dns_cache/cache_data.rs +++ b/src/dns_cache/cache_data.rs @@ -165,7 +165,7 @@ impl CacheData{ .into_iter() .filter_map(|(rtype, mut host_data)| { host_data.filter_timeout_host_data(); - if host_data.get_host_hash().is_empty() { + if host_data.get_domain_names_data().is_empty() { None } else { Some((rtype, host_data)) @@ -331,7 +331,7 @@ mod cache_data_test{ let host_data = cache_hash.get(&Rtype::A).unwrap(); - assert!(host_data.get_host_hash().is_empty()); + assert!(host_data.get_domain_names_data().is_empty()); } //Get from cache data test diff --git a/src/dns_cache/cache_data/host_data.rs b/src/dns_cache/cache_data/host_data.rs index 945735b1..cee0a6d0 100644 --- a/src/dns_cache/cache_data/host_data.rs +++ b/src/dns_cache/cache_data/host_data.rs @@ -10,7 +10,7 @@ use std::{collections::HashMap, net::IpAddr}; /// of an specific `Rtype`. #[derive(Clone, Debug)] pub struct HostData { - host_hash: HashMap>, + domain_names_data: HashMap>, } ///functions for the host data @@ -23,7 +23,7 @@ impl HostData{ /// ``` pub fn new() -> HostData { HostData { - host_hash: HashMap::new(), + domain_names_data: HashMap::new(), } } @@ -42,18 +42,18 @@ impl HostData{ /// * `host_name` - A Domain Name that represents the name of the host /// * `rr_cache` - A RRStoredData that represents the rr_cache of the host pub fn add_to_host_data(&mut self, host_name: DomainName, rr_cache: RRStoredData) { - let mut host_hash = self.get_host_hash(); - if let Some(y) = host_hash.get_mut(&host_name){ + let mut domain_names_data = self.get_domain_names_data(); + if let Some(y) = domain_names_data.get_mut(&host_name){ let mut rr_cache_vec = y.clone(); rr_cache_vec.push(rr_cache); - host_hash.insert(host_name, rr_cache_vec); + domain_names_data.insert(host_name, rr_cache_vec); } else{ let mut rr_cache_vec = Vec::new(); rr_cache_vec.push(rr_cache); - host_hash.insert(host_name, rr_cache_vec); + domain_names_data.insert(host_name, rr_cache_vec); } - self.set_host_hash(host_hash); + self.set_domain_names_data(domain_names_data); } ///function to remove an element from the host data @@ -69,9 +69,9 @@ impl HostData{ /// host_data.remove_from_host_data(domain_name); /// ``` pub fn remove_from_host_data(&mut self, host_name: DomainName) -> u32{ - let mut host_hash = self.get_host_hash(); - if let Some(_x) = host_hash.remove(&host_name){ - self.set_host_hash(host_hash); + let mut domain_names_data = self.get_domain_names_data(); + if let Some(_x) = domain_names_data.remove(&host_name){ + self.set_domain_names_data(domain_names_data); return _x.len() as u32; } return 0; @@ -94,16 +94,16 @@ impl HostData{ /// let host_data_2_vec = host_data.get_from_host_data(domain_name); /// ``` pub fn get_from_host_data(&mut self, host_name: DomainName) -> Option>{ - let mut host_hash = self.get_host_hash(); - if let Some(x) = host_hash.get(&host_name){ + let mut domain_names_data = self.get_domain_names_data(); + if let Some(x) = domain_names_data.get(&host_name){ let new_x = x.clone(); let mut rr_cache_vec = Vec::::new(); for mut rr_cache in new_x{ rr_cache.set_last_use(Utc::now()); rr_cache_vec.push(rr_cache.clone()); } - host_hash.insert(host_name, rr_cache_vec.clone()); - self.set_host_hash(host_hash); + domain_names_data.insert(host_name, rr_cache_vec.clone()); + self.set_domain_names_data(domain_names_data); return Some(rr_cache_vec); } @@ -126,7 +126,7 @@ impl HostData{ /// let host_data_2 = get_oldest_used.get_from_host_data(domain_name); /// ``` pub fn get_oldest_used(&mut self)-> (DomainName,DateTime){ - let host = self.get_host_hash(); + let host = self.get_domain_names_data(); let mut used_in = Utc::now(); let mut oldest_used_domain_name = DomainName::new(); @@ -166,7 +166,7 @@ impl HostData{ /// host_data.insert(domain_name_new, rr_cache_2); /// ``` pub fn insert(&mut self,domain_name:DomainName, rr_cache_vec : Vec) -> Option>{ - return self.host_hash.insert(domain_name, rr_cache_vec) + return self.domain_names_data.insert(domain_name, rr_cache_vec) } ///function to update the response time @@ -186,8 +186,8 @@ impl HostData{ /// host_data.update_response_time(ip_address, 2000, domain_name); /// ``` pub fn update_response_time(&mut self, ip_address: IpAddr, response_time: u32, domain_name: DomainName){ - let mut host_hash = self.get_host_hash(); - if let Some(x) = host_hash.get(&domain_name){ + let mut domain_names_data = self.get_domain_names_data(); + if let Some(x) = domain_names_data.get(&domain_name){ let rr_cache_vec = x.clone(); let mut new_rr_cache_vec = Vec::::new(); @@ -204,16 +204,16 @@ impl HostData{ new_rr_cache_vec.push(rr_cache.clone()); } - host_hash.insert(domain_name, new_rr_cache_vec); + domain_names_data.insert(domain_name, new_rr_cache_vec); - self.set_host_hash(host_hash); + self.set_domain_names_data(domain_names_data); } } /// For each domain name, it removes the RRStoredData past its TTL. pub fn filter_timeout_host_data(&mut self) { let mut new_hash = HashMap::>::new(); - let data = self.get_host_hash(); + let data = self.get_domain_names_data(); let current_time = Utc::now(); for (domain_name, rr_cache_vec) in data.into_iter() { let filtered_rr_cache_vec: Vec = rr_cache_vec @@ -223,7 +223,7 @@ impl HostData{ new_hash.insert(domain_name, filtered_rr_cache_vec); } - self.set_host_hash(new_hash); + self.set_domain_names_data(new_hash); } } @@ -231,16 +231,16 @@ impl HostData{ ///setter and getter for the host data impl HostData{ - pub fn get_host_hash(&self) -> HashMap> { - return self.host_hash.clone(); + pub fn get_domain_names_data(&self) -> HashMap> { + return self.domain_names_data.clone(); } - pub fn set_host_hash(&mut self, host_hash: HashMap>) { - self.host_hash = host_hash; + pub fn set_domain_names_data(&mut self, domain_names_data: HashMap>) { + self.domain_names_data = domain_names_data; } pub fn get(&self,domain_name:&DomainName) -> Option<&Vec>{ - return self.host_hash.get(domain_name) + return self.domain_names_data.get(domain_name) } } @@ -262,35 +262,35 @@ mod host_data_test{ #[test] fn constructor_test(){ let host_data = HostData::new(); - assert!(host_data.host_hash.is_empty()); + assert!(host_data.domain_names_data.is_empty()); } //Getters and setters test #[test] - fn get_host_hash(){ + fn get_domain_names_data(){ let host_data = HostData::new(); - let host_hash = host_data.get_host_hash(); + let domain_names_data = host_data.get_domain_names_data(); - assert!(host_hash.is_empty()); + assert!(domain_names_data.is_empty()); } #[test] - fn set_host_hash(){ + fn set_domain_names_data(){ let mut host_data = HostData::new(); - let mut host_hash = HashMap::new(); + let mut domain_names_data = HashMap::new(); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("uchile.cl")); - host_hash.insert(domain_name.clone(), Vec::new()); + domain_names_data.insert(domain_name.clone(), Vec::new()); - assert!(host_data.host_hash.is_empty()); + assert!(host_data.domain_names_data.is_empty()); - host_data.set_host_hash(host_hash.clone()); + host_data.set_domain_names_data(domain_names_data.clone()); - let host_hash_2 = host_data.get_host_hash(); + let domain_names_data_2 = host_data.get_domain_names_data(); - assert!(!host_hash_2.is_empty()); + assert!(!domain_names_data_2.is_empty()); } //add_to_host_data test @@ -304,9 +304,9 @@ mod host_data_test{ domain_name.set_name(String::from("uchile.cl")); host_data.add_to_host_data(domain_name.clone(), rr_cache); - let host_hash = host_data.get_host_hash(); + let domain_names_data = host_data.get_domain_names_data(); - assert_eq!(host_hash.len(), 1); + assert_eq!(domain_names_data.len(), 1); let mut new_vec = Vec::new(); new_vec.push(String::from("hola")); @@ -315,13 +315,13 @@ mod host_data_test{ let rr_cache_2 = RRStoredData::new(resource_record_2); host_data.add_to_host_data(domain_name.clone(), rr_cache_2); - let host_hash_2 = host_data.get_host_hash(); + let domain_names_data_2 = host_data.get_domain_names_data(); - assert_eq!(host_hash_2.len(), 1); + assert_eq!(domain_names_data_2.len(), 1); - let host_hash_vec = host_hash_2.get(&domain_name).unwrap(); + let domain_names_data_vec = domain_names_data_2.get(&domain_name).unwrap(); - assert_eq!(host_hash_vec.len(), 2); + assert_eq!(domain_names_data_vec.len(), 2); } //remove_from_host_data test @@ -335,15 +335,15 @@ mod host_data_test{ domain_name.set_name(String::from("uchile.cl")); host_data.add_to_host_data(domain_name.clone(), rr_cache); - let host_hash = host_data.get_host_hash(); + let domain_names_data = host_data.get_domain_names_data(); - assert_eq!(host_hash.len(), 1); + assert_eq!(domain_names_data.len(), 1); host_data.remove_from_host_data(domain_name.clone()); - let host_hash_2 = host_data.get_host_hash(); + let domain_names_data_2 = host_data.get_domain_names_data(); - assert_eq!(host_hash_2.len(), 0); + assert_eq!(domain_names_data_2.len(), 0); } //get_from_host_data test @@ -357,13 +357,13 @@ mod host_data_test{ domain_name.set_name(String::from("uchile.cl")); host_data.add_to_host_data(domain_name.clone(), rr_cache); - let host_hash = host_data.get_host_hash(); + let domain_names_data = host_data.get_domain_names_data(); - assert_eq!(host_hash.len(), 1); + assert_eq!(domain_names_data.len(), 1); - let host_hash_vec = host_data.get_from_host_data(domain_name.clone()).unwrap(); + let domain_names_data_vec = host_data.get_from_host_data(domain_name.clone()).unwrap(); - assert_eq!(host_hash_vec.len(), 1); + assert_eq!(domain_names_data_vec.len(), 1); } //get_from_host_data test with no domain name @@ -377,10 +377,10 @@ mod host_data_test{ domain_name.set_name(String::from("uchile.cl")); host_data.add_to_host_data(domain_name.clone(), rr_cache); - let host_hash = host_data.get_host_hash(); + let domain_names_data = host_data.get_domain_names_data(); // One domain name - assert_eq!(host_hash.len(), 1); + assert_eq!(domain_names_data.len(), 1); // Assuming this test is for the case where the domain name is not in the host data let domain_name_2 = DomainName::new(); @@ -474,9 +474,9 @@ mod host_data_test{ host_data.add_to_host_data(domain_name.clone(), rr_cache); host_data.update_response_time(ip_address, 2000, domain_name.clone()); - let host_hash = host_data.get_host_hash(); + let domain_names_data = host_data.get_domain_names_data(); - let rr_cache_vec = host_hash.get(&domain_name).unwrap(); + let rr_cache_vec = domain_names_data.get(&domain_name).unwrap(); let rr_cache = rr_cache_vec.get(0).unwrap(); @@ -503,8 +503,8 @@ mod host_data_test{ host_data.add_to_host_data(domain_name.clone(), rr_cache_valid); host_data.add_to_host_data(domain_name.clone(), rr_cache_invalid); - assert_eq!(host_data.get_host_hash().len(), 1); - if let Some(rr_cache_vec) = host_data.get_host_hash().get(&domain_name) { + assert_eq!(host_data.get_domain_names_data().len(), 1); + if let Some(rr_cache_vec) = host_data.get_domain_names_data().get(&domain_name) { assert_eq!(rr_cache_vec.len(), 2); } @@ -513,8 +513,8 @@ mod host_data_test{ println!("After timeout: {:?}", Utc::now()); host_data.filter_timeout_host_data(); - assert_eq!(host_data.get_host_hash().len(), 1); - if let Some(rr_cache_vec) = host_data.get_host_hash().get(&domain_name) { + assert_eq!(host_data.get_domain_names_data().len(), 1); + if let Some(rr_cache_vec) = host_data.get_domain_names_data().get(&domain_name) { assert_eq!(rr_cache_vec.len(), 1); } } From 3a0b0eb1829edfa2e5ca63182fdf2389300fb57d Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Fri, 29 Dec 2023 17:27:28 -0300 Subject: [PATCH 071/137] refactor: HostData to DomainNameCache --- src/dns_cache.rs | 4 +-- src/dns_cache/cache_data.rs | 30 ++++++++-------- src/dns_cache/cache_data/host_data.rs | 50 +++++++++++++-------------- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/dns_cache.rs b/src/dns_cache.rs index 25bae1bd..7e8d0dcd 100644 --- a/src/dns_cache.rs +++ b/src/dns_cache.rs @@ -220,7 +220,7 @@ impl DnsCache { mod dns_cache_test { use crate::dns_cache::DnsCache; use crate::dns_cache::cache_data::CacheData; - use crate::dns_cache::cache_data::host_data::HostData; + use crate::dns_cache::cache_data::host_data::DomainNameCache; use crate::rr_cache::RRStoredData; use crate::domain_name::DomainName; use crate::message::rdata::a_rdata::ARdata; @@ -263,7 +263,7 @@ mod dns_cache_test { assert!(cache.get_cache().get_cache_data().is_empty()); let mut cache_data = CacheData::new(); let mut cache_data_hash = HashMap::new(); - let mut host_data = HostData::new(); + let mut host_data = DomainNameCache::new(); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("uchile.cl")); let a_rdata = Rdata::A(ARdata::new()); diff --git a/src/dns_cache/cache_data.rs b/src/dns_cache/cache_data.rs index 1ac10f10..5584e91f 100644 --- a/src/dns_cache/cache_data.rs +++ b/src/dns_cache/cache_data.rs @@ -5,7 +5,7 @@ use chrono::Utc; use crate::message::type_rtype::Rtype; use crate::rr_cache::RRStoredData; use std::net::IpAddr; -use crate::dns_cache::cache_data::host_data::HostData; +use crate::dns_cache::cache_data::host_data::DomainNameCache; use std::collections::HashMap; use crate::domain_name::DomainName; @@ -13,7 +13,7 @@ use crate::domain_name::DomainName; ///struct to define the cache data #[derive(Clone, Debug)] pub struct CacheData { - cache_data: HashMap, + cache_data: HashMap, } /// functions for the cache data @@ -48,12 +48,12 @@ impl CacheData{ pub fn add_to_cache_data(&mut self, rtype: Rtype, domain_name: DomainName, rr_cache:RRStoredData){ let mut cache_data = self.get_cache_data(); if let Some(x) = cache_data.get_mut(&rtype) { - let mut type_hash: HostData = x.clone(); + let mut type_hash: DomainNameCache = x.clone(); type_hash.add_to_host_data(domain_name, rr_cache); cache_data.insert(rtype, type_hash); } else { - let mut type_hash: HostData = HostData::new(); + let mut type_hash: DomainNameCache = DomainNameCache::new(); type_hash.add_to_host_data(domain_name, rr_cache); cache_data.insert(rtype, type_hash); } @@ -78,7 +78,7 @@ impl CacheData{ pub fn remove_from_cache_data(&mut self, domain_name: DomainName, rtype: Rtype) -> u32{ let mut cache_data = self.get_cache_data(); if let Some(x) = cache_data.get_mut(&rtype) { - let mut type_hash: HostData = x.clone(); + let mut type_hash: DomainNameCache = x.clone(); let length = type_hash.remove_from_host_data(domain_name); cache_data.insert(rtype, type_hash); self.set_cache_data(cache_data); @@ -143,7 +143,7 @@ impl CacheData{ pub fn get_from_cache_data(&mut self, domain_name: DomainName, rtype: Rtype) -> Option>{ let mut cache_data = self.get_cache_data(); if let Some(x) = cache_data.get(&rtype) { - let mut type_hash: HostData = x.clone(); + let mut type_hash: DomainNameCache = x.clone(); let rr_cache_vec = type_hash.get_from_host_data(domain_name); cache_data.insert(rtype, type_hash); self.set_cache_data(cache_data); @@ -157,11 +157,11 @@ impl CacheData{ /// Removes the cache data that has expired. /// /// For each type of cache data, it removes the cache data that has expired, using - /// the `timeout_rr_cache` method of the `HostData` struct. If the `HostData` struct + /// the `timeout_rr_cache` method of the `DomainNameCache` struct. If the `DomainNameCache` struct /// is empty after the removal, it is removed from the cache data. pub fn filter_timeout_cache_data(&mut self) { let cache_data = self.get_cache_data(); - let clean_cache_data: HashMap = cache_data + let clean_cache_data: HashMap = cache_data .into_iter() .filter_map(|(rtype, mut host_data)| { host_data.filter_timeout_host_data(); @@ -192,12 +192,12 @@ impl CacheData{ } - pub fn insert(&mut self,rtype:Rtype, host_data: HostData) { + pub fn insert(&mut self,rtype:Rtype, host_data: DomainNameCache) { self.cache_data.insert(rtype, host_data); } - pub fn iter(&mut self) -> std::collections::hash_map::Iter<'_, Rtype, HostData>{ + pub fn iter(&mut self) -> std::collections::hash_map::Iter<'_, Rtype, DomainNameCache>{ return self.cache_data.iter() } @@ -206,15 +206,15 @@ impl CacheData{ ///setter and getter for the host data impl CacheData{ - pub fn get_cache_data(&self) -> HashMap { + pub fn get_cache_data(&self) -> HashMap { return self.cache_data.clone(); } - pub fn set_cache_data(&mut self, cache_data: HashMap) { + pub fn set_cache_data(&mut self, cache_data: HashMap) { self.cache_data = cache_data; } - pub fn get(&self, rtype : Rtype) -> Option<&HostData>{ + pub fn get(&self, rtype : Rtype) -> Option<&DomainNameCache>{ return self.cache_data.get(&rtype); } } @@ -232,7 +232,7 @@ mod cache_data_test{ use crate::message::rdata::Rdata; use crate::message::rdata::a_rdata::ARdata; use crate::message::resource_record::ResourceRecord; - use crate::dns_cache::cache_data::host_data::HostData; + use crate::dns_cache::cache_data::host_data::DomainNameCache; use std::{collections::HashMap, net::IpAddr}; @@ -262,7 +262,7 @@ mod cache_data_test{ let mut cache_data = CacheData::new(); let mut cache_data_hash = HashMap::new(); - let mut host_data = HostData::new(); + let mut host_data = DomainNameCache::new(); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("uchile.cl")); let a_rdata = Rdata::A(ARdata::new()); diff --git a/src/dns_cache/cache_data/host_data.rs b/src/dns_cache/cache_data/host_data.rs index cee0a6d0..117c16c1 100644 --- a/src/dns_cache/cache_data/host_data.rs +++ b/src/dns_cache/cache_data/host_data.rs @@ -9,20 +9,20 @@ use std::{collections::HashMap, net::IpAddr}; /// This means, all the cache data associated with a single host /// of an specific `Rtype`. #[derive(Clone, Debug)] -pub struct HostData { +pub struct DomainNameCache { domain_names_data: HashMap>, } ///functions for the host data -impl HostData{ +impl DomainNameCache{ ///function to create a new host data /// # Example /// ``` - /// let host_data = HostData::new(); + /// let host_data = DomainNameCache::new(); /// ``` - pub fn new() -> HostData { - HostData { + pub fn new() -> DomainNameCache { + DomainNameCache { domain_names_data: HashMap::new(), } } @@ -30,7 +30,7 @@ impl HostData{ ///function to add a rr_cache to the host data /// # Example /// ``` - /// let mut host_data = HostData::new(); + /// let mut host_data = DomainNameCache::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); /// let rr_cache = RRStoredData::new(resource_record); @@ -59,7 +59,7 @@ impl HostData{ ///function to remove an element from the host data /// # Example /// ``` - /// let mut host_data = HostData::new(); + /// let mut host_data = DomainNameCache::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); /// let rr_cache = RRStoredData::new(resource_record); @@ -84,7 +84,7 @@ impl HostData{ /// /// # Example /// ``` - /// let mut host_data = HostData::new(); + /// let mut host_data = DomainNameCache::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); /// let rr_cache = RRStoredData::new(resource_record); @@ -115,7 +115,7 @@ impl HostData{ ///function to get the oldest used /// # Example /// ``` - /// let mut host_data = HostData::new(); + /// let mut host_data = DomainNameCache::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); /// let rr_cache = RRStoredData::new(resource_record); @@ -150,7 +150,7 @@ impl HostData{ /// the domain name didn't exist before /// # Example /// ``` - /// let mut host_data = HostData::new(); + /// let mut host_data = DomainNameCache::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); /// let mut rr_cache = RRStoredData::new(resource_record); @@ -172,7 +172,7 @@ impl HostData{ ///function to update the response time /// # Example /// ``` - /// let mut host_data = HostData::new(); + /// let mut host_data = DomainNameCache::new(); /// let ip_address = IpAddr::from([127, 0, 0, 1]); /// let a_rdata = ARdata::new(); /// a_rdata.set_address(ip_address); @@ -229,7 +229,7 @@ impl HostData{ } ///setter and getter for the host data -impl HostData{ +impl DomainNameCache{ pub fn get_domain_names_data(&self) -> HashMap> { return self.domain_names_data.clone(); @@ -256,19 +256,19 @@ mod host_data_test{ use crate::message::resource_record::ResourceRecord; use std::{collections::HashMap, net::IpAddr}; - use super::HostData; + use super::DomainNameCache; //Contructor test #[test] fn constructor_test(){ - let host_data = HostData::new(); + let host_data = DomainNameCache::new(); assert!(host_data.domain_names_data.is_empty()); } //Getters and setters test #[test] fn get_domain_names_data(){ - let host_data = HostData::new(); + let host_data = DomainNameCache::new(); let domain_names_data = host_data.get_domain_names_data(); @@ -277,7 +277,7 @@ mod host_data_test{ #[test] fn set_domain_names_data(){ - let mut host_data = HostData::new(); + let mut host_data = DomainNameCache::new(); let mut domain_names_data = HashMap::new(); let mut domain_name = DomainName::new(); @@ -296,7 +296,7 @@ mod host_data_test{ //add_to_host_data test #[test] fn add_to_host_data(){ - let mut host_data = HostData::new(); + let mut host_data = DomainNameCache::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); let rr_cache = RRStoredData::new(resource_record); @@ -327,7 +327,7 @@ mod host_data_test{ //remove_from_host_data test #[test] fn remove_from_host_data(){ - let mut host_data = HostData::new(); + let mut host_data = DomainNameCache::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); let rr_cache = RRStoredData::new(resource_record); @@ -349,7 +349,7 @@ mod host_data_test{ //get_from_host_data test #[test] fn get_from_host_data(){ - let mut host_data = HostData::new(); + let mut host_data = DomainNameCache::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); let rr_cache = RRStoredData::new(resource_record); @@ -369,7 +369,7 @@ mod host_data_test{ //get_from_host_data test with no domain name #[test] fn get_from_host_data_no_domain_name(){ - let mut host_data = HostData::new(); + let mut host_data = DomainNameCache::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); let rr_cache = RRStoredData::new(resource_record); @@ -391,7 +391,7 @@ mod host_data_test{ //get test #[test] fn get(){ - let mut host_data = HostData::new(); + let mut host_data = DomainNameCache::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); let mut rr_cache = RRStoredData::new(resource_record); @@ -410,7 +410,7 @@ mod host_data_test{ //get oldest used test #[test] fn get_oldest_used(){ - let mut host_data = HostData::new(); + let mut host_data = DomainNameCache::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); let mut rr_cache = RRStoredData::new(resource_record); @@ -436,7 +436,7 @@ mod host_data_test{ //get insert used test #[test] fn insert(){ - let mut host_data = HostData::new(); + let mut host_data = DomainNameCache::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); let mut rr_cache = RRStoredData::new(resource_record); @@ -461,7 +461,7 @@ mod host_data_test{ //update response time test #[test] fn update_response_time(){ - let mut host_data = HostData::new(); + let mut host_data = DomainNameCache::new(); let ip_address = IpAddr::from([127, 0, 0, 1]); let mut a_rdata = ARdata::new(); a_rdata.set_address(ip_address); @@ -486,7 +486,7 @@ mod host_data_test{ #[test] fn timeout_rr_cache() { use std::{thread, time}; - let mut host_data = HostData::new(); + let mut host_data = DomainNameCache::new(); let a_rdata = Rdata::A(ARdata::new()); let mut resource_record_valid = ResourceRecord::new(a_rdata.clone()); From c04617f8d5e5ac91b57058d6572de61d3afdc5f1 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Fri, 29 Dec 2023 17:30:36 -0300 Subject: [PATCH 072/137] docs: description to domain_names_data --- src/dns_cache/cache_data/host_data.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/dns_cache/cache_data/host_data.rs b/src/dns_cache/cache_data/host_data.rs index 117c16c1..e2d6f140 100644 --- a/src/dns_cache/cache_data/host_data.rs +++ b/src/dns_cache/cache_data/host_data.rs @@ -10,6 +10,10 @@ use std::{collections::HashMap, net::IpAddr}; /// of an specific `Rtype`. #[derive(Clone, Debug)] pub struct DomainNameCache { + /// Contains the Resource Records associated to each host domain name. + /// + /// The key is the `DomainName` of the host, and the value is a `Vec`, + /// which contains all the Resource Records data associated to the host. domain_names_data: HashMap>, } From 8b80fc4ddb95741bb24fe6cde06e2fe9d8f5b009 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Fri, 29 Dec 2023 17:32:06 -0300 Subject: [PATCH 073/137] docs: add description to domain_names_data --- src/dns_cache/cache_data/host_data.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/dns_cache/cache_data/host_data.rs b/src/dns_cache/cache_data/host_data.rs index e2d6f140..cc9da6bd 100644 --- a/src/dns_cache/cache_data/host_data.rs +++ b/src/dns_cache/cache_data/host_data.rs @@ -12,8 +12,9 @@ use std::{collections::HashMap, net::IpAddr}; pub struct DomainNameCache { /// Contains the Resource Records associated to each host domain name. /// - /// The key is the `DomainName` of the host, and the value is a `Vec`, - /// which contains all the Resource Records data associated to the host. + /// The key is the `DomainName` of the host, and the value is a + /// `Vec`, which contains all the Resource Records + /// data associated to the host for a single `Rtype`. domain_names_data: HashMap>, } From 8e083a2567c6440667bfe15c5d2e7690aa1cc04d Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Fri, 29 Dec 2023 17:54:05 -0300 Subject: [PATCH 074/137] refactor: DomainNameCache to CacheByDomainName --- src/dns_cache.rs | 4 +-- src/dns_cache/cache_data.rs | 30 ++++++++-------- src/dns_cache/cache_data/host_data.rs | 50 +++++++++++++-------------- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/dns_cache.rs b/src/dns_cache.rs index 7e8d0dcd..777b1219 100644 --- a/src/dns_cache.rs +++ b/src/dns_cache.rs @@ -220,7 +220,7 @@ impl DnsCache { mod dns_cache_test { use crate::dns_cache::DnsCache; use crate::dns_cache::cache_data::CacheData; - use crate::dns_cache::cache_data::host_data::DomainNameCache; + use crate::dns_cache::cache_data::host_data::CacheByDomainName; use crate::rr_cache::RRStoredData; use crate::domain_name::DomainName; use crate::message::rdata::a_rdata::ARdata; @@ -263,7 +263,7 @@ mod dns_cache_test { assert!(cache.get_cache().get_cache_data().is_empty()); let mut cache_data = CacheData::new(); let mut cache_data_hash = HashMap::new(); - let mut host_data = DomainNameCache::new(); + let mut host_data = CacheByDomainName::new(); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("uchile.cl")); let a_rdata = Rdata::A(ARdata::new()); diff --git a/src/dns_cache/cache_data.rs b/src/dns_cache/cache_data.rs index 5584e91f..149dce9b 100644 --- a/src/dns_cache/cache_data.rs +++ b/src/dns_cache/cache_data.rs @@ -5,7 +5,7 @@ use chrono::Utc; use crate::message::type_rtype::Rtype; use crate::rr_cache::RRStoredData; use std::net::IpAddr; -use crate::dns_cache::cache_data::host_data::DomainNameCache; +use crate::dns_cache::cache_data::host_data::CacheByDomainName; use std::collections::HashMap; use crate::domain_name::DomainName; @@ -13,7 +13,7 @@ use crate::domain_name::DomainName; ///struct to define the cache data #[derive(Clone, Debug)] pub struct CacheData { - cache_data: HashMap, + cache_data: HashMap, } /// functions for the cache data @@ -48,12 +48,12 @@ impl CacheData{ pub fn add_to_cache_data(&mut self, rtype: Rtype, domain_name: DomainName, rr_cache:RRStoredData){ let mut cache_data = self.get_cache_data(); if let Some(x) = cache_data.get_mut(&rtype) { - let mut type_hash: DomainNameCache = x.clone(); + let mut type_hash: CacheByDomainName = x.clone(); type_hash.add_to_host_data(domain_name, rr_cache); cache_data.insert(rtype, type_hash); } else { - let mut type_hash: DomainNameCache = DomainNameCache::new(); + let mut type_hash: CacheByDomainName = CacheByDomainName::new(); type_hash.add_to_host_data(domain_name, rr_cache); cache_data.insert(rtype, type_hash); } @@ -78,7 +78,7 @@ impl CacheData{ pub fn remove_from_cache_data(&mut self, domain_name: DomainName, rtype: Rtype) -> u32{ let mut cache_data = self.get_cache_data(); if let Some(x) = cache_data.get_mut(&rtype) { - let mut type_hash: DomainNameCache = x.clone(); + let mut type_hash: CacheByDomainName = x.clone(); let length = type_hash.remove_from_host_data(domain_name); cache_data.insert(rtype, type_hash); self.set_cache_data(cache_data); @@ -143,7 +143,7 @@ impl CacheData{ pub fn get_from_cache_data(&mut self, domain_name: DomainName, rtype: Rtype) -> Option>{ let mut cache_data = self.get_cache_data(); if let Some(x) = cache_data.get(&rtype) { - let mut type_hash: DomainNameCache = x.clone(); + let mut type_hash: CacheByDomainName = x.clone(); let rr_cache_vec = type_hash.get_from_host_data(domain_name); cache_data.insert(rtype, type_hash); self.set_cache_data(cache_data); @@ -157,11 +157,11 @@ impl CacheData{ /// Removes the cache data that has expired. /// /// For each type of cache data, it removes the cache data that has expired, using - /// the `timeout_rr_cache` method of the `DomainNameCache` struct. If the `DomainNameCache` struct + /// the `timeout_rr_cache` method of the `CacheByDomainName` struct. If the `CacheByDomainName` struct /// is empty after the removal, it is removed from the cache data. pub fn filter_timeout_cache_data(&mut self) { let cache_data = self.get_cache_data(); - let clean_cache_data: HashMap = cache_data + let clean_cache_data: HashMap = cache_data .into_iter() .filter_map(|(rtype, mut host_data)| { host_data.filter_timeout_host_data(); @@ -192,12 +192,12 @@ impl CacheData{ } - pub fn insert(&mut self,rtype:Rtype, host_data: DomainNameCache) { + pub fn insert(&mut self,rtype:Rtype, host_data: CacheByDomainName) { self.cache_data.insert(rtype, host_data); } - pub fn iter(&mut self) -> std::collections::hash_map::Iter<'_, Rtype, DomainNameCache>{ + pub fn iter(&mut self) -> std::collections::hash_map::Iter<'_, Rtype, CacheByDomainName>{ return self.cache_data.iter() } @@ -206,15 +206,15 @@ impl CacheData{ ///setter and getter for the host data impl CacheData{ - pub fn get_cache_data(&self) -> HashMap { + pub fn get_cache_data(&self) -> HashMap { return self.cache_data.clone(); } - pub fn set_cache_data(&mut self, cache_data: HashMap) { + pub fn set_cache_data(&mut self, cache_data: HashMap) { self.cache_data = cache_data; } - pub fn get(&self, rtype : Rtype) -> Option<&DomainNameCache>{ + pub fn get(&self, rtype : Rtype) -> Option<&CacheByDomainName>{ return self.cache_data.get(&rtype); } } @@ -232,7 +232,7 @@ mod cache_data_test{ use crate::message::rdata::Rdata; use crate::message::rdata::a_rdata::ARdata; use crate::message::resource_record::ResourceRecord; - use crate::dns_cache::cache_data::host_data::DomainNameCache; + use crate::dns_cache::cache_data::host_data::CacheByDomainName; use std::{collections::HashMap, net::IpAddr}; @@ -262,7 +262,7 @@ mod cache_data_test{ let mut cache_data = CacheData::new(); let mut cache_data_hash = HashMap::new(); - let mut host_data = DomainNameCache::new(); + let mut host_data = CacheByDomainName::new(); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("uchile.cl")); let a_rdata = Rdata::A(ARdata::new()); diff --git a/src/dns_cache/cache_data/host_data.rs b/src/dns_cache/cache_data/host_data.rs index cc9da6bd..9b44cc54 100644 --- a/src/dns_cache/cache_data/host_data.rs +++ b/src/dns_cache/cache_data/host_data.rs @@ -9,7 +9,7 @@ use std::{collections::HashMap, net::IpAddr}; /// This means, all the cache data associated with a single host /// of an specific `Rtype`. #[derive(Clone, Debug)] -pub struct DomainNameCache { +pub struct CacheByDomainName { /// Contains the Resource Records associated to each host domain name. /// /// The key is the `DomainName` of the host, and the value is a @@ -19,15 +19,15 @@ pub struct DomainNameCache { } ///functions for the host data -impl DomainNameCache{ +impl CacheByDomainName { ///function to create a new host data /// # Example /// ``` - /// let host_data = DomainNameCache::new(); + /// let host_data = CacheByDomainName::new(); /// ``` - pub fn new() -> DomainNameCache { - DomainNameCache { + pub fn new() -> CacheByDomainName { + CacheByDomainName { domain_names_data: HashMap::new(), } } @@ -35,7 +35,7 @@ impl DomainNameCache{ ///function to add a rr_cache to the host data /// # Example /// ``` - /// let mut host_data = DomainNameCache::new(); + /// let mut host_data = CacheByDomainName::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); /// let rr_cache = RRStoredData::new(resource_record); @@ -64,7 +64,7 @@ impl DomainNameCache{ ///function to remove an element from the host data /// # Example /// ``` - /// let mut host_data = DomainNameCache::new(); + /// let mut host_data = CacheByDomainName::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); /// let rr_cache = RRStoredData::new(resource_record); @@ -89,7 +89,7 @@ impl DomainNameCache{ /// /// # Example /// ``` - /// let mut host_data = DomainNameCache::new(); + /// let mut host_data = CacheByDomainName::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); /// let rr_cache = RRStoredData::new(resource_record); @@ -120,7 +120,7 @@ impl DomainNameCache{ ///function to get the oldest used /// # Example /// ``` - /// let mut host_data = DomainNameCache::new(); + /// let mut host_data = CacheByDomainName::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); /// let rr_cache = RRStoredData::new(resource_record); @@ -155,7 +155,7 @@ impl DomainNameCache{ /// the domain name didn't exist before /// # Example /// ``` - /// let mut host_data = DomainNameCache::new(); + /// let mut host_data = CacheByDomainName::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); /// let mut rr_cache = RRStoredData::new(resource_record); @@ -177,7 +177,7 @@ impl DomainNameCache{ ///function to update the response time /// # Example /// ``` - /// let mut host_data = DomainNameCache::new(); + /// let mut host_data = CacheByDomainName::new(); /// let ip_address = IpAddr::from([127, 0, 0, 1]); /// let a_rdata = ARdata::new(); /// a_rdata.set_address(ip_address); @@ -234,7 +234,7 @@ impl DomainNameCache{ } ///setter and getter for the host data -impl DomainNameCache{ +impl CacheByDomainName{ pub fn get_domain_names_data(&self) -> HashMap> { return self.domain_names_data.clone(); @@ -261,19 +261,19 @@ mod host_data_test{ use crate::message::resource_record::ResourceRecord; use std::{collections::HashMap, net::IpAddr}; - use super::DomainNameCache; + use super::CacheByDomainName; //Contructor test #[test] fn constructor_test(){ - let host_data = DomainNameCache::new(); + let host_data = CacheByDomainName::new(); assert!(host_data.domain_names_data.is_empty()); } //Getters and setters test #[test] fn get_domain_names_data(){ - let host_data = DomainNameCache::new(); + let host_data = CacheByDomainName::new(); let domain_names_data = host_data.get_domain_names_data(); @@ -282,7 +282,7 @@ mod host_data_test{ #[test] fn set_domain_names_data(){ - let mut host_data = DomainNameCache::new(); + let mut host_data = CacheByDomainName::new(); let mut domain_names_data = HashMap::new(); let mut domain_name = DomainName::new(); @@ -301,7 +301,7 @@ mod host_data_test{ //add_to_host_data test #[test] fn add_to_host_data(){ - let mut host_data = DomainNameCache::new(); + let mut host_data = CacheByDomainName::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); let rr_cache = RRStoredData::new(resource_record); @@ -332,7 +332,7 @@ mod host_data_test{ //remove_from_host_data test #[test] fn remove_from_host_data(){ - let mut host_data = DomainNameCache::new(); + let mut host_data = CacheByDomainName::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); let rr_cache = RRStoredData::new(resource_record); @@ -354,7 +354,7 @@ mod host_data_test{ //get_from_host_data test #[test] fn get_from_host_data(){ - let mut host_data = DomainNameCache::new(); + let mut host_data = CacheByDomainName::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); let rr_cache = RRStoredData::new(resource_record); @@ -374,7 +374,7 @@ mod host_data_test{ //get_from_host_data test with no domain name #[test] fn get_from_host_data_no_domain_name(){ - let mut host_data = DomainNameCache::new(); + let mut host_data = CacheByDomainName::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); let rr_cache = RRStoredData::new(resource_record); @@ -396,7 +396,7 @@ mod host_data_test{ //get test #[test] fn get(){ - let mut host_data = DomainNameCache::new(); + let mut host_data = CacheByDomainName::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); let mut rr_cache = RRStoredData::new(resource_record); @@ -415,7 +415,7 @@ mod host_data_test{ //get oldest used test #[test] fn get_oldest_used(){ - let mut host_data = DomainNameCache::new(); + let mut host_data = CacheByDomainName::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); let mut rr_cache = RRStoredData::new(resource_record); @@ -441,7 +441,7 @@ mod host_data_test{ //get insert used test #[test] fn insert(){ - let mut host_data = DomainNameCache::new(); + let mut host_data = CacheByDomainName::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); let mut rr_cache = RRStoredData::new(resource_record); @@ -466,7 +466,7 @@ mod host_data_test{ //update response time test #[test] fn update_response_time(){ - let mut host_data = DomainNameCache::new(); + let mut host_data = CacheByDomainName::new(); let ip_address = IpAddr::from([127, 0, 0, 1]); let mut a_rdata = ARdata::new(); a_rdata.set_address(ip_address); @@ -491,7 +491,7 @@ mod host_data_test{ #[test] fn timeout_rr_cache() { use std::{thread, time}; - let mut host_data = DomainNameCache::new(); + let mut host_data = CacheByDomainName::new(); let a_rdata = Rdata::A(ARdata::new()); let mut resource_record_valid = ResourceRecord::new(a_rdata.clone()); From 1770dc9d04a69d768d46491ed691de7ad8aafcd8 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Fri, 29 Dec 2023 18:34:33 -0300 Subject: [PATCH 075/137] refactor: cache_data to record_types_data --- src/dns_cache/cache_data.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/dns_cache/cache_data.rs b/src/dns_cache/cache_data.rs index 149dce9b..f5a243bf 100644 --- a/src/dns_cache/cache_data.rs +++ b/src/dns_cache/cache_data.rs @@ -13,7 +13,7 @@ use crate::domain_name::DomainName; ///struct to define the cache data #[derive(Clone, Debug)] pub struct CacheData { - cache_data: HashMap, + record_types_data: HashMap, } /// functions for the cache data @@ -25,7 +25,7 @@ impl CacheData{ /// ``` pub fn new() -> CacheData { CacheData { - cache_data: HashMap::new(), + record_types_data: HashMap::new(), } } @@ -193,12 +193,12 @@ impl CacheData{ } pub fn insert(&mut self,rtype:Rtype, host_data: CacheByDomainName) { - self.cache_data.insert(rtype, host_data); + self.record_types_data.insert(rtype, host_data); } pub fn iter(&mut self) -> std::collections::hash_map::Iter<'_, Rtype, CacheByDomainName>{ - return self.cache_data.iter() + return self.record_types_data.iter() } } @@ -207,15 +207,15 @@ impl CacheData{ impl CacheData{ pub fn get_cache_data(&self) -> HashMap { - return self.cache_data.clone(); + return self.record_types_data.clone(); } pub fn set_cache_data(&mut self, cache_data: HashMap) { - self.cache_data = cache_data; + self.record_types_data = cache_data; } pub fn get(&self, rtype : Rtype) -> Option<&CacheByDomainName>{ - return self.cache_data.get(&rtype); + return self.record_types_data.get(&rtype); } } @@ -244,7 +244,7 @@ mod cache_data_test{ fn constructor_test(){ let cache_data = CacheData::new(); - assert!(cache_data.cache_data.is_empty()); + assert!(cache_data.record_types_data.is_empty()); } //Getter and setter test From 5a2d3e98229a476a73a6cc064b3481e90a5a18bf Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Fri, 29 Dec 2023 19:13:16 -0300 Subject: [PATCH 076/137] refactor: CacheData to CacheByRecordType --- src/dns_cache.rs | 14 ++++++------- src/dns_cache/cache_data.rs | 40 ++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/dns_cache.rs b/src/dns_cache.rs index 777b1219..7f3ed752 100644 --- a/src/dns_cache.rs +++ b/src/dns_cache.rs @@ -1,6 +1,6 @@ pub mod cache_data; -use crate::dns_cache::cache_data::CacheData; +use crate::dns_cache::cache_data::CacheByRecordType; use crate::message::rdata::Rdata; use crate::message::resource_record::ResourceRecord; use crate::rr_cache::RRStoredData; @@ -13,7 +13,7 @@ use std::cmp; /// Struct that represents a cache for dns pub struct DnsCache { // first hash by type, then by hostname - cache: CacheData, + cache: CacheByRecordType, max_size: u32, size: u32, } @@ -30,7 +30,7 @@ impl DnsCache { /// pub fn new() -> Self { let cache = DnsCache { - cache: CacheData::new(), + cache: CacheByRecordType::new(), max_size: 0, size: 0, }; @@ -183,7 +183,7 @@ impl DnsCache { // Getters impl DnsCache { // Gets the cache from the struct - pub fn get_cache(&self) -> CacheData{ + pub fn get_cache(&self) -> CacheByRecordType{ self.cache.clone() } @@ -201,7 +201,7 @@ impl DnsCache { // Setters impl DnsCache { // Sets the cache - pub fn set_cache(&mut self, cache: CacheData) { + pub fn set_cache(&mut self, cache: CacheByRecordType) { self.cache = cache } @@ -219,7 +219,7 @@ impl DnsCache { #[cfg(test)] mod dns_cache_test { use crate::dns_cache::DnsCache; - use crate::dns_cache::cache_data::CacheData; + use crate::dns_cache::cache_data::CacheByRecordType; use crate::dns_cache::cache_data::host_data::CacheByDomainName; use crate::rr_cache::RRStoredData; use crate::domain_name::DomainName; @@ -261,7 +261,7 @@ mod dns_cache_test { fn set_and_get_cache(){ let mut cache = DnsCache::new(); assert!(cache.get_cache().get_cache_data().is_empty()); - let mut cache_data = CacheData::new(); + let mut cache_data = CacheByRecordType::new(); let mut cache_data_hash = HashMap::new(); let mut host_data = CacheByDomainName::new(); let mut domain_name = DomainName::new(); diff --git a/src/dns_cache/cache_data.rs b/src/dns_cache/cache_data.rs index f5a243bf..5479461e 100644 --- a/src/dns_cache/cache_data.rs +++ b/src/dns_cache/cache_data.rs @@ -12,19 +12,19 @@ use crate::domain_name::DomainName; ///struct to define the cache data #[derive(Clone, Debug)] -pub struct CacheData { +pub struct CacheByRecordType { record_types_data: HashMap, } /// functions for the cache data -impl CacheData{ - /// function to create a new CacheData +impl CacheByRecordType{ + /// function to create a new CacheByRecordType /// Example /// ``` - /// let cache_data = CacheData::new(); + /// let cache_data = CacheByRecordType::new(); /// ``` - pub fn new() -> CacheData { - CacheData { + pub fn new() -> CacheByRecordType { + CacheByRecordType { record_types_data: HashMap::new(), } } @@ -32,7 +32,7 @@ impl CacheData{ ///function to add a new element into the cache_data /// # Example /// ``` - /// let mut cache_data = CacheData::new(); + /// let mut cache_data = CacheByRecordType::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); /// let rr_cache = RRStoredData::new(resource_record); @@ -63,7 +63,7 @@ impl CacheData{ ///function to remove an element from the cache data /// # Example /// ``` - /// let mut cache_data = CacheData::new(); + /// let mut cache_data = CacheByRecordType::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); /// let rr_cache = RRStoredData::new(resource_record); @@ -90,7 +90,7 @@ impl CacheData{ ///function to remove the oldest element from the cache data /// # Example /// ``` - /// let mut cache_data = CacheData::new(); + /// let mut cache_data = CacheByRecordType::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); /// let rr_cache = RRStoredData::new(resource_record); @@ -126,7 +126,7 @@ impl CacheData{ ///function to get an element from the cache data /// # Example /// ``` - /// let mut cache_data = CacheData::new(); + /// let mut cache_data = CacheByRecordType::new(); /// let a_rdata = Rdata::A(ARdata::new()); /// let resource_record = ResourceRecord::new(a_rdata); /// let rr_cache = RRStoredData::new(resource_record); @@ -204,7 +204,7 @@ impl CacheData{ } ///setter and getter for the host data -impl CacheData{ +impl CacheByRecordType{ pub fn get_cache_data(&self) -> HashMap { return self.record_types_data.clone(); @@ -237,12 +237,12 @@ mod cache_data_test{ - use super::CacheData; + use super::CacheByRecordType; //Constructor test #[test] fn constructor_test(){ - let cache_data = CacheData::new(); + let cache_data = CacheByRecordType::new(); assert!(cache_data.record_types_data.is_empty()); } @@ -250,7 +250,7 @@ mod cache_data_test{ //Getter and setter test #[test] fn get_cache_data(){ - let cache_data = CacheData::new(); + let cache_data = CacheByRecordType::new(); let cache_data_hash = cache_data.get_cache_data(); @@ -259,7 +259,7 @@ mod cache_data_test{ #[test] fn set_cache_data(){ - let mut cache_data = CacheData::new(); + let mut cache_data = CacheByRecordType::new(); let mut cache_data_hash = HashMap::new(); let mut host_data = CacheByDomainName::new(); @@ -279,7 +279,7 @@ mod cache_data_test{ //Add to cache data test #[test] fn add_to_cache_data(){ - let mut cache_data = CacheData::new(); + let mut cache_data = CacheByRecordType::new(); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("uchile.cl")); @@ -313,7 +313,7 @@ mod cache_data_test{ //Remove from cache data test #[test] fn remove_from_cache_data(){ - let mut cache_data = CacheData::new(); + let mut cache_data = CacheByRecordType::new(); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("uchile.cl")); @@ -337,7 +337,7 @@ mod cache_data_test{ //Get from cache data test #[test] fn get_from_cache_data(){ - let mut cache_data = CacheData::new(); + let mut cache_data = CacheByRecordType::new(); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("uchile.cl")); @@ -373,7 +373,7 @@ mod cache_data_test{ //remove oldest used test #[test] fn remove_oldest_used(){ - let mut cache_data = CacheData::new(); + let mut cache_data = CacheByRecordType::new(); let a_rdata = Rdata::A(ARdata::new()); let resource_record = ResourceRecord::new(a_rdata); @@ -413,7 +413,7 @@ mod cache_data_test{ //update response time test #[test] fn update_response_time(){ - let mut cache_data = CacheData::new(); + let mut cache_data = CacheByRecordType::new(); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("uchile.cl")); From b11bcd6942bf823d3446fa8a2dc1a3cfc539c821 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Fri, 29 Dec 2023 19:16:47 -0300 Subject: [PATCH 077/137] docs: CacheByRecord TYpe --- src/dns_cache/cache_data.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/dns_cache/cache_data.rs b/src/dns_cache/cache_data.rs index 5479461e..7df55ea8 100644 --- a/src/dns_cache/cache_data.rs +++ b/src/dns_cache/cache_data.rs @@ -10,9 +10,13 @@ use std::collections::HashMap; use crate::domain_name::DomainName; -///struct to define the cache data +/// Struct that represents the cache data of the DNS cache by record type. #[derive(Clone, Debug)] pub struct CacheByRecordType { + /// HashMap that represents the cache data of the DNS cache by record type. + /// + /// The key is the record type and the value is the cache data of the DNS + /// cache by domain name. record_types_data: HashMap, } From bf190d01290604d5a3b48698d20b2f69951c4190 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Tue, 2 Jan 2024 12:52:43 -0300 Subject: [PATCH 078/137] refact: host_data to cache_by_domain_name --- src/dns_cache.rs | 2 +- src/dns_cache/cache_data.rs | 6 +++--- .../cache_data/{host_data.rs => cache_by_domain_name.rs} | 0 3 files changed, 4 insertions(+), 4 deletions(-) rename src/dns_cache/cache_data/{host_data.rs => cache_by_domain_name.rs} (100%) diff --git a/src/dns_cache.rs b/src/dns_cache.rs index 7f3ed752..dd98b76b 100644 --- a/src/dns_cache.rs +++ b/src/dns_cache.rs @@ -220,7 +220,7 @@ impl DnsCache { mod dns_cache_test { use crate::dns_cache::DnsCache; use crate::dns_cache::cache_data::CacheByRecordType; - use crate::dns_cache::cache_data::host_data::CacheByDomainName; + use crate::dns_cache::cache_data::cache_by_domain_name::CacheByDomainName; use crate::rr_cache::RRStoredData; use crate::domain_name::DomainName; use crate::message::rdata::a_rdata::ARdata; diff --git a/src/dns_cache/cache_data.rs b/src/dns_cache/cache_data.rs index 7df55ea8..36f1689b 100644 --- a/src/dns_cache/cache_data.rs +++ b/src/dns_cache/cache_data.rs @@ -1,11 +1,11 @@ -pub mod host_data; +pub mod cache_by_domain_name; use chrono::Utc; //use crate::message::rdata::Rdata; use crate::message::type_rtype::Rtype; use crate::rr_cache::RRStoredData; use std::net::IpAddr; -use crate::dns_cache::cache_data::host_data::CacheByDomainName; +use crate::dns_cache::cache_data::cache_by_domain_name::CacheByDomainName; use std::collections::HashMap; use crate::domain_name::DomainName; @@ -236,7 +236,7 @@ mod cache_data_test{ use crate::message::rdata::Rdata; use crate::message::rdata::a_rdata::ARdata; use crate::message::resource_record::ResourceRecord; - use crate::dns_cache::cache_data::host_data::CacheByDomainName; + use crate::dns_cache::cache_data::cache_by_domain_name::CacheByDomainName; use std::{collections::HashMap, net::IpAddr}; diff --git a/src/dns_cache/cache_data/host_data.rs b/src/dns_cache/cache_data/cache_by_domain_name.rs similarity index 100% rename from src/dns_cache/cache_data/host_data.rs rename to src/dns_cache/cache_data/cache_by_domain_name.rs From 5be42860ad9903657b0d6b53bf52be3f53ff2f32 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Tue, 2 Jan 2024 12:55:55 -0300 Subject: [PATCH 079/137] refact: cache_data to cache_by_record_type --- src/dns_cache.rs | 8 ++++---- src/dns_cache/{cache_data.rs => cache_by_record_type.rs} | 4 ++-- .../cache_by_domain_name.rs | 0 3 files changed, 6 insertions(+), 6 deletions(-) rename src/dns_cache/{cache_data.rs => cache_by_record_type.rs} (98%) rename src/dns_cache/{cache_data => cache_by_record_type}/cache_by_domain_name.rs (100%) diff --git a/src/dns_cache.rs b/src/dns_cache.rs index dd98b76b..cea95b4a 100644 --- a/src/dns_cache.rs +++ b/src/dns_cache.rs @@ -1,6 +1,6 @@ -pub mod cache_data; +pub mod cache_by_record_type; -use crate::dns_cache::cache_data::CacheByRecordType; +use crate::dns_cache::cache_by_record_type::CacheByRecordType; use crate::message::rdata::Rdata; use crate::message::resource_record::ResourceRecord; use crate::rr_cache::RRStoredData; @@ -219,8 +219,8 @@ impl DnsCache { #[cfg(test)] mod dns_cache_test { use crate::dns_cache::DnsCache; - use crate::dns_cache::cache_data::CacheByRecordType; - use crate::dns_cache::cache_data::cache_by_domain_name::CacheByDomainName; + use crate::dns_cache::cache_by_record_type::CacheByRecordType; + use crate::dns_cache::cache_by_record_type::cache_by_domain_name::CacheByDomainName; use crate::rr_cache::RRStoredData; use crate::domain_name::DomainName; use crate::message::rdata::a_rdata::ARdata; diff --git a/src/dns_cache/cache_data.rs b/src/dns_cache/cache_by_record_type.rs similarity index 98% rename from src/dns_cache/cache_data.rs rename to src/dns_cache/cache_by_record_type.rs index 36f1689b..c3998536 100644 --- a/src/dns_cache/cache_data.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -5,7 +5,7 @@ use chrono::Utc; use crate::message::type_rtype::Rtype; use crate::rr_cache::RRStoredData; use std::net::IpAddr; -use crate::dns_cache::cache_data::cache_by_domain_name::CacheByDomainName; +use crate::dns_cache::cache_by_record_type::cache_by_domain_name::CacheByDomainName; use std::collections::HashMap; use crate::domain_name::DomainName; @@ -236,7 +236,7 @@ mod cache_data_test{ use crate::message::rdata::Rdata; use crate::message::rdata::a_rdata::ARdata; use crate::message::resource_record::ResourceRecord; - use crate::dns_cache::cache_data::cache_by_domain_name::CacheByDomainName; + use crate::dns_cache::cache_by_record_type::cache_by_domain_name::CacheByDomainName; use std::{collections::HashMap, net::IpAddr}; diff --git a/src/dns_cache/cache_data/cache_by_domain_name.rs b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs similarity index 100% rename from src/dns_cache/cache_data/cache_by_domain_name.rs rename to src/dns_cache/cache_by_record_type/cache_by_domain_name.rs From 11a454bf972531e56bd2c2bc8197c64a0d2078e5 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Tue, 2 Jan 2024 14:26:45 -0300 Subject: [PATCH 080/137] refact: rr_cache to rr_stored_data --- src/dns_cache.rs | 4 ++-- src/dns_cache/cache_by_record_type.rs | 4 ++-- src/dns_cache/cache_by_record_type/cache_by_domain_name.rs | 4 ++-- src/lib.rs | 2 +- src/{rr_cache.rs => rr_stored_data.rs} | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) rename src/{rr_cache.rs => rr_stored_data.rs} (99%) diff --git a/src/dns_cache.rs b/src/dns_cache.rs index cea95b4a..f594ca4c 100644 --- a/src/dns_cache.rs +++ b/src/dns_cache.rs @@ -3,7 +3,7 @@ pub mod cache_by_record_type; use crate::dns_cache::cache_by_record_type::CacheByRecordType; use crate::message::rdata::Rdata; use crate::message::resource_record::ResourceRecord; -use crate::rr_cache::RRStoredData; +use crate::rr_stored_data::RRStoredData; use crate::message::type_rtype::Rtype; use std::net::IpAddr; use crate::domain_name::DomainName; @@ -221,7 +221,7 @@ mod dns_cache_test { use crate::dns_cache::DnsCache; use crate::dns_cache::cache_by_record_type::CacheByRecordType; use crate::dns_cache::cache_by_record_type::cache_by_domain_name::CacheByDomainName; - use crate::rr_cache::RRStoredData; + use crate::rr_stored_data::RRStoredData; use crate::domain_name::DomainName; use crate::message::rdata::a_rdata::ARdata; use crate::message::rdata::txt_rdata::TxtRdata; diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index c3998536..b31c9421 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -3,7 +3,7 @@ pub mod cache_by_domain_name; use chrono::Utc; //use crate::message::rdata::Rdata; use crate::message::type_rtype::Rtype; -use crate::rr_cache::RRStoredData; +use crate::rr_stored_data::RRStoredData; use std::net::IpAddr; use crate::dns_cache::cache_by_record_type::cache_by_domain_name::CacheByDomainName; use std::collections::HashMap; @@ -231,7 +231,7 @@ mod cache_data_test{ use crate::message::rdata::txt_rdata::TxtRdata; use crate::message::type_rtype::Rtype; - use crate::rr_cache::RRStoredData; + use crate::rr_stored_data::RRStoredData; use crate::domain_name::DomainName; use crate::message::rdata::Rdata; use crate::message::rdata::a_rdata::ARdata; diff --git a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs index 9b44cc54..4d8cc149 100644 --- a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs +++ b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs @@ -1,5 +1,5 @@ use chrono::{Utc, DateTime}; -use crate::{rr_cache::RRStoredData, domain_name::DomainName, message::rdata::Rdata}; +use crate::{rr_stored_data::RRStoredData, domain_name::DomainName, message::rdata::Rdata}; use std::{collections::HashMap, net::IpAddr}; /// This struct saves the data associated with a host in the cache. @@ -254,7 +254,7 @@ impl CacheByDomainName{ mod host_data_test{ use chrono::Utc; use crate::message::rdata::txt_rdata::TxtRdata; - use crate::rr_cache::RRStoredData; + use crate::rr_stored_data::RRStoredData; use crate::domain_name::DomainName; use crate::message::rdata::Rdata; use crate::message::rdata::a_rdata::ARdata; diff --git a/src/lib.rs b/src/lib.rs index 14751d02..09ed32ae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ pub mod dns_cache; pub mod domain_name; pub mod message; pub mod async_resolver; -pub mod rr_cache; +pub mod rr_stored_data; pub mod utils; pub mod truncated_dns_message; diff --git a/src/rr_cache.rs b/src/rr_stored_data.rs similarity index 99% rename from src/rr_cache.rs rename to src/rr_stored_data.rs index f443d30c..435ca2ce 100644 --- a/src/rr_cache.rs +++ b/src/rr_stored_data.rs @@ -91,7 +91,7 @@ mod rr_cache_test { use crate::message::rdata::Rdata; use crate::message::type_rtype::Rtype; use crate::message::resource_record::ResourceRecord; - use crate::rr_cache::RRStoredData; + use crate::rr_stored_data::RRStoredData; use std::net::IpAddr; use chrono::prelude::*; From cf6473c80efc97daa6042e78b7bb5114da3cd2ed Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Tue, 2 Jan 2024 14:41:00 -0300 Subject: [PATCH 081/137] refact: rr_stored_data inside cache_by_record_type crate --- src/dns_cache.rs | 4 ++-- src/dns_cache/cache_by_record_type.rs | 6 +++--- src/dns_cache/cache_by_record_type/cache_by_domain_name.rs | 5 +++-- src/{ => dns_cache/cache_by_record_type}/rr_stored_data.rs | 2 +- src/lib.rs | 1 - 5 files changed, 9 insertions(+), 9 deletions(-) rename src/{ => dns_cache/cache_by_record_type}/rr_stored_data.rs (98%) diff --git a/src/dns_cache.rs b/src/dns_cache.rs index f594ca4c..fd0a2f13 100644 --- a/src/dns_cache.rs +++ b/src/dns_cache.rs @@ -1,9 +1,9 @@ pub mod cache_by_record_type; use crate::dns_cache::cache_by_record_type::CacheByRecordType; +use crate::dns_cache::cache_by_record_type::rr_stored_data::RRStoredData; use crate::message::rdata::Rdata; use crate::message::resource_record::ResourceRecord; -use crate::rr_stored_data::RRStoredData; use crate::message::type_rtype::Rtype; use std::net::IpAddr; use crate::domain_name::DomainName; @@ -221,7 +221,7 @@ mod dns_cache_test { use crate::dns_cache::DnsCache; use crate::dns_cache::cache_by_record_type::CacheByRecordType; use crate::dns_cache::cache_by_record_type::cache_by_domain_name::CacheByDomainName; - use crate::rr_stored_data::RRStoredData; + use crate::dns_cache::cache_by_record_type::rr_stored_data::RRStoredData; use crate::domain_name::DomainName; use crate::message::rdata::a_rdata::ARdata; use crate::message::rdata::txt_rdata::TxtRdata; diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index b31c9421..7f756b7f 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -1,13 +1,13 @@ pub mod cache_by_domain_name; +pub mod rr_stored_data; use chrono::Utc; -//use crate::message::rdata::Rdata; use crate::message::type_rtype::Rtype; -use crate::rr_stored_data::RRStoredData; use std::net::IpAddr; use crate::dns_cache::cache_by_record_type::cache_by_domain_name::CacheByDomainName; use std::collections::HashMap; use crate::domain_name::DomainName; +use self::rr_stored_data::RRStoredData; /// Struct that represents the cache data of the DNS cache by record type. @@ -231,7 +231,7 @@ mod cache_data_test{ use crate::message::rdata::txt_rdata::TxtRdata; use crate::message::type_rtype::Rtype; - use crate::rr_stored_data::RRStoredData; + use crate::dns_cache::cache_by_record_type::rr_stored_data::RRStoredData; use crate::domain_name::DomainName; use crate::message::rdata::Rdata; use crate::message::rdata::a_rdata::ARdata; diff --git a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs index 4d8cc149..02204c98 100644 --- a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs +++ b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs @@ -1,5 +1,6 @@ use chrono::{Utc, DateTime}; -use crate::{rr_stored_data::RRStoredData, domain_name::DomainName, message::rdata::Rdata}; +use crate::{domain_name::DomainName, message::rdata::Rdata}; +use crate::dns_cache::cache_by_record_type::rr_stored_data::RRStoredData; use std::{collections::HashMap, net::IpAddr}; /// This struct saves the data associated with a host in the cache. @@ -254,7 +255,7 @@ impl CacheByDomainName{ mod host_data_test{ use chrono::Utc; use crate::message::rdata::txt_rdata::TxtRdata; - use crate::rr_stored_data::RRStoredData; + use crate::dns_cache::cache_by_record_type::rr_stored_data::RRStoredData; use crate::domain_name::DomainName; use crate::message::rdata::Rdata; use crate::message::rdata::a_rdata::ARdata; diff --git a/src/rr_stored_data.rs b/src/dns_cache/cache_by_record_type/rr_stored_data.rs similarity index 98% rename from src/rr_stored_data.rs rename to src/dns_cache/cache_by_record_type/rr_stored_data.rs index 435ca2ce..94bda048 100644 --- a/src/rr_stored_data.rs +++ b/src/dns_cache/cache_by_record_type/rr_stored_data.rs @@ -91,7 +91,7 @@ mod rr_cache_test { use crate::message::rdata::Rdata; use crate::message::type_rtype::Rtype; use crate::message::resource_record::ResourceRecord; - use crate::rr_stored_data::RRStoredData; + use crate::dns_cache::cache_by_record_type::rr_stored_data::RRStoredData; use std::net::IpAddr; use chrono::prelude::*; diff --git a/src/lib.rs b/src/lib.rs index 09ed32ae..9fbfcdcc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,6 @@ pub mod dns_cache; pub mod domain_name; pub mod message; pub mod async_resolver; -pub mod rr_stored_data; pub mod utils; pub mod truncated_dns_message; From bc3365b805c40b0fcafc220106b7a9ee6da8d10a Mon Sep 17 00:00:00 2001 From: Francisca Ortega Date: Wed, 3 Jan 2024 10:18:33 -0500 Subject: [PATCH 082/137] added: client connection get ip function --- src/client.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/client.rs b/src/client.rs index 6e84c7c8..50acd0fa 100644 --- a/src/client.rs +++ b/src/client.rs @@ -104,9 +104,11 @@ impl Client { let client_query = self.get_dns_query(); let conn: &T = &self.get_conn(); + let ip_addr = conn.get_ip(); let dns_response: DnsMessage = match conn.send(client_query) { Ok(response_message) => { + //let response_ip = get_sender_ip(&response_message)?; match DnsMessage::from_bytes(&response_message) { Ok(dns_message) => dns_message, Err(_) => return Err(ClientError::FormatError("The name server was unable to interpret the query."))?, From d84079cc49e24bd802786771edc78018c380000b Mon Sep 17 00:00:00 2001 From: Francisca Ortega Date: Wed, 3 Jan 2024 10:20:49 -0500 Subject: [PATCH 083/137] added client connection get ip --- src/client/client_connection.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/client/client_connection.rs b/src/client/client_connection.rs index 008c7cdb..d7658a32 100644 --- a/src/client/client_connection.rs +++ b/src/client/client_connection.rs @@ -11,6 +11,8 @@ pub trait ClientConnection: Copy {//: 'static + Sized + Send + Sync + Unpin //Sends query fn send(self,dns_query:DnsMessage) -> Result, ClientError>; + + fn get_ip(&self) -> IpAddr; } #[derive(Clone, Copy, Debug, PartialEq, Eq)] From 7116f82cc69231ecb757722b0d66e8a74e3754ab Mon Sep 17 00:00:00 2001 From: Francisca Ortega Date: Thu, 4 Jan 2024 07:20:05 -0500 Subject: [PATCH 084/137] added: get_ip function in tcp connection --- src/client/tcp_connection.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/client/tcp_connection.rs b/src/client/tcp_connection.rs index 3983d25e..fa049c91 100644 --- a/src/client/tcp_connection.rs +++ b/src/client/tcp_connection.rs @@ -27,7 +27,12 @@ impl ClientConnection for ClientTCPConnection { timeout: timeout, } } - + ///implement get_ip + /// returns IpAddr + fn get_ip(&self) -> IpAddr { + return self.server_addr.clone(); + } + /// creates socket tcp, sends query and receive response fn send(self, dns_query: DnsMessage) -> Result, ClientError>{ From a2a455aea71a54ad1810c13ba8d69a15657fec52 Mon Sep 17 00:00:00 2001 From: Francisca Ortega Date: Thu, 4 Jan 2024 07:21:21 -0500 Subject: [PATCH 085/137] added: function get_ip in udp_connection --- src/client/udp_connection.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/client/udp_connection.rs b/src/client/udp_connection.rs index ca7a2a5f..bcabdda2 100644 --- a/src/client/udp_connection.rs +++ b/src/client/udp_connection.rs @@ -26,6 +26,11 @@ impl ClientConnection for ClientUDPConnection { timeout: timeout, } } + /// implement get_ip + /// returns IpAddr + fn get_ip(&self) -> IpAddr { + return self.server_addr.clone(); + } fn send(self, dns_query:DnsMessage) -> Result, ClientError> { From 27e8c70ecc057cd3cc63d40a7b15d3fa5fd1b952 Mon Sep 17 00:00:00 2001 From: Francisca Ortega Date: Thu, 4 Jan 2024 08:08:53 -0500 Subject: [PATCH 086/137] fixed: warning in client --- src/client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index 50acd0fa..72b26615 100644 --- a/src/client.rs +++ b/src/client.rs @@ -104,7 +104,7 @@ impl Client { let client_query = self.get_dns_query(); let conn: &T = &self.get_conn(); - let ip_addr = conn.get_ip(); + let _ip_addr = conn.get_ip(); let dns_response: DnsMessage = match conn.send(client_query) { Ok(response_message) => { From d7d6529d86391dc5df2f9a7a40c8fc5047f990d7 Mon Sep 17 00:00:00 2001 From: Francisca Ortega Date: Thu, 4 Jan 2024 09:42:36 -0500 Subject: [PATCH 087/137] added: function get io in client connection --- src/client/client_connection.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/client/client_connection.rs b/src/client/client_connection.rs index 008c7cdb..ef07e933 100644 --- a/src/client/client_connection.rs +++ b/src/client/client_connection.rs @@ -11,6 +11,9 @@ pub trait ClientConnection: Copy {//: 'static + Sized + Send + Sync + Unpin //Sends query fn send(self,dns_query:DnsMessage) -> Result, ClientError>; + + //Gets ips + fn get_ip(&self) -> IpAddr; } #[derive(Clone, Copy, Debug, PartialEq, Eq)] From be02a0baee53d9c0a9984922120d5ae0588a9dfd Mon Sep 17 00:00:00 2001 From: Francisca Ortega Date: Thu, 4 Jan 2024 09:43:30 -0500 Subject: [PATCH 088/137] added: get_ip function in udp connection --- src/client/udp_connection.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/client/udp_connection.rs b/src/client/udp_connection.rs index ca7a2a5f..8a9a7d24 100644 --- a/src/client/udp_connection.rs +++ b/src/client/udp_connection.rs @@ -27,6 +27,12 @@ impl ClientConnection for ClientUDPConnection { } } + /// implement get_ip + /// returns IpAddr + fn get_ip(&self) -> IpAddr { + return self.server_addr.clone(); + } + fn send(self, dns_query:DnsMessage) -> Result, ClientError> { let timeout:Duration = self.timeout; From b5af86d2e978838613b18278b170efc485c43444 Mon Sep 17 00:00:00 2001 From: Francisca Ortega Date: Thu, 4 Jan 2024 09:44:23 -0500 Subject: [PATCH 089/137] added: get ip function in tcp connection --- src/client/tcp_connection.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/client/tcp_connection.rs b/src/client/tcp_connection.rs index 3983d25e..d67d1fbf 100644 --- a/src/client/tcp_connection.rs +++ b/src/client/tcp_connection.rs @@ -28,6 +28,12 @@ impl ClientConnection for ClientTCPConnection { } } + ///implement get_ip + /// returns IpAddr + fn get_ip(&self) -> IpAddr { + return self.server_addr.clone(); + } + /// creates socket tcp, sends query and receive response fn send(self, dns_query: DnsMessage) -> Result, ClientError>{ From 3b3be0699054f97e5039dc22bb12b14f5f65d7bc Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Thu, 4 Jan 2024 14:39:15 -0300 Subject: [PATCH 090/137] add test get ip V4 in TCP connection --- src/client/tcp_connection.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/client/tcp_connection.rs b/src/client/tcp_connection.rs index d67d1fbf..252b486f 100644 --- a/src/client/tcp_connection.rs +++ b/src/client/tcp_connection.rs @@ -105,6 +105,7 @@ impl ClientTCPConnection { mod tcp_connection_test{ use super::*; + use core::time; use std::net::{IpAddr,Ipv4Addr}; use crate::domain_name::DomainName; use crate::message::type_qtype::Qtype; @@ -125,6 +126,15 @@ mod tcp_connection_test{ assert_eq!(_conn_new.get_timeout(), Duration::from_secs(100)); } + #[test] + fn get_ip_v4(){ + let ip_address = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)); + let timeout = Duration::from_secs(100); + let connection = ClientTCPConnection::new(ip_address, timeout); + //check if the ip is the same + assert_eq!(connection.get_ip(), IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1))); + } + //Setters and Getters test #[test] fn get_server_addr(){ From 82010dca0ed84b553a93c03d2be2cf14ff2604a7 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Thu, 4 Jan 2024 14:41:45 -0300 Subject: [PATCH 091/137] add test get ip V6 in TCP connection --- src/client/tcp_connection.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/client/tcp_connection.rs b/src/client/tcp_connection.rs index 252b486f..f9f8adff 100644 --- a/src/client/tcp_connection.rs +++ b/src/client/tcp_connection.rs @@ -106,7 +106,7 @@ mod tcp_connection_test{ use super::*; use core::time; - use std::net::{IpAddr,Ipv4Addr}; + use std::net::{IpAddr,Ipv4Addr,Ipv6Addr}; use crate::domain_name::DomainName; use crate::message::type_qtype::Qtype; use crate::message::class_qclass::Qclass; @@ -135,6 +135,15 @@ mod tcp_connection_test{ assert_eq!(connection.get_ip(), IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1))); } + #[test] + fn get_ip_v6(){ + let ip_address = IpAddr::V6(Ipv6Addr::new(0xc0, 0xa8, 0, 1, 0, 0, 0, 0)); + let timeout = Duration::from_secs(100); + let connection = ClientTCPConnection::new(ip_address, timeout); + //check if the ip is the same + assert_eq!(connection.get_ip(), IpAddr::V6(Ipv6Addr::new(0xc0, 0xa8, 0, 1, 0, 0, 0, 0))); + } + //Setters and Getters test #[test] fn get_server_addr(){ From 7897b98dbc41aa2657ac2819fdcd05b11d54f0d1 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Thu, 4 Jan 2024 14:43:14 -0300 Subject: [PATCH 092/137] add test get ip V4 in UDP connection --- src/client/udp_connection.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/client/udp_connection.rs b/src/client/udp_connection.rs index 8a9a7d24..882e0ddc 100644 --- a/src/client/udp_connection.rs +++ b/src/client/udp_connection.rs @@ -124,6 +124,15 @@ mod udp_connection_test{ assert_eq!(_conn_new.get_server_addr(), IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1))); } + #[test] + fn get_ip_v4(){ + let ip_address = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)); + let timeout = Duration::from_secs(100); + let connection = ClientUDPConnection::new(ip_address, timeout); + //check if the ip is the same + assert_eq!(connection.get_ip(), IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1))); + } + #[test] fn set_server_addr(){ let ip_addr = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)); From c42195ee5f21f1185ffbf4a8d1ce9dfde6762036 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Thu, 4 Jan 2024 14:45:03 -0300 Subject: [PATCH 093/137] add test get ip V6 in UDP connection --- src/client/udp_connection.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/client/udp_connection.rs b/src/client/udp_connection.rs index 882e0ddc..ab79720e 100644 --- a/src/client/udp_connection.rs +++ b/src/client/udp_connection.rs @@ -101,7 +101,7 @@ mod udp_connection_test{ use crate::message::type_qtype::Qtype; use crate::message::class_qclass::Qclass; use super::*; - use std::net::{IpAddr,Ipv4Addr}; + use std::net::{IpAddr,Ipv4Addr,Ipv6Addr}; #[test] fn create_udp() { @@ -133,6 +133,15 @@ mod udp_connection_test{ assert_eq!(connection.get_ip(), IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1))); } + #[test] + fn get_ip_v6(){ + let ip_address = IpAddr::V6(Ipv6Addr::new(0xc0, 0xa8, 0, 1, 0, 0, 0, 0)); + let timeout = Duration::from_secs(100); + let connection = ClientUDPConnection::new(ip_address, timeout); + //check if the ip is the same + assert_eq!(connection.get_ip(), IpAddr::V6(Ipv6Addr::new(0xc0, 0xa8, 0, 1, 0, 0, 0, 0))); + } + #[test] fn set_server_addr(){ let ip_addr = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)); From ed95a4729300945499268fddf46c26689f9d3679 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Thu, 4 Jan 2024 14:55:26 -0300 Subject: [PATCH 094/137] add commentary in get ip v6 In UDP and TCP --- src/client/tcp_connection.rs | 1 + src/client/udp_connection.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/client/tcp_connection.rs b/src/client/tcp_connection.rs index f9f8adff..3b15b96c 100644 --- a/src/client/tcp_connection.rs +++ b/src/client/tcp_connection.rs @@ -137,6 +137,7 @@ mod tcp_connection_test{ #[test] fn get_ip_v6(){ + // ip in V6 version is the equivalent to (192, 168, 0, 1) in V4 let ip_address = IpAddr::V6(Ipv6Addr::new(0xc0, 0xa8, 0, 1, 0, 0, 0, 0)); let timeout = Duration::from_secs(100); let connection = ClientTCPConnection::new(ip_address, timeout); diff --git a/src/client/udp_connection.rs b/src/client/udp_connection.rs index ab79720e..3b9d4d15 100644 --- a/src/client/udp_connection.rs +++ b/src/client/udp_connection.rs @@ -135,6 +135,7 @@ mod udp_connection_test{ #[test] fn get_ip_v6(){ + // ip in V6 version is the equivalent to (192, 168, 0, 1) in V4 let ip_address = IpAddr::V6(Ipv6Addr::new(0xc0, 0xa8, 0, 1, 0, 0, 0, 0)); let timeout = Duration::from_secs(100); let connection = ClientUDPConnection::new(ip_address, timeout); From 25a045694c069e31ae376ccd28c6eac7c51abe3a Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Thu, 4 Jan 2024 16:05:15 -0300 Subject: [PATCH 095/137] delete: duplicated code from merge --- src/client/udp_connection.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/client/udp_connection.rs b/src/client/udp_connection.rs index 306bdbfd..a7ca354d 100644 --- a/src/client/udp_connection.rs +++ b/src/client/udp_connection.rs @@ -32,12 +32,6 @@ impl ClientConnection for ClientUDPConnection { return self.server_addr.clone(); } - /// implement get_ip - /// returns IpAddr - fn get_ip(&self) -> IpAddr { - return self.server_addr.clone(); - } - fn send(self, dns_query:DnsMessage) -> Result, ClientError> { let timeout:Duration = self.timeout; From 83bff64229d5956242952899becdb7357c7c3b36 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Thu, 4 Jan 2024 17:10:41 -0300 Subject: [PATCH 096/137] add extra verification timeout_rr_cache test --- src/dns_cache/cache_by_record_type/cache_by_domain_name.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs index 02204c98..0dad4015 100644 --- a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs +++ b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs @@ -497,7 +497,7 @@ mod host_data_test{ let mut resource_record_valid = ResourceRecord::new(a_rdata.clone()); resource_record_valid.set_ttl(1000); - let rr_cache_valid = RRStoredData::new(resource_record_valid); + let rr_cache_valid = RRStoredData::new(resource_record_valid.clone()); let mut resource_record_invalid = ResourceRecord::new(a_rdata); resource_record_invalid.set_ttl(4); @@ -522,6 +522,11 @@ mod host_data_test{ assert_eq!(host_data.get_domain_names_data().len(), 1); if let Some(rr_cache_vec) = host_data.get_domain_names_data().get(&domain_name) { assert_eq!(rr_cache_vec.len(), 1); + //check if the rescource record who survives is the correct + if let Some(rrstore_data_valid) = rr_cache_vec.get(0){ + let resource_record_after_filter = rrstore_data_valid.get_resource_record(); + assert_eq!(resource_record_after_filter, resource_record_valid); + } } } } From 438a38fb016e66c1b46b639e621dc2ec57455af8 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Thu, 4 Jan 2024 18:13:06 -0300 Subject: [PATCH 097/137] add test filter_timeout with rtype a in cache_by_recor_type --- src/dns_cache/cache_by_record_type.rs | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index 7f756b7f..01a03b6c 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -439,4 +439,46 @@ mod cache_data_test{ assert_eq!(rr_cache.get_response_time(), 2500); } } + + #[test] + fn filter_timeout_cache_data_rtype_a() { + use std::{thread, time}; + let mut cache_record_type = CacheByRecordType::new(); + let a_rdata = Rdata::A(ARdata::new()); + + let mut resource_record_valid = ResourceRecord::new(a_rdata.clone()); + resource_record_valid.set_ttl(1000); + let rr_cache_valid = RRStoredData::new(resource_record_valid.clone()); + + let mut resource_record_invalid = ResourceRecord::new(a_rdata); + resource_record_invalid.set_ttl(4); + let rr_cache_invalid = RRStoredData::new(resource_record_invalid); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("uchile.cl")); + + cache_record_type.add_to_cache_data(Rtype::A, domain_name.clone(), rr_cache_valid); + cache_record_type.add_to_cache_data(Rtype::A, domain_name.clone(), rr_cache_invalid); + + //check if the domain with A type has 2 RRStoredData + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::A){ + assert_eq!(rr_cache_vec.len(), 2); + } + + println!("Before timeout: {:?}", Utc::now()); + thread::sleep(time::Duration::from_secs(5)); + println!("After timeout: {:?}", Utc::now()); + cache_record_type.filter_timeout_cache_data(); + + //check if the len is 1 instead of 2 (one RRStoredData was eliminated) + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::A){ + assert_eq!(rr_cache_vec.len(), 1); + //chek if the resource record who survives is the right one + if let Some(rrstore_data_valid) = rr_cache_vec.get(0){ + let resource_record_after_filter = rrstore_data_valid.get_resource_record(); + assert_eq!(resource_record_after_filter, resource_record_valid); + } + } + + } } \ No newline at end of file From 28e3be666f175d1baa4ddc8cccd3e4e7d5db7eec Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Thu, 4 Jan 2024 19:16:01 -0300 Subject: [PATCH 098/137] add test filter_timeout with differents rtypes in cache_by_recor_type, but fails, does not clean the ttl with less time --- src/dns_cache/cache_by_record_type.rs | 60 ++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index 01a03b6c..c5056aaa 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -229,7 +229,7 @@ mod cache_data_test{ //use std::thread::sleep; //use std::time::Duration as StdDuration; - use crate::message::rdata::txt_rdata::TxtRdata; + use crate::message::rdata::{txt_rdata::TxtRdata, ns_rdata::NsRdata}; use crate::message::type_rtype::Rtype; use crate::dns_cache::cache_by_record_type::rr_stored_data::RRStoredData; use crate::domain_name::DomainName; @@ -480,5 +480,63 @@ mod cache_data_test{ } } + } + + #[test] + fn filter_timout_cache_data_2_differents_rtypes(){ + use std::{thread, time}; + let mut cache_record_type = CacheByRecordType::new(); + let a_rdata = Rdata::A(ARdata::new()); + let ns_rdata = Rdata::NS(NsRdata::new()); + + let mut resource_record_valid = ResourceRecord::new(a_rdata.clone()); + resource_record_valid.set_ttl(1000); + let rr_cache_valid = RRStoredData::new(resource_record_valid.clone()); + + let mut resource_record_invalid = ResourceRecord::new(ns_rdata); + resource_record_invalid.set_ttl(4); + let rr_cache_invalid = RRStoredData::new(resource_record_invalid); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("uchile.cl")); + + cache_record_type.add_to_cache_data(Rtype::A, domain_name.clone(), rr_cache_valid); + cache_record_type.add_to_cache_data(Rtype::NS, domain_name.clone(), rr_cache_invalid); + + //check if every record_types_data (HashMap for A and for NS) has 1 element + let record_types_data = cache_record_type.get_cache_data(); + //CacheByDomainName for A type + if let Some(record_types_data_a) = record_types_data.get(&Rtype::A) { + if let Some(rrstore_data_vec_a) = record_types_data_a.clone().get_from_host_data(domain_name.clone()){ + assert_eq!(rrstore_data_vec_a.len(), 1); + } + } + //CacheByDomainName for NS type + if let Some(record_types_data_ns) = record_types_data.get(&Rtype::NS) { + if let Some(rrstore_data_vec_ns) = record_types_data_ns.clone().get_from_host_data(domain_name.clone()){ + assert_eq!(rrstore_data_vec_ns.len(), 1); + } + } + + println!("Before timeout: {:?}", Utc::now()); + thread::sleep(time::Duration::from_secs(5)); + println!("After timeout: {:?}", Utc::now()); + cache_record_type.filter_timeout_cache_data(); + + if let Some(record_types_data_a) = record_types_data.get(&Rtype::A) { + if let Some(rrstore_data_vec_a) = record_types_data_a.clone().get_from_host_data(domain_name.clone()){ + assert_eq!(rrstore_data_vec_a.len(), 1); + } + } + + if let Some(record_types_data_ns) = record_types_data.get(&Rtype::NS) { + println!("{:?}", record_types_data_ns); + assert!(false, "Si habia algo dentro del Rtype NS"); + } else { + assert!(true); + } + + + } } \ No newline at end of file From ca056027a789a6d262315295b124b137a2cd64b2 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Fri, 5 Jan 2024 11:36:00 -0300 Subject: [PATCH 099/137] refactor variables names and change name test for timeout_rr_cache --- .../cache_by_domain_name.rs | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs index 0dad4015..81eaead1 100644 --- a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs +++ b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs @@ -490,37 +490,38 @@ mod host_data_test{ } #[test] - fn timeout_rr_cache() { + fn timeout_rr_cache_one_domain() { use std::{thread, time}; - let mut host_data = CacheByDomainName::new(); + let mut cache_by_domain_name = CacheByDomainName::new(); let a_rdata = Rdata::A(ARdata::new()); let mut resource_record_valid = ResourceRecord::new(a_rdata.clone()); resource_record_valid.set_ttl(1000); - let rr_cache_valid = RRStoredData::new(resource_record_valid.clone()); + let rrstore_data_valid = RRStoredData::new(resource_record_valid.clone()); let mut resource_record_invalid = ResourceRecord::new(a_rdata); resource_record_invalid.set_ttl(4); - let rr_cache_invalid = RRStoredData::new(resource_record_invalid); + let rrstore_data_invalid = RRStoredData::new(resource_record_invalid); let mut domain_name = DomainName::new(); domain_name.set_name(String::from("uchile.cl")); - host_data.add_to_host_data(domain_name.clone(), rr_cache_valid); - host_data.add_to_host_data(domain_name.clone(), rr_cache_invalid); + cache_by_domain_name.add_to_host_data(domain_name.clone(), rrstore_data_valid); + cache_by_domain_name.add_to_host_data(domain_name.clone(), rrstore_data_invalid); - assert_eq!(host_data.get_domain_names_data().len(), 1); - if let Some(rr_cache_vec) = host_data.get_domain_names_data().get(&domain_name) { + assert_eq!(cache_by_domain_name.get_domain_names_data().len(), 1); + if let Some(rr_cache_vec) = cache_by_domain_name.get_domain_names_data().get(&domain_name) { assert_eq!(rr_cache_vec.len(), 2); } println!("Before timeout: {:?}", Utc::now()); thread::sleep(time::Duration::from_secs(5)); println!("After timeout: {:?}", Utc::now()); - host_data.filter_timeout_host_data(); + //clean the data with expired ttl + cache_by_domain_name.filter_timeout_host_data(); - assert_eq!(host_data.get_domain_names_data().len(), 1); - if let Some(rr_cache_vec) = host_data.get_domain_names_data().get(&domain_name) { + assert_eq!(cache_by_domain_name.get_domain_names_data().len(), 1); + if let Some(rr_cache_vec) = cache_by_domain_name.get_domain_names_data().get(&domain_name) { assert_eq!(rr_cache_vec.len(), 1); //check if the rescource record who survives is the correct if let Some(rrstore_data_valid) = rr_cache_vec.get(0){ From 8015de7f02f1ca27699f9eb5c1d16c141f3f7aed Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Fri, 5 Jan 2024 12:16:58 -0300 Subject: [PATCH 100/137] add test timeout cache by domain name with 2 differents domains --- .../cache_by_domain_name.rs | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs index 81eaead1..3dac519f 100644 --- a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs +++ b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs @@ -530,4 +530,56 @@ mod host_data_test{ } } } + + #[test] + fn timeout_rr_cache_two_domain(){ + //this test prove the for iteration in filter_timeout_host_data + use std::{thread, time}; + let mut cache_by_domain_name = CacheByDomainName::new(); + let a_rdata = Rdata::A(ARdata::new()); + + let mut resource_record_valid = ResourceRecord::new(a_rdata.clone()); + resource_record_valid.set_ttl(1000); + let rrstore_data_valid = RRStoredData::new(resource_record_valid.clone()); + + let mut resource_record_invalid = ResourceRecord::new(a_rdata); + resource_record_invalid.set_ttl(4); + let rrstore_data_invalid = RRStoredData::new(resource_record_invalid); + + let mut domain_name_1 = DomainName::new(); + domain_name_1.set_name(String::from("uchile.cl")); + + let mut domain_name_2 = DomainName::new(); + domain_name_2.set_name(String::from("example.com")); + + cache_by_domain_name.add_to_host_data(domain_name_1.clone(), rrstore_data_valid); + cache_by_domain_name.add_to_host_data(domain_name_2.clone(), rrstore_data_invalid); + + assert_eq!(cache_by_domain_name.get_domain_names_data().len(), 2); + if let Some(rr_cache_vec) = cache_by_domain_name.get_domain_names_data().get(&domain_name_1) { + assert_eq!(rr_cache_vec.len(), 1); + } + if let Some(rr_cache_vec) = cache_by_domain_name.get_domain_names_data().get(&domain_name_2) { + assert_eq!(rr_cache_vec.len(), 1); + } + + println!("Before timeout: {:?}", Utc::now()); + thread::sleep(time::Duration::from_secs(5)); + println!("After timeout: {:?}", Utc::now()); + //clean the data with expired ttl + cache_by_domain_name.filter_timeout_host_data(); + + println!("The new cache is {:?} ", cache_by_domain_name.get_domain_names_data()); + + //check if the value who survives is the same + if let Some(rr_cache_vec) = cache_by_domain_name.get_domain_names_data().get(&domain_name_2) { + if let Some(rrstore_data) = rr_cache_vec.get(0) { + let resocurce_record_after_clean = rrstore_data.get_resource_record(); + assert_eq!(resocurce_record_after_clean, resource_record_valid); + } + } + //after the filter shoud be just one data in the cache (example.com shoud have been eliminated) + //FIXME: + assert_eq!(cache_by_domain_name.get_domain_names_data().len(), 1); + } } From 5d9f831608330be109503e60d966ed4fa9a1e3c6 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Fri, 5 Jan 2024 12:36:01 -0300 Subject: [PATCH 101/137] add test filter_timeout with rtype NS in cache_by_recor_type --- src/dns_cache/cache_by_record_type.rs | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index c5056aaa..b7db6b0d 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -482,6 +482,48 @@ mod cache_data_test{ } + #[test] + fn filter_timeout_cache_data_rtype_ns() { + use std::{thread, time}; + let mut cache_record_type = CacheByRecordType::new(); + let ns_rdata = Rdata::NS(NsRdata::new()); + + let mut resource_record_valid = ResourceRecord::new(ns_rdata.clone()); + resource_record_valid.set_ttl(1000); + let rr_cache_valid = RRStoredData::new(resource_record_valid.clone()); + + let mut resource_record_invalid = ResourceRecord::new(ns_rdata); + resource_record_invalid.set_ttl(4); + let rr_cache_invalid = RRStoredData::new(resource_record_invalid); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("uchile.cl")); + + cache_record_type.add_to_cache_data(Rtype::NS, domain_name.clone(), rr_cache_valid); + cache_record_type.add_to_cache_data(Rtype::NS, domain_name.clone(), rr_cache_invalid); + + //check if the domain with A type has 2 RRStoredData + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::NS){ + assert_eq!(rr_cache_vec.len(), 2); + } + + println!("Before timeout: {:?}", Utc::now()); + thread::sleep(time::Duration::from_secs(5)); + println!("After timeout: {:?}", Utc::now()); + cache_record_type.filter_timeout_cache_data(); + + //check if the len is 1 instead of 2 (one RRStoredData was eliminated) + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::NS){ + assert_eq!(rr_cache_vec.len(), 1); + //chek if the resource record who survives is the right one + if let Some(rrstore_data_valid) = rr_cache_vec.get(0){ + let resource_record_after_filter = rrstore_data_valid.get_resource_record(); + assert_eq!(resource_record_after_filter, resource_record_valid); + } + } + + } + #[test] fn filter_timout_cache_data_2_differents_rtypes(){ use std::{thread, time}; From 616bf2fecd158e74436d190ab45176de8e6b287c Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Fri, 5 Jan 2024 12:36:49 -0300 Subject: [PATCH 102/137] add FIXME with documentation in test with 2 differents domains in cache by domain --- src/dns_cache/cache_by_record_type/cache_by_domain_name.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs index 3dac519f..725c7d68 100644 --- a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs +++ b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs @@ -579,7 +579,7 @@ mod host_data_test{ } } //after the filter shoud be just one data in the cache (example.com shoud have been eliminated) - //FIXME: + //FIXME: Does not eliminated the (key, data), instead the key points to a empty array ( Domain name {example.com} -> []) assert_eq!(cache_by_domain_name.get_domain_names_data().len(), 1); } } From e0a5d01ef53d2a566d3482b7cccd9ca492bca1c8 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Fri, 5 Jan 2024 12:39:11 -0300 Subject: [PATCH 103/137] add test filter_timeout with rtype CNAME in cache_by_recor_type --- src/dns_cache/cache_by_record_type.rs | 43 +++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index b7db6b0d..86100021 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -229,6 +229,7 @@ mod cache_data_test{ //use std::thread::sleep; //use std::time::Duration as StdDuration; + use crate::message::rdata::cname_rdata::CnameRdata; use crate::message::rdata::{txt_rdata::TxtRdata, ns_rdata::NsRdata}; use crate::message::type_rtype::Rtype; use crate::dns_cache::cache_by_record_type::rr_stored_data::RRStoredData; @@ -524,6 +525,48 @@ mod cache_data_test{ } + #[test] + fn filter_timeout_cache_data_rtype_cname() { + use std::{thread, time}; + let mut cache_record_type = CacheByRecordType::new(); + let cname_rdata = Rdata::CNAME(CnameRdata::new()); + + let mut resource_record_valid = ResourceRecord::new(cname_rdata.clone()); + resource_record_valid.set_ttl(1000); + let rr_cache_valid = RRStoredData::new(resource_record_valid.clone()); + + let mut resource_record_invalid = ResourceRecord::new(cname_rdata); + resource_record_invalid.set_ttl(4); + let rr_cache_invalid = RRStoredData::new(resource_record_invalid); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("uchile.cl")); + + cache_record_type.add_to_cache_data(Rtype::CNAME, domain_name.clone(), rr_cache_valid); + cache_record_type.add_to_cache_data(Rtype::CNAME, domain_name.clone(), rr_cache_invalid); + + //check if the domain with A type has 2 RRStoredData + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::CNAME){ + assert_eq!(rr_cache_vec.len(), 2); + } + + println!("Before timeout: {:?}", Utc::now()); + thread::sleep(time::Duration::from_secs(5)); + println!("After timeout: {:?}", Utc::now()); + cache_record_type.filter_timeout_cache_data(); + + //check if the len is 1 instead of 2 (one RRStoredData was eliminated) + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::CNAME){ + assert_eq!(rr_cache_vec.len(), 1); + //chek if the resource record who survives is the right one + if let Some(rrstore_data_valid) = rr_cache_vec.get(0){ + let resource_record_after_filter = rrstore_data_valid.get_resource_record(); + assert_eq!(resource_record_after_filter, resource_record_valid); + } + } + + } + #[test] fn filter_timout_cache_data_2_differents_rtypes(){ use std::{thread, time}; From 4dee8b8bc43376191bbaaf524b0e838a88b29d5f Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Fri, 5 Jan 2024 12:41:15 -0300 Subject: [PATCH 104/137] add test filter_timeout with rtype SOA in cache_by_recor_type --- src/dns_cache/cache_by_record_type.rs | 43 +++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index 86100021..9cc82e8d 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -230,6 +230,7 @@ mod cache_data_test{ //use std::time::Duration as StdDuration; use crate::message::rdata::cname_rdata::CnameRdata; + use crate::message::rdata::soa_rdata::SoaRdata; use crate::message::rdata::{txt_rdata::TxtRdata, ns_rdata::NsRdata}; use crate::message::type_rtype::Rtype; use crate::dns_cache::cache_by_record_type::rr_stored_data::RRStoredData; @@ -567,6 +568,48 @@ mod cache_data_test{ } + #[test] + fn filter_timeout_cache_data_rtype_soa() { + use std::{thread, time}; + let mut cache_record_type = CacheByRecordType::new(); + let soa_rdata = Rdata::SOA(SoaRdata::new()); + + let mut resource_record_valid = ResourceRecord::new(soa_rdata.clone()); + resource_record_valid.set_ttl(1000); + let rr_cache_valid = RRStoredData::new(resource_record_valid.clone()); + + let mut resource_record_invalid = ResourceRecord::new(soa_rdata); + resource_record_invalid.set_ttl(4); + let rr_cache_invalid = RRStoredData::new(resource_record_invalid); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("uchile.cl")); + + cache_record_type.add_to_cache_data(Rtype::SOA, domain_name.clone(), rr_cache_valid); + cache_record_type.add_to_cache_data(Rtype::SOA, domain_name.clone(), rr_cache_invalid); + + //check if the domain with A type has 2 RRStoredData + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::SOA){ + assert_eq!(rr_cache_vec.len(), 2); + } + + println!("Before timeout: {:?}", Utc::now()); + thread::sleep(time::Duration::from_secs(5)); + println!("After timeout: {:?}", Utc::now()); + cache_record_type.filter_timeout_cache_data(); + + //check if the len is 1 instead of 2 (one RRStoredData was eliminated) + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::SOA){ + assert_eq!(rr_cache_vec.len(), 1); + //chek if the resource record who survives is the right one + if let Some(rrstore_data_valid) = rr_cache_vec.get(0){ + let resource_record_after_filter = rrstore_data_valid.get_resource_record(); + assert_eq!(resource_record_after_filter, resource_record_valid); + } + } + + } + #[test] fn filter_timout_cache_data_2_differents_rtypes(){ use std::{thread, time}; From 0fd0765674d18b28371b402828e2435fa10435cc Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Fri, 5 Jan 2024 12:54:08 -0300 Subject: [PATCH 105/137] add test filter_timeout with rtype PTR in cache_by_recor_type --- src/dns_cache/cache_by_record_type.rs | 43 +++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index 9cc82e8d..61691b8d 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -230,6 +230,7 @@ mod cache_data_test{ //use std::time::Duration as StdDuration; use crate::message::rdata::cname_rdata::CnameRdata; + use crate::message::rdata::ptr_rdata::PtrRdata; use crate::message::rdata::soa_rdata::SoaRdata; use crate::message::rdata::{txt_rdata::TxtRdata, ns_rdata::NsRdata}; use crate::message::type_rtype::Rtype; @@ -610,6 +611,48 @@ mod cache_data_test{ } + #[test] + fn filter_timeout_cache_data_rtype_ptr() { + use std::{thread, time}; + let mut cache_record_type = CacheByRecordType::new(); + let ptr_rdata = Rdata::PTR(PtrRdata::new()); + + let mut resource_record_valid = ResourceRecord::new(ptr_rdata.clone()); + resource_record_valid.set_ttl(1000); + let rr_cache_valid = RRStoredData::new(resource_record_valid.clone()); + + let mut resource_record_invalid = ResourceRecord::new(ptr_rdata); + resource_record_invalid.set_ttl(4); + let rr_cache_invalid = RRStoredData::new(resource_record_invalid); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("uchile.cl")); + + cache_record_type.add_to_cache_data(Rtype::PTR, domain_name.clone(), rr_cache_valid); + cache_record_type.add_to_cache_data(Rtype::PTR, domain_name.clone(), rr_cache_invalid); + + //check if the domain with A type has 2 RRStoredData + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::PTR){ + assert_eq!(rr_cache_vec.len(), 2); + } + + println!("Before timeout: {:?}", Utc::now()); + thread::sleep(time::Duration::from_secs(5)); + println!("After timeout: {:?}", Utc::now()); + cache_record_type.filter_timeout_cache_data(); + + //check if the len is 1 instead of 2 (one RRStoredData was eliminated) + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::PTR){ + assert_eq!(rr_cache_vec.len(), 1); + //chek if the resource record who survives is the right one + if let Some(rrstore_data_valid) = rr_cache_vec.get(0){ + let resource_record_after_filter = rrstore_data_valid.get_resource_record(); + assert_eq!(resource_record_after_filter, resource_record_valid); + } + } + + } + #[test] fn filter_timout_cache_data_2_differents_rtypes(){ use std::{thread, time}; From ba168c5fd04eed259b2deb963038dd4c5978a483 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Fri, 5 Jan 2024 12:56:02 -0300 Subject: [PATCH 106/137] add test filter_timeout with rtype MX in cache_by_recor_type --- src/dns_cache/cache_by_record_type.rs | 43 +++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index 61691b8d..5c9bae2c 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -230,6 +230,7 @@ mod cache_data_test{ //use std::time::Duration as StdDuration; use crate::message::rdata::cname_rdata::CnameRdata; + use crate::message::rdata::mx_rdata::MxRdata; use crate::message::rdata::ptr_rdata::PtrRdata; use crate::message::rdata::soa_rdata::SoaRdata; use crate::message::rdata::{txt_rdata::TxtRdata, ns_rdata::NsRdata}; @@ -653,6 +654,48 @@ mod cache_data_test{ } + #[test] + fn filter_timeout_cache_data_rtype_mx() { + use std::{thread, time}; + let mut cache_record_type = CacheByRecordType::new(); + let mx_rdata = Rdata::MX(MxRdata::new()); + + let mut resource_record_valid = ResourceRecord::new(mx_rdata.clone()); + resource_record_valid.set_ttl(1000); + let rr_cache_valid = RRStoredData::new(resource_record_valid.clone()); + + let mut resource_record_invalid = ResourceRecord::new(mx_rdata); + resource_record_invalid.set_ttl(4); + let rr_cache_invalid = RRStoredData::new(resource_record_invalid); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("uchile.cl")); + + cache_record_type.add_to_cache_data(Rtype::MX, domain_name.clone(), rr_cache_valid); + cache_record_type.add_to_cache_data(Rtype::MX, domain_name.clone(), rr_cache_invalid); + + //check if the domain with A type has 2 RRStoredData + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::MX){ + assert_eq!(rr_cache_vec.len(), 2); + } + + println!("Before timeout: {:?}", Utc::now()); + thread::sleep(time::Duration::from_secs(5)); + println!("After timeout: {:?}", Utc::now()); + cache_record_type.filter_timeout_cache_data(); + + //check if the len is 1 instead of 2 (one RRStoredData was eliminated) + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::MX){ + assert_eq!(rr_cache_vec.len(), 1); + //chek if the resource record who survives is the right one + if let Some(rrstore_data_valid) = rr_cache_vec.get(0){ + let resource_record_after_filter = rrstore_data_valid.get_resource_record(); + assert_eq!(resource_record_after_filter, resource_record_valid); + } + } + + } + #[test] fn filter_timout_cache_data_2_differents_rtypes(){ use std::{thread, time}; From bd7e3ae5d54138f6a1e12a86615b41e732a7cd3a Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Fri, 5 Jan 2024 13:00:45 -0300 Subject: [PATCH 107/137] add test filter_timeout with rtype TXT in cache_by_recor_type --- src/dns_cache/cache_by_record_type.rs | 42 +++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index 5c9bae2c..34d94fb6 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -696,6 +696,48 @@ mod cache_data_test{ } + #[test] + fn filter_timeout_cache_data_rtype_txt() { + use std::{thread, time}; + let mut cache_record_type = CacheByRecordType::new(); + let txt_rdata = Rdata::TXT(TxtRdata::new(vec![String::from("test")])); + + let mut resource_record_valid = ResourceRecord::new(txt_rdata.clone()); + resource_record_valid.set_ttl(1000); + let rr_cache_valid = RRStoredData::new(resource_record_valid.clone()); + + let mut resource_record_invalid = ResourceRecord::new(txt_rdata); + resource_record_invalid.set_ttl(4); + let rr_cache_invalid = RRStoredData::new(resource_record_invalid); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("uchile.cl")); + + cache_record_type.add_to_cache_data(Rtype::TXT, domain_name.clone(), rr_cache_valid); + cache_record_type.add_to_cache_data(Rtype::TXT, domain_name.clone(), rr_cache_invalid); + + //check if the domain with A type has 2 RRStoredData + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::TXT){ + assert_eq!(rr_cache_vec.len(), 2); + } + + println!("Before timeout: {:?}", Utc::now()); + thread::sleep(time::Duration::from_secs(5)); + println!("After timeout: {:?}", Utc::now()); + cache_record_type.filter_timeout_cache_data(); + + //check if the len is 1 instead of 2 (one RRStoredData was eliminated) + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::TXT){ + assert_eq!(rr_cache_vec.len(), 1); + //chek if the resource record who survives is the right one + if let Some(rrstore_data_valid) = rr_cache_vec.get(0){ + let resource_record_after_filter = rrstore_data_valid.get_resource_record(); + assert_eq!(resource_record_after_filter, resource_record_valid); + } + } + + } + #[test] fn filter_timout_cache_data_2_differents_rtypes(){ use std::{thread, time}; From 6ef7b4ab68d4ad3a906b62c82ae5c6eb7ac187e2 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Fri, 5 Jan 2024 13:02:58 -0300 Subject: [PATCH 108/137] add test filter_timeout with rtype HINFO in cache_by_recor_type --- src/dns_cache/cache_by_record_type.rs | 43 +++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index 34d94fb6..9ab0c415 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -230,6 +230,7 @@ mod cache_data_test{ //use std::time::Duration as StdDuration; use crate::message::rdata::cname_rdata::CnameRdata; + use crate::message::rdata::hinfo_rdata::HinfoRdata; use crate::message::rdata::mx_rdata::MxRdata; use crate::message::rdata::ptr_rdata::PtrRdata; use crate::message::rdata::soa_rdata::SoaRdata; @@ -738,6 +739,48 @@ mod cache_data_test{ } + #[test] + fn filter_timeout_cache_data_rtype_hinfo() { + use std::{thread, time}; + let mut cache_record_type = CacheByRecordType::new(); + let hinfo_rdata = Rdata::HINFO(HinfoRdata::new()); + + let mut resource_record_valid = ResourceRecord::new(hinfo_rdata.clone()); + resource_record_valid.set_ttl(1000); + let rr_cache_valid = RRStoredData::new(resource_record_valid.clone()); + + let mut resource_record_invalid = ResourceRecord::new(hinfo_rdata); + resource_record_invalid.set_ttl(4); + let rr_cache_invalid = RRStoredData::new(resource_record_invalid); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("uchile.cl")); + + cache_record_type.add_to_cache_data(Rtype::HINFO, domain_name.clone(), rr_cache_valid); + cache_record_type.add_to_cache_data(Rtype::HINFO, domain_name.clone(), rr_cache_invalid); + + //check if the domain with A type has 2 RRStoredData + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::HINFO){ + assert_eq!(rr_cache_vec.len(), 2); + } + + println!("Before timeout: {:?}", Utc::now()); + thread::sleep(time::Duration::from_secs(5)); + println!("After timeout: {:?}", Utc::now()); + cache_record_type.filter_timeout_cache_data(); + + //check if the len is 1 instead of 2 (one RRStoredData was eliminated) + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::HINFO){ + assert_eq!(rr_cache_vec.len(), 1); + //chek if the resource record who survives is the right one + if let Some(rrstore_data_valid) = rr_cache_vec.get(0){ + let resource_record_after_filter = rrstore_data_valid.get_resource_record(); + assert_eq!(resource_record_after_filter, resource_record_valid); + } + } + + } + #[test] fn filter_timout_cache_data_2_differents_rtypes(){ use std::{thread, time}; From ea2b5110bd75936684dc8b80e142a45d7f3ada6a Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Fri, 5 Jan 2024 13:04:14 -0300 Subject: [PATCH 109/137] add test filter_timeout with rtype TSIG in cache_by_recor_type --- src/dns_cache/cache_by_record_type.rs | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index 9ab0c415..9e0d5ee9 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -234,6 +234,7 @@ mod cache_data_test{ use crate::message::rdata::mx_rdata::MxRdata; use crate::message::rdata::ptr_rdata::PtrRdata; use crate::message::rdata::soa_rdata::SoaRdata; + use crate::message::rdata::tsig_rdata::TSigRdata; use crate::message::rdata::{txt_rdata::TxtRdata, ns_rdata::NsRdata}; use crate::message::type_rtype::Rtype; use crate::dns_cache::cache_by_record_type::rr_stored_data::RRStoredData; @@ -781,6 +782,49 @@ mod cache_data_test{ } + + #[test] + fn filter_timeout_cache_data_rtype_tsig() { + use std::{thread, time}; + let mut cache_record_type = CacheByRecordType::new(); + let tsig_rdata = Rdata::TSIG(TSigRdata::new()); + + let mut resource_record_valid = ResourceRecord::new(tsig_rdata.clone()); + resource_record_valid.set_ttl(1000); + let rr_cache_valid = RRStoredData::new(resource_record_valid.clone()); + + let mut resource_record_invalid = ResourceRecord::new(tsig_rdata); + resource_record_invalid.set_ttl(4); + let rr_cache_invalid = RRStoredData::new(resource_record_invalid); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("uchile.cl")); + + cache_record_type.add_to_cache_data(Rtype::TSIG, domain_name.clone(), rr_cache_valid); + cache_record_type.add_to_cache_data(Rtype::TSIG, domain_name.clone(), rr_cache_invalid); + + //check if the domain with A type has 2 RRStoredData + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::TSIG){ + assert_eq!(rr_cache_vec.len(), 2); + } + + println!("Before timeout: {:?}", Utc::now()); + thread::sleep(time::Duration::from_secs(5)); + println!("After timeout: {:?}", Utc::now()); + cache_record_type.filter_timeout_cache_data(); + + //check if the len is 1 instead of 2 (one RRStoredData was eliminated) + if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::TSIG){ + assert_eq!(rr_cache_vec.len(), 1); + //chek if the resource record who survives is the right one + if let Some(rrstore_data_valid) = rr_cache_vec.get(0){ + let resource_record_after_filter = rrstore_data_valid.get_resource_record(); + assert_eq!(resource_record_after_filter, resource_record_valid); + } + } + + } + #[test] fn filter_timout_cache_data_2_differents_rtypes(){ use std::{thread, time}; From 853c032434bc178827ed1aa3149a0bb123454434 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Fri, 5 Jan 2024 17:50:23 -0300 Subject: [PATCH 110/137] add test timeout cache 1 domain same Rtype in DnsCache, but faills --- src/dns_cache.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/dns_cache.rs b/src/dns_cache.rs index fd0a2f13..3c9951bd 100644 --- a/src/dns_cache.rs +++ b/src/dns_cache.rs @@ -1,5 +1,6 @@ pub mod cache_by_record_type; + use crate::dns_cache::cache_by_record_type::CacheByRecordType; use crate::dns_cache::cache_by_record_type::rr_stored_data::RRStoredData; use crate::message::rdata::Rdata; @@ -218,6 +219,7 @@ impl DnsCache { #[cfg(test)] mod dns_cache_test { + use chrono::Utc; use crate::dns_cache::DnsCache; use crate::dns_cache::cache_by_record_type::CacheByRecordType; use crate::dns_cache::cache_by_record_type::cache_by_domain_name::CacheByDomainName; @@ -498,4 +500,58 @@ mod dns_cache_test { assert_eq!(cache.is_cached(domain_name, Rtype::A), true); } + + #[test] + fn timeout_cache_1_domain_same_type(){ + use std::{thread, time}; + let mut dns_cache = DnsCache::new(); + + dns_cache.set_max_size(3); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("uchile.cl")); + + let a_rdata = Rdata::A(ARdata::new()); + + let mut resource_record = ResourceRecord::new(a_rdata.clone()); + resource_record.set_ttl(4); + + dns_cache.add(domain_name.clone(), resource_record.clone()); + + assert_eq!(dns_cache.get_cache().get_cache_data().len(), 1); + + let mut resource_record_2 = ResourceRecord::new(a_rdata.clone()); + resource_record_2.set_ttl(4); + + dns_cache.add(domain_name.clone(), resource_record_2.clone()); + + //because both are of the same type, the cache_data (cache by record type) has 1 element + // Rdata::A -> cache_by_domain_name + assert_eq!(dns_cache.get_cache().get_cache_data().len(), 1); + if let Some(cache_by_domain_name) = dns_cache.get_cache().get(Rtype::A) { + if let Some(rrstore_data_vec) = cache_by_domain_name.get(&domain_name) { + assert_eq!(rrstore_data_vec.len(), 2); + } + } + assert_eq!(dns_cache.get_size(), 2); + + println!("Before timeout: {:?}", Utc::now()); + thread::sleep(time::Duration::from_secs(5)); + println!("After timeout: {:?}", Utc::now()); + dns_cache.timeout_cache(); + + //FIXME: the size shoud be 1 because we have only 1 resocurce_record associated with 1 domain + // assert_eq!(dns_cache.get_size(), 1); + + //check iof the resource_record_2 was deleted + if let Some(cache_by_domain_name) = dns_cache.get_cache().get(Rtype::A) { + if let Some(rrstore_data_vec) = cache_by_domain_name.get(&domain_name) { + //FIXME: gives 0 instead of 1, meaning all the rrstored records were deleted, when only 1 shoud have been deleted + assert_eq!(rrstore_data_vec.len(), 1); + } + } + + } + + } From a6dbdf390c9fff3d93743fbad48737487d6bfbfe Mon Sep 17 00:00:00 2001 From: Francisca Ortega Date: Mon, 8 Jan 2024 09:56:57 -0500 Subject: [PATCH 111/137] added get ip from the sender --- src/client/client_connection.rs | 3 +-- src/client/tcp_connection.rs | 9 +++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/client/client_connection.rs b/src/client/client_connection.rs index d7658a32..4d88db7f 100644 --- a/src/client/client_connection.rs +++ b/src/client/client_connection.rs @@ -10,8 +10,7 @@ pub trait ClientConnection: Copy {//: 'static + Sized + Send + Sync + Unpin timeout:Duration) -> Self; //Sends query - fn send(self,dns_query:DnsMessage) -> Result, ClientError>; - + fn send(self, dns_query: DnsMessage) -> Result<(Vec, IpAddr), ClientError>; fn get_ip(&self) -> IpAddr; } diff --git a/src/client/tcp_connection.rs b/src/client/tcp_connection.rs index fa049c91..4f5c8cd6 100644 --- a/src/client/tcp_connection.rs +++ b/src/client/tcp_connection.rs @@ -4,7 +4,7 @@ use super::client_error::ClientError; use std::io::{Write, Read}; -use std::net::{TcpStream,SocketAddr,IpAddr}; +use std::net::{TcpStream,SocketAddr,IpAddr, Ipv4Addr}; use std::time::Duration; use std::io::Error as IoError; use std::io::ErrorKind; @@ -34,7 +34,7 @@ impl ClientConnection for ClientTCPConnection { } /// creates socket tcp, sends query and receive response - fn send(self, dns_query: DnsMessage) -> Result, ClientError>{ + fn send(self, dns_query: DnsMessage) -> Result<(Vec, IpAddr), ClientError>{ let timeout: Duration = self.get_timeout(); let bytes: Vec = dns_query.to_bytes(); @@ -58,6 +58,7 @@ impl ClientConnection for ClientTCPConnection { let tcp_msg_len: u16 = (msg_size_response[0] as u16) << 8 | msg_size_response[1] as u16; let mut vec_msg: Vec = Vec::new(); + let ip = self.get_server_addr(); while vec_msg.len() < tcp_msg_len as usize { let mut msg = [0; 512]; @@ -69,7 +70,7 @@ impl ClientConnection for ClientTCPConnection { vec_msg.extend_from_slice(&msg[..number_of_bytes_msg]); } - return Ok(vec_msg); + return Ok((vec_msg, ip)); } } @@ -186,7 +187,7 @@ mod tcp_connection_test{ 0, false, 1); - let response = conn_new.send(dns_query).unwrap(); + let (response, ip) = conn_new.send(dns_query).unwrap(); assert!(DnsMessage::from_bytes(&response).unwrap().get_answer().len() > 0); // FIXME: From 21e890266ec7c1ce4c04e6709f8b39091f7476ae Mon Sep 17 00:00:00 2001 From: Francisca Ortega Date: Mon, 8 Jan 2024 10:01:48 -0500 Subject: [PATCH 112/137] added: send also return ip address --- src/client/udp_connection.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/client/udp_connection.rs b/src/client/udp_connection.rs index bcabdda2..710aea0e 100644 --- a/src/client/udp_connection.rs +++ b/src/client/udp_connection.rs @@ -32,7 +32,7 @@ impl ClientConnection for ClientUDPConnection { return self.server_addr.clone(); } - fn send(self, dns_query:DnsMessage) -> Result, ClientError> { + fn send(self, dns_query:DnsMessage) -> Result<(Vec, IpAddr), ClientError> { let timeout:Duration = self.timeout; let server_addr = SocketAddr::new(self.get_server_addr(), 53); @@ -61,8 +61,10 @@ impl ClientConnection for ClientUDPConnection { Ok(_) => (), }; + let ip = self.get_server_addr(); + drop(socket_udp); - return Ok(msg.to_vec()); + return Ok((msg.to_vec(), ip)); } } @@ -199,7 +201,7 @@ mod udp_connection_test{ false, 1); - let response = conn.send(dns_query).unwrap(); + let (response, ip) = conn.send(dns_query).unwrap(); // assert!(result.is_ok()); From 822064c11bfa0d7be60e07c22de20c113afb75f0 Mon Sep 17 00:00:00 2001 From: Francisca Ortega Date: Mon, 8 Jan 2024 10:06:07 -0500 Subject: [PATCH 113/137] added: check if the ip of who sends an receiveque query is the same --- src/client.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/client.rs b/src/client.rs index 72b26615..8d37d03e 100644 --- a/src/client.rs +++ b/src/client.rs @@ -104,11 +104,13 @@ impl Client { let client_query = self.get_dns_query(); let conn: &T = &self.get_conn(); - let _ip_addr = conn.get_ip(); + let ip_addr = conn.get_ip(); let dns_response: DnsMessage = match conn.send(client_query) { - Ok(response_message) => { - //let response_ip = get_sender_ip(&response_message)?; + Ok((response_message, ip)) => { + if ip != ip_addr { + return Err(ClientError::Message("The ip address of the server is not the same as the one in the connection."))?; + } match DnsMessage::from_bytes(&response_message) { Ok(dns_message) => dns_message, Err(_) => return Err(ClientError::FormatError("The name server was unable to interpret the query."))?, From 635bf399cb77d12433c47c1dc58444a35091b8b4 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Mon, 8 Jan 2024 12:15:52 -0300 Subject: [PATCH 114/137] fix test two domains in timout rr cache in cache by domain --- .../cache_by_record_type/cache_by_domain_name.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs index 725c7d68..7a3a4708 100644 --- a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs +++ b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs @@ -542,7 +542,7 @@ mod host_data_test{ resource_record_valid.set_ttl(1000); let rrstore_data_valid = RRStoredData::new(resource_record_valid.clone()); - let mut resource_record_invalid = ResourceRecord::new(a_rdata); + let mut resource_record_invalid = ResourceRecord::new(a_rdata.clone()); resource_record_invalid.set_ttl(4); let rrstore_data_invalid = RRStoredData::new(resource_record_invalid); @@ -552,8 +552,8 @@ mod host_data_test{ let mut domain_name_2 = DomainName::new(); domain_name_2.set_name(String::from("example.com")); - cache_by_domain_name.add_to_host_data(domain_name_1.clone(), rrstore_data_valid); - cache_by_domain_name.add_to_host_data(domain_name_2.clone(), rrstore_data_invalid); + cache_by_domain_name.add_to_host_data(domain_name_1.clone(), rrstore_data_valid.clone()); + cache_by_domain_name.add_to_host_data(domain_name_2.clone(), rrstore_data_invalid.clone()); assert_eq!(cache_by_domain_name.get_domain_names_data().len(), 2); if let Some(rr_cache_vec) = cache_by_domain_name.get_domain_names_data().get(&domain_name_1) { @@ -569,13 +569,13 @@ mod host_data_test{ //clean the data with expired ttl cache_by_domain_name.filter_timeout_host_data(); - println!("The new cache is {:?} ", cache_by_domain_name.get_domain_names_data()); + // println!("The new cache is {:?} ", cache_by_domain_name.get_domain_names_data()); //check if the value who survives is the same - if let Some(rr_cache_vec) = cache_by_domain_name.get_domain_names_data().get(&domain_name_2) { + if let Some(rr_cache_vec) = cache_by_domain_name.get_domain_names_data().get(&domain_name_1) { if let Some(rrstore_data) = rr_cache_vec.get(0) { - let resocurce_record_after_clean = rrstore_data.get_resource_record(); - assert_eq!(resocurce_record_after_clean, resource_record_valid); + println!("the rrstore for domain {:?} afther de timeout is {:?} ", domain_name_1.get_name(), rrstore_data); + assert_eq!(rrstore_data_valid, rrstore_data.clone()); } } //after the filter shoud be just one data in the cache (example.com shoud have been eliminated) From fd52482c969869f97cf99169f9d2278bfe571a45 Mon Sep 17 00:00:00 2001 From: Francisca Ortega Date: Mon, 8 Jan 2024 10:31:52 -0500 Subject: [PATCH 115/137] fixed: extended changes to lookup --- src/async_resolver/lookup.rs | 9 ++++++--- src/client/tcp_connection.rs | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/async_resolver/lookup.rs b/src/async_resolver/lookup.rs index 6f294365..559d9dcd 100644 --- a/src/async_resolver/lookup.rs +++ b/src/async_resolver/lookup.rs @@ -6,6 +6,7 @@ use crate::client::client_connection::ClientConnection; use crate::message::class_qclass::Qclass; use crate::message::type_qtype::Qtype; use futures_util::{FutureExt,task::Waker}; +use std::net::IpAddr; use std::thread; use std::time::Duration; use std::pin::Pin; @@ -289,9 +290,9 @@ fn send_query_resolver_by_protocol(protocol: ConnectionProtocol,query:DnsMessage /// excessively long TTL, say greater than 1 week, either discard /// the whole response, or limit all TTLs in the response to 1 /// week. -fn parse_response(response_result: Result, ClientError>, query_id:u16) -> Result { +fn parse_response(response_result: Result<(Vec, IpAddr), ClientError>, query_id:u16) -> Result { let dns_msg = response_result.map_err(Into::into) - .and_then(|response_message| { + .and_then(|(response_message , _ip)| { DnsMessage::from_bytes(&response_message) .map_err(|_| ResolverError::Parse("The name server was unable to interpret the query.".to_string())) })?; @@ -595,7 +596,7 @@ mod async_resolver_test { // TODO: test } - +/* #[test] #[ignore] //FIXME: fn parse_response_ok() { @@ -606,6 +607,7 @@ mod async_resolver_test { 1, 0, 0, 0b00010110, 0b00001010, 0, 6, 5, 104, 101, 108, 108, 111, ]; let query_id = 0b00100100; + let ip let response_result: Result, ClientError> = Ok(bytes.to_vec()); let response_dns_msg = parse_response(response_result,query_id); println!("[###############] {:?}",response_dns_msg); @@ -676,4 +678,5 @@ mod async_resolver_test { assert!(false); } } + */ } \ No newline at end of file diff --git a/src/client/tcp_connection.rs b/src/client/tcp_connection.rs index 4f5c8cd6..bafa7dee 100644 --- a/src/client/tcp_connection.rs +++ b/src/client/tcp_connection.rs @@ -187,7 +187,7 @@ mod tcp_connection_test{ 0, false, 1); - let (response, ip) = conn_new.send(dns_query).unwrap(); + let (response, _ip) = conn_new.send(dns_query).unwrap(); assert!(DnsMessage::from_bytes(&response).unwrap().get_answer().len() > 0); // FIXME: From 251e940ac14919436921b30b2a8650c7769a395e Mon Sep 17 00:00:00 2001 From: Francisca Ortega Date: Mon, 8 Jan 2024 10:32:25 -0500 Subject: [PATCH 116/137] fixed: warning in udp connection --- src/client/udp_connection.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/udp_connection.rs b/src/client/udp_connection.rs index 710aea0e..73be020b 100644 --- a/src/client/udp_connection.rs +++ b/src/client/udp_connection.rs @@ -201,7 +201,7 @@ mod udp_connection_test{ false, 1); - let (response, ip) = conn.send(dns_query).unwrap(); + let (response, _ip) = conn.send(dns_query).unwrap(); // assert!(result.is_ok()); From cdd3e5b50fcd198e544ffe100bff669885309bfa Mon Sep 17 00:00:00 2001 From: Francisca Ortega Date: Mon, 8 Jan 2024 10:32:48 -0500 Subject: [PATCH 117/137] fixed: warning in tcp connection --- src/client/tcp_connection.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/tcp_connection.rs b/src/client/tcp_connection.rs index bafa7dee..56b411b1 100644 --- a/src/client/tcp_connection.rs +++ b/src/client/tcp_connection.rs @@ -4,7 +4,7 @@ use super::client_error::ClientError; use std::io::{Write, Read}; -use std::net::{TcpStream,SocketAddr,IpAddr, Ipv4Addr}; +use std::net::{TcpStream,SocketAddr,IpAddr}; use std::time::Duration; use std::io::Error as IoError; use std::io::ErrorKind; From d05ac75c091033880340922c4cdb48083d629360 Mon Sep 17 00:00:00 2001 From: Francisca Ortega Date: Mon, 8 Jan 2024 10:47:34 -0500 Subject: [PATCH 118/137] fixed : tests in lookup --- src/async_resolver/lookup.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/async_resolver/lookup.rs b/src/async_resolver/lookup.rs index 559d9dcd..68bb9903 100644 --- a/src/async_resolver/lookup.rs +++ b/src/async_resolver/lookup.rs @@ -596,7 +596,7 @@ mod async_resolver_test { // TODO: test } -/* + #[test] #[ignore] //FIXME: fn parse_response_ok() { @@ -607,8 +607,8 @@ mod async_resolver_test { 1, 0, 0, 0b00010110, 0b00001010, 0, 6, 5, 104, 101, 108, 108, 111, ]; let query_id = 0b00100100; - let ip - let response_result: Result, ClientError> = Ok(bytes.to_vec()); + let ip = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); + let response_result: Result<(Vec, IpAddr), ClientError> = Ok((bytes.to_vec(), ip)); let response_dns_msg = parse_response(response_result,query_id); println!("[###############] {:?}",response_dns_msg); assert!(response_dns_msg.is_ok()); @@ -630,7 +630,8 @@ mod async_resolver_test { 1, 0, 0, 0b00010110, 0b00001010, 0, 6, 5, 104, 101, 108, 108, 111, ]; let query_id = 0b10100101; - let response_result: Result, ClientError> = Ok(bytes.to_vec()); + let ip = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); + let response_result: Result<(Vec, IpAddr), ClientError> = Ok((bytes.to_vec(), ip)); let response_dns_msg = parse_response(response_result,query_id); let err_msg = "Message is a query. A response was expected.".to_string(); if let Err(ResolverError::Parse(err)) = response_dns_msg { @@ -639,7 +640,7 @@ mod async_resolver_test { assert!(false); } } - + /* #[test] fn parse_error() { let bytes: [u8; 50] = [ @@ -649,7 +650,8 @@ mod async_resolver_test { 1, 0, 0, 0b00010110, 0b00001010, 0, 6, 5, 104, 101, 108, 108, 111, ]; let query_id = 0b10100101; - let response_result: Result, ClientError> = Ok(bytes.to_vec()); + let ip= IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); + let response_result: Result<(Vec, IpAddr), ClientError> = Ok((bytes.to_vec(), ip)); let response_dns_msg = parse_response(response_result,query_id); let err_msg = "The name server was unable to interpret the query.".to_string(); if let Err(ResolverError::Parse(err)) = response_dns_msg { @@ -678,5 +680,6 @@ mod async_resolver_test { assert!(false); } } - */ + */ + } \ No newline at end of file From 68ab57a0067838fcb7ca4695aa5efade23ca1bef Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Mon, 8 Jan 2024 12:49:48 -0300 Subject: [PATCH 119/137] add test timeout cache 2 rtypes 2 domains in cache by rtype --- src/dns_cache/cache_by_record_type.rs | 66 +++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index 9e0d5ee9..d47ec1e4 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -826,7 +826,7 @@ mod cache_data_test{ } #[test] - fn filter_timout_cache_data_2_differents_rtypes(){ + fn filter_timout_cache_data_2_differents_rtypes_same_domain(){ use std::{thread, time}; let mut cache_record_type = CacheByRecordType::new(); let a_rdata = Rdata::A(ARdata::new()); @@ -868,18 +868,76 @@ mod cache_data_test{ if let Some(record_types_data_a) = record_types_data.get(&Rtype::A) { if let Some(rrstore_data_vec_a) = record_types_data_a.clone().get_from_host_data(domain_name.clone()){ + //the valid one still having the value assert_eq!(rrstore_data_vec_a.len(), 1); } } if let Some(record_types_data_ns) = record_types_data.get(&Rtype::NS) { - println!("{:?}", record_types_data_ns); - assert!(false, "Si habia algo dentro del Rtype NS"); + println!(" el CacheByDOmain de NS es {:?}", record_types_data_ns); + assert!(false, "Si habia algo dentro del Rtype NS y NO debía ser así"); } else { assert!(true); } + } - + #[test] + fn filter_timout_cache_data_2_differents_rtypes_different_domain(){ + use std::{thread, time}; + let mut cache_record_type = CacheByRecordType::new(); + let a_rdata = Rdata::A(ARdata::new()); + let ns_rdata = Rdata::NS(NsRdata::new()); + + let mut resource_record_valid = ResourceRecord::new(a_rdata.clone()); + resource_record_valid.set_ttl(1000); + let rr_cache_valid = RRStoredData::new(resource_record_valid.clone()); + + let mut resource_record_invalid = ResourceRecord::new(ns_rdata); + resource_record_invalid.set_ttl(4); + let rr_cache_invalid = RRStoredData::new(resource_record_invalid); + + let mut domain_name_1 = DomainName::new(); + domain_name_1.set_name(String::from("example.com")); + + let mut domain_name_2 = DomainName::new(); + domain_name_2.set_name(String::from("uchile.cl")); + + + cache_record_type.add_to_cache_data(Rtype::A, domain_name_1.clone(), rr_cache_valid); + cache_record_type.add_to_cache_data(Rtype::NS, domain_name_2.clone(), rr_cache_invalid); + + //check if every record_types_data (HashMap for A and for NS) has 1 element + let record_types_data = cache_record_type.get_cache_data(); + //CacheByDomainName for A type + if let Some(record_types_data_a) = record_types_data.get(&Rtype::A) { + if let Some(rrstore_data_vec_a) = record_types_data_a.clone().get_from_host_data(domain_name_1.clone()){ + assert_eq!(rrstore_data_vec_a.len(), 1); + } + } + //CacheByDomainName for NS type + if let Some(record_types_data_ns) = record_types_data.get(&Rtype::NS) { + if let Some(rrstore_data_vec_ns) = record_types_data_ns.clone().get_from_host_data(domain_name_2.clone()){ + assert_eq!(rrstore_data_vec_ns.len(), 1); + } + } + + println!("Before timeout: {:?}", Utc::now()); + thread::sleep(time::Duration::from_secs(5)); + println!("After timeout: {:?}", Utc::now()); + cache_record_type.filter_timeout_cache_data(); + + if let Some(record_types_data_a) = record_types_data.get(&Rtype::A) { + if let Some(rrstore_data_vec_a) = record_types_data_a.clone().get_from_host_data(domain_name_1.clone()){ + //the valid one still having the value + assert_eq!(rrstore_data_vec_a.len(), 1); + } + } + if let Some(record_types_data_ns) = record_types_data.get(&Rtype::NS) { + println!(" el CacheByDomain de NS es : \n {:?}", record_types_data_ns); + assert!(false, "Si habia algo dentro del Rtype NS y NO debía ser así"); + } else { + assert!(true); + } } } \ No newline at end of file From 92343b85e977aaaae1224b399122f803822edc6f Mon Sep 17 00:00:00 2001 From: Francisca Ortega Date: Mon, 8 Jan 2024 10:51:12 -0500 Subject: [PATCH 120/137] fixed error in test lookup --- src/async_resolver/lookup.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/async_resolver/lookup.rs b/src/async_resolver/lookup.rs index 68bb9903..3758dbb4 100644 --- a/src/async_resolver/lookup.rs +++ b/src/async_resolver/lookup.rs @@ -640,7 +640,7 @@ mod async_resolver_test { assert!(false); } } - /* + #[test] fn parse_error() { let bytes: [u8; 50] = [ @@ -670,7 +670,8 @@ mod async_resolver_test { 1, 0, 0, 0b00010110, 0b00001010, 0, 6, 5, 104, 101, 108, 108, 111, ]; let query_id = 0b10100101; - let response_result: Result, ClientError> = Ok(bytes.to_vec()); + let ip = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8)); + let response_result: Result<(Vec, IpAddr), ClientError> = Ok((bytes.to_vec(), ip)); let response_dns_msg = parse_response(response_result,query_id); let err_msg = "The name server was unable to interpret the query.".to_string(); @@ -680,6 +681,6 @@ mod async_resolver_test { assert!(false); } } - */ + } \ No newline at end of file From 0b457b9a8aeaa2d11766ff0f41ff1ace148ea80e Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Mon, 8 Jan 2024 14:37:49 -0300 Subject: [PATCH 121/137] FIx: add new update variable in tests who fails in cache by record (still fail) --- src/dns_cache/cache_by_record_type.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index d47ec1e4..bb8f1891 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -866,7 +866,9 @@ mod cache_data_test{ println!("After timeout: {:?}", Utc::now()); cache_record_type.filter_timeout_cache_data(); - if let Some(record_types_data_a) = record_types_data.get(&Rtype::A) { + let record_types_data_after_clean = cache_record_type.get_cache_data(); + + if let Some(record_types_data_a) = record_types_data_after_clean.get(&Rtype::A) { if let Some(rrstore_data_vec_a) = record_types_data_a.clone().get_from_host_data(domain_name.clone()){ //the valid one still having the value assert_eq!(rrstore_data_vec_a.len(), 1); @@ -926,7 +928,9 @@ mod cache_data_test{ println!("After timeout: {:?}", Utc::now()); cache_record_type.filter_timeout_cache_data(); - if let Some(record_types_data_a) = record_types_data.get(&Rtype::A) { + let record_types_data_after_cleaning = cache_record_type.get_cache_data(); + + if let Some(record_types_data_a) = record_types_data_after_cleaning.get(&Rtype::A) { if let Some(rrstore_data_vec_a) = record_types_data_a.clone().get_from_host_data(domain_name_1.clone()){ //the valid one still having the value assert_eq!(rrstore_data_vec_a.len(), 1); @@ -940,4 +944,5 @@ mod cache_data_test{ assert!(true); } } + } \ No newline at end of file From fa7c89dcd75ac93d3c4c70a542adb8842a8d8f70 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Mon, 8 Jan 2024 14:52:23 -0300 Subject: [PATCH 122/137] add bigger test in cache by rtype to prove the correctly cleaning in a layer down (fails) --- src/dns_cache/cache_by_record_type.rs | 82 +++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index bb8f1891..f1b21ab7 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -945,4 +945,86 @@ mod cache_data_test{ } } + #[test] + //this test is going to prove if the cleaning after the timeout is acting correctly one layer down (CacheByDomain) + // ------BEFORE THE 5 SECONDS----- + // RTYPE:A -> {uchile (invalid) -> [..], example.com (valid) -> [..]} + // RTYPE:NS -> {uchile (valid) -> [..], example.com (invalid) -> [...]} + //-------AFTER THE 5 SECONDS----- + // RTYPE:A -> {example.com -> [...]} + // RTYPE:NS -> {uchile.com -> [...]} + fn filter_timout_cache_data_cleaning_layer_down(){ + use std::{thread, time}; + let mut cache_record_type = CacheByRecordType::new(); + //Defaults Rdatas to use + let a_rdata = Rdata::A(ARdata::new()); + let ns_rdata = Rdata::NS(NsRdata::new()); + + + let mut domain_name_1 = DomainName::new(); + domain_name_1.set_name(String::from("example.com")); + + let mut domain_name_2 = DomainName::new(); + domain_name_2.set_name(String::from("uchile.cl")); + + //adding in A rtypes + let mut resource_record_valid_a = ResourceRecord::new(a_rdata.clone()); + resource_record_valid_a.set_ttl(1000); + let rr_cache_valid_a = RRStoredData::new(resource_record_valid_a.clone()); + cache_record_type.add_to_cache_data(Rtype::A, domain_name_1.clone(), rr_cache_valid_a); + + let mut resource_record_invalid_a = ResourceRecord::new(a_rdata.clone()); + resource_record_invalid_a.set_ttl(4); + let rr_cache_invalid_a = RRStoredData::new(resource_record_invalid_a.clone()); + cache_record_type.add_to_cache_data(Rtype::A, domain_name_2.clone(), rr_cache_invalid_a); + + //adding in NS rtypes + let mut resource_record_valid_ns = ResourceRecord::new(ns_rdata.clone()); + resource_record_valid_ns.set_ttl(1000); + let rr_cache_valid_ns = RRStoredData::new(resource_record_valid_ns.clone()); + cache_record_type.add_to_cache_data(Rtype::NS, domain_name_2.clone(), rr_cache_valid_ns); + + let mut resource_record_invalid_ns = ResourceRecord::new(ns_rdata.clone()); + resource_record_invalid_ns.set_ttl(4); + let rr_cache_invalid_ns = RRStoredData::new(resource_record_invalid_ns.clone()); + cache_record_type.add_to_cache_data(Rtype::NS, domain_name_1.clone(), rr_cache_invalid_ns); + + + //check if every record_types_data (HashMap for A and for NS) has 2 element + let record_types_data = cache_record_type.get_cache_data(); + //CacheByDomainName for A type + if let Some(record_types_data_a) = record_types_data.get(&Rtype::A) { + // println!("the cache by domain for A type is : \n {:?}",record_types_data_a.get_domain_names_data()); + assert_eq!(record_types_data_a.get_domain_names_data().len(), 2); + } + //CacheByDomainName for NS type + if let Some(record_types_data_ns) = record_types_data.get(&Rtype::NS) { + // println!("the cache by domain for NS type is : \n {:?}",record_types_data_ns.get_domain_names_data()); + assert_eq!(record_types_data_ns.get_domain_names_data().len(), 2); + } + + println!("Before timeout: {:?}", Utc::now()); + thread::sleep(time::Duration::from_secs(5)); + println!("After timeout: {:?}", Utc::now()); + cache_record_type.filter_timeout_cache_data(); + + let record_types_data_after_cleaning = cache_record_type.get_cache_data(); + + //after the cleaning, each cache shoud have 1 element + if let Some(record_types_data_a) = record_types_data_after_cleaning.get(&Rtype::A) { + println!("the cache by domain for A type after the cleaning is : \n {:?}",record_types_data_a.get_domain_names_data()); + //FIXME: Does not delete the invadil rrstore, instead points to a empty array (same error as in cache by domain) + assert_eq!(record_types_data_a.get_domain_names_data().len(), 1); + } + //CacheByDomainName for NS type + if let Some(record_types_data_ns) = record_types_data_after_cleaning.get(&Rtype::NS) { + println!("the cache by domain for NS type after the cleaning is : \n {:?}",record_types_data_ns.get_domain_names_data()); + //FIXME: Does not delete the invadil rrstore, instead points to a empty array (same error as in cache by domain) + assert_eq!(record_types_data_ns.get_domain_names_data().len(), 1); + } + + + } + + } \ No newline at end of file From aa7aadf46e94ac836544ba915d707a28aeca31f4 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Mon, 8 Jan 2024 15:10:05 -0300 Subject: [PATCH 123/137] add extra confirmation in test filter_timout_cache_data_cleaning_layer_down --- src/dns_cache/cache_by_record_type.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index f1b21ab7..b9459863 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -1015,12 +1015,26 @@ mod cache_data_test{ println!("the cache by domain for A type after the cleaning is : \n {:?}",record_types_data_a.get_domain_names_data()); //FIXME: Does not delete the invadil rrstore, instead points to a empty array (same error as in cache by domain) assert_eq!(record_types_data_a.get_domain_names_data().len(), 1); + //check if is the same resource record valid (which survives) + if let Some(rrstore_a_after_cleaning) = record_types_data_a.clone().get_from_host_data(domain_name_1.clone()){ + if let Some(rrstore_data_valid) = rrstore_a_after_cleaning.get(0){ + let resource_record_after_filter = rrstore_data_valid.get_resource_record(); + assert_eq!(resource_record_after_filter, resource_record_valid_a); + } + } } //CacheByDomainName for NS type if let Some(record_types_data_ns) = record_types_data_after_cleaning.get(&Rtype::NS) { println!("the cache by domain for NS type after the cleaning is : \n {:?}",record_types_data_ns.get_domain_names_data()); //FIXME: Does not delete the invadil rrstore, instead points to a empty array (same error as in cache by domain) assert_eq!(record_types_data_ns.get_domain_names_data().len(), 1); + //check if is the same resource record valid (which survives) + if let Some(rrstore_ns_after_cleaning) = record_types_data_ns.clone().get_from_host_data(domain_name_2.clone()){ + if let Some(rrstore_data_valid) = rrstore_ns_after_cleaning.get(0){ + let resource_record_after_filter = rrstore_data_valid.get_resource_record(); + assert_eq!(resource_record_after_filter, resource_record_valid_a); + } + } } From 19c2314c05e1207357a2d1f4b2ffbc4152f59252 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Mon, 8 Jan 2024 15:25:55 -0300 Subject: [PATCH 124/137] fix test timeout cache in Dns_cache, now pass, buts has a ambiguous FIX --- src/dns_cache.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/dns_cache.rs b/src/dns_cache.rs index 3c9951bd..68af4aa2 100644 --- a/src/dns_cache.rs +++ b/src/dns_cache.rs @@ -502,7 +502,7 @@ mod dns_cache_test { } #[test] - fn timeout_cache_1_domain_same_type(){ + fn timeout_cache_1_domain_same_rtype(){ use std::{thread, time}; let mut dns_cache = DnsCache::new(); @@ -514,7 +514,7 @@ mod dns_cache_test { let a_rdata = Rdata::A(ARdata::new()); let mut resource_record = ResourceRecord::new(a_rdata.clone()); - resource_record.set_ttl(4); + resource_record.set_ttl(1000); dns_cache.add(domain_name.clone(), resource_record.clone()); @@ -540,14 +540,18 @@ mod dns_cache_test { println!("After timeout: {:?}", Utc::now()); dns_cache.timeout_cache(); - //FIXME: the size shoud be 1 because we have only 1 resocurce_record associated with 1 domain + //FIXME: the size shoud be 1 because we have only 1 resocurce_record associated with 1 domain? // assert_eq!(dns_cache.get_size(), 1); - //check iof the resource_record_2 was deleted + //check if the resource_record_2 was deleted if let Some(cache_by_domain_name) = dns_cache.get_cache().get(Rtype::A) { if let Some(rrstore_data_vec) = cache_by_domain_name.get(&domain_name) { - //FIXME: gives 0 instead of 1, meaning all the rrstored records were deleted, when only 1 shoud have been deleted assert_eq!(rrstore_data_vec.len(), 1); + //check if the resource_record_1 survive + if let Some(rrstore_after_cleaning) = rrstore_data_vec.get(0) { + let resource_record_after_cleaning = rrstore_after_cleaning.get_resource_record(); + assert_eq!(resource_record_after_cleaning, resource_record); + } } } From a957a8423ee0079e2a421fe6572897d3b2b88574 Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Tue, 9 Jan 2024 14:50:47 -0300 Subject: [PATCH 125/137] Fix: little typos in the tests --- src/dns_cache.rs | 43 ++++++++++++++++++- src/dns_cache/cache_by_record_type.rs | 7 +-- .../cache_by_domain_name.rs | 4 +- 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/dns_cache.rs b/src/dns_cache.rs index 68af4aa2..47ec3681 100644 --- a/src/dns_cache.rs +++ b/src/dns_cache.rs @@ -220,7 +220,7 @@ impl DnsCache { #[cfg(test)] mod dns_cache_test { use chrono::Utc; - use crate::dns_cache::DnsCache; + use crate::{dns_cache::DnsCache, message::rdata::ns_rdata::NsRdata}; use crate::dns_cache::cache_by_record_type::CacheByRecordType; use crate::dns_cache::cache_by_record_type::cache_by_domain_name::CacheByDomainName; use crate::dns_cache::cache_by_record_type::rr_stored_data::RRStoredData; @@ -557,5 +557,46 @@ mod dns_cache_test { } + #[test] + fn timeout_cache_1_domain_differents_rtype(){ + use std::{thread, time}; + let mut dns_cache = DnsCache::new(); + + dns_cache.set_max_size(3); + + let mut domain_name = DomainName::new(); + domain_name.set_name(String::from("uchile.cl")); + + let a_rdata = Rdata::A(ARdata::new()); + let ns_rdata = Rdata::NS(NsRdata::new()); + + let mut resource_record_a = ResourceRecord::new(a_rdata.clone()); + resource_record_a.set_ttl(1000); + + dns_cache.add(domain_name.clone(), resource_record_a.clone()); + + assert_eq!(dns_cache.get_cache().get_cache_data().len(), 1); + + let mut resource_record_ns = ResourceRecord::new(ns_rdata.clone()); + resource_record_ns.set_ttl(4); + + dns_cache.add(domain_name.clone(), resource_record_ns.clone()); + + //because rtypes are differents the size of the cache is 2? + assert_eq!(dns_cache.get_cache().get_cache_data().len(), 2); + + println!("Before timeout: {:?}", Utc::now()); + thread::sleep(time::Duration::from_secs(5)); + println!("After timeout: {:?}", Utc::now()); + dns_cache.timeout_cache(); + + //FIXME: the size shoud be 1 because we have only 1 resocurce_record associated with 1 domain? + // assert_eq!(dns_cache.get_size(), 1); + + //After the cleaning, the size of the cache shoud be 1 (NS Was deleted) + println!("the Rtype cache is {:?} : \n", dns_cache.get_cache().get_cache_data()); + //FIXME: Domain uchile points to a empty array and (NS, CacheByDomainName) still exists + assert_eq!(dns_cache.get_cache().get_cache_data().len(),1); + } } diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index b9459863..095aeb92 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -874,8 +874,9 @@ mod cache_data_test{ assert_eq!(rrstore_data_vec_a.len(), 1); } } - - if let Some(record_types_data_ns) = record_types_data.get(&Rtype::NS) { + + //FIXME: + if let Some(record_types_data_ns) = record_types_data_after_clean.get(&Rtype::NS) { println!(" el CacheByDOmain de NS es {:?}", record_types_data_ns); assert!(false, "Si habia algo dentro del Rtype NS y NO debía ser así"); } else { @@ -949,7 +950,7 @@ mod cache_data_test{ //this test is going to prove if the cleaning after the timeout is acting correctly one layer down (CacheByDomain) // ------BEFORE THE 5 SECONDS----- // RTYPE:A -> {uchile (invalid) -> [..], example.com (valid) -> [..]} - // RTYPE:NS -> {uchile (valid) -> [..], example.com (invalid) -> [...]} + // RTYPE:NS -> {example (valid) -> [..], example.com (invalid) -> [...]} //-------AFTER THE 5 SECONDS----- // RTYPE:A -> {example.com -> [...]} // RTYPE:NS -> {uchile.com -> [...]} diff --git a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs index 7a3a4708..5d3be596 100644 --- a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs +++ b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs @@ -569,12 +569,12 @@ mod host_data_test{ //clean the data with expired ttl cache_by_domain_name.filter_timeout_host_data(); - // println!("The new cache is {:?} ", cache_by_domain_name.get_domain_names_data()); + println!("The new cache is {:?} ", cache_by_domain_name.get_domain_names_data()); //check if the value who survives is the same if let Some(rr_cache_vec) = cache_by_domain_name.get_domain_names_data().get(&domain_name_1) { if let Some(rrstore_data) = rr_cache_vec.get(0) { - println!("the rrstore for domain {:?} afther de timeout is {:?} ", domain_name_1.get_name(), rrstore_data); + // println!("the rrstore for domain {:?} afther de timeout is {:?} ", domain_name_1.get_name(), rrstore_data); assert_eq!(rrstore_data_valid, rrstore_data.clone()); } } From d76327c7161f3c4727cf01fa93edbb0aa27c3d7b Mon Sep 17 00:00:00 2001 From: NegroDCC Date: Tue, 9 Jan 2024 15:24:51 -0300 Subject: [PATCH 126/137] add test two layers down in DNS_Cache for timeouts, (equivalent of test in cache by record type) --- src/dns_cache.rs | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/src/dns_cache.rs b/src/dns_cache.rs index 47ec3681..cb372081 100644 --- a/src/dns_cache.rs +++ b/src/dns_cache.rs @@ -599,4 +599,102 @@ mod dns_cache_test { assert_eq!(dns_cache.get_cache().get_cache_data().len(),1); } + + + #[test] + //this test is going to prove if the cleaning after the timeout is acting correctly two layer down (CacheByDomain) + // ------BEFORE THE 5 SECONDS----- + // RTYPE:A -> {uchile (invalid) -> [..], example.com (valid) -> [..]} + // RTYPE:NS -> {example (valid) -> [..], example.com (invalid) -> [...]} + //-------AFTER THE 5 SECONDS----- + // RTYPE:A -> {example.com -> [...]} + // RTYPE:NS -> {uchile.com -> [...]} + fn filter_timout_cache_data_cleaning_two_layer_down(){ + use std::{thread, time}; + let mut dns_cache = DnsCache::new(); + + dns_cache.set_max_size(5); + //Defaults Rdatas to use + let a_rdata = Rdata::A(ARdata::new()); + let ns_rdata = Rdata::NS(NsRdata::new()); + + + let mut domain_name_1 = DomainName::new(); + domain_name_1.set_name(String::from("example.com")); + + let mut domain_name_2 = DomainName::new(); + domain_name_2.set_name(String::from("uchile.cl")); + + //adding in A rtypes + let mut resource_record_valid_a = ResourceRecord::new(a_rdata.clone()); + resource_record_valid_a.set_ttl(1000); + dns_cache.add(domain_name_1.clone(), resource_record_valid_a.clone()); + + let mut resource_record_invalid_a = ResourceRecord::new(a_rdata.clone()); + resource_record_invalid_a.set_ttl(4); + dns_cache.add(domain_name_2.clone(), resource_record_invalid_a.clone()); + + //adding in NS rtypes + let mut resource_record_valid_ns = ResourceRecord::new(ns_rdata.clone()); + resource_record_valid_ns.set_ttl(1000); + dns_cache.add(domain_name_2.clone(), resource_record_valid_ns.clone()); + + let mut resource_record_invalid_ns = ResourceRecord::new(ns_rdata.clone()); + resource_record_invalid_ns.set_ttl(4); + dns_cache.add(domain_name_1.clone(), resource_record_invalid_ns.clone()); + + + //check if every record_types_data (HashMap for A and for NS) has 2 element + let record_types_data = dns_cache.get_cache().get_cache_data(); + //CacheByDomainName for A type + if let Some(record_types_data_a) = record_types_data.get(&Rtype::A) { + // println!("the cache by domain for A type is : \n {:?}",record_types_data_a.get_domain_names_data()); + assert_eq!(record_types_data_a.get_domain_names_data().len(), 2); + } + //CacheByDomainName for NS type + if let Some(record_types_data_ns) = record_types_data.get(&Rtype::NS) { + // println!("the cache by domain for NS type is : \n {:?}",record_types_data_ns.get_domain_names_data()); + assert_eq!(record_types_data_ns.get_domain_names_data().len(), 2); + } + + //check the size of the dns_cache is correctly + assert_eq!(dns_cache.get_size(), 4); + + println!("Before timeout: {:?}", Utc::now()); + thread::sleep(time::Duration::from_secs(5)); + println!("After timeout: {:?}", Utc::now()); + dns_cache.timeout_cache(); + + let record_types_data_after_cleaning = dns_cache.get_cache().get_cache_data(); + + //after the cleaning, each cache shoud have 1 element + if let Some(record_types_data_a) = record_types_data_after_cleaning.get(&Rtype::A) { + println!("the cache by domain for A type after the cleaning is : \n {:?}",record_types_data_a.get_domain_names_data()); + //FIXME: Does not delete the invadil rrstore, instead points to a empty array (same error as in cache by domain) + assert_eq!(record_types_data_a.get_domain_names_data().len(), 1); + //check if is the same resource record valid (which survives) + if let Some(rrstore_a_after_cleaning) = record_types_data_a.clone().get_from_host_data(domain_name_1.clone()){ + if let Some(rrstore_data_valid) = rrstore_a_after_cleaning.get(0){ + let resource_record_after_filter = rrstore_data_valid.get_resource_record(); + assert_eq!(resource_record_after_filter, resource_record_valid_a); + } + } + } + //CacheByDomainName for NS type + if let Some(record_types_data_ns) = record_types_data_after_cleaning.get(&Rtype::NS) { + println!("the cache by domain for NS type after the cleaning is : \n {:?}",record_types_data_ns.get_domain_names_data()); + //FIXME: Does not delete the invadil rrstore, instead points to a empty array (same error as in cache by domain) + assert_eq!(record_types_data_ns.get_domain_names_data().len(), 1); + //check if is the same resource record valid (which survives) + if let Some(rrstore_ns_after_cleaning) = record_types_data_ns.clone().get_from_host_data(domain_name_2.clone()){ + if let Some(rrstore_data_valid) = rrstore_ns_after_cleaning.get(0){ + let resource_record_after_filter = rrstore_data_valid.get_resource_record(); + assert_eq!(resource_record_after_filter, resource_record_valid_a); + } + } + } + + + } + } From f360783f589e5b294d57928c807f65cd1904002c Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Tue, 9 Jan 2024 19:39:53 -0300 Subject: [PATCH 127/137] fix: empty vec as answer is an error --- src/async_resolver.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/async_resolver.rs b/src/async_resolver.rs index 441de036..23f06491 100644 --- a/src/async_resolver.rs +++ b/src/async_resolver.rs @@ -134,6 +134,9 @@ impl AsyncResolver { let rcode = header.get_rcode(); if rcode == 0 { let answer = dns_mgs.get_answer(); + if answer.len() == 0 { + Err(ClientError::TemporaryError("no answer found"))?; + } return Ok(answer); } match rcode { From 734a0956170014b8ece19b43f857b35e3636fd5d Mon Sep 17 00:00:00 2001 From: Ephy Date: Tue, 9 Jan 2024 23:48:22 -0300 Subject: [PATCH 128/137] test: update integration_test.rs --- tests/integration_test.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/tests/integration_test.rs b/tests/integration_test.rs index fad5dd26..774928a3 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -32,9 +32,7 @@ async fn query_a_type() { } else { panic!("No ip address"); } - } else { - panic!("No response") - } + } } /// 6.2.2 Query normal Qtype = * @@ -66,9 +64,6 @@ async fn query_mx_type() { } else { panic!("Record is not MX type"); } - - } else { - panic!("No response received") } } @@ -95,9 +90,6 @@ async fn query_ns_type() { } else { panic!("Second record is not NS"); } - - } else { - panic!("No response received") } } @@ -112,6 +104,7 @@ async fn mistyped_host_name() { #[tokio::test] async fn no_resource_available() { let response = query_response("example.com", "UDP", "CNAME").await; + println!("{:?}", response); assert!(response.is_err()); } From 52a27c5526575605547676e2400c41a1acbc89cf Mon Sep 17 00:00:00 2001 From: Ephy Date: Wed, 10 Jan 2024 01:06:26 -0300 Subject: [PATCH 129/137] test: update integration_test.rs --- tests/integration_test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 774928a3..1f587b56 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -37,7 +37,7 @@ async fn query_a_type() { /// 6.2.2 Query normal Qtype = * #[tokio::test] -async fn query_all_type() { +async fn query_any_type() { let udp_response = query_response("example.com", "UDP", "ANY").await; let tcp_response = query_response("example.com", "TCP", "ANY").await; assert!(udp_response.is_err()); From f2a24ac92bf1376eba1d962de41796890650161e Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Wed, 10 Jan 2024 18:42:15 -0300 Subject: [PATCH 130/137] refact: filter_timeout by domain name --- src/dns_cache/cache_by_record_type.rs | 2 +- .../cache_by_record_type/cache_by_domain_name.rs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index 095aeb92..af9540bb 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -168,7 +168,7 @@ impl CacheByRecordType{ let clean_cache_data: HashMap = cache_data .into_iter() .filter_map(|(rtype, mut host_data)| { - host_data.filter_timeout_host_data(); + host_data.filter_timeout_by_domain_name(); if host_data.get_domain_names_data().is_empty() { None } else { diff --git a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs index 5d3be596..206b236c 100644 --- a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs +++ b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs @@ -217,8 +217,8 @@ impl CacheByDomainName { } /// For each domain name, it removes the RRStoredData past its TTL. - pub fn filter_timeout_host_data(&mut self) { - let mut new_hash = HashMap::>::new(); + pub fn filter_timeout_by_domain_name(&mut self) { + let mut new_hash: HashMap> = HashMap::>::new(); let data = self.get_domain_names_data(); let current_time = Utc::now(); for (domain_name, rr_cache_vec) in data.into_iter() { @@ -518,7 +518,7 @@ mod host_data_test{ thread::sleep(time::Duration::from_secs(5)); println!("After timeout: {:?}", Utc::now()); //clean the data with expired ttl - cache_by_domain_name.filter_timeout_host_data(); + cache_by_domain_name.filter_timeout_by_domain_name(); assert_eq!(cache_by_domain_name.get_domain_names_data().len(), 1); if let Some(rr_cache_vec) = cache_by_domain_name.get_domain_names_data().get(&domain_name) { @@ -533,7 +533,7 @@ mod host_data_test{ #[test] fn timeout_rr_cache_two_domain(){ - //this test prove the for iteration in filter_timeout_host_data + //this test prove the for iteration in filter_timeout_by_domain_name use std::{thread, time}; let mut cache_by_domain_name = CacheByDomainName::new(); let a_rdata = Rdata::A(ARdata::new()); @@ -567,7 +567,7 @@ mod host_data_test{ thread::sleep(time::Duration::from_secs(5)); println!("After timeout: {:?}", Utc::now()); //clean the data with expired ttl - cache_by_domain_name.filter_timeout_host_data(); + cache_by_domain_name.filter_timeout_by_domain_name(); println!("The new cache is {:?} ", cache_by_domain_name.get_domain_names_data()); From 7220311e9b87a844820f874cf78806009369cca6 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Wed, 10 Jan 2024 18:43:40 -0300 Subject: [PATCH 131/137] fix remove key in filter timeout --- src/dns_cache/cache_by_record_type/cache_by_domain_name.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs index 206b236c..d0f4e32d 100644 --- a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs +++ b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs @@ -227,7 +227,10 @@ impl CacheByDomainName { .filter(|rr_cache| rr_cache.get_absolute_ttl() > current_time) .collect(); - new_hash.insert(domain_name, filtered_rr_cache_vec); + if !filtered_rr_cache_vec.is_empty() { + new_hash.insert(domain_name, filtered_rr_cache_vec); + } + } self.set_domain_names_data(new_hash); } From 936904890feaac7a26e4da9cdcae4597267485bf Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Wed, 10 Jan 2024 18:46:13 -0300 Subject: [PATCH 132/137] refact filter_timeout_by_rtype name --- src/dns_cache.rs | 2 +- src/dns_cache/cache_by_record_type.rs | 52 +++++++++++++-------------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/dns_cache.rs b/src/dns_cache.rs index cb372081..cbe9950e 100644 --- a/src/dns_cache.rs +++ b/src/dns_cache.rs @@ -176,7 +176,7 @@ impl DnsCache { /// If it has expired, it removes it from the cache. pub fn timeout_cache(&mut self) { let mut cache = self.get_cache(); - cache.filter_timeout_cache_data(); + cache.filter_timeout_by_rtype(); self.set_cache(cache); } } diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index af9540bb..9139f450 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -163,16 +163,16 @@ impl CacheByRecordType{ /// For each type of cache data, it removes the cache data that has expired, using /// the `timeout_rr_cache` method of the `CacheByDomainName` struct. If the `CacheByDomainName` struct /// is empty after the removal, it is removed from the cache data. - pub fn filter_timeout_cache_data(&mut self) { + pub fn filter_timeout_by_rtype(&mut self) { let cache_data = self.get_cache_data(); let clean_cache_data: HashMap = cache_data .into_iter() - .filter_map(|(rtype, mut host_data)| { - host_data.filter_timeout_by_domain_name(); - if host_data.get_domain_names_data().is_empty() { + .filter_map(|(rtype, mut data_by_domain)| { + data_by_domain.filter_timeout_by_domain_name(); + if data_by_domain.get_domain_names_data().is_empty() { None } else { - Some((rtype, host_data)) + Some((rtype, data_by_domain)) } }) .collect(); @@ -447,7 +447,7 @@ mod cache_data_test{ } #[test] - fn filter_timeout_cache_data_rtype_a() { + fn filter_timeout_by_rtype_rtype_a() { use std::{thread, time}; let mut cache_record_type = CacheByRecordType::new(); let a_rdata = Rdata::A(ARdata::new()); @@ -474,7 +474,7 @@ mod cache_data_test{ println!("Before timeout: {:?}", Utc::now()); thread::sleep(time::Duration::from_secs(5)); println!("After timeout: {:?}", Utc::now()); - cache_record_type.filter_timeout_cache_data(); + cache_record_type.filter_timeout_by_rtype(); //check if the len is 1 instead of 2 (one RRStoredData was eliminated) if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::A){ @@ -489,7 +489,7 @@ mod cache_data_test{ } #[test] - fn filter_timeout_cache_data_rtype_ns() { + fn filter_timeout_by_rtype_rtype_ns() { use std::{thread, time}; let mut cache_record_type = CacheByRecordType::new(); let ns_rdata = Rdata::NS(NsRdata::new()); @@ -516,7 +516,7 @@ mod cache_data_test{ println!("Before timeout: {:?}", Utc::now()); thread::sleep(time::Duration::from_secs(5)); println!("After timeout: {:?}", Utc::now()); - cache_record_type.filter_timeout_cache_data(); + cache_record_type.filter_timeout_by_rtype(); //check if the len is 1 instead of 2 (one RRStoredData was eliminated) if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::NS){ @@ -531,7 +531,7 @@ mod cache_data_test{ } #[test] - fn filter_timeout_cache_data_rtype_cname() { + fn filter_timeout_by_rtype_rtype_cname() { use std::{thread, time}; let mut cache_record_type = CacheByRecordType::new(); let cname_rdata = Rdata::CNAME(CnameRdata::new()); @@ -558,7 +558,7 @@ mod cache_data_test{ println!("Before timeout: {:?}", Utc::now()); thread::sleep(time::Duration::from_secs(5)); println!("After timeout: {:?}", Utc::now()); - cache_record_type.filter_timeout_cache_data(); + cache_record_type.filter_timeout_by_rtype(); //check if the len is 1 instead of 2 (one RRStoredData was eliminated) if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::CNAME){ @@ -573,7 +573,7 @@ mod cache_data_test{ } #[test] - fn filter_timeout_cache_data_rtype_soa() { + fn filter_timeout_by_rtype_rtype_soa() { use std::{thread, time}; let mut cache_record_type = CacheByRecordType::new(); let soa_rdata = Rdata::SOA(SoaRdata::new()); @@ -600,7 +600,7 @@ mod cache_data_test{ println!("Before timeout: {:?}", Utc::now()); thread::sleep(time::Duration::from_secs(5)); println!("After timeout: {:?}", Utc::now()); - cache_record_type.filter_timeout_cache_data(); + cache_record_type.filter_timeout_by_rtype(); //check if the len is 1 instead of 2 (one RRStoredData was eliminated) if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::SOA){ @@ -615,7 +615,7 @@ mod cache_data_test{ } #[test] - fn filter_timeout_cache_data_rtype_ptr() { + fn filter_timeout_by_rtype_rtype_ptr() { use std::{thread, time}; let mut cache_record_type = CacheByRecordType::new(); let ptr_rdata = Rdata::PTR(PtrRdata::new()); @@ -642,7 +642,7 @@ mod cache_data_test{ println!("Before timeout: {:?}", Utc::now()); thread::sleep(time::Duration::from_secs(5)); println!("After timeout: {:?}", Utc::now()); - cache_record_type.filter_timeout_cache_data(); + cache_record_type.filter_timeout_by_rtype(); //check if the len is 1 instead of 2 (one RRStoredData was eliminated) if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::PTR){ @@ -657,7 +657,7 @@ mod cache_data_test{ } #[test] - fn filter_timeout_cache_data_rtype_mx() { + fn filter_timeout_by_rtype_rtype_mx() { use std::{thread, time}; let mut cache_record_type = CacheByRecordType::new(); let mx_rdata = Rdata::MX(MxRdata::new()); @@ -684,7 +684,7 @@ mod cache_data_test{ println!("Before timeout: {:?}", Utc::now()); thread::sleep(time::Duration::from_secs(5)); println!("After timeout: {:?}", Utc::now()); - cache_record_type.filter_timeout_cache_data(); + cache_record_type.filter_timeout_by_rtype(); //check if the len is 1 instead of 2 (one RRStoredData was eliminated) if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::MX){ @@ -699,7 +699,7 @@ mod cache_data_test{ } #[test] - fn filter_timeout_cache_data_rtype_txt() { + fn filter_timeout_by_rtype_rtype_txt() { use std::{thread, time}; let mut cache_record_type = CacheByRecordType::new(); let txt_rdata = Rdata::TXT(TxtRdata::new(vec![String::from("test")])); @@ -726,7 +726,7 @@ mod cache_data_test{ println!("Before timeout: {:?}", Utc::now()); thread::sleep(time::Duration::from_secs(5)); println!("After timeout: {:?}", Utc::now()); - cache_record_type.filter_timeout_cache_data(); + cache_record_type.filter_timeout_by_rtype(); //check if the len is 1 instead of 2 (one RRStoredData was eliminated) if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::TXT){ @@ -741,7 +741,7 @@ mod cache_data_test{ } #[test] - fn filter_timeout_cache_data_rtype_hinfo() { + fn filter_timeout_by_rtype_rtype_hinfo() { use std::{thread, time}; let mut cache_record_type = CacheByRecordType::new(); let hinfo_rdata = Rdata::HINFO(HinfoRdata::new()); @@ -768,7 +768,7 @@ mod cache_data_test{ println!("Before timeout: {:?}", Utc::now()); thread::sleep(time::Duration::from_secs(5)); println!("After timeout: {:?}", Utc::now()); - cache_record_type.filter_timeout_cache_data(); + cache_record_type.filter_timeout_by_rtype(); //check if the len is 1 instead of 2 (one RRStoredData was eliminated) if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::HINFO){ @@ -784,7 +784,7 @@ mod cache_data_test{ #[test] - fn filter_timeout_cache_data_rtype_tsig() { + fn filter_timeout_by_rtype_rtype_tsig() { use std::{thread, time}; let mut cache_record_type = CacheByRecordType::new(); let tsig_rdata = Rdata::TSIG(TSigRdata::new()); @@ -811,7 +811,7 @@ mod cache_data_test{ println!("Before timeout: {:?}", Utc::now()); thread::sleep(time::Duration::from_secs(5)); println!("After timeout: {:?}", Utc::now()); - cache_record_type.filter_timeout_cache_data(); + cache_record_type.filter_timeout_by_rtype(); //check if the len is 1 instead of 2 (one RRStoredData was eliminated) if let Some(rr_cache_vec) = cache_record_type.get_from_cache_data(domain_name.clone(), Rtype::TSIG){ @@ -864,7 +864,7 @@ mod cache_data_test{ println!("Before timeout: {:?}", Utc::now()); thread::sleep(time::Duration::from_secs(5)); println!("After timeout: {:?}", Utc::now()); - cache_record_type.filter_timeout_cache_data(); + cache_record_type.filter_timeout_by_rtype(); let record_types_data_after_clean = cache_record_type.get_cache_data(); @@ -927,7 +927,7 @@ mod cache_data_test{ println!("Before timeout: {:?}", Utc::now()); thread::sleep(time::Duration::from_secs(5)); println!("After timeout: {:?}", Utc::now()); - cache_record_type.filter_timeout_cache_data(); + cache_record_type.filter_timeout_by_rtype(); let record_types_data_after_cleaning = cache_record_type.get_cache_data(); @@ -1007,7 +1007,7 @@ mod cache_data_test{ println!("Before timeout: {:?}", Utc::now()); thread::sleep(time::Duration::from_secs(5)); println!("After timeout: {:?}", Utc::now()); - cache_record_type.filter_timeout_cache_data(); + cache_record_type.filter_timeout_by_rtype(); let record_types_data_after_cleaning = cache_record_type.get_cache_data(); From 8fe7158a44f593351792de4e52137e83889bf784 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Wed, 10 Jan 2024 18:47:12 -0300 Subject: [PATCH 133/137] fix filter timeout test --- src/dns_cache/cache_by_record_type.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index 9139f450..270d23cd 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -938,7 +938,7 @@ mod cache_data_test{ } } - if let Some(record_types_data_ns) = record_types_data.get(&Rtype::NS) { + if let Some(record_types_data_ns) = record_types_data_after_cleaning.get(&Rtype::NS) { println!(" el CacheByDomain de NS es : \n {:?}", record_types_data_ns); assert!(false, "Si habia algo dentro del Rtype NS y NO debía ser así"); } else { From e65683e546276c80abe9eaadfef5c42252872696 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Wed, 10 Jan 2024 18:51:44 -0300 Subject: [PATCH 134/137] add verification in filter_timout test --- src/dns_cache/cache_by_record_type.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index 270d23cd..0bd25700 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -923,6 +923,7 @@ mod cache_data_test{ assert_eq!(rrstore_data_vec_ns.len(), 1); } } + assert_eq!(record_types_data.len(), 2); println!("Before timeout: {:?}", Utc::now()); thread::sleep(time::Duration::from_secs(5)); @@ -930,6 +931,8 @@ mod cache_data_test{ cache_record_type.filter_timeout_by_rtype(); let record_types_data_after_cleaning = cache_record_type.get_cache_data(); + + assert_eq!(record_types_data_after_cleaning.len(), 1); if let Some(record_types_data_a) = record_types_data_after_cleaning.get(&Rtype::A) { if let Some(rrstore_data_vec_a) = record_types_data_a.clone().get_from_host_data(domain_name_1.clone()){ From e5f670a53e0bf08a7f86670bd44ced51e27c081f Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Wed, 10 Jan 2024 19:03:33 -0300 Subject: [PATCH 135/137] fix filter timout chache data layer down test --- src/dns_cache/cache_by_record_type.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/dns_cache/cache_by_record_type.rs b/src/dns_cache/cache_by_record_type.rs index 0bd25700..1c5d0a7a 100644 --- a/src/dns_cache/cache_by_record_type.rs +++ b/src/dns_cache/cache_by_record_type.rs @@ -931,7 +931,7 @@ mod cache_data_test{ cache_record_type.filter_timeout_by_rtype(); let record_types_data_after_cleaning = cache_record_type.get_cache_data(); - + assert_eq!(record_types_data_after_cleaning.len(), 1); if let Some(record_types_data_a) = record_types_data_after_cleaning.get(&Rtype::A) { @@ -1027,16 +1027,20 @@ mod cache_data_test{ } } } + //CacheByDomainName for NS type if let Some(record_types_data_ns) = record_types_data_after_cleaning.get(&Rtype::NS) { println!("the cache by domain for NS type after the cleaning is : \n {:?}",record_types_data_ns.get_domain_names_data()); //FIXME: Does not delete the invadil rrstore, instead points to a empty array (same error as in cache by domain) assert_eq!(record_types_data_ns.get_domain_names_data().len(), 1); //check if is the same resource record valid (which survives) - if let Some(rrstore_ns_after_cleaning) = record_types_data_ns.clone().get_from_host_data(domain_name_2.clone()){ - if let Some(rrstore_data_valid) = rrstore_ns_after_cleaning.get(0){ + if let Some(rrstore_ns_after_cleaning) = + record_types_data_ns + .clone() + .get_from_host_data(domain_name_2.clone()) { + if let Some(rrstore_data_valid) = rrstore_ns_after_cleaning.get(0) { let resource_record_after_filter = rrstore_data_valid.get_resource_record(); - assert_eq!(resource_record_after_filter, resource_record_valid_a); + assert_eq!(resource_record_after_filter, resource_record_valid_ns); } } } From 68e78419b6e0644d00b20a9a8dfd157df66c80db Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Wed, 10 Jan 2024 19:04:53 -0300 Subject: [PATCH 136/137] fix filter timoeut chace data layer down dns cache test --- src/dns_cache.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/dns_cache.rs b/src/dns_cache.rs index cbe9950e..613b6dab 100644 --- a/src/dns_cache.rs +++ b/src/dns_cache.rs @@ -689,12 +689,10 @@ mod dns_cache_test { if let Some(rrstore_ns_after_cleaning) = record_types_data_ns.clone().get_from_host_data(domain_name_2.clone()){ if let Some(rrstore_data_valid) = rrstore_ns_after_cleaning.get(0){ let resource_record_after_filter = rrstore_data_valid.get_resource_record(); - assert_eq!(resource_record_after_filter, resource_record_valid_a); + assert_eq!(resource_record_after_filter, resource_record_valid_ns); } } } - - } } From 38e8c07f68a962ce1339ff75b0616c93301537c0 Mon Sep 17 00:00:00 2001 From: Katia Fredes Date: Wed, 10 Jan 2024 19:14:26 -0300 Subject: [PATCH 137/137] refact filter_timeout_by_domain using filter map --- .../cache_by_domain_name.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs index d0f4e32d..4a675697 100644 --- a/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs +++ b/src/dns_cache/cache_by_record_type/cache_by_domain_name.rs @@ -218,21 +218,24 @@ impl CacheByDomainName { /// For each domain name, it removes the RRStoredData past its TTL. pub fn filter_timeout_by_domain_name(&mut self) { - let mut new_hash: HashMap> = HashMap::>::new(); - let data = self.get_domain_names_data(); let current_time = Utc::now(); - for (domain_name, rr_cache_vec) in data.into_iter() { + let data_by_domain = self.get_domain_names_data(); + let clean_data_by_domain: HashMap> = data_by_domain + .into_iter() + .filter_map(|(domain_name, rr_cache_vec)| { let filtered_rr_cache_vec: Vec = rr_cache_vec .into_iter() .filter(|rr_cache| rr_cache.get_absolute_ttl() > current_time) .collect(); if !filtered_rr_cache_vec.is_empty() { - new_hash.insert(domain_name, filtered_rr_cache_vec); + Some((domain_name, filtered_rr_cache_vec)) + } else { + None } - - } - self.set_domain_names_data(new_hash); + }).collect(); + + self.set_domain_names_data(clean_data_by_domain); } }