Struct ClamClient

Source
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

Source

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 to
  • port: 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?
examples/info.rs (line 6)
5fn main() {
6    let client = ClamClient::new("127.0.0.1", 3310).unwrap();
7    println!("ClamD info:\n\t{:?}\n", client.version().unwrap());
8    println!("ClamD stats:\n\t{:?}\n", client.stats().unwrap());
9}
More examples
Hide additional examples
examples/simple.rs (line 9)
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}
examples/stream.rs (line 10)
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}
Source

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 to
  • port: The port to connect to
  • timeout_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());
    }
}
Source

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.

Source

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.

Examples found in repository?
examples/info.rs (line 7)
5fn main() {
6    let client = ClamClient::new("127.0.0.1", 3310).unwrap();
7    println!("ClamD info:\n\t{:?}\n", client.version().unwrap());
8    println!("ClamD stats:\n\t{:?}\n", client.stats().unwrap());
9}
Source

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.

Source

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?
examples/simple.rs (line 11)
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}
Source

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.

Source

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 implement Read, 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?
examples/stream.rs (line 13)
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}
Source

pub fn stats(&self) -> ClamResult<ClamStats>

Implements the ClamD STATS command, and returns a struct of ClamStats.

Examples found in repository?
examples/info.rs (line 8)
5fn main() {
6    let client = ClamClient::new("127.0.0.1", 3310).unwrap();
7    println!("ClamD info:\n\t{:?}\n", client.version().unwrap());
8    println!("ClamD stats:\n\t{:?}\n", client.stats().unwrap());
9}
Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.