diff --git a/files/mac_list b/files/mac_list index e69de29..8a35cbc 100644 --- a/files/mac_list +++ b/files/mac_list @@ -0,0 +1,2 @@ +a4:2b:b0:de:f1:fd +f0:79:60:1c:26:f0 diff --git a/src/include/datastorage.h b/src/include/datastorage.h index 9705a27..efe3580 100644 --- a/src/include/datastorage.h +++ b/src/include/datastorage.h @@ -8,14 +8,30 @@ #include #include #include +#include + #ifndef ETH_ALEN #define ETH_ALEN 6 #endif +/* Mac */ + +// ---------------- Defines ------------------- +#define MAC_LIST_LENGTH 100 + +// ---------------- Structs ---------------- +uint8_t mac_list[MAC_LIST_LENGTH][ETH_ALEN]; + +// ---------------- Functions ---------- +void insert_macs_from_file(); +int insert_to_maclist(uint8_t mac[]); + /* Metric */ +struct probe_metric_s dawn_metric; + // ---------------- Structs ---------------- struct probe_metric_s { int ht_support; @@ -175,6 +191,8 @@ void print_ap_array(); ap ap_array_get_ap(uint8_t bssid_addr[]); +int build_hearing_map_sort_client(struct blob_buf *b); + /* Utils */ // ---------------- Defines ------------------- diff --git a/src/include/ubus.h b/src/include/ubus.h index 5c169fc..7788cb0 100644 --- a/src/include/ubus.h +++ b/src/include/ubus.h @@ -6,8 +6,6 @@ #include "datastorage.h" -#define MIN_PROBE_REQ 2 // TODO: Parse from config file... - int dawn_init_ubus(const char *ubus_socket, const char *hostapd_dir); int parse_to_probe_req(struct blob_attr *msg, probe_entry *prob_req); @@ -34,4 +32,6 @@ void update_hostapd_sockets(struct uloop_timeout *t); void add_client_update_timer(time_t time); +void blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const uint8_t *addr); + #endif diff --git a/src/include/utils.h b/src/include/utils.h index ec0e87a..e07a776 100644 --- a/src/include/utils.h +++ b/src/include/utils.h @@ -15,4 +15,6 @@ int hwaddr_aton(const char *txt, uint8_t *addr); int convert_mac(char *in, char *out); +void write_mac_to_file(char* path, uint8_t addr[]); + #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 84294d8..4252b4f 100644 --- a/src/main.c +++ b/src/main.c @@ -8,19 +8,18 @@ #include "networksocket.h" #include "ubus.h" #include "dawn_uci.h" -#include "dawn_uci.h" #include "crypto.h" -#define BUFSIZE 17 -#define BUFSIZE_DIR 256 - void daemon_shutdown(); void signal_handler(int sig); -struct sigaction newSigAction; +int init_mutex(); + +struct sigaction signal_action; void daemon_shutdown() { + // kill threads close_socket(); uci_clear(); @@ -36,50 +35,24 @@ void daemon_shutdown() { } void signal_handler(int sig) { - printf("SOME SIGNAL RECEIVED!\n"); switch (sig) { case SIGHUP: + daemon_shutdown(); break; case SIGINT: + daemon_shutdown(); + break; case SIGTERM: daemon_shutdown(); exit(EXIT_SUCCESS); - break; default: + daemon_shutdown(); break; } } -int main(int argc, char **argv) { - //free_counter = 0; - - const char *ubus_socket = NULL; - // int ch; - - argc -= optind; - argv += optind; - - newSigAction.sa_handler = signal_handler; - sigemptyset(&newSigAction.sa_mask); - newSigAction.sa_flags = 0; - - sigaction(SIGHUP, &newSigAction, NULL); - sigaction(SIGTERM, &newSigAction, NULL); - sigaction(SIGINT, &newSigAction, NULL); - - uci_init(); - struct network_config_s net_config = uci_get_dawn_network(); - printf("Broadcst bla: %s\n", net_config.broadcast_ip); - - gcrypt_init(); - gcrypt_set_key_and_iv(net_config.shared_key, net_config.iv); - - struct time_config_s time_config = uci_get_time_config(); - timeout_config = time_config; // TODO: Refactor... - - hostapd_dir_glob = uci_get_dawn_hostapd_dir(); - sort_string = (char*) uci_get_dawn_sort_order(); - +int init_mutex() +{ if (pthread_mutex_init(&list_mutex, NULL) != 0) { printf("\n mutex init failed\n"); return 1; @@ -99,9 +72,42 @@ int main(int argc, char **argv) { printf("\n mutex init failed\n"); return 1; } + return 0; +} + +int main(int argc, char **argv) { + + const char *ubus_socket = NULL; + + argc -= optind; + argv += optind; + + // connect signals + signal_action.sa_handler = signal_handler; + sigemptyset(&signal_action.sa_mask); + signal_action.sa_flags = 0; + sigaction(SIGHUP, &signal_action, NULL); + sigaction(SIGTERM, &signal_action, NULL); + sigaction(SIGINT, &signal_action, NULL); + + uci_init(); + struct network_config_s net_config = uci_get_dawn_network(); + printf("Broadcst bla: %s\n", net_config.broadcast_ip); + + gcrypt_init(); + gcrypt_set_key_and_iv(net_config.shared_key, net_config.iv); + + struct time_config_s time_config = uci_get_time_config(); + timeout_config = time_config; // TODO: Refactor... + + hostapd_dir_glob = uci_get_dawn_hostapd_dir(); + sort_string = (char*) uci_get_dawn_sort_order(); + + init_mutex(); init_socket_runopts(net_config.broadcast_ip, net_config.broadcast_port, net_config.bool_multicast); + insert_macs_from_file(); dawn_init_ubus(ubus_socket, hostapd_dir_glob); return 0; diff --git a/src/storage/datastorage.c b/src/storage/datastorage.c index bc3ab08..5ec49f5 100644 --- a/src/storage/datastorage.c +++ b/src/storage/datastorage.c @@ -41,11 +41,14 @@ int probe_array_update_rssi(uint8_t bssid_addr[], uint8_t client_addr[], uint32_ int is_connected(uint8_t bssid_addr[], uint8_t client_addr[]); +int mac_in_maclist(uint8_t mac[]); + int compare_station_count(uint8_t *bssid_addr_own, uint8_t *bssid_addr_to_compare, int automatic_kick); int probe_entry_last = -1; int client_entry_last = -1; int ap_entry_last = -1; +int mac_list_entry_last = -1; void remove_probe_array_cb(struct uloop_timeout *t); @@ -65,6 +68,52 @@ struct uloop_timeout ap_timeout = { .cb = remove_ap_array_cb }; +int build_hearing_map_sort_client(struct blob_buf *b) +{ + pthread_mutex_lock(&probe_array_mutex); + + void *client_list, *ap_list; + char ap_mac_buf[20]; + char client_mac_buf[20]; + + blob_buf_init(b, 0); + int i; + for (i = 0; i <= probe_entry_last; i++) { + int k; + sprintf(client_mac_buf, MACSTR, MAC2STR(probe_array[i].client_addr)); + client_list = blobmsg_open_table(b, client_mac_buf); + for (k = i; i <= probe_entry_last; k++){ + if(!mac_is_equal(probe_array[k].client_addr, probe_array[i].client_addr)) + { + i = k - 1; + break; + } + sprintf(ap_mac_buf, MACSTR, MAC2STR(probe_array[k].bssid_addr)); + ap_list = blobmsg_open_table(b, ap_mac_buf); + blobmsg_add_u32(b, "signal", probe_array[k].signal); + blobmsg_add_u32(b, "freq", probe_array[k].freq); + blobmsg_add_u8(b, "ht_support", probe_array[k].ht_support); + blobmsg_add_u8(b, "vht_support", probe_array[k].vht_support); + + ap ap_entry = ap_array_get_ap(probe_array[k].bssid_addr); + + // check if ap entry is available + if (mac_is_equal(ap_entry.bssid_addr, probe_array[k].bssid_addr)) { + blobmsg_add_u32(b, "channel_utilization", ap_entry.channel_utilization); + blobmsg_add_u32(b, "num_sta", ap_entry.station_count); + blobmsg_add_u32(b, "ht", ap_entry.ht); + blobmsg_add_u32(b, "vht", ap_entry.vht); + } + + blobmsg_add_u32(b, "score", eval_probe_metric(probe_array[k])); + blobmsg_close_table(b, ap_list); + } + blobmsg_close_table(b, client_list); + } + pthread_mutex_unlock(&probe_array_mutex); + return 0; +} + int eval_probe_metric(struct probe_entry_s probe_entry) { int score = 0; @@ -175,7 +224,7 @@ int better_ap_available(uint8_t bssid_addr[], uint8_t client_addr[], int automat } int kick_client(struct client_s client_entry) { - return better_ap_available(client_entry.bssid_addr, client_entry.client_addr, 1); + return !mac_in_maclist(client_entry.client_addr) && better_ap_available(client_entry.bssid_addr, client_entry.client_addr, 1); } void kick_clients(uint8_t bssid[], uint32_t id) { @@ -667,6 +716,81 @@ void insert_client_to_array(client entry) { pthread_mutex_unlock(&client_array_mutex); } +void insert_macs_from_file() +{ + FILE * fp; + char * line = NULL; + size_t len = 0; + ssize_t read; + + fp = fopen("/etc/dawn/mac_list", "r"); + if (fp == NULL) + exit(EXIT_FAILURE); + + while ((read = getline(&line, &len, fp)) != -1) { + printf("Retrieved line of length %zu :\n", read); + printf("%s", line); + + int tmp_int_mac[ETH_ALEN]; + sscanf(line, MACSTR, STR2MAC(tmp_int_mac)); + + mac_list_entry_last++; + for (int i = 0; i < ETH_ALEN; ++i) { + mac_list[mac_list_entry_last][i] = (uint8_t) tmp_int_mac[i]; + } + } + + printf("Printing MAC List:\n"); + for(int i = 0; i <= mac_list_entry_last; i++) + { + char mac_buf_target[20]; + sprintf(mac_buf_target, MACSTR, MAC2STR(mac_list[i])); + printf("%d: %s\n", i, mac_buf_target); + } + + fclose(fp); + if (line) + free(line); + //exit(EXIT_SUCCESS); +} + +int insert_to_maclist(uint8_t mac[]) +{ + if(mac_in_maclist(mac)) + { + return -1; + } + + mac_list_entry_last++; + for (int i = 0; i < ETH_ALEN; ++i) { + mac_list[mac_list_entry_last][i] = mac[i]; + } + + return 0; +} + + +int mac_in_maclist(uint8_t mac[]) +{ + for(int i = 0; i <= mac_list_entry_last; i++) + { + if(mac_is_equal(mac, mac_list[i])) + { + return 1; + } + } + return 0; +} + + + + + + + + + + node *delete_probe_req(node **ret_remove, node *head, uint8_t bssid_addr[], uint8_t client_addr[]); diff --git a/src/utils/ubus.c b/src/utils/ubus.c index 28db0e5..30c99f4 100644 --- a/src/utils/ubus.c +++ b/src/utils/ubus.c @@ -137,11 +137,21 @@ static int subscribe_to_hostapd_interfaces(const char *hostapd_dir); static int ubus_get_clients(); -int hostapd_array_check_id(uint32_t id); +static int +add_mac(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg); + +static int get_hearing_map(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg); + + int hostapd_array_check_id(uint32_t id); void hostapd_array_insert(uint32_t id); void hostapd_array_delete(uint32_t id); +static void ubus_add_oject(); void add_client_update_timer(time_t time) { uloop_timeout_set(&client_timer, time); @@ -193,8 +203,7 @@ void hostapd_array_delete(uint32_t id) { } -static void -blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const uint8_t *addr) { +void blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const uint8_t *addr) { char *s; s = blobmsg_alloc_string_buffer(buf, name, 20); @@ -473,8 +482,11 @@ int dawn_init_ubus(const char *ubus_socket, const char *hostapd_dir) { //ubus_call_umdns(); + ubus_add_oject(); + uloop_run(); + close_socket(); ubus_free(ctx); @@ -694,4 +706,77 @@ int ubus_send_probe_via_network(struct probe_entry_s probe_entry) { send_string_enc(str); return 0; +} + +enum { + MAC_ADDR, + __ADD_DEL_MAC_MAX +}; + +static const struct blobmsg_policy add_del_policy[__ADD_DEL_MAC_MAX] = { + [MAC_ADDR] = { "addr", BLOBMSG_TYPE_STRING }, +}; + +static const struct ubus_method dawn_methods[] = { + UBUS_METHOD("add_mac", add_mac, add_del_policy), + UBUS_METHOD_NOARG("get_hearing_map", get_hearing_map) + //UBUS_METHOD_NOARG("get_aps"); + //UBUS_METHOD_NOARG("get_clients"); +}; + +static struct ubus_object_type dawn_object_type = + UBUS_OBJECT_TYPE("dawn", dawn_methods); + +static struct ubus_object dawn_object = { + .name = "dawn", + .type = &dawn_object_type, + .methods = dawn_methods, + .n_methods = ARRAY_SIZE(dawn_methods), +}; + +static int add_mac(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) { + + struct blob_attr *tb[__ADD_DEL_MAC_MAX]; + uint8_t addr[ETH_ALEN]; + + blobmsg_parse(add_del_policy, __ADD_DEL_MAC_MAX, tb, blob_data(msg), blob_len(msg)); + + if (!tb[MAC_ADDR]) + return UBUS_STATUS_INVALID_ARGUMENT; + + if (hwaddr_aton(blobmsg_data(tb[MAC_ADDR]), addr)) + return UBUS_STATUS_INVALID_ARGUMENT; + + if(insert_to_maclist(addr) == 0) + { + write_mac_to_file("/etc/dawn/mac_list", addr); + } + + return 0; +} + +static int get_hearing_map(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) { + + build_hearing_map_sort_client(&b); + ubus_send_reply(ctx, req, b.head); + return 0; +} + +static void ubus_add_oject() +{ + int ret; + + ret = ubus_add_object(ctx, &dawn_object); + if (ret) + fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret)); + printf("ADDED UBUS OBJECT!!!\n"); + + /*ret = ubus_register_subscriber(ctx, &test_event); + if (ret) + fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret)); + */ } \ No newline at end of file diff --git a/src/utils/utils.c b/src/utils/utils.c index 323d4b2..8af0488 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -44,3 +44,19 @@ int convert_mac(char *in, char *out) { return 0; } +void write_mac_to_file(char* path, uint8_t addr[]) +{ + FILE *f = fopen(path, "a"); + if (f == NULL) + { + printf("Error opening file!\n"); + exit(1); + } + + char mac_buf[20]; + sprintf(mac_buf, MACSTR, MAC2STR(addr)); + + fprintf(f, "%s\n", mac_buf); + + fclose(f); +} \ No newline at end of file