|
|
|
|
@ -12,7 +12,7 @@
|
|
|
|
|
|
|
|
|
|
#include <string.h> |
|
|
|
|
#include <stdlib.h> |
|
|
|
|
#include <openssl/sha.h> |
|
|
|
|
#include <openssl/evp.h> |
|
|
|
|
#include <sqlite3.h> |
|
|
|
|
|
|
|
|
|
#define MS_ID "member_sync" |
|
|
|
|
@ -89,14 +89,14 @@ static void _compute_member_hash(uint64_t node_id, const uint8_t* x25519,
|
|
|
|
|
const uint8_t* ed25519, const uint8_t* join_sig, |
|
|
|
|
const uint8_t* addrs_data, int addr_count, int online, |
|
|
|
|
uint8_t hash_out[MS_HASH_SIZE]) { |
|
|
|
|
SHA256_CTX ctx; |
|
|
|
|
SHA256_Init(&ctx); |
|
|
|
|
SHA256_Update(&ctx, &node_id, 8); |
|
|
|
|
SHA256_Update(&ctx, x25519, 32); |
|
|
|
|
SHA256_Update(&ctx, ed25519, 32); |
|
|
|
|
SHA256_Update(&ctx, join_sig, 64); |
|
|
|
|
EVP_MD_CTX* ctx = EVP_MD_CTX_new(); |
|
|
|
|
EVP_DigestInit_ex(ctx, EVP_sha256(), NULL); |
|
|
|
|
EVP_DigestUpdate(ctx, &node_id, 8); |
|
|
|
|
EVP_DigestUpdate(ctx, x25519, 32); |
|
|
|
|
EVP_DigestUpdate(ctx, ed25519, 32); |
|
|
|
|
EVP_DigestUpdate(ctx, join_sig, 64); |
|
|
|
|
uint8_t ac = (uint8_t)addr_count; |
|
|
|
|
SHA256_Update(&ctx, &ac, 1); |
|
|
|
|
EVP_DigestUpdate(ctx, &ac, 1); |
|
|
|
|
|
|
|
|
|
if (addr_count > 0 && addrs_data) { |
|
|
|
|
struct addr_item items[256]; int n = 0; |
|
|
|
|
@ -111,16 +111,17 @@ static void _compute_member_hash(uint64_t node_id, const uint8_t* x25519,
|
|
|
|
|
} |
|
|
|
|
qsort(items, (size_t)n, sizeof(struct addr_item), _addr_cmp); |
|
|
|
|
for (int i = 0; i < n; i++) { |
|
|
|
|
SHA256_Update(&ctx, &items[i].family, 1); |
|
|
|
|
SHA256_Update(&ctx, items[i].addr, (size_t)(items[i].family == 4 ? 4 : 16)); |
|
|
|
|
EVP_DigestUpdate(ctx, &items[i].family, 1); |
|
|
|
|
EVP_DigestUpdate(ctx, items[i].addr, (size_t)(items[i].family == 4 ? 4 : 16)); |
|
|
|
|
uint8_t port_be[2] = { (uint8_t)(items[i].port >> 8), (uint8_t)(items[i].port & 0xFF) }; |
|
|
|
|
SHA256_Update(&ctx, port_be, 2); |
|
|
|
|
EVP_DigestUpdate(ctx, port_be, 2); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
uint8_t ol = (uint8_t)(online ? 1 : 0); |
|
|
|
|
SHA256_Update(&ctx, &ol, 1); |
|
|
|
|
SHA256_Final(hash_out, &ctx); |
|
|
|
|
EVP_DigestUpdate(ctx, &ol, 1); |
|
|
|
|
EVP_DigestFinal_ex(ctx, hash_out, NULL); |
|
|
|
|
EVP_MD_CTX_free(ctx); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void _peers_table(const char* ch_id, char* buf, size_t sz) { |
|
|
|
|
@ -153,8 +154,8 @@ static int _recompute_bucket(struct UTUN_INSTANCE* inst, const char* ch_id,
|
|
|
|
|
sqlite3_stmt* stmt = NULL; |
|
|
|
|
if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) != SQLITE_OK) return -1; |
|
|
|
|
|
|
|
|
|
SHA256_CTX ctx; |
|
|
|
|
SHA256_Init(&ctx); |
|
|
|
|
EVP_MD_CTX* ctx = EVP_MD_CTX_new(); |
|
|
|
|
EVP_DigestInit_ex(ctx, EVP_sha256(), NULL); |
|
|
|
|
int count = 0; |
|
|
|
|
|
|
|
|
|
while (sqlite3_step(stmt) == SQLITE_ROW) { |
|
|
|
|
@ -167,12 +168,13 @@ static int _recompute_bucket(struct UTUN_INSTANCE* inst, const char* ch_id,
|
|
|
|
|
|
|
|
|
|
uint8_t mh[MS_HASH_SIZE]; |
|
|
|
|
_compute_member_hash(nid, x25, ed, sig, NULL, 0, online, mh); |
|
|
|
|
SHA256_Update(&ctx, mh, MS_HASH_SIZE); |
|
|
|
|
EVP_DigestUpdate(ctx, mh, MS_HASH_SIZE); |
|
|
|
|
count++; |
|
|
|
|
} |
|
|
|
|
sqlite3_finalize(stmt); |
|
|
|
|
|
|
|
|
|
if (count == 0) { |
|
|
|
|
EVP_MD_CTX_free(ctx); |
|
|
|
|
snprintf(sql, sizeof(sql), |
|
|
|
|
"DELETE FROM member_tree_hash WHERE channel_id=? AND level=? AND prefix64=?"); |
|
|
|
|
sqlite3_stmt* ds = NULL; |
|
|
|
|
@ -186,7 +188,8 @@ static int _recompute_bucket(struct UTUN_INSTANCE* inst, const char* ch_id,
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
uint8_t hash[MS_HASH_SIZE]; |
|
|
|
|
SHA256_Final(hash, &ctx); |
|
|
|
|
EVP_DigestFinal_ex(ctx, hash, NULL); |
|
|
|
|
EVP_MD_CTX_free(ctx); |
|
|
|
|
|
|
|
|
|
snprintf(sql, sizeof(sql), |
|
|
|
|
"INSERT OR REPLACE INTO member_tree_hash(channel_id, level, prefix64, hash, member_count)" |
|
|
|
|
|