Skip to content

Commit

Permalink
Add version command
Browse files Browse the repository at this point in the history
  • Loading branch information
toblux committed Feb 28, 2024
1 parent beabba8 commit afd96a1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
39 changes: 35 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,38 @@ const DEFAULT_CHUNK_SIZE: usize = 4096;

/// ClamAV commands
const PING: &[u8; 6] = b"zPING\0";
const VERSION: &[u8; 9] = b"zVERSION\0";
const INSTREAM: &[u8; 10] = b"zINSTREAM\0";
const END_OF_STREAM: &[u8; 4] = &[0, 0, 0, 0];

/// ClamAV responses
pub const PONG: &[u8; 5] = b"PONG\0";

fn ping<RW: Read + Write>(mut stream: RW) -> IoResult {
stream.write_all(PING)?;
fn send_command<RW: Read + Write>(
mut stream: RW,
command: &[u8],
expected_response_length: Option<usize>,
) -> IoResult {
stream.write_all(command)?;
stream.flush()?;

let capacity = PONG.len();
let mut response = Vec::with_capacity(capacity);
let mut response = match expected_response_length {
Some(len) => Vec::with_capacity(len),
None => Vec::new(),
};

stream.read_to_end(&mut response)?;
Ok(response)
}

fn ping<RW: Read + Write>(stream: RW) -> IoResult {
send_command(stream, PING, Some(PONG.len()))
}

fn get_clamav_version<RW: Read + Write>(stream: RW) -> IoResult {
send_command(stream, VERSION, None)
}

fn scan<R: Read, RW: Read + Write>(
mut input: R,
chunk_size: Option<usize>,
Expand Down Expand Up @@ -96,6 +112,15 @@ pub fn ping_socket<P: AsRef<Path>>(socket_path: P) -> IoResult {
ping(stream)
}

/// TODO
#[cfg(unix)]
pub fn get_clamav_version_socket<P: AsRef<Path>>(socket_path: P) -> IoResult {
use std::os::unix::net::UnixStream;

let stream = UnixStream::connect(socket_path)?;
get_clamav_version(stream)
}

/// Scans a file for viruses using a Unix socket connection
///
/// This function reads data from a file located at the specified `path` and
Expand Down Expand Up @@ -171,6 +196,12 @@ pub fn ping_tcp<A: ToSocketAddrs>(host_address: A) -> IoResult {
ping(stream)
}

/// TODO
pub fn get_clamav_version_tcp<A: ToSocketAddrs>(host_address: A) -> IoResult {
let stream = TcpStream::connect(host_address)?;
get_clamav_version(stream)
}

/// Scans a file for viruses using a TCP connection
///
/// This function reads data from a file located at the specified `path` and
Expand Down
21 changes: 21 additions & 0 deletions tests/clamav_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ fn ping_socket() {
assert_eq!(&response, clamav_client::PONG);
}

#[test]
#[cfg(unix)]
fn get_clamav_version_socket() {
let err_msg = format!(
"Could not get ClamAV version via Unix socket at {}",
TEST_SOCKET_PATH
);
let response = clamav_client::get_clamav_version_socket(TEST_SOCKET_PATH).expect(&err_msg);
assert!(&response.starts_with(b"ClamAV"));
}

#[test]
#[cfg(unix)]
fn scan_socket_infected_file() {
Expand Down Expand Up @@ -86,6 +97,16 @@ fn ping_tcp() {
assert_eq!(&response, clamav_client::PONG);
}

#[test]
fn get_clamav_version_tcp() {
let err_msg = format!(
"Could not get ClamAV version via TCP at {}",
TEST_HOST_ADDRESS
);
let response = clamav_client::get_clamav_version_tcp(TEST_HOST_ADDRESS).expect(&err_msg);
assert!(&response.starts_with(b"ClamAV"));
}

#[test]
fn scan_tcp_infected_file() {
let err_msg = format!(
Expand Down

0 comments on commit afd96a1

Please sign in to comment.