diff --git a/src/include/datastorage.h b/src/include/datastorage.h index 68b165b..e9d1aa3 100644 --- a/src/include/datastorage.h +++ b/src/include/datastorage.h @@ -75,6 +75,21 @@ typedef struct client_s { uint32_t aid; } client; +typedef struct ap_s { + uint8_t bssid_addr[ETH_ALEN]; + uint32_t freq; + uint8_t ht; + uint8_t vht; + time_t time; +} ap; + +// Array +#define ARRAY_AP_LEN 50 +#define TIME_THRESHOLD_AP 30 +struct ap_s ap_array[ARRAY_AP_LEN]; +pthread_mutex_t ap_array_mutex; + +ap insert_to_ap_array(ap entry); // Array #define ARRAY_CLIENT_LEN 1000 diff --git a/src/main.c b/src/main.c index 7c2e6d2..194a702 100644 --- a/src/main.c +++ b/src/main.c @@ -79,6 +79,11 @@ int main(int argc, char **argv) { return 1; } + if (pthread_mutex_init(&ap_array_mutex, NULL) != 0) { + printf("\n mutex init failed\n"); + return 1; + } + init_socket_runopts(opt_broadcast_ip, opt_broadcast_port, 1); pthread_t tid_probe; @@ -101,6 +106,8 @@ int main(int argc, char **argv) { pthread_mutex_destroy(&list_mutex); pthread_mutex_destroy(&probe_array_mutex); pthread_mutex_destroy(&client_array_mutex); + pthread_mutex_destroy(&ap_array_mutex); + //free_list(probe_list_head); diff --git a/src/storage/datastorage.c b/src/storage/datastorage.c index 93984a6..23d8a34 100644 --- a/src/storage/datastorage.c +++ b/src/storage/datastorage.c @@ -24,15 +24,28 @@ int eval_probe_metric(struct probe_entry_s probe_entry); int kick_client(struct client_s client_entry); +void ap_array_insert(ap entry); + +ap ap_array_delete(ap entry); + int probe_entry_last = -1; int client_entry_last = -1; +int ap_entry_last = -1; int eval_probe_metric(struct probe_entry_s probe_entry) { int score = 0; +/* + client ap_entry = client_array_get_ap(probe_entry.bssid_addr); + + // check if ap entry is available + if(mac_is_equal(ap_entry.bssid_addr, probe_entry.bssid_addr)) { + ap_entry. + } else { + score += probe_entry.ht_support ? dawn_metric.vht_support : dawn_metric.n_vht_support; + score += probe_entry.vht_support ? dawn_metric.ht_support : dawn_metric.n_ht_support; + }*/ - score += probe_entry.ht_support ? dawn_metric.vht_support : dawn_metric.n_vht_support; - score += probe_entry.vht_support ? dawn_metric.ht_support : dawn_metric.n_ht_support; score += (probe_entry.freq > 5000) ? dawn_metric.freq : 0; score += (probe_entry.signal > -60) ? dawn_metric.rssi : 0; @@ -331,6 +344,69 @@ probe_entry insert_to_array(probe_entry entry, int inc_counter) { return entry; } +ap insert_to_ap_array(ap entry) { + pthread_mutex_lock(&ap_array_mutex); + + entry.time = time(0); + ap_array_delete(entry); + ap_array_insert(entry); + pthread_mutex_unlock(&ap_array_mutex); + + return entry; +} + +void ap_array_insert(ap entry) { + if (ap_entry_last == -1) { + ap_array[0] = entry; + ap_entry_last++; + return; + } + + int i; + for (i = 0; i <= ap_entry_last; i++) { + if (!mac_is_greater(entry.bssid_addr, ap_array[i].bssid_addr)) { + break; + } + } + for (int j = ap_entry_last; j >= i; j--) { + if (j + 1 <= ARRAY_AP_LEN) { + ap_array[j + 1] = ap_array[j]; + } + } + ap_array[i] = entry; + + if (ap_entry_last < ARRAY_AP_LEN) { + ap_entry_last++; + } +} + +ap ap_array_delete(ap entry) { + int i; + int found_in_array = 0; + ap tmp; + + if (ap_entry_last == -1) { + return tmp; + } + + for (i = 0; i <= ap_entry_last; i++) { + if (mac_is_equal(entry.bssid_addr, ap_array[i].bssid_addr)) { + found_in_array = 1; + tmp = ap_array[i]; + break; + } + } + + for (int j = i; j <= ap_entry_last; j++) { + ap_array[j] = ap_array[j + 1]; + } + + if (ap_entry_last > -1 && found_in_array) { + ap_entry_last--; + } + return tmp; +} + void remove_old_client_entries(time_t current_time, long long int threshold) { for (int i = 0; i < probe_entry_last; i++) { if (client_array[i].time < current_time - threshold) { diff --git a/src/utils/ubus.c b/src/utils/ubus.c index e624d49..59f69b6 100644 --- a/src/utils/ubus.c +++ b/src/utils/ubus.c @@ -473,8 +473,13 @@ int parse_to_clients(struct blob_attr *msg, int do_kick, uint32_t id) { * here i know my bssid to kick the clients * seems a good idea ?! */ - uint8_t bssid[ETH_ALEN]; - hwaddr_aton(blobmsg_data(tb[CLIENT_TABLE_BSSID]), bssid); + + ap ap_entry; + hwaddr_aton(blobmsg_data(tb[CLIENT_TABLE_BSSID]), ap_entry.bssid_addr); + ap_entry.freq = blobmsg_get_u32(tb[CLIENT_TABLE_FREQ]); + ap_entry.ht = blobmsg_get_u8(tb[CLIENT_TABLE_HT]); + ap_entry.vht = blobmsg_get_u8(tb[CLIENT_TABLE_VHT]); + insert_to_ap_array(ap_entry); /* if((tb[CLIENT_TABLE_CHAN_UTIL])) { @@ -483,7 +488,7 @@ int parse_to_clients(struct blob_attr *msg, int do_kick, uint32_t id) { */ if (do_kick) { printf("[CLIENTS] : Kick Clients\n"); - kick_clients(bssid, id); + kick_clients(ap_entry.bssid_addr, id); printf("[CLIENTS] : KickED Clients\n"); } }