fluffici_clamav_client/
lib.rs

1use std::{
2    fs::File,
3    io::{Error, Read, Write},
4    net::{TcpStream, ToSocketAddrs},
5    path::Path,
6    result::Result,
7    str::{from_utf8, Utf8Error},
8};
9
10type IoResult = Result<Vec<u8>, Error>;
11type Utf8Result = Result<bool, Utf8Error>;
12
13const DEFAULT_CHUNK_SIZE: u32 = 4096; // 4 kibibytes
14
15fn ping<RW>(mut stream: RW) -> IoResult
16where
17    RW: Read + Write,
18{
19    stream.write_all(b"zPING\0")?;
20
21    let capacity = b"PONG\0".len();
22    let mut response = Vec::with_capacity(capacity);
23    stream.read_to_end(&mut response)?;
24    Ok(response)
25}
26
27fn scan<P, RW>(file_path: P, chunk_size: Option<u32>, mut stream: RW) -> IoResult
28where
29    P: AsRef<Path>,
30    RW: Read + Write,
31{
32    stream.write_all(b"zINSTREAM\0")?;
33
34    let chunk_size = chunk_size.unwrap_or(DEFAULT_CHUNK_SIZE);
35    let mut file = File::open(file_path)?;
36    let mut buffer = vec![0; chunk_size as usize];
37    loop {
38        let len = file.read(&mut buffer[..])?;
39        if len != 0 {
40            stream.write_all(&(len as u32).to_be_bytes())?;
41            stream.write_all(&buffer[0..len])?;
42        } else {
43            stream.write_all(&[0; 4])?;
44            break;
45        }
46    }
47
48    let mut response = Vec::new();
49    stream.read_to_end(&mut response)?;
50    Ok(response)
51}
52
53fn scan_buffer<RW>(buffer: &[u8], mut stream: RW, chunk_size: Option<u32>) -> IoResult
54where
55    RW: Read + Write,
56{
57    stream.write_all(b"zINSTREAM\0")?;
58
59    let chunk_size = chunk_size.unwrap_or(DEFAULT_CHUNK_SIZE) as usize;
60    for chunk in buffer.chunks(chunk_size) {
61        stream.write_all(&(chunk.len() as u32).to_be_bytes())?;
62        stream.write_all(&chunk[..])?;
63    }
64
65    stream.write_all(&[0; 4])?;
66
67    let mut response = Vec::new();
68    stream.read_to_end(&mut response)?;
69    Ok(response)
70}
71
72#[cfg(target_family = "unix")]
73pub fn ping_socket<P>(socket_path: P) -> IoResult
74where
75    P: AsRef<Path>,
76{
77    use std::os::unix::net::UnixStream;
78
79    let stream = UnixStream::connect(socket_path)?;
80    ping(stream)
81}
82
83#[cfg(target_family = "unix")]
84pub fn scan_socket<P>(file_path: P, socket_path: P, chunk_size: Option<u32>) -> IoResult
85where
86    P: AsRef<Path>,
87{
88    use std::os::unix::net::UnixStream;
89
90    let stream = UnixStream::connect(socket_path)?;
91    scan(file_path, chunk_size, stream)
92}
93
94#[cfg(target_family = "unix")]
95pub fn scan_buffer_socket<P>(buffer: &[u8], socket_path: P, chunk_size: Option<u32>) -> IoResult
96where
97    P: AsRef<Path>,
98{
99    use std::os::unix::net::UnixStream;
100
101    let stream = UnixStream::connect(socket_path)?;
102    scan_buffer(buffer, stream, chunk_size)
103}
104
105pub fn ping_tcp<A>(host_address: A) -> IoResult
106where
107    A: ToSocketAddrs,
108{
109    let stream = TcpStream::connect(host_address)?;
110    ping(stream)
111}
112
113pub fn scan_tcp<P, A>(file_path: P, host_address: A, chunk_size: Option<u32>) -> IoResult
114where
115    A: ToSocketAddrs,
116    P: AsRef<Path>,
117{
118    let stream = TcpStream::connect(host_address)?;
119    scan(file_path, chunk_size, stream)
120}
121
122pub fn scan_buffer_tcp<A>(buffer: &[u8], host_address: A, chunk_size: Option<u32>) -> IoResult
123where
124    A: ToSocketAddrs,
125{
126    let stream = TcpStream::connect(host_address)?;
127    scan_buffer(buffer, stream, chunk_size)
128}
129
130pub fn clean(response: &[u8]) -> Utf8Result {
131    let response = from_utf8(response)?;
132    Ok(response.contains("OK") && !response.contains("FOUND"))
133}