feat: added a pcap listener to parse commands

This commit is contained in:
Andrew Rioux
2023-04-28 15:42:21 -04:00
parent bac3e56f3c
commit e0c7e1c240
5 changed files with 149 additions and 2 deletions

View File

@@ -16,7 +16,7 @@
use errno::Errno;
use std::{
convert::From,
ffi::{self, CStr, CString},
ffi::{self, CStr, CString}, fmt::Display, error,
};
#[derive(Debug)]
@@ -32,6 +32,36 @@ pub enum Error {
pub type Result<T> = std::result::Result<T, Error>;
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::PcapError(err) => {
if let Ok(err_str) = std::str::from_utf8(err.as_bytes()) {
return write!(f, "pcap error: {err_str}");
}
write!(f, "unknown pcap error")
},
Error::StringParse => write!(f, "unable to parse a string from pcap"),
Error::UnknownPacketType(ptype) => write!(f, "unknown packet type ({ptype})"),
Error::PacketLengthInvalid => write!(f, "received a packet with a length that mismatched the header"),
Error::InvalidPcapFd => write!(f, "internal pcap file descriptor error"),
Error::Io(io) => write!(f, "std::io error ({io})"),
Error::Libc(err) => write!(f, "libc error ({err})"),
}
}
}
impl error::Error for Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self {
Error::Io(err) => Some(err),
Error::Libc(err) => Some(err),
_ => None
}
}
}
impl From<&[i8; 256]> for Error {
fn from(buf: &[i8; 256]) -> Error {
match CString::new(