From 45d69dd97f4208837e994945449d387885157c68 Mon Sep 17 00:00:00 2001 From: gurkenhabicht Date: Thu, 21 May 2020 01:48:13 +0200 Subject: [PATCH] changed first if loop to match statement --- src/parser.rs | 126 ++++++++++++++++++++++++++------------------------ 1 file changed, 65 insertions(+), 61 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index c81cbe0..ad8827e 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -5,9 +5,9 @@ mod packet_handler; use eui48::MacAddress; use pcap::Capture; //use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; -use std::str; -use regex::bytes::Regex; use regex::bytes::Match; +use regex::bytes::Regex; +use std::str; /* protocol ids, LittleEndian */ const ETH_P_IPV6: usize = 0xDD86; @@ -34,16 +34,13 @@ pub struct QryData { pub tcp_header: Option, } -fn flag_carnage( re: &Regex, payload: &[u8]) -> Option { - //let _payload: [u8] = payload.copy_from_slice(&payload); - for mat in re.find_iter(payload){ - //println!("{:?}", mat.as_bytes().to_owned().as_string()); +fn flag_carnage(re: &Regex, payload: &[u8]) -> Option { + for mat in re.find_iter(payload) { println!("{:?}", std::str::from_utf8(mat.as_bytes())); - } + } Some("test".to_owned()) } - pub fn parse(parse_file: &str, filter_str: &str) -> Vec { let ether_init = build_ether(); @@ -65,37 +62,40 @@ pub fn parse(parse_file: &str, filter_str: &str) -> Vec { while let Ok(packet) = cap.next() { me.time = (packet.header.ts.tv_usec as f64 / 1000000.0) + packet.header.ts.tv_sec as f64; me.data = Some(packet.data.to_vec()); - flag_carnage( &re, packet.data); + flag_carnage(&re, packet.data); me.ether_header = packet_handler::ethernet_handler(packet.data); - if ETH_P_IP == me.ether_header.ether_type as usize { - me.ipv6_header = None::; - me.ipv4_header = Some(packet_handler::ip_handler(packet.data)).unwrap(); - if TCP == me.ipv4_header.unwrap().ip_protocol as usize { - me.tcp_header = Some(packet_handler::tcp_handler( - me.ipv4_header.unwrap().ip_ihl, - packet.data, - )) - .unwrap(); - me.data = packet_handler::payload_handler( - me.ipv4_header.unwrap().ip_ihl, - me.tcp_header.unwrap().data_offset, - packet.data, - ); + match me.ether_header.ether_type as usize { + ETH_P_IP => { + me.ipv6_header = None::; + me.ipv4_header = Some(packet_handler::ip_handler(packet.data)).unwrap(); + if TCP == me.ipv4_header.unwrap().ip_protocol as usize { + me.tcp_header = Some(packet_handler::tcp_handler( + me.ipv4_header.unwrap().ip_ihl, + packet.data, + )) + .unwrap(); + me.data = packet_handler::payload_handler( + me.ipv4_header.unwrap().ip_ihl, + me.tcp_header.unwrap().data_offset, + packet.data, + ); + } } - } - if ETH_P_IPV6 == me.ether_header.ether_type as usize { - me.ipv4_header = None::; - me.ipv6_header = Some(packet_handler::ipv6_handler(packet.data)).unwrap(); - if TCP == me.ipv6_header.unwrap().next_header as usize { - me.tcp_header = Some(packet_handler::tcp_handler(10, packet.data)).unwrap(); - me.data = packet_handler::payload_handler( - 10, - me.tcp_header.unwrap().data_offset, - packet.data, - ); + ETH_P_IPV6 => { + me.ipv4_header = None::; + me.ipv6_header = Some(packet_handler::ipv6_handler(packet.data)).unwrap(); + if TCP == me.ipv6_header.unwrap().next_header as usize { + me.tcp_header = Some(packet_handler::tcp_handler(10, packet.data)).unwrap(); + me.data = packet_handler::payload_handler( + 10, + me.tcp_header.unwrap().data_offset, + packet.data, + ); + } } - } + _ => println!("network protocol not implemented"), + } v.push(QryData { id: 0, time: me.time, @@ -129,36 +129,40 @@ pub fn parse_device(parse_device: &str, filter_str: &str, insert_max: &usize) -> 'parse: while let Ok(packet) = cap.next() { me.time = (packet.header.ts.tv_usec as f64 / 1000000.0) + packet.header.ts.tv_sec as f64; me.data = Some(packet.data.to_vec()); + flag_carnage(&re, packet.data); me.ether_header = packet_handler::ethernet_handler(packet.data); - if ETH_P_IP == me.ether_header.ether_type as usize { - me.ipv6_header = None::; - me.ipv4_header = Some(packet_handler::ip_handler(packet.data)).unwrap(); - if TCP == me.ipv4_header.unwrap().ip_protocol as usize { - me.tcp_header = Some(packet_handler::tcp_handler( - me.ipv4_header.unwrap().ip_ihl, - packet.data, - )) - .unwrap(); - me.data = packet_handler::payload_handler( - me.ipv4_header.unwrap().ip_ihl, - me.tcp_header.unwrap().data_offset, - packet.data, - ); + match me.ether_header.ether_type as usize { + ETH_P_IP => { + me.ipv6_header = None::; + me.ipv4_header = Some(packet_handler::ip_handler(packet.data)).unwrap(); + if TCP == me.ipv4_header.unwrap().ip_protocol as usize { + me.tcp_header = Some(packet_handler::tcp_handler( + me.ipv4_header.unwrap().ip_ihl, + packet.data, + )) + .unwrap(); + me.data = packet_handler::payload_handler( + me.ipv4_header.unwrap().ip_ihl, + me.tcp_header.unwrap().data_offset, + packet.data, + ); + } } - } - if ETH_P_IPV6 == me.ether_header.ether_type as usize { - me.ipv4_header = None::; - me.ipv6_header = Some(packet_handler::ipv6_handler(packet.data)).unwrap(); - if TCP == me.ipv6_header.unwrap().next_header as usize { - me.tcp_header = Some(packet_handler::tcp_handler(10, packet.data)).unwrap(); - me.data = packet_handler::payload_handler( - 10, - me.tcp_header.unwrap().data_offset, - packet.data, - ); + ETH_P_IPV6 => { + me.ipv4_header = None::; + me.ipv6_header = Some(packet_handler::ipv6_handler(packet.data)).unwrap(); + if TCP == me.ipv6_header.unwrap().next_header as usize { + me.tcp_header = Some(packet_handler::tcp_handler(10, packet.data)).unwrap(); + me.data = packet_handler::payload_handler( + 10, + me.tcp_header.unwrap().data_offset, + packet.data, + ); + } } - } + _ => println!("network protocol not implemented"), + } v.push(QryData { id: 0, time: me.time,