Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f629ebceb7 | |||
| a968eea712 | |||
| 0bfa68fee7 |
@@ -1,23 +0,0 @@
|
||||
name: CI
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
build-and-test:
|
||||
runs-on: ubuntu-latest
|
||||
container: gitea.tap-tap.win/taptap/fastsync-ci:v5
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Configure
|
||||
run: cmake -B build -S .
|
||||
|
||||
- name: Build
|
||||
run: cmake --build build -j$(nproc)
|
||||
|
||||
- name: Unit Tests
|
||||
run: ./build/tests
|
||||
|
||||
- name: Integration Tests
|
||||
run: python3 test.py
|
||||
+4
-6
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.22)
|
||||
cmake_minimum_required(VERSION 4.1)
|
||||
|
||||
project(FastFileTransfer)
|
||||
|
||||
@@ -19,8 +19,6 @@ if(NOT ZSTD_LIBRARY)
|
||||
message(FATAL_ERROR "zstd library not found. Ensure it is in your nix-shell!")
|
||||
endif()
|
||||
|
||||
find_package(OpenSSL REQUIRED)
|
||||
|
||||
file(GLOB SHARED_SRCS "src/shared/*.c")
|
||||
file(GLOB SERVER_SRCS "src/server/*.c")
|
||||
file(GLOB CLIENT_SRCS "src/client/*.c")
|
||||
@@ -28,13 +26,13 @@ file(GLOB TEST_SRCS "tests/*.c")
|
||||
|
||||
add_executable(server ${SERVER_SRCS} ${SHARED_SRCS})
|
||||
target_include_directories(server PRIVATE src/shared src/server src/client)
|
||||
target_link_libraries(server PRIVATE Threads::Threads ${ZSTD_LIBRARY} OpenSSL::SSL OpenSSL::Crypto)
|
||||
target_link_libraries(server PRIVATE Threads::Threads ${ZSTD_LIBRARY})
|
||||
|
||||
add_executable(client ${CLIENT_SRCS} ${SHARED_SRCS})
|
||||
target_include_directories(client PRIVATE src/shared src/server src/client)
|
||||
target_link_libraries(client PRIVATE Threads::Threads ${ZSTD_LIBRARY} OpenSSL::SSL OpenSSL::Crypto)
|
||||
target_link_libraries(client PRIVATE Threads::Threads ${ZSTD_LIBRARY})
|
||||
|
||||
add_executable(tests ${TEST_SRCS} ${SHARED_SRCS} src/client/scanner.c)
|
||||
target_include_directories(tests PRIVATE tests src/shared src/server src/client)
|
||||
target_link_libraries(tests PRIVATE Threads::Threads ${ZSTD_LIBRARY} OpenSSL::SSL OpenSSL::Crypto)
|
||||
target_link_libraries(tests PRIVATE Threads::Threads ${ZSTD_LIBRARY})
|
||||
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
FROM ubuntu:24.04
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
gcc g++ make libc6-dev cmake libzstd-dev libssl-dev git ca-certificates curl && \
|
||||
curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
|
||||
apt-get install -y --no-install-recommends nodejs && \
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
@@ -8,13 +8,11 @@ pkgs.mkShell {
|
||||
cmake
|
||||
gnumake
|
||||
pkg-config
|
||||
docker
|
||||
tea
|
||||
];
|
||||
|
||||
buildInputs = with pkgs; [
|
||||
zstd
|
||||
openssl
|
||||
];
|
||||
|
||||
NIX_ENFORCE_PURITY = 0;
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#include "config.h"
|
||||
#include "log.h"
|
||||
#include "protocol.h"
|
||||
#include "transport_tls.h"
|
||||
#include "utils.h"
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
@@ -48,10 +47,6 @@ static void print_usage(void) {
|
||||
printf(" --server-host <ip> Server IP address (default: 127.0.0.1)\n");
|
||||
printf(" --server-port <n> Server port (default: 8080)\n");
|
||||
printf(" --bwlimit <KB/s> Bandwidth limit in kilobytes per second\n");
|
||||
printf(" --tls Enable TLS encryption\n");
|
||||
printf(" --cert <path> TLS certificate file (PEM)\n");
|
||||
printf(" --key <path> TLS private key file (PEM)\n");
|
||||
printf(" --ca <path> TLS CA certificate file (PEM)\n");
|
||||
printf(" --help Show this help\n");
|
||||
}
|
||||
|
||||
@@ -159,17 +154,6 @@ int main(int argc, char *argv[]) {
|
||||
unsigned long long val = strtoull(argv[++i], NULL, 10);
|
||||
if (val > 0)
|
||||
config->chunk_size = val;
|
||||
} else if (strcmp(argv[i], "--tls") == 0) {
|
||||
config->use_tls = true;
|
||||
} else if (strcmp(argv[i], "--cert") == 0 && i + 1 < argc) {
|
||||
free(config->tls_cert);
|
||||
config->tls_cert = str_dup(argv[++i]);
|
||||
} else if (strcmp(argv[i], "--key") == 0 && i + 1 < argc) {
|
||||
free(config->tls_key);
|
||||
config->tls_key = str_dup(argv[++i]);
|
||||
} else if (strcmp(argv[i], "--ca") == 0 && i + 1 < argc) {
|
||||
free(config->tls_ca);
|
||||
config->tls_ca = str_dup(argv[++i]);
|
||||
} else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) {
|
||||
set_log_level(LOG_LEVEL_DEBUG);
|
||||
} else if (argv[i][0] == '-') {
|
||||
@@ -231,14 +215,6 @@ int main(int argc, char *argv[]) {
|
||||
config->use_metadata = true;
|
||||
}
|
||||
|
||||
if (config->use_tls) {
|
||||
if (!config->tls_cert || !config->tls_key) {
|
||||
fprintf(stderr, "Error: --tls requires --cert and --key\n");
|
||||
return 1;
|
||||
}
|
||||
tls_global_init();
|
||||
}
|
||||
|
||||
if (config->use_multithreading)
|
||||
return send_files_multithreaded(config);
|
||||
return send_files(config);
|
||||
|
||||
@@ -11,7 +11,6 @@
|
||||
#include "scanner.h"
|
||||
#include "transport_tcp.h"
|
||||
#include "transport_ssh.h"
|
||||
#include "transport_tls.h"
|
||||
#include "utils.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -58,11 +57,11 @@ int send_chunk(Client *client, Chunk *chunk, Config *config) {
|
||||
int rc = incremental_check(client, chunk->items[i]);
|
||||
if (rc < 0) return -1;
|
||||
if (rc > 0) continue;
|
||||
if (!file_send_sendfile(chunk->items[i], client->file_descriptor, config->use_metadata, false))
|
||||
if (!file_send_sendfile_no_path(chunk->items[i], client->file_descriptor, config->use_metadata))
|
||||
return -1;
|
||||
} else {
|
||||
if (!send_status(client->file_descriptor, STATUS_NEXT)) return -1;
|
||||
if (!file_send_sendfile(chunk->items[i], client->file_descriptor, config->use_metadata, true))
|
||||
if (!file_send_sendfile(chunk->items[i], client->file_descriptor, config->use_metadata))
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -72,17 +71,15 @@ int send_chunk(Client *client, Chunk *chunk, Config *config) {
|
||||
int rc = incremental_check(client, chunk->items[i]);
|
||||
if (rc < 0) return -1;
|
||||
if (rc > 0) continue;
|
||||
if (!file_send_single_calls(chunk->items[i], client->file_descriptor,
|
||||
if (!file_send_single_calls_no_path(chunk->items[i], client->file_descriptor,
|
||||
config->use_metadata,
|
||||
config->use_compression ? config->compression_level : 0,
|
||||
false))
|
||||
config->use_compression ? config->compression_level : 0))
|
||||
return -1;
|
||||
} else {
|
||||
if (!send_status(client->file_descriptor, STATUS_NEXT)) return -1;
|
||||
if (!file_send_single_calls(chunk->items[i], client->file_descriptor,
|
||||
config->use_metadata,
|
||||
config->use_compression ? config->compression_level : 0,
|
||||
true))
|
||||
config->use_compression ? config->compression_level : 0))
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@@ -99,16 +96,6 @@ static int send_chunks_multithreaded(void *pipeline_context) {
|
||||
return 1;
|
||||
}
|
||||
client = client_connect_ssh(context->config->ssh_destination, context->config->ssh_port);
|
||||
} else if (context->config->use_tls) {
|
||||
client = client_create();
|
||||
if (!client || !client_connect_tls(client, server_host, server_port,
|
||||
context->config->tls_cert,
|
||||
context->config->tls_key,
|
||||
context->config->tls_ca)) {
|
||||
if (client) client_delete(client);
|
||||
fprintf(stderr, "Error: could not connect to server via TLS\n");
|
||||
return thrd_error;
|
||||
}
|
||||
} else {
|
||||
client = client_create();
|
||||
if (!client || !client_connect(client, server_host, server_port)) {
|
||||
@@ -253,15 +240,6 @@ int send_files(Config *config) {
|
||||
}
|
||||
client = client_connect_ssh(config->ssh_destination, config->ssh_port);
|
||||
if (!client) return 1;
|
||||
} else if (config->use_tls) {
|
||||
client = client_create();
|
||||
if (!client || !client_connect_tls(client, server_host, server_port,
|
||||
config->tls_cert, config->tls_key,
|
||||
config->tls_ca)) {
|
||||
if (client) client_delete(client);
|
||||
fprintf(stderr, "Error: could not connect to server via TLS\n");
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
client = client_create();
|
||||
if (!client || !client_connect(client, server_host, server_port)) {
|
||||
|
||||
+121
-94
@@ -1,72 +1,163 @@
|
||||
#include "array_list.h"
|
||||
#include "chunk.h"
|
||||
#include "compression.h"
|
||||
#include "config.h"
|
||||
#include "data.h"
|
||||
#include "file.h"
|
||||
#include "log.h"
|
||||
#include "metadata.h"
|
||||
#include "multiprocessing.h"
|
||||
#include "protocol.h"
|
||||
#include "queue.h"
|
||||
#include "transport_tcp.h"
|
||||
#include "transport_tls.h"
|
||||
#include "unistd.h"
|
||||
#include "utils.h"
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
int receive_files(Config *config, int fd) {
|
||||
int receive_files(Config *config, int file_descriptor) {
|
||||
Status status;
|
||||
if (!receive_status(fd, &status)) return -1;
|
||||
|
||||
if (!receive_status(file_descriptor, &status)) return -1;
|
||||
while (status == STATUS_NEXT || status == STATUS_CHUNK || status == STATUS_CHECK) {
|
||||
if (status == STATUS_CHECK) {
|
||||
bool skipped;
|
||||
File *file = receive_incremental_check(fd, config, &skipped);
|
||||
if (skipped) goto next;
|
||||
if (file == NULL && !skipped) return -1;
|
||||
if (config->save_to_disk)
|
||||
file_save_to_disk(config->receive_root_directory, file);
|
||||
file_destroy(file);
|
||||
} else if (status == STATUS_CHUNK) {
|
||||
Chunk *chunk = receive_chunk_data(fd, config);
|
||||
if (chunk == NULL) {
|
||||
send_status(fd, STATUS_ERROR);
|
||||
char *check_path = receive_str(file_descriptor);
|
||||
if (check_path == NULL) { send_status(file_descriptor, STATUS_ERROR); return -1; }
|
||||
unsigned long long check_size;
|
||||
long long check_mtime;
|
||||
if (!receive_n_data(file_descriptor, &check_size, sizeof(check_size)) ||
|
||||
!receive_n_data(file_descriptor, &check_mtime, sizeof(check_mtime))) {
|
||||
free(check_path);
|
||||
send_status(file_descriptor, STATUS_ERROR);
|
||||
return -1;
|
||||
}
|
||||
char *full_path = path_cat(config->receive_root_directory, check_path);
|
||||
struct stat st;
|
||||
bool match = false;
|
||||
if (full_path && stat(full_path, &st) == 0 &&
|
||||
(unsigned long long)st.st_size == check_size &&
|
||||
(long long)st.st_mtime == check_mtime) {
|
||||
match = true;
|
||||
}
|
||||
free(full_path);
|
||||
if (match) {
|
||||
if (!send_status(file_descriptor, STATUS_OK)) { free(check_path); return -1; }
|
||||
free(check_path);
|
||||
} else {
|
||||
if (!send_status(file_descriptor, STATUS_NEXT)) { free(check_path); return -1; }
|
||||
File *file = file_create(check_path);
|
||||
free(check_path);
|
||||
if (file == NULL) { send_status(file_descriptor, STATUS_ERROR); return -1; }
|
||||
if (config->use_metadata) {
|
||||
int meta_ok = 1;
|
||||
file->metadata = metadata_receive(file_descriptor, &meta_ok);
|
||||
if (!meta_ok) { file_destroy(file); send_status(file_descriptor, STATUS_ERROR); return -1; }
|
||||
}
|
||||
Data *file_data = receive_data(file_descriptor);
|
||||
if (file_data == NULL) {
|
||||
file_destroy(file);
|
||||
send_status(file_descriptor, STATUS_ERROR);
|
||||
return -1;
|
||||
}
|
||||
if (config->use_compression) {
|
||||
Data *uncompressed = data_decompress(file_data);
|
||||
data_destroy(file_data);
|
||||
if (uncompressed == NULL) { file_destroy(file); send_status(file_descriptor, STATUS_ERROR); return -1; }
|
||||
file_data = uncompressed;
|
||||
}
|
||||
data_destroy(file->data);
|
||||
file->data = file_data;
|
||||
if (config->save_to_disk) {
|
||||
char *disk_path = path_cat(config->receive_root_directory, file->path);
|
||||
if (disk_path) {
|
||||
to_disk(disk_path, file->data->data, file->data->size);
|
||||
file_restore_metadata(disk_path, file->metadata);
|
||||
free(disk_path);
|
||||
}
|
||||
}
|
||||
file_destroy(file);
|
||||
}
|
||||
} else if (status == STATUS_CHUNK) {
|
||||
Data *chunk_data = receive_data(file_descriptor);
|
||||
if (chunk_data == NULL) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to receive chunk data");
|
||||
send_status(file_descriptor, STATUS_ERROR);
|
||||
return -1;
|
||||
}
|
||||
Data *data_to_process = chunk_data;
|
||||
if (config->use_compression) {
|
||||
data_to_process = data_decompress(chunk_data);
|
||||
data_destroy(chunk_data);
|
||||
if (data_to_process == NULL) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to decompress chunk");
|
||||
send_status(file_descriptor, STATUS_ERROR);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
Chunk *chunk = chunk_deserialize(data_to_process, config->use_metadata);
|
||||
data_destroy(data_to_process);
|
||||
if (chunk == NULL) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to deserialize chunk, skipping");
|
||||
send_status(file_descriptor, STATUS_ERROR);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int i = 0; i < chunk->element_count; i++) {
|
||||
if (config->save_to_disk)
|
||||
file_save_to_disk(config->receive_root_directory, chunk->items[i]);
|
||||
if (config->save_to_disk) {
|
||||
char *disk_path = path_cat(config->receive_root_directory, chunk->items[i]->path);
|
||||
if (disk_path) {
|
||||
to_disk(disk_path, chunk->items[i]->data->data, chunk->items[i]->data->size);
|
||||
file_restore_metadata(disk_path, chunk->items[i]->metadata);
|
||||
free(disk_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
chunk_destroy(chunk);
|
||||
} else {
|
||||
File *file = file_receive(config, fd);
|
||||
File *file = file_receive(config, file_descriptor);
|
||||
if (file == NULL) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to receive file");
|
||||
send_status(fd, STATUS_ERROR);
|
||||
send_status(file_descriptor, STATUS_ERROR);
|
||||
return -1;
|
||||
}
|
||||
if (config->save_to_disk)
|
||||
file_save_to_disk(config->receive_root_directory, file);
|
||||
if (config->save_to_disk) {
|
||||
char *disk_path = path_cat(config->receive_root_directory, file->path);
|
||||
if (disk_path) {
|
||||
to_disk(disk_path, file->data->data, file->data->size);
|
||||
file_restore_metadata(disk_path, file->metadata);
|
||||
free(disk_path);
|
||||
}
|
||||
}
|
||||
file_destroy(file);
|
||||
}
|
||||
next:
|
||||
if (!receive_status(fd, &status)) {
|
||||
send_status(fd, STATUS_ERROR);
|
||||
if (!receive_status(file_descriptor, &status)) {
|
||||
send_status(file_descriptor, STATUS_ERROR);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (status == STATUS_MANIFEST) {
|
||||
if (receive_manifest(fd, config, &status) != 0) return -1;
|
||||
int count;
|
||||
if (!receive_int(file_descriptor, &count)) return -1;
|
||||
ArrayList *manifest = array_list_create(free);
|
||||
if (manifest) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
char *s = receive_str(file_descriptor);
|
||||
if (s) array_list_add(manifest, s);
|
||||
}
|
||||
fprintf(stderr, "Deleting files not in manifest...\n");
|
||||
delete_extras(config->receive_root_directory, manifest);
|
||||
array_list_delete(manifest);
|
||||
}
|
||||
if (!receive_status(file_descriptor, &status)) return -1;
|
||||
}
|
||||
if (status != STATUS_FINISHED) {
|
||||
log_message(LOG_LEVEL_ERROR, "Did not receive FINISHED Status");
|
||||
send_status(fd, STATUS_ERROR);
|
||||
send_status(file_descriptor, STATUS_ERROR);
|
||||
return -1;
|
||||
}
|
||||
send_status(fd, STATUS_OK);
|
||||
send_status(file_descriptor, STATUS_OK);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -119,88 +210,24 @@ static void cleanup(int sig) {
|
||||
_exit(0);
|
||||
}
|
||||
|
||||
static void print_server_usage(void) {
|
||||
printf("FastSync Server\n");
|
||||
printf("Usage: fastsync-server [options]\n");
|
||||
printf("\n");
|
||||
printf("Options:\n");
|
||||
printf(" --stdio Run in stdio mode (SSH transport)\n");
|
||||
printf(" -p <port> TCP port (default: 8080, range: 1-65535)\n");
|
||||
printf(" --tls Enable TLS encryption\n");
|
||||
printf(" --cert <path> TLS certificate file (PEM)\n");
|
||||
printf(" --key <path> TLS private key file (PEM)\n");
|
||||
printf(" --ca <path> TLS CA certificate file (PEM)\n");
|
||||
printf(" -v, --verbose Enable debug logging\n");
|
||||
printf(" --help Show this help\n");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
bool use_tls = false;
|
||||
char *tls_cert = NULL;
|
||||
char *tls_key = NULL;
|
||||
char *tls_ca = NULL;
|
||||
int port = 8080;
|
||||
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (strcmp(argv[i], "--help") == 0) {
|
||||
print_server_usage();
|
||||
return 0;
|
||||
} else if (strcmp(argv[i], "--stdio") == 0) {
|
||||
if (strcmp(argv[i], "--stdio") == 0) {
|
||||
io_set_fds(STDIN_FILENO, STDOUT_FILENO);
|
||||
handler(STDIN_FILENO);
|
||||
return 0;
|
||||
} else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) {
|
||||
set_log_level(LOG_LEVEL_DEBUG);
|
||||
} else if (strcmp(argv[i], "--tls") == 0) {
|
||||
use_tls = true;
|
||||
} else if (strcmp(argv[i], "--cert") == 0 && i + 1 < argc) {
|
||||
tls_cert = argv[++i];
|
||||
} else if (strcmp(argv[i], "--key") == 0 && i + 1 < argc) {
|
||||
tls_key = argv[++i];
|
||||
} else if (strcmp(argv[i], "--ca") == 0 && i + 1 < argc) {
|
||||
tls_ca = argv[++i];
|
||||
} else if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) {
|
||||
char *end;
|
||||
long p = strtol(argv[++i], &end, 10);
|
||||
if (*end || p <= 0 || p > 65535) {
|
||||
fprintf(stderr, "Error: invalid port '%s' (must be 1-65535)\n", argv[i]);
|
||||
return 1;
|
||||
}
|
||||
port = (int)p;
|
||||
} else if (argv[i][0] == '-') {
|
||||
fprintf(stderr, "Unknown option: %s\n", argv[i]);
|
||||
print_server_usage();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (tls_ca && !use_tls) {
|
||||
log_message(LOG_LEVEL_WARNING, "--ca has no effect without --tls");
|
||||
}
|
||||
|
||||
signal(SIGINT, cleanup);
|
||||
signal(SIGTERM, cleanup);
|
||||
g_server = server_create(port);
|
||||
g_server = server_create(8080);
|
||||
if (g_server == NULL) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to create server");
|
||||
return 1;
|
||||
}
|
||||
if (use_tls) {
|
||||
if (!tls_cert || !tls_key) {
|
||||
fprintf(stderr, "Error: --tls requires --cert and --key\n");
|
||||
server_delete(&g_server);
|
||||
return 1;
|
||||
}
|
||||
tls_global_init();
|
||||
if (!server_create_tls(g_server, tls_cert, tls_key, tls_ca)) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to set up TLS");
|
||||
server_delete(&g_server);
|
||||
return 1;
|
||||
}
|
||||
server_listen_tls(g_server, handler);
|
||||
} else {
|
||||
server_listen(g_server, handler);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
#include "file.h"
|
||||
#include "log.h"
|
||||
#include "metadata.h"
|
||||
#include "protocol.h"
|
||||
|
||||
Chunk *chunk_create(File **items, int element_count) {
|
||||
Chunk *chunk = (Chunk *)malloc(sizeof(Chunk));
|
||||
@@ -179,27 +178,5 @@ Data *chunk_compress(Chunk *chunk, int compression_level, bool use_metadata) {
|
||||
return compressed;
|
||||
}
|
||||
|
||||
Chunk *receive_chunk_data(int fd, Config *config) {
|
||||
Data *chunk_data = receive_data(fd);
|
||||
if (chunk_data == NULL) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to receive chunk data");
|
||||
return NULL;
|
||||
}
|
||||
Data *data_to_process = chunk_data;
|
||||
if (config->use_compression) {
|
||||
data_to_process = data_decompress(chunk_data);
|
||||
data_destroy(chunk_data);
|
||||
if (data_to_process == NULL) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to decompress chunk");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
Chunk *chunk = chunk_deserialize(data_to_process, config->use_metadata);
|
||||
data_destroy(data_to_process);
|
||||
if (chunk == NULL)
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to deserialize chunk, skipping");
|
||||
return chunk;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#ifndef CHUNK_H
|
||||
#define CHUNK_H
|
||||
|
||||
#include "config.h"
|
||||
#include "data.h"
|
||||
#include "file.h"
|
||||
#include <stdbool.h>
|
||||
@@ -19,6 +18,5 @@ void chunk_destroy(void *chunk);
|
||||
Data *chunk_serialize(Chunk *chunk, bool use_metadata);
|
||||
Chunk *chunk_deserialize(Data *data, bool use_metadata);
|
||||
Data *chunk_compress(Chunk *chunk, int compression_level, bool use_metadata);
|
||||
Chunk *receive_chunk_data(int fd, Config *config);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -39,10 +39,6 @@ Config *config_create(char *version, char *send_directory,
|
||||
config->max_size = 0;
|
||||
config->min_size = 0;
|
||||
config->use_incremental = false;
|
||||
config->use_tls = false;
|
||||
config->tls_cert = NULL;
|
||||
config->tls_key = NULL;
|
||||
config->tls_ca = NULL;
|
||||
return config;
|
||||
}
|
||||
|
||||
@@ -78,9 +74,6 @@ void config_delete(Config *config) {
|
||||
for (int i = 0; i < config->include_count; i++)
|
||||
free(config->include_patterns[i]);
|
||||
free(config->include_patterns);
|
||||
free(config->tls_cert);
|
||||
free(config->tls_key);
|
||||
free(config->tls_ca);
|
||||
free(config);
|
||||
}
|
||||
|
||||
@@ -156,10 +149,6 @@ Config *config_receive(int file_descriptor) {
|
||||
config->include_count = 0;
|
||||
config->max_size = 0;
|
||||
config->min_size = 0;
|
||||
config->use_tls = false;
|
||||
config->tls_cert = NULL;
|
||||
config->tls_key = NULL;
|
||||
config->tls_ca = NULL;
|
||||
if (!send_status(file_descriptor, STATUS_OK)) goto error;
|
||||
return config;
|
||||
|
||||
|
||||
@@ -33,10 +33,6 @@ typedef struct Config {
|
||||
unsigned long long max_size;
|
||||
unsigned long long min_size;
|
||||
bool use_incremental;
|
||||
bool use_tls;
|
||||
char *tls_cert;
|
||||
char *tls_key;
|
||||
char *tls_ca;
|
||||
} Config;
|
||||
|
||||
#define PROTOCOL_VERSION "1.1.0"
|
||||
|
||||
+50
-85
@@ -96,7 +96,7 @@ bool file_load_data(File *file) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool file_send_single_calls(File *file, int file_descriptor, bool use_metadata, int compression_level, bool send_path) {
|
||||
bool file_send_single_calls_no_path(File *file, int file_descriptor, bool use_metadata, int compression_level) {
|
||||
Data *data_to_send = file->data;
|
||||
Data *compressed_data = NULL;
|
||||
if (compression_level > 0) {
|
||||
@@ -107,10 +107,6 @@ bool file_send_single_calls(File *file, int file_descriptor, bool use_metadata,
|
||||
}
|
||||
data_to_send = compressed_data;
|
||||
}
|
||||
if (send_path && !send_str(file_descriptor, file->path)) {
|
||||
data_destroy(compressed_data);
|
||||
return false;
|
||||
}
|
||||
if (use_metadata && !metadata_send(file_descriptor, file->metadata)) {
|
||||
data_destroy(compressed_data);
|
||||
return false;
|
||||
@@ -123,74 +119,60 @@ bool file_send_single_calls(File *file, int file_descriptor, bool use_metadata,
|
||||
return true;
|
||||
}
|
||||
|
||||
bool file_save_to_disk(const char *root_directory, File *file) {
|
||||
char *disk_path = path_cat((char *)root_directory, file->path);
|
||||
if (disk_path == NULL) return false;
|
||||
bool ok = to_disk(disk_path, file->data->data, file->data->size);
|
||||
if (ok) file_restore_metadata(disk_path, file->metadata);
|
||||
free(disk_path);
|
||||
return ok;
|
||||
bool file_send_sendfile_no_path(File *file, int file_descriptor, bool use_metadata) {
|
||||
if (use_metadata && !metadata_send(file_descriptor, file->metadata)) return false;
|
||||
|
||||
int fd = open(file->path, O_RDONLY);
|
||||
if (fd == -1) {
|
||||
perror("Could not open file for sendfile");
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned long long file_size = file->data->size;
|
||||
if (!send_n_data(file_descriptor, &file_size, sizeof(unsigned long long))) {
|
||||
close(fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
off_t offset = 0;
|
||||
while (offset < file_size) {
|
||||
ssize_t sent = sendfile(file_descriptor, fd, &offset, file_size - offset);
|
||||
if (sent == -1) {
|
||||
perror("sendfile failed");
|
||||
close(fd);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return true;
|
||||
}
|
||||
|
||||
File *receive_incremental_check(int fd, Config *config, bool *skipped) { *skipped = false;
|
||||
char *check_path = receive_str(fd);
|
||||
if (check_path == NULL) { send_status(fd, STATUS_ERROR); return NULL; }
|
||||
|
||||
unsigned long long check_size;
|
||||
long long check_mtime;
|
||||
if (!receive_n_data(fd, &check_size, sizeof(check_size)) ||
|
||||
!receive_n_data(fd, &check_mtime, sizeof(check_mtime))) {
|
||||
free(check_path);
|
||||
send_status(fd, STATUS_ERROR);
|
||||
return NULL;
|
||||
bool file_send_single_calls(File *file, int file_descriptor, bool use_metadata, int compression_level) {
|
||||
Data *data_to_send = file->data;
|
||||
Data *compressed_data = NULL;
|
||||
if (compression_level > 0) {
|
||||
compressed_data = data_compress(file->data, compression_level);
|
||||
if (compressed_data == NULL) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to compress file data");
|
||||
return false;
|
||||
}
|
||||
|
||||
char *full_path = path_cat(config->receive_root_directory, check_path);
|
||||
struct stat st;
|
||||
bool match = false;
|
||||
if (full_path && stat(full_path, &st) == 0 &&
|
||||
(unsigned long long)st.st_size == check_size &&
|
||||
(long long)st.st_mtime == check_mtime) {
|
||||
match = true;
|
||||
data_to_send = compressed_data;
|
||||
}
|
||||
free(full_path);
|
||||
|
||||
if (match) {
|
||||
if (!send_status(fd, STATUS_OK)) { free(check_path); return NULL; }
|
||||
free(check_path);
|
||||
*skipped = true;
|
||||
return NULL;
|
||||
if (!send_str(file_descriptor, file->path)) {
|
||||
data_destroy(compressed_data);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!send_status(fd, STATUS_NEXT)) { free(check_path); return NULL; }
|
||||
|
||||
File *file = file_create(check_path);
|
||||
free(check_path);
|
||||
if (file == NULL) { send_status(fd, STATUS_ERROR); return NULL; }
|
||||
|
||||
if (config->use_metadata) {
|
||||
int meta_ok = 1;
|
||||
file->metadata = metadata_receive(fd, &meta_ok);
|
||||
if (!meta_ok) { file_destroy(file); send_status(fd, STATUS_ERROR); return NULL; }
|
||||
if (use_metadata && !metadata_send(file_descriptor, file->metadata)) {
|
||||
data_destroy(compressed_data);
|
||||
return false;
|
||||
}
|
||||
|
||||
Data *file_data = receive_data(fd);
|
||||
if (file_data == NULL) {
|
||||
file_destroy(file);
|
||||
send_status(fd, STATUS_ERROR);
|
||||
return NULL;
|
||||
if (!send_data(file_descriptor, data_to_send)) {
|
||||
data_destroy(compressed_data);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (config->use_compression) {
|
||||
Data *uncompressed = data_decompress(file_data);
|
||||
data_destroy(file_data);
|
||||
if (uncompressed == NULL) { file_destroy(file); send_status(fd, STATUS_ERROR); return NULL; }
|
||||
file_data = uncompressed;
|
||||
}
|
||||
|
||||
data_destroy(file->data);
|
||||
file->data = file_data;
|
||||
return file;
|
||||
data_destroy(compressed_data);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool to_disk(const char *path, const void *data, unsigned long long data_size) {
|
||||
@@ -218,8 +200,8 @@ bool to_disk(const char *path, const void *data, unsigned long long data_size) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool file_send_sendfile(File *file, int file_descriptor, bool use_metadata, bool send_path) {
|
||||
if (send_path && !send_str(file_descriptor, file->path)) return false;
|
||||
bool file_send_sendfile(File *file, int file_descriptor, bool use_metadata) {
|
||||
if (!send_str(file_descriptor, file->path)) return false;
|
||||
if (use_metadata && !metadata_send(file_descriptor, file->metadata)) return false;
|
||||
|
||||
int fd = open(file->path, O_RDONLY);
|
||||
@@ -295,21 +277,4 @@ size_t file_content_to_buffer(File *file) {
|
||||
return bytes_read;
|
||||
}
|
||||
|
||||
int receive_manifest(int fd, Config *config, int *next_status) {
|
||||
int count;
|
||||
if (!receive_int(fd, &count)) return -1;
|
||||
ArrayList *manifest = array_list_create(free);
|
||||
if (manifest) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
char *s = receive_str(fd);
|
||||
if (s) array_list_add(manifest, s);
|
||||
}
|
||||
fprintf(stderr, "Deleting files not in manifest...\n");
|
||||
delete_extras(config->receive_root_directory, manifest);
|
||||
array_list_delete(manifest);
|
||||
}
|
||||
if (!receive_status(fd, next_status)) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+4
-5
@@ -24,14 +24,13 @@ File *file_create(const char *path);
|
||||
void file_destroy(void *item);
|
||||
bool file_load_data(File *file);
|
||||
File *file_receive(Config *config, int file_descriptor);
|
||||
bool file_send_single_calls(File *file, int file_descriptor, bool use_metadata, int compression_level, bool send_path);
|
||||
bool file_send_sendfile(File *file, int file_descriptor, bool use_metadata, bool send_path);
|
||||
bool file_send_single_calls(File *file, int file_descriptor, bool use_metadata, int compression_level);
|
||||
bool file_send_single_calls_no_path(File *file, int file_descriptor, bool use_metadata, int compression_level);
|
||||
bool file_send_sendfile(File *file, int file_descriptor, bool use_metadata);
|
||||
bool file_send_sendfile_no_path(File *file, int file_descriptor, bool use_metadata);
|
||||
size_t file_content_to_buffer(File *file);
|
||||
FileMetadata *file_metadata_create(struct stat *stats);
|
||||
void file_metadata_destroy(void *metadata);
|
||||
bool to_disk(const char *path, const void *data, unsigned long long data_size);
|
||||
bool file_save_to_disk(const char *root_directory, File *file);
|
||||
File *receive_incremental_check(int fd, Config *config, bool *skipped);
|
||||
int receive_manifest(int fd, Config *config, int *next_status);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
#include "multiprocessing.h"
|
||||
#include "array_list.h"
|
||||
#include "chunk.h"
|
||||
#include "compression.h"
|
||||
#include "config.h"
|
||||
#include "data.h"
|
||||
#include "file.h"
|
||||
#include "log.h"
|
||||
#include "metadata.h"
|
||||
#include "protocol.h"
|
||||
#include "queue.h"
|
||||
#include "utils.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <threads.h>
|
||||
|
||||
PipelineContextSender *pipeline_context_sender_create(Config *config,
|
||||
@@ -83,8 +86,26 @@ void pipeline_context_receiver_destroy(PipelineContextReceiver *context) {
|
||||
|
||||
static void receive_chunk_enqueue(int file_descriptor,
|
||||
PipelineContextReceiver *context) {
|
||||
Chunk *chunk = receive_chunk_data(file_descriptor, context->config);
|
||||
if (chunk == NULL) return;
|
||||
Data *chunk_data = receive_data(file_descriptor);
|
||||
if (chunk_data == NULL) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to receive chunk data");
|
||||
return;
|
||||
}
|
||||
Data *data_to_process = chunk_data;
|
||||
if (context->config->use_compression) {
|
||||
data_to_process = data_decompress(chunk_data);
|
||||
data_destroy(chunk_data);
|
||||
if (data_to_process == NULL) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to decompress chunk");
|
||||
return;
|
||||
}
|
||||
}
|
||||
Chunk *chunk = chunk_deserialize(data_to_process, context->config->use_metadata);
|
||||
data_destroy(data_to_process);
|
||||
if (chunk == NULL) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to deserialize chunk, skipping");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < chunk->element_count; i++) {
|
||||
File *file = chunk->items[i];
|
||||
@@ -108,10 +129,52 @@ int receive_thread(void *pipeline_context) {
|
||||
if (!receive_status(file_descriptor, &status)) return thrd_error;
|
||||
while (status == STATUS_NEXT || status == STATUS_CHUNK || status == STATUS_CHECK) {
|
||||
if (status == STATUS_CHECK) {
|
||||
bool skipped;
|
||||
File *file = receive_incremental_check(file_descriptor, config, &skipped);
|
||||
if (!skipped) {
|
||||
if (file == NULL) return thrd_error;
|
||||
char *check_path = receive_str(file_descriptor);
|
||||
if (check_path == NULL) { send_status(file_descriptor, STATUS_ERROR); return thrd_error; }
|
||||
unsigned long long check_size;
|
||||
long long check_mtime;
|
||||
if (!receive_n_data(file_descriptor, &check_size, sizeof(check_size)) ||
|
||||
!receive_n_data(file_descriptor, &check_mtime, sizeof(check_mtime))) {
|
||||
free(check_path);
|
||||
send_status(file_descriptor, STATUS_ERROR);
|
||||
return thrd_error;
|
||||
}
|
||||
char *full_path = path_cat(config->receive_root_directory, check_path);
|
||||
struct stat st;
|
||||
bool match = false;
|
||||
if (full_path && stat(full_path, &st) == 0 &&
|
||||
(unsigned long long)st.st_size == check_size &&
|
||||
(long long)st.st_mtime == check_mtime) {
|
||||
match = true;
|
||||
}
|
||||
free(full_path);
|
||||
if (match) {
|
||||
if (!send_status(file_descriptor, STATUS_OK)) { free(check_path); return thrd_error; }
|
||||
free(check_path);
|
||||
} else {
|
||||
if (!send_status(file_descriptor, STATUS_NEXT)) { free(check_path); return thrd_error; }
|
||||
File *file = file_create(check_path);
|
||||
free(check_path);
|
||||
if (file == NULL) { send_status(file_descriptor, STATUS_ERROR); return thrd_error; }
|
||||
if (config->use_metadata) {
|
||||
int meta_ok = 1;
|
||||
file->metadata = metadata_receive(file_descriptor, &meta_ok);
|
||||
if (!meta_ok) { file_destroy(file); send_status(file_descriptor, STATUS_ERROR); return thrd_error; }
|
||||
}
|
||||
Data *file_data = receive_data(file_descriptor);
|
||||
if (file_data == NULL) {
|
||||
file_destroy(file);
|
||||
send_status(file_descriptor, STATUS_ERROR);
|
||||
return thrd_error;
|
||||
}
|
||||
if (config->use_compression) {
|
||||
Data *uncompressed = data_decompress(file_data);
|
||||
data_destroy(file_data);
|
||||
if (uncompressed == NULL) { file_destroy(file); send_status(file_descriptor, STATUS_ERROR); return thrd_error; }
|
||||
file_data = uncompressed;
|
||||
}
|
||||
data_destroy(file->data);
|
||||
file->data = file_data;
|
||||
queue_enqueue_multithreaded(context->queue, file, &context->mutex,
|
||||
&context->condition_not_empty,
|
||||
&context->condition_not_full);
|
||||
@@ -131,7 +194,22 @@ int receive_thread(void *pipeline_context) {
|
||||
if (!receive_status(file_descriptor, &status)) return thrd_error;
|
||||
}
|
||||
if (status == STATUS_MANIFEST) {
|
||||
if (receive_manifest(file_descriptor, config, &status) != 0) return thrd_error;
|
||||
int count;
|
||||
if (!receive_int(file_descriptor, &count)) return thrd_error;
|
||||
ArrayList *manifest = array_list_create(free);
|
||||
if (manifest) {
|
||||
for (int i = 0; i < count; i++) {
|
||||
char *s = receive_str(file_descriptor);
|
||||
if (s) {
|
||||
array_list_add(manifest, s);
|
||||
}
|
||||
}
|
||||
delete_extras(context->config->receive_root_directory, manifest);
|
||||
for (int i = 0; i < manifest->size; i++)
|
||||
free(manifest->items[i]);
|
||||
array_list_delete(manifest);
|
||||
}
|
||||
if (!receive_status(file_descriptor, &status)) return thrd_error;
|
||||
}
|
||||
mtx_lock(&context->mutex);
|
||||
context->receiver_done = true;
|
||||
@@ -156,8 +234,14 @@ int write_thread(void *pipeline_context) {
|
||||
free(root_directory);
|
||||
return thrd_success;
|
||||
}
|
||||
if (save_to_disk)
|
||||
file_save_to_disk(root_directory, file);
|
||||
if (save_to_disk) {
|
||||
char *disk_path = path_cat(root_directory, file->path);
|
||||
if (disk_path) {
|
||||
to_disk(disk_path, file->data->data, file->data->size);
|
||||
file_restore_metadata(disk_path, file->metadata);
|
||||
free(disk_path);
|
||||
}
|
||||
}
|
||||
file_destroy(file);
|
||||
}
|
||||
}
|
||||
|
||||
+4
-18
@@ -1,7 +1,6 @@
|
||||
#include "protocol.h"
|
||||
#include "log.h"
|
||||
#include <errno.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -10,7 +9,6 @@
|
||||
|
||||
static __thread int io_read_fd = -1;
|
||||
static __thread int io_write_fd = -1;
|
||||
static SSL *io_ssl = NULL;
|
||||
|
||||
static unsigned long long io_bwlimit = 0;
|
||||
static long long bw_tokens = 0;
|
||||
@@ -56,10 +54,6 @@ static void bw_throttle(size_t bytes_written) {
|
||||
}
|
||||
}
|
||||
|
||||
void io_set_ssl(SSL *ssl) {
|
||||
io_ssl = ssl;
|
||||
}
|
||||
|
||||
static int io_fd(int dir_fd, int file_descriptor) {
|
||||
return (dir_fd != -1) ? dir_fd : file_descriptor;
|
||||
}
|
||||
@@ -72,11 +66,8 @@ bool send_n_data(int file_descriptor, void *data, size_t data_size) {
|
||||
size_t chunk = data_size - total_bytes_send;
|
||||
if (io_bwlimit > 0 && chunk > 65536)
|
||||
chunk = 65536;
|
||||
ssize_t bytes_send;
|
||||
if (io_ssl)
|
||||
bytes_send = SSL_write(io_ssl, (char *)data + total_bytes_send, chunk);
|
||||
else
|
||||
bytes_send = write(fd, (char *)data + total_bytes_send, chunk);
|
||||
ssize_t bytes_send =
|
||||
write(fd, (char *)data + total_bytes_send, chunk);
|
||||
if (bytes_send <= 0) {
|
||||
log_message(LOG_LEVEL_ERROR, "Could not send data");
|
||||
return false;
|
||||
@@ -93,13 +84,8 @@ bool receive_n_data(int file_descriptor, void *data, size_t data_size) {
|
||||
int fd = io_fd(io_read_fd, file_descriptor);
|
||||
size_t total_bytes_received = 0;
|
||||
while (total_bytes_received < data_size) {
|
||||
ssize_t bytes_received;
|
||||
if (io_ssl)
|
||||
bytes_received = SSL_read(io_ssl, (char *)data + total_bytes_received,
|
||||
data_size - total_bytes_received);
|
||||
else
|
||||
bytes_received = read(fd, (char *)data + total_bytes_received,
|
||||
data_size - total_bytes_received);
|
||||
ssize_t bytes_received =
|
||||
read(fd, (char *)data + total_bytes_received, data_size - total_bytes_received);
|
||||
if (bytes_received <= 0) {
|
||||
if (bytes_received == 0)
|
||||
log_message(LOG_LEVEL_ERROR, "Connection closed while receiving data");
|
||||
|
||||
@@ -5,15 +5,11 @@
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct ssl_st SSL;
|
||||
|
||||
typedef int Status;
|
||||
enum NET_STATUS { STATUS_OK, STATUS_ERROR, STATUS_FINISHED, STATUS_NEXT, STATUS_CHUNK, STATUS_MANIFEST, STATUS_CHECK };
|
||||
|
||||
void io_set_fds(int read_fd, int write_fd);
|
||||
void io_set_bwlimit(unsigned long long bytes_per_sec);
|
||||
typedef struct ssl_st SSL;
|
||||
void io_set_ssl(SSL *ssl);
|
||||
bool send_n_data(int file_descriptor, void *data, size_t data_size);
|
||||
bool receive_n_data(int file_descriptor, void *data, size_t data_size);
|
||||
|
||||
|
||||
@@ -143,7 +143,5 @@ Client *client_connect_ssh(char *destination, int port) {
|
||||
client->address.sin_family = AF_UNIX;
|
||||
client->address_length = 0;
|
||||
client->ssh_child_pid = pid;
|
||||
client->ssl = NULL;
|
||||
client->ssl_ctx = NULL;
|
||||
return client;
|
||||
}
|
||||
|
||||
+15
-49
@@ -1,8 +1,6 @@
|
||||
#include "transport_tcp.h"
|
||||
#include "log.h"
|
||||
#include "protocol.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -38,7 +36,6 @@ Server *server_create(int port) {
|
||||
server->address.sin_addr.s_addr = INADDR_ANY;
|
||||
server->address.sin_port = htons(port);
|
||||
server->address_length = sizeof(server->address);
|
||||
server->ssl_ctx = NULL;
|
||||
|
||||
if (bind(server->file_descriptor, (struct sockaddr *)&server->address,
|
||||
server->address_length) < 0) {
|
||||
@@ -54,63 +51,43 @@ Server *server_create(int port) {
|
||||
void server_delete(Server **server) {
|
||||
if (server == NULL || *server == NULL) return;
|
||||
close((*server)->file_descriptor);
|
||||
if ((*server)->ssl_ctx) {
|
||||
SSL_CTX_free((*server)->ssl_ctx);
|
||||
(*server)->ssl_ctx = NULL;
|
||||
}
|
||||
free(*server);
|
||||
*server = NULL;
|
||||
}
|
||||
|
||||
static void accept_loop(Server *server, void (*child_fn)(int, void *),
|
||||
void *child_ctx, const char *log_fmt) {
|
||||
bool server_listen(Server *server, void (*handler)(int file_descriptor)) {
|
||||
log_message(LOG_LEVEL_INFO, "Start Listening on Port: %d",
|
||||
server->address.sin_port);
|
||||
if (listen(server->file_descriptor, SOMAXCONN) < 0) {
|
||||
perror("Could not listen on port!");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
signal(SIGCHLD, SIG_IGN);
|
||||
|
||||
while (1) {
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t client_len = sizeof(client_addr);
|
||||
int fd = accept(server->file_descriptor, (struct sockaddr *)&client_addr,
|
||||
int file_descriptor =
|
||||
accept(server->file_descriptor, (struct sockaddr *)&client_addr,
|
||||
&client_len);
|
||||
if (fd < 0) {
|
||||
if (file_descriptor < 0) {
|
||||
perror("Could not accept the connection");
|
||||
continue;
|
||||
}
|
||||
log_message(LOG_LEVEL_INFO, "%s", log_fmt);
|
||||
log_message(LOG_LEVEL_INFO, "Received Connection");
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
close(server->file_descriptor);
|
||||
child_fn(fd, child_ctx);
|
||||
close(fd);
|
||||
handler(file_descriptor);
|
||||
close(file_descriptor);
|
||||
_exit(0);
|
||||
}
|
||||
close(fd);
|
||||
close(file_descriptor);
|
||||
}
|
||||
}
|
||||
|
||||
struct plain_ctx { void (*handler)(int); };
|
||||
|
||||
static void plain_child_fn(int fd, void *ctx) {
|
||||
((struct plain_ctx *)ctx)->handler(fd);
|
||||
}
|
||||
|
||||
bool server_listen(Server *server, void (*handler)(int file_descriptor)) {
|
||||
log_message(LOG_LEVEL_INFO, "Start Listening on Port: %d",
|
||||
ntohs(server->address.sin_port));
|
||||
struct plain_ctx ctx = {handler};
|
||||
accept_loop(server, plain_child_fn, &ctx, "Received Connection");
|
||||
return true;
|
||||
}
|
||||
|
||||
void server_accept_loop(Server *server, void (*child_fn)(int, void *),
|
||||
void *child_ctx, const char *log_fmt) {
|
||||
log_message(LOG_LEVEL_INFO, "Start TLS Listening on Port: %d",
|
||||
ntohs(server->address.sin_port));
|
||||
accept_loop(server, child_fn, child_ctx, log_fmt);
|
||||
}
|
||||
|
||||
Client *client_create() {
|
||||
int file_descriptor = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (file_descriptor < 0) {
|
||||
@@ -127,8 +104,6 @@ Client *client_create() {
|
||||
client->address.sin_family = AF_INET;
|
||||
client->address_length = sizeof(client->address);
|
||||
client->ssh_child_pid = -1;
|
||||
client->ssl = NULL;
|
||||
client->ssl_ctx = NULL;
|
||||
return client;
|
||||
}
|
||||
|
||||
@@ -149,12 +124,6 @@ bool client_connect(Client *client, char *host, int port) {
|
||||
}
|
||||
|
||||
void client_disconnect(Client *client) {
|
||||
if (client->ssl) {
|
||||
SSL_shutdown(client->ssl);
|
||||
SSL_free(client->ssl);
|
||||
client->ssl = NULL;
|
||||
io_set_ssl(NULL);
|
||||
}
|
||||
close(client->file_descriptor);
|
||||
if (client->ssh_child_pid > 0) {
|
||||
int status;
|
||||
@@ -164,10 +133,7 @@ void client_disconnect(Client *client) {
|
||||
}
|
||||
|
||||
void client_delete(Client *client) {
|
||||
if (client == NULL) return;
|
||||
if (client->ssl_ctx) {
|
||||
SSL_CTX_free(client->ssl_ctx);
|
||||
client->ssl_ctx = NULL;
|
||||
}
|
||||
if (client == NULL)
|
||||
return;
|
||||
free(client);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ typedef struct Server {
|
||||
struct sockaddr_in address;
|
||||
unsigned int address_length;
|
||||
int file_descriptor;
|
||||
void *ssl_ctx;
|
||||
} Server;
|
||||
|
||||
typedef struct Client {
|
||||
@@ -17,14 +16,10 @@ typedef struct Client {
|
||||
unsigned int address_length;
|
||||
int file_descriptor;
|
||||
pid_t ssh_child_pid;
|
||||
void *ssl;
|
||||
void *ssl_ctx;
|
||||
} Client;
|
||||
|
||||
Server *server_create(int port);
|
||||
bool server_listen(Server *server, void (*handler)(int file_descriptor));
|
||||
void server_accept_loop(Server *server, void (*child_fn)(int, void *),
|
||||
void *child_ctx, const char *log_fmt);
|
||||
void server_delete(Server **server);
|
||||
Client *client_create();
|
||||
bool client_connect(Client *client, char *host, int port);
|
||||
|
||||
@@ -1,162 +0,0 @@
|
||||
#include "transport_tls.h"
|
||||
#include "log.h"
|
||||
#include "protocol.h"
|
||||
#include "transport_tcp.h"
|
||||
#include <arpa/inet.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
bool tls_global_init(void) {
|
||||
#if OPENSSL_VERSION_NUMBER < 0x10100000L
|
||||
SSL_library_init();
|
||||
OpenSSL_add_all_algorithms();
|
||||
SSL_load_error_strings();
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
static void log_ssl_errors(void) {
|
||||
unsigned long err;
|
||||
char buf[256];
|
||||
while ((err = ERR_get_error()) != 0) {
|
||||
ERR_error_string_n(err, buf, sizeof(buf));
|
||||
log_message(LOG_LEVEL_ERROR, "SSL error: %s", buf);
|
||||
}
|
||||
}
|
||||
|
||||
static SSL_CTX *create_ssl_ctx(bool is_server, const char *cert,
|
||||
const char *key, const char *ca_path) {
|
||||
const SSL_METHOD *method =
|
||||
is_server ? TLS_server_method() : TLS_client_method();
|
||||
SSL_CTX *ctx = SSL_CTX_new(method);
|
||||
if (!ctx) {
|
||||
log_message(LOG_LEVEL_ERROR, "Unable to create SSL context");
|
||||
log_ssl_errors();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION);
|
||||
|
||||
if (cert && key) {
|
||||
if (SSL_CTX_use_certificate_file(ctx, cert, SSL_FILETYPE_PEM) <= 0) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to load certificate: %s", cert);
|
||||
log_ssl_errors();
|
||||
SSL_CTX_free(ctx);
|
||||
return NULL;
|
||||
}
|
||||
if (SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM) <= 0) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to load private key: %s", key);
|
||||
log_ssl_errors();
|
||||
SSL_CTX_free(ctx);
|
||||
return NULL;
|
||||
}
|
||||
if (!SSL_CTX_check_private_key(ctx)) {
|
||||
log_message(LOG_LEVEL_ERROR,
|
||||
"Private key does not match certificate");
|
||||
SSL_CTX_free(ctx);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (ca_path) {
|
||||
if (!SSL_CTX_load_verify_locations(ctx, ca_path, NULL)) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to load CA: %s", ca_path);
|
||||
log_ssl_errors();
|
||||
SSL_CTX_free(ctx);
|
||||
return NULL;
|
||||
}
|
||||
SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
|
||||
SSL_CTX_set_verify_depth(ctx, 4);
|
||||
}
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static SSL *wrap_fd_with_ssl(int fd, SSL_CTX *ctx, bool is_server) {
|
||||
SSL *ssl = SSL_new(ctx);
|
||||
if (!ssl) {
|
||||
log_message(LOG_LEVEL_ERROR, "Failed to create SSL object");
|
||||
return NULL;
|
||||
}
|
||||
SSL_set_fd(ssl, fd);
|
||||
int ret;
|
||||
if (is_server)
|
||||
ret = SSL_accept(ssl);
|
||||
else
|
||||
ret = SSL_connect(ssl);
|
||||
|
||||
if (ret <= 0) {
|
||||
log_message(LOG_LEVEL_ERROR, "SSL %s failed",
|
||||
is_server ? "accept" : "connect");
|
||||
log_ssl_errors();
|
||||
SSL_free(ssl);
|
||||
return NULL;
|
||||
}
|
||||
return ssl;
|
||||
}
|
||||
|
||||
bool server_create_tls(Server *server, const char *cert_path,
|
||||
const char *key_path, const char *ca_path) {
|
||||
SSL_CTX *ctx = create_ssl_ctx(true, cert_path, key_path, ca_path);
|
||||
if (!ctx) return false;
|
||||
server->ssl_ctx = ctx;
|
||||
return true;
|
||||
}
|
||||
|
||||
struct tls_child_ctx {
|
||||
void (*handler)(int);
|
||||
SSL_CTX *ssl_ctx;
|
||||
};
|
||||
|
||||
static void tls_child_fn(int fd, void *arg) {
|
||||
struct tls_child_ctx *ctx = (struct tls_child_ctx *)arg;
|
||||
SSL *ssl = wrap_fd_with_ssl(fd, ctx->ssl_ctx, true);
|
||||
if (!ssl) return;
|
||||
io_set_ssl(ssl);
|
||||
ctx->handler(fd);
|
||||
SSL_shutdown(ssl);
|
||||
SSL_free(ssl);
|
||||
io_set_ssl(NULL);
|
||||
}
|
||||
|
||||
bool server_listen_tls(Server *server, void (*handler)(int file_descriptor)) {
|
||||
struct tls_child_ctx ctx = {handler, (SSL_CTX *)server->ssl_ctx};
|
||||
server_accept_loop(server, tls_child_fn, &ctx, "Received TLS Connection");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool client_connect_tls(Client *client, char *host, int port,
|
||||
const char *cert_path, const char *key_path,
|
||||
const char *ca_path) {
|
||||
client->address.sin_port = htons(port);
|
||||
if (inet_pton(AF_INET, host, &client->address.sin_addr) <= 0) {
|
||||
perror("Could not convert host address!");
|
||||
return false;
|
||||
}
|
||||
if (connect(client->file_descriptor, (struct sockaddr *)&client->address,
|
||||
client->address_length) < 0) {
|
||||
perror("Could not connect to Server!");
|
||||
return false;
|
||||
}
|
||||
|
||||
SSL_CTX *ctx = create_ssl_ctx(false, cert_path, key_path, ca_path);
|
||||
if (!ctx) return false;
|
||||
client->ssl_ctx = ctx;
|
||||
|
||||
SSL *ssl = wrap_fd_with_ssl(client->file_descriptor, ctx, false);
|
||||
if (!ssl) {
|
||||
SSL_CTX_free(ctx);
|
||||
client->ssl_ctx = NULL;
|
||||
return false;
|
||||
}
|
||||
client->ssl = ssl;
|
||||
io_set_ssl(ssl);
|
||||
return true;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
#ifndef TRANSPORT_TLS_H
|
||||
#define TRANSPORT_TLS_H
|
||||
|
||||
#include "transport_tcp.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
bool tls_global_init(void);
|
||||
|
||||
bool server_create_tls(Server *server, const char *cert_path,
|
||||
const char *key_path, const char *ca_path);
|
||||
bool server_listen_tls(Server *server, void (*handler)(int file_descriptor));
|
||||
bool client_connect_tls(Client *client, char *host, int port,
|
||||
const char *cert_path, const char *key_path,
|
||||
const char *ca_path);
|
||||
|
||||
#endif
|
||||
@@ -50,7 +50,7 @@ CLIENT_CMD_PREFIX = [
|
||||
|
||||
BASE_CLIENT_FLAGS = ["--save-to-disk"]
|
||||
|
||||
TEST_CASES_FULL = [
|
||||
TEST_CASES = [
|
||||
{"name": "Standard", "flags": []},
|
||||
{"name": "Posix Args (no flags)", "flags": [], "posix": True},
|
||||
{"name": "Standard (no metadata)", "flags": [], "use_metadata": False},
|
||||
@@ -68,14 +68,7 @@ TEST_CASES_FULL = [
|
||||
{"name": "Sendfile + Multithreading (-f -m)", "flags": ["-f", "-m"]},
|
||||
]
|
||||
|
||||
TEST_CASES_LIGHT = [
|
||||
{"name": "Standard", "flags": []},
|
||||
{"name": "Compression (-c)", "flags": ["-c"]},
|
||||
{"name": "Chunk Serialization (-s)", "flags": ["-s"]},
|
||||
{"name": "Multithreading + Compression + Chunk Serialization (-m -c -s)", "flags": ["-m", "-c", "-s"]},
|
||||
]
|
||||
|
||||
SSH_CASES_FULL = [
|
||||
SSH_CASES = [
|
||||
{"name": "SSH (localhost)", "flags": []},
|
||||
{"name": "SSH Multithreading (-m)", "flags": ["-m"]},
|
||||
{"name": "SSH Compression (-c)", "flags": ["-c"]},
|
||||
@@ -86,17 +79,11 @@ SSH_CASES_FULL = [
|
||||
{"name": "SSH Multithreading + Compression + Chunk Serialization (-m -c -s)", "flags": ["-m", "-c", "-s"]},
|
||||
]
|
||||
|
||||
SSH_CASES_LIGHT = [
|
||||
{"name": "SSH (localhost)", "flags": []},
|
||||
]
|
||||
|
||||
RSYNC_CASES_FULL = [
|
||||
RSYNC_CASES = [
|
||||
{"name": "rsync (archive)", "args": ["-aH"]},
|
||||
{"name": "rsync (archive + compress)", "args": ["-aHz"]},
|
||||
]
|
||||
|
||||
RSYNC_CASES_LIGHT = []
|
||||
|
||||
|
||||
def netem_apply(profile):
|
||||
params = NETWORK_PROFILES[profile]
|
||||
@@ -132,12 +119,12 @@ def find_free_port():
|
||||
return s.getsockname()[1]
|
||||
|
||||
|
||||
def generate_test_files(source_dir, full=False):
|
||||
def generate_test_files(source_dir):
|
||||
if os.path.exists(source_dir):
|
||||
shutil.rmtree(source_dir)
|
||||
os.makedirs(source_dir)
|
||||
|
||||
target_total = 25 * 1024 * 1024 if full else 0
|
||||
target_total = 25 * 1024 * 1024
|
||||
written = 0
|
||||
|
||||
files = {
|
||||
@@ -154,7 +141,6 @@ def generate_test_files(source_dir, full=False):
|
||||
f.write(content)
|
||||
written += len(content)
|
||||
|
||||
if full:
|
||||
os.makedirs(os.path.join(source_dir, "bulk"), exist_ok=True)
|
||||
i = 0
|
||||
while written < target_total:
|
||||
@@ -268,24 +254,19 @@ def print_profile_header(profile_name):
|
||||
print(" No limits applied")
|
||||
|
||||
|
||||
def run_profile(profile_name, source_dir, dest_dir, *, full=False, test_cases=None, ssh_cases=None, rsync_cases=None):
|
||||
def run_profile(profile_name, source_dir, dest_dir):
|
||||
print_profile_header(profile_name)
|
||||
is_limited = profile_name != "Unlimited"
|
||||
client_prefix = CLIENT_CMD_PREFIX if is_limited else []
|
||||
|
||||
if test_cases is None:
|
||||
test_cases = TEST_CASES_FULL if full else TEST_CASES_LIGHT
|
||||
if ssh_cases is None:
|
||||
ssh_cases = SSH_CASES_FULL if full else SSH_CASES_LIGHT
|
||||
if rsync_cases is None:
|
||||
rsync_cases = RSYNC_CASES_FULL if full else RSYNC_CASES_LIGHT
|
||||
|
||||
try:
|
||||
if is_limited and full:
|
||||
if is_limited:
|
||||
netem_apply(profile_name)
|
||||
else:
|
||||
netem_reset()
|
||||
|
||||
results = []
|
||||
for case in test_cases:
|
||||
for case in TEST_CASES:
|
||||
flags = BASE_CLIENT_FLAGS + (["-M"] if case.get("use_metadata", True) else []) + case["flags"]
|
||||
if case.get("posix"):
|
||||
cmd = client_prefix + BASE_CLIENT_CMD + [source_dir, dest_dir] + flags
|
||||
@@ -300,7 +281,7 @@ def run_profile(profile_name, source_dir, dest_dir, *, full=False, test_cases=No
|
||||
results.append({"name": case["name"], "suite": profile_name, "status": "Error", "time": "N/A", "error": str(e)})
|
||||
|
||||
if SSH_AVAILABLE:
|
||||
for case in ssh_cases:
|
||||
for case in SSH_CASES:
|
||||
flags = BASE_CLIENT_FLAGS + (["-M"] if case.get("use_metadata", True) else []) + case["flags"]
|
||||
ssh_dest = f"localhost:{dest_dir}_ssh"
|
||||
cmd = BASE_CLIENT_CMD + [source_dir, ssh_dest] + flags
|
||||
@@ -312,10 +293,9 @@ def run_profile(profile_name, source_dir, dest_dir, *, full=False, test_cases=No
|
||||
except Exception as e:
|
||||
results.append({"name": case["name"], "suite": profile_name, "status": "Error", "time": "N/A", "error": str(e)})
|
||||
|
||||
if rsync_cases:
|
||||
port, conf, daemon = start_rsync_daemon(source_dir)
|
||||
try:
|
||||
for case in rsync_cases:
|
||||
for case in RSYNC_CASES:
|
||||
cmd = client_prefix + ["rsync"] + case["args"] + [f"rsync://localhost:{port}/source/", f"{dest_dir}/"]
|
||||
print(f"\n --- {case['name']} ---\n Running: {' '.join(cmd)}")
|
||||
try:
|
||||
@@ -344,7 +324,6 @@ def run_profile(profile_name, source_dir, dest_dir, *, full=False, test_cases=No
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if full:
|
||||
# Feature-specific tests for rsync-compatible flags
|
||||
print("\n " + "─" * 56 + "\n Feature Tests\n " + "─" * 56)
|
||||
|
||||
@@ -594,26 +573,19 @@ def check_ssh_localhost():
|
||||
build_dir = os.path.abspath("build")
|
||||
server_path = os.path.join(build_dir, "server")
|
||||
|
||||
try:
|
||||
r = subprocess.run(["ssh", "-o", "BatchMode=yes", "-o", "ConnectTimeout=5",
|
||||
"localhost", "which", "fastsync-server"],
|
||||
capture_output=True, timeout=10)
|
||||
except FileNotFoundError:
|
||||
SSH_AVAILABLE = False
|
||||
return
|
||||
if r.returncode == 0:
|
||||
SSH_AVAILABLE = True
|
||||
return
|
||||
|
||||
SSH_AVAILABLE = False
|
||||
# Try each PATH dir: create symlink, then verify with which
|
||||
try:
|
||||
r = subprocess.run(
|
||||
["ssh", "-o", "BatchMode=yes", "localhost",
|
||||
'echo "$PATH"'],
|
||||
capture_output=True, timeout=10, text=True)
|
||||
except FileNotFoundError:
|
||||
return
|
||||
if r.returncode != 0:
|
||||
return
|
||||
for d in r.stdout.strip().split(":"):
|
||||
@@ -694,10 +666,9 @@ def main():
|
||||
parser.add_argument("--keep-data", action="store_true")
|
||||
parser.add_argument("--unlimited", action="store_true")
|
||||
parser.add_argument("--wan", action="store_true")
|
||||
parser.add_argument("--full", action="store_true", help="Run full test suite with network shaping, SSH, rsync benchmarks")
|
||||
args = parser.parse_args()
|
||||
|
||||
total_bytes = generate_test_files(args.source_dir, full=args.full)
|
||||
total_bytes = generate_test_files(args.source_dir)
|
||||
if os.path.exists(args.dest_dir):
|
||||
shutil.rmtree(args.dest_dir)
|
||||
os.makedirs(args.dest_dir, exist_ok=True)
|
||||
@@ -708,12 +679,12 @@ def main():
|
||||
elif args.wan:
|
||||
profiles.append("WAN")
|
||||
else:
|
||||
profiles.append("LAN" if args.full else "Unlimited")
|
||||
profiles.append("LAN")
|
||||
|
||||
try:
|
||||
all_results = []
|
||||
for p in profiles:
|
||||
all_results.extend(run_profile(p, args.source_dir, args.dest_dir, full=args.full))
|
||||
all_results.extend(run_profile(p, args.source_dir, args.dest_dir))
|
||||
|
||||
print("\n" + "=" * 130)
|
||||
print(f"{'RESULTS':^130}")
|
||||
|
||||
Reference in New Issue
Block a user