improve logging + fix segfault by init wifi_clients avl

This commit is contained in:
Martin/Geno 2018-07-16 10:46:13 +02:00
parent 528fccea8b
commit 62d73930ec
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
4 changed files with 52 additions and 20 deletions

View File

@ -16,7 +16,7 @@ void log_debug(const char *format, ...) {
void log_verbose(const char *format, ...) { void log_verbose(const char *format, ...) {
if (!verbose) if (verbose)
return; return;
va_list args; va_list args;
va_start(args, format); va_start(args, format);

View File

@ -101,6 +101,8 @@ static int receive_notify(struct ubus_context *ctx, struct ubus_object *obj, str
int rem = blobmsg_data_len(msg); int rem = blobmsg_data_len(msg);
// read msg // read msg
log_debug("ubus_events.receive_notify(): read msg\n");
blobmsg_for_each_attr(pos, msg, rem) { blobmsg_for_each_attr(pos, msg, rem) {
attr_name = blobmsg_name(pos); attr_name = blobmsg_name(pos);
@ -114,22 +116,29 @@ static int receive_notify(struct ubus_context *ctx, struct ubus_object *obj, str
} }
} }
// handle // handle
log_debug("ubus_events.receive_notify(): handle\n");
if (!strcmp(method, "probe")) { if (!strcmp(method, "probe")) {
log_verbose("probe\n"); log_verbose("probe["MACSTR"]", MAC2STR(addr));
if (client_probe_learning) if (client_probe_learning) {
log_verbose(" + learn freq[%d]\n", freq);
wifi_clients_learn(addr, freq); wifi_clients_learn(addr, freq);
}else{
log_verbose("\n");
}
return WLAN_STATUS_SUCCESS; return WLAN_STATUS_SUCCESS;
} }
if (!strcmp(method, "auth")) { if (!strcmp(method, "auth")) {
if (!wifi_clients_try(addr, freq)) { log_info("auth["MACSTR"]", MAC2STR(addr));
log_info("auth [drop]-> %s\n", addr); if (wifi_clients_try(addr, freq)) {
log_info(" -> drop\n");
return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY; return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
} }
log_info("auth [accept]-> %s\n", addr); log_info(" -> accept\n");
} else { } else {
log_verbose("%s\n", method); log_verbose("%s["MACSTR"]\n", method, MAC2STR(addr));
} }
return WLAN_STATUS_SUCCESS; return WLAN_STATUS_SUCCESS;

View File

@ -3,12 +3,18 @@
#include <stdlib.h> #include <stdlib.h>
#include <libubox/avl.h> #include <libubox/avl.h>
#include "hostapd/ieee802_11_defs.h" // ETH_ALEN #include "hostapd/ieee802_11_defs.h" // ETH_ALEN
#include "log.h"
#include "wifi_clients.h" #include "wifi_clients.h"
static void wifi_clients_del(const u8 *addr); static void wifi_clients_del(const u8 *addr);
static struct avl_tree clients_by_addr; static struct avl_tree clients_by_addr;
static int avl_compare_macaddr(const void *k1, const void *k2, void *ptr)
{
return memcmp(k1, k2, ETH_ALEN);
}
struct wifi_client { struct wifi_client {
struct avl_node avl; struct avl_node avl;
u8 addr[ETH_ALEN]; u8 addr[ETH_ALEN];
@ -18,6 +24,7 @@ struct wifi_client {
}; };
int wifi_clients_init() { int wifi_clients_init() {
avl_init(&clients_by_addr, avl_compare_macaddr, false, NULL);
return 0; return 0;
} }
@ -25,26 +32,33 @@ void wifi_clients_close() {
} }
struct wifi_client *__get_client(const u8 *addr){ struct wifi_client *__get_client(const u8 *address){
struct wifi_client *client; 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) if (client) {
return client; log_debug("wifi_clients.__get_client("MACSTR"): found existing client\n", MAC2STR(address));
client = malloc(sizeof(*client));
memcpy(client->addr, addr, sizeof(client->addr));
client->highfreq = 0;
client->try = 0;
client->time = 0;
client->avl.key = client->addr;
avl_insert(&clients_by_addr, &client->avl);
return client; return client;
}
log_debug("wifi_clients.__get_client("MACSTR"): gen new client\n", MAC2STR(address));
client = calloc(sizeof(*client), 1);
memcpy(client->addr, address, sizeof(client->addr));
client->highfreq = 0;
client->try = 0;
client->time = 0;
client->avl.key = client->addr;
log_debug("wifi_clients.__get_client("MACSTR"): add client to mem\n", MAC2STR(address));
avl_insert(&clients_by_addr, &client->avl);
return client;
} }
void __client_learn(struct wifi_client *client, uint32_t freq) { void __client_learn(struct wifi_client *client, uint32_t freq) {
log_debug("wifi_clients.__client_learn(., %d): ", freq);
if (client->highfreq < freq) { if (client->highfreq < freq) {
client->highfreq = freq; client->highfreq = freq;
log_debug("new highfreq");
} }
log_debug("\n");
//TODO time set and reset clean //TODO time set and reset clean
} }
@ -59,13 +73,17 @@ int wifi_clients_try(const u8 *address, uint32_t freq) {
client = __get_client(address); client = __get_client(address);
__client_learn(client, freq); __client_learn(client, freq);
log_debug("wifi_clients.wifi_clients_try("MACSTR", %d): ", MAC2STR(address), freq);
if (freq > 5000) { if (freq > 5000) {
log_debug("used correct freq\n");
client->try = 0; client->try = 0;
} else { } else {
if(client->try > client_freq_try_threashold) { if(client->try > client_freq_try_threashold) {
log_debug("clients %d try over threashold %d\n",client->try, client_freq_try_threashold);
return 0; return 0;
} }
client->try++; client->try++;
log_debug("clients->try now by %d\n",client->try);
} }
return client->try; return client->try;

View File

@ -7,13 +7,18 @@ int main(void)
{ {
verbose = 1; verbose = 1;
client_probe_learning = 1; client_probe_learning = 1;
int ret = 0; int ret = 0;
#ifdef MINI #ifdef MINI
log_error("start wifictld (mini)\n"); log_info("start wifictld (mini)\n");
#else #else
log_error("start wifictld (full)\n"); log_info("start wifictld (full)\n");
#endif #endif
log_debug("log: show debug\n");
log_verbose("log: show verbose\n");
uloop_init(); uloop_init();
// define wifi clients memory // define wifi clients memory