-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modbus: project layout and base traits
Adds basic modules that we will be using to write common traits. Also creates a new workspace for modbus to show how we can layout protocols.
- Loading branch information
1 parent
b4fbbba
commit 021bab4
Showing
8 changed files
with
127 additions
and
2 deletions.
There are no files selected for viewing
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,13 +1,23 @@ | ||
[package] | ||
name = "sawp" | ||
version = "0.1.0" | ||
authors = ["cccs-rtmorti"] | ||
authors = ["cccs-rtmorti", "cccs-sadugas"] | ||
description = "Security Aware Wire Protocol parsing library" | ||
readme = "README.md" | ||
edition = "2018" | ||
license-file = "LICENSE" | ||
|
||
[workspace] | ||
members = [ | ||
"sawp-modbus" | ||
] | ||
|
||
[lib] | ||
crate-type = ["staticlib", "rlib", "cdylib"] | ||
|
||
[dependencies] | ||
[dev-dependencies] | ||
criterion = "0.3" | ||
|
||
[[bench]] | ||
name = "modbus" | ||
harness = false |
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,13 @@ | ||
use criterion::{black_box, criterion_group, criterion_main, Criterion}; | ||
|
||
// TODO: benchmark modbus protocol parsing | ||
fn modbus(n: u64) -> bool { | ||
n == 20 | ||
} | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
c.bench_function("modbus", |b| b.iter(|| modbus(black_box(20)))); | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
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,14 @@ | ||
[package] | ||
name = "sawp-modbus" | ||
version = "0.1.0" | ||
authors = ["cccs-sadugas"] | ||
readme = "../README.md" | ||
edition = "2018" | ||
license-file = "../LICENSE" | ||
|
||
[dependencies] | ||
sawp = { path = ".." } | ||
nom = "5.1.2" | ||
|
||
[dev-dependencies] | ||
rstest = "0.6.4" |
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,41 @@ | ||
use sawp::error::{Error, ErrorKind, Result}; | ||
use sawp::parser::Parse; | ||
use sawp::protocol::Protocol; | ||
|
||
#[derive(Debug)] | ||
pub struct Modbus {} | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct Message {} | ||
|
||
impl Protocol for Modbus { | ||
type Message = Message; | ||
|
||
fn name() -> &'static str { | ||
"modbus" | ||
} | ||
} | ||
|
||
impl Parse for Modbus { | ||
fn parse(_input: &[u8]) -> Result<(usize, Self::Message)> { | ||
Err(Error::new(ErrorKind::Unimplemented)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use rstest::rstest; | ||
use sawp::error::{Error, ErrorKind, Result}; | ||
|
||
#[rstest( | ||
input, | ||
expected, | ||
case(b"", Err(Error::new(ErrorKind::Unimplemented))), | ||
case(b"hello world", Err(Error::new(ErrorKind::Unimplemented))) | ||
)] | ||
#[test] | ||
fn test_modbus(input: &[u8], expected: Result<(usize, <Modbus as Protocol>::Message)>) { | ||
assert_eq!(Modbus::parse(input), expected); | ||
} | ||
} |
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,25 @@ | ||
pub type Result<T> = std::result::Result<T, Error>; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct Error { | ||
kind: ErrorKind, | ||
} | ||
|
||
impl Error { | ||
pub fn new(kind: ErrorKind) -> Self { | ||
Self { kind } | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum ErrorKind { | ||
Unimplemented, | ||
} | ||
|
||
impl std::fmt::Display for Error { | ||
fn fmt(&self, _: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> { | ||
todo!() | ||
} | ||
} | ||
|
||
impl std::error::Error for Error {} |
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,7 @@ | ||
pub mod error; | ||
pub mod parser; | ||
pub mod protocol; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
|
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,10 @@ | ||
use crate::error::Result; | ||
use crate::protocol::Protocol; | ||
|
||
pub trait Parse: Protocol { | ||
/// Parse a chunk of bytes into the protocol's Message type. | ||
/// | ||
/// Returns a tuple with the number of bytes parsed from the input and the | ||
/// parsed message. | ||
fn parse(input: &[u8]) -> Result<(usize, Self::Message)>; | ||
} |
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,8 @@ | ||
/// Represents the basic elements of a protocol | ||
pub trait Protocol { | ||
/// Type of message returned when parsing | ||
type Message; | ||
|
||
/// Protocol name string | ||
fn name() -> &'static str; | ||
} |