cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
db_sync.c File Reference

Minimal TCP protocol for shipping encrypted SQLite snapshots between peers. More...

#include <cwist/net/db_sync/db_sync.h>
#include <cwist/security/db_crypt/db_crypt.h>
#include <sqlite3.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
Include dependency graph for db_sync.c:

Go to the source code of this file.

Macros

#define _POSIX_C_SOURCE   200809L
 
#define CWIST_DB_SYNC_MAX_BLOB_SIZE   ((uint64_t)4 * 1024 * 1024 * 1024)
 

Functions

int cwist_db_sync_serve (sqlite3 *db, const cwist_db_crypt_ctx_t *ctx, int port)
 Serialize and encrypt a SQLite database, then serve it to one TCP client.
 
int cwist_db_sync_pull (const char *host, int port, const cwist_db_crypt_ctx_t *ctx, unsigned char **out_bytes, size_t *out_len)
 Connect to a sync server, download the encrypted snapshot, and decrypt it.
 

Detailed Description

Minimal TCP protocol for shipping encrypted SQLite snapshots between peers.

Definition in file db_sync.c.

Macro Definition Documentation

◆ _POSIX_C_SOURCE

#define _POSIX_C_SOURCE   200809L

Definition at line 1 of file db_sync.c.

◆ CWIST_DB_SYNC_MAX_BLOB_SIZE

#define CWIST_DB_SYNC_MAX_BLOB_SIZE   ((uint64_t)4 * 1024 * 1024 * 1024)

Function Documentation

◆ cwist_db_sync_pull()

int cwist_db_sync_pull ( const char *  host,
int  port,
const cwist_db_crypt_ctx_t *  ctx,
unsigned char **  out_bytes,
size_t *  out_len 
)

Connect to a sync server, download the encrypted snapshot, and decrypt it.

Pull a database from a sync server.

Parameters
hostRemote host name or address to connect to.
portRemote TCP port, or the default when <= 0.
ctxEncryption context used to decrypt the transferred blob.
out_bytesOutput pointer that receives the decrypted SQLite image.
out_lenOutput pointer that receives the decrypted byte count.
Returns
CWIST_DB_SYNC_OK on success, or a protocol/network/crypto error code.

Definition at line 163 of file db_sync.c.

References cap, cwist_db_crypt_open(), CWIST_DB_SYNC_DEFAULT_PORT, CWIST_DB_SYNC_ERR_ARGS, CWIST_DB_SYNC_ERR_CRYPTO, CWIST_DB_SYNC_ERR_MEM, CWIST_DB_SYNC_ERR_NET, CWIST_DB_SYNC_ERR_PROTO, CWIST_DB_SYNC_MAX_BLOB_SIZE, and CWIST_DB_SYNC_OK.

◆ cwist_db_sync_serve()

int cwist_db_sync_serve ( sqlite3 *  db,
const cwist_db_crypt_ctx_t *  ctx,
int  port 
)

Serialize and encrypt a SQLite database, then serve it to one TCP client.

Serve the database once: accept one connection, send the sealed blob, then close.

Parameters
dbSQLite connection whose main database should be transferred.
ctxEncryption context used to seal the serialized bytes.
portTCP port to listen on, or the default when <= 0.
Returns
CWIST_DB_SYNC_OK on success, or a protocol/network/crypto error code.

Definition at line 69 of file db_sync.c.

References cap, cwist_db_crypt_seal(), CWIST_DB_SYNC_DEFAULT_PORT, CWIST_DB_SYNC_ERR_ARGS, CWIST_DB_SYNC_ERR_CRYPTO, CWIST_DB_SYNC_ERR_MEM, CWIST_DB_SYNC_ERR_NET, CWIST_DB_SYNC_ERR_PROTO, and CWIST_DB_SYNC_OK.