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 Nov 25, 2024
1 parent f56ee1e commit d7e85c8
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions rust/src/nasl/builtin/network/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,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 d7e85c8

Please sign in to comment.