summaryrefslogtreecommitdiff
path: root/src/server.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/server.c')
-rw-r--r--src/server.c76
1 files changed, 73 insertions, 3 deletions
diff --git a/src/server.c b/src/server.c
index 98680a3..71722ba 100644
--- a/src/server.c
+++ b/src/server.c
@@ -217,7 +217,7 @@ void paste_server(
relay_info2s(info, output_data + 1);
- reply(1 + 237, output_data);
+ reply(1 + 245, output_data);
return;
}
@@ -428,7 +428,7 @@ void paste_server(
uint64_t storage_index = set_data_packet(&packet);
firehose_push(packet.hash, storage_index);
- if (storage_index != minus_one) {
+ if (storage_index != invalid_index) {
output_data[ 1 ] = true;
save_packet(packet);
@@ -563,6 +563,75 @@ void paste_server(
reply(2, __reply);
} break;
+ case query_packets: {
+ if (in_len != 1 + 32 + 32 + 64 + 2 + 8) {
+ *out = NULL;
+ *out_len = 0;
+ return;
+ }
+ public_key author_enc, author_sign;
+ signature sig;
+ size_t off = 1;
+ memcpy(author_enc, in + off, 32);
+ off += 32;
+ memcpy(author_sign, in + off, 32);
+ off += 32;
+ memcpy(sig, in + off, 64);
+ off += 64;
+ uint16_t req_count = read_u16_le(in + off);
+ off += 2;
+ uint64_t req_offset = read_u64_le(in + off);
+ off += 8;
+ byte msg[ 32 + 32 + 2 + 8 ];
+ memcpy(msg, author_enc, 32);
+ memcpy(msg + 32, author_sign, 32);
+ write_u16_le(msg + 64, req_count);
+ write_u64_le(msg + 66, req_offset);
+ if (!verify_raw_signature(msg, sizeof(msg), sig, author_sign)) {
+ *out = NULL;
+ *out_len = 0;
+ return;
+ }
+ if (req_count > 128) req_count = 128;
+ struct data_packet* packets = malloc((size_t) req_count * sizeof(struct data_packet));
+ if (!packets) {
+ *out = NULL;
+ *out_len = 0;
+ return;
+ }
+ uint64_t new_offset;
+ uint16_t actual_count
+ = get_packets_for_author(author_enc, req_offset, req_count, packets, &new_offset);
+ uint16_t verified_count = 0;
+ for (uint16_t i = 0; i < actual_count; i++) {
+ if (memcmp(packets[ i ].contents.author_signing_pub, author_sign, 32) == 0) {
+ packets[ verified_count++ ] = packets[ i ];
+ }
+ }
+ size_t _out_len = 1 + 2 + 8 + (size_t) verified_count * PACKET_SIZE;
+ byte* _out = malloc(_out_len);
+ if (!out) {
+ free(packets);
+ *out = NULL;
+ *out_len = 0;
+ return;
+ }
+ off = 0;
+ _out[ off++ ] = queried_author_packets;
+ write_u16_le(_out + off, verified_count);
+ off += 2;
+ write_u64_le(_out + off, new_offset);
+ off += 8;
+ for (uint16_t i = 0; i < verified_count; i++) {
+ data_packet2s(packets[ i ], _out + off);
+ off += PACKET_SIZE;
+ }
+ free(packets);
+ *out = _out;
+ *out_len = _out_len;
+ return;
+ } break;
+
default: {
log(_info, "<%.*s> request type: invalid method", (int) ip_size, ip_addr);
@@ -924,12 +993,13 @@ int main(int argc, char** argv) {
// init
packet_storage = alloc_map(max_storage * sizeof(struct data_packet_entry));
+ author_storage = alloc_map(max_authors * sizeof(struct author_entry));
neighbor_storage = alloc_map(max_neighbors * sizeof(struct relay_neighbor));
empty_neighbor_slots = alloc_map(max_neighbors * sizeof(bool));
memset(empty_neighbor_slots, 1, max_neighbors * sizeof(bool));
- if (!packet_storage || !neighbor_storage || !empty_neighbor_slots) {
+ if (!packet_storage || !author_storage || !neighbor_storage || !empty_neighbor_slots) {
perror("init alloc failed");
return 1;
}