-
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 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 <aadilshabier1@gmail.com>
- Loading branch information
1 parent
df86e98
commit f8fcb38
Showing
23 changed files
with
291 additions
and
5 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
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,35 @@ | ||
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: Vec<u8> = vec![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 = &result[0]; | ||
let res_string = res | ||
.iter() | ||
// .map(|x| "0x".to_string() + &x.to_string() + " ") | ||
.fold(String::new(), |acc, num| { | ||
acc + "0x" + &num.to_string() + " " | ||
}) | ||
.trim_end() | ||
.to_string(); | ||
|
||
println!("res: {:#?}", res_string); | ||
|
||
let p = scalpel::Packet::from_bytes(&res, 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,55 @@ | ||
use crate::{errors::Error, Layer, Packet}; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct PacketBuilder { | ||
inner: Packet, | ||
} | ||
|
||
impl PacketBuilder { | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
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(), | ||
)) | ||
} | ||
} | ||
|
||
pub fn stack_bytes(mut self, bytes: Vec<u8>) -> Self { | ||
self.inner.unprocessed = 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 mut bytes = | ||
self.inner.layers[len - 1].stack_and_encode(&self.inner.unprocessed, "raw")?; | ||
results.push(bytes); | ||
|
||
for i in (0..len - 1).rev() { | ||
let next_layer = results.last().unwrap(); | ||
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
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
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
Oops, something went wrong.