Skip to content

Commit 4854538

Browse files
committed
Add: Nasl builtin function
1 parent a7233f1 commit 4854538

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

rust/examples/tcp.nasl renamed to rust/examples/socket/tcp.nasl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ display("fd: ", sock);
1010
ret = send(socket: sock, data: 'foobar');
1111
display("num bytes sent: ", ret);
1212
rec = recv(socket: sock, length: 10, min: 3);
13-
display(rec);
13+
display("received: ", rec);
14+
port = get_source_port(sock);
15+
display("source port: ", port);
1416
close(sock);
15-
display("end");
17+
display("end");
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# SPDX-FileCopyrightText: 2024 Greenbone AG
2+
#
3+
# SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception
4+
5+
display("Start");
6+
display("is function defined: ", defined_func("open_sock_udp"));
7+
sock = open_sock_tcp(34254, transport: 1);
8+
if (isnull(sock)) {
9+
display("Failed to open socket");
10+
exit(0);
11+
}
12+
display("fd: ", sock);
13+
ret = send(socket: sock, data: '123');
14+
if (ret < 0) {
15+
display("Failed to send data");
16+
exit(0);
17+
}
18+
display("num bytes sent: ", ret);
19+
rec = recv_line(socket: sock, length: 10, timeout: 1);
20+
display("line1: ", rec);
21+
rec = recv_line(socket: sock, length: 10, timeout: 1);
22+
display("line2: ", rec);
23+
rec = recv_line(socket: sock, length: 10, timeout: 1);
24+
display("line3: ", rec);
25+
display("end");
File renamed without changes.

rust/src/nasl/builtin/network/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@
1313
- get_host_ip
1414
- scanner_add_port
1515
- recv_line
16+
- get_source_port
1617

1718
## Missing
1819

19-
- end_denial
2020
- ftp_get_pasv_port
2121
- ftp_log_in
2222
- get_host_open_port
2323
- get_port_state
2424
- get_port_transport
25-
- get_source_port
2625
- get_tcp_port_state
2726
- get_udp_port_state
2827
- join_multicast_group
@@ -31,4 +30,5 @@
3130
- open_priv_sock_udp
3231
- scanner_get_port
3332
- start_denial
33+
- end_denial
3434
- telnet_init

rust/src/nasl/builtin/network/socket.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,28 @@ impl NaslSockets {
887887

888888
Ok(NaslValue::Number(fd as i64))
889889
}
890+
891+
/// Get the source port of a open socket
892+
#[nasl_function]
893+
fn get_source_port(&self, socket: usize) -> Result<NaslValue, FunctionErrorKind> {
894+
let handles = self.handles.read().unwrap();
895+
let socket = handles
896+
.handles
897+
.get(socket)
898+
.ok_or(FunctionErrorKind::WrongArgument(
899+
"the given socket FD does not exist".to_string(),
900+
))?;
901+
let port = match socket {
902+
NaslSocket::Tcp(conn) => conn.socket.local_addr()?.port(),
903+
NaslSocket::Udp(conn) => conn.socket.local_addr()?.port(),
904+
NaslSocket::Close => {
905+
return Err(FunctionErrorKind::WrongArgument(
906+
"the given socket FD is already closed".to_string(),
907+
))
908+
}
909+
};
910+
Ok(NaslValue::Number(port as i64))
911+
}
890912
}
891913

892914
function_set! {
@@ -900,5 +922,6 @@ function_set! {
900922
(NaslSockets::send, "send"),
901923
(NaslSockets::recv, "recv"),
902924
(NaslSockets::recv_line, "recv_line"),
925+
(NaslSockets::get_source_port, "get_source_port"),
903926
)
904927
}

0 commit comments

Comments
 (0)