Skip to content

Commit

Permalink
Refactor sockets
Browse files Browse the repository at this point in the history
Split socket into different modules for handling tcp and udp connections. This reduces complexety of the socket module.
  • Loading branch information
Kraemii committed Oct 28, 2024
1 parent 4854538 commit 98d3bbd
Show file tree
Hide file tree
Showing 6 changed files with 633 additions and 468 deletions.
5 changes: 4 additions & 1 deletion rust/src/nasl/builtin/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use crate::storage::{Field, Retrieve};
pub mod network;
pub mod network_utils;
pub mod socket;
pub mod tcp;
pub mod tls;
pub mod udp;

// 512 Bytes are typically supported by network devices. The ip header maximum size is 60 and a UDP
// header contains 8 bytes, which must be subtracted from the max size for UDP packages.
Expand Down Expand Up @@ -80,7 +83,7 @@ pub fn get_retry(context: &Context) -> u8 {
match val {
NaslValue::String(val) => val.parse::<u8>().unwrap_or(2),
NaslValue::Number(val) => {
if val < 1 || val > 255 {
if !(1..=255).contains(&val) {
2
} else {
val as u8
Expand Down
11 changes: 4 additions & 7 deletions rust/src/nasl/builtin/network/network_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

//! This module provides utility functions for IP handling.
use std::{
io,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, UdpSocket},
ptr,
str::FromStr,
Expand Down Expand Up @@ -35,14 +36,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, FunctionErrorKind> {
let fe = Err(FunctionErrorKind::Diagnostic(
"Error binding".to_string(),
None,
));
pub fn bind_local_socket(dst: &SocketAddr) -> io::Result<UdpSocket> {
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"),
SocketAddr::V6(_) => UdpSocket::bind("[::]:0"),
}
}

Expand Down
Loading

0 comments on commit 98d3bbd

Please sign in to comment.