-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make PacketBuilder to sculpt Packets and Layer::stack_and_encode
- Implement `PacketBuilder` to sculpt packets. - Implement `Layer::stack_and_encode` for Ethernet and IPv4. - Implement `as_slice` and `as_mut_slice` for MACAddress, IPv4Address and IPv6Address. - Create example `packet_sculpting.rs`. - Add new `crate::Error` variant SculptingError. Signed-off-by: Mohammad Aadil Shabier <[email protected]>
- Loading branch information
1 parent
df86e98
commit 593fb6f
Showing
10 changed files
with
258 additions
and
1 deletion.
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
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,33 @@ | ||
use std::error::Error; | ||
|
||
use scalpel::{layers, ENCAP_TYPE_ETH}; | ||
|
||
fn main() -> Result<(), Box<dyn Error>> { | ||
let _ = scalpel::register_defaults()?; | ||
|
||
let eth_layer = Box::new(layers::ethernet::Ethernet::new()); | ||
let ipv4_layer = Box::new(layers::ipv4::IPv4::new()); | ||
let bytes = [0x12, 0x34, 0x56, 0x78, 0x90]; | ||
|
||
let builder = scalpel::builder::PacketBuilder::new() | ||
.stack(eth_layer)? | ||
.stack(ipv4_layer)? | ||
.stack_bytes(&bytes); | ||
|
||
let (_, result) = builder.build().unwrap(); | ||
|
||
let res_string = result[0] | ||
.iter() | ||
.fold(String::new(), |acc, num| { | ||
acc + "0x" + &num.to_string() + " " | ||
}) | ||
.trim_end() | ||
.to_string(); | ||
|
||
println!("res: {:#?}", res_string); | ||
|
||
let p = scalpel::Packet::from_bytes(&result[0], ENCAP_TYPE_ETH); | ||
println!("{}", serde_json::to_string_pretty(&p.unwrap()).unwrap()); | ||
|
||
Ok(()) | ||
} |
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,65 @@ | ||
use crate::{errors::Error, Layer, Packet}; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct PacketBuilder { | ||
inner: Packet, | ||
} | ||
|
||
impl PacketBuilder { | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
/// Stacks a layer on top of the Packet. The return value on success is the | ||
/// `PacketBuilder` on success. Failure happens when a layer is attempted to | ||
/// be pushed after pushing bytes. In this case it returns a | ||
/// [SculptingError][`crate::errors::Error::SculptingError`]. | ||
pub fn stack(mut self, layer: Box<dyn Layer + Send>) -> Result<Self, Error> { | ||
if self.inner.unprocessed.is_empty() { | ||
self.inner.layers.push(layer); | ||
Ok(self) | ||
} else { | ||
Err(Error::SculptingError( | ||
"Cannot push layer on top of raw byte layer".to_string(), | ||
)) | ||
} | ||
} | ||
|
||
/// Stacks bytes on top of the packet. If the layer already contains some bytes | ||
/// the new bytes are concatenated to the previous bytes in the packet. | ||
pub fn stack_bytes(mut self, bytes: &[u8]) -> Self { | ||
self.inner.unprocessed.extend(bytes); | ||
self | ||
} | ||
|
||
pub fn build(mut self) -> Result<(Packet, Vec<Vec<u8>>), Error> { | ||
let len = self.inner.layers.len(); | ||
if len < 1 { | ||
return Err(Error::SculptingError( | ||
"Packet to build does not contain any layers".to_string(), | ||
)); | ||
} | ||
let mut results = vec![]; | ||
|
||
// last layer | ||
let next_layer = if self.inner.unprocessed.is_empty() { | ||
None | ||
} else { | ||
Some(self.inner.unprocessed.as_slice()) | ||
}; | ||
let mut bytes = self.inner.layers[len - 1].stack_and_encode(next_layer, "raw")?; | ||
results.push(bytes); | ||
|
||
for i in (0..len - 1).rev() { | ||
let next_layer = results.last().map(|layer| layer.as_slice()); | ||
let info = self.inner.layers[i + 1].name(); | ||
bytes = self.inner.layers[i].stack_and_encode(next_layer, info)?; | ||
results.push(bytes); | ||
} | ||
|
||
results.reverse(); | ||
Ok((self.inner, results)) | ||
} | ||
|
||
// TODO: Add metadata related functions | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,8 @@ pub mod layer; | |
|
||
pub mod types; | ||
|
||
pub mod builder; | ||
|
||
#[doc(inline)] | ||
pub use layers::register_defaults; | ||
|
||
|
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