From 4fa701ff956291bc1dfa38655d646d6f55279a17 Mon Sep 17 00:00:00 2001 From: Toni Peter Date: Mon, 11 Nov 2024 10:11:51 +0100 Subject: [PATCH] Move PacketForgeryError into RawIpError --- rust/src/nasl/builtin/error.rs | 5 ----- rust/src/nasl/builtin/raw_ip/mod.rs | 4 ++-- rust/src/nasl/builtin/raw_ip/packet_forgery.rs | 18 +++++++++++++----- rust/src/nasl/mod.rs | 1 + 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/rust/src/nasl/builtin/error.rs b/rust/src/nasl/builtin/error.rs index 9ce464fe7..9fd6c52c1 100644 --- a/rust/src/nasl/builtin/error.rs +++ b/rust/src/nasl/builtin/error.rs @@ -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), } @@ -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); diff --git a/rust/src/nasl/builtin/raw_ip/mod.rs b/rust/src/nasl/builtin/raw_ip/mod.rs index afab04c48..25924f4d4 100644 --- a/rust/src/nasl/builtin/raw_ip/mod.rs +++ b/rust/src/nasl/builtin/raw_ip/mod.rs @@ -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.")] @@ -27,6 +25,8 @@ pub enum RawIpError { FailedToBind(io::Error), #[error("No route to destination.")] NoRouteToDestination, + #[error("{0}")] + PacketForgery(PacketForgeryError), } pub struct RawIp; diff --git a/rust/src/nasl/builtin/raw_ip/packet_forgery.rs b/rust/src/nasl/builtin/raw_ip/packet_forgery.rs index 8d61eebfc..8e43b0cf0 100644 --- a/rust/src/nasl/builtin/raw_ip/packet_forgery.rs +++ b/rust/src/nasl/builtin/raw_ip/packet_forgery.rs @@ -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 for FnError { + fn from(e: PacketForgeryError) -> Self { + RawIpError::PacketForgery(e).into() + } } fn error(s: String) -> FnError { @@ -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()) }; } @@ -157,7 +165,7 @@ fn forge_ip_packet(register: &Register, configs: &Context) -> Result Result *x as u8, @@ -409,7 +417,7 @@ fn dump_ip_packet(register: &Register, _: &Context) -> Result { 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()); @@ -1794,7 +1802,7 @@ fn forge_igmp_packet(register: &Register, _configs: &Context) -> Result { diff --git a/rust/src/nasl/mod.rs b/rust/src/nasl/mod.rs index ca9a21a6d..c150e41c6 100644 --- a/rust/src/nasl/mod.rs +++ b/rust/src/nasl/mod.rs @@ -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;