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 30, 2024
1 parent 4854538 commit b06d30b
Show file tree
Hide file tree
Showing 7 changed files with 582 additions and 477 deletions.
16 changes: 16 additions & 0 deletions rust/examples/socket/ftp_login.nasl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# SPDX-FileCopyrightText: 2024 Greenbone AG
#
# SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

display("Start");
display("is function open_sock_tcp defined: ", defined_func("open_sock_tcp"));
sock = open_sock_tcp(21, transport: 1);
display("was socket created: ", !isnull(sock));
display("fd: ", sock);
display("is function ftp_log_in defined: ", defined_func("ftp_log_in"));
# Login data for ftp://ftp.dlptest.com/ provided by https://dlptest.com/ftp-test/
user = "dlpuser";
pass = "rNrKYTX9g7z3RgJRmxWuGHbeu";
display("login succeeded: ", ftp_log_in(user: user, pass: pass, socket: sock));
close(sock);
display("end");
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
21 changes: 7 additions & 14 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 All @@ -25,24 +26,16 @@ pub fn ipstr2ipaddr(ip_addr: &str) -> Result<IpAddr, FunctionErrorKind> {

/// Convert timeout
pub fn convert_timeout(timeout: Option<i64>) -> Option<Duration> {
timeout.and_then(|timeout| {
if timeout < 1 {
None
} else {
Some(Duration::from_secs(timeout as u64))
}
})
timeout
.filter(|timeout| *timeout >= 1)
.map(|timeout| Duration::from_secs(timeout as u64))
}

/// 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 b06d30b

Please sign in to comment.