diff --git a/src/include/datastorage.h b/src/include/datastorage.h index 52c4343..d50f39f 100644 --- a/src/include/datastorage.h +++ b/src/include/datastorage.h @@ -14,10 +14,10 @@ #endif #define SORT_NUM 5 -#define TIME_THRESHOLD 60 // every minute +#define TIME_THRESHOLD 5 // every minute // Probe entrys -typedef struct { +typedef struct probe_entry_s { uint8_t bssid_addr[ETH_ALEN]; uint8_t client_addr[ETH_ALEN]; uint8_t target_addr[ETH_ALEN]; @@ -64,6 +64,39 @@ typedef struct { uint32_t mfp; } client; + +// Array + +#define ARRAY_LEN 1000 + +struct probe_entry_s probe_array[ARRAY_LEN]; + +void insert_to_array(probe_entry entry, int inc_counter); +void probe_array_insert(probe_entry entry); +probe_entry* probe_array_delete(probe_entry entry); +void print_array(); +void *remove_array_thread(void *arg); + +pthread_mutex_t probe_array_mutex; + + + + + + + + + + + + + + + + + + + // List typedef struct node { probe_entry data; diff --git a/src/main.c b/src/main.c index 39ce5bc..e03d91e 100644 --- a/src/main.c +++ b/src/main.c @@ -48,10 +48,16 @@ int main(int argc, char **argv) { return 1; } + if (pthread_mutex_init(&probe_array_mutex, NULL) != 0) { + printf("\n mutex init failed\n"); + return 1; + } + init_socket_runopts(opt_broadcast_ip, opt_broadcast_port, 0); pthread_t tid; - pthread_create(&tid, NULL, &remove_thread, NULL); + pthread_create(&tid, NULL, &remove_array_thread, NULL); + //pthread_create(&tid, NULL, &remove_thread, NULL); dawn_init_ubus(ubus_socket, opt_hostapd_dir); diff --git a/src/network/networksocket.c b/src/network/networksocket.c index f4915a5..cf30bdf 100644 --- a/src/network/networksocket.c +++ b/src/network/networksocket.c @@ -75,8 +75,10 @@ void *receive_msg(void *args) { printf("Parsed: '%s'\n", str); parse_to_probe_req(b.head, &prob_req); + insert_to_array(prob_req, 0); + // insert to list - insert_to_list(prob_req, 0); + //insert_to_list(prob_req, 0); } } diff --git a/src/storage/datastorage.c b/src/storage/datastorage.c index 6760558..31d6788 100644 --- a/src/storage/datastorage.c +++ b/src/storage/datastorage.c @@ -9,6 +9,142 @@ int go_next(char sort_order[], int i, probe_entry entry, int mac_is_equal(uint8_t addr1[], uint8_t addr2[]); int mac_is_greater(uint8_t addr1[], uint8_t addr2[]); void print_probe_entry(probe_entry entry); +void remove_old_probe_entries(time_t current_time, long long int threshold); + +int probe_entry_last = -1; + + +void probe_array_insert(probe_entry entry) +{ + if(probe_entry_last == -1) + { + probe_array[0] = entry; + probe_entry_last++; + return; + } + + int i; + for(i = 0; i <= probe_entry_last; i++) + { + if(!go_next(sort_string, SORT_NUM, entry, probe_array[i])) + { + break; + } + } + for(int j = probe_entry_last; j >= i; j--) + { + if(j + 1 <= ARRAY_LEN) + { + probe_array[j + 1] = probe_array[j]; + } + } + probe_array[i] = entry; + + if(probe_entry_last < ARRAY_LEN) + { + probe_entry_last++; + } +} + +probe_entry* probe_array_delete(probe_entry entry) +{ + int i; + int found_in_array = 0; + probe_entry* tmp = NULL; + + if(probe_entry_last == -1) + { + return NULL; + } + + for(i = 0; i <= probe_entry_last; i++) + { + if(mac_is_equal(entry.bssid_addr, probe_array[i].bssid_addr) && + mac_is_equal(entry.client_addr, probe_array[i].client_addr)) + { + found_in_array = 1; + tmp = &probe_array[i]; + break; + } + } + + for(int j = i; j <= probe_entry_last; j++) + { + probe_array[j] = probe_array[j + 1]; + } + + if(probe_entry_last > -1 && found_in_array) + { + probe_entry_last--; + } + return tmp; +} + +void print_array() +{ + printf("------------------\n"); + printf("Probe Entry Last: %d\n", probe_entry_last); + for(int i = 0; i <= probe_entry_last; i++) + { + print_probe_entry(probe_array[i]); + } + printf("------------------\n"); +} + +void insert_to_array(probe_entry entry, int inc_counter) +{ + pthread_mutex_lock(&probe_array_mutex); + + entry.time = time(0); + entry.counter = 0; + probe_entry* tmp = probe_array_delete(entry); + + if(tmp != NULL) + { + entry.counter = tmp->counter; + } + + if (inc_counter) + { + entry.counter++; + } + + probe_array_insert(entry); + + pthread_mutex_unlock(&probe_array_mutex); +} + +void remove_old_probe_entries(time_t current_time, long long int threshold) +{ + for(int i = 0; i < probe_entry_last; i++) + { + if (probe_array[i].time < current_time - threshold) + { + probe_array_delete(probe_array[i]); + } + } +} + +void *remove_array_thread(void *arg) { + while (1) { + sleep(TIME_THRESHOLD); + pthread_mutex_lock(&probe_array_mutex); + printf("[Thread] : Removing old entries!\n"); + remove_old_probe_entries(time(0), TIME_THRESHOLD); + pthread_mutex_unlock(&probe_array_mutex); + } + return 0; +} + + + + + + + + + + node *delete_probe_req(node **ret_remove, node *head, uint8_t bssid_addr[], uint8_t client_addr[]); int mac_is_first_in_list(node *head, uint8_t bssid_addr[], @@ -24,6 +160,7 @@ void insert_to_list(probe_entry entry, int inc_counter) { entry.time = time(0); entry.counter = 0; + // first delete probe request // probe_list_head = remove_old_entries(probe_list_head, time(0), // TIME_THRESHOLD); diff --git a/src/utils/ubus.c b/src/utils/ubus.c index cf9895c..99fbe8d 100644 --- a/src/utils/ubus.c +++ b/src/utils/ubus.c @@ -86,7 +86,7 @@ static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj, static int add_subscriber(char *name); static int subscribe_to_hostapd_interfaces(char *hostapd_dir); static int ubus_get_clients(); - +/* static int decide_function(probe_entry *prob_req) { // TODO: Refactor... if (prob_req->counter < MIN_PROBE_REQ) { @@ -102,7 +102,7 @@ static int decide_function(probe_entry *prob_req) { } return ret; } - +*/ static void hostapd_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s, uint32_t id) { fprintf(stderr, "Object %08x went away\n", id); @@ -136,7 +136,8 @@ static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj, struct blob_attr *msg) { probe_entry prob_req; parse_to_probe_req(msg, &prob_req); - insert_to_list(prob_req, 1); + //insert_to_list(prob_req, 1); + insert_to_array(prob_req, 1); // send probe via network char *str; @@ -145,14 +146,14 @@ static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj, printf("[WC] Hostapd-Probe: %s : %s\n", method, str); - print_list(); + print_array(); // sleep(2); // sleep for 2s // deny access - if (!decide_function(&prob_req)) { - return UBUS_STATUS_UNKNOWN_ERROR; - } + //if (!decide_function(&prob_req)) { + // return UBUS_STATUS_UNKNOWN_ERROR; + //} // allow access return 0;