Skip to content

Commit

Permalink
Add: Nasl builtin function
Browse files Browse the repository at this point in the history
  • Loading branch information
Kraemii committed Oct 28, 2024
1 parent a7233f1 commit 4854538
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 4 deletions.
6 changes: 4 additions & 2 deletions rust/examples/tcp.nasl → rust/examples/socket/tcp.nasl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ display("fd: ", sock);
ret = send(socket: sock, data: 'foobar');
display("num bytes sent: ", ret);
rec = recv(socket: sock, length: 10, min: 3);
display(rec);
display("received: ", rec);
port = get_source_port(sock);
display("source port: ", port);
close(sock);
display("end");
display("end");
25 changes: 25 additions & 0 deletions rust/examples/socket/tcp_recv_line.nasl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# SPDX-FileCopyrightText: 2024 Greenbone AG
#
# SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

display("Start");
display("is function defined: ", defined_func("open_sock_udp"));
sock = open_sock_tcp(34254, transport: 1);
if (isnull(sock)) {
display("Failed to open socket");
exit(0);
}
display("fd: ", sock);
ret = send(socket: sock, data: '123');
if (ret < 0) {
display("Failed to send data");
exit(0);
}
display("num bytes sent: ", ret);
rec = recv_line(socket: sock, length: 10, timeout: 1);
display("line1: ", rec);
rec = recv_line(socket: sock, length: 10, timeout: 1);
display("line2: ", rec);
rec = recv_line(socket: sock, length: 10, timeout: 1);
display("line3: ", rec);
display("end");
File renamed without changes.
4 changes: 2 additions & 2 deletions rust/src/nasl/builtin/network/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@
- get_host_ip
- scanner_add_port
- recv_line
- get_source_port

## Missing

- end_denial
- ftp_get_pasv_port
- ftp_log_in
- get_host_open_port
- get_port_state
- get_port_transport
- get_source_port
- get_tcp_port_state
- get_udp_port_state
- join_multicast_group
Expand All @@ -31,4 +30,5 @@
- open_priv_sock_udp
- scanner_get_port
- start_denial
- end_denial
- telnet_init
23 changes: 23 additions & 0 deletions rust/src/nasl/builtin/network/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,28 @@ impl NaslSockets {

Ok(NaslValue::Number(fd as i64))
}

/// Get the source port of a open socket
#[nasl_function]
fn get_source_port(&self, socket: usize) -> Result<NaslValue, FunctionErrorKind> {
let handles = self.handles.read().unwrap();
let socket = handles
.handles
.get(socket)
.ok_or(FunctionErrorKind::WrongArgument(
"the given socket FD does not exist".to_string(),
))?;
let port = match socket {
NaslSocket::Tcp(conn) => conn.socket.local_addr()?.port(),
NaslSocket::Udp(conn) => conn.socket.local_addr()?.port(),
NaslSocket::Close => {
return Err(FunctionErrorKind::WrongArgument(
"the given socket FD is already closed".to_string(),
))
}
};
Ok(NaslValue::Number(port as i64))
}
}

function_set! {
Expand All @@ -900,5 +922,6 @@ function_set! {
(NaslSockets::send, "send"),
(NaslSockets::recv, "recv"),
(NaslSockets::recv_line, "recv_line"),
(NaslSockets::get_source_port, "get_source_port"),
)
}

0 comments on commit 4854538

Please sign in to comment.