Skip to content

Commit

Permalink
Merge pull request #16 from niclabs/dnssec-develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
joalopez1206 authored Jul 14, 2024
2 parents 7512265 + 55809e5 commit ac12650
Show file tree
Hide file tree
Showing 44 changed files with 3,284 additions and 2,289 deletions.
546 changes: 229 additions & 317 deletions src/async_resolver.rs

Large diffs are not rendered by default.

252 changes: 228 additions & 24 deletions src/async_resolver/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
use crate::client::{udp_connection::ClientUDPConnection, tcp_connection::ClientTCPConnection,client_connection::ClientConnection };
use crate::client::client_connection::ConnectionProtocol;
use std::{net::{IpAddr,SocketAddr,Ipv4Addr}, time::Duration, vec};
use crate::message::DnsMessage;
use std::cmp::max;
use std::option;
use std::{net::{IpAddr,SocketAddr,Ipv4Addr}, time::Duration};

use super::server_info::ServerInfo;

const GOOGLE_PRIMARY_DNS_SERVER: [u8; 4] = [8, 8, 8, 8];
const GOOGLE_SECONDARY_DNS_SERVER: [u8; 4] = [8, 8, 4, 4];
const CLOUDFLARE_PRIMARY_DNS_SERVER: [u8; 4] = [1, 1, 1, 1];
const CLOUDFLARE_SECONDARY_DNS_SERVER: [u8; 4] = [1, 0, 0, 1];
const OPEN_DNS_PRIMARY_DNS_SERVER: [u8; 4] = [208, 67, 222, 222];
const OPEN_DNS_SECONDARY_DNS_SERVER: [u8; 4] = [208, 67, 220, 220];
const QUAD9_PRIMARY_DNS_SERVER: [u8; 4] = [9, 9, 9, 9];
const QUAD9_SECONDARY_DNS_SERVER: [u8; 4] = [149, 112, 112, 112];

#[derive(Clone, Debug, PartialEq, Eq)]

/// Configuration for the resolver.
Expand All @@ -22,7 +34,7 @@ pub struct ResolverConfig {
///
/// If this number is surpassed, the resolver is expected to panic in
/// a Temporary Error.
retry: u16,
retransmission_loop_attempts: u16,
/// Activation of cache in this resolver.
///
/// This is whether the resolver uses cache or not.
Expand All @@ -40,6 +52,27 @@ pub struct ResolverConfig {
///
/// This corresponds a `Duration` type.
timeout: Duration,
max_retry_interval_seconds: u64,
min_retry_interval_seconds: u64,
// While local limits on the number of times a resolver will retransmit
// a particular query to a particular name server address are
// essential, the resolver should have a global per-request
// counter to limit work on a single request. The counter should
// be set to some initial value and decremented whenever the
// resolver performs any action (retransmission timeout,
// retransmission, etc.) If the counter passes zero, the request
// is terminated with a temporary error.
global_retransmission_limit: u16,
/// This is whether ends0 is enabled or not.
ends0: bool,
/// Max payload for the resolver.
max_payload: u16,
/// Version of endns0.
ends0_version: u16,
/// edns0 flags for the resolver.
ends0_flags: u16,
/// edns0 options for the resolver.
ends0_options: Vec<u16>,
}

impl ResolverConfig {
Expand All @@ -59,35 +92,68 @@ impl ResolverConfig {
/// let resolver_config = ResolverConfig::new(addr, protocol, timeout);
/// assert_eq!(resolver_config.get_addr(), SocketAddr::new(addr, 53));
/// ```
pub fn new(resolver_addr: IpAddr, protocol: ConnectionProtocol, timeout: Duration) -> Self {
pub fn new(
resolver_addr: IpAddr,
protocol: ConnectionProtocol,
timeout: Duration,
) -> Self {
let resolver_config: ResolverConfig = ResolverConfig {
name_servers: Vec::new(),
bind_addr: SocketAddr::new(resolver_addr, 53),
retry: 30,
retransmission_loop_attempts: 3,
cache_enabled: true,
recursive_available: false,
protocol: protocol,
timeout: timeout,
max_retry_interval_seconds: 10,
min_retry_interval_seconds: 1,
global_retransmission_limit: 30,
ends0: false,
max_payload: 512,
ends0_version: 0,
ends0_flags: 0,
ends0_options: Vec::new(),
};
resolver_config
}

pub fn default()-> Self {
// FIXME: these are examples values
let google_server:IpAddr = IpAddr::V4(Ipv4Addr::new(8, 8, 8, 8));
let timeout = Duration::from_secs(10);

let conn_udp:ClientUDPConnection = ClientUDPConnection::new(google_server, timeout);
let conn_tcp:ClientTCPConnection = ClientTCPConnection::new(google_server, timeout);
let retransmission_loop_attempts = 3;
let global_retransmission_limit = 30;
let timeout = Duration::from_secs(45);
let max_retry_interval_seconds = 60;

let mut servers_info = Vec::new();
servers_info.push(ServerInfo::new_from_addr(GOOGLE_PRIMARY_DNS_SERVER.into(), timeout));
servers_info.push(ServerInfo::new_from_addr(CLOUDFLARE_PRIMARY_DNS_SERVER.into(), timeout));
servers_info.push(ServerInfo::new_from_addr(OPEN_DNS_PRIMARY_DNS_SERVER.into(), timeout));
servers_info.push(ServerInfo::new_from_addr(QUAD9_PRIMARY_DNS_SERVER.into(), timeout));
servers_info.push(ServerInfo::new_from_addr(GOOGLE_SECONDARY_DNS_SERVER.into(), timeout));
servers_info.push(ServerInfo::new_from_addr(CLOUDFLARE_SECONDARY_DNS_SERVER.into(), timeout));
servers_info.push(ServerInfo::new_from_addr(OPEN_DNS_SECONDARY_DNS_SERVER.into(), timeout));
servers_info.push(ServerInfo::new_from_addr(QUAD9_SECONDARY_DNS_SERVER.into(), timeout));

// Recommended by RFC 1536: max(4, 5/number_of_server_to_query)
let number_of_server_to_query = servers_info.len() as u64;
let min_retry_interval_seconds: u64 = max(1, 5/number_of_server_to_query).into();

let resolver_config: ResolverConfig = ResolverConfig {
name_servers: vec![ServerInfo::new_with_ip(google_server, conn_udp, conn_tcp)],
name_servers: servers_info,
bind_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 5333),
retry: 30,
retransmission_loop_attempts: retransmission_loop_attempts,
cache_enabled: true,
recursive_available: false,
protocol: ConnectionProtocol::UDP,
timeout: timeout,
max_retry_interval_seconds: max_retry_interval_seconds,
min_retry_interval_seconds: min_retry_interval_seconds,
global_retransmission_limit: global_retransmission_limit,
ends0: false,
max_payload: 512,
ends0_version: 0,
ends0_flags: 0,
ends0_options: Vec::new(),
};
resolver_config
}
Expand Down Expand Up @@ -138,6 +204,47 @@ impl ResolverConfig {
pub fn remove_servers(&mut self) {
self.name_servers = Vec::new();
}

/// add edns0 to the resolver
///
/// # Examples
///
/// ```
/// use std::net::IpAddr;
/// use std::time::Duration;
/// use dns_resolver::client::client_connection::ConnectionProtocol;
/// use dns_resolver::resolver::config::ResolverConfig;
///
/// let mut resolver_config = ResolverConfig::default();
/// resolver_config.add_edns0(Some(1024), 0, 0, Some(vec![12]));
/// ```
pub fn add_edns0(&mut self, max_payload: Option<u16>, version: u16, flags: u16, options: Option<Vec<u16>>) {
self.set_ends0(true);
if let Some(max_payload) = max_payload {
self.set_max_payload(max_payload);
}
self.set_ends0_version(version);
self.set_ends0_flags(flags);
if let Some(options) = options {
self.set_ends0_options(options);
}
}

/// add edns0 from the resolver to a dns message
///
/// # Examples
///
/// ```
/// let mut resolver_config = ResolverConfig::default();
/// resolver_config.add_edns0(Some(1024), 0, 0, Some(vec![12]));
/// let message = Message::new();
/// resolver_config.add_edns0_to_message(&message);
/// ```
pub fn add_edns0_to_message(&self, message: &mut DnsMessage) {
if self.ends0 {
message.add_edns0(Some(self.get_max_payload()), self.get_ends0_version(), self.get_ends0_flags(), Some(self.get_ends0_options()));
}
}
}

///Getters
Expand All @@ -155,8 +262,8 @@ impl ResolverConfig {

/// Returns the quantity of retries before the resolver panic in a
/// Temporary Error.
pub fn get_retry(&self) -> u16 {
self.retry
pub fn get_retransmission_loop_attempts(&self) -> u16 {
self.retransmission_loop_attempts
}

/// Returns whether the cache is enabled or not.
Expand All @@ -178,6 +285,38 @@ impl ResolverConfig {
pub fn get_timeout(&self) -> Duration {
self.timeout
}

pub fn get_max_retry_interval_seconds(&self) -> u64 {
self.max_retry_interval_seconds
}

pub fn get_min_retry_interval_seconds(&self) -> u64 {
self.min_retry_interval_seconds
}

pub fn get_global_retransmission_limit(&self) -> u16 {
self.global_retransmission_limit
}

pub fn get_ends0(&self) -> bool {
self.ends0
}

pub fn get_max_payload(&self) -> u16 {
self.max_payload
}

pub fn get_ends0_version(&self) -> u16 {
self.ends0_version
}

pub fn get_ends0_flags(&self) -> u16 {
self.ends0_flags
}

pub fn get_ends0_options(&self) -> Vec<u16> {
self.ends0_options.clone()
}
}

///Setters
Expand All @@ -195,8 +334,8 @@ impl ResolverConfig{

/// Sets the quantity of retries before the resolver panic in a
/// Temporary Error.
pub fn set_retry(&mut self, retry:u16) {
self.retry = retry;
pub fn set_retransmission_loop_attempts(&mut self, retransmission_loop_attempts:u16) {
self.retransmission_loop_attempts = retransmission_loop_attempts;
}

/// Sets whether the cache is enabled or not.
Expand All @@ -218,6 +357,38 @@ impl ResolverConfig{
pub fn set_timeout(&mut self, timeout: Duration) {
self.timeout = timeout;
}

pub fn set_max_retry_interval_seconds(&mut self, max_retry_interval_seconds: u64) {
self.max_retry_interval_seconds = max_retry_interval_seconds;
}

pub fn set_min_retry_interval_seconds(&mut self, min_retry_interval_seconds: u64) {
self.min_retry_interval_seconds = min_retry_interval_seconds;
}

pub fn set_global_retransmission_limit(&mut self, global_retransmission_limit: u16) {
self.global_retransmission_limit = global_retransmission_limit;
}

pub fn set_ends0(&mut self, ends0: bool) {
self.ends0 = ends0;
}

pub fn set_max_payload(&mut self, max_payload: u16) {
self.max_payload = max_payload;
}

pub fn set_ends0_version(&mut self, ends0_version: u16) {
self.ends0_version = ends0_version;
}

pub fn set_ends0_flags(&mut self, ends0_flags: u16) {
self.ends0_flags = ends0_flags;
}

pub fn set_ends0_options(&mut self, ends0_options: Vec<u16>) {
self.ends0_options = ends0_options;
}
}


Expand Down Expand Up @@ -249,14 +420,14 @@ mod tests_resolver_config {
let mut resolver_config = ResolverConfig::default();
let addr = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1));
resolver_config.add_servers(addr);
assert_eq!(resolver_config.get_name_servers().len(), 2);
assert_eq!(resolver_config.get_name_servers().len(), 9);
}

#[test]
fn get_and_set_name_servers() {
let mut resolver_config = ResolverConfig::default();

assert_eq!(resolver_config.get_name_servers().len(), 1);
assert_eq!(resolver_config.get_name_servers().len(), 8);

let addr_1 = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1));
let tcp_conn_1 = ClientTCPConnection::new(addr_1, Duration::from_secs(TIMEOUT));
Expand Down Expand Up @@ -287,14 +458,14 @@ mod tests_resolver_config {
}

#[test]
fn get_and_set_retry() {
fn get_and_set_retransmission_loop_attempts() {
let mut resolver_config = ResolverConfig::default();

assert_eq!(resolver_config.get_retry(), 30);
assert_eq!(resolver_config.get_retransmission_loop_attempts(), 3);

resolver_config.set_retry(10);
resolver_config.set_retransmission_loop_attempts(10);

assert_eq!(resolver_config.get_retry(), 10);
assert_eq!(resolver_config.get_retransmission_loop_attempts(), 10);
}

#[test]
Expand Down Expand Up @@ -323,7 +494,7 @@ mod tests_resolver_config {
fn get_and_set_timeout() {
let mut resolver_config = ResolverConfig::default();

assert_eq!(resolver_config.get_timeout(), Duration::from_secs(TIMEOUT));
assert_eq!(resolver_config.get_timeout(), Duration::from_secs(45));

resolver_config.set_timeout(Duration::from_secs(10));

Expand All @@ -346,8 +517,41 @@ mod tests_resolver_config {
let mut resolver_config = ResolverConfig::default();
let addr = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1));
resolver_config.add_servers(addr);
assert_eq!(resolver_config.get_name_servers().len(), 2);
assert_eq!(resolver_config.get_name_servers().len(), 9);
resolver_config.remove_servers();
assert_eq!(resolver_config.get_name_servers().len(), 0);
}
}

#[test]
fn get_and_set_max_retry_interval_seconds() {
let mut resolver_config = ResolverConfig::default();

assert_eq!(resolver_config.get_max_retry_interval_seconds(), 60);

resolver_config.set_max_retry_interval_seconds(20);

assert_eq!(resolver_config.get_max_retry_interval_seconds(), 20);
}

#[test]
fn get_and_set_min_retry_interval_seconds() {
let mut resolver_config = ResolverConfig::default();

assert_eq!(resolver_config.get_min_retry_interval_seconds(), 1);

resolver_config.set_min_retry_interval_seconds(2);

assert_eq!(resolver_config.get_min_retry_interval_seconds(), 2);
}

#[test]
fn get_and_set_global_retransmission_limit() {
let mut resolver_config = ResolverConfig::default();

assert_eq!(resolver_config.get_global_retransmission_limit(), 30);

resolver_config.set_global_retransmission_limit(40);

assert_eq!(resolver_config.get_global_retransmission_limit(), 40);
}
}
Loading

0 comments on commit ac12650

Please sign in to comment.