docs/ref/modules/utils/sync-protocol/api-reference.md
The Agent Sync Protocol exposes APIs through both C++ and C interfaces, allowing modules written in different languages to integrate seamlessly. The protocol manages the complete synchronization lifecycle, from persisting differences to handling manager responses.
#include "agent_sync_protocol.hpp"
#include "iagent_sync_protocol.hpp"
#include "agent_sync_protocol_types.hpp"
AgentSyncProtocolImplements the IAgentSyncProtocol interface for synchronization operations.
AgentSyncProtocol(const std::string& moduleName,
const std::string& dbPath,
MQ_Functions mqFuncs,
LoggerFunc logger,
std::shared_ptr<IPersistentQueue> queue = nullptr)
Parameters:
moduleName: Unique identifier for the module (e.g., "FIM", "SCA", "Inventory")dbPath: Full path to the SQLite database file for persistent storagemqFuncs: Structure containing message queue function pointerslogger: Callback function for logging messagesqueue: Optional custom persistent queue implementationpersistDifference()void persistDifference(const std::string& id,
Operation operation,
const std::string& index,
const std::string& data)
Persists a data to the internal queue for later synchronization.
Parameters:
id: Unique identifier for the data source (typically a hash of primary keys, e.g., file-path)operation: Type of operation (Operation::CREATE, Operation::UPDATE, Operation::DELETE)index: Target index or destination for the datadata: JSON string containing the difference dataExample:
protocol.persistDifference(
"abc123def456",
Operation::CREATE,
"fim_events",
"{\"path\": \"/etc/passwd\", \"hash\": \"...\", \"timestamp\": 1234567890}"
);
persistDifferenceInMemory()void persistDifferenceInMemory(const std::string& id,
Operation operation,
const std::string& index,
const std::string& data)
Persists a difference to in-memory vector instead of database. This method is used for recovery scenarios where data should be kept in memory.
Parameters:
id: Unique identifier for the data itemoperation: Type of operation (Operation::CREATE, Operation::UPDATE, Operation::DELETE)index: Logical index for the data itemdata: Serialized content of the messagesynchronizeModule()bool synchronizeModule(Mode mode,
std::chrono::seconds timeout,
unsigned int retries,
size_t maxEps)
Initiates a synchronization session with the manager.
Parameters:
mode: Synchronization mode (Mode::Full or Mode::Delta)timeout: Maximum time to wait for each responseretries: Number of retry attempts for Start and End messagesmaxEps: Maximum events per second (0 = unlimited)Returns: true if synchronization completed successfully, false otherwise
Example:
bool success = protocol.synchronizeModule(
Mode::Delta,
std::chrono::seconds(30),
3,
1000
);
requiresFullSync()bool requiresFullSync(const std::string& index,
const std::string& checksum,
std::chrono::seconds timeout,
unsigned int retries,
size_t maxEps)
Checks if a module index requires full synchronization by verifying the checksum with the manager.
Parameters:
index: The index/table to checkchecksum: The calculated checksum for the indextimeout: Maximum time to wait for each responseretries: Number of retry attemptsmaxEps: Maximum events per second (0 = unlimited)Returns: true if full sync is required (checksum mismatch); false if integrity is valid or connection error.
clearInMemoryData()void clearInMemoryData()
Clears the in-memory data queue. This method removes all entries from the in-memory vector used for recovery scenarios.
synchronizeMetadataOrGroups()bool synchronizeMetadataOrGroups(Mode mode,
std::chrono::seconds timeout,
unsigned int retries,
size_t maxEps,
uint64_t globalVersion)
Synchronizes metadata or groups with the server without sending data. This method handles the following modes: MetadataDelta, MetadataCheck, GroupDelta, GroupCheck. The sequence is: Start → StartAck → End → EndAck (no Data messages).
Parameters:
mode: Synchronization mode (must be Mode::MetadataDelta, Mode::MetadataCheck, Mode::GroupDelta, or Mode::GroupCheck)timeout: Timeout duration for waiting for server responsesretries: Number of retry attempts for each messagemaxEps: Maximum events per second (0 = unlimited)globalVersion: Global version to include in the Start message.Returns: true if synchronization completed successfully, false otherwise
notifyDataClean()bool notifyDataClean(const std::vector<std::string>& indices,
std::chrono::seconds timeout,
unsigned int retries,
size_t maxEps)
Notifies the manager about data cleaning for specified indices. This method sends DataClean messages for each index in the provided vector. The sequence is: Start → StartAck → DataClean (for each index) → End → EndAck. Upon receiving Ok, it clears the local database and returns true.
Parameters:
indices: Vector of index names to cleantimeout: Timeout duration for waiting for server responsesretries: Number of retry attempts for each messagemaxEps: Maximum events per second (0 = unlimited)Returns: true if notification completed successfully and database was cleared, false otherwise
Example:
std::vector<std::string> indices = {"fim_files", "fim_registry"};
bool success = protocol.notifyDataClean(
indices,
std::chrono::seconds(30),
3,
1000
);
deleteDatabase()void deleteDatabase()
Deletes the database file. This method closes the database connection and removes the database file from disk.
Example:
protocol.deleteDatabase();
parseResponseBuffer()bool parseResponseBuffer(const uint8_t* data, size_t length)
Processes FlatBuffer-encoded responses from the manager.
Parameters:
data: Pointer to the FlatBuffer messagelength: Size of the message in bytesReturns: true if message was successfully parsed and processed
#include "agent_sync_protocol_c_interface.h"
#include "agent_sync_protocol_c_interface_types.h"
asp_create()AgentSyncProtocolHandle* asp_create(const char* module,
const char* db_path,
const MQ_Functions* mq_funcs,
asp_logger_t logger)
Creates a new Agent Sync Protocol instance.
Parameters:
module: Module name stringdb_path: Database file pathmq_funcs: Pointer to message queue functions structurelogger: Logging callback functionReturns: Opaque handle to the protocol instance, or NULL on failure
asp_destroy()void asp_destroy(AgentSyncProtocolHandle* handle)
Destroys a protocol instance and releases resources.
Parameters:
handle: Protocol handle to destroyasp_persist_diff()void asp_persist_diff(AgentSyncProtocolHandle* handle,
const char* id,
Operation_t operation,
const char* index,
const char* data)
C wrapper for persistDifference().
Parameters:
handle: Protocol handleid: Data source identifieroperation: Operation type (OPERATION_CREATE, OPERATION_MODIFY, OPERATION_DELETE, OPERATION_NO_OP)index: Target indexdata: JSON data stringasp_persist_diff_in_memory()void asp_persist_diff_in_memory(AgentSyncProtocolHandle* handle,
const char* id,
Operation_t operation,
const char* index,
const char* data)
C wrapper for persistDifferenceInMemory(). Persists a difference to in-memory vector instead of database.
Parameters:
handle: Protocol handleid: Unique identifier for the data itemoperation: Operation type (OPERATION_CREATE, OPERATION_MODIFY, OPERATION_DELETE, OPERATION_NO_OP)index: Logical index for the data itemdata: Serialized content of the messageasp_sync_module()bool asp_sync_module(AgentSyncProtocolHandle* handle,
Mode_t mode,
unsigned int sync_timeout,
unsigned int sync_retries,
size_t max_eps)
C wrapper for synchronizeModule().
Parameters:
handle: Protocol handlemode: Sync mode (MODE_FULL or MODE_DELTA)sync_timeout: Timeout in secondssync_retries: Number of retriesmax_eps: Maximum events per secondReturns: true on success, false on failure
asp_requires_full_sync()bool asp_requires_full_sync(AgentSyncProtocolHandle* handle,
const char* index,
const char* checksum,
unsigned int sync_timeout,
unsigned int sync_retries,
size_t max_eps)
C wrapper for requiresFullSync(). Checks if a module index requires full synchronization.
Parameters:
handle: Protocol handleindex: The index/table to checkchecksum: The calculated checksum for the indexsync_timeout: Timeout in secondssync_retries: Number of retriesmax_eps: Maximum events per secondReturns: true if full sync is required (checksum mismatch); false if integrity is valid
asp_clear_in_memory_data()void asp_clear_in_memory_data(AgentSyncProtocolHandle* handle)
C wrapper for clearInMemoryData(). Clears the in-memory data queue.
Parameters:
handle: Protocol handleasp_sync_metadata_or_groups()bool asp_sync_metadata_or_groups(AgentSyncProtocolHandle* handle,
Mode_t mode,
unsigned int sync_timeout,
unsigned int sync_retries,
size_t max_eps,
uint64_t global_version)
C wrapper for synchronizeMetadataOrGroups(). Synchronizes metadata or groups with the server without sending data.
Parameters:
handle: Protocol handlemode: Sync mode (MODE_METADATA_DELTA, MODE_METADATA_CHECK, MODE_GROUP_DELTA, or MODE_GROUP_CHECK)sync_timeout: Timeout in secondssync_retries: Number of retriesmax_eps: Maximum events per secondglobal_version: Global version to include in the Start message.Returns: true on success, false on failure
asp_notify_data_clean()bool asp_notify_data_clean(AgentSyncProtocolHandle* handle,
const char** indices,
size_t indices_count,
unsigned int sync_timeout,
unsigned int sync_retries,
size_t max_eps)
C wrapper for notifyDataClean(). Notifies the manager about data cleaning for specified indices. This function sends DataClean messages for each index in the provided array. The sequence is: Start → StartAck → DataClean (for each index) → End → EndAck. Upon receiving Ok, it clears the local database and returns true.
Parameters:
handle: Protocol handleindices: Array of index name strings to cleanindices_count: Number of indices in the arraysync_timeout: Timeout in secondssync_retries: Number of retriesmax_eps: Maximum events per secondReturns: true if notification completed successfully and database was cleared, false otherwise
Example:
const char* indices[] = {"fim_files", "fim_registry"};
bool success = asp_notify_data_clean(
handle,
indices,
2,
30,
3,
1000
);
asp_delete_database()void asp_delete_database(AgentSyncProtocolHandle* handle)
C wrapper for deleteDatabase(). Deletes the database file. This function closes the database connection and removes the database file from disk.
Parameters:
handle: Protocol handleExample:
asp_delete_database(handle);
asp_parse_response_buffer()bool asp_parse_response_buffer(AgentSyncProtocolHandle* handle,
const uint8_t* data,
size_t length)
C wrapper for parseResponseBuffer().
Operation / Operation_tenum class Operation {
CREATE,
UPDATE,
DELETE
};
typedef enum {
OPERATION_CREATE = 0,
OPERATION_MODIFY = 1,
OPERATION_DELETE = 2,
OPERATION_NO_OP = 3
} Operation_t;
Mode / Mode_tenum class Mode {
FULL, // Full synchronization mode
DELTA, // Delta synchronization mode
CHECK, // Integrity check mode
METADATA_DELTA, // Metadata delta synchronization mode
METADATA_CHECK, // Metadata integrity check mode
GROUP_DELTA, // Group delta synchronization mode
GROUP_CHECK // Group integrity check mode
};
typedef enum {
MODE_FULL,
MODE_DELTA,
MODE_CHECK,
MODE_METADATA_DELTA,
MODE_METADATA_CHECK,
MODE_GROUP_DELTA,
MODE_GROUP_CHECK
} Mode_t;
using LoggerFunc = std::function<void(int level, const std::string& message)>;
typedef void (*asp_logger_t)(modules_log_level_t level, const char* message);
Where modules_log_level_t is an enumeration for logging levels (typically defined in logging_helper.h).
typedef struct MQ_Functions {
mq_start_fn start;
mq_send_binary_fn send_binary;
} MQ_Functions;
Where the function pointers are defined as:
typedef int (*mq_start_fn)(const char* key, short type, short attempts);
typedef int (*mq_send_binary_fn)(int queue, const void* message, size_t message_len,
const char* locmsg, char loc);
Function Parameters:
mq_start_fn:
key: The identifier key for the message queuetype: The type of queue or messageattempts: The number of connection attemptsmq_send_binary_fn:
queue: The queue identifiermessage: The message payload to sendmessage_len: The length of the message payload in byteslocmsg: Additional location/context message (optional)loc: A character representing the message location or typeThe protocol uses logging callbacks to report errors. Common error scenarios include:
Errors are logged with appropriate severity levels:
0: Debug1: Info2: Warning3: ErrorThe Agent Sync Protocol is designed to be thread-safe:
persistDifference() concurrentlysynchronizeModule()) should be active at a timeparseResponseBuffer()) is synchronized internallyasp_destroy() to release resources