Skip to content

Commit

Permalink
Fix: nasl builtin function recv timeout after first response
Browse files Browse the repository at this point in the history
recv may return before length bytes have been read: as soon as at least one byte
has been received, the timeout is lowered to 1 second. If no data is received
during that time, the function returns the already read data; otherwise, if the
full initial timeout has not been reached, a 1 second timeout is re-armed and
the script tries to receive more data from the socket. This special feature was
implemented to get a good compromise between reliability and speed when
openvas-scanner talks to unknown or complex protocols. This functionality was missing.
  • Loading branch information
Kraemii committed Oct 30, 2024
1 parent fafdab3 commit 002562d
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions rust/src/nasl/builtin/network/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later

use std::{
io::{BufRead, Read, Write},
io::{self, BufRead, Read, Write},
sync::RwLock,
thread::sleep,
time::{Duration, SystemTime},
Expand Down Expand Up @@ -224,12 +224,17 @@ impl NaslSockets {
"the given socket FD {socket} does not exist"
)))? {
NaslSocket::Tcp(conn) => {
let mut pos = 0;
let mut pos = match convert_timeout(timeout) {
Some(timeout) => conn.read_with_timeout(&mut data, timeout),
None => conn.read(&mut data),
}?;
let timeout = Duration::from_secs(1);
while pos < min {
pos += match convert_timeout(timeout) {
Some(timeout) => conn.read_with_timeout(&mut data[pos..], timeout),
None => conn.read(&mut data[pos..]),
}?;
match conn.read_with_timeout(&mut data[pos..], timeout) {
Ok(n) => pos += n,
Err(e) if e.kind() == io::ErrorKind::TimedOut => break,
Err(e) => return Err(e.into()),
}
}
Ok(NaslValue::Data(data[..pos].to_vec()))
}
Expand Down

0 comments on commit 002562d

Please sign in to comment.