Skip to content

Commit

Permalink
Replace Diagnostic variants in network_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehforsch committed Dec 4, 2024
1 parent c9970a0 commit 4b2b577
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion rust/src/nasl/builtin/network/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn this_host(context: &Context) -> Result<String, SocketError> {

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
Expand Down
17 changes: 6 additions & 11 deletions rust/src/nasl/builtin/network/network_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ use super::socket::SocketError;
pub fn ipstr2ipaddr(ip_addr: &str) -> Result<IpAddr, SocketError> {
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())),
}
}

Expand All @@ -32,10 +29,10 @@ pub fn convert_timeout(timeout: Option<i64>) -> Option<Duration> {

/// 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<UdpSocket, SocketError> {
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),
}
}

Expand All @@ -49,7 +46,7 @@ pub fn get_source_ip(dst: IpAddr, port: u16) -> Result<IpAddr, SocketError> {
.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
Expand Down Expand Up @@ -130,7 +127,5 @@ pub fn get_netmask_by_local_ip(local_address: IpAddr) -> Result<Option<IpAddr>,
unsafe {
libc::freeifaddrs(interfaces);
}
Err(SocketError::Diagnostic(
"No route to destination".to_string(),
))
Err(SocketError::NoRouteToDestination(local_address))
}
8 changes: 7 additions & 1 deletion rust/src/nasl/builtin/network/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use std::{
io::{self, BufRead, Read, Write},
net::IpAddr,
net::{IpAddr, SocketAddr},
sync::Mutex,
thread::sleep,
time::{Duration, SystemTime},
Expand Down Expand Up @@ -52,6 +52,12 @@ pub enum SocketError {
FailedToParseResponseCode(std::num::ParseIntError),
#[error("Expected code {0:?}, got response: {1}")]
ResponseCodeMismatch(Vec<usize>, 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
Expand Down

0 comments on commit 4b2b577

Please sign in to comment.