Implemented DHT

This commit is contained in:
t
2025-01-10 01:59:20 +01:00
parent b06a71186a
commit 3c3e8127c9
6 changed files with 258 additions and 54 deletions
+58
View File
@@ -0,0 +1,58 @@
#include "lookup.h"
void init_lookup_table()
{
table = malloc(sizeof(lookup_head));
if (table == NULL) return;
table->head = NULL;
}
void push_lookup(lookup_request *request)
{
if (free_space == 0)
{
lookup_table *infront = table->head;
while (infront->next != NULL && infront->next->next != NULL)
{
infront = infront->next;
}
free(infront->next->request);
free(infront->next);
infront->next = NULL;
// if malloc fails this does not leak space
free_space++;
}
lookup_table *new = (lookup_table*)malloc(sizeof(lookup_table));
if (new == NULL) return;
new->request = request;
new->next = table->head;
table->head = new;
free_space--;
}
lookup_request *find_lookup(const uint16_t hash_id)
{
const lookup_table *current = table->head;
while (current != NULL && current->request->hash_id != hash_id)
current = current->next;
return current == NULL ? NULL : current->request;
}
void free_lookup_table()
{
if (table == NULL) return;
lookup_table *current = table->head;
while (current != NULL)
{
lookup_table *temp = current;
current = current->next;
free(temp->request);
free(temp);
}
free(table);
table = NULL;
free_space = TABLE_SIZE;
}