Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0" }
erased-serde = "0.4"
log = { version = "0.4", optional = true }
console_error_panic_hook = "0.1.6"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and next three two dependencies should be added only when feature wasm is used.

web-sys = "0.3.62"
console_log = "0.2.2"


[target.'cfg(target_family = "wasm")'.dependencies]
wasm-bindgen = { version = "0.2", optional=true}
Expand Down
19 changes: 15 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,29 @@ fn scalpel(py: Python, m: &PyModule) -> PyResult<()> {
#[cfg(all(not(feature = "python-bindings"), target_family = "wasm"))]
use wasm_bindgen::prelude::*;

#[cfg(all(not(feature = "python-bindings"), target_family = "wasm"))]
#[wasm_bindgen(start)]
pub fn initialize() {
console_error_panic_hook::set_once();
}

#[cfg(all(not(feature = "python-bindings"), target_family = "wasm"))]
#[wasm_bindgen]
pub fn dissect_packet(packet: String) -> String {
pub fn dissect_packet( encap_type: u16 , packet: String) -> Result<String,String> {

let _ = layers::register_defaults();

let packet = hex::decode(packet);
let packet = hex::decode(packet);

let packet = packet.unwrap();

let p = Packet::from_bytes(&packet, ENCAP_TYPE_ETH);
let p = Packet::from_bytes(&packet, encap_type);

let p = p.unwrap();

serde_json::to_string_pretty(&p).unwrap()
let result = serde_json::to_string_pretty(&p).unwrap();

Ok(result)

}

15 changes: 14 additions & 1 deletion tests/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;

use web_sys::console::error_1;
use web_sys::console::log_1;

#[wasm_bindgen_test]
fn simple_dissect_test() {
let bytestream = "003096e6fc3900309605283888470001ddff45c0002800030000ff06a4e80a0102010a2200012af90017983210058dd58ea55010102099cd00000000";
scalpel::dissect_packet(bytestream.to_string());
let encap_type = scalpel::ENCAP_TYPE_ETH;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and other ENCAP_TYPE_* constants should be exported to the JS world. Not sure whether this is possible. I

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's indeed not possible to export constants we can ignore this one. I tried around a bit, but couldn't find an easy way to do it.


match scalpel::dissect_packet(encap_type, bytestream.to_string()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also add another API something like dissect_packet_bin when the data is available as actual binary data. While using this, we realized there's an unnecessary round trip to hex string and back. So see if that can be avoided by defining new API.

Ok(result) => {
log_1(&format!("Dissected packet result: {}", result).into());
}
Err(err) => {
error_1(&format!("Dissect packet failed: {}", err).into());
panic!("Dissect packet failed: {}", err);
}
}
assert!(true);
}
14 changes: 13 additions & 1 deletion tests/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,23 @@
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;

use web_sys::console::error_1;
use web_sys::console::log_1;
wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
fn simple_dissect_test() {
let bytestream = "003096e6fc3900309605283888470001ddff45c0002800030000ff06a4e80a0102010a2200012af90017983210058dd58ea55010102099cd00000000";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible try to add a test case or two for other encap types as well.

scalpel::dissect_packet(bytestream.to_string());
let encap_type = scalpel::ENCAP_TYPE_ETH;

match scalpel::dissect_packet(encap_type, bytestream.to_string()) {
Ok(result) => {
log_1(&format!("Dissected packet result: {}", result).into());
}
Err(err) => {
error_1(&format!("Dissect packet failed: {}", err).into());
panic!("Dissect packet failed: {}", err);
}
}
assert!(true);
}