1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
use async_std::{
fs::File,
io::{ReadExt, WriteExt},
net::{TcpStream, ToSocketAddrs},
path::Path,
};
use super::{IoResult, DEFAULT_CHUNK_SIZE, END_OF_STREAM, INSTREAM, PING, PONG};
async fn ping<RW: ReadExt + WriteExt + Unpin>(mut stream: RW) -> IoResult {
stream.write_all(PING).await?;
let capacity = PONG.len();
let mut response = Vec::with_capacity(capacity);
stream.read_to_end(&mut response).await?;
Ok(response)
}
async fn scan<R: ReadExt + Unpin, RW: ReadExt + WriteExt + Unpin>(
mut input: R,
chunk_size: Option<usize>,
mut stream: RW,
) -> IoResult {
stream.write_all(INSTREAM).await?;
let chunk_size = chunk_size
.unwrap_or(DEFAULT_CHUNK_SIZE)
.min(u32::MAX as usize);
let mut buffer = vec![0; chunk_size];
loop {
let len = input.read(&mut buffer[..]).await?;
if len != 0 {
stream.write_all(&(len as u32).to_be_bytes()).await?;
stream.write_all(&buffer[..len]).await?;
} else {
stream.write_all(END_OF_STREAM).await?;
break;
}
}
let mut response = Vec::new();
stream.read_to_end(&mut response).await?;
Ok(response)
}
/// Sends a ping request to ClamAV using a Unix socket connection
///
/// This function establishes a Unix socket connection to a ClamAV server at the
/// specified `socket_path` and sends a ping request to it.
///
/// # Example
///
/// ```
/// # #[async_std::main]
/// # async fn main() {
/// let clamd_available = match clamav_client::async_std::ping_socket("/tmp/clamd.socket").await {
/// Ok(ping_response) => ping_response == b"PONG\0",
/// Err(_) => false,
/// };
/// # assert!(clamd_available);
/// # }
/// ```
///
#[cfg(unix)]
pub async fn ping_socket<P: AsRef<Path>>(socket_path: P) -> IoResult {
use async_std::os::unix::net::UnixStream;
let stream = UnixStream::connect(socket_path).await?;
ping(stream).await
}
/// Scans a file for viruses using a Unix socket connection
///
/// This function reads data from a file located at the specified `path` and
/// streams it to a ClamAV server through a Unix socket connection for scanning.
///
/// # Arguments
///
/// * `file_path`: Path to the file to be scanned
/// * `socket_path`: Path to the Unix socket for the ClamAV server
/// * `chunk_size`: An optional chunk size for reading data. If `None`, a default chunk size is used
///
/// # Returns
///
/// An `IoResult` containing the server's response as a vector of bytes
///
#[cfg(unix)]
pub async fn scan_file_socket<P: AsRef<Path>>(
file_path: P,
socket_path: P,
chunk_size: Option<usize>,
) -> IoResult {
use async_std::os::unix::net::UnixStream;
let file = File::open(file_path).await?;
let stream = UnixStream::connect(socket_path).await?;
scan(file, chunk_size, stream).await
}
/// Scans a data buffer for viruses using a Unix socket connection
///
/// This function streams the provided `buffer` data to a ClamAV server through
/// a Unix socket connection for scanning.
///
/// # Arguments
///
/// * `buffer`: The data to be scanned
/// * `socket_path`: The path to the Unix socket for the ClamAV server
/// * `chunk_size`: An optional chunk size for reading data. If `None`, a default chunk size is used
///
/// # Returns
///
/// An `IoResult` containing the server's response as a vector of bytes
///
#[cfg(unix)]
pub async fn scan_buffer_socket<P: AsRef<Path>>(
buffer: &[u8],
socket_path: P,
chunk_size: Option<usize>,
) -> IoResult {
use async_std::os::unix::net::UnixStream;
let stream = UnixStream::connect(socket_path).await?;
scan(buffer, chunk_size, stream).await
}
/// Sends a ping request to ClamAV using a TCP connection
///
/// This function establishes a TCP connection to a ClamAV server at the
/// specified `host_address` and sends a ping request to it.
///
/// # Example
///
/// ```
/// # #[async_std::main]
/// # async fn main() {
/// let clamd_available = match clamav_client::async_std::ping_tcp("localhost:3310").await {
/// Ok(ping_response) => ping_response == b"PONG\0",
/// Err(_) => false,
/// };
/// # assert!(clamd_available);
/// # }
/// ```
///
pub async fn ping_tcp<A: ToSocketAddrs>(host_address: A) -> IoResult {
let stream = TcpStream::connect(host_address).await?;
ping(stream).await
}
/// Scans a file for viruses using a TCP connection
///
/// This function reads data from a file located at the specified `path` and
/// streams it to a ClamAV server through a TCP connection for scanning.
///
/// # Arguments
///
/// * `file_path`: The path to the file to be scanned
/// * `host_address`: The address (host and port) of the ClamAV server
/// * `chunk_size`: An optional chunk size for reading data. If `None`, a default chunk size is used
///
/// # Returns
///
/// An `IoResult` containing the server's response as a vector of bytes
///
pub async fn scan_file_tcp<P: AsRef<Path>, A: ToSocketAddrs>(
file_path: P,
host_address: A,
chunk_size: Option<usize>,
) -> IoResult {
let file = File::open(file_path).await?;
let stream = TcpStream::connect(host_address).await?;
scan(file, chunk_size, stream).await
}
/// Scans a data buffer for viruses using a TCP connection
///
/// This function streams the provided `buffer` data to a ClamAV server through
/// a TCP connection for scanning.
///
/// # Arguments
///
/// * `buffer`: The data to be scanned
/// * `host_address`: The address (host and port) of the ClamAV server
/// * `chunk_size`: An optional chunk size for reading data. If `None`, a default chunk size is used
///
/// # Returns
///
/// An `IoResult` containing the server's response as a vector of bytes
///
pub async fn scan_buffer_tcp<A: ToSocketAddrs>(
buffer: &[u8],
host_address: A,
chunk_size: Option<usize>,
) -> IoResult {
let stream = TcpStream::connect(host_address).await?;
scan(buffer, chunk_size, stream).await
}