Quick Start
Dart client for the ClamAV daemon (clamd) over TCP or Unix domain sockets.
- Talks directly to
clamdusing its native protocol - Supports
PING,VERSION,RELOAD,SHUTDOWN,INSTREAM - Designed for server-side Dart / CLI / Flutter mobile/desktop
TCP (Network)
import 'package:clamav/clamav.dart';
Future<void> main() async {
// ClamAV clamd TCP socket (e.g. port 3310)
final clam = Network('localhost', 3310);
final isAlive = await clam.ping();
print('ClamAV alive: $isAlive');
final version = await clam.version();
print('ClamAV version: $version');
final isClean = await clam.fileScan('/path/to/file.dmg');
print('File clean: $isClean');
await clam.reload(); // optional
await clam.shutdown(); // optional, typically not used in production
}
Unix Domain Socket (Pipe)
import 'package:clamav/clamav.dart';
Future<void> main() async {
// Example socket path (Linux):
// /var/run/clamav/clamd.ctl or /run/clamav/clamd.ctl
final clam = Pipe('/var/run/clamav/clamd.sock');
final isAlive = await clam.ping();
print('ClamAV alive: $isAlive');
final version = await clam.version();
print('ClamAV version: $version');
final isClean = await clam.fileScan('/path/to/file.dmg');
print('File clean: $isClean');
}
Library API
Entry point:
import 'package:clamav/clamav.dart';
Exported types:
Network– TCP clientPipe– Unix domain socket clientClamAVClient– abstract base classClamAVException– error type
Network
class Network extends ClamAVClient {
Network(
String host,
int port, {
Duration? timeout,
int chunkSize = ClamAVClient.defaultChunkSize,
});
}
Parameters:
host:clamdhostport:clamdTCP port (commonly3310)timeout: connect timeout (default: 5 seconds)chunkSize:INSTREAMchunk size in bytes (default: 16 KB)
Example:
final clam = Network('localhost', 3310);
Pipe
class Pipe extends ClamAVClient {
Pipe(
String socketPath, {
Duration? timeout,
int chunkSize = ClamAVClient.defaultChunkSize,
});
}
Parameters:
socketPath: Unix domain socket pathtimeout/chunkSize: same semantics asNetwork
Example:
final clam = Pipe('/var/run/clamav/clamd.sock');
Shared Methods (ClamAVClient)
Both Network and Pipe provide the same methods.
Future<bool> ping()
Sends a PING command.
- Returns
trueifclamdresponds withPONG - Throws
ClamAVExceptionon errors (connection, protocol, etc.)
final ok = await clam.ping();
Future<String> version()
Sends a VERSION command.
final version = await clam.version();
print(version); // e.g. "ClamAV 1.2.3/12345/Mon Jan 01 00:00:00 2024"
Future<bool> fileScan(String path)
Scans a file on disk using INSTREAM.
Return value:
true→ file is cleanfalse→ file is infected- Throws
ClamAVExceptionon errors (file not found, protocol error, etc.)
final isClean = await clam.fileScan('/tmp/upload.bin');
if (!isClean) {
// reject file, log, etc.
}
Future<bool> reload()
Sends a RELOAD command to clamd to reload its virus database.
- Returns
trueif no error is detected in the response - Throws
ClamAVExceptionon error
await clam.reload();
Future<bool> shutdown()
Sends a SHUTDOWN command to clamd.
- Mainly useful in development/test environments
- Returns
trueon success, throwsClamAVExceptionon error
await clam.shutdown();
Error Handling
All network/IO/protocol errors are wrapped in ClamAVException.
import 'package:clamav/clamav.dart';
Future<void> main() async {
final clam = Network('localhost', 3310);
try {
final clean = await clam.fileScan('/path/to/file.dmg');
print('Clean: $clean');
} on ClamAVException catch (e) {
// clamd not running, network error, protocol error, etc.
print('ClamAV error: $e');
}
}
Example error messages:
ClamAVException: Unable to connect to ClamAV server (SocketException: ...)ClamAVException: Empty response from ClamAVClamAVException: ClamAV error: stream: <details> ERROR
INSTREAM Protocol Details
fileScan uses the INSTREAM protocol of clamd:
-
Send the command:
zINSTREAM\0 -
Send the file content as a sequence of chunks
- Each chunk is prefixed by a 4-byte big-endian length
- Followed immediately by the raw chunk bytes
-
Terminate the stream with a zero-length chunk (
0x00000000) -
Read the response, which typically looks like:
stream: OKstream: <virus name> FOUNDstream: <details> ERROR
The library interprets the response as:
- Contains
FOUND→ returnsfalse(infected) - Contains
ERROR→ throwsClamAVException - Otherwise → returns
true(clean)
Requirements
-
Dart SDK:
>=2.17.0 <4.0.0 -
A running ClamAV
clamdinstance:- Listening on TCP (
TCPSocket/TCPAddrinclamd.conf), or - Listening on a Unix domain socket (
LocalSocket/LocalSocketGroup)
- Listening on TCP (
Example Docker usage:
docker run --rm -p 3310:3310 clamav/clamav:latest
Then in Dart:
final clam = Network('localhost', 3310);
or use the container hostname / IP as appropriate.
Contributing / License
This package is a Dart client for ClamAV based on the behavior of the Appwrite ecosystem.
- Use standard Dart/Flutter formatting (
dart format) - Add tests for new features
- Open issues/PRs on the repository if you encounter bugs or want to propose improvements
Check the LICENSE file in the repository for licensing details.