pub struct ClamClient { /* private fields */ }Expand description
ClamClient is the crux of the crate, it retains information about what socket to connect
to, thus that it can reconnect, and what timeout (if any) to use when connecting.
Note: Future versions may move timeout to be use in command operations as well as
when connecting. However since the latter is so variable, this may require a different - or even
per call - timeout value.
Implementations§
Source§impl ClamClient
impl ClamClient
Sourcepub fn new(ip: &str, port: u16) -> ClamResult<ClamClient>
pub fn new(ip: &str, port: u16) -> ClamResult<ClamClient>
Creates a new instance of ClamClient with no connect timeout, commands issued from this
client will indefinitely block if ClamD becomes unavailable.
Arguments
ip: The IP address to connect toport: The port to connect to
Example
extern crate clam_client;
use clam_client::client::ClamClient;
fn main() {
if let Ok(client) = ClamClient::new("127.0.0.1", 3310) {
println!("{:?}", client.version());
}
}Examples found in repository?
More examples
7fn main() {
8 if let Some(path) = env::args().nth(1) {
9 let client = ClamClient::new("127.0.0.1", 3310).unwrap();
10 println!("Scanning: {}", path);
11 if let Ok(results) = client.scan_path(&path, true) {
12 for result in results.iter() {
13 match result {
14 ClamScanResult::Ok => println!("File {} is OK!", path),
15 ClamScanResult::Found(location, virus) => {
16 println!("\tFound virus: '{}' in {}", virus, location)
17 }
18 ClamScanResult::Error(err) => println!("Received error from ClamAV: {}", err),
19 }
20 }
21 }
22 } else {
23 println!("USAGE: cargo run --example simple \"<file_path>\"");
24 }
25}8fn main() {
9 if let Some(path) = env::args().nth(1) {
10 let client = ClamClient::new("127.0.0.1", 3310).unwrap();
11 let file = File::open(&path).unwrap();
12
13 match client.scan_stream(file) {
14 Ok(result) => match result {
15 ClamScanResult::Ok => println!("File {} is OK!", path),
16 ClamScanResult::Found(location, virus) => {
17 println!("Found virus: '{}' in {}", virus, location)
18 }
19 ClamScanResult::Error(err) => println!("Received error from ClamAV: {}", err),
20 },
21 Err(e) => println!("A network error occurred whilst talking to ClamAV:\n{}", e),
22 }
23 } else {
24 println!("USAGE: cargo run --example stream \"<file_path>\"");
25 }
26}Sourcepub fn new_with_timeout(
ip: &str,
port: u16,
timeout_secs: u64,
) -> ClamResult<ClamClient>
pub fn new_with_timeout( ip: &str, port: u16, timeout_secs: u64, ) -> ClamResult<ClamClient>
Creates a new instance of ClamClient with a connection timeout (in seconds). Any command
issued from this client will error after timeout_secs if ClamD is unavailable.
Arguments
ip: The IP address to connect toport: The port to connect totimeout_secs: The number of seconds to wait before aborting the connection
Example
extern crate clam_client;
use clam_client::client::ClamClient;
fn main() {
if let Ok(client) = ClamClient::new_with_timeout("127.0.0.1", 3310, 10) {
println!("{:?}", client.version());
}
}Sourcepub fn ping(&self) -> bool
pub fn ping(&self) -> bool
Implements the ClamD PING command, returns true if ClamD responds with PONG, or false if
there was an error, or ClamD did not respond with PONG.
Sourcepub fn version(&self) -> ClamResult<ClamVersion>
pub fn version(&self) -> ClamResult<ClamVersion>
Implements the ClamD VERSION conmand, returns a struct of ClamVersion if successful,
or an error if processing the respnose failed, or if there was an issue talking to ClamD.
Sourcepub fn reload(&self) -> ClamResult<String>
pub fn reload(&self) -> ClamResult<String>
Implements the ClamD RELOAD command, returns the state of the request as a String from
ClamD, or a network error if the command failed.
Sourcepub fn scan_path(
&self,
path: &str,
continue_on_virus: bool,
) -> ClamResult<Vec<ClamScanResult>>
pub fn scan_path( &self, path: &str, continue_on_virus: bool, ) -> ClamResult<Vec<ClamScanResult>>
Implements the ClamD SCAN and CONTSCAN commands, returns a Vec<ClamScanResult> if the command
was successful, or a network error if the command failed.
Arguments:
path: The path to scan, this is a path that is on the ClamD server, or that it has access to.continue_on_virus: If true, instructs ClamD to continue scanning even after it detects a virus.
Example
extern crate clam_client;
use clam_client::client::ClamClient;
use clam_client::response::ClamScanResult;
fn main() {
let client = ClamClient::new("127.0.0.1", 3310).unwrap();
if let Ok(scan_results) = client.scan_path("/tmp/", true){
for result in scan_results.iter() {
match result {
ClamScanResult::Found(location, virus) => {
println!("Found virus: '{}' in {}", virus, location)
},
_ => {},
}
}
}
}Examples found in repository?
7fn main() {
8 if let Some(path) = env::args().nth(1) {
9 let client = ClamClient::new("127.0.0.1", 3310).unwrap();
10 println!("Scanning: {}", path);
11 if let Ok(results) = client.scan_path(&path, true) {
12 for result in results.iter() {
13 match result {
14 ClamScanResult::Ok => println!("File {} is OK!", path),
15 ClamScanResult::Found(location, virus) => {
16 println!("\tFound virus: '{}' in {}", virus, location)
17 }
18 ClamScanResult::Error(err) => println!("Received error from ClamAV: {}", err),
19 }
20 }
21 }
22 } else {
23 println!("USAGE: cargo run --example simple \"<file_path>\"");
24 }
25}Sourcepub fn multiscan_path(&self, path: &str) -> ClamResult<Vec<ClamScanResult>>
pub fn multiscan_path(&self, path: &str) -> ClamResult<Vec<ClamScanResult>>
Implements the ClamD MULTISCAN command which allows the ClamD instance to perform
multi-threaded scanning. Returns a Vec<ClamScanResult> if the command wassuccessful,
or a network error if the command failed.
Sourcepub fn scan_stream<T: Read>(&self, stream: T) -> ClamResult<ClamScanResult>
pub fn scan_stream<T: Read>(&self, stream: T) -> ClamResult<ClamScanResult>
Implements the ClamD INSTREAM command, which allows the caller to stream a file to the ClamD
instance. Retuns a ClamScanResult if the command was successful.
Arguments:
stream: The object to be scanned, this must implementRead, it will be read into a buffer of 4096 bytes and then written to the ClamD instance. This object must not exceed the ClamD max stream size, else the socket will be forcibly closed - in which case an error will be reutned from this function.
Example
extern crate clam_client;
use clam_client::client::ClamClient;
use clam_client::response::ClamScanResult;
use std::fs::File;
fn main() {
let client = ClamClient::new("127.0.0.1", 3310).unwrap();
let file = File::open("/etc/hosts").unwrap();
match client.scan_stream(file) {
Ok(result) => match result {
ClamScanResult::Ok => println!("File /etc/hostname is OK!"),
ClamScanResult::Found(location, virus) => {
println!("Found virus: '{}' in {}", virus, location)
},
ClamScanResult::Error(err) => println!("Received error from ClamAV: {}", err),
},
Err(e) => println!("A network error occurred whilst talking to ClamAV:\n{}", e),
}
}Examples found in repository?
8fn main() {
9 if let Some(path) = env::args().nth(1) {
10 let client = ClamClient::new("127.0.0.1", 3310).unwrap();
11 let file = File::open(&path).unwrap();
12
13 match client.scan_stream(file) {
14 Ok(result) => match result {
15 ClamScanResult::Ok => println!("File {} is OK!", path),
16 ClamScanResult::Found(location, virus) => {
17 println!("Found virus: '{}' in {}", virus, location)
18 }
19 ClamScanResult::Error(err) => println!("Received error from ClamAV: {}", err),
20 },
21 Err(e) => println!("A network error occurred whilst talking to ClamAV:\n{}", e),
22 }
23 } else {
24 println!("USAGE: cargo run --example stream \"<file_path>\"");
25 }
26}Sourcepub fn stats(&self) -> ClamResult<ClamStats>
pub fn stats(&self) -> ClamResult<ClamStats>
Implements the ClamD STATS command, and returns a struct of ClamStats.
Sourcepub fn shutdown(self) -> ClamResult<String>
pub fn shutdown(self) -> ClamResult<String>
Implements the ClamD SHUTDOWN command, and returns the status message - if any -
from ClamD.
Note: Since this shuts down the ClamD instance, it will ensure all future calls to
this or any other ClamClient return errors, as such, thus function consumes the calling client.