From 59999d7b0f0c9c4ca7775c6121d83dc020740580 Mon Sep 17 00:00:00 2001 From: PolynomialDivision Date: Wed, 13 Dec 2017 01:41:36 +0100 Subject: [PATCH] add new network json msg, fix a lot of memory leaks, etc. ... --- src/include/ubus.h | 4 + src/network/networksocket.c | 31 +------- src/utils/ubus.c | 152 +++++++++++++++++++++++++++++++++--- 3 files changed, 144 insertions(+), 43 deletions(-) diff --git a/src/include/ubus.h b/src/include/ubus.h index 5537dde..1fdb59d 100644 --- a/src/include/ubus.h +++ b/src/include/ubus.h @@ -36,4 +36,8 @@ void update_hostapd_sockets(struct uloop_timeout *t); void add_client_update_timer(time_t time); +int handle_network_msg(char* msg); + +int send_blob_attr_via_network(struct blob_attr *msg, char* method); + #endif diff --git a/src/network/networksocket.c b/src/network/networksocket.c index ed33e02..ecacc4f 100644 --- a/src/network/networksocket.c +++ b/src/network/networksocket.c @@ -148,37 +148,8 @@ void *receive_msg_enc(void *args) { char *dec = gcrypt_decrypt_msg(base64_dec_str, base64_dec_length); - //printf("Free %s: %p\n","base64_dec_str", base64_dec_str); free(base64_dec_str); - - //printf("[WC] Network-Received: %s\n", dec); - - probe_entry prob_req; - struct blob_buf b; - - blob_buf_init(&b, 0); - blobmsg_add_json_from_string(&b, dec); - - char *str; - str = blobmsg_format_json(b.head, true); - - if (str == NULL) { - return 0; - } - - if (strlen(str) <= 0) { - return 0; - } - - if (strstr(str, "clients") != NULL) { - parse_to_clients(b.head, 0, 0); - } else if (strstr(str, "target") != NULL) { - if (parse_to_probe_req(b.head, &prob_req) == 0) { - insert_to_array(prob_req, 0); - } - } - // free encrypted string - //printf("Free %s: %p\n","dec", dec); + handle_network_msg(dec); free(dec); } } diff --git a/src/utils/ubus.c b/src/utils/ubus.c index d3a63b4..9a0f6e1 100644 --- a/src/utils/ubus.c +++ b/src/utils/ubus.c @@ -35,6 +35,17 @@ struct uloop_timeout hostapd_timer = { uint32_t hostapd_sock_arr[MAX_HOSTAPD_SOCKETS]; int hostapd_sock_last = -1; +enum { + NETWORK_METHOD, + NETWORK_DATA, + __NETWORK_MAX, +}; + +static const struct blobmsg_policy network_policy[__NETWORK_MAX] = { + [NETWORK_METHOD] = {.name = "method", .type = BLOBMSG_TYPE_STRING}, + [NETWORK_DATA] = {.name = "data", .type = BLOBMSG_TYPE_STRING}, +}; + enum { HOSTAPD_NOTIFY_BSSID_ADDR, HOSTAPD_NOTIFY_CLIENT_ADDR, @@ -362,13 +373,15 @@ static int handle_probe_req(struct blob_attr *msg) { //probe_entry tmp_probe = insert_to_array(prob_req, 1); + send_blob_attr_via_network(msg, "probe"); // send probe via network - char *str; + /*char *str; str = blobmsg_format_json(msg, true); send_string_enc(str); - printf("[WC] Hostapd-Probe: %s : %s\n", "probe", str); + printf("[WC] Hostapd-Probe: %s : %s\n", "probe", str);*/ + //print_probe_array(); /* @@ -394,17 +407,108 @@ static int handle_deauth_req(struct blob_attr *msg) { client_array_delete(client_entry); pthread_mutex_unlock(&client_array_mutex); + printf("[WC] Deauth: %s\n", "deauth"); + + return 0; +} + +int handle_network_msg(char* msg) +{ + printf("HANDLING NETWORK MSG!\n"); + struct blob_attr *tb[__NETWORK_MAX]; + char *method; + char *data; + + printf("TO JSON NETWORK MSG!\n"); blob_buf_init(&b, 0); - blobmsg_add_blob(&b, msg); - blobmsg_add_string(&b, "test", "test_attr"); + blobmsg_add_json_from_string(&b, msg); + + printf("PARSING NETWORK MSG!\n"); + blobmsg_parse(network_policy, __NETWORK_MAX, tb, blob_data(b.head), blob_len(b.head)); + printf("PARSING FINISHED NETWORK MSG!\n"); + + if(!tb[NETWORK_METHOD] ||!tb[NETWORK_DATA] ) + { + return -1; + } + printf("GET STRING NETWORK MSG!\n"); + method = blobmsg_get_string(tb[NETWORK_METHOD]); + data = blobmsg_get_string(tb[NETWORK_DATA]); + printf("GET STRING FINISHED NETWORK MSG!\n"); + + blob_buf_init(&b, 0); + blobmsg_add_json_from_string(&b, data); + printf("JSON PARSING AGAIN FINISHED NETWORK MSG!\n"); + + printf("DO STRINGCOMPARE: %s : %s!\n", method, data); + if(!blob_len(b.head)) + { + printf("NULL?!\n"); + return -1; + } + + if(blob_len(b.head) <= 0) + { + printf("NULL?!\n"); + return -1; + } + + if(strlen(method) < 5) + { + printf("STRING IS LESS THAN 5!\n"); + return -1; + } + + if (strncmp(method, "probe", 5) == 0) { + probe_entry entry; + parse_to_probe_req(b.head, &entry); + probe_array_insert(entry); + } else if (strncmp(method, "clients", 5) == 0) { + printf("PARSING CLIENTS NETWORK MSG!\n"); + parse_to_clients(b.head, 0, 0); + } else if (strncmp(method, "deauth", 5) == 0) { + hostapd_notify_entry entry; + parse_to_hostapd_notify(b.head, &entry); + + client client_entry; + memcpy(client_entry.bssid_addr, client_entry.bssid_addr, sizeof(uint8_t) * ETH_ALEN ); + memcpy(client_entry.client_addr, client_entry.client_addr, sizeof(uint8_t) * ETH_ALEN ); + + pthread_mutex_lock(&client_array_mutex); + client_array_delete(client_entry); + pthread_mutex_unlock(&client_array_mutex); + } + free(method); + free(data); + printf("HANDLING FINISHED NETWORK MSG!\n"); + return 0; +} +int send_blob_attr_via_network(struct blob_attr *msg, char* method) +{ + char *data_str; char *str; + printf("TO JSON\n"); + data_str = blobmsg_format_json(msg, true); + printf("JSON FINISHED\n"); + + printf("ADD STRINGS!\n"); + blob_buf_init(&b, 0); + blobmsg_add_string(&b, "method", method); + blobmsg_add_string(&b, "data", data_str); + printf("ADD FINISHED!\n"); + + + //blobmsg_add_blob(&b, msg); + printf("TO JSON AGAIN\n"); str = blobmsg_format_json(b.head, true); + printf("TO JSON AGAIN FINISHED\n"); + printf("SENDING\n"); send_string_enc(str); - - printf("[WC] Hostapd-Probe: %s : %s\n", "deauth", str); - + printf("SENDING FINISHED!\n"); + free(str); + free(data_str); return 0; } @@ -414,6 +518,7 @@ static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj, char *str; str = blobmsg_format_json(msg, true); printf("METHOD new: %s : %s\n", method, str); + free(str); // TODO: Only handle probe request and NOT assoc, ... @@ -617,6 +722,24 @@ dump_client_table(struct blob_attr *head, int len, const char *bssid_addr, uint3 int parse_to_clients(struct blob_attr *msg, int do_kick, uint32_t id) { struct blob_attr *tb[__CLIENT_TABLE_MAX]; + if(!msg) + { + return -1; + } + + if(!blob_data(msg)) + { + printf("DATA IS NULL!!!\n"); + return -1; + } + + printf("BLOB LEN: %d\n", blob_len(msg)); + if(blob_len(msg) <= 0) + { + printf("DATA IS NULL!!!\n"); + return -1; + } + blobmsg_parse(client_table_policy, __CLIENT_TABLE_MAX, tb, blob_data(msg), blob_len(msg)); if (tb[CLIENT_TABLE] && tb[CLIENT_TABLE_BSSID] && tb[CLIENT_TABLE_FREQ] && tb[CLIENT_TABLE_HT] && @@ -644,10 +767,14 @@ static void ubus_get_clients_cb(struct ubus_request *req, int type, struct blob_ if (!msg) return; + printf("PARSE TO CLIENTS!\n"); parse_to_clients(msg, 1, req->peer); - char *str = blobmsg_format_json(msg, true); - send_string_enc(str); + printf("SENDING CLIENTS VIA NETWORK\n"); + send_blob_attr_via_network(msg, "clients"); + printf("SEND CLIENTS FINISHED!\n"); + + print_client_array(); print_ap_array(); } @@ -730,6 +857,8 @@ int ubus_call_umdns() { int ubus_send_probe_via_network(struct probe_entry_s probe_entry) { + printf("SENDING PROBE VIA NETWORK!\n"); + static struct blob_buf b; blob_buf_init(&b, 0); @@ -741,10 +870,7 @@ int ubus_send_probe_via_network(struct probe_entry_s probe_entry) { 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); + send_blob_attr_via_network(b.head, "probe"); return 0; } \ No newline at end of file