Skip to content

Commit

Permalink
Move PacketForgeryError into RawIpError
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehforsch committed Nov 11, 2024
1 parent 1adfac7 commit 4fa701f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
5 changes: 0 additions & 5 deletions rust/src/nasl/builtin/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ pub enum BuiltinError {
KB(KBError),
#[cfg(feature = "nasl-builtin-raw-ip")]
#[error("{0}")]
PacketForgery(super::raw_ip::PacketForgeryError),
#[cfg(feature = "nasl-builtin-raw-ip")]
#[error("{0}")]
RawIp(super::raw_ip::RawIpError),
}

Expand Down Expand Up @@ -77,7 +74,5 @@ builtin_error_variant!(IsotimeError, Isotime);
builtin_error_variant!(RegexError, Regex);
builtin_error_variant!(KBError, KB);

#[cfg(feature = "nasl-builtin-raw-ip")]
builtin_error_variant!(super::raw_ip::PacketForgeryError, PacketForgery);
#[cfg(feature = "nasl-builtin-raw-ip")]
builtin_error_variant!(super::raw_ip::RawIpError, RawIp);
4 changes: 2 additions & 2 deletions rust/src/nasl/builtin/raw_ip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ use thiserror::Error;
pub enum RawIpError {
#[error("Failed to get local MAC address.")]
FailedToGetLocalMacAddress,
#[error("Failed to create packet from buffer.")]
FailedToCreatePacket,
#[error("Failed to get device list.")]
FailedToGetDeviceList,
#[error("Invalid IP address.")]
Expand All @@ -27,6 +25,8 @@ pub enum RawIpError {
FailedToBind(io::Error),
#[error("No route to destination.")]
NoRouteToDestination,
#[error("{0}")]
PacketForgery(PacketForgeryError),
}

pub struct RawIp;
Expand Down
18 changes: 13 additions & 5 deletions rust/src/nasl/builtin/raw_ip/packet_forgery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ pub enum PacketForgeryError {
ParseSocketAddr(std::net::AddrParseError),
#[error("Failed to send packet. {0}")]
SendPacket(std::io::Error),
#[error("Failed to create packet from buffer.")]
CreatePacket,
}

impl From<PacketForgeryError> for FnError {
fn from(e: PacketForgeryError) -> Self {
RawIpError::PacketForgery(e).into()
}
}

fn error(s: String) -> FnError {
Expand All @@ -54,7 +62,7 @@ fn error(s: String) -> FnError {

macro_rules! custom_error {
($a:expr, $b:expr) => {
Err(PacketForgeryError::Custom(format!($a, $b)).into())
Err(RawIpError::PacketForgery(PacketForgeryError::Custom(format!($a, $b))).into())
};
}

Expand Down Expand Up @@ -157,7 +165,7 @@ fn forge_ip_packet(register: &Register, configs: &Context) -> Result<NaslValue,
let total_length = 20 + data.len();
let mut buf = vec![0; total_length];
let mut pkt = packet::ipv4::MutableIpv4Packet::new(&mut buf)
.ok_or_else(|| RawIpError::FailedToCreatePacket)?;
.ok_or_else(|| PacketForgeryError::CreatePacket)?;

pkt.set_total_length(total_length as u16);

Expand Down Expand Up @@ -284,7 +292,7 @@ fn set_ip_elements(register: &Register, _configs: &Context) -> Result<NaslValue,
};

let mut pkt = packet::ipv4::MutableIpv4Packet::new(&mut buf)
.ok_or_else(|| RawIpError::FailedToCreatePacket)?;
.ok_or_else(|| PacketForgeryError::CreatePacket)?;

let ip_hl = match register.named("ip_hl") {
Some(ContextType::Value(NaslValue::Number(x))) => *x as u8,
Expand Down Expand Up @@ -409,7 +417,7 @@ fn dump_ip_packet(register: &Register, _: &Context) -> Result<NaslValue, FnError
match ip {
NaslValue::Data(data) => {
let pkt = packet::ipv4::Ipv4Packet::new(data)
.ok_or_else(|| RawIpError::FailedToCreatePacket)?;
.ok_or_else(|| PacketForgeryError::CreatePacket)?;

println!("\tip_hl={}", pkt.get_header_length());
println!("\tip_v={}", pkt.get_version());
Expand Down Expand Up @@ -1794,7 +1802,7 @@ fn forge_igmp_packet(register: &Register, _configs: &Context) -> Result<NaslValu
ip_buf.append(&mut buf);
let l = ip_buf.len();
let mut pkt = packet::ipv4::MutableIpv4Packet::new(&mut ip_buf)
.ok_or_else(|| RawIpError::FailedToCreatePacket)?;
.ok_or_else(|| PacketForgeryError::CreatePacket)?;
pkt.set_total_length(l as u16);
match register.named("update_ip_len") {
Some(ContextType::Value(NaslValue::Boolean(l))) if !(*l) => {
Expand Down
1 change: 1 addition & 0 deletions rust/src/nasl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod prelude {
pub use super::syntax::FSPluginLoader;
pub use super::syntax::Loader;
pub use super::syntax::NaslValue;
pub use super::utils::error::FnErrorKind;
pub use super::utils::error::WithErrorInfo;
pub use super::utils::function::CheckedPositionals;
pub use super::utils::function::FromNaslValue;
Expand Down

0 comments on commit 4fa701f

Please sign in to comment.