diff --git a/src/include/datastorage.h b/src/include/datastorage.h index c6335ea..5fc77c1 100644 --- a/src/include/datastorage.h +++ b/src/include/datastorage.h @@ -278,7 +278,7 @@ int probe_array_update_rcpi_rsni(struct dawn_mac bssid_addr, struct dawn_mac cli void remove_old_client_entries(time_t current_time, long long int threshold); -void insert_client_to_array(client *entry); +client *insert_client_to_array(client *entry); int kick_clients(ap* kicking_ap, uint32_t id); diff --git a/src/include/memory_utils.h b/src/include/memory_utils.h index 16de90a..7f97417 100644 --- a/src/include/memory_utils.h +++ b/src/include/memory_utils.h @@ -13,7 +13,7 @@ enum dawn_memop DAWN_FREE }; -#define DAWN_MEMORY_AUDITING +//#define DAWN_MEMORY_AUDITING #ifdef DAWN_MEMORY_AUDITING #define dawn_malloc(size) dawn_memory_alloc(DAWN_MALLOC, __FILE__, __LINE__, 1, size, NULL) diff --git a/src/network/tcpsocket.c b/src/network/tcpsocket.c index e044798..cbfe59a 100644 --- a/src/network/tcpsocket.c +++ b/src/network/tcpsocket.c @@ -214,7 +214,6 @@ int add_tcp_conncection(char *ipv4, int port) { serv_addr.sin_port = htons(port); struct network_con_s *tmp = tcp_list_contains_address(serv_addr); - dawn_regmem(tmp); if (tmp != NULL) { if(tmp->connected == true) { @@ -223,7 +222,7 @@ int add_tcp_conncection(char *ipv4, int port) { // Delete already existing entry close(tmp->fd.fd); list_del(&tmp->list); - dawn_free(tmp); + // TODO: Removed free(tmp) here - was it needed? } } diff --git a/src/storage/datastorage.c b/src/storage/datastorage.c index c9c00cf..faa4ed3 100644 --- a/src/storage/datastorage.c +++ b/src/storage/datastorage.c @@ -1176,18 +1176,26 @@ void remove_old_ap_entries(time_t current_time, long long int threshold) { } } -void insert_client_to_array(client *entry) { +client *insert_client_to_array(client *entry) { +client * ret = NULL; + client **client_tmp = client_find_first_bc_entry(entry->bssid_addr, entry->client_addr, true); if (*client_tmp == NULL || !mac_is_equal_bb(entry->bssid_addr, (*client_tmp)->bssid_addr) || !mac_is_equal_bb(entry->client_addr, (*client_tmp)->client_addr)) { entry->kick_count = 0; client_array_insert(entry, client_tmp); + ret = entry; } + + return ret; } void insert_macs_from_file() { FILE *fp; char *line = NULL; +#ifdef DAWN_MEMORY_AUDITING + char *old_line = NULL; +#endif size_t len = 0; ssize_t read; @@ -1195,8 +1203,19 @@ void insert_macs_from_file() { fp = fopen("/tmp/dawn_mac_list", "r"); if (fp == NULL) exit(EXIT_FAILURE); + dawn_regmem(fp); while ((read = getline(&line, &len, fp)) != -1) { +#ifdef DAWN_MEMORY_AUDITING + if (old_line != line) + { + if (old_line != NULL) + dawn_unregmem(old_line); + old_line = line; + dawn_regmem(old_line); + } +#endif + printf("Retrieved line of length %zu :\n", read); printf("%s", line); @@ -1228,6 +1247,7 @@ void insert_macs_from_file() { } fclose(fp); + dawn_unregmem(fp); if (line) dawn_free(line); //exit(EXIT_SUCCESS); diff --git a/src/utils/dawn_uci.c b/src/utils/dawn_uci.c index 8d9508f..e0c43a1 100644 --- a/src/utils/dawn_uci.c +++ b/src/utils/dawn_uci.c @@ -8,8 +8,8 @@ #include "dawn_uci.h" -static struct uci_context *uci_ctx; -static struct uci_package *uci_pkg; +static struct uci_context *uci_ctx = NULL; +static struct uci_package *uci_pkg = NULL; // why is this not included in uci lib...?! // found here: https://github.com/br101/pingcheck/blob/master/uci.c @@ -191,7 +191,9 @@ bool uci_get_dawn_sort_order() { int uci_reset() { uci_unload(uci_ctx, uci_pkg); + dawn_unregmem(uci_pkg); uci_load(uci_ctx, "dawn", &uci_pkg); + dawn_regmem(uci_pkg); return 0; } @@ -210,15 +212,17 @@ int uci_init() { // shouldn't happen? uci_pkg = uci_lookup_package(ctx, "dawn"); if (uci_pkg) + { uci_unload(ctx, uci_pkg); + dawn_unregmem(uci_pkg); + uci_pkg = NULL; + } } if (uci_load(ctx, "dawn", &uci_pkg)) - { - // TODO: Is this allocating memory? - dawn_regmem(uci_pkg); return -1; - } + else + dawn_regmem(uci_pkg); return 1; } @@ -227,6 +231,7 @@ int uci_clear() { if (uci_pkg != NULL) { uci_unload(uci_ctx, uci_pkg); dawn_unregmem(uci_pkg); + uci_pkg = NULL; } if (uci_ctx != NULL) { uci_free_context(uci_ctx); diff --git a/src/utils/memory_utils.c b/src/utils/memory_utils.c index e06b6ca..9a66c1d 100644 --- a/src/utils/memory_utils.c +++ b/src/utils/memory_utils.c @@ -1,6 +1,8 @@ #include +#include #include #include +#include #include "memory_utils.h" @@ -12,9 +14,11 @@ struct mem_list { char type; size_t size; void* ptr; + uint64_t ref; }; -struct mem_list* mem_base = NULL; +static struct mem_list* mem_base = NULL; +static uint64_t alloc_ref = 0; void* dawn_memory_alloc(enum dawn_memop type, char* file, int line, size_t nmemb, size_t size, void *ptr) { @@ -26,8 +30,9 @@ void* dawn_memory_alloc(enum dawn_memop type, char* file, int line, size_t nmemb ret = malloc(size); break; case DAWN_REALLOC: - dawn_memory_unregister(DAWN_REALLOC, file, line, ptr); ret = realloc(ptr, size); + if (ret != NULL) + dawn_memory_unregister(DAWN_REALLOC, file, line, ptr); break; case DAWN_CALLOC: ret = calloc(nmemb, size); @@ -68,7 +73,7 @@ void* dawn_memory_register(enum dawn_memop type, char* file, int line, size_t si type_c = 'X'; break; default: - printf("Unexpected memory op tag!\n"); + printf("mem-audit: Unexpected memory op tag!\n"); break; } @@ -79,7 +84,7 @@ void* dawn_memory_register(enum dawn_memop type, char* file, int line, size_t si if (*ipos != NULL && (*ipos)->ptr == ptr) { - printf("attempting to register memory already registered (%c@%s:%d)...\n", type_c, file, line); + printf("mem-audit: attempting to register memory already registered (%c@%s:%d)...\n", type_c, file, line); } else { @@ -87,7 +92,7 @@ void* dawn_memory_register(enum dawn_memop type, char* file, int line, size_t si if (this_log == NULL) { - printf("Oh the irony! malloc() failed in dawn_memory_register()!\n"); + printf("mem-audit: Oh the irony! malloc() failed in dawn_memory_register()!\n"); } else { @@ -106,6 +111,7 @@ void* dawn_memory_register(enum dawn_memop type, char* file, int line, size_t si this_log->line = line; this_log->ptr = ptr; this_log->size = size; + this_log->ref = alloc_ref++; } } @@ -117,7 +123,7 @@ void dawn_memory_unregister(enum dawn_memop type, char* file, int line, void* pt struct mem_list** mem = &mem_base; char type_c = '?'; - while (*mem != NULL && (*mem)->ptr <= ptr) + while (*mem != NULL && (*mem)->ptr < ptr) { mem = &((*mem)->next_mem); } @@ -134,7 +140,7 @@ void dawn_memory_unregister(enum dawn_memop type, char* file, int line, void* pt type_c = 'R'; break; default: - printf("Unexpected memory op tag!\n"); + printf("mem-audit: Unexpected memory op tag!\n"); break; } @@ -146,7 +152,7 @@ void dawn_memory_unregister(enum dawn_memop type, char* file, int line, void* pt } else { - printf("Releasing (%c) memory we hadn't registered (%s:%d)...\n", type_c, file, line); + printf("mem-audit: Releasing (%c) memory we hadn't registered (%s:%d)...\n", type_c, file, line); } return; @@ -163,10 +169,10 @@ void dawn_memory_free(enum dawn_memop type, char* file, int line, void* ptr) void dawn_memory_audit() { - printf("Currently recorded memory allocations...\n"); + printf("mem-audit: Currently recorded allocations...\n"); for (struct mem_list* mem = mem_base; mem != NULL; mem = mem->next_mem) { - printf("%c - %s@%d: %ld\n", mem->type, mem->file, mem->line, mem->size); + printf("mem-audit: %8" PRIu64 "=%c - %s@%d: %zu\n", mem->ref, mem->type, mem->file, mem->line, mem->size); } - printf("[End of memory allocation list]\n"); + printf("mem-audit: [End of list]\n"); } diff --git a/src/utils/msghandler.c b/src/utils/msghandler.c index d83b28e..f0bafc0 100644 --- a/src/utils/msghandler.c +++ b/src/utils/msghandler.c @@ -428,7 +428,9 @@ dump_client(struct blob_attr** tb, struct dawn_mac client_addr, const char* bssi client_entry->time = time(0); pthread_mutex_lock(&client_array_mutex); - insert_client_to_array(client_entry); + // If entry was akraedy in list it won't be added, so free memorY + if (client_entry != insert_client_to_array(client_entry)) + dawn_free(client_entry); pthread_mutex_unlock(&client_array_mutex); } diff --git a/src/utils/ubus.c b/src/utils/ubus.c index 10b1868..d1e92c4 100644 --- a/src/utils/ubus.c +++ b/src/utils/ubus.c @@ -1370,6 +1370,7 @@ int uci_send_via_network() return 0; } + int build_hearing_map_sort_client(struct blob_buf *b) { print_probe_array(); pthread_mutex_lock(&probe_array_mutex); @@ -1377,95 +1378,78 @@ int build_hearing_map_sort_client(struct blob_buf *b) { void *client_list, *ap_list, *ssid_list; char ap_mac_buf[20]; char client_mac_buf[20]; - bool new_ssid = false; + bool same_ssid = false; blob_buf_init(b, 0); for (ap* m = ap_set; m != NULL; m = m->next_ap) { - // MUSTDO: Not sure this has translated to pointers correclty. What are we trying to do with SSID check??? - // Looks like it is trying to make sure we only handle the first SSID found in the list, ingoring any others? - if (new_ssid) { - new_ssid = false; - continue; - } - ssid_list = blobmsg_open_table(b, (char*)m->ssid); - probe_entry* i = probe_set; - while (i != NULL) { - /*if(!mac_is_equal(ap_array[m].bssid_addr, probe_array[i].bssid_addr)) - { - continue; - }*/ + // MUSTDO: Ensure SSID / BSSID ordering. Lost when switched to linked list! + // Scan AP list to find first of each SSID + if (!same_ssid) { + ssid_list = blobmsg_open_table(b, (char*)m->ssid); + probe_entry* i = probe_set; + while (i != NULL) { + ap *ap_entry_i = ap_array_get_ap(i->bssid_addr); - // TODO: Can we do this only when the BSSID changes in the probe list - ap *ap_entry_i = ap_array_get_ap(i->bssid_addr); - - if (ap_entry_i == NULL) { - i = i->next_probe; - continue; - } - - if (strcmp((char*)ap_entry_i->ssid, (char*)m->ssid) != 0) { - i = i->next_probe; - continue; - } - - sprintf(client_mac_buf, MACSTR, MAC2STR(i->client_addr.u8)); - client_list = blobmsg_open_table(b, client_mac_buf); - probe_entry *k; - for (k = i; k != NULL; k = k->next_probe) { - ap *ap_k = ap_array_get_ap(k->bssid_addr); - - if (ap_k == NULL) { + if (ap_entry_i == NULL) { + i = i->next_probe; continue; } - if (strcmp((char*)ap_k->ssid, (char*)m->ssid) != 0) { + if (strcmp((char*)ap_entry_i->ssid, (char*)m->ssid) != 0) { + i = i->next_probe; continue; } + sprintf(client_mac_buf, MACSTR, MAC2STR(i->client_addr.u8)); + client_list = blobmsg_open_table(b, client_mac_buf); + probe_entry *k; + for (k = i; + k != NULL && mac_is_equal_bb(k->client_addr, i->client_addr); + k = k->next_probe) { + + ap *ap_k = ap_array_get_ap(k->bssid_addr); + + if (ap_k == NULL || strcmp((char*)ap_k->ssid, (char*)m->ssid) != 0) { + continue; + } + + sprintf(ap_mac_buf, MACSTR, MAC2STR(k->bssid_addr.u8)); + ap_list = blobmsg_open_table(b, ap_mac_buf); + blobmsg_add_u32(b, "signal", k->signal); + blobmsg_add_u32(b, "rcpi", k->rcpi); + blobmsg_add_u32(b, "rsni", k->rsni); + blobmsg_add_u32(b, "freq", k->freq); + blobmsg_add_u8(b, "ht_capabilities", k->ht_capabilities); + blobmsg_add_u8(b, "vht_capabilities", k->vht_capabilities); + + + // check if ap entry is available + blobmsg_add_u32(b, "channel_utilization", ap_k->channel_utilization); + blobmsg_add_u32(b, "num_sta", ap_k->station_count); + blobmsg_add_u8(b, "ht_support", ap_k->ht_support); + blobmsg_add_u8(b, "vht_support", ap_k->vht_support); + + blobmsg_add_u32(b, "score", eval_probe_metric(k, ap_k)); + blobmsg_close_table(b, ap_list); + } + + blobmsg_close_table(b, client_list); + // TODO: Change this so that i and k are single loop? - if (!mac_is_equal_bb(k->client_addr, i->client_addr)) { - i = k; - break; - } - else if (k->next_probe == NULL) { - i = NULL; - } - else - i = i->next_probe; - - sprintf(ap_mac_buf, MACSTR, MAC2STR(k->bssid_addr.u8)); - ap_list = blobmsg_open_table(b, ap_mac_buf); - blobmsg_add_u32(b, "signal", k->signal); - blobmsg_add_u32(b, "rcpi", k->rcpi); - blobmsg_add_u32(b, "rsni", k->rsni); - blobmsg_add_u32(b, "freq", k->freq); - blobmsg_add_u8(b, "ht_capabilities", k->ht_capabilities); - blobmsg_add_u8(b, "vht_capabilities", k->vht_capabilities); - - - // check if ap entry is available - blobmsg_add_u32(b, "channel_utilization", ap_k->channel_utilization); - blobmsg_add_u32(b, "num_sta", ap_k->station_count); - blobmsg_add_u8(b, "ht_support", ap_k->ht_support); - blobmsg_add_u8(b, "vht_support", ap_k->vht_support); - - blobmsg_add_u32(b, "score", eval_probe_metric(k, ap_k)); - blobmsg_close_table(b, ap_list); + i = k; } - blobmsg_close_table(b, client_list); + } - if (k == NULL) { - i = NULL; - } - } - blobmsg_close_table(b, ssid_list); - - if ((m->next_ap != NULL) && strcmp((char*)m->ssid, (char*)((m->next_ap)->ssid)) == 0) - { - new_ssid = true; - } + if ((m->next_ap == NULL) || strcmp((char*)m->ssid, (char*)((m->next_ap)->ssid)) != 0) + { + blobmsg_close_table(b, ssid_list); + same_ssid = false; + } + else + same_ssid = true; } + pthread_mutex_unlock(&probe_array_mutex); return 0; }