summaryrefslogtreecommitdiff
path: root/src/packet_storage.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/packet_storage.c')
-rw-r--r--src/packet_storage.c72
1 files changed, 41 insertions, 31 deletions
diff --git a/src/packet_storage.c b/src/packet_storage.c
index 569eed4..39cff5c 100644
--- a/src/packet_storage.c
+++ b/src/packet_storage.c
@@ -48,11 +48,11 @@ struct author_entry {
} *author_storage;
struct recipient_entry {
- public_key recipient;
- uint64_t packet_head;
- uint64_t next;
- uint64_t prev;
- bool is_full;
+ recipient recipient;
+ uint64_t packet_head;
+ uint64_t next;
+ uint64_t prev;
+ bool is_full;
} *recipient_storage;
pthread_mutex_t author_storage_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -154,12 +154,12 @@ void save_author_packet_hash(const public_key author, const hash packet_hash) {
fclose(f);
}
-void save_recipient_packet_hash(const public_key recipient, const hash packet_hash) {
+void save_recipient_packet_hash(const recipient recipient, const hash packet_hash) {
const char *dir = "packets_by_recipient";
mkdir(dir, 0755);
- char recipient_hex[ 65 ];
- sprintf(recipient_hex, hash_fmt, hash_ref(recipient));
+ char recipient_hex[ 129 ];
+ sprintf(recipient_hex, hash_fmt hash_fmt, hash_ref(recipient), hash_ref((recipient + 32)));
char filename[ 256 ];
snprintf(filename, sizeof(filename), "%s/%s.list", dir, recipient_hex);
@@ -227,7 +227,7 @@ uint64_t get_author_entry(const public_key auth_key, bool create) {
return invalid_index;
}
-uint64_t get_recipient_entry(const public_key recip_key, bool create) {
+uint64_t get_recipient_entry(const recipient recip_key, bool create) {
uint64_t hash_val;
memcpy(&hash_val, recip_key, sizeof(uint64_t));
uint64_t start_index = hash_val % max_recipients;
@@ -237,7 +237,7 @@ uint64_t get_recipient_entry(const public_key recip_key, bool create) {
struct recipient_entry *current = &recipient_storage[ cur_index ];
while (true) {
- if (current->is_full && memcmp(current->recipient, recip_key, 32) == 0) {
+ if (current->is_full && memcmp(current->recipient, recip_key, 64) == 0) {
pthread_mutex_unlock(&recipient_storage_mutex);
return cur_index;
}
@@ -252,7 +252,7 @@ uint64_t get_recipient_entry(const public_key recip_key, bool create) {
}
if (!current->is_full) {
- memcpy(current->recipient, recip_key, 32);
+ memcpy(current->recipient, recip_key, 64);
current->packet_head = invalid_index;
current->is_full = true;
current->next = invalid_index;
@@ -266,7 +266,7 @@ uint64_t get_recipient_entry(const public_key recip_key, bool create) {
while (probes < max_recipients) {
struct recipient_entry *empty = &recipient_storage[ lp_idx ];
if (!empty->is_full) {
- memcpy(empty->recipient, recip_key, 32);
+ memcpy(empty->recipient, recip_key, 64);
empty->packet_head = invalid_index;
empty->is_full = true;
empty->next = invalid_index;
@@ -343,22 +343,32 @@ uint64_t set_data_packet(struct data_packet *new_packet) {
struct data_packet_entry *current_entry = &packet_index[ cur_index ];
while (true) {
- if (current_entry->is_full
- && memcmp(
- ((struct data_packet *) current_entry->compressed_payload)->hash,
- new_packet->hash,
- sizeof(hash))
- == 0) {
- arena_free(current_entry->compressed_payload);
- current_entry->compressed_payload = compressed_payload;
- current_entry->compressed_size = compressed_len;
- pthread_mutex_unlock(&packet_storage_mutex);
- save_author_packet_hash(new_packet->contents.author, new_packet->hash);
- for (uint8_t i = 0; i < new_packet->contents.recipient_count; i++) {
- save_recipient_packet_hash(new_packet->contents.recipients[ i ], new_packet->hash);
+ if (current_entry->is_full) {
+ static __thread byte decompressed[ sizeof(struct data_packet) ];
+ uLongf decompressed_len = sizeof(struct data_packet);
+ if (uncompress(
+ decompressed,
+ &decompressed_len,
+ current_entry->compressed_payload,
+ current_entry->compressed_size)
+ == Z_OK) {
+ if (memcmp(
+ ((struct data_packet *) decompressed)->hash, new_packet->hash, sizeof(hash))
+ == 0) {
+ arena_free(current_entry->compressed_payload);
+ current_entry->compressed_payload = compressed_payload;
+ current_entry->compressed_size = compressed_len;
+ pthread_mutex_unlock(&packet_storage_mutex);
+ save_author_packet_hash(new_packet->contents.author, new_packet->hash);
+ for (uint8_t i = 0; i < new_packet->contents.recipient_count; i++) {
+ save_recipient_packet_hash(
+ new_packet->contents.recipients[ i ], new_packet->hash);
+ }
+ return cur_index;
+ }
}
- return cur_index;
}
+
if (current_entry->next == invalid_index) break;
cur_index = current_entry->next;
current_entry = &packet_index[ cur_index ];
@@ -575,11 +585,11 @@ bool _delete_at_index(uint64_t index) {
for (uint8_t i = 0;
i < ((struct data_packet *) current_entry->compressed_payload)->contents.recipient_count;
i++) {
- public_key recip_key;
+ recipient recip_key;
memcpy(
recip_key,
((struct data_packet *) current_entry->compressed_payload)->contents.recipients[ i ],
- 32);
+ 64);
uint64_t recip_idx = get_recipient_entry(recip_key, false);
if (recip_idx != invalid_index) {
if (recipient_storage[ recip_idx ].packet_head == index) {
@@ -732,7 +742,7 @@ uint16_t get_packets_for_author(
}
uint16_t get_packets_for_recipient(
- const public_key recipient,
+ const recipient recipient,
uint64_t offset,
uint16_t max_count,
struct data_packet *out_packets,
@@ -776,8 +786,8 @@ uint16_t get_packets_for_recipient(
}
pthread_mutex_unlock(&packet_storage_mutex);
- char recipient_hex[ 65 ];
- sprintf(recipient_hex, hash_fmt, hash_ref(recipient));
+ char recipient_hex[ 129 ];
+ sprintf(recipient_hex, hash_fmt hash_fmt, hash_ref(recipient), hash_ref((recipient + 32)));
char filename[ 256 ];
snprintf(filename, sizeof(filename), "packets_by_recipient/%s.list", recipient_hex);