1 unstable release

Uses old Rust 2015

0.6.1 Mar 4, 2019

#2361 in Parser implementations

Download history 2/week @ 2023-11-20 12/week @ 2023-11-27 7/week @ 2023-12-04 1/week @ 2023-12-18 4/week @ 2023-12-25 1/week @ 2024-01-01 23/week @ 2024-01-15 5/week @ 2024-02-05 22/week @ 2024-02-12 29/week @ 2024-02-19 28/week @ 2024-02-26 26/week @ 2024-03-04

106 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

~4–5.5MB
~103K SLoC