changed first if loop to match statement

This commit is contained in:
gurkenhabicht 2020-05-21 01:48:13 +02:00
parent 59e96a1205
commit 45d69dd97f
1 changed files with 65 additions and 61 deletions

View File

@ -5,9 +5,9 @@ mod packet_handler;
use eui48::MacAddress; use eui48::MacAddress;
use pcap::Capture; use pcap::Capture;
//use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; //use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use std::str;
use regex::bytes::Regex;
use regex::bytes::Match; use regex::bytes::Match;
use regex::bytes::Regex;
use std::str;
/* protocol ids, LittleEndian */ /* protocol ids, LittleEndian */
const ETH_P_IPV6: usize = 0xDD86; const ETH_P_IPV6: usize = 0xDD86;
@ -34,16 +34,13 @@ pub struct QryData {
pub tcp_header: Option<packet_handler::TcpHeader>, pub tcp_header: Option<packet_handler::TcpHeader>,
} }
fn flag_carnage( re: &Regex, payload: &[u8]) -> Option<String> { fn flag_carnage(re: &Regex, payload: &[u8]) -> Option<String> {
//let _payload: [u8] = payload.copy_from_slice(&payload); for mat in re.find_iter(payload) {
for mat in re.find_iter(payload){
//println!("{:?}", mat.as_bytes().to_owned().as_string());
println!("{:?}", std::str::from_utf8(mat.as_bytes())); println!("{:?}", std::str::from_utf8(mat.as_bytes()));
} }
Some("test".to_owned()) Some("test".to_owned())
} }
pub fn parse(parse_file: &str, filter_str: &str) -> Vec<QryData> { pub fn parse(parse_file: &str, filter_str: &str) -> Vec<QryData> {
let ether_init = build_ether(); let ether_init = build_ether();
@ -65,37 +62,40 @@ pub fn parse(parse_file: &str, filter_str: &str) -> Vec<QryData> {
while let Ok(packet) = cap.next() { 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.time = (packet.header.ts.tv_usec as f64 / 1000000.0) + packet.header.ts.tv_sec as f64;
me.data = Some(packet.data.to_vec()); 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); me.ether_header = packet_handler::ethernet_handler(packet.data);
if ETH_P_IP == me.ether_header.ether_type as usize { match me.ether_header.ether_type as usize {
me.ipv6_header = None::<packet_handler::IpV6Header>; ETH_P_IP => {
me.ipv4_header = Some(packet_handler::ip_handler(packet.data)).unwrap(); me.ipv6_header = None::<packet_handler::IpV6Header>;
if TCP == me.ipv4_header.unwrap().ip_protocol as usize { me.ipv4_header = Some(packet_handler::ip_handler(packet.data)).unwrap();
me.tcp_header = Some(packet_handler::tcp_handler( if TCP == me.ipv4_header.unwrap().ip_protocol as usize {
me.ipv4_header.unwrap().ip_ihl, me.tcp_header = Some(packet_handler::tcp_handler(
packet.data, me.ipv4_header.unwrap().ip_ihl,
)) packet.data,
.unwrap(); ))
me.data = packet_handler::payload_handler( .unwrap();
me.ipv4_header.unwrap().ip_ihl, me.data = packet_handler::payload_handler(
me.tcp_header.unwrap().data_offset, me.ipv4_header.unwrap().ip_ihl,
packet.data, me.tcp_header.unwrap().data_offset,
); packet.data,
);
}
} }
} ETH_P_IPV6 => {
if ETH_P_IPV6 == me.ether_header.ether_type as usize { me.ipv4_header = None::<packet_handler::IpV4Header>;
me.ipv4_header = None::<packet_handler::IpV4Header>; me.ipv6_header = Some(packet_handler::ipv6_handler(packet.data)).unwrap();
me.ipv6_header = Some(packet_handler::ipv6_handler(packet.data)).unwrap(); if TCP == me.ipv6_header.unwrap().next_header as usize {
if TCP == me.ipv6_header.unwrap().next_header as usize { me.tcp_header = Some(packet_handler::tcp_handler(10, packet.data)).unwrap();
me.tcp_header = Some(packet_handler::tcp_handler(10, packet.data)).unwrap(); me.data = packet_handler::payload_handler(
me.data = packet_handler::payload_handler( 10,
10, me.tcp_header.unwrap().data_offset,
me.tcp_header.unwrap().data_offset, packet.data,
packet.data, );
); }
} }
}
_ => println!("network protocol not implemented"),
}
v.push(QryData { v.push(QryData {
id: 0, id: 0,
time: me.time, 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() { '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.time = (packet.header.ts.tv_usec as f64 / 1000000.0) + packet.header.ts.tv_sec as f64;
me.data = Some(packet.data.to_vec()); me.data = Some(packet.data.to_vec());
flag_carnage(&re, packet.data);
me.ether_header = packet_handler::ethernet_handler(packet.data); me.ether_header = packet_handler::ethernet_handler(packet.data);
if ETH_P_IP == me.ether_header.ether_type as usize { match me.ether_header.ether_type as usize {
me.ipv6_header = None::<packet_handler::IpV6Header>; ETH_P_IP => {
me.ipv4_header = Some(packet_handler::ip_handler(packet.data)).unwrap(); me.ipv6_header = None::<packet_handler::IpV6Header>;
if TCP == me.ipv4_header.unwrap().ip_protocol as usize { me.ipv4_header = Some(packet_handler::ip_handler(packet.data)).unwrap();
me.tcp_header = Some(packet_handler::tcp_handler( if TCP == me.ipv4_header.unwrap().ip_protocol as usize {
me.ipv4_header.unwrap().ip_ihl, me.tcp_header = Some(packet_handler::tcp_handler(
packet.data, me.ipv4_header.unwrap().ip_ihl,
)) packet.data,
.unwrap(); ))
me.data = packet_handler::payload_handler( .unwrap();
me.ipv4_header.unwrap().ip_ihl, me.data = packet_handler::payload_handler(
me.tcp_header.unwrap().data_offset, me.ipv4_header.unwrap().ip_ihl,
packet.data, me.tcp_header.unwrap().data_offset,
); packet.data,
);
}
} }
} ETH_P_IPV6 => {
if ETH_P_IPV6 == me.ether_header.ether_type as usize { me.ipv4_header = None::<packet_handler::IpV4Header>;
me.ipv4_header = None::<packet_handler::IpV4Header>; me.ipv6_header = Some(packet_handler::ipv6_handler(packet.data)).unwrap();
me.ipv6_header = Some(packet_handler::ipv6_handler(packet.data)).unwrap(); if TCP == me.ipv6_header.unwrap().next_header as usize {
if TCP == me.ipv6_header.unwrap().next_header as usize { me.tcp_header = Some(packet_handler::tcp_handler(10, packet.data)).unwrap();
me.tcp_header = Some(packet_handler::tcp_handler(10, packet.data)).unwrap(); me.data = packet_handler::payload_handler(
me.data = packet_handler::payload_handler( 10,
10, me.tcp_header.unwrap().data_offset,
me.tcp_header.unwrap().data_offset, packet.data,
packet.data, );
); }
} }
}
_ => println!("network protocol not implemented"),
}
v.push(QryData { v.push(QryData {
id: 0, id: 0,
time: me.time, time: me.time,