-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6e06dd
commit cb23cef
Showing
7 changed files
with
198 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ edition = "2021" | |
|
||
[dependencies] | ||
nom = "7.1.3" | ||
hex = "0.4.3" | ||
hex = "0.4.3" | ||
thiserror = "1.0.56" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod message; | ||
pub mod message_header; | ||
pub mod query; | ||
pub mod resource_record; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
use crate::{message_header::*, query::*, resource_record::*}; | ||
use nom::{multi::count, IResult}; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum ParseError { | ||
#[error("unknown parse error")] | ||
Unknown, | ||
} | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct Message { | ||
pub header: MessageHeader, | ||
pub queries: Vec<Query>, | ||
pub answers: Vec<ResourceRecord>, | ||
} | ||
|
||
impl TryFrom<&[u8]> for Message { | ||
type Error = ParseError; | ||
|
||
fn try_from(input: &[u8]) -> Result<Self, Self::Error> { | ||
// TODO improve error reporting | ||
let (_, m) = message(input).map_err(|_op| ParseError::Unknown)?; | ||
Ok(m) | ||
} | ||
} | ||
|
||
fn message(input: &[u8]) -> IResult<&[u8], Message> { | ||
let (input, header) = message_header(input)?; | ||
let (input, queries) = count(query, header.query_count as usize)(input)?; | ||
let (input, answers) = count(resource_record, header.answer_count as usize)(input)?; | ||
|
||
Ok(( | ||
input, | ||
Message { | ||
header, | ||
queries, | ||
answers, | ||
}, | ||
)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::net::Ipv4Addr; | ||
|
||
#[test] | ||
fn test_message_request() { | ||
let message_bytes = | ||
hex::decode("690601000001000000000000076578616d706c6503636f6d0000010001").unwrap(); | ||
let result = message(&message_bytes); | ||
|
||
assert_eq!( | ||
result, | ||
Ok(( | ||
&b""[..], | ||
Message { | ||
header: MessageHeader { | ||
message_id: 0x6906, | ||
flags: Flags { | ||
qr: QR::Query, | ||
opcode: Opcode::Query, | ||
aa: AuthoritativeAnswer::NonAuthoritative, | ||
truncated: Truncated::NotTruncated, | ||
recursion_desired: RecursionDesired::Desired, | ||
recursion_available: RecursionAvailable::NotAvailable, | ||
rcode: Rcode::NoError, | ||
}, | ||
query_count: 1, | ||
answer_count: 0, | ||
name_server_count: 0, | ||
additional_count: 0, | ||
}, | ||
queries: vec![Query { | ||
name: vec![String::from("example"), String::from("com")], | ||
query_type: QueryType::A, | ||
query_class: QueryClass::Internet, | ||
}], | ||
answers: vec![] | ||
} | ||
)) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_message_response() { | ||
let message_bytes = hex::decode( | ||
"690681800001000100000000076578616d706c6503636f6d0000010001c00c0001000100005a0200045db8d822" | ||
) | ||
.unwrap(); | ||
let result = message(&message_bytes); | ||
|
||
assert_eq!( | ||
result, | ||
Ok(( | ||
&b""[..], | ||
Message { | ||
header: MessageHeader { | ||
message_id: 0x6906, | ||
flags: Flags { | ||
qr: QR::Response, | ||
opcode: Opcode::Query, | ||
aa: AuthoritativeAnswer::NonAuthoritative, | ||
truncated: Truncated::NotTruncated, | ||
recursion_desired: RecursionDesired::Desired, | ||
recursion_available: RecursionAvailable::Available, | ||
rcode: Rcode::NoError, | ||
}, | ||
query_count: 1, | ||
answer_count: 1, | ||
name_server_count: 0, | ||
additional_count: 0, | ||
}, | ||
queries: vec![Query { | ||
name: vec![String::from("example"), String::from("com")], | ||
query_type: QueryType::A, | ||
query_class: QueryClass::Internet, | ||
}], | ||
answers: vec![ResourceRecord { | ||
name: Name::Pointer(49164), | ||
resource_type: ResourceType::A, | ||
resource_class: ResourceClass::Internet, | ||
ttl: 23042, | ||
rdata: ResourceData::A(Ipv4Addr::from(0x5db8d822)) | ||
}] | ||
} | ||
)) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters