Skip to content

Commit

Permalink
'sculpting' as an opt-in feature
Browse files Browse the repository at this point in the history
Packet 'sculpting' is an experimental feature and WIP, it needs more
work before it can be made a default feature.  Making sculpting as an
opt-in feature so that it does not affect the existing users as the
feature is being developed.

Signed-off-by: Abhijit Gadgil <[email protected]>
  • Loading branch information
gabhijit committed May 5, 2024
1 parent 593fb6f commit 2e33d41
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ walkdir = "2"
[features]
python-bindings = ['pyo3']
logging = ["log"]
sculpting = []

wasm = ['wasm-bindgen']
13 changes: 9 additions & 4 deletions examples/packet_sculpting.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#![allow(unused)]
use std::error::Error;

use scalpel::{layers, ENCAP_TYPE_ETH};

#[cfg(feature = "sculpting")]
fn main() -> Result<(), Box<dyn Error>> {
let _ = scalpel::register_defaults()?;

Expand All @@ -18,16 +20,19 @@ fn main() -> Result<(), Box<dyn Error>> {

let res_string = result[0]
.iter()
.fold(String::new(), |acc, num| {
acc + "0x" + &num.to_string() + " "
})
.fold(String::new(), |acc, num| format!("{}{:02x}", acc, num))
.trim_end()
.to_string();

println!("res: {:#?}", res_string);
println!("Packet Data: {:#?}", res_string);

let p = scalpel::Packet::from_bytes(&result[0], ENCAP_TYPE_ETH);
println!("{}", serde_json::to_string_pretty(&p.unwrap()).unwrap());

Ok(())
}

#[cfg(not(feature = "sculpting"))]
fn main() {
eprintln!("Feature 'sculpting' is required for this example!.");
}
2 changes: 2 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "sculpting")]

use crate::{errors::Error, Layer, Packet};

#[derive(Debug, Default)]
Expand Down
1 change: 1 addition & 0 deletions src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub trait Layer: Send + Debug + erased_serde::Serialize {
bytes: &[u8],
) -> Result<(Option<Box<dyn Layer + Send>>, usize), Error>;

#[cfg(feature = "sculpting")]
/// Main 'encoder' function.
///
/// The return value is a `Vec<u8>` on success. This indicates the encoded packet of the
Expand Down
6 changes: 4 additions & 2 deletions src/layers/ethernet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::convert::TryInto;

// FIXME: Should work with no_std
use std::collections::HashMap;
use std::sync::{RwLock,OnceLock};
use std::sync::{OnceLock, RwLock};

use serde::Serialize;

Expand All @@ -19,7 +19,8 @@ pub fn get_ethertypes_map() -> &'static RwLock<HashMap<EtherType, LayerCreatorFn
///
/// The creator function simply creates a `default` L3 struct that implements the dissector
/// for the Layer.
pub(crate) static ETHERTYPES_MAP: OnceLock<RwLock<HashMap<EtherType, LayerCreatorFn>>> = OnceLock::new();
pub(crate) static ETHERTYPES_MAP: OnceLock<RwLock<HashMap<EtherType, LayerCreatorFn>>> =
OnceLock::new();
ETHERTYPES_MAP.get_or_init(|| RwLock::new(HashMap::new()))
}

Expand Down Expand Up @@ -91,6 +92,7 @@ impl Layer for Ethernet {
}
}

#[cfg(feature = "sculpting")]
fn stack_and_encode(
&mut self,
next_layer: Option<&[u8]>,
Expand Down
2 changes: 2 additions & 0 deletions src/layers/ipv4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ impl IPv4 {
Ok(((len as u8, data), i))
}

#[cfg(feature = "sculpting")]
fn calculate_checksum(_bytes: &[u8]) -> u16 {
0
}
Expand Down Expand Up @@ -322,6 +323,7 @@ impl Layer for IPv4 {
"ip"
}

#[cfg(feature = "sculpting")]
fn stack_and_encode(
&mut self,
next_layer: Option<&[u8]>,
Expand Down

0 comments on commit 2e33d41

Please sign in to comment.