Skip to content

Commit

Permalink
Updated for nom v4
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrik Jansson committed Feb 27, 2019
1 parent 012771f commit cb4c666
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 173 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pcapng"
version = "0.0.4"
version = "0.0.5"
authors = ["Richo Healey <richo@psych0tik.net>"]

repository = "https://github.com/richo/pcapng-rs"
Expand All @@ -9,4 +9,4 @@ license = "MIT"
documentation = "http://richo.psych0tik.net/pcapng-rs/pcapng/"

[dependencies]
nom = "^3.0"
nom = "^4.0"
29 changes: 0 additions & 29 deletions examples/producer_errors.rs

This file was deleted.

11 changes: 5 additions & 6 deletions examples/read_pcapng.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#[macro_use]
extern crate nom;
extern crate pcapng;

use std::env;
use std::fs;
use std::io::Read;
use nom::IResult;
use pcapng::block::parse_blocks;

fn main() {
Expand All @@ -20,14 +18,15 @@ fn main() {
let _ = fh.read_to_end(&mut buf);

match parse_blocks(buf.as_slice()) {
IResult::Done(_, blocks) => {
Ok((_, blocks)) => {
for i in blocks {
if let IResult::Done(_, blk) = i.parse() {
if let Ok((_, blk)) = i.parse() {
println!("{:?}", blk);
}
}
}
IResult::Error(e) => panic!("Error: {:?}", e),
IResult::Incomplete(i) => panic!("Incomplete: {:?}", i),
Err(nom::Err::Error(e)) => panic!("Error: {:?}", e),
Err(nom::Err::Incomplete(i)) => panic!("Incomplete: {:?}", i),
Err(nom::Err::Failure(f)) => panic!("Failure: {:?}", f),
}
}
101 changes: 53 additions & 48 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,50 +34,46 @@ pub struct RawBlock<'a> {
}

impl<'a> RawBlock<'a> {
pub fn parse(self) -> IResult<&'a [u8], Block<'a>> {
pub fn parse(self) -> IResult<&'a [u8], Block<'a> > {
match self.ty {
blocks::section_header::TY => {
match blocks::section_header::parse(self) {
IResult::Done(left, blk) => IResult::Done(left, Block::SectionHeader(blk)),
IResult::Error(e) => IResult::Error(e),
IResult::Incomplete(e) => IResult::Incomplete(e),
Ok((left, blk)) => Ok((left, Block::SectionHeader(blk))),
Err(e) => Err(e),
}
}

blocks::enhanced_packet::TY => {
match blocks::enhanced_packet::parse(self) {
IResult::Done(left, blk) => IResult::Done(left, Block::EnhancedPacket(blk)),
IResult::Error(e) => IResult::Error(e),
IResult::Incomplete(e) => IResult::Incomplete(e),
Ok((left, blk)) => Ok((left, Block::EnhancedPacket(blk))),
Err(e) => Err(e),
}
}

blocks::interface_stats::TY => {
match blocks::interface_stats::parse(self) {
IResult::Done(left, blk) => {
IResult::Done(left, Block::InterfaceStatistics(blk))
}
IResult::Error(e) => IResult::Error(e),
IResult::Incomplete(e) => IResult::Incomplete(e),
Ok((left, blk)) => {
Ok((left, Block::InterfaceStatistics(blk)))
},
Err(e) => Err(e),
}
}

blocks::interface_description::TY => {
match blocks::interface_description::parse(self) {
IResult::Done(left, blk) => {
IResult::Done(left, Block::InterfaceDescription(blk))
}
IResult::Error(e) => IResult::Error(e),
IResult::Incomplete(e) => IResult::Incomplete(e),
Ok((left, blk)) => {
Ok((left, Block::InterfaceDescription(blk)))
},
Err(e) => Err(e),
}
}
_ => IResult::Done(&self.body[0..0], Block::UnknownBlock(self)),
_ => Ok((&[], Block::UnknownBlock(self))),
}
}
}


named!(pub parse_block< &[u8],RawBlock >,
named!(pub parse_block<RawBlock >,
do_parse!(
ty: le_u32
>> block_length: le_u32
Expand All @@ -94,16 +90,20 @@ named!(pub parse_block< &[u8],RawBlock >,
)
);

named!(pub parse_blocks< &[u8],Vec<RawBlock> >,
many1!(parse_block)
named!(pub parse_blocks<Vec<RawBlock> >,
many1!(complete!(parse_block))
);


#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_block() {
let input = b"\n\r\r\n\x1c\x00\x00\x00M<+\x1a\x01\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\x1c\x00\x00\x00";
match parse_block(input) {
IResult::Done(left, RawBlock { ty, block_length, body, check_length }) => {
Ok((left, RawBlock { ty, block_length, body, check_length })) => {
// Ignored because we do not currently parse the whole block
assert_eq!(left, b"");
assert_eq!(ty, 0x0A0D0D0A);
Expand All @@ -122,7 +122,7 @@ fn test_parse_blocks() {
let input = b"\n\r\r\n\x1c\x00\x00\x00M<+\x1a\x01\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\x1c\x00\x00\x00\
\n\r\r\n\x1c\x00\x00\x00M<+\x1a\x01\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\x1c\x00\x00\x00";
match parse_blocks(input) {
IResult::Done(left, blocks) => {
Ok((left, blocks)) => {
assert_eq!(blocks.len(), 2);
for i in blocks {
let RawBlock { ty, block_length, body, check_length } = i;
Expand All @@ -134,7 +134,8 @@ fn test_parse_blocks() {
assert_eq!(check_length, 28);
}
}
_ => {
err => {
println!("error: {:?}", err);
assert_eq!(1, 2);
}
}
Expand All @@ -144,7 +145,7 @@ fn test_parse_blocks() {
fn test_parse_weird_length_block() {
let input = b"\n\r\r\n\x1b\x00\x00\x00<+\x1a\x01\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\x00\x1b\x00\x00\x00";
match parse_block(input) {
IResult::Done(left, RawBlock { ty, block_length, body, check_length }) => {
Ok((left, RawBlock { ty, block_length, body, check_length })) => {
// Ignored because we do not currently parse the whole block
assert_eq!(left, b"");
assert_eq!(ty, 0x0A0D0D0A);
Expand All @@ -166,35 +167,39 @@ fn test_multiple_options() {
\x6f\x77\x73\x20\x58\x50\x00\x00\x04\x00\x0c\x00\x54\x65\x73\x74\
\x30\x30\x34\x2e\x65\x78\x65\x00\x00\x00\x00\x00\x40\x00\x00\x00";
match parse_block(input) {
IResult::Done(left, block) => {
Ok((left, block)) => {
assert_eq!(left, b"");
if let IResult::Done(_, Block::SectionHeader(blk)) = block.parse() {
if let Some(opts) = blk.options {
assert_eq!(opts.options.len(), 3);

let o = &opts.options[0];
assert_eq!(o.code, 0x03);
assert_eq!(o.length, 0x0b);
assert_eq!(&o.value[..], b"Windows XP\x00");

let o = &opts.options[1];
assert_eq!(o.code, 0x04);
assert_eq!(o.length, 0x0c);
assert_eq!(&o.value[..], b"Test004.exe\x00");

let o = &opts.options[2];
assert_eq!(o.code, 0x00);
assert_eq!(o.length, 0x00);
assert_eq!(&o.value[..], b"");
} else {
unreachable!();
match block.parse() {
Ok((_, Block::SectionHeader(blk))) => {
if let Some(opts) = blk.options {
assert_eq!(opts.options.len(), 3);

let o = &opts.options[0];
assert_eq!(o.code, 0x03);
assert_eq!(o.length, 0x0b);
assert_eq!(&o.value[..], b"Windows XP\x00");

let o = &opts.options[1];
assert_eq!(o.code, 0x04);
assert_eq!(o.length, 0x0c);
assert_eq!(&o.value[..], b"Test004.exe\x00");

let o = &opts.options[2];
assert_eq!(o.code, 0x00);
assert_eq!(o.length, 0x00);
assert_eq!(&o.value[..], b"");
} else {
unreachable!();
}
} ,
err =>{
panic!("error: {:?}", err);
}
} else {
unreachable!();
}
}
_ => {
panic!("Hit a codepath we shouldn't have");
}
}
}
}
13 changes: 6 additions & 7 deletions src/blocks/enhanced_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub const TY: u32 = 0x00000006;
// | Block Total Length |
// +---------------------------------------------------------------+

named!(enhanced_packet_body<&[u8],EnhancedPacket>,
named!(enhanced_packet_body<EnhancedPacket>,
do_parse!(
interface_id: le_u32
>> timestamp_hi: le_u32
Expand All @@ -50,7 +50,7 @@ named!(enhanced_packet_body<&[u8],EnhancedPacket>,
// Field to a 32-bit boundary
>> data: take!(captured_len as usize)
>> take!(util::pad_to_32bits(captured_len as usize))
>> options: opt!(complete!(parse_options))
>> options: opt!(parse_options)


>> (
Expand All @@ -73,13 +73,12 @@ named!(enhanced_packet_body<&[u8],EnhancedPacket>,
pub fn parse(blk: RawBlock) -> IResult<&[u8], EnhancedPacket> {
match enhanced_packet_body(blk.body) {
// FIXME(richo) actually do something with the leftover bytes
IResult::Done(left, mut block) => {
Ok((left, mut block)) => {
block.block_length = blk.block_length;
block.check_length = blk.check_length;
IResult::Done(left, block)
}
IResult::Error(e) => IResult::Error(e),
IResult::Incomplete(e) => IResult::Incomplete(e),
Ok((left, block))
},
other => other
}
}

Expand Down
Loading

0 comments on commit cb4c666

Please sign in to comment.