From 454e44cf5d4d7811f402219f8bd4d53cf0714643 Mon Sep 17 00:00:00 2001 From: PolynomialDivision Date: Sat, 15 Jul 2017 23:20:16 +0200 Subject: [PATCH 1/3] Add insert and delete function --- src/include/datastorage.h | 35 +++++++++++- src/main.c | 9 ++- src/storage/datastorage.c | 117 ++++++++++++++++++++++++++++++++++++++ src/utils/ubus.c | 17 +++--- 4 files changed, 167 insertions(+), 11 deletions(-) diff --git a/src/include/datastorage.h b/src/include/datastorage.h index 982b020..9415135 100644 --- a/src/include/datastorage.h +++ b/src/include/datastorage.h @@ -17,7 +17,7 @@ #define TIME_THRESHOLD 60 // 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]; @@ -27,6 +27,39 @@ typedef struct { int counter; } probe_entry; + +// 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(); + +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..e32a8a8 100644 --- a/src/main.c +++ b/src/main.c @@ -48,10 +48,15 @@ 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_t tid; + //pthread_create(&tid, NULL, &remove_thread, NULL); dawn_init_ubus(ubus_socket, opt_hostapd_dir); diff --git a/src/storage/datastorage.c b/src/storage/datastorage.c index 6760558..75ca2e1 100644 --- a/src/storage/datastorage.c +++ b/src/storage/datastorage.c @@ -9,6 +9,122 @@ 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); + +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); +} + + + + + + + + + + + + + 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 +140,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 47939c6..4434ede 100644 --- a/src/utils/ubus.c +++ b/src/utils/ubus.c @@ -42,7 +42,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) { @@ -58,7 +58,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); @@ -92,23 +92,24 @@ 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; str = blobmsg_format_json(msg, true); - send_string(str); + //send_string(str); 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; From a1d488b633761e7b43f209c477e11a0c5620b040 Mon Sep 17 00:00:00 2001 From: PolynomialDivision Date: Sun, 16 Jul 2017 11:09:09 +0200 Subject: [PATCH 2/3] Remove old entries --- src/include/datastorage.h | 3 ++- src/main.c | 3 ++- src/storage/datastorage.c | 26 +++++++++++++++++++++++--- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/include/datastorage.h b/src/include/datastorage.h index 9415135..70c8592 100644 --- a/src/include/datastorage.h +++ b/src/include/datastorage.h @@ -14,7 +14,7 @@ #endif #define SORT_NUM 5 -#define TIME_THRESHOLD 60 // every minute +#define TIME_THRESHOLD 5 // every minute // Probe entrys typedef struct probe_entry_s { @@ -38,6 +38,7 @@ 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; diff --git a/src/main.c b/src/main.c index e32a8a8..e03d91e 100644 --- a/src/main.c +++ b/src/main.c @@ -55,7 +55,8 @@ int main(int argc, char **argv) { init_socket_runopts(opt_broadcast_ip, opt_broadcast_port, 0); - //pthread_t tid; + pthread_t tid; + 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/storage/datastorage.c b/src/storage/datastorage.c index 75ca2e1..31d6788 100644 --- a/src/storage/datastorage.c +++ b/src/storage/datastorage.c @@ -9,6 +9,7 @@ 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; @@ -93,7 +94,7 @@ void print_array() 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); @@ -112,9 +113,28 @@ void insert_to_array(probe_entry entry, int inc_counter) 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; +} From 81f4813ce81a631858fbbbd541bcd3e72c735259 Mon Sep 17 00:00:00 2001 From: PolynomialDivision Date: Sun, 16 Jul 2017 11:23:06 +0200 Subject: [PATCH 3/3] Enable network receiving probe request --- src/network/networksocket.c | 4 +++- src/utils/ubus.c | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) 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/utils/ubus.c b/src/utils/ubus.c index 4434ede..e07e87f 100644 --- a/src/utils/ubus.c +++ b/src/utils/ubus.c @@ -98,7 +98,7 @@ static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj, // send probe via network char *str; str = blobmsg_format_json(msg, true); - //send_string(str); + send_string(str); printf("[WC] Hostapd-Probe: %s : %s\n", method, str);