Skip to content

Commit

Permalink
feat: change net functions to use net/mod.rs's NetError
Browse files Browse the repository at this point in the history
  • Loading branch information
SkuldNorniern committed Mar 13, 2024
1 parent 48b1034 commit 4edaf04
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::fs::File;
use std::{fmt::Display, process::exit};

use crate::logger::{Logger, Logstdout};
use crate::net::capture::DeviceError;
use crate::net::DeviceError;
// use env_logger;::{init, Logger};

use log::{debug, Level, LevelFilter};
Expand Down
18 changes: 16 additions & 2 deletions src/net/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//mod fluereflow
pub mod capture;
pub mod errors;
mod capture;
// pub mod errors;
mod flows;
// mod interface;
pub mod live_fluereflow;
Expand Down Expand Up @@ -28,6 +28,11 @@ use pcap::Error;
pub enum NetError {
DeviceError(DeviceError),
PcapError(Error),
UnknownProtocol(u8),
UnknownEtherType(String),
UnknownDSCP(u8),
InvalidPacket,
EmptyPacket,
}

impl From<DeviceError> for NetError {
Expand All @@ -47,6 +52,15 @@ impl Display for NetError {
match self {
NetError::DeviceError(err) => err.fmt(f),
NetError::PcapError(err) => err.fmt(f),
NetError::UnknownProtocol(protocol) => {
write!(f, "Unknown protocol: {}", protocol)
},
NetError::UnknownEtherType(ether_type) => {
write!(f, "Unknown ether type: {}", ether_type)
},
NetError::UnknownDSCP(dscp) => write!(f, "Unknown dscp: {}", dscp),
NetError::InvalidPacket => write!(f, "Invalid packet"),
NetError::EmptyPacket => write!(f, "Empty packet"),
}
}
}
26 changes: 7 additions & 19 deletions src/net/parser/fluereflows.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use pcap;

use crate::net::errors::NetError;
use crate::net::NetError;
use crate::net::parser::{
dscp_to_tos, parse_flags, parse_microseconds, parse_ports, protocol_to_number,
};
Expand Down Expand Up @@ -130,9 +130,9 @@ pub fn parse_fluereflow(packet: pcap::Packet) -> Result<(usize, [u8; 9], FluereR
arp_packet(time, i)
}
_ => {
return Err(NetError::UnknownProtocol {
protocol: ethernet_packet.get_ethertype().to_string(),
})
return Err(NetError::UnknownEtherType (
ethernet_packet.get_ethertype().to_string(),
))
}
};

Expand Down Expand Up @@ -197,15 +197,8 @@ fn ipv4_packet(time: u64, packet: Ipv4Packet) -> Result<(usize, [u8; 9], FluereR
let dst_ip = packet.get_destination();

// ports parsing
let parsed_ports = parse_ports(protocol, packet.payload());
match parsed_ports {
Ok(_) => {}
Err(e) => {
println!("Unknown protocol {}\n Report to the developer", e);
return Err(e);
}
}
let (src_port, dst_port) = parsed_ports.unwrap();
let (src_port, dst_port) = parse_ports(protocol, packet.payload())?;

// TCP flags Fin Syn Rst Psh Ack Urg Ece Cwr Ns
let flags = parse_flags(protocol, packet.payload());

Expand Down Expand Up @@ -255,12 +248,7 @@ fn ipv6_packet(time: u64, packet: Ipv6Packet) -> Result<(usize, [u8; 9], FluereR
let dst_ip = packet.get_destination();

// ports parsing
let parsed_ports = parse_ports(protocol, packet.payload());
match parsed_ports {
Ok(_) => {}
Err(e) => return Err(e),
}
let (src_port, dst_port) = parsed_ports.unwrap();
let (src_port, dst_port) = parse_ports(protocol, packet.payload())?;
// TCP flags Fin Syn Rst Psh Ack Urg Ece Cwr Ns
let flags = parse_flags(protocol, packet.payload());

Expand Down
8 changes: 4 additions & 4 deletions src/net/parser/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use pnet::packet::ipv6::Ipv6Packet;
use pnet::packet::udp::UdpPacket;
use pnet::packet::Packet;

use crate::net::errors::NetError;
use crate::net::NetError;
use crate::net::parser::{parse_ports, protocol_to_number};
use crate::net::types::{Key, MacAddress};

Expand Down Expand Up @@ -146,9 +146,9 @@ pub fn parse_keys(packet: pcap::Packet) -> Result<(Key, Key), NetError> {
}

_ => {
return Err(NetError::UnknownProtocol {
protocol: ethernet_packet.get_ethertype().to_string(),
})
return Err(NetError::UnknownEtherType (
ethernet_packet.get_ethertype().to_string()
))
}
};

Expand Down
24 changes: 10 additions & 14 deletions src/net/parser/ports.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::net::errors::NetError;
use crate::net::NetError;

use log::debug;
use pnet::packet::{tcp::TcpPacket, udp::UdpPacket};
Expand All @@ -7,27 +7,23 @@ pub fn parse_ports(protocol: u8, payload: &[u8]) -> Result<(u16, u16), NetError>
match protocol {
58 => Ok((0, 0)),
17 => {
let udp = UdpPacket::new(payload).unwrap();

Ok((udp.get_source(), udp.get_destination()))
match UdpPacket::new(payload) {
Some(udp) => Ok((udp.get_source(), udp.get_destination())),
None => Err(NetError::InvalidPacket),
}
}
6 => {
let tcp = TcpPacket::new(payload).unwrap();

Ok((tcp.get_source(), tcp.get_destination()))
match TcpPacket::new(payload){
Some(tcp) => Ok((tcp.get_source(), tcp.get_destination())),
None => Err(NetError::InvalidPacket),
}
}
2 => Ok((0, 0)),
1 => Ok((0, 0)),
0 => Ok((0, 0)),
_ => {
debug!("Unknown protocol: {}", protocol);
Err(NetError::UnknownProtocol {
protocol: protocol.to_string(),
})
Err(NetError::UnknownProtocol(protocol))
}
}

//Err(NetError::UnknownProtocol {
// protocol: protocol.to_string(),
//})
}
6 changes: 3 additions & 3 deletions src/net/parser/tos.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::net::errors::ParseError;
use crate::net::NetError;

pub fn dscp_to_tos(dscp: u8) -> Result<u8, ParseError> {
pub fn dscp_to_tos(dscp: u8) -> Result<u8, NetError> {
let tos = match dscp {
0 => 0,
8 => 32,
Expand All @@ -23,7 +23,7 @@ pub fn dscp_to_tos(dscp: u8) -> Result<u8, ParseError> {
46 => 184,
48 => 192,
56 => 224,
_ => return Err(ParseError::UnknownDSCP { dscp }),
_ => return Err(NetError::UnknownDSCP ( dscp )),
};

Ok(tos)
Expand Down

0 comments on commit 4edaf04

Please sign in to comment.