From 27e3ac11dbb4a9c87ae7bb59263e1b54515c5c06 Mon Sep 17 00:00:00 2001 From: TapTap Date: Mon, 20 Jul 2026 19:53:03 +0200 Subject: [PATCH] fix: bump protocol version, add static_assert for metadata sizes --- src/shared/config.h | 2 +- src/shared/metadata.c | 13 +++++++++++++ src/shared/metadata.h | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/shared/config.h b/src/shared/config.h index 183f60c..610fca5 100644 --- a/src/shared/config.h +++ b/src/shared/config.h @@ -42,7 +42,7 @@ typedef struct Config { char* tls_ca; } Config; -#define PROTOCOL_VERSION "1.3.0" +#define PROTOCOL_VERSION "2.0.0" #define DEFAULT_CHUNK_SIZE (10 * 1024 * 1024) Config* config_create(char* version, char* send_directory, char* receive_directory, diff --git a/src/shared/metadata.c b/src/shared/metadata.c index d93f584..2564f7d 100644 --- a/src/shared/metadata.c +++ b/src/shared/metadata.c @@ -11,6 +11,19 @@ #include #include +/* + * Wire format serialization (protocol version 2.0.0+): + * All metadata fields are serialized as fixed-width integers (int32_t / int64_t) + * to ensure cross-platform binary compatiblity. See metadata.h for the + * exact wire layout. + * + * Compile-time assertions verify that the native platform types fit within + * the chosen fixed-width representations. + */ +typedef char static_assert_mode_t_fits[(sizeof(mode_t) <= sizeof(int32_t)) ? 1 : -1]; +typedef char static_assert_uid_t_fits[(sizeof(uid_t) <= sizeof(int32_t)) ? 1 : -1]; +typedef char static_assert_gid_t_fits[(sizeof(gid_t) <= sizeof(int32_t)) ? 1 : -1]; + void metadata_to_buf(char** buf, const FileMetadata* m) { int32_t present = (m != NULL) ? 1 : 0; memcpy(*buf, &present, sizeof(present)); diff --git a/src/shared/metadata.h b/src/shared/metadata.h index 518077b..f15570f 100644 --- a/src/shared/metadata.h +++ b/src/shared/metadata.h @@ -6,6 +6,20 @@ #include #include +/* + * Wire format (introduced in protocol version 2.0.0): + * int32_t present + * int32_t mode (was mode_t, platform-dependent) + * int32_t uid (was uid_t, platform-dependent) + * int32_t gid (was gid_t, platform-dependent) + * int64_t mtime_sec (was time_t, platform-dependent) + * int64_t mtime_nsec (was long, platform-dependent) + * + * Prior to 2.0.0 the wire format used the raw platform-dependent types, + * which broke compatiblity across different systems. All fields are now + * serialized as fixed-width integers. + */ + #define FILE_METADATA_WIRE_SIZE (sizeof(int32_t) * 3 + sizeof(int64_t) * 2) void metadata_to_buf(char** buf, const FileMetadata* m);