diff --git a/src/main.c b/src/main.c index 726e36c..1581c70 100644 --- a/src/main.c +++ b/src/main.c @@ -9,76 +9,76 @@ #define BUFSIZE_DIR 255 int main(int argc, char **argv) { - const char *ubus_socket = NULL; - int ch; + const char *ubus_socket = NULL; + int ch; - char opt_broadcast_ip[BUFSIZE]; - char opt_broadcast_port[BUFSIZE]; - char opt_hostapd_dir[BUFSIZE_DIR]; + char opt_broadcast_ip[BUFSIZE]; + char opt_broadcast_port[BUFSIZE]; + char opt_hostapd_dir[BUFSIZE_DIR]; - while ((ch = getopt(argc, argv, "cs:p:i:b:o:h:")) != -1) { - switch (ch) { - case 's': - ubus_socket = optarg; - break; - case 'p': - snprintf(opt_broadcast_port, BUFSIZE, "%s", optarg); - printf("broadcast port: %s\n", opt_broadcast_port); - break; - case 'i': - snprintf(opt_broadcast_ip, BUFSIZE, "%s", optarg); - printf("broadcast ip: %s\n", opt_broadcast_ip); - break; - case 'o': - snprintf(sort_string, SORT_NUM, "%s", optarg); - printf("sort string: %s\n", sort_string); - case 'h': - snprintf(opt_hostapd_dir, BUFSIZE_DIR, "%s", optarg); - printf("hostapd dir: %s\n", opt_hostapd_dir); - hostapd_dir_glob = optarg; - default: - break; + while ((ch = getopt(argc, argv, "cs:p:i:b:o:h:")) != -1) { + switch (ch) { + case 's': + ubus_socket = optarg; + break; + case 'p': + snprintf(opt_broadcast_port, BUFSIZE, "%s", optarg); + printf("broadcast port: %s\n", opt_broadcast_port); + break; + case 'i': + snprintf(opt_broadcast_ip, BUFSIZE, "%s", optarg); + printf("broadcast ip: %s\n", opt_broadcast_ip); + break; + case 'o': + snprintf(sort_string, SORT_NUM, "%s", optarg); + printf("sort string: %s\n", sort_string); + case 'h': + snprintf(opt_hostapd_dir, BUFSIZE_DIR, "%s", optarg); + printf("hostapd dir: %s\n", opt_hostapd_dir); + hostapd_dir_glob = optarg; + default: + break; + } } - } - argc -= optind; - argv += optind; + argc -= optind; + argv += optind; - if (pthread_mutex_init(&list_mutex, NULL) != 0) { - printf("\n mutex init failed\n"); - return 1; - } + if (pthread_mutex_init(&list_mutex, NULL) != 0) { + printf("\n mutex init failed\n"); + return 1; + } - if (pthread_mutex_init(&probe_array_mutex, NULL) != 0) { - printf("\n mutex init failed\n"); - return 1; - } + if (pthread_mutex_init(&probe_array_mutex, NULL) != 0) { + printf("\n mutex init failed\n"); + return 1; + } - if (pthread_mutex_init(&client_array_mutex, NULL) != 0) { - printf("\n mutex init failed\n"); - return 1; - } - - init_socket_runopts(opt_broadcast_ip, opt_broadcast_port, 1); + if (pthread_mutex_init(&client_array_mutex, NULL) != 0) { + printf("\n mutex init failed\n"); + return 1; + } - pthread_t tid_probe; - pthread_create(&tid_probe, NULL, &remove_array_thread, NULL); + init_socket_runopts(opt_broadcast_ip, opt_broadcast_port, 1); - pthread_t tid_client; - pthread_create(&tid_client, NULL, &remove_client_array_thread, NULL); + pthread_t tid_probe; + pthread_create(&tid_probe, NULL, &remove_array_thread, NULL); - pthread_t tid_get_client; - pthread_create(&tid_get_client, NULL, &update_clients_thread, NULL); + pthread_t tid_client; + pthread_create(&tid_client, NULL, &remove_client_array_thread, NULL); + + pthread_t tid_get_client; + pthread_create(&tid_get_client, NULL, &update_clients_thread, NULL); - - //pthread_create(&tid, NULL, &remove_thread, NULL); - dawn_init_ubus(ubus_socket, opt_hostapd_dir); + //pthread_create(&tid, NULL, &remove_thread, NULL); - // free ressources - pthread_mutex_destroy(&list_mutex); - free_list(probe_list_head); + dawn_init_ubus(ubus_socket, opt_hostapd_dir); - return 0; + // free ressources + pthread_mutex_destroy(&list_mutex); + free_list(probe_list_head); + + return 0; } \ No newline at end of file diff --git a/src/network/broadcastsocket.c b/src/network/broadcastsocket.c index 44c6ce9..152a7d8 100644 --- a/src/network/broadcastsocket.c +++ b/src/network/broadcastsocket.c @@ -9,33 +9,33 @@ #include "broadcastsocket.h" int setup_broadcast_socket(const char *_broadcast_ip, unsigned short _broadcast_port, struct sockaddr_in *addr) { - int sock; - int broadcast_permission; + int sock; + int broadcast_permission; - /* Create socket */ - if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { - fprintf(stderr, "Failed to create socket.\n"); - return -1; - } + /* Create socket */ + if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { + fprintf(stderr, "Failed to create socket.\n"); + return -1; + } - /* Allow broadcast */ - broadcast_permission = 1; - if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *)&broadcast_permission, - sizeof(broadcast_permission)) < 0) { - fprintf(stderr, "Failed to create socket.\n"); - return -1; - } + /* Allow broadcast */ + broadcast_permission = 1; + if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) &broadcast_permission, + sizeof(broadcast_permission)) < 0) { + fprintf(stderr, "Failed to create socket.\n"); + return -1; + } - /* Construct Address */ - memset(addr, 0, sizeof(*addr)); - addr->sin_family = AF_INET; - addr->sin_addr.s_addr = inet_addr(_broadcast_ip); - addr->sin_port = htons(_broadcast_port); + /* Construct Address */ + memset(addr, 0, sizeof(*addr)); + addr->sin_family = AF_INET; + addr->sin_addr.s_addr = inet_addr(_broadcast_ip); + addr->sin_port = htons(_broadcast_port); - if (bind(sock, (struct sockaddr *)addr, sizeof(*addr)) < - 0) { - fprintf(stderr, "Binding socket failed!\n"); - return -1; - } - return sock; + if (bind(sock, (struct sockaddr *) addr, sizeof(*addr)) < + 0) { + fprintf(stderr, "Binding socket failed!\n"); + return -1; + } + return sock; } \ No newline at end of file diff --git a/src/network/multicastsocket.c b/src/network/multicastsocket.c index 0f5e49a..5fed812 100644 --- a/src/network/multicastsocket.c +++ b/src/network/multicastsocket.c @@ -13,59 +13,58 @@ static struct ip_mreq command; /* static ?! */ -int setup_multicast_socket(const char *_multicast_ip, unsigned short _multicast_port, struct sockaddr_in *addr) -{ - int loop = 1; - int sock; +int setup_multicast_socket(const char *_multicast_ip, unsigned short _multicast_port, struct sockaddr_in *addr) { + int loop = 1; + int sock; - memset (addr, 0, sizeof (*addr)); - addr->sin_family = AF_INET; - addr->sin_addr.s_addr = htonl (INADDR_ANY); - addr->sin_port = htons (_multicast_port); + memset(addr, 0, sizeof(*addr)); + addr->sin_family = AF_INET; + addr->sin_addr.s_addr = htonl (INADDR_ANY); + addr->sin_port = htons (_multicast_port); - if ( ( sock = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { - perror ("socket()"); - exit (EXIT_FAILURE); - } + if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { + perror("socket()"); + exit(EXIT_FAILURE); + } - /* Mehr Prozessen erlauben, denselben Port zu nutzen */ - loop = 1; - if (setsockopt ( sock, + /* Mehr Prozessen erlauben, denselben Port zu nutzen */ + loop = 1; + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, - &loop, sizeof (loop)) < 0) { - perror ("setsockopt:SO_REUSEADDR"); - exit (EXIT_FAILURE); - } - if(bind( sock, - (struct sockaddr *)addr, - sizeof(*addr)) < 0) { - perror ("bind"); - exit (EXIT_FAILURE); - } + &loop, sizeof(loop)) < 0) { + perror("setsockopt:SO_REUSEADDR"); + exit(EXIT_FAILURE); + } + if (bind(sock, + (struct sockaddr *) addr, + sizeof(*addr)) < 0) { + perror("bind"); + exit(EXIT_FAILURE); + } - /* Broadcast auf dieser Maschine zulassen */ - loop = 1; - if (setsockopt ( sock, + /* Broadcast auf dieser Maschine zulassen */ + loop = 1; + if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, - &loop, sizeof (loop)) < 0) { - perror ("setsockopt:IP_MULTICAST_LOOP"); - exit (EXIT_FAILURE); - } + &loop, sizeof(loop)) < 0) { + perror("setsockopt:IP_MULTICAST_LOOP"); + exit(EXIT_FAILURE); + } - /* Join the broadcast group: */ - command.imr_multiaddr.s_addr = inet_addr (_multicast_ip); - command.imr_interface.s_addr = htonl (INADDR_ANY); - if (command.imr_multiaddr.s_addr == -1) { - perror ("224.0.0.1 ist keine Multicast-Adresse\n"); - exit (EXIT_FAILURE); - } - if (setsockopt ( sock, + /* Join the broadcast group: */ + command.imr_multiaddr.s_addr = inet_addr(_multicast_ip); + command.imr_interface.s_addr = htonl (INADDR_ANY); + if (command.imr_multiaddr.s_addr == -1) { + perror("224.0.0.1 ist keine Multicast-Adresse\n"); + exit(EXIT_FAILURE); + } + if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, - &command, sizeof (command)) < 0) { - perror ("setsockopt:IP_ADD_MEMBERSHIP"); - } - return sock; + &command, sizeof(command)) < 0) { + perror("setsockopt:IP_ADD_MEMBERSHIP"); + } + return sock; } \ No newline at end of file diff --git a/src/network/networksocket.c b/src/network/networksocket.c index 7f550ea..07ab8e3 100644 --- a/src/network/networksocket.c +++ b/src/network/networksocket.c @@ -26,127 +26,120 @@ const char *ip; unsigned short port; char recv_string[MAX_RECV_STRING + 1]; int recv_string_len; + void *receive_msg(void *args); int init_socket_runopts(char *_ip, char *_port, int broadcast_socket) { - - port = atoi(_port); - ip = _ip; - if(broadcast_socket) - { - sock = setup_broadcast_socket(ip, port, &addr); - } else - { - printf("Settingup multicastsocket!\n"); - sock = setup_multicast_socket(ip, port, &addr); - } + port = atoi(_port); + ip = _ip; - pthread_t sniffer_thread; - if (pthread_create(&sniffer_thread, NULL, receive_msg, NULL)) { - fprintf(stderr, "Could not create receiving thread!"); - return -1; - } + if (broadcast_socket) { + sock = setup_broadcast_socket(ip, port, &addr); + } else { + printf("Settingup multicastsocket!\n"); + sock = setup_multicast_socket(ip, port, &addr); + } - fprintf(stdout, "Connected to %s:%d\n", ip, port); + pthread_t sniffer_thread; + if (pthread_create(&sniffer_thread, NULL, receive_msg, NULL)) { + fprintf(stderr, "Could not create receiving thread!"); + return -1; + } - return 0; + fprintf(stdout, "Connected to %s:%d\n", ip, port); + + return 0; } void *receive_msg(void *args) { - while (1) { - if ((recv_string_len = - recvfrom(sock, recv_string, MAX_RECV_STRING, 0, NULL, 0)) < 0) { - fprintf(stderr, "Could not receive message!"); - continue; + while (1) { + if ((recv_string_len = + recvfrom(sock, recv_string, MAX_RECV_STRING, 0, NULL, 0)) < 0) { + fprintf(stderr, "Could not receive message!"); + continue; + } + + if (recv_string == NULL) { + return 0; + } + + if (strlen(recv_string) <= 0) { + return 0; + } + recv_string[recv_string_len] = '\0'; + + printf("[WC] Network-Received: %s\n", recv_string); + + probe_entry prob_req; + struct blob_buf b; + + blob_buf_init(&b, 0); + blobmsg_add_json_from_string(&b, recv_string); + + char *str; + str = blobmsg_format_json(b.head, true); + + + /* + TODO: REFACTOR THIS!!! (just workaround) + OTHERWISE NULLPOINTER?! + + */ + + if (str == NULL) { + return 0; + } + + if (strlen(str) <= 0) { + return 0; + } + + /* + HERE IS NULLPOINTER PROBABLY + */ + + if (strstr(str, "clients") != NULL) { + parse_to_clients(b.head); + } else if (strstr(str, "target") != NULL) { + if (parse_to_probe_req(b.head, &prob_req) == 0) { + insert_to_array(prob_req, 0); + } + } + + //if(parse_to_probe_req(b.head, &prob_req) == 0) + //{ + // insert_to_array(prob_req, 0); + //} + + + // insert to list + //insert_to_list(prob_req, 0); } - - if(recv_string == NULL) - { - return 0; - } - - if(strlen(recv_string) <= 0) - { - return 0; - } - recv_string[recv_string_len] = '\0'; - - printf("[WC] Network-Received: %s\n", recv_string); - - probe_entry prob_req; - struct blob_buf b; - - blob_buf_init(&b, 0); - blobmsg_add_json_from_string(&b, recv_string); - - char *str; - str = blobmsg_format_json(b.head, true); - - - /* - TODO: REFACTOR THIS!!! (just workaround) - OTHERWISE NULLPOINTER?! - - */ - - if(str == NULL) - { - return 0; - } - - if( strlen(str) <= 0) - { - return 0; - } - - /* - HERE IS NULLPOINTER PROBABLY - */ - - if(strstr(str, "clients") != NULL) { - parse_to_clients(b.head); - } else if(strstr(str, "target")!= NULL) - { - if(parse_to_probe_req(b.head, &prob_req) == 0) - { - insert_to_array(prob_req, 0); - } - } - - //if(parse_to_probe_req(b.head, &prob_req) == 0) - //{ - // insert_to_array(prob_req, 0); - //} - - - // insert to list - //insert_to_list(prob_req, 0); - } } - + int send_string(char *msg) { - pthread_mutex_lock(&send_mutex); - int msglen = strlen(msg); - printf("Sending string! %s\n", msg); - if (sendto( sock, - msg, - msglen, - 0, - (struct sockaddr *) &addr, - sizeof (addr)) < 0) { - perror ("sendto()"); - exit (EXIT_FAILURE); + pthread_mutex_lock(&send_mutex); + int msglen = strlen(msg); + printf("Sending string! %s\n", msg); + if (sendto(sock, + msg, + msglen, + 0, + (struct sockaddr *) &addr, + sizeof(addr)) < 0) { + perror("sendto()"); + exit(EXIT_FAILURE); } - pthread_mutex_unlock(&send_mutex); + pthread_mutex_unlock(&send_mutex); - /*if (sendto(sock, msg, msglen, 0, (struct sockaddr *)&addr, - sizeof(addr)) != msglen) { - fprintf(stderr, "Failed to send message.\n"); - return -1; - }*/ - return 0; + /*if (sendto(sock, msg, msglen, 0, (struct sockaddr *)&addr, + sizeof(addr)) != msglen) { + fprintf(stderr, "Failed to send message.\n"); + return -1; + }*/ + return 0; } void close_socket() { close(sock); } diff --git a/src/storage/datastorage.c b/src/storage/datastorage.c index c856433..01242bf 100644 --- a/src/storage/datastorage.c +++ b/src/storage/datastorage.c @@ -6,683 +6,640 @@ int go_next_help(char sort_order[], int i, probe_entry entry, probe_entry next_entry); + int go_next(char sort_order[], int i, probe_entry entry, probe_entry next_entry); + int mac_is_equal(uint8_t addr1[], uint8_t addr2[]); + int mac_is_greater(uint8_t addr1[], uint8_t addr2[]); + void print_probe_entry(probe_entry entry); + void remove_old_probe_entries(time_t current_time, long long int threshold); int client_array_go_next(char sort_order[], int i, client entry, - client next_entry); + client next_entry); + int client_array_go_next_help(char sort_order[], int i, client entry, - client next_entry); + client next_entry); + void remove_old_client_entries(time_t current_time, long long int threshold); + int kick_client(uint8_t bssid[], uint8_t client[]); int probe_entry_last = -1; int client_entry_last = -1; -int kick_client(uint8_t bssid[], uint8_t client[]) -{ - int i; - for(i = 0; i <= client_entry_last; i++) - { - if(mac_is_equal(probe_array[i].client_addr, client)) - { - // check if bssid is first in list... - return (mac_is_equal(bssid, probe_array[i].bssid_addr)); +int kick_client(uint8_t bssid[], uint8_t client[]) { + int i; + for (i = 0; i <= client_entry_last; i++) { + if (mac_is_equal(probe_array[i].client_addr, client)) { + // check if bssid is first in list... + return (mac_is_equal(bssid, probe_array[i].bssid_addr)); + } } - } - return 0; + return 0; } -void kick_clients(uint8_t bssid[]) -{ - // Seach for BSSID - int i; - for(i = 0; i <= client_entry_last; i++) - { - if(mac_is_equal(client_array[i].bssid_addr, bssid)) - { - break; +void kick_clients(uint8_t bssid[]) { + // Seach for BSSID + int i; + for (i = 0; i <= client_entry_last; i++) { + if (mac_is_equal(client_array[i].bssid_addr, bssid)) { + break; + } } - } - // Go threw clients - int j; - for(j = i; j <= client_entry_last; j++) - { - if(!mac_is_equal(client_array[j].bssid_addr, bssid)) - { - break; + // Go threw clients + int j; + for (j = i; j <= client_entry_last; j++) { + if (!mac_is_equal(client_array[j].bssid_addr, bssid)) { + break; + } + if (kick_client(bssid, client_array[j].client_addr)) { + /* + TODO: KICK ONLY FROM ONE BSSID? + */ + printf("KICKING CLIENT!!!!!!!!!!!!!\n"); + //del_client(client_array[j].client_addr, 5, 1, 60000); + } else { + printf("STAAAY CLIENT!!!!!!!!!!!!!\n"); + } } - if(kick_client(bssid, client_array[j].client_addr)) - { - /* - TODO: KICK ONLY FROM ONE BSSID? - */ - printf("KICKING CLIENT!!!!!!!!!!!!!\n"); - //del_client(client_array[j].client_addr, 5, 1, 60000); - } else - { - printf("STAAAY CLIENT!!!!!!!!!!!!!\n"); - } - } } int client_array_go_next_help(char sort_order[], int i, client entry, - client next_entry) { - switch (sort_order[i]) { - // bssid-mac - case 'b': - return mac_is_greater(entry.bssid_addr, next_entry.bssid_addr) && - mac_is_equal(entry.client_addr, next_entry.client_addr); - break; + client next_entry) { + switch (sort_order[i]) { + // bssid-mac + case 'b': + return mac_is_greater(entry.bssid_addr, next_entry.bssid_addr) && + mac_is_equal(entry.client_addr, next_entry.client_addr); + break; - // client-mac - case 'c': - return mac_is_greater(entry.client_addr, next_entry.client_addr); - break; + // client-mac + case 'c': + return mac_is_greater(entry.client_addr, next_entry.client_addr); + break; - // frequency - // mac is 5 ghz or 2.4 ghz? - // case 'f': - // return //entry.freq < next_entry.freq && - // entry.freq < 5000 && - // next_entry.freq >= 5000 && - // //entry.freq < 5 && - // mac_is_equal(entry.client_addr, next_entry.client_addr); - // break; + // frequency + // mac is 5 ghz or 2.4 ghz? + // case 'f': + // return //entry.freq < next_entry.freq && + // entry.freq < 5000 && + // next_entry.freq >= 5000 && + // //entry.freq < 5 && + // mac_is_equal(entry.client_addr, next_entry.client_addr); + // break; - // signal strength (RSSI) - //case 's': - // return entry.signal < next_entry.signal && - // mac_is_equal(entry.client_addr, next_entry.client_addr); - // break; + // signal strength (RSSI) + //case 's': + // return entry.signal < next_entry.signal && + // mac_is_equal(entry.client_addr, next_entry.client_addr); + // break; - default: - return 0; - break; - } + default: + return 0; + break; + } } int client_array_go_next(char sort_order[], int i, client entry, - client next_entry) { - int conditions = 1; - for (int j = 0; j < i; j++) { - i &= !(client_array_go_next(sort_order, j, entry, next_entry)); - } - return conditions && client_array_go_next_help(sort_order, i, entry, next_entry); -} - -void client_array_insert(client entry) -{ - if(client_entry_last == -1) - { - client_array[0] = entry; - client_entry_last++; - return; - } - - int i; - for(i = 0; i <= client_entry_last; i++) - { - if(!client_array_go_next("bc", 2, entry, client_array[i])) - { - break; + client next_entry) { + int conditions = 1; + for (int j = 0; j < i; j++) { + i &= !(client_array_go_next(sort_order, j, entry, next_entry)); } - } - for(int j = client_entry_last; j >= i; j--) - { - if(j + 1 <= ARRAY_LEN) - { - client_array[j + 1] = client_array[j]; + return conditions && client_array_go_next_help(sort_order, i, entry, next_entry); +} + +void client_array_insert(client entry) { + if (client_entry_last == -1) { + client_array[0] = entry; + client_entry_last++; + return; } - } - client_array[i] = entry; - if(client_entry_last < ARRAY_LEN) - { - client_entry_last++; - } -} - -client* client_array_delete(client entry) -{ - - int i; - int found_in_array = 0; - client* tmp = NULL; - - if(client_entry_last == -1) - { - return NULL; - } - - for(i = 0; i <= client_entry_last; i++) - { - if(mac_is_equal(entry.bssid_addr, client_array[i].bssid_addr) && - mac_is_equal(entry.client_addr, client_array[i].client_addr)) - { - found_in_array = 1; - tmp = &client_array[i]; - break; + int i; + for (i = 0; i <= client_entry_last; i++) { + if (!client_array_go_next("bc", 2, entry, client_array[i])) { + break; + } } - } - - for(int j = i; j <= client_entry_last; j++) - { - client_array[j] = client_array[j + 1]; - } - - if(client_entry_last > -1 && found_in_array) - { - client_entry_last--; - } - return tmp; -} - - -void probe_array_insert(probe_entry entry) -{ - if(probe_entry_last == -1) - { - probe_array[0] = entry; - probe_entry_last++; - return; - } - - int i; - for(i = 0; i <= probe_entry_last; i++) - { - if(!go_next(sort_string, SORT_NUM, entry, probe_array[i])) - { - break; + for (int j = client_entry_last; j >= i; j--) { + if (j + 1 <= ARRAY_LEN) { + client_array[j + 1] = client_array[j]; + } } - } - for(int j = probe_entry_last; j >= i; j--) - { - if(j + 1 <= ARRAY_LEN) - { - probe_array[j + 1] = probe_array[j]; + client_array[i] = entry; + + if (client_entry_last < ARRAY_LEN) { + client_entry_last++; } - } - probe_array[i] = entry; - - if(probe_entry_last < ARRAY_LEN) - { - probe_entry_last++; - } } -probe_entry* probe_array_delete(probe_entry entry) -{ - int i; - int found_in_array = 0; - probe_entry* tmp = NULL; +client *client_array_delete(client entry) { - if(probe_entry_last == -1) - { - return NULL; - } + int i; + int found_in_array = 0; + client *tmp = NULL; - for(i = 0; i <= probe_entry_last; i++) - { - if(mac_is_equal(entry.bssid_addr, probe_array[i].bssid_addr) && - mac_is_equal(entry.client_addr, probe_array[i].client_addr)) - { - found_in_array = 1; - tmp = &probe_array[i]; - break; + if (client_entry_last == -1) { + return NULL; } - } - for(int j = i; j <= probe_entry_last; j++) - { - probe_array[j] = probe_array[j + 1]; - } - - if(probe_entry_last > -1 && found_in_array) - { - probe_entry_last--; - } - return tmp; -} - -void print_array() -{ - printf("------------------\n"); - printf("Probe Entry Last: %d\n", probe_entry_last); - for(int i = 0; i <= probe_entry_last; i++) - { - print_probe_entry(probe_array[i]); - } - printf("------------------\n"); -} - -void insert_to_array(probe_entry entry, int inc_counter) -{ - pthread_mutex_lock(&probe_array_mutex); - - entry.time = time(0); - entry.counter = 0; - probe_entry* tmp = probe_array_delete(entry); - - if(tmp != NULL) - { - entry.counter = tmp->counter; - } - - if (inc_counter) - { - entry.counter++; - } - - probe_array_insert(entry); - - pthread_mutex_unlock(&probe_array_mutex); -} - -void remove_old_client_entries(time_t current_time, long long int threshold) -{ - for(int i = 0; i < probe_entry_last; i++) - { - if (client_array[i].time < current_time - threshold) - { - client_array_delete(client_array[i]); + for (i = 0; i <= client_entry_last; i++) { + if (mac_is_equal(entry.bssid_addr, client_array[i].bssid_addr) && + mac_is_equal(entry.client_addr, client_array[i].client_addr)) { + found_in_array = 1; + tmp = &client_array[i]; + break; + } } - } + + for (int j = i; j <= client_entry_last; j++) { + client_array[j] = client_array[j + 1]; + } + + if (client_entry_last > -1 && found_in_array) { + client_entry_last--; + } + return tmp; } -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]); + +void probe_array_insert(probe_entry entry) { + if (probe_entry_last == -1) { + probe_array[0] = entry; + probe_entry_last++; + return; + } + + int i; + for (i = 0; i <= probe_entry_last; i++) { + if (!go_next(sort_string, SORT_NUM, entry, probe_array[i])) { + break; + } + } + for (int j = probe_entry_last; j >= i; j--) { + if (j + 1 <= ARRAY_LEN) { + probe_array[j + 1] = probe_array[j]; + } + } + probe_array[i] = entry; + + if (probe_entry_last < ARRAY_LEN) { + probe_entry_last++; } - } } - -void *remove_array_thread(void *arg) { - while (1) { - sleep(TIME_THRESHOLD); + +probe_entry *probe_array_delete(probe_entry entry) { + int i; + int found_in_array = 0; + probe_entry *tmp = NULL; + + if (probe_entry_last == -1) { + return NULL; + } + + for (i = 0; i <= probe_entry_last; i++) { + if (mac_is_equal(entry.bssid_addr, probe_array[i].bssid_addr) && + mac_is_equal(entry.client_addr, probe_array[i].client_addr)) { + found_in_array = 1; + tmp = &probe_array[i]; + break; + } + } + + for (int j = i; j <= probe_entry_last; j++) { + probe_array[j] = probe_array[j + 1]; + } + + if (probe_entry_last > -1 && found_in_array) { + probe_entry_last--; + } + return tmp; +} + +void print_array() { + printf("------------------\n"); + printf("Probe Entry Last: %d\n", probe_entry_last); + for (int i = 0; i <= probe_entry_last; i++) { + print_probe_entry(probe_array[i]); + } + printf("------------------\n"); +} + +void insert_to_array(probe_entry entry, int inc_counter) { pthread_mutex_lock(&probe_array_mutex); - printf("[Thread] : Removing old entries!\n"); - remove_old_probe_entries(time(0), TIME_THRESHOLD); + + entry.time = time(0); + entry.counter = 0; + probe_entry *tmp = probe_array_delete(entry); + + if (tmp != NULL) { + entry.counter = tmp->counter; + } + + if (inc_counter) { + entry.counter++; + } + + probe_array_insert(entry); + pthread_mutex_unlock(&probe_array_mutex); - } - return 0; +} + +void remove_old_client_entries(time_t current_time, long long int threshold) { + for (int i = 0; i < probe_entry_last; i++) { + if (client_array[i].time < current_time - threshold) { + client_array_delete(client_array[i]); + } + } +} + +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]); + } + } +} + +void *remove_array_thread(void *arg) { + while (1) { + sleep(TIME_THRESHOLD); + pthread_mutex_lock(&probe_array_mutex); + printf("[Thread] : Removing old entries!\n"); + remove_old_probe_entries(time(0), TIME_THRESHOLD); + pthread_mutex_unlock(&probe_array_mutex); + } + return 0; } void *remove_client_array_thread(void *arg) { - while (1) { - sleep(TIME_THRESHOLD_CLIENT); + while (1) { + sleep(TIME_THRESHOLD_CLIENT); + pthread_mutex_lock(&client_array_mutex); + printf("[Thread] : Removing old client entries!\n"); + remove_old_client_entries(time(0), TIME_THRESHOLD_CLIENT); + pthread_mutex_unlock(&client_array_mutex); + } + return 0; +} + + +void insert_client_to_array(client entry) { pthread_mutex_lock(&client_array_mutex); - printf("[Thread] : Removing old client entries!\n"); - remove_old_client_entries(time(0), TIME_THRESHOLD_CLIENT); + entry.time = time(0); + + client_array_delete(entry); + client_array_insert(entry); + pthread_mutex_unlock(&client_array_mutex); - } - return 0; } -void insert_client_to_array(client entry) -{ - pthread_mutex_lock(&client_array_mutex); - entry.time = time(0); - - client_array_delete(entry); - client_array_insert(entry); - - pthread_mutex_unlock(&client_array_mutex); -} - - - - - - - - node *delete_probe_req(node **ret_remove, node *head, uint8_t bssid_addr[], uint8_t client_addr[]); + int mac_is_first_in_list(node *head, uint8_t bssid_addr[], uint8_t client_addr[]); + node *remove_node(node *head, node *curr, node *prev); + node *remove_old_entries(node *head, time_t current_time, long long int threshold); + void print_list_with_head(node *head); void insert_to_list(probe_entry entry, int inc_counter) { - pthread_mutex_lock(&list_mutex); + pthread_mutex_lock(&list_mutex); - entry.time = time(0); - entry.counter = 0; + entry.time = time(0); + entry.counter = 0; - - // first delete probe request - // probe_list_head = remove_old_entries(probe_list_head, time(0), - // TIME_THRESHOLD); - node *tmp_probe_req = NULL; - probe_list_head = delete_probe_req(&tmp_probe_req, probe_list_head, - entry.bssid_addr, entry.client_addr); - if (tmp_probe_req) { - // local ubus - tmp_probe_req->data.signal = entry.signal; - tmp_probe_req->data.time = entry.time; - if (inc_counter) { - // when network don't increase counter... - tmp_probe_req->data.counter++; + // first delete probe request + // probe_list_head = remove_old_entries(probe_list_head, time(0), + // TIME_THRESHOLD); + node *tmp_probe_req = NULL; + probe_list_head = delete_probe_req(&tmp_probe_req, probe_list_head, + entry.bssid_addr, entry.client_addr); + + if (tmp_probe_req) { + // local ubus + tmp_probe_req->data.signal = entry.signal; + tmp_probe_req->data.time = entry.time; + if (inc_counter) { + // when network don't increase counter... + tmp_probe_req->data.counter++; + } + + // is this correct? + probe_list_head = insert(probe_list_head, tmp_probe_req->data); + free(tmp_probe_req); + } else { + printf("New entry!\n"); + probe_list_head = insert(probe_list_head, entry); } - // is this correct? - probe_list_head = insert(probe_list_head, tmp_probe_req->data); - free(tmp_probe_req); - } else { - printf("New entry!\n"); - probe_list_head = insert(probe_list_head, entry); - } - - pthread_mutex_unlock(&list_mutex); + pthread_mutex_unlock(&list_mutex); } int go_next_help(char sort_order[], int i, probe_entry entry, probe_entry next_entry) { - switch (sort_order[i]) { - // bssid-mac - case 'b': - return mac_is_greater(entry.bssid_addr, next_entry.bssid_addr) && - mac_is_equal(entry.client_addr, next_entry.client_addr); - break; + switch (sort_order[i]) { + // bssid-mac + case 'b': + return mac_is_greater(entry.bssid_addr, next_entry.bssid_addr) && + mac_is_equal(entry.client_addr, next_entry.client_addr); + break; - // client-mac - case 'c': - return mac_is_greater(entry.client_addr, next_entry.client_addr); - break; + // client-mac + case 'c': + return mac_is_greater(entry.client_addr, next_entry.client_addr); + break; - // frequency - // mac is 5 ghz or 2.4 ghz? - case 'f': - return //entry.freq < next_entry.freq && - entry.freq < 5000 && - next_entry.freq >= 5000 && - //entry.freq < 5 && - mac_is_equal(entry.client_addr, next_entry.client_addr); - break; + // frequency + // mac is 5 ghz or 2.4 ghz? + case 'f': + return //entry.freq < next_entry.freq && + entry.freq < 5000 && + next_entry.freq >= 5000 && + //entry.freq < 5 && + mac_is_equal(entry.client_addr, next_entry.client_addr); + break; - // signal strength (RSSI) - case 's': - return entry.signal < next_entry.signal && - mac_is_equal(entry.client_addr, next_entry.client_addr); - break; + // signal strength (RSSI) + case 's': + return entry.signal < next_entry.signal && + mac_is_equal(entry.client_addr, next_entry.client_addr); + break; - default: - return 0; - break; - } + default: + return 0; + break; + } } int go_next(char sort_order[], int i, probe_entry entry, probe_entry next_entry) { - int conditions = 1; - for (int j = 0; j < i; j++) { - i &= !(go_next(sort_order, j, entry, next_entry)); - } - return conditions && go_next_help(sort_order, i, entry, next_entry); + int conditions = 1; + for (int j = 0; j < i; j++) { + i &= !(go_next(sort_order, j, entry, next_entry)); + } + return conditions && go_next_help(sort_order, i, entry, next_entry); } node *insert(node *head, probe_entry entry) { - node *temp, *prev, *next; - temp = (node *)malloc(sizeof(node)); - temp->data = entry; - temp->ptr = NULL; + node *temp, *prev, *next; + temp = (node *) malloc(sizeof(node)); + temp->data = entry; + temp->ptr = NULL; - // length of sorting string - // char sort_string[] = "cfsb"; - int i = 0; + // length of sorting string + // char sort_string[] = "cfsb"; + int i = 0; - if (!head) { - head = temp; - } else { - prev = NULL; - next = head; - while (next) { - if (go_next(sort_string, i, entry, next->data)) { - prev = next; - next = next->ptr; - } else if (i < strlen(sort_string)) { - i++; - } else { - break; - } - } - if (!next) { - prev->ptr = temp; - } else { - if (prev) { - temp->ptr = prev->ptr; - prev->ptr = temp; - } else { - temp->ptr = head; + if (!head) { head = temp; - } + } else { + prev = NULL; + next = head; + while (next) { + if (go_next(sort_string, i, entry, next->data)) { + prev = next; + next = next->ptr; + } else if (i < strlen(sort_string)) { + i++; + } else { + break; + } + } + if (!next) { + prev->ptr = temp; + } else { + if (prev) { + temp->ptr = prev->ptr; + prev->ptr = temp; + } else { + temp->ptr = head; + head = temp; + } + } } - } - return head; + return head; } node *delete_probe_req(node **ret_remove, node *head, uint8_t bssid_addr[], uint8_t client_addr[]) { - if (!head) { - return head; - } - - if (mac_is_equal(client_addr, head->data.client_addr) && - mac_is_equal(bssid_addr, head->data.bssid_addr)) { - node *temp = head; - head = head->ptr; - *ret_remove = temp; - // don't free pointer - // free(temp); - return head; - } - - node *prev = NULL; - node *next = head; - while (next) { - if (mac_is_greater(next->data.client_addr, client_addr)) { - break; + if (!head) { + return head; } - if (mac_is_equal(client_addr, next->data.client_addr) && - mac_is_equal(bssid_addr, next->data.bssid_addr)) { - node *temp = next; - prev->ptr = next->ptr; - // free(temp); - *ret_remove = temp; - return head; + if (mac_is_equal(client_addr, head->data.client_addr) && + mac_is_equal(bssid_addr, head->data.bssid_addr)) { + node *temp = head; + head = head->ptr; + *ret_remove = temp; + // don't free pointer + // free(temp); + return head; } - prev = next; - next = next->ptr; - } - return head; + + node *prev = NULL; + node *next = head; + while (next) { + if (mac_is_greater(next->data.client_addr, client_addr)) { + break; + } + + if (mac_is_equal(client_addr, next->data.client_addr) && + mac_is_equal(bssid_addr, next->data.bssid_addr)) { + node *temp = next; + prev->ptr = next->ptr; + // free(temp); + *ret_remove = temp; + return head; + } + prev = next; + next = next->ptr; + } + return head; } void *remove_thread(void *arg) { - while (1) { - sleep(TIME_THRESHOLD); - pthread_mutex_lock(&list_mutex); - printf("[Thread] : Removing old entries!\n"); - probe_list_head = - remove_old_entries(probe_list_head, time(0), TIME_THRESHOLD); - pthread_mutex_unlock(&list_mutex); - // print_list(); - } - return 0; + while (1) { + sleep(TIME_THRESHOLD); + pthread_mutex_lock(&list_mutex); + printf("[Thread] : Removing old entries!\n"); + probe_list_head = + remove_old_entries(probe_list_head, time(0), TIME_THRESHOLD); + pthread_mutex_unlock(&list_mutex); + // print_list(); + } + return 0; } node *remove_old_entries(node *head, time_t current_time, long long int threshold) { - if (head) { - node *prev = NULL; - node *next = head; - while (next) { - if (next->data.time < current_time - threshold) { - head = remove_node(head, next, prev); - // print_list_with_head(head); - if (prev == NULL) // removed head - { - next = head; - } else { - next = prev->ptr; + if (head) { + node *prev = NULL; + node *next = head; + while (next) { + if (next->data.time < current_time - threshold) { + head = remove_node(head, next, prev); + // print_list_with_head(head); + if (prev == NULL) // removed head + { + next = head; + } else { + next = prev->ptr; + } + } else { + prev = next; + next = next->ptr; + } } - } else { - prev = next; - next = next->ptr; - } } - } - return head; + return head; } // return headpointer node *remove_node(node *head, node *curr, node *prev) { - if (curr == head) { - node *temp = head; - head = head->ptr; - free(temp); - } else { - node *temp = curr; - prev->ptr = curr->ptr; - free(temp); - } - // printf("Removed old entry!\n"); - return head; + if (curr == head) { + node *temp = head; + head = head->ptr; + free(temp); + } else { + node *temp = curr; + prev->ptr = curr->ptr; + free(temp); + } + // printf("Removed old entry!\n"); + return head; } int mac_is_first_in_list(node *head, uint8_t bssid_addr[], uint8_t client_addr[]) { - if (!head) { - return 1; - } - node *next = head; - while (next) { - if (mac_is_greater(next->data.client_addr, client_addr)) { - break; + if (!head) { + return 1; } + node *next = head; + while (next) { + if (mac_is_greater(next->data.client_addr, client_addr)) { + break; + } - if (mac_is_equal(client_addr, next->data.client_addr)) { - print_probe_entry(next->data); - return mac_is_equal(bssid_addr, next->data.bssid_addr); + if (mac_is_equal(client_addr, next->data.client_addr)) { + print_probe_entry(next->data); + return mac_is_equal(bssid_addr, next->data.bssid_addr); + } + next = next->ptr; } - next = next->ptr; - } - return 0; + return 0; } int mac_first_in_probe_list(uint8_t bssid_addr[], uint8_t client_addr[]) { - pthread_mutex_lock(&list_mutex); - int ret = mac_is_first_in_list(probe_list_head, bssid_addr, client_addr); - pthread_mutex_unlock(&list_mutex); - return ret; + pthread_mutex_lock(&list_mutex); + int ret = mac_is_first_in_list(probe_list_head, bssid_addr, client_addr); + pthread_mutex_unlock(&list_mutex); + return ret; } void free_list(node *head) { - node *prev = head; - node *cur = head; - while (cur) { - prev = cur; - cur = prev->ptr; - free(prev); - } + node *prev = head; + node *cur = head; + while (cur) { + prev = cur; + cur = prev->ptr; + free(prev); + } } int mac_is_equal(uint8_t addr1[], uint8_t addr2[]) { - return memcmp(addr1, addr2, ETH_ALEN * sizeof(uint8_t)) == 0; + return memcmp(addr1, addr2, ETH_ALEN * sizeof(uint8_t)) == 0; } int mac_is_greater(uint8_t addr1[], uint8_t addr2[]) { - for (int i = 0; i < ETH_ALEN; i++) { - if (addr1[i] > addr2[i]) { - return 1; - } - if (addr1[i] < addr2[i]) { - return 0; - } + for (int i = 0; i < ETH_ALEN; i++) { + if (addr1[i] > addr2[i]) { + return 1; + } + if (addr1[i] < addr2[i]) { + return 0; + } - // if equal continue... - } - return 0; + // if equal continue... + } + return 0; } void print_list_with_head(node *head) { - pthread_mutex_lock(&list_mutex); - printf("------------------\n"); - if (head) { - node *next; - next = head; - while (next) { - print_probe_entry(next->data); - next = next->ptr; + pthread_mutex_lock(&list_mutex); + printf("------------------\n"); + if (head) { + node *next; + next = head; + while (next) { + print_probe_entry(next->data); + next = next->ptr; + } } - } - printf("------------------\n"); - pthread_mutex_unlock(&list_mutex); + printf("------------------\n"); + pthread_mutex_unlock(&list_mutex); } void print_list() { - pthread_mutex_lock(&list_mutex); - printf("------------------\n"); - node *head = probe_list_head; - if (head) { - node *next; - next = head; - while (next) { - print_probe_entry(next->data); - next = next->ptr; + pthread_mutex_lock(&list_mutex); + printf("------------------\n"); + node *head = probe_list_head; + if (head) { + node *next; + next = head; + while (next) { + print_probe_entry(next->data); + next = next->ptr; + } } - } - printf("------------------\n"); - pthread_mutex_unlock(&list_mutex); + printf("------------------\n"); + pthread_mutex_unlock(&list_mutex); } void print_probe_entry(probe_entry entry) { - char mac_buf_ap[20]; - char mac_buf_client[20]; - char mac_buf_target[20]; + char mac_buf_ap[20]; + char mac_buf_client[20]; + char mac_buf_target[20]; - sprintf(mac_buf_ap, "%x:%x:%x:%x:%x:%x", MAC2STR(entry.bssid_addr)); - sprintf(mac_buf_client, "%x:%x:%x:%x:%x:%x", MAC2STR(entry.client_addr)); - sprintf(mac_buf_target, "%x:%x:%x:%x:%x:%x", MAC2STR(entry.target_addr)); + sprintf(mac_buf_ap, "%x:%x:%x:%x:%x:%x", MAC2STR(entry.bssid_addr)); + sprintf(mac_buf_client, "%x:%x:%x:%x:%x:%x", MAC2STR(entry.client_addr)); + sprintf(mac_buf_target, "%x:%x:%x:%x:%x:%x", MAC2STR(entry.target_addr)); - printf( - "bssid_addr: %s, client_addr: %s, target_addr: %s, signal: %d, freq: " - "%d, ht: %d, vht: %d, counter: %d\n", - mac_buf_ap, mac_buf_client, mac_buf_target, entry.signal, entry.freq, entry.ht_support, entry.vht_support, - entry.counter); + printf( + "bssid_addr: %s, client_addr: %s, target_addr: %s, signal: %d, freq: " + "%d, ht: %d, vht: %d, counter: %d\n", + mac_buf_ap, mac_buf_client, mac_buf_target, entry.signal, entry.freq, entry.ht_support, entry.vht_support, + entry.counter); } void print_client_entry(client entry) { - char mac_buf_ap[20]; - char mac_buf_client[20]; + char mac_buf_ap[20]; + char mac_buf_client[20]; - sprintf(mac_buf_ap, "%x:%x:%x:%x:%x:%x", MAC2STR(entry.bssid_addr)); - sprintf(mac_buf_client, "%x:%x:%x:%x:%x:%x", MAC2STR(entry.client_addr)); + sprintf(mac_buf_ap, "%x:%x:%x:%x:%x:%x", MAC2STR(entry.bssid_addr)); + sprintf(mac_buf_client, "%x:%x:%x:%x:%x:%x", MAC2STR(entry.client_addr)); - printf("bssid_addr: %s, client_addr: %s, freq: %d, ht_supported: %d, vht_supported: %d, ht: %d, vht: %d\n", - mac_buf_ap, mac_buf_client, entry.freq, entry.ht_supported, entry.vht_supported, entry.ht, entry.vht); + printf("bssid_addr: %s, client_addr: %s, freq: %d, ht_supported: %d, vht_supported: %d, ht: %d, vht: %d\n", + mac_buf_ap, mac_buf_client, entry.freq, entry.ht_supported, entry.vht_supported, entry.ht, entry.vht); } void print_client_array() { - printf("--------Clients------\n"); - printf("Client Entry Last: %d\n", client_entry_last); - for(int i = 0; i <= client_entry_last; i++) - { - print_client_entry(client_array[i]); - } - printf("------------------\n"); + printf("--------Clients------\n"); + printf("Client Entry Last: %d\n", client_entry_last); + for (int i = 0; i <= client_entry_last; i++) { + print_client_entry(client_array[i]); + } + printf("------------------\n"); } \ No newline at end of file diff --git a/src/utils/ubus.c b/src/utils/ubus.c index bc95f9b..b924b0b 100644 --- a/src/utils/ubus.c +++ b/src/utils/ubus.c @@ -18,81 +18,86 @@ static struct ubus_subscriber hostapd_event; static struct blob_buf b; enum { - PROB_BSSID_ADDR, - PROB_CLIENT_ADDR, - PROB_TARGET_ADDR, - PROB_SIGNAL, - PROB_FREQ, - PROB_HT_SUPPORT, - PROB_VHT_SUPPORT, - __PROB_MAX, + PROB_BSSID_ADDR, + PROB_CLIENT_ADDR, + PROB_TARGET_ADDR, + PROB_SIGNAL, + PROB_FREQ, + PROB_HT_SUPPORT, + PROB_VHT_SUPPORT, + __PROB_MAX, }; static const struct blobmsg_policy prob_policy[__PROB_MAX] = { - [PROB_BSSID_ADDR] = {.name = "bssid", .type = BLOBMSG_TYPE_STRING}, - [PROB_CLIENT_ADDR] = {.name = "address", .type = BLOBMSG_TYPE_STRING}, - [PROB_TARGET_ADDR] = {.name = "target", .type = BLOBMSG_TYPE_STRING}, - [PROB_SIGNAL] = {.name = "signal", .type = BLOBMSG_TYPE_INT32}, - [PROB_FREQ] = {.name = "freq", .type = BLOBMSG_TYPE_INT32}, - [PROB_HT_SUPPORT] = {.name = "ht_support", .type = BLOBMSG_TYPE_INT8}, - [PROB_VHT_SUPPORT] = {.name = "vht_support", .type = BLOBMSG_TYPE_INT8}, + [PROB_BSSID_ADDR] = {.name = "bssid", .type = BLOBMSG_TYPE_STRING}, + [PROB_CLIENT_ADDR] = {.name = "address", .type = BLOBMSG_TYPE_STRING}, + [PROB_TARGET_ADDR] = {.name = "target", .type = BLOBMSG_TYPE_STRING}, + [PROB_SIGNAL] = {.name = "signal", .type = BLOBMSG_TYPE_INT32}, + [PROB_FREQ] = {.name = "freq", .type = BLOBMSG_TYPE_INT32}, + [PROB_HT_SUPPORT] = {.name = "ht_support", .type = BLOBMSG_TYPE_INT8}, + [PROB_VHT_SUPPORT] = {.name = "vht_support", .type = BLOBMSG_TYPE_INT8}, }; enum { - CLIENT_TABLE, - CLIENT_TABLE_BSSID, - CLIENT_TABLE_FREQ, - CLIENT_TABLE_HT, - CLIENT_TABLE_VHT, - __CLIENT_TABLE_MAX, + CLIENT_TABLE, + CLIENT_TABLE_BSSID, + CLIENT_TABLE_FREQ, + CLIENT_TABLE_HT, + CLIENT_TABLE_VHT, + __CLIENT_TABLE_MAX, }; static const struct blobmsg_policy client_table_policy[__CLIENT_TABLE_MAX] = { - [CLIENT_TABLE] = {.name = "clients", .type = BLOBMSG_TYPE_TABLE}, - [CLIENT_TABLE_BSSID] = {.name = "bssid", .type = BLOBMSG_TYPE_STRING}, - [CLIENT_TABLE_FREQ] = {.name = "freq", .type = BLOBMSG_TYPE_INT32}, - [CLIENT_TABLE_HT] = {.name = "ht_supported", .type = BLOBMSG_TYPE_INT8}, - [CLIENT_TABLE_VHT] = {.name = "vht_supported", .type = BLOBMSG_TYPE_INT8}, + [CLIENT_TABLE] = {.name = "clients", .type = BLOBMSG_TYPE_TABLE}, + [CLIENT_TABLE_BSSID] = {.name = "bssid", .type = BLOBMSG_TYPE_STRING}, + [CLIENT_TABLE_FREQ] = {.name = "freq", .type = BLOBMSG_TYPE_INT32}, + [CLIENT_TABLE_HT] = {.name = "ht_supported", .type = BLOBMSG_TYPE_INT8}, + [CLIENT_TABLE_VHT] = {.name = "vht_supported", .type = BLOBMSG_TYPE_INT8}, }; enum { - CLIENT_AUTH, - CLIENT_ASSOC, - CLIENT_AUTHORIZED, - CLIENT_PREAUTH, - CLIENT_WDS, - CLIENT_WMM, - CLIENT_HT, - CLIENT_VHT, - CLIENT_WPS, - CLIENT_MFP, - CLIENT_AID, - __CLIENT_MAX, + CLIENT_AUTH, + CLIENT_ASSOC, + CLIENT_AUTHORIZED, + CLIENT_PREAUTH, + CLIENT_WDS, + CLIENT_WMM, + CLIENT_HT, + CLIENT_VHT, + CLIENT_WPS, + CLIENT_MFP, + CLIENT_AID, + __CLIENT_MAX, }; static const struct blobmsg_policy client_policy[__CLIENT_MAX] = { - [CLIENT_AUTH] = {.name = "auth", .type = BLOBMSG_TYPE_INT8}, - [CLIENT_ASSOC] = {.name = "assoc", .type = BLOBMSG_TYPE_INT8}, - [CLIENT_AUTHORIZED] = {.name = "authorized", .type = BLOBMSG_TYPE_INT8}, - [CLIENT_PREAUTH] = {.name = "preauth", .type = BLOBMSG_TYPE_INT8}, - [CLIENT_WDS] = {.name = "wds", .type = BLOBMSG_TYPE_INT8}, - [CLIENT_WMM] = {.name = "wmm", .type = BLOBMSG_TYPE_INT8}, - [CLIENT_HT] = {.name = "ht", .type = BLOBMSG_TYPE_INT8}, - [CLIENT_VHT] = {.name = "vht", .type = BLOBMSG_TYPE_INT8}, - [CLIENT_WPS] = {.name = "wps", .type = BLOBMSG_TYPE_INT8}, - [CLIENT_MFP] = {.name = "mfp", .type = BLOBMSG_TYPE_INT8}, - [CLIENT_AID] = {.name = "aid", .type = BLOBMSG_TYPE_INT32}, + [CLIENT_AUTH] = {.name = "auth", .type = BLOBMSG_TYPE_INT8}, + [CLIENT_ASSOC] = {.name = "assoc", .type = BLOBMSG_TYPE_INT8}, + [CLIENT_AUTHORIZED] = {.name = "authorized", .type = BLOBMSG_TYPE_INT8}, + [CLIENT_PREAUTH] = {.name = "preauth", .type = BLOBMSG_TYPE_INT8}, + [CLIENT_WDS] = {.name = "wds", .type = BLOBMSG_TYPE_INT8}, + [CLIENT_WMM] = {.name = "wmm", .type = BLOBMSG_TYPE_INT8}, + [CLIENT_HT] = {.name = "ht", .type = BLOBMSG_TYPE_INT8}, + [CLIENT_VHT] = {.name = "vht", .type = BLOBMSG_TYPE_INT8}, + [CLIENT_WPS] = {.name = "wps", .type = BLOBMSG_TYPE_INT8}, + [CLIENT_MFP] = {.name = "mfp", .type = BLOBMSG_TYPE_INT8}, + [CLIENT_AID] = {.name = "aid", .type = BLOBMSG_TYPE_INT32}, }; /* Function Definitions */ static void hostapd_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s, uint32_t id); + static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg); + static int add_subscriber(char *name); + static int subscribe_to_hostapd_interfaces(char *hostapd_dir); + static int ubus_get_clients(); + /* static int decide_function(probe_entry *prob_req) { // TODO: Refactor... @@ -112,338 +117,325 @@ static int decide_function(probe_entry *prob_req) { */ static void hostapd_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s, uint32_t id) { - fprintf(stderr, "Object %08x went away\n", id); + fprintf(stderr, "Object %08x went away\n", id); } int parse_to_probe_req(struct blob_attr *msg, probe_entry *prob_req) { - struct blob_attr *tb[__PROB_MAX]; - - blobmsg_parse(prob_policy, __PROB_MAX, tb, blob_data(msg), blob_len(msg)); + struct blob_attr *tb[__PROB_MAX]; - if (hwaddr_aton(blobmsg_data(tb[PROB_BSSID_ADDR]), prob_req->bssid_addr)) - return UBUS_STATUS_INVALID_ARGUMENT; + blobmsg_parse(prob_policy, __PROB_MAX, tb, blob_data(msg), blob_len(msg)); - if (hwaddr_aton(blobmsg_data(tb[PROB_CLIENT_ADDR]), prob_req->client_addr)) - return UBUS_STATUS_INVALID_ARGUMENT; + if (hwaddr_aton(blobmsg_data(tb[PROB_BSSID_ADDR]), prob_req->bssid_addr)) + return UBUS_STATUS_INVALID_ARGUMENT; - if (hwaddr_aton(blobmsg_data(tb[PROB_TARGET_ADDR]), prob_req->target_addr)) - return UBUS_STATUS_INVALID_ARGUMENT; + if (hwaddr_aton(blobmsg_data(tb[PROB_CLIENT_ADDR]), prob_req->client_addr)) + return UBUS_STATUS_INVALID_ARGUMENT; - if (tb[PROB_SIGNAL]) { - prob_req->signal = blobmsg_get_u32(tb[PROB_SIGNAL]); - } + if (hwaddr_aton(blobmsg_data(tb[PROB_TARGET_ADDR]), prob_req->target_addr)) + return UBUS_STATUS_INVALID_ARGUMENT; - if (tb[PROB_FREQ]) { - prob_req->freq = blobmsg_get_u32(tb[PROB_FREQ]); - } + if (tb[PROB_SIGNAL]) { + prob_req->signal = blobmsg_get_u32(tb[PROB_SIGNAL]); + } - if (tb[PROB_HT_SUPPORT]) { - prob_req->ht_support = blobmsg_get_u8(tb[PROB_HT_SUPPORT]); - } + if (tb[PROB_FREQ]) { + prob_req->freq = blobmsg_get_u32(tb[PROB_FREQ]); + } - if (tb[PROB_VHT_SUPPORT]) { - prob_req->vht_support = blobmsg_get_u8(tb[PROB_VHT_SUPPORT]); - } + if (tb[PROB_HT_SUPPORT]) { + prob_req->ht_support = blobmsg_get_u8(tb[PROB_HT_SUPPORT]); + } - return 0; + if (tb[PROB_VHT_SUPPORT]) { + prob_req->vht_support = blobmsg_get_u8(tb[PROB_VHT_SUPPORT]); + } + + return 0; } static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { - probe_entry prob_req; - parse_to_probe_req(msg, &prob_req); - //insert_to_list(prob_req, 1); - insert_to_array(prob_req, 1); + probe_entry prob_req; + parse_to_probe_req(msg, &prob_req); + //insert_to_list(prob_req, 1); + insert_to_array(prob_req, 1); - // send probe via network - char *str; - str = blobmsg_format_json(msg, true); - send_string(str); + // send probe via network + char *str; + str = blobmsg_format_json(msg, true); + send_string(str); - printf("[WC] Hostapd-Probe: %s : %s\n", method, str); + printf("[WC] Hostapd-Probe: %s : %s\n", method, str); - print_array(); + print_array(); - // sleep(2); // sleep for 2s + // sleep(2); // sleep for 2s - // deny access - //if (!decide_function(&prob_req)) { - // return UBUS_STATUS_UNKNOWN_ERROR; - //} + // deny access + //if (!decide_function(&prob_req)) { + // return UBUS_STATUS_UNKNOWN_ERROR; + //} - // allow access - return 0; + // allow access + return 0; } static int add_subscriber(char *name) { - uint32_t id = 0; + uint32_t id = 0; - if (ubus_lookup_id(ctx, name, &id)) { - fprintf(stderr, "Failed to look up test object for %s\n", name); - return -1; - } + if (ubus_lookup_id(ctx, name, &id)) { + fprintf(stderr, "Failed to look up test object for %s\n", name); + return -1; + } - // add callbacks - hostapd_event.remove_cb = hostapd_handle_remove; - hostapd_event.cb = hostapd_notify; + // add callbacks + hostapd_event.remove_cb = hostapd_handle_remove; + hostapd_event.cb = hostapd_notify; - int ret = ubus_subscribe(ctx, &hostapd_event, id); + int ret = ubus_subscribe(ctx, &hostapd_event, id); - fprintf(stderr, "Watching object %08x: %s\n", id, ubus_strerror(ret)); + fprintf(stderr, "Watching object %08x: %s\n", id, ubus_strerror(ret)); - return 0; + return 0; } static int subscribe_to_hostapd_interfaces(char *hostapd_dir) { - DIR *dirp; - struct dirent *entry; + DIR *dirp; + struct dirent *entry; - int ret = ubus_register_subscriber(ctx, &hostapd_event); - if (ret) { - fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret)); - return -1; - } - - dirp = opendir(hostapd_dir); // error handling? - while ((entry = readdir(dirp)) != NULL) { - if (entry->d_type == DT_SOCK) { - char subscribe_name[256]; - sprintf(subscribe_name, "hostapd.%s", entry->d_name); - printf("Subscribing to %s\n", subscribe_name); - add_subscriber(subscribe_name); + int ret = ubus_register_subscriber(ctx, &hostapd_event); + if (ret) { + fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret)); + return -1; } - } - // free(hostapd_dir); // free string - return 0; + + dirp = opendir(hostapd_dir); // error handling? + while ((entry = readdir(dirp)) != NULL) { + if (entry->d_type == DT_SOCK) { + char subscribe_name[256]; + sprintf(subscribe_name, "hostapd.%s", entry->d_name); + printf("Subscribing to %s\n", subscribe_name); + add_subscriber(subscribe_name); + } + } + // free(hostapd_dir); // free string + return 0; } int dawn_init_ubus(const char *ubus_socket, char *hostapd_dir) { - uloop_init(); - signal(SIGPIPE, SIG_IGN); + uloop_init(); + signal(SIGPIPE, SIG_IGN); - ctx = ubus_connect(ubus_socket); - if (!ctx) { - fprintf(stderr, "Failed to connect to ubus\n"); - return -1; - } else { - printf("Connected to ubus\n"); - } + ctx = ubus_connect(ubus_socket); + if (!ctx) { + fprintf(stderr, "Failed to connect to ubus\n"); + return -1; + } else { + printf("Connected to ubus\n"); + } - ubus_add_uloop(ctx); + ubus_add_uloop(ctx); - subscribe_to_hostapd_interfaces(hostapd_dir); + subscribe_to_hostapd_interfaces(hostapd_dir); - ubus_get_clients(); -/* - printf("Deleting Client\n"); - //"78:02:f8:bc:ac:0b" + ubus_get_clients(); - int tmp_int_mac[ETH_ALEN]; - uint8_t tmp_mac[ETH_ALEN]; - sscanf("78:02:f8:bc:ac:0b", "%x:%x:%x:%x:%x:%x", STR2MAC(tmp_int_mac)); - for(int i = 0; i < ETH_ALEN; ++i ) - tmp_mac[i] = (uint8_t) tmp_int_mac[i]; - del_client(tmp_mac, 5, 1, 60000); -*/ - uloop_run(); + uloop_run(); - close_socket(); + close_socket(); - ubus_free(ctx); - uloop_done(); - return 0; + ubus_free(ctx); + uloop_done(); + return 0; } static void -dump_client(struct blob_attr **tb, uint8_t client_addr[], const char* bssid_addr, uint32_t freq, uint8_t ht_supported, uint8_t vht_supported) -{ - client client_entry; +dump_client(struct blob_attr **tb, uint8_t client_addr[], const char *bssid_addr, uint32_t freq, uint8_t ht_supported, + uint8_t vht_supported) { + client client_entry; - hwaddr_aton(bssid_addr, client_entry.bssid_addr); + hwaddr_aton(bssid_addr, client_entry.bssid_addr); - memcpy(client_entry.client_addr, client_addr, ETH_ALEN * sizeof(uint8_t)); + memcpy(client_entry.client_addr, client_addr, ETH_ALEN * sizeof(uint8_t)); - //char mac_buf_client[20]; - //char mac_buf_ap[20]; + //char mac_buf_client[20]; + //char mac_buf_ap[20]; - //sprintf(mac_buf_ap, "%x:%x:%x:%x:%x:%x", MAC2STR(client_entry.bssid_addr)); - //sprintf(mac_buf_client, "%x:%x:%x:%x:%x:%x", MAC2STR(client_entry.client_addr)); - //printf("Frequency is: %d\n",freq); - client_entry.freq = freq; + //sprintf(mac_buf_ap, "%x:%x:%x:%x:%x:%x", MAC2STR(client_entry.bssid_addr)); + //sprintf(mac_buf_client, "%x:%x:%x:%x:%x:%x", MAC2STR(client_entry.client_addr)); + //printf("Frequency is: %d\n",freq); + client_entry.freq = freq; - client_entry.ht_supported = ht_supported; - client_entry.vht_supported = vht_supported; + client_entry.ht_supported = ht_supported; + client_entry.vht_supported = vht_supported; - if (tb[CLIENT_AUTH]) { - client_entry.auth = blobmsg_get_u8(tb[CLIENT_AUTH]); - } - if (tb[CLIENT_ASSOC]) { - client_entry.assoc = blobmsg_get_u8(tb[CLIENT_ASSOC]); - } - if(tb[CLIENT_AUTHORIZED]) - { - client_entry.authorized = blobmsg_get_u8(tb[CLIENT_AUTHORIZED]); - } - if(tb[CLIENT_PREAUTH]){ - client_entry.preauth = blobmsg_get_u8(tb[CLIENT_PREAUTH]); - } - if(tb[CLIENT_WDS]){ - client_entry.wds = blobmsg_get_u8(tb[CLIENT_WDS]); - } - if(tb[CLIENT_WMM]){ - client_entry.wmm = blobmsg_get_u8(tb[CLIENT_WMM]); - } - if(tb[CLIENT_HT]){ - client_entry.ht = blobmsg_get_u8(tb[CLIENT_HT]); - } - if(tb[CLIENT_VHT]){ - client_entry.vht = blobmsg_get_u8(tb[CLIENT_VHT]); - } - if(tb[CLIENT_WPS]){ - client_entry.wps = blobmsg_get_u8(tb[CLIENT_WPS]); - } - if(tb[CLIENT_MFP]){ - client_entry.mfp = blobmsg_get_u8(tb[CLIENT_MFP]); - } - if(tb[CLIENT_AID]){ - client_entry.aid = blobmsg_get_u32(tb[CLIENT_AID]); - } + if (tb[CLIENT_AUTH]) { + client_entry.auth = blobmsg_get_u8(tb[CLIENT_AUTH]); + } + if (tb[CLIENT_ASSOC]) { + client_entry.assoc = blobmsg_get_u8(tb[CLIENT_ASSOC]); + } + if (tb[CLIENT_AUTHORIZED]) { + client_entry.authorized = blobmsg_get_u8(tb[CLIENT_AUTHORIZED]); + } + if (tb[CLIENT_PREAUTH]) { + client_entry.preauth = blobmsg_get_u8(tb[CLIENT_PREAUTH]); + } + if (tb[CLIENT_WDS]) { + client_entry.wds = blobmsg_get_u8(tb[CLIENT_WDS]); + } + if (tb[CLIENT_WMM]) { + client_entry.wmm = blobmsg_get_u8(tb[CLIENT_WMM]); + } + if (tb[CLIENT_HT]) { + client_entry.ht = blobmsg_get_u8(tb[CLIENT_HT]); + } + if (tb[CLIENT_VHT]) { + client_entry.vht = blobmsg_get_u8(tb[CLIENT_VHT]); + } + if (tb[CLIENT_WPS]) { + client_entry.wps = blobmsg_get_u8(tb[CLIENT_WPS]); + } + if (tb[CLIENT_MFP]) { + client_entry.mfp = blobmsg_get_u8(tb[CLIENT_MFP]); + } + if (tb[CLIENT_AID]) { + client_entry.aid = blobmsg_get_u32(tb[CLIENT_AID]); + } - insert_client_to_array(client_entry); + insert_client_to_array(client_entry); } static void -dump_client_table(struct blob_attr *head, int len, const char* bssid_addr, uint32_t freq, uint8_t ht_supported, uint8_t vht_supported) -{ - struct blob_attr *attr; - struct blobmsg_hdr *hdr; +dump_client_table(struct blob_attr *head, int len, const char *bssid_addr, uint32_t freq, uint8_t ht_supported, + uint8_t vht_supported) { + struct blob_attr *attr; + struct blobmsg_hdr *hdr; - __blob_for_each_attr(attr, head, len) { - hdr = blob_data(attr); + __blob_for_each_attr(attr, head, len) + { + hdr = blob_data(attr); - struct blob_attr *tb[__CLIENT_MAX]; - blobmsg_parse(client_policy, __CLIENT_MAX, tb, blobmsg_data(attr), blobmsg_len(attr)); - //char* str = blobmsg_format_json_indent(attr, true, -1); + struct blob_attr *tb[__CLIENT_MAX]; + blobmsg_parse(client_policy, __CLIENT_MAX, tb, blobmsg_data(attr), blobmsg_len(attr)); + //char* str = blobmsg_format_json_indent(attr, true, -1); - int tmp_int_mac[ETH_ALEN]; - uint8_t tmp_mac[ETH_ALEN]; - sscanf((char*)hdr->name, "%x:%x:%x:%x:%x:%x", STR2MAC(tmp_int_mac)); - for(int i = 0; i < ETH_ALEN; ++i ) - tmp_mac[i] = (uint8_t) tmp_int_mac[i]; + int tmp_int_mac[ETH_ALEN]; + uint8_t tmp_mac[ETH_ALEN]; + sscanf((char *) hdr->name, "%x:%x:%x:%x:%x:%x", STR2MAC(tmp_int_mac)); + for (int i = 0; i < ETH_ALEN; ++i) + tmp_mac[i] = (uint8_t) tmp_int_mac[i]; - dump_client(tb, tmp_mac, bssid_addr, freq, ht_supported, vht_supported); - } + dump_client(tb, tmp_mac, bssid_addr, freq, ht_supported, vht_supported); + } } int parse_to_clients(struct blob_attr *msg) { - struct blob_attr *tb[__CLIENT_TABLE_MAX]; + struct blob_attr *tb[__CLIENT_TABLE_MAX]; - blobmsg_parse(client_table_policy, __CLIENT_TABLE_MAX, tb, blob_data(msg), blob_len(msg)); + 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] && tb[CLIENT_TABLE_VHT]) { - dump_client_table(blobmsg_data(tb[CLIENT_TABLE]), blobmsg_data_len(tb[CLIENT_TABLE]), blobmsg_data(tb[CLIENT_TABLE_BSSID]), blobmsg_get_u32(tb[CLIENT_TABLE_FREQ]), blobmsg_get_u8(tb[CLIENT_TABLE_HT]), blobmsg_get_u8(tb[CLIENT_TABLE_VHT])); - - /* BSSID */ - /* - * here i know my bssid to kick the clients - * seems a good idea ?! - */ - uint8_t bssid[ETH_ALEN]; - hwaddr_aton(blobmsg_data(tb[CLIENT_TABLE_BSSID]), bssid); - kick_clients(bssid); - } + if (tb[CLIENT_TABLE] && tb[CLIENT_TABLE_BSSID] && tb[CLIENT_TABLE_FREQ] && tb[CLIENT_TABLE_HT] && + tb[CLIENT_TABLE_VHT]) { + dump_client_table(blobmsg_data(tb[CLIENT_TABLE]), blobmsg_data_len(tb[CLIENT_TABLE]), + blobmsg_data(tb[CLIENT_TABLE_BSSID]), blobmsg_get_u32(tb[CLIENT_TABLE_FREQ]), + blobmsg_get_u8(tb[CLIENT_TABLE_HT]), blobmsg_get_u8(tb[CLIENT_TABLE_VHT])); - return 0; + /* BSSID */ + /* + * here i know my bssid to kick the clients + * seems a good idea ?! + */ + uint8_t bssid[ETH_ALEN]; + hwaddr_aton(blobmsg_data(tb[CLIENT_TABLE_BSSID]), bssid); + kick_clients(bssid); + } + + return 0; } -static void ubus_get_clients_cb(struct ubus_request *req, int type, struct blob_attr *msg) -{ - if (!msg) - return; +static void ubus_get_clients_cb(struct ubus_request *req, int type, struct blob_attr *msg) { + if (!msg) + return; - parse_to_clients(msg); + parse_to_clients(msg); - char *str = blobmsg_format_json(msg, true); - send_string(str); - print_client_array(); + char *str = blobmsg_format_json(msg, true); + send_string(str); + print_client_array(); } static int ubus_get_clients() { - DIR *dirp; - struct dirent *entry; + DIR *dirp; + struct dirent *entry; - dirp = opendir(hostapd_dir_glob); // error handling? - while ((entry = readdir(dirp)) != NULL) { - if (entry->d_type == DT_SOCK) { - char hostapd_iface[256]; - uint32_t id; - sprintf(hostapd_iface, "hostapd.%s", entry->d_name); - int ret = ubus_lookup_id(ctx, hostapd_iface, &id); - if(!ret) - { - int timeout = 1; - ubus_invoke(ctx, id, "get_clients", NULL, ubus_get_clients_cb, NULL, timeout * 1000); - } + dirp = opendir(hostapd_dir_glob); // error handling? + while ((entry = readdir(dirp)) != NULL) { + if (entry->d_type == DT_SOCK) { + char hostapd_iface[256]; + uint32_t id; + sprintf(hostapd_iface, "hostapd.%s", entry->d_name); + int ret = ubus_lookup_id(ctx, hostapd_iface, &id); + if (!ret) { + int timeout = 1; + ubus_invoke(ctx, id, "get_clients", NULL, ubus_get_clients_cb, NULL, timeout * 1000); + } + } } - } - return 0; + return 0; } -void *update_clients_thread(void *arg) -{ - while (1){ - sleep(TIME_THRESHOLD_CLIENT_UPDATE); - printf("[Thread] : Updating clients!\n"); - ubus_get_clients(); - } - return 0; +void *update_clients_thread(void *arg) { + while (1) { + sleep(TIME_THRESHOLD_CLIENT_UPDATE); + printf("[Thread] : Updating clients!\n"); + ubus_get_clients(); + } + return 0; } /* hostapd function */ #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x" static void -blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const uint8_t *addr) -{ - char *s; +bblobmsg_add_macaddr(struct blob_buf *buf, const char *name, const uint8_t *addr) { + char *s; - s = blobmsg_alloc_string_buffer(buf, name, 20); - sprintf(s, MACSTR, MAC2STR(addr)); - blobmsg_add_string_buffer(buf); + s = blobmsg_alloc_string_buffer(buf, name, 20); + sprintf(s, MACSTR, MAC2STR(addr)); + blobmsg_add_string_buffer(buf); } -void del_client(const uint8_t* client_addr, uint32_t reason, uint8_t deauth, uint32_t ban_time) -{ - /* Problem: - On which interface is the client? - First send to all ifaces to ban client... xD - Maybe Hashmap? +void del_client(const uint8_t *client_addr, uint32_t reason, uint8_t deauth, uint32_t ban_time) { + /* Problem: + On which interface is the client? + First send to all ifaces to ban client... xD + Maybe Hashmap? - * get_clients method and look if client is there - * save on which hostapd the client is... - */ + * get_clients method and look if client is there + * save on which hostapd the client is... + */ - blob_buf_init(&b, 0); - blobmsg_add_macaddr(&b, "addr", client_addr); - blobmsg_add_u32(&b, "reason", reason); - blobmsg_add_u8(&b, "deauth", deauth); - blobmsg_add_u32(&b, "ban_time", ban_time); + blob_buf_init(&b, 0); + blobmsg_add_macaddr(&b, "addr", client_addr); + blobmsg_add_u32(&b, "reason", reason); + blobmsg_add_u8(&b, "deauth", deauth); + blobmsg_add_u32(&b, "ban_time", ban_time); - DIR *dirp; - struct dirent *entry; - dirp = opendir(hostapd_dir_glob); // error handling? - while ((entry = readdir(dirp)) != NULL) { - if (entry->d_type == DT_SOCK) { - char hostapd_iface[256]; - uint32_t id; - sprintf(hostapd_iface, "hostapd.%s", entry->d_name); - int ret = ubus_lookup_id(ctx, hostapd_iface, &id); - if(!ret) - { - int timeout = 1; - ubus_invoke(ctx, id, "del_client", b.head, NULL, NULL, timeout * 1000); - } + DIR *dirp; + struct dirent *entry; + dirp = opendir(hostapd_dir_glob); // error handling? + while ((entry = readdir(dirp)) != NULL) { + if (entry->d_type == DT_SOCK) { + char hostapd_iface[256]; + uint32_t id; + sprintf(hostapd_iface, "hostapd.%s", entry->d_name); + int ret = ubus_lookup_id(ctx, hostapd_iface, &id); + if (!ret) { + int timeout = 1; + ubus_invoke(ctx, id, "del_client", b.head, NULL, NULL, timeout * 1000); + } + } } - } } \ No newline at end of file