diff --git a/Cargo.lock b/Cargo.lock index 54be1ae..fe8790e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -575,6 +575,13 @@ dependencies = [ "winapi", ] +[[package]] +name = "sparse-protocol" +version = "0.1.0" +dependencies = [ + "pcap-sys", +] + [[package]] name = "subtle" version = "2.4.1" diff --git a/Cargo.toml b/Cargo.toml index 4b48f8d..393e133 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,9 @@ [workspace] -members = ["pcap-sys", "nl-sys", "examples/*/*"] +members = [ + "pcap-sys", "nl-sys", + "examples/*/*", + "sparse-protocol" +] [profile.release] strip = true diff --git a/sparse-protocol/Cargo.toml b/sparse-protocol/Cargo.toml new file mode 100644 index 0000000..f78a880 --- /dev/null +++ b/sparse-protocol/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "sparse-protocol" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +pcap-sys = { path = "../pcap-sys" } \ No newline at end of file diff --git a/sparse-protocol/src/lib.rs b/sparse-protocol/src/lib.rs new file mode 100644 index 0000000..77539f7 --- /dev/null +++ b/sparse-protocol/src/lib.rs @@ -0,0 +1,70 @@ +use std::marker::PhantomData; + +/// A composable protocol trait. +/// +/// Can be reused to allow for composition of multiple compatible protocols +pub trait Protocol { + /// Allows for handling incoming packets, and returning something up to the handler of the protocol as well + /// as packets to send out + /// + /// Usually, this will take in a slice of bytes and return a Vec of some enum or another slice of bytes + /// to be consumed by another protocol + fn handle_event(&mut self, packet: I) -> (T, I); + + /// Allows for composing multiple protocols on top of each other, for instance to use + /// TCP fragmenting to work with HTTP to form a complete packet, and then take the HTTP + /// conversation and encode Sparse messages into it + fn compose
(self, other: P) -> ProtocolCompose