Skip to content

Commit

Permalink
Merge pull request #4 from tsnoam/32bit_vlanid
Browse files Browse the repository at this point in the history
support vlan ids larger than 16-bit
  • Loading branch information
dovreshef authored Mar 19, 2024
2 parents bd69795 + 59a4278 commit 981b85f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 42 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[package]
name = "min_shark"
version = "0.4.0"
version = "0.5.0"
edition = "2021"
authors = ["Dov Reshef <[email protected]>"]
license = "MIT OR Apache-2.0"
description = "A crate to parse and match a subset of the wireshark filter display language"
readme = "README.md"
keywords = ["tshark", "wireshark", "filter"]
categories = ["matching", "parsing"]
categories = ["parsing", "command-line-interface", "network-programming"]
documentation = "https://docs.rs/min_shark"
repository = "https://github.com/dovreshef/min_shark"
exclude = [
Expand Down
56 changes: 28 additions & 28 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,30 +152,30 @@ impl IpOp {
#[derive(Debug, Clone, PartialEq, Eq, derive_more::Display)]
pub enum ValOp {
#[display(fmt = "{op} {val}")]
Compare { op: CmpOp, val: u16 },
Compare { op: CmpOp, val: u32 },
#[display(fmt = "in {_0:?}")]
MatchAny(Vec<u16>),
MatchAny(Vec<u32>),
#[display(fmt = "not in {_0:?}")]
MatchNone(Vec<u16>),
MatchNone(Vec<u32>),
}

impl ValOp {
#[inline]
pub(crate) fn compare(op: CmpOp, val: u16) -> Self {
pub(crate) fn compare(op: CmpOp, val: u32) -> Self {
Self::Compare { op, val }
}

#[inline]
pub(crate) fn match_any(val: Vec<u16>) -> Self {
pub(crate) fn match_any(val: Vec<u32>) -> Self {
Self::MatchAny(val)
}

#[inline]
pub(crate) fn match_none(val: Vec<u16>) -> Self {
pub(crate) fn match_none(val: Vec<u32>) -> Self {
Self::MatchNone(val)
}

fn is_match(&self, x: u16) -> bool {
fn is_match(&self, x: u32) -> bool {
match self {
ValOp::Compare { op, val } => match op {
CmpOp::Equal => x == *val,
Expand Down Expand Up @@ -223,16 +223,16 @@ impl PayloadOp {
#[derive(Debug, Clone, PartialEq, Eq, derive_more::Display)]
pub enum PayloadLenOp {
#[display(fmt = "{op} {val}")]
Compare { op: CmpOp, val: u16 },
Compare { op: CmpOp, val: u32 },
}

impl PayloadLenOp {
#[inline]
pub(crate) fn compare(op: CmpOp, val: u16) -> Self {
pub(crate) fn compare(op: CmpOp, val: u32) -> Self {
Self::Compare { op, val }
}

fn is_match(&self, len: u16) -> bool {
fn is_match(&self, len: u32) -> bool {
match self {
PayloadLenOp::Compare { op, val } => match op {
CmpOp::Equal => len == *val,
Expand Down Expand Up @@ -311,20 +311,20 @@ impl Clause {
.unwrap_or_default(),
Clause::PortDst(port_op) => matcher
.dstport
.map(|p| port_op.is_match(p))
.map(|p| port_op.is_match(p.into()))
.unwrap_or_default(),
Clause::PortSrc(port_op) => matcher
.srcport
.map(|p| port_op.is_match(p))
.map(|p| port_op.is_match(p.into()))
.unwrap_or_default(),
Clause::Port(port_op) => {
matcher
.srcport
.map(|p| port_op.is_match(p))
.map(|p| port_op.is_match(p.into()))
.unwrap_or_default()
|| matcher
.dstport
.map(|p| port_op.is_match(p))
.map(|p| port_op.is_match(p.into()))
.unwrap_or_default()
}
Clause::EthDst(eth_op) => matcher
Expand Down Expand Up @@ -369,7 +369,7 @@ impl Clause {
.unwrap_or_default(),
Clause::PayloadLen(plen_op) => matcher
.payload
.map(|p| plen_op.is_match(p.len() as u16))
.map(|p| plen_op.is_match(p.len() as u32))
.unwrap_or_default(),
}
}
Expand Down Expand Up @@ -513,7 +513,7 @@ pub struct Matcher<'e, 'p> {
dst_ip: Option<IpNet>,
srcport: Option<u16>,
dstport: Option<u16>,
vlan: Option<u16>,
vlan: Option<u32>,
payload: Option<&'p [u8]>,
}

Expand All @@ -531,9 +531,9 @@ impl<'e, 'p> Matcher<'e, 'p> {
}

/// Whether the packet has vlan data
pub fn vlan(mut self, val: u16) -> Self {
pub fn vlan(mut self, val: impl Into<u32>) -> Self {
self.is_vlan = Some(true);
self.vlan = Some(val);
self.vlan = Some(val.into());
self
}

Expand Down Expand Up @@ -832,18 +832,18 @@ mod tests {
fn test_single_clause_port_expressions() {
init_test_logging();

let srcport = 26112;
let dstport = 80;
let srcport: u16 = 26112;
let dstport: u16 = 80;
let clauses = [
Clause::Port(ValOp::compare(CmpOp::Equal, srcport)),
Clause::Port(ValOp::compare(CmpOp::Equal, dstport)),
Clause::PortSrc(ValOp::compare(CmpOp::Equal, srcport)),
Clause::PortDst(ValOp::compare(CmpOp::Equal, dstport)),
Clause::Port(ValOp::match_any(vec![srcport])),
Clause::Port(ValOp::match_any(vec![dstport])),
Clause::Port(ValOp::compare(CmpOp::Equal, srcport.into())),
Clause::Port(ValOp::compare(CmpOp::Equal, dstport.into())),
Clause::PortSrc(ValOp::compare(CmpOp::Equal, srcport.into())),
Clause::PortDst(ValOp::compare(CmpOp::Equal, dstport.into())),
Clause::Port(ValOp::match_any(vec![srcport.into()])),
Clause::Port(ValOp::match_any(vec![dstport.into()])),
Clause::Port(ValOp::match_none(vec![1, 2, 3])),
Clause::PortSrc(ValOp::match_any(vec![srcport])),
Clause::PortDst(ValOp::match_any(vec![dstport])),
Clause::PortSrc(ValOp::match_any(vec![srcport.into()])),
Clause::PortDst(ValOp::match_any(vec![dstport.into()])),
Clause::PortDst(ValOp::match_none(vec![1, 2, 3])),
];
for clause in clauses.into_iter() {
Expand Down
10 changes: 5 additions & 5 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
parse_ip_net,
parse_mac_addr,
parse_regex,
parse_u16,
parse_u32,
},
Expression,
};
Expand Down Expand Up @@ -266,7 +266,7 @@ impl<'a> Parser<'a> {
fn parse_value_operations(&mut self) -> Result<ValOp, ErrorKind> {
let val_op = match self.parse_comparison_operator() {
Ok(cmp_op) => {
let num = self.parse_value(TokenKind::Value, &parse_u16, "number")?;
let num = self.parse_value(TokenKind::Value, &parse_u32, "number")?;
ValOp::compare(cmp_op, num)
}
_ => match self.current.kind {
Expand All @@ -280,13 +280,13 @@ impl<'a> Parser<'a> {
}
self.advance();
let values =
self.parse_list(TokenKind::Value, &parse_u16, "list of numbers")?;
self.parse_list(TokenKind::Value, &parse_u32, "list of numbers")?;
ValOp::match_none(values)
}
TokenKind::In => {
self.advance();
let values =
self.parse_list(TokenKind::Value, &parse_u16, "list of numbers")?;
self.parse_list(TokenKind::Value, &parse_u32, "list of numbers")?;
ValOp::match_any(values)
}
_ => {
Expand Down Expand Up @@ -335,7 +335,7 @@ impl<'a> Parser<'a> {
fn parse_payload_len_operations(&mut self) -> Result<PayloadLenOp, ErrorKind> {
let val_op = match self.parse_comparison_operator() {
Ok(cmp_op) => {
let num = self.parse_value(TokenKind::Value, &parse_u16, "number")?;
let num = self.parse_value(TokenKind::Value, &parse_u32, "number")?;
PayloadLenOp::compare(cmp_op, num)
}
_ => {
Expand Down
14 changes: 7 additions & 7 deletions src/value_parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use regex::bytes::Regex;
use std::net::IpAddr;

/// Parse a given string as a u16
pub fn parse_u16(val: &BStr) -> Result<u16, &'static str> {
pub(crate) fn parse_u32(val: &BStr) -> Result<u32, &'static str> {
let val = std::str::from_utf8(val).map_err(|_| "not a valid utf-8 string")?;
val.parse().map_err(|_| "not a valid number")
}
Expand Down Expand Up @@ -203,7 +203,7 @@ mod tests {
parse_ip_net,
parse_mac_addr,
parse_regex,
parse_u16,
parse_u32,
};
use crate::{
mac_addr::MacAddr,
Expand All @@ -213,16 +213,16 @@ mod tests {
use tracing::info;

#[test]
fn test_parse_u16() {
fn test_parse_u32() {
init_test_logging();

for val in [u16::MIN, u16::MAX] {
for val in [u32::MIN, u32::MAX] {
info!("Parsing \"{val}\" as u16 - should succeed");
let num = parse_u16(val.to_string().as_str().into()).unwrap();
let num = parse_u32(val.to_string().as_str().into()).unwrap();
assert_eq!(val, num);
}

let num = (u16::MAX as u32 + 1).to_string();
let num = (u32::MAX as u64 + 1).to_string();
for val in [
"25d".as_bytes(),
"-1".as_bytes(),
Expand All @@ -232,7 +232,7 @@ mod tests {
] {
let val = val.into();
info!("Parsing \"{val}\" as u16 - should fail");
let res = parse_u16(val);
let res = parse_u32(val);
assert!(res.is_err());
}
}
Expand Down

0 comments on commit 981b85f

Please sign in to comment.