Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update high ResponseCode in EDNS Section if required #1265

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/proto/src/op/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ impl Header {
}

/// The low response code (original response codes before EDNS extensions)
///
/// Warning: This method can only used for basic ResponseCodes.
/// Some ResponseCode require the use of the EDNS record to represent the high bits.
pub fn set_response_code(&mut self, response_code: ResponseCode) -> &mut Self {
self.response_code = response_code;
self
Expand Down
28 changes: 22 additions & 6 deletions crates/proto/src/op/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ impl Message {

/// Returns a Message constructed with error details to return to a client
///
/// If the `response_code` requires an EDNS section, this function creates one if not already present.
///
/// # Arguments
///
/// * `id` - message id should match the request message id
Expand Down Expand Up @@ -212,6 +214,9 @@ impl Message {
/// see `Header::set_response_code`
pub fn set_response_code(&mut self, response_code: ResponseCode) -> &mut Self {
self.header.set_response_code(response_code);
if response_code.high() != 0 {
self.edns_mut().set_rcode_high(response_code.high());
}
self
}

Expand Down Expand Up @@ -508,11 +513,7 @@ impl Message {

/// If edns is_none, this will create a new default Edns.
pub fn edns_mut(&mut self) -> &mut Edns {
if self.edns.is_none() {
self.edns = Some(Edns::new());
}

self.edns.as_mut().unwrap()
self.edns.get_or_insert_with(Edns::new)
}

/// # Return value
Expand Down Expand Up @@ -589,7 +590,7 @@ impl Message {
query_count: self.queries.len(),
answer_count: self.answers.len(),
nameserver_count: self.name_servers.len(),
additional_count: self.additionals.len(),
additional_count: self.additionals.len() + if self.edns.is_some() { 1 } else { 0 },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EDNS is converted to an additional record, but on the Rust side not represented as one. So the counts here are off if this is not accounted for.

},
);
self
Expand Down Expand Up @@ -1054,6 +1055,21 @@ fn test_emit_and_read_records() {
test_emit_and_read(message);
}

#[test]
fn test_extended_response_code() {
let mut message = Message::new();
message.set_response_code(ResponseCode::BADCOOKIE);
message.update_counts();
test_emit_and_read(message);
Copy link
Contributor Author

@jonasbb jonasbb Oct 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests currently fail, since the reparsed message does not contain an additional entry whereas the original message does contain one. This is the OPT record for EDNS.

Some feedback why parsing a Message removes the OPT record but correctly extracts the EDNS data would be appreciated.

The additional_count differs:
https://github.com/bluejekyll/trust-dns/runs/1311780951#step:5:439

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds a little confusing, I'm not sure why that would be different. Is it possible there is an ordering issue on calculating the counts? One possibility is that some of the processing logic is dropping EDNS due to mishandling this particular ResponseCode or something?

Copy link
Contributor Author

@jonasbb jonasbb Mar 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the PR again and now found that I probably need to call update_counts before test_emit_and_read.

}

#[test]
fn test_extended_response_code_error() {
let mut message = Message::error_msg(0, OpCode::Query, ResponseCode::BADVERS);
message.update_counts();
test_emit_and_read(message);
}

#[cfg(test)]
fn test_emit_and_read(message: Message) {
let mut byte_vec: Vec<u8> = Vec::with_capacity(512);
Expand Down
31 changes: 30 additions & 1 deletion crates/server/src/authority/message_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ impl<'q> MessageResponseBuilder<'q> {

/// Constructs a new error MessageResponse with associated settings
///
/// If the `response_code` requires an EDNS section, this function creates one if not already present.
///
/// # Arguments
///
/// * `id` - request id to which this is a response
Expand All @@ -226,6 +228,11 @@ impl<'q> MessageResponseBuilder<'q> {
> {
let mut header = Header::response_from_request(request_header);
header.set_response_code(response_code);
let mut edns = self.edns;
if response_code.high() != 0 {
edns.get_or_insert_with(Edns::new)
.set_rcode_high(response_code.high());
}

MessageResponse {
header,
Expand All @@ -235,7 +242,7 @@ impl<'q> MessageResponseBuilder<'q> {
soa: Box::new(None.into_iter()),
additionals: Box::new(None.into_iter()),
sig0: self.sig0.unwrap_or_default(),
edns: self.edns,
edns,
}
}
}
Expand Down Expand Up @@ -322,4 +329,26 @@ mod tests {
assert_eq!(response.answer_count(), 0);
assert!(response.name_server_count() > 1);
}

#[test]
fn test_extended_response_codes() {
let mut buf = Vec::with_capacity(512);
{
let mut encoder = BinEncoder::new(&mut buf);
encoder.set_max_size(512);

let message = MessageResponseBuilder {
query: None,
sig0: None,
edns: None,
};
message
.error_msg(&Header::new(), ResponseCode::BADCOOKIE)
.destructive_emit(&mut encoder)
.expect("failed to encode");
}

let response = Message::from_vec(&buf).expect("failed to decode");
assert_eq!(response.response_code(), ResponseCode::BADCOOKIE);
}
}