From 2a256f4fc9aa0979c95dd9945b975c89d49d356c Mon Sep 17 00:00:00 2001 From: Toni Peter Date: Wed, 4 Dec 2024 11:25:27 +0100 Subject: [PATCH] Replace Diagnostic variants in network_utils --- rust/src/nasl/builtin/network/network.rs | 2 +- rust/src/nasl/builtin/network/network_utils.rs | 17 ++++++----------- rust/src/nasl/builtin/network/socket.rs | 8 +++++++- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/rust/src/nasl/builtin/network/network.rs b/rust/src/nasl/builtin/network/network.rs index 761bdc7f6..547670d12 100644 --- a/rust/src/nasl/builtin/network/network.rs +++ b/rust/src/nasl/builtin/network/network.rs @@ -30,7 +30,7 @@ fn this_host(context: &Context) -> Result { get_source_ip(dst, port) .map(|ip| ip.to_string()) - .map_err(|_| SocketError::Diagnostic("No route to destination".to_string())) + .map_err(|_| SocketError::NoRouteToDestination(dst)) } /// Get the host name of the current (attacking) machine diff --git a/rust/src/nasl/builtin/network/network_utils.rs b/rust/src/nasl/builtin/network/network_utils.rs index 6bc75847d..ca03810e5 100644 --- a/rust/src/nasl/builtin/network/network_utils.rs +++ b/rust/src/nasl/builtin/network/network_utils.rs @@ -16,10 +16,7 @@ use super::socket::SocketError; pub fn ipstr2ipaddr(ip_addr: &str) -> Result { match IpAddr::from_str(ip_addr) { Ok(ip) => Ok(ip), - Err(_) => Err(SocketError::Diagnostic(format!( - "Invalid IP address ({})", - ip_addr - ))), + Err(_) => Err(SocketError::InvalidIpAddress(ip_addr.into())), } } @@ -32,10 +29,10 @@ pub fn convert_timeout(timeout: Option) -> Option { /// Bind a local UDP socket to a V4 or V6 address depending on the given destination address pub fn bind_local_socket(dst: &SocketAddr) -> Result { - let fe = Err(SocketError::Diagnostic("Error binding".to_string())); + let fe = |e| Err(SocketError::FailedToBindSocket(e, dst.clone())); match dst { - SocketAddr::V4(_) => UdpSocket::bind("0.0.0.0:0").or(fe), - SocketAddr::V6(_) => UdpSocket::bind("[::]:0").or(fe), + SocketAddr::V4(_) => UdpSocket::bind("0.0.0.0:0").or_else(fe), + SocketAddr::V6(_) => UdpSocket::bind("[::]:0").or_else(fe), } } @@ -49,7 +46,7 @@ pub fn get_source_ip(dst: IpAddr, port: u16) -> Result { .ok() .and_then(|_| local_socket.local_addr().ok()) .and_then(|l_addr| IpAddr::from_str(&l_addr.ip().to_string()).ok()) - .ok_or_else(|| SocketError::Diagnostic("No route to destination".to_string())) + .ok_or_else(|| SocketError::NoRouteToDestination(dst)) } /// Tests whether a packet sent to IP is LIKELY to route through the @@ -130,7 +127,5 @@ pub fn get_netmask_by_local_ip(local_address: IpAddr) -> Result, unsafe { libc::freeifaddrs(interfaces); } - Err(SocketError::Diagnostic( - "No route to destination".to_string(), - )) + Err(SocketError::NoRouteToDestination(local_address)) } diff --git a/rust/src/nasl/builtin/network/socket.rs b/rust/src/nasl/builtin/network/socket.rs index b76302d25..eb63a1f65 100644 --- a/rust/src/nasl/builtin/network/socket.rs +++ b/rust/src/nasl/builtin/network/socket.rs @@ -4,7 +4,7 @@ use std::{ io::{self, BufRead, Read, Write}, - net::IpAddr, + net::{IpAddr, SocketAddr}, sync::Mutex, thread::sleep, time::{Duration, SystemTime}, @@ -52,6 +52,12 @@ pub enum SocketError { FailedToParseResponseCode(std::num::ParseIntError), #[error("Expected code {0:?}, got response: {1}")] ResponseCodeMismatch(Vec, String), + #[error("String is not an IP address: {0}")] + InvalidIpAddress(String), + #[error("Failed to bind socket to {1}. {0}")] + FailedToBindSocket(io::Error, SocketAddr), + #[error("No route to destination: {0}.")] + NoRouteToDestination(IpAddr), } /// Interval used for timing tcp requests. Any tcp request has to wait at least