Skip to content

Commit

Permalink
Add shutdown command
Browse files Browse the repository at this point in the history
  • Loading branch information
toblux committed Mar 21, 2024
1 parent 57663b0 commit a59f666
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/async_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use async_trait::async_trait;
#[cfg(unix)]
use async_std::os::unix::net::UnixStream;

use super::{IoResult, DEFAULT_CHUNK_SIZE, END_OF_STREAM, INSTREAM, PING, PONG, VERSION};
use super::{IoResult, DEFAULT_CHUNK_SIZE, END_OF_STREAM, INSTREAM, PING, PONG, SHUTDOWN, VERSION};

async fn send_command<RW: ReadExt + WriteExt + Unpin>(
mut stream: RW,
Expand Down Expand Up @@ -473,3 +473,22 @@ pub async fn scan_stream<
let output_stream = connection.connect().await?;
_scan_stream(input_stream, chunk_size, output_stream).await
}

/// Shuts down a ClamAV server
///
/// This function establishes a connection to a ClamAV server and sends the
/// SHUTDOWN command to it. If the server is available, it will perform a clean
/// exit and shut itself down. The response will be empty.
///
/// # Arguments
///
/// * `connection`: The connection type to use - either TCP or a Unix socket connection
///
/// # Returns
///
/// An [`IoResult`] containing the server's response
///
pub async fn shutdown<T: TransportProtocol>(connection: T) -> IoResult {
let stream = connection.connect().await?;
send_command(stream, SHUTDOWN, None).await
}
20 changes: 20 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const DEFAULT_CHUNK_SIZE: usize = 4096;
/// ClamAV commands
const PING: &[u8; 6] = b"zPING\0";
const VERSION: &[u8; 9] = b"zVERSION\0";
const SHUTDOWN: &[u8; 10] = b"zSHUTDOWN\0";
const INSTREAM: &[u8; 10] = b"zINSTREAM\0";
const END_OF_STREAM: &[u8; 4] = &[0, 0, 0, 0];

Expand Down Expand Up @@ -467,3 +468,22 @@ pub fn scan_buffer<T: TransportProtocol>(
let stream = connection.connect()?;
scan(buffer, chunk_size, stream)
}

/// Shuts down a ClamAV server
///
/// This function establishes a connection to a ClamAV server and sends the
/// SHUTDOWN command to it. If the server is available, it will perform a clean
/// exit and shut itself down. The response will be empty.
///
/// # Arguments
///
/// * `connection`: The connection type to use - either TCP or a Unix socket connection
///
/// # Returns
///
/// An [`IoResult`] containing the server's response
///
pub fn shutdown<T: TransportProtocol>(connection: T) -> IoResult {
let stream = connection.connect()?;
send_command(stream, SHUTDOWN, None)
}
21 changes: 20 additions & 1 deletion src/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tokio::net::UnixStream;
#[cfg(feature = "tokio-stream")]
use tokio_stream::{Stream, StreamExt};

use super::{IoResult, DEFAULT_CHUNK_SIZE, END_OF_STREAM, INSTREAM, PING, PONG, VERSION};
use super::{IoResult, DEFAULT_CHUNK_SIZE, END_OF_STREAM, INSTREAM, PING, PONG, SHUTDOWN, VERSION};

async fn send_command<RW: AsyncRead + AsyncWrite + Unpin>(
mut stream: RW,
Expand Down Expand Up @@ -486,3 +486,22 @@ pub async fn scan_stream<
let output_stream = connection.connect().await?;
_scan_stream(input_stream, chunk_size, output_stream).await
}

/// Shuts down a ClamAV server
///
/// This function establishes a connection to a ClamAV server and sends the
/// SHUTDOWN command to it. If the server is available, it will perform a clean
/// exit and shut itself down. The response will be empty.
///
/// # Arguments
///
/// * `connection`: The connection type to use - either TCP or a Unix socket connection
///
/// # Returns
///
/// An [`IoResult`] containing the server's response
///
pub async fn shutdown<T: TransportProtocol>(connection: T) -> IoResult {
let stream = connection.connect().await?;
send_command(stream, SHUTDOWN, None).await
}

0 comments on commit a59f666

Please sign in to comment.