From ed725710d767675f39d999fcf864756e531e0717 Mon Sep 17 00:00:00 2001 From: PolynomialDivision Date: Sun, 19 Nov 2017 20:40:03 +0100 Subject: [PATCH 1/2] remove prints --- src/include/ubus.h | 4 +++- src/main.c | 6 +++--- src/utils/ubus.c | 50 ++++++++++++++++++++++++++++------------------ 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/src/include/ubus.h b/src/include/ubus.h index 9c6a2b6..ce91d3a 100644 --- a/src/include/ubus.h +++ b/src/include/ubus.h @@ -2,6 +2,8 @@ #define __DAWN_UBUS_H #include +#include + #include "datastorage.h" #define MIN_PROBE_REQ 2 // TODO: Parse from config file... @@ -28,6 +30,6 @@ int ubus_call_umdns(); int ubus_send_probe_via_network(struct probe_entry_s probe_entry); -void *update_hostapd_sockets(void *arg); +void update_hostapd_sockets(struct uloop_timeout *t); #endif diff --git a/src/main.c b/src/main.c index 4d7ff8e..b147250 100644 --- a/src/main.c +++ b/src/main.c @@ -39,7 +39,7 @@ struct sigaction newSigAction; pthread_t tid_probe; pthread_t tid_client; pthread_t tid_get_client; -pthread_t tid_update_hostapd_socks; +//pthread_t tid_update_hostapd_socks; pthread_t tid_kick_clients; pthread_t tid_ap; @@ -50,7 +50,7 @@ void daemon_shutdown() { pthread_cancel(tid_probe); pthread_cancel(tid_client); pthread_cancel(tid_get_client); - pthread_cancel(tid_update_hostapd_socks); + //pthread_cancel(tid_update_hostapd_socks); // free ressources printf("Freeing mutex ressources\n"); @@ -173,7 +173,7 @@ int main(int argc, char **argv) { 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); + //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/utils/ubus.c b/src/utils/ubus.c index 1920e16..923424f 100644 --- a/src/utils/ubus.c +++ b/src/utils/ubus.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include @@ -16,11 +17,15 @@ static struct ubus_context *ctx = NULL; static struct ubus_context *ctx_clients; /* own ubus conext otherwise strange behavior... */ -static struct ubus_context *ctx_hostapd; +//static struct ubus_context *ctx_hostapd; static struct ubus_subscriber hostapd_event; static struct blob_buf b; +struct uloop_timeout hostapd_timer = { + .cb = update_hostapd_sockets +}; + #define MAX_HOSTAPD_SOCKETS 10 uint32_t hostapd_sock_arr[MAX_HOSTAPD_SOCKETS]; int hostapd_sock_last = -1; @@ -194,7 +199,7 @@ blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const uint8_t *addr) static int decide_function(probe_entry *prob_req) { // TODO: Refactor... - //printf("COUNTER: %d\n", prob_req->counter); + printf("COUNTER: %d\n", prob_req->counter); if (prob_req->counter < dawn_metric.min_probe_count) { return 0; @@ -212,7 +217,7 @@ static int decide_function(probe_entry *prob_req) { static void hostapd_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s, uint32_t id) { fprintf(stderr, "Object %08x went away\n", id); - //ubus_unsubscribe(ctx, &hostapd_event, id); + ubus_unsubscribe(ctx, s, id); hostapd_array_delete(id); } @@ -330,7 +335,7 @@ static int handle_probe_req(struct blob_attr *msg) { printf("[WC] Hostapd-Probe: %s : %s\n", "probe", str); - print_probe_array(); + //print_probe_array(); /* // deny access if (!decide_function(&tmp_probe)) { @@ -362,17 +367,19 @@ static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj, static int add_subscriber(char *name) { uint32_t id = 0; - if (ubus_lookup_id(ctx_hostapd, name, &id)) { + printf("DOING LOOKUP!\n"); + if (ubus_lookup_id(ctx, name, &id)) { fprintf(stderr, "Failed to look up test object for %s\n", name); return -1; } + printf("Lookup success!\n"); if(hostapd_array_check_id(id)) { return 0; } - int ret = ubus_subscribe(ctx_hostapd, &hostapd_event, id); + int ret = ubus_subscribe(ctx, &hostapd_event, id); hostapd_array_insert(id); fprintf(stderr, "Watching object %08x: %s\n", id, ubus_strerror(ret)); @@ -383,7 +390,7 @@ static int subscribe_to_hostapd_interfaces(char *hostapd_dir) { DIR *dirp; struct dirent *entry; - if(ctx_hostapd == NULL) + if(ctx == NULL) { return 0; } @@ -406,13 +413,13 @@ static int subscribe_to_hostapd_interfaces(char *hostapd_dir) { static int subscribe_to_hostapd(char *hostapd_dir) { - if(ctx_hostapd == NULL) + if(ctx == NULL) { return 0; } printf("Registering ubus event subscriber!\n"); - int ret = ubus_register_subscriber(ctx_hostapd, &hostapd_event); + int ret = ubus_register_subscriber(ctx, &hostapd_event); if (ret) { fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret)); return -1; @@ -446,7 +453,9 @@ int dawn_init_ubus(const char *ubus_socket, char *hostapd_dir) { // set dawn metric dawn_metric = uci_get_dawn_metric(); - //subscribe_to_hostapd(hostapd_dir); + subscribe_to_hostapd(hostapd_dir); + + uloop_timeout_add(&hostapd_timer); //ubus_call_umdns(); @@ -593,18 +602,21 @@ void *update_clients_thread(void *arg) { return 0; } -void *update_hostapd_sockets(void *arg) { - time_t time_update_hostapd = *(time_t *) arg; +void update_hostapd_sockets(struct uloop_timeout *t) { + //uloop_init(); + //time_t time_update_hostapd = *(time_t *) arg; - printf("Update hostapd thread with time: %lu\n", time_update_hostapd); - const char *ubus_socket = NULL; - ctx_hostapd = ubus_connect(ubus_socket); + //printf("Update hostapd thread with time: %lu\n", time_update_hostapd); + //const char *ubus_socket = NULL; + //ctx_hostapd = ubus_connect(ubus_socket); + //ubus_add_uloop(ctx_hostapd); + //uloop_run(); + uloop_timeout_set(&hostapd_timer, 1000); - subscribe_to_hostapd(hostapd_dir_glob); - while (1) { +// while (1) { subscribe_to_hostapd_interfaces(hostapd_dir_glob); - sleep(time_update_hostapd); - } +// sleep(time_update_hostapd); +// } } void del_client_all_interfaces(const uint8_t *client_addr, uint32_t reason, uint8_t deauth, uint32_t ban_time) { From 8e1262c7f80042f52b90d3388fe137ee2a6997d8 Mon Sep 17 00:00:00 2001 From: PolynomialDivision Date: Mon, 20 Nov 2017 11:37:41 +0100 Subject: [PATCH 2/2] change to ubus timeout --- src/include/datastorage.h | 2 ++ src/main.c | 1 + src/utils/ubus.c | 16 ++-------------- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/include/datastorage.h b/src/include/datastorage.h index 1f50f6d..0a66b3f 100644 --- a/src/include/datastorage.h +++ b/src/include/datastorage.h @@ -37,6 +37,8 @@ struct time_config_s { time_t update_hostapd; }; +struct time_config_s timeout_config; + // ---------------- Global variables ---------------- struct probe_metric_s dawn_metric; diff --git a/src/main.c b/src/main.c index b147250..e26efbc 100644 --- a/src/main.c +++ b/src/main.c @@ -147,6 +147,7 @@ int main(int argc, char **argv) { gcrypt_set_key_and_iv(shared_key, iv); struct time_config_s time_config = uci_get_time_config(); + timeout_config = time_config; // TODO: Refactor... if (pthread_mutex_init(&list_mutex, NULL) != 0) { printf("\n mutex init failed\n"); diff --git a/src/utils/ubus.c b/src/utils/ubus.c index 923424f..7c75645 100644 --- a/src/utils/ubus.c +++ b/src/utils/ubus.c @@ -603,20 +603,8 @@ void *update_clients_thread(void *arg) { } void update_hostapd_sockets(struct uloop_timeout *t) { - //uloop_init(); - //time_t time_update_hostapd = *(time_t *) arg; - - //printf("Update hostapd thread with time: %lu\n", time_update_hostapd); - //const char *ubus_socket = NULL; - //ctx_hostapd = ubus_connect(ubus_socket); - //ubus_add_uloop(ctx_hostapd); - //uloop_run(); - uloop_timeout_set(&hostapd_timer, 1000); - -// while (1) { - subscribe_to_hostapd_interfaces(hostapd_dir_glob); -// sleep(time_update_hostapd); -// } + uloop_timeout_set(&hostapd_timer, timeout_config.update_hostapd); + subscribe_to_hostapd_interfaces(hostapd_dir_glob); } void del_client_all_interfaces(const uint8_t *client_addr, uint32_t reason, uint8_t deauth, uint32_t ban_time) {