1#define _POSIX_C_SOURCE 200809L
15#include <sys/socket.h>
24static const unsigned char SYNC_MAGIC[4] = {0x43, 0x57, 0x53, 0x59};
33static int db_sync_write_all(
int fd,
const void *buf,
size_t len) {
34 const unsigned char *
p = (
const unsigned char *)buf;
37 if (
n <= 0)
return -1;
51static int read_all(
int fd,
void *buf,
size_t len) {
52 unsigned char *
p = (
unsigned char *)buf;
55 if (
n <= 0)
return -1;
143 if (db_sync_write_all(
cli_fd,
len_buf, 8) < 0 || db_sync_write_all(
cli_fd, blob, blob_len) < 0) {
164 unsigned char **
out_bytes,
size_t *out_len) {
178 fprintf(
stderr,
"[db_sync] getaddrinfo failed for %s\n", host);
183 int fd =
socket(res->ai_family, res->ai_socktype, res->ai_protocol);
189 if (
connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
198 if (db_sync_write_all(fd, SYNC_MAGIC, 4) < 0) {
205 if (read_all(fd,
len_buf, 8) < 0) {
219#ifndef CWIST_DB_SYNC_MAX_BLOB_SIZE
220#define CWIST_DB_SYNC_MAX_BLOB_SIZE ((uint64_t)4 * 1024 * 1024 * 1024)
228 unsigned char *blob = (
unsigned char *)
malloc(blob_len);
235 if (read_all(fd, blob, blob_len) < 0) {
Two-level key hierarchy for encrypting an SQLite database blob.
unsigned char * cwist_db_crypt_seal(const cwist_db_crypt_ctx_t *ctx, const unsigned char *sqlite_bytes, size_t sqlite_len, size_t *out_len)
Encrypt an SQLite database blob.
unsigned char * cwist_db_crypt_open(const cwist_db_crypt_ctx_t *ctx, const unsigned char *blob, size_t blob_len, size_t *out_len)
Decrypt a sealed blob produced by cwist_db_crypt_seal().
#define CWIST_DB_SYNC_MAX_BLOB_SIZE
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.
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.
Server-to-server SQLite database synchronisation over TCP.
#define CWIST_DB_SYNC_ERR_PROTO
#define CWIST_DB_SYNC_ERR_ARGS
#define CWIST_DB_SYNC_OK
Return codes.
#define CWIST_DB_SYNC_DEFAULT_PORT
#define CWIST_DB_SYNC_ERR_NET
#define CWIST_DB_SYNC_ERR_CRYPTO
#define CWIST_DB_SYNC_ERR_MEM