From 9f024afe64da72e6b8983fb28ba59f22ed045432 Mon Sep 17 00:00:00 2001 From: PolynomialDivision Date: Mon, 5 Jun 2017 17:39:53 +0200 Subject: [PATCH] Add remove thread --- src/datastorage.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++- src/datastorage.h | 4 +++ src/main.c | 3 +++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/datastorage.c b/src/datastorage.c index 9355c9b..a4a1a60 100644 --- a/src/datastorage.c +++ b/src/datastorage.c @@ -9,11 +9,15 @@ int mac_is_greater(uint8_t addr1[], uint8_t addr2[]); void print_probe_entry(probe_entry entry); node* delete_probe_req(node* head, uint8_t bssid_addr[], uint8_t client_addr[]); int mac_is_first_in_list(node* head, uint8_t bssid_addr[], uint8_t client_addr[]); +node* remove_node(node* head, node* curr, node* prev); +node* remove_old_entries(node* head, time_t current_time, long long int threshold); void insert_to_list(probe_entry entry) { pthread_mutex_lock(&list_mutex); + entry.time = time(0); + // first delete probe request probe_list_head = delete_probe_req(probe_list_head, entry.bssid_addr, entry.client_addr); probe_list_head = insert(probe_list_head, entry); @@ -114,7 +118,6 @@ node* insert(node* head, probe_entry entry) { node* delete_probe_req(node* head, uint8_t bssid_addr[], uint8_t client_addr[]) { - if(!head) { return head; @@ -152,6 +155,62 @@ node* delete_probe_req(node* head, uint8_t bssid_addr[], uint8_t client_addr[]) return head; } +void *remove_thread(void *arg) +{ + while(1) + { + sleep(TIME_THRESHOLD); + pthread_mutex_lock(&list_mutex); + printf("Removing old entries now!\n"); + probe_list_head = remove_old_entries(probe_list_head, time(0), TIME_THRESHOLD); + pthread_mutex_unlock(&list_mutex); + print_list(); + } + return 0; +} + +node* remove_old_entries(node* head, time_t current_time, long long int threshold) +{ + if(head) + { + node *prev = NULL; + node *next = head; + while(next) + { + printf("Going next...\n"); + printf("Entry Time: %ld, Curr Time: %lld\n", next->data.time, current_time - threshold); + if(next->data.time < current_time - threshold) + { + printf("Removing node!\n"); + head = remove_node(head, next, prev); + } + prev = next; + next = next->ptr; + } + } + + return head; +} + +// return headpointer +node* remove_node(node* head, node* curr, node* prev) +{ + if(curr == head) + { + node *temp = head; + head = head->ptr; + free(temp); + } + else + { + node *temp = curr; + prev->ptr = curr->ptr; + free(temp); + } + printf("Removed old entry!\n"); + return head; +} + int mac_is_first_in_list(node* head, uint8_t bssid_addr[], uint8_t client_addr[]) { if(!head) @@ -224,6 +283,7 @@ void print_list() node* head = probe_list_head; if(!head) { + printf("------------------\n"); return; } node* next; diff --git a/src/datastorage.h b/src/datastorage.h index 92a6b29..9c82f8a 100644 --- a/src/datastorage.h +++ b/src/datastorage.h @@ -7,12 +7,14 @@ #include #include #include +#include #ifndef ETH_ALEN #define ETH_ALEN 6 #endif #define SORT_NUM 5 +#define TIME_THRESHOLD 60 // every minute // Probe entrys typedef struct { @@ -37,6 +39,8 @@ void print_list(); void insert_to_list(probe_entry entry); int mac_first_in_probe_list(uint8_t bssid_addr[], uint8_t client_addr[]); +void *remove_thread(void *arg); + pthread_mutex_t list_mutex; node* probe_list_head; char sort_string[SORT_NUM]; diff --git a/src/main.c b/src/main.c index 127bba9..6911c86 100644 --- a/src/main.c +++ b/src/main.c @@ -39,6 +39,9 @@ int main(int argc, char **argv) argv += optind; init_socket_runopts(opt_broadcast_ip, opt_broadcast_port); + + pthread_t tid; + pthread_create(&tid, NULL, &remove_thread, NULL); dawn_init_ubus(ubus_socket);