From 9c80d0542e57d97d785b3f95b23561575f59da93 Mon Sep 17 00:00:00 2001 From: PolynomialDivision Date: Mon, 20 Nov 2017 12:19:44 +0100 Subject: [PATCH] switch to ubloop --- files/dawn.config | 1 + src/include/datastorage.h | 4 +++ src/main.c | 14 +++++------ src/storage/datastorage.c | 51 +++++++++++++++++++++++++++++++++++++++ src/utils/dawn_uci.c | 7 ++++++ src/utils/ubus.c | 36 ++++++++++++++++----------- 6 files changed, 92 insertions(+), 21 deletions(-) diff --git a/files/dawn.config b/files/dawn.config index 816506f..4051456 100644 --- a/files/dawn.config +++ b/files/dawn.config @@ -14,6 +14,7 @@ config settings times option update_client '50' option remove_client '120' option remove_probe '120' + option remove_ap '460' option update_hostapd '10' config settings metric diff --git a/src/include/datastorage.h b/src/include/datastorage.h index 0a66b3f..4e5dd02 100644 --- a/src/include/datastorage.h +++ b/src/include/datastorage.h @@ -8,6 +8,7 @@ #include #include #include +#include #ifndef ETH_ALEN #define ETH_ALEN 6 @@ -34,6 +35,7 @@ struct time_config_s { time_t update_client; time_t remove_client; time_t remove_probe; + time_t remove_ap; time_t update_hostapd; }; @@ -92,6 +94,8 @@ void print_auth_entry(auth_entry entry); void *remove_probe_array_thread(void *arg); +void uloop_add_data_cbs(); + /* AP, Client */ // ---------------- Structs ---------------- diff --git a/src/main.c b/src/main.c index e26efbc..fe69836 100644 --- a/src/main.c +++ b/src/main.c @@ -38,7 +38,7 @@ struct sigaction newSigAction; pthread_t tid_probe; pthread_t tid_client; -pthread_t tid_get_client; +//pthread_t tid_get_client; //pthread_t tid_update_hostapd_socks; pthread_t tid_kick_clients; pthread_t tid_ap; @@ -47,9 +47,9 @@ void daemon_shutdown() { // kill threads printf("Cancelling Threads!\n"); - pthread_cancel(tid_probe); - pthread_cancel(tid_client); - pthread_cancel(tid_get_client); + // pthread_cancel(tid_probe); + // pthread_cancel(tid_client); + //pthread_cancel(tid_get_client); //pthread_cancel(tid_update_hostapd_socks); // free ressources @@ -171,9 +171,9 @@ int main(int argc, char **argv) { init_socket_runopts(opt_broadcast_ip, opt_broadcast_port, 1); - pthread_create(&tid_probe, NULL, &remove_probe_array_thread, (void *) &time_config.remove_probe); - pthread_create(&tid_client, NULL, &remove_client_array_thread, (void *) &time_config.remove_client); - pthread_create(&tid_get_client, NULL, &update_clients_thread, (void *) &time_config.update_client); + //pthread_create(&tid_probe, NULL, &remove_probe_array_thread, (void *) &time_config.remove_probe); + //pthread_create(&tid_client, NULL, &remove_client_array_thread, (void *) &time_config.remove_client); + //pthread_create(&tid_get_client, NULL, &update_clients_thread, (void *) &time_config.update_client); //pthread_create(&tid_update_hostapd_socks, NULL, &update_hostapd_sockets, &time_config.update_hostapd); dawn_init_ubus(ubus_socket, opt_hostapd_dir); diff --git a/src/storage/datastorage.c b/src/storage/datastorage.c index f8c389c..b7783fc 100644 --- a/src/storage/datastorage.c +++ b/src/storage/datastorage.c @@ -1,6 +1,7 @@ #include "datastorage.h" #include +#include #include "ubus.h" #include "rssi.h" @@ -44,6 +45,24 @@ int probe_entry_last = -1; int client_entry_last = -1; int ap_entry_last = -1; +void remove_probe_array_cb(struct uloop_timeout *t); + +struct uloop_timeout probe_timeout = { + .cb = remove_probe_array_cb +}; + +void remove_client_array_cb(struct uloop_timeout *t); + +struct uloop_timeout client_timeout = { + .cb = remove_client_array_cb +}; + +void remove_ap_array_cb(struct uloop_timeout *t); + +struct uloop_timeout ap_timeout = { + .cb = remove_ap_array_cb +}; + int eval_probe_metric(struct probe_entry_s probe_entry) { int score = 0; @@ -539,6 +558,13 @@ void remove_old_ap_entries(time_t current_time, long long int threshold) { } } +void uloop_add_data_cbs() +{ + uloop_timeout_add(&probe_timeout); + uloop_timeout_add(&client_timeout); + uloop_timeout_add(&ap_timeout); +} + void *remove_probe_array_thread(void *arg) { printf("Removing thread with time: %lu\n", *(long int *) arg); time_t time_treshold = *(time_t *) arg; @@ -552,6 +578,14 @@ void *remove_probe_array_thread(void *arg) { return 0; } +void remove_probe_array_cb(struct uloop_timeout *t) { + pthread_mutex_lock(&probe_array_mutex); + printf("[Thread] : Removing old entries!\n"); + remove_old_probe_entries(time(0), timeout_config.remove_probe); + pthread_mutex_unlock(&probe_array_mutex); + uloop_timeout_set(&probe_timeout, timeout_config.remove_probe * 1000); +} + void *remove_client_array_thread(void *arg) { time_t time_treshold_client = *(time_t *) arg; printf("Removing client thread with time: %lu\n", time_treshold_client); @@ -565,6 +599,15 @@ void *remove_client_array_thread(void *arg) { return 0; } +void remove_client_array_cb(struct uloop_timeout *t) +{ + pthread_mutex_lock(&client_array_mutex); + printf("[Thread] : Removing old client entries!\n"); + remove_old_client_entries(time(0), timeout_config.update_client); + pthread_mutex_unlock(&client_array_mutex); + uloop_timeout_set(&client_timeout, timeout_config.update_client * 1000); +} + void *remove_ap_array_thread(void *arg) { time_t time_treshold_ap = *(time_t *) arg; printf("Removing ap thread with time: %lu\n", time_treshold_ap); @@ -578,6 +621,14 @@ void *remove_ap_array_thread(void *arg) { return 0; } +void remove_ap_array_cb(struct uloop_timeout *t) { + pthread_mutex_lock(&ap_array_mutex); + printf("[ULOOP] : Removing old ap entries!\n"); + remove_old_ap_entries(time(0), timeout_config.remove_ap); + pthread_mutex_unlock(&ap_array_mutex); + uloop_timeout_set(&ap_timeout, timeout_config.remove_ap * 1000); +} + void insert_client_to_array(client entry) { pthread_mutex_lock(&client_array_mutex); entry.time = time(0); diff --git a/src/utils/dawn_uci.c b/src/utils/dawn_uci.c index 378284b..186578c 100644 --- a/src/utils/dawn_uci.c +++ b/src/utils/dawn_uci.c @@ -63,6 +63,13 @@ struct time_config_s uci_get_time_config() { if (ptr.o->type == UCI_TYPE_STRING) ret.update_hostapd = atoi(ptr.o->v.string); + char tmp_remove_ap[] = "dawn.times.remove_ap"; + if (uci_lookup_ptr(c, &ptr, tmp_remove_ap, 1) != UCI_OK) { + uci_perror(c, "uci_get_daw_metric Error"); + return ret; + } + if (ptr.o->type == UCI_TYPE_STRING) + ret.remove_ap = atoi(ptr.o->v.string); printf("Times: %lu, %lu, %lu %lu\n", ret.update_client, ret.remove_client, ret.remove_probe, ret.update_hostapd); diff --git a/src/utils/ubus.c b/src/utils/ubus.c index 7c75645..b3d599e 100644 --- a/src/utils/ubus.c +++ b/src/utils/ubus.c @@ -14,6 +14,7 @@ #include "networksocket.h" #include "utils.h" #include "dawn_uci.h" +#include "datastorage.h" static struct ubus_context *ctx = NULL; static struct ubus_context *ctx_clients; /* own ubus conext otherwise strange behavior... */ @@ -22,10 +23,16 @@ static struct ubus_context *ctx_clients; /* own ubus conext otherwise strange be static struct ubus_subscriber hostapd_event; static struct blob_buf b; +void update_clients(struct uloop_timeout *t); + struct uloop_timeout hostapd_timer = { .cb = update_hostapd_sockets }; +struct uloop_timeout client_timer = { + .cb = update_clients +}; + #define MAX_HOSTAPD_SOCKETS 10 uint32_t hostapd_sock_arr[MAX_HOSTAPD_SOCKETS]; int hostapd_sock_last = -1; @@ -455,8 +462,19 @@ int dawn_init_ubus(const char *ubus_socket, char *hostapd_dir) { subscribe_to_hostapd(hostapd_dir); + // update hostapd uloop_timeout_add(&hostapd_timer); + // remove probe + //uloop_timeout_add(&probe_timeout); + uloop_add_data_cbs(); + + // get clients + const char *ubus_socket_clients = NULL; + ctx_clients = ubus_connect(ubus_socket_clients); + uloop_timeout_add(&client_timer); + + //ubus_call_umdns(); uloop_run(); @@ -587,24 +605,14 @@ static int ubus_get_clients() { return 0; } -void *update_clients_thread(void *arg) { - time_t time_update_client = *(time_t *) arg; - printf("Update client thread with time: %lu\n", time_update_client); - - const char *ubus_socket = NULL; - ctx_clients = ubus_connect(ubus_socket); - - while (1) { - sleep(time_update_client); - printf("[Thread] : Kicking clients!\n"); - ubus_get_clients(); - } - return 0; +void update_clients(struct uloop_timeout *t) { + ubus_get_clients(); + uloop_timeout_set(&client_timer, timeout_config.update_client * 1000); } void update_hostapd_sockets(struct uloop_timeout *t) { - uloop_timeout_set(&hostapd_timer, timeout_config.update_hostapd); subscribe_to_hostapd_interfaces(hostapd_dir_glob); + uloop_timeout_set(&hostapd_timer, timeout_config.update_hostapd * 1000); } void del_client_all_interfaces(const uint8_t *client_addr, uint32_t reason, uint8_t deauth, uint32_t ban_time) {