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 Nov 20, 2024
1 parent 37dbf79 commit 3c4e952
Show file tree
Hide file tree
Showing 8 changed files with 583 additions and 478 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");
2 changes: 1 addition & 1 deletion rust/src/nasl/builtin/network/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
- scanner_add_port
- recv_line
- get_source_port
- ftp_log_in

## Missing

- ftp_get_pasv_port
- ftp_log_in
- get_host_open_port
- get_port_state
- get_port_transport
Expand Down
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 3c4e952

Please sign in to comment.