1 unstable release

Uses old Rust 2015

0.6.1 Mar 4, 2019

#11 in #wireshark

Download history 28/week @ 2024-07-22 18/week @ 2024-07-29 20/week @ 2024-08-05 18/week @ 2024-08-12 37/week @ 2024-08-19 8/week @ 2024-08-26 20/week @ 2024-09-16 24/week @ 2024-09-23 18/week @ 2024-09-30 22/week @ 2024-10-07 47/week @ 2024-10-14 15/week @ 2024-10-21 30/week @ 2024-10-28 27/week @ 2024-11-04

120 downloads per month

MIT license

100KB
2.5K SLoC

Wirefilter

Build status Crates.io License

This is an execution engine for Wireshark®-like filters.

It contains public APIs for parsing filter syntax, compiling them into an executable IR and, finally, executing filters against provided values.

Example

use wirefilter::{ExecutionContext, Scheme, Type};

fn main() -> Result<(), failure::Error> {
    // Create a map of possible filter fields.
    let scheme = Scheme! {
        http.method: Bytes,
        http.ua: Bytes,
        port: Int,
    };

    // Parse a Wireshark-like expression into an AST.
    let ast = scheme.parse(r#"
        http.method != "POST" &&
        not http.ua matches "(googlebot|facebook)" &&
        port in {80 443}
    "#)?;

    println!("Parsed filter representation: {:?}", ast);

    // Compile the AST into an executable filter.
    let filter = ast.compile();

    // Set runtime field values to test the filter against.
    let mut ctx = ExecutionContext::new(&scheme);

    ctx.set_field_value("http.method", "GET")?;

    ctx.set_field_value(
        "http.ua",
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0",
    )?;

    ctx.set_field_value("port", 443)?;

    // Execute the filter with given runtime values.
    println!("Filter matches: {:?}", filter.execute(&ctx)?); // true

    // Amend one of the runtime values and execute the filter again.
    ctx.set_field_value("port", 8080)?;

    println!("Filter matches: {:?}", filter.execute(&ctx)?); // false

    Ok(())
}

Licensing

Licensed under the MIT license. See the LICENSE file for details.

Dependencies

~3.5–5.5MB
~99K SLoC