switch to ubloop

This commit is contained in:
PolynomialDivision 2017-11-20 12:19:44 +01:00
parent 90bb949342
commit 9c80d0542e
6 changed files with 92 additions and 21 deletions

View file

@ -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

View file

@ -8,6 +8,7 @@
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <libubox/uloop.h>
#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 ----------------

View file

@ -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);

View file

@ -1,6 +1,7 @@
#include "datastorage.h"
#include <limits.h>
#include <libubox/uloop.h>
#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);

View file

@ -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);

View file

@ -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) {