diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d48466e..e9acdc3 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -39,7 +39,7 @@ SET(SOURCES crypto/base64.c include/base64.h - utils/utils.c) + utils/utils.c include/rssi.h utils/rssi.c) SET(LIBS ubox ubus json-c blobmsg_json config uci gcrypt ssl crypto) diff --git a/src/include/rssi.h b/src/include/rssi.h new file mode 100644 index 0000000..d0c56ed --- /dev/null +++ b/src/include/rssi.h @@ -0,0 +1,14 @@ +// +// Created by nick on 19.10.17. +// + +#ifndef DAWN_RSSI_H +#define DAWN_RSSI_H + +#include +#include +#include + +int get_rssi_from_iwinfo(__uint8_t* client_addr); + +#endif //DAWN_RSSI_H diff --git a/src/include/ubus.h b/src/include/ubus.h index e2356bd..0395af6 100644 --- a/src/include/ubus.h +++ b/src/include/ubus.h @@ -28,4 +28,6 @@ char *hostapd_dir_glob; int ubus_call_umdns(); +int ubus_send_probe_via_network(struct probe_entry_s probe_entry); + #endif diff --git a/src/include/utils.h b/src/include/utils.h index 79f9863..941efe1 100644 --- a/src/include/utils.h +++ b/src/include/utils.h @@ -9,5 +9,6 @@ int hex_to_bin(char ch); int hwaddr_aton(const char *txt, uint8_t *addr); +int convert_mac(char* in, char* out); #endif \ No newline at end of file diff --git a/src/storage/datastorage.c b/src/storage/datastorage.c index 52f897b..ce091d9 100644 --- a/src/storage/datastorage.c +++ b/src/storage/datastorage.c @@ -1,6 +1,9 @@ #include "datastorage.h" +#include + #include "ubus.h" +#include "rssi.h" #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] @@ -32,6 +35,10 @@ void remove_old_ap_entries(time_t current_time, long long int threshold); void print_ap_entry(ap entry); +int probe_array_update_rssi(uint8_t bssid_addr[], uint8_t client_addr[], uint32_t rssi); + +int is_connected(uint8_t bssid_addr[], uint8_t client_addr[]); + int probe_entry_last = -1; int client_entry_last = -1; int ap_entry_last = -1; @@ -129,13 +136,29 @@ void kick_clients(uint8_t bssid[], uint32_t id) { if (!mac_is_equal(client_array[j].bssid_addr, bssid)) { break; } + + // update rssi + int rssi = get_rssi_from_iwinfo(client_array[j].client_addr); + if(rssi != INT_MIN) + { + printf("UPDATING RSSI!!!\n"); + pthread_mutex_unlock(&probe_array_mutex); + if(probe_array_update_rssi(client_array[j].bssid_addr, client_array[j].client_addr, rssi)) + { + printf("RSSI UPDATED!!!\n"); + } + pthread_mutex_lock(&probe_array_mutex); + + } + if (kick_client(client_array[j]) > 0) { // TODO: Better debug output printf("KICKING CLIENT!!!!!!!!!!!!!\n"); del_client_interface(id, client_array[j].client_addr, 5, 1, 60000); } else if (kick_client(client_array[j]) == -1) { printf("Force client to reconnect!!!!!!!!!!!!!\n"); - del_client_interface(id, client_array[j].client_addr, 0, 0, 0); + printf("TRY TO READ RSSI!\n"); + //del_client_interface(id, client_array[j].client_addr, 0, 0, 0); } else { printf("STAAAY CLIENT!!!!!!!!!!!!!\n"); } @@ -145,6 +168,27 @@ void kick_clients(uint8_t bssid[], uint32_t id) { pthread_mutex_unlock(&client_array_mutex); } +int is_connected(uint8_t bssid_addr[], uint8_t client_addr[]) +{ + int i; + int found_in_array = 0; + + if (client_entry_last == -1) { + return 0; + } + + for (i = 0; i <= client_entry_last; i++) { + + if ( mac_is_equal(bssid_addr, client_array[i].bssid_addr) && + mac_is_equal(client_addr, client_array[i].client_addr)) + { + found_in_array = 1; + break; + } + } + return found_in_array; +} + int client_array_go_next_help(char sort_order[], int i, client entry, client next_entry) { switch (sort_order[i]) { @@ -298,6 +342,33 @@ probe_entry probe_array_delete(probe_entry entry) { return tmp; } +int probe_array_update_rssi(uint8_t bssid_addr[], uint8_t client_addr[], uint32_t rssi) { + + printf("Try to find probe reqeuest...\n"); + + int updated = 0; + + if (probe_entry_last == -1) { + return 0; + } + + + pthread_mutex_lock(&probe_array_mutex); + for (int i = 0; i <= probe_entry_last; i++) { + if (mac_is_equal(bssid_addr, probe_array[i].bssid_addr) && + mac_is_equal(client_addr, probe_array[i].client_addr)) { + probe_array[i].signal = rssi; + updated = 1; + // TODO: Send updatet probe to others :'( + printf("NOW SENDING TO THE OTHERS!!!\n"); + ubus_send_probe_via_network(probe_array[i]); + } + } + pthread_mutex_unlock(&probe_array_mutex); + + return updated; +} + probe_entry probe_array_get_entry(uint8_t bssid_addr[], uint8_t client_addr[]) { int i; @@ -456,7 +527,8 @@ void remove_old_client_entries(time_t current_time, long long int threshold) { void remove_old_probe_entries(time_t current_time, long long int threshold) { for (int i = 0; i < probe_entry_last; i++) { if (probe_array[i].time < current_time - threshold) { - probe_array_delete(probe_array[i]); + if(!is_connected(probe_array[i].bssid_addr, probe_array[i].client_addr)) + probe_array_delete(probe_array[i]); } } } diff --git a/src/utils/rssi.c b/src/utils/rssi.c new file mode 100644 index 0000000..8523849 --- /dev/null +++ b/src/utils/rssi.c @@ -0,0 +1,78 @@ +#include "rssi.h" + +#include + +#include "utils.h" + +#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] + +int call_iwinfo(char* client_addr); +int parse_rssi(char* iwinfo_string); + +int get_rssi_from_iwinfo(__uint8_t* client_addr) +{ + char mac_buf[20]; + sprintf(mac_buf, "%x:%x:%x:%x:%x:%x", MAC2STR(client_addr)); + char mac_buf_conv[20]; + + convert_mac(mac_buf, mac_buf_conv); + + return call_iwinfo(mac_buf_conv); +} + +int call_iwinfo(char* client_addr) +{ + FILE *fp; + char path[1035]; + + /* Open the command for reading. */ + + // TODO: refactor this + int rssi = INT_MIN; + int command_length = 68; + char iwinfo_command[command_length]; + char* first_command = "(iwinfo wlan0 assoc && iwinfo wlan1 assoc) | grep "; + size_t length_first_command = strlen(first_command); + memcpy(iwinfo_command, first_command, length_first_command); + memcpy(iwinfo_command + length_first_command, client_addr, strlen(client_addr)); + iwinfo_command[command_length - 1] = '\0'; + printf("iwinfo command:\n%s\n", iwinfo_command); + + + fp = popen(iwinfo_command, "r"); + if (fp == NULL) { + printf("Failed to run command\n" ); + exit(1); + } + + /* Read the output a line at a time - output it. */ + while (fgets(path, sizeof(path)-1, fp) != NULL) { + rssi = parse_rssi(path); + } + + /* close */ + pclose(fp); + + return rssi; +} + +int parse_rssi(char* iwinfo_string) +{ + char cut_1[] = " "; + char cut_2[] = "dBm"; + char *p_1 = strstr(iwinfo_string, cut_1); + char *p_2 = strstr(iwinfo_string, cut_2); + int rssi = INT_MIN; + if(p_1 != NULL && p_2 != NULL) + { + printf("Found substring: %s", p_2); + printf("Length: %d\n", p_2 - p_1); + int length = (int) (p_2 - p_1); + char dest[length + 1]; + memcpy(dest, p_1, (int) (p_2 - p_1)); + dest[length] = '\0'; + rssi = atoi(dest); + printf("After cutting:\n%s\nInt:%d\n", dest, rssi); + } + return rssi; +} \ No newline at end of file diff --git a/src/utils/ubus.c b/src/utils/ubus.c index c4e792d..d8544ae 100644 --- a/src/utils/ubus.c +++ b/src/utils/ubus.c @@ -611,7 +611,6 @@ static void ubus_umdns_cb(struct ubus_request *req, int type, struct blob_attr * printf("UMDNS:\n%s", str); } - int ubus_call_umdns() { u_int32_t id; @@ -622,5 +621,30 @@ int ubus_call_umdns() int timeout = 1; ubus_invoke(ctx_clients, id, "browse", NULL, ubus_umdns_cb, NULL, timeout * 1000); + return 0; +} + +int ubus_send_probe_via_network(struct probe_entry_s probe_entry) +{ + printf("Try to send new probe!!!\n"); + + static struct blob_buf b; + + blob_buf_init(&b, 0); + blobmsg_add_macaddr(&b, "bssid", probe_entry.bssid_addr); + blobmsg_add_macaddr(&b, "address", probe_entry.client_addr); + blobmsg_add_macaddr(&b, "target", probe_entry.target_addr); + blobmsg_add_u32(&b, "signal", probe_entry.signal); + blobmsg_add_u32(&b, "freq", probe_entry.freq); + blobmsg_add_u8(&b, "ht_support", probe_entry.ht_support); + blobmsg_add_u8(&b, "vht_support", probe_entry.vht_support); + + // send probe via network + char *str; + str = blobmsg_format_json(b.head, 1); + send_string_enc(str); + + printf("SENDING NEW PROBE!!!: %s\n", str); + return 0; } \ No newline at end of file diff --git a/src/utils/utils.c b/src/utils/utils.c index f200114..13c5da6 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -23,4 +23,24 @@ int hwaddr_aton(const char *txt, uint8_t *addr) { } return 0; -} \ No newline at end of file +} + +int convert_mac(char* in, char* out) { + int i,j = 0; + + for (i = 0; i < 6; i++) { + if(in[j+1] != ':' && in[j+1] != '\0') { + out[3 * i] = toupper(in[j]); + out[(3 * i) + 1] = toupper(in[j + 1]); + out[(3 * i) + 2] = in[j + 2]; + j+= 3; + } else { + out[3 * i] = '0'; + out[(3 * i) + 1] = toupper(in[j]); + out[(3 * i) + 2] = toupper(in[j+1]); + j += 2; + } + } + return 0; +} +