Skip to content

Commit

Permalink
pass RequestInfo into Authority on search
Browse files Browse the repository at this point in the history
  • Loading branch information
bluejekyll committed Jan 30, 2022
1 parent d7aa80f commit 9e8ea5c
Show file tree
Hide file tree
Showing 13 changed files with 457 additions and 116 deletions.
18 changes: 9 additions & 9 deletions crates/proto/src/op/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ impl fmt::Display for Flags {

impl Default for Header {
fn default() -> Self {
Self::new()
}
}

impl Header {
// TODO: we should make id, message_type and op_code all required and non-editable
/// A default Header, not very useful.
pub const fn new() -> Self {
Header {
id: 0,
message_type: MessageType::Query,
Expand All @@ -142,21 +150,13 @@ impl Default for Header {
recursion_available: false,
authentic_data: false,
checking_disabled: false,
response_code: ResponseCode::default(),
response_code: ResponseCode::NoError,
query_count: 0,
answer_count: 0,
name_server_count: 0,
additional_count: 0,
}
}
}

impl Header {
// TODO: we should make id, message_type and op_code all required and non-editable
/// A default Header, not very useful.
pub fn new() -> Self {
Default::default()
}

/// Construct a new header based off the request header. This copies over the RD (recursion-desired)
/// and CD (checking-disabled), as well as the op_code and id of the request.
Expand Down
8 changes: 3 additions & 5 deletions crates/server/src/authority/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ use crate::client::{
};
use crate::{
authority::{LookupError, MessageRequest, UpdateResult, ZoneType},
client::{
op::LowerQuery,
rr::{LowerName, RecordSet, RecordType},
},
client::rr::{LowerName, RecordSet, RecordType},
proto::rr::RrsetRecords,
server::RequestInfo,
};

/// LookupOptions that specify different options from the client to include or exclude various records in the response.
Expand Down Expand Up @@ -148,7 +146,7 @@ pub trait Authority: Send + Sync {
/// `is_secure` is true, in the case of no records found then NSEC records will be returned.
async fn search(
&self,
query: &LowerQuery,
request: RequestInfo<'_>,
lookup_options: LookupOptions,
) -> Result<Self::Lookup, LookupError>;

Expand Down
14 changes: 6 additions & 8 deletions crates/server/src/authority/authority_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ use log::debug;

use crate::{
authority::{Authority, LookupError, LookupOptions, MessageRequest, UpdateResult, ZoneType},
client::{
op::LowerQuery,
rr::{LowerName, Record, RecordType},
},
client::rr::{LowerName, Record, RecordType},
server::RequestInfo,
};

/// An Object safe Authority
Expand Down Expand Up @@ -71,7 +69,7 @@ pub trait AuthorityObject: Send + Sync {
/// `is_secure` is true, in the case of no records found then NSEC records will be returned.
async fn search(
&self,
query: &LowerQuery,
request_info: RequestInfo<'_>,
lookup_options: LookupOptions,
) -> Result<Box<dyn LookupObject>, LookupError>;

Expand Down Expand Up @@ -185,12 +183,12 @@ where
/// `is_secure` is true, in the case of no records found then NSEC records will be returned.
async fn search(
&self,
query: &LowerQuery,
request_info: RequestInfo<'_>,
lookup_options: LookupOptions,
) -> Result<Box<dyn LookupObject>, LookupError> {
let this = self.as_ref();
debug!("performing {} on {}", query, this.origin());
let lookup = Authority::search(&*this, query, lookup_options).await;
debug!("performing {} on {}", request_info.query, this.origin());
let lookup = Authority::search(&*this, request_info, lookup_options).await;
lookup.map(|l| Box::new(l) as Box<dyn LookupObject>)
}

Expand Down
4 changes: 3 additions & 1 deletion crates/server/src/authority/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ async fn lookup<'a, R: ResponseHandler + Unpin>(

let (response_header, sections) = build_response(
&*authority,
request_info,
request.id(),
request.header(),
query,
Expand Down Expand Up @@ -466,6 +467,7 @@ fn lookup_options_for_edns(edns: Option<&Edns>) -> LookupOptions {

async fn build_response(
authority: &dyn AuthorityObject,
request_info: RequestInfo<'_>,
request_id: u16,
request_header: &Header,
query: &LowerQuery,
Expand All @@ -485,7 +487,7 @@ async fn build_response(
response_header.set_authoritative(authority.zone_type().is_authoritative());

debug!("performing {} on {}", query, authority.origin());
let future = authority.search(query, lookup_options);
let future = authority.search(request_info, lookup_options);

#[allow(deprecated)]
let sections = match authority.zone_type() {
Expand Down
25 changes: 25 additions & 0 deletions crates/server/src/server/request_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl std::ops::Deref for Request {
}
}

// TODO: add ProtocolInfo that would have TLS details or other additional things...
/// A narrow view of the Request, specifically a verified single query for the request
#[non_exhaustive]
pub struct RequestInfo<'a> {
Expand All @@ -81,6 +82,30 @@ pub struct RequestInfo<'a> {
pub query: &'a LowerQuery,
}

impl<'a> RequestInfo<'a> {
/// Construct a new RequestInfo
///
/// # Arguments
///
/// * `src` - The source address from which the request came
/// * `protocol` - The protocol used for the request
/// * `header` - The header from the original request
/// * `query` - The query from the request, LowerQuery is intended to reduce complexity for lookups in authorities
pub fn new(
src: SocketAddr,
protocol: Protocol,
header: &'a Header,
query: &'a LowerQuery,
) -> Self {
Self {
src,
protocol,
header,
query,
}
}
}

/// Information about the response sent for a request
#[derive(Clone, Copy)]
#[repr(transparent)]
Expand Down
6 changes: 3 additions & 3 deletions crates/server/src/store/file/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ use crate::{
use crate::{
authority::{Authority, LookupError, LookupOptions, MessageRequest, UpdateResult, ZoneType},
client::{
op::LowerQuery,
rr::{LowerName, Name, RecordSet, RecordType, RrKey},
serialize::txt::{Lexer, Parser, Token},
},
server::RequestInfo,
store::{file::FileConfig, in_memory::InMemoryAuthority},
};

Expand Down Expand Up @@ -295,10 +295,10 @@ impl Authority for FileAuthority {
/// `is_secure` is true, in the case of no records found then NSEC records will be returned.
async fn search(
&self,
query: &LowerQuery,
request_info: RequestInfo<'_>,
lookup_options: LookupOptions,
) -> Result<Self::Lookup, LookupError> {
self.0.search(query, lookup_options).await
self.0.search(request_info, lookup_options).await
}

/// Get the NS, NameServer, record for the zone
Expand Down
13 changes: 9 additions & 4 deletions crates/server/src/store/forwarder/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ use crate::{
Authority, LookupError, LookupObject, LookupOptions, MessageRequest, UpdateResult, ZoneType,
},
client::{
op::{LowerQuery, ResponseCode},
op::ResponseCode,
rr::{LowerName, Name, Record, RecordType},
},
resolver::{
config::ResolverConfig, lookup::Lookup as ResolverLookup, TokioAsyncResolver, TokioHandle,
},
server::RequestInfo,
store::forwarder::ForwardConfig,
};

Expand Down Expand Up @@ -116,11 +117,15 @@ impl Authority for ForwardAuthority {

async fn search(
&self,
query: &LowerQuery,
request_info: RequestInfo<'_>,
lookup_options: LookupOptions,
) -> Result<Self::Lookup, LookupError> {
self.lookup(query.name(), query.query_type(), lookup_options)
.await
self.lookup(
request_info.query.name(),
request_info.query.query_type(),
lookup_options,
)
.await
}

async fn get_nsec_records(
Expand Down
11 changes: 6 additions & 5 deletions crates/server/src/store/in_memory/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ use crate::{
MessageRequest, UpdateResult, ZoneType,
},
client::{
op::{LowerQuery, ResponseCode},
op::ResponseCode,
rr::{
rdata::SOA,
{DNSClass, LowerName, Name, RData, Record, RecordSet, RecordType, RrKey},
},
},
server::RequestInfo,
};

/// InMemoryAuthority is responsible for storing the resource records for a particular zone.
Expand Down Expand Up @@ -1141,13 +1142,13 @@ impl Authority for InMemoryAuthority {

async fn search(
&self,
query: &LowerQuery,
request_info: RequestInfo<'_>,
lookup_options: LookupOptions,
) -> Result<Self::Lookup, LookupError> {
debug!("searching InMemoryAuthority for: {}", query);
debug!("searching InMemoryAuthority for: {}", request_info.query);

let lookup_name = query.name();
let record_type: RecordType = query.query_type();
let lookup_name = request_info.query.name();
let record_type: RecordType = request_info.query.query_type();

// if this is an AXFR zone transfer, verify that this is either the Secondary or Primary
// for AXFR the first and last record must be the SOA
Expand Down
10 changes: 4 additions & 6 deletions crates/server/src/store/sqlite/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ use log::{error, info, warn};

use crate::{
authority::{Authority, LookupError, LookupOptions, MessageRequest, UpdateResult, ZoneType},
client::{
op::LowerQuery,
rr::{LowerName, RrKey},
},
client::rr::{LowerName, RrKey},
error::{PersistenceErrorKind, PersistenceResult},
proto::{
op::ResponseCode,
rr::{DNSClass, Name, RData, Record, RecordSet, RecordType},
},
server::RequestInfo,
store::{
in_memory::InMemoryAuthority,
sqlite::{Journal, SqliteConfig},
Expand Down Expand Up @@ -968,10 +966,10 @@ impl Authority for SqliteAuthority {

async fn search(
&self,
query: &LowerQuery,
request_info: RequestInfo<'_>,
lookup_options: LookupOptions,
) -> Result<Self::Lookup, LookupError> {
self.in_memory.search(query, lookup_options).await
self.in_memory.search(request_info, lookup_options).await
}

/// Return the NSEC records based on the given name
Expand Down
Loading

0 comments on commit 9e8ea5c

Please sign in to comment.