From 4d314298c8607453010a3112ae42eec3f0d81e00 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Sun, 22 Jul 2018 11:03:48 +0200 Subject: [PATCH] extract data from hostapd to extra struct --- wireless/wifictld/src/data.h | 1 - wireless/wifictld/src/ubus_events.c | 24 ++++--- wireless/wifictld/src/wifi_clients.c | 99 ++++++++++++++-------------- wireless/wifictld/src/wifi_clients.h | 18 +++-- 4 files changed, 78 insertions(+), 64 deletions(-) diff --git a/wireless/wifictld/src/data.h b/wireless/wifictld/src/data.h index be1b7b7..0842342 100644 --- a/wireless/wifictld/src/data.h +++ b/wireless/wifictld/src/data.h @@ -19,5 +19,4 @@ struct wifi_client { uint32_t signal_highfreq; }; - #endif diff --git a/wireless/wifictld/src/ubus_events.c b/wireless/wifictld/src/ubus_events.c index 180ae81..860dbad 100644 --- a/wireless/wifictld/src/ubus_events.c +++ b/wireless/wifictld/src/ubus_events.c @@ -85,7 +85,13 @@ static int receive_notify(struct ubus_context *ctx, struct ubus_object *obj, str { const char *attr_name, *str; u8 addr[ETH_ALEN]; - uint32_t freq, ssi_signal = -1; + struct hostapd_client hclient = { + .method = method, + .auth = false, + .freq = -1, + .ssi_signal = -1, + }; + struct blob_attr *pos; int rem = blobmsg_data_len(msg); @@ -98,18 +104,20 @@ static int receive_notify(struct ubus_context *ctx, struct ubus_object *obj, str if (!strcmp(attr_name, "address")){ str = blobmsg_get_string(pos); hwaddr_aton(str, addr); + hclient.address = addr; } else if(!strcmp(attr_name, "signal")){ - ssi_signal = blobmsg_get_u32(pos); + hclient.ssi_signal = blobmsg_get_u32(pos); } else if(!strcmp(attr_name, "freq")){ - freq = blobmsg_get_u32(pos); + hclient.freq = blobmsg_get_u32(pos); } } // handle - log_verbose("%s["MACSTR"] freq: %d signal %d", method, MAC2STR(addr), freq, ssi_signal); + log_verbose("%s["MACSTR"] freq: %d signal %d", method, MAC2STR(addr), hclient.freq, hclient.ssi_signal); if (!strcmp(method, "auth")) { - if (wifi_clients_try(true, addr, freq, ssi_signal)) { + hclient.auth = true; + if (wifi_clients_try(&hclient)) { log_debug(" -> reject\n"); return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY; } @@ -119,7 +127,7 @@ static int receive_notify(struct ubus_context *ctx, struct ubus_object *obj, str if (!strcmp(method, "probe")) { if(config_client_probe_steering) { - if (wifi_clients_try(false, addr, freq, ssi_signal)) { + if (wifi_clients_try(&hclient)) { log_debug(" -> reject\n"); return WLAN_STATUS_UNSPECIFIED_FAILURE; } @@ -128,12 +136,12 @@ static int receive_notify(struct ubus_context *ctx, struct ubus_object *obj, str } if(config_client_probe_learning) { log_verbose(" learn"); - wifi_clients_learn(addr, freq, ssi_signal); + wifi_clients_learn(&hclient); } } if (!strcmp(method, "deauth")) { - wifi_clients_disconnect(addr, freq, ssi_signal); + wifi_clients_disconnect(&hclient); log_verbose(" -> disconnect\n"); return WLAN_STATUS_SUCCESS; } diff --git a/wireless/wifictld/src/wifi_clients.c b/wireless/wifictld/src/wifi_clients.c index 7374cd8..2b02a0f 100644 --- a/wireless/wifictld/src/wifi_clients.c +++ b/wireless/wifictld/src/wifi_clients.c @@ -62,85 +62,84 @@ void wifi_clients_close() { } -struct wifi_client *__get_client(const u8 *address){ +void __client_setvalues(struct wifi_client *client, struct hostapd_client *hclient) { + log_debug("wifi_clients.__client_setvalues(., %d):", hclient->freq); + if (client->freq_highest < hclient->freq) { + client->freq_highest = hclient->freq; + log_debug(" new highest freq"); + } + if (hclient->freq > WIFI_CLIENT_FREQ_THREASHOLD) { + client->signal_highfreq = hclient->ssi_signal; + }else{ + client->signal_lowfreq = hclient->ssi_signal; + } + log_debug("\n"); + time(&client->time); + if(!client->authed) { + client->authed = hclient->auth; + } +} + +struct wifi_client *__get_client(struct hostapd_client *hclient){ struct wifi_client *client; - client = avl_find_element(&clients_by_addr, address, client, avl); + client = avl_find_element(&clients_by_addr, hclient->address, client, avl); if (client) { - log_debug("wifi_clients.__get_client("MACSTR"): found existing client\n", MAC2STR(address)); + __client_setvalues(client, hclient); + log_debug("wifi_clients.__get_client("MACSTR"): found existing client\n", MAC2STR(hclient->address)); return client; } - log_debug("wifi_clients.__get_client("MACSTR"): gen new client\n", MAC2STR(address)); + log_debug("wifi_clients.__get_client("MACSTR"): gen new client\n", MAC2STR(hclient->address)); client = malloc(sizeof(*client)); - memcpy(client->addr, address, sizeof(client->addr)); + memcpy(client->addr, hclient->address, sizeof(client->addr)); client->try_probe = 0; client->try_auth = 0; - time(&client->time); - client->authed = 0; client->connected = 0; client->freq_highest = 0; client->signal_lowfreq = 0; client->signal_highfreq = 0; - client->time = 0; + __client_setvalues(client, hclient); client->avl.key = client->addr; - log_debug("wifi_clients.__get_client("MACSTR"): add client to mem\n", MAC2STR(address)); + log_debug("wifi_clients.__get_client("MACSTR"): add client to mem\n", MAC2STR(hclient->address)); avl_insert(&clients_by_addr, &client->avl); return client; } -void __client_learn(struct wifi_client *client, uint32_t freq, uint32_t ssi_signal) { - log_debug("wifi_clients.__client_learn(., %d):", freq); - if (client->freq_highest < freq) { - client->freq_highest = freq; - log_debug(" new highest freq"); - } - if (freq > WIFI_CLIENT_FREQ_THREASHOLD) { - client->signal_highfreq = ssi_signal; - }else{ - client->signal_lowfreq = ssi_signal; - } - log_debug("\n"); - time(&client->time); +void wifi_clients_learn(struct hostapd_client *hclient) { + __get_client(hclient); } -void wifi_clients_learn(const u8 *address, uint32_t freq, uint32_t ssi_signal) { +int wifi_clients_try(struct hostapd_client *hclient) { struct wifi_client *client; - client = __get_client(address); - __client_learn(client, freq, ssi_signal); -} -int wifi_clients_try(bool auth, const u8 *address, uint32_t freq, uint32_t ssi_signal) { - struct wifi_client *client; - client = __get_client(address); - __client_learn(client, freq, ssi_signal); + client = __get_client(hclient); - if (auth) { - log_info("auth(try=%d mac="MACSTR" freq=%d ssi=%d): ", client->try_auth, MAC2STR(address), freq, ssi_signal); + if (hclient->auth) { + log_info("auth(try=%d mac="MACSTR" freq=%d ssi=%d): ", client->try_auth, MAC2STR(hclient->address), hclient->freq, hclient->ssi_signal); client->try_auth++; client->try_probe = 0; }else{ if(config_client_force_probe){ - log_verbose("probe(try=%d mac="MACSTR" freq=%d ssi=%d): ", client->try_auth, MAC2STR(address), freq, ssi_signal); + log_verbose("probe(try=%d mac="MACSTR" freq=%d ssi=%d): ", client->try_auth, MAC2STR(hclient->address), hclient->freq, hclient->ssi_signal); }else{ - log_verbose("probe(try=%d mac="MACSTR" freq=%d ssi=%d): ", client->try_probe, MAC2STR(address), freq, ssi_signal); + log_verbose("probe(try=%d mac="MACSTR" freq=%d ssi=%d): ", client->try_probe, MAC2STR(hclient->address), hclient->freq, hclient->ssi_signal); client->try_probe++; } } - if (freq > WIFI_CLIENT_FREQ_THREASHOLD) { - if(!auth){ + if (hclient->freq > WIFI_CLIENT_FREQ_THREASHOLD) { + if(!hclient->auth){ client->try_probe = 0; log_verbose("accept\n"); return 0; } log_info("accept\n"); client->try_auth = 0; - client->authed = 1; client->connected = 1; return 0; } if (client->freq_highest > WIFI_CLIENT_FREQ_THREASHOLD) { - if (config_client_force || config_client_force_probe && !auth) { - if(!auth){ + if (config_client_force || config_client_force_probe && !hclient->auth) { + if(!hclient->auth){ log_verbose("reject - force\n"); return -1; } @@ -148,8 +147,8 @@ int wifi_clients_try(bool auth, const u8 *address, uint32_t freq, uint32_t ssi_s return -1; } - if (ssi_signal > config_client_signal_threashold) { - if(!auth){ + if (hclient->ssi_signal > config_client_signal_threashold) { + if(!hclient->auth){ log_verbose("reject - learned higher freq + ssi is high enough\n"); return -1; } @@ -158,21 +157,20 @@ int wifi_clients_try(bool auth, const u8 *address, uint32_t freq, uint32_t ssi_s } } - if(auth && client->try_auth > config_client_try_threashold || - !auth && client->try_probe > config_client_try_threashold + if(hclient->auth && client->try_auth > config_client_try_threashold || + !hclient->auth && client->try_probe > config_client_try_threashold ) { - if(!auth){ + if(!hclient->auth){ client->try_probe = 0; log_verbose("accept - threashold\n"); return 0; } log_info("accept - threashold\n"); client->try_auth = 0; - client->authed = 1; client->connected = 1; return 0; } - if(!auth){ + if(!hclient->auth){ log_verbose("reject\n"); return client->try_probe; } @@ -180,17 +178,16 @@ int wifi_clients_try(bool auth, const u8 *address, uint32_t freq, uint32_t ssi_s return client->try_auth; } -void wifi_clients_disconnect(const u8 *address, uint32_t freq, uint32_t ssi_signal) { +void wifi_clients_disconnect(struct hostapd_client *hclient) { struct wifi_client *client; - client = __get_client(address); - __client_learn(client, freq, ssi_signal); + client = __get_client(hclient); client->connected = 0; } -void wifi_clients_del(const u8 *addr) { +void wifi_clients_del(const u8 *address) { struct wifi_client *client; - client = avl_find_element(&clients_by_addr, addr, client, avl); + client = avl_find_element(&clients_by_addr, address, client, avl); if (!client) return; diff --git a/wireless/wifictld/src/wifi_clients.h b/wireless/wifictld/src/wifi_clients.h index fc1361a..426dc2d 100644 --- a/wireless/wifictld/src/wifi_clients.h +++ b/wireless/wifictld/src/wifi_clients.h @@ -3,13 +3,23 @@ #include "include.h" + + +struct hostapd_client { + u8 *address; + const char *method; + bool auth; + uint32_t freq; + uint32_t ssi_signal; +}; + int wifi_clients_init(); void wifi_clients_close(); -void wifi_clients_learn(const uint8_t *address, uint32_t freq, uint32_t ssi_signal); -int wifi_clients_try(bool auth, const uint8_t *address, uint32_t freq, uint32_t ssi_signal); -void wifi_clients_disconnect(const uint8_t *address, uint32_t freq, uint32_t ssi_signal); +void wifi_clients_learn(struct hostapd_client *hclient); +int wifi_clients_try(struct hostapd_client *hclient); +void wifi_clients_disconnect(struct hostapd_client *hclient); -void wifi_clients_del(const u8 *addr); +void wifi_clients_del(const u8 *address); #endif