Prase rssi from iwinfo

This commit is contained in:
PolynomialDivision 2017-10-19 15:37:09 +02:00
parent de516c68d3
commit 6ac15a5bac
8 changed files with 216 additions and 5 deletions

View file

@ -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)

14
src/include/rssi.h Normal file
View file

@ -0,0 +1,14 @@
//
// Created by nick on 19.10.17.
//
#ifndef DAWN_RSSI_H
#define DAWN_RSSI_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int get_rssi_from_iwinfo(__uint8_t* client_addr);
#endif //DAWN_RSSI_H

View file

@ -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

View file

@ -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

View file

@ -1,6 +1,9 @@
#include "datastorage.h"
#include <limits.h>
#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]);
}
}
}

78
src/utils/rssi.c Normal file
View file

@ -0,0 +1,78 @@
#include "rssi.h"
#include <limits.h>
#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;
}

View file

@ -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;
}

View file

@ -23,4 +23,24 @@ int hwaddr_aton(const char *txt, uint8_t *addr) {
}
return 0;
}
}
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;
}