Skip to content

Commit

Permalink
Replace read from buffer with NaslSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
Tehforsch committed Jan 14, 2025
1 parent b01d077 commit 75b300c
Showing 1 changed file with 30 additions and 23 deletions.
53 changes: 30 additions & 23 deletions rust/src/nasl/builtin/find_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception

use std::net::IpAddr;
use std::{io, net::IpAddr, time::Duration};

use thiserror::Error;

Expand Down Expand Up @@ -34,29 +34,36 @@ enum SpecialBehavior {
// TODO fill this in for services
}

async fn scan_port(target: IpAddr, port: u16) -> Result<Option<Service>, FindServiceError> {
let socket = make_tcp_socket(target, port, 0)?;
enum ReadResult {
Data(String),
Timeout,
}

async fn read_from_tcp_at_port(target: IpAddr, port: u16) -> Result<ReadResult, FindServiceError> {
let mut socket = make_tcp_socket(target, port, 0)?;
let mut buf: &mut [u8] = &mut [0; 100];
let result = socket.read_with_timeout(buf, Duration::from_millis(TIMEOUT_MILLIS));
match result {
Ok(pos) => Ok(ReadResult::Data(
String::from_utf8(buf[0..pos].to_vec()).unwrap(),
)),
Err(e) if e.kind() == io::ErrorKind::TimedOut => Ok(ReadResult::Timeout),
Err(e) => Err(SocketError::IO(e).into()),
}
}

let mut buf: &mut [u8] = &mut [0; 10];
todo!()
// let result = timeout(Duration::from_millis(TIMEOUT_MILLIS), async move {
// let result = socket.read_line();
// Ok::<_, FindServiceError>(buf)
// })
// .await;
// match result {
// Ok(Ok(buf)) => {
// println!("{}", String::from_utf8(buf.to_vec()).unwrap());
// }
// Ok(Err(_)) => {
// println!("err");
// }
// Err(_) => {
// println!("{}", port);
// println!("timeout");
// }
// }
// Ok(None)
async fn scan_port(target: IpAddr, port: u16) -> Result<Option<Service>, FindServiceError> {
let result = read_from_tcp_at_port(target, port).await?;
match result {
ReadResult::Data(data) => {
println!("{}", data);
}
ReadResult::Timeout => {
println!("{}", port);
println!("timeout");
}
}
Ok(None)
}

#[nasl_function]
Expand Down

0 comments on commit 75b300c

Please sign in to comment.