summaryrefslogtreecommitdiff
path: root/server.c
diff options
context:
space:
mode:
authorGravatar moon <moon@coffin.ir> 2025-09-12 19:29:17 +0330
committerGravatar moon <moon@coffin.ir> 2025-09-12 19:29:17 +0330
commitcad89335a2d03921a0f349673114e0f69adc54d4 (patch)
tree570e84d433abe4b91254f9d5fa15e94b3baa065f /server.c
parentstarting commit (diff)
bug fixes
Diffstat (limited to 'server.c')
-rw-r--r--server.c38
1 files changed, 32 insertions, 6 deletions
diff --git a/server.c b/server.c
index 9c1a710..9715467 100644
--- a/server.c
+++ b/server.c
@@ -1,3 +1,4 @@
+#include <dirent.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
@@ -403,12 +404,14 @@ void paste_server(
save_packet(packet);
- struct data_packet* thread_arg = malloc(sizeof(struct data_packet));
- if (thread_arg) {
- *thread_arg = packet;
- pthread_t prop_thread;
- pthread_create(&prop_thread, NULL, propagate_packet, thread_arg);
- pthread_detach(prop_thread);
+ if (((float) rand() / (float) RAND_MAX) > 0.5) {
+ struct data_packet* thread_arg = malloc(sizeof(struct data_packet));
+ if (thread_arg) {
+ *thread_arg = packet;
+ pthread_t prop_thread;
+ pthread_create(&prop_thread, NULL, propagate_packet, thread_arg);
+ pthread_detach(prop_thread);
+ }
}
} else {
output_data[ 1 ] = false;
@@ -477,6 +480,11 @@ void paste_server(
return;
}
+ case register_neighbor: {
+ // TODO
+ }
+ // break;
+
default: {
log(_info, "<%.*s> request type: invalid method\n", (int) ip_size, ip_addr);
@@ -611,6 +619,7 @@ void* cleanup_packets(void* arg) {
uint64_t current_time_ms = get_current_time_ms();
uint64_t retention_ms = (uint64_t) server_stats.retention_hours * 60 * 60 * 1000;
+ // memory
for (uint64_t i = 0; i < max_storage; i++) {
pthread_mutex_lock(&packet_storage_mutex);
if (packet_storage[ i ].is_full
@@ -633,6 +642,23 @@ void* cleanup_packets(void* arg) {
}
pthread_mutex_unlock(&packet_storage_mutex);
}
+
+ DIR* dir = opendir("packets");
+ if (dir) {
+ struct dirent* entry;
+ while ((entry = readdir(dir)) != NULL) {
+ if (entry->d_type == DT_REG) {
+ char filepath[ 256 ];
+ snprintf(filepath, sizeof(filepath), "packets/%s", entry->d_name);
+ struct stat st;
+ if (stat(filepath, &st) == 0
+ && current_time_ms - (uint64_t) st.st_mtime * 1000 > retention_ms) {
+ if (remove(filepath) != 0) log(_error, "failed to delete %s\n", filepath);
+ }
+ }
+ }
+ closedir(dir);
+ }
}
return NULL;