From da1c22eb93143ebecdbb74d38c0920de0dea5c93 Mon Sep 17 00:00:00 2001 From: PolynomialDivision Date: Sun, 25 Jun 2017 20:27:29 +0200 Subject: [PATCH] Format code --- src/datastorage.c | 565 +++++++++++++++++++++----------------------- src/datastorage.h | 35 ++- src/main.c | 101 ++++---- src/networksocket.c | 234 +++++++++--------- src/networksocket.h | 2 +- src/runopts.c | 1 - src/ubus.c | 309 ++++++++++++------------ src/ubus.h | 6 +- src/utils.h | 41 ++-- 9 files changed, 607 insertions(+), 687 deletions(-) diff --git a/src/datastorage.c b/src/datastorage.c index 0ca4c18..801d329 100644 --- a/src/datastorage.c +++ b/src/datastorage.c @@ -2,360 +2,323 @@ #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] -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 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); -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); +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); +void insert_to_list(probe_entry entry, int inc_counter) { + 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); + // 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); + 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++; } - - pthread_mutex_unlock(&list_mutex); + + // 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); } -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; +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; - // 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 - case 'f': - return entry.freq < next_entry.freq && 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; + // frequency + case 'f': + return entry.freq < next_entry.freq && + mac_is_equal(entry.client_addr, next_entry.client_addr); + break; - default: - return 0; - 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; + } } -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 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); } -node* insert(node* head, probe_entry entry) { - node *temp, *prev, *next; - temp = (node*)malloc(sizeof(node)); - temp->data = entry; - temp->ptr = NULL; +node *insert(node *head, probe_entry entry) { + 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) - { + 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; - } - 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; - } +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; + 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 (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; +} + +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(mac_is_greater(next->data.client_addr, client_addr)) + 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 { - 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; + next = head; + } else { + next = prev->ptr; } + } else { prev = next; - next = next->ptr; + 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; -} - -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; - } - } - 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; +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; } -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(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; +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; } - return 0; + + 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; + } + 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; +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; } 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; +int mac_is_equal(uint8_t addr1[], uint8_t addr2[]) { + 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; - } - - // if equal continue... +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; } - 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; - } + if (addr1[i] < addr2[i]) { + return 0; } - printf("------------------\n"); - pthread_mutex_unlock(&list_mutex); + + // if equal continue... + } + return 0; } -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; - } +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; } - 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]; +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; + } + } + printf("------------------\n"); + pthread_mutex_unlock(&list_mutex); +} - 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, counter: %d\n", - mac_buf_ap, mac_buf_client, mac_buf_target, entry.signal, entry.freq, entry.counter); - +void print_probe_entry(probe_entry entry) { + 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)); + + printf( + "bssid_addr: %s, client_addr: %s, target_addr: %s, signal: %d, freq: " + "%d, counter: %d\n", + mac_buf_ap, mac_buf_client, mac_buf_target, entry.signal, entry.freq, + entry.counter); } \ No newline at end of file diff --git a/src/datastorage.h b/src/datastorage.h index bcc54fa..982b020 100644 --- a/src/datastorage.h +++ b/src/datastorage.h @@ -1,12 +1,12 @@ #ifndef __DAWN_DATASTORAGE_H #define __DAWN_DATASTORAGE_H +#include +#include #include #include -#include -#include #include -#include +#include #include #ifndef ETH_ALEN @@ -14,27 +14,26 @@ #endif #define SORT_NUM 5 -#define TIME_THRESHOLD 60 // every minute +#define TIME_THRESHOLD 60 // every minute // Probe entrys typedef struct { - uint8_t bssid_addr[ETH_ALEN]; - uint8_t client_addr[ETH_ALEN]; - uint8_t target_addr[ETH_ALEN]; - uint32_t signal; - uint32_t freq; - time_t time; - int counter; -}probe_entry; - + uint8_t bssid_addr[ETH_ALEN]; + uint8_t client_addr[ETH_ALEN]; + uint8_t target_addr[ETH_ALEN]; + uint32_t signal; + uint32_t freq; + time_t time; + int counter; +} probe_entry; // List -typedef struct node{ - probe_entry data; - struct node *ptr; +typedef struct node { + probe_entry data; + struct node *ptr; } node; -node* insert(node* head, probe_entry entry); +node *insert(node *head, probe_entry entry); void free_list(node *head); void print_list(); void insert_to_list(probe_entry entry, int inc_counter); @@ -43,7 +42,7 @@ int mac_first_in_probe_list(uint8_t bssid_addr[], uint8_t client_addr[]); void *remove_thread(void *arg); pthread_mutex_t list_mutex; -node* probe_list_head; +node *probe_list_head; char sort_string[SORT_NUM]; #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 574c798..22ecdc2 100644 --- a/src/main.c +++ b/src/main.c @@ -1,66 +1,63 @@ -#include #include +#include -#include "ubus.h" #include "datastorage.h" #include "networksocket.h" +#include "ubus.h" #define BUFSIZE 17 #define BUFSIZE_DIR 255 +int main(int argc, char **argv) { + const char *ubus_socket = NULL; + int ch; -int main(int argc, char **argv) -{ - 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); - default: - break; - } - } - - argc -= optind; - argv += optind; - - if (pthread_mutex_init(&list_mutex, NULL) != 0) - { - printf("\n mutex init failed\n"); - return 1; + 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); + default: + break; } + } - init_socket_runopts(opt_broadcast_ip, opt_broadcast_port); - - pthread_t tid; - pthread_create(&tid, NULL, &remove_thread, NULL); + argc -= optind; + argv += optind; - dawn_init_ubus(ubus_socket, opt_hostapd_dir); - - // free ressources - pthread_mutex_destroy(&list_mutex); - free_list(probe_list_head); - - return 0; + if (pthread_mutex_init(&list_mutex, NULL) != 0) { + printf("\n mutex init failed\n"); + return 1; + } + + init_socket_runopts(opt_broadcast_ip, opt_broadcast_port); + + pthread_t tid; + pthread_create(&tid, NULL, &remove_thread, NULL); + + dawn_init_ubus(ubus_socket, opt_hostapd_dir); + + // free ressources + pthread_mutex_destroy(&list_mutex); + free_list(probe_list_head); + + return 0; } \ No newline at end of file diff --git a/src/networksocket.c b/src/networksocket.c index cd10d46..45fcf6d 100644 --- a/src/networksocket.c +++ b/src/networksocket.c @@ -1,172 +1,154 @@ -#include -#include #include +#include #include #include +#include #include -#include +#include #include -#include "networksocket.h" #include "datastorage.h" +#include "networksocket.h" #include "ubus.h" - /* Network Defines */ -#define MAX_RECV_STRING 255 -#define NET_CONFIG_PATH "/etc/wlancontroller/networkconfig.conf" +#define MAX_RECV_STRING 255 +#define NET_CONFIG_PATH "/etc/wlancontroller/networkconfig.conf" /* Network Attributes */ -int sock; -struct sockaddr_in broadcast_addr; -const char *broadcast_ip; +int sock; +struct sockaddr_in broadcast_addr; +const char *broadcast_ip; unsigned short broadcast_port; int broadcast_permission; -char recv_string[MAX_RECV_STRING+1]; +char recv_string[MAX_RECV_STRING + 1]; int recv_stringLen; -void* receive_msg(void *args); +void *receive_msg(void *args); -int init_socket_runopts(char* _broadcast_ip, char* _broadcast_port){ +int init_socket_runopts(char *_broadcast_ip, char *_broadcast_port) { + int tmp_broacast_port = atoi(_broadcast_port); + init_socket(_broadcast_ip, tmp_broacast_port); - int tmp_broacast_port = atoi(_broadcast_port); - init_socket(_broadcast_ip, tmp_broacast_port); + pthread_t sniffer_thread; + if (pthread_create(&sniffer_thread, NULL, receive_msg, NULL)) { + fprintf(stderr, "Could not create receiving thread!"); + return -1; + } - pthread_t sniffer_thread; - if( pthread_create( &sniffer_thread , NULL , receive_msg , NULL) ) - { - fprintf(stderr, "Could not create receiving thread!"); - return -1; - } + fprintf(stdout, "Connected to %s:%d\n", _broadcast_ip, tmp_broacast_port); - fprintf(stdout, "Connected to %s:%d\n", _broadcast_ip, tmp_broacast_port); - - return 0; + return 0; } -int init_socket_conffile(){ - const char *_broadcast_ip; - int _broacast_port; +int init_socket_conffile() { + const char *_broadcast_ip; + int _broacast_port; - config_t cfg; - //config_setting_t *setting; - const char *config_file_name = NET_CONFIG_PATH; + config_t cfg; + // config_setting_t *setting; + const char *config_file_name = NET_CONFIG_PATH; - config_init(&cfg); + config_init(&cfg); - /* Read the file. If there is an error, report it and exit. */ - if(! config_read_file(&cfg, config_file_name)) - { + /* Read the file. If there is an error, report it and exit. */ + if (!config_read_file(&cfg, config_file_name)) { fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); - return(EXIT_FAILURE); - } + return (EXIT_FAILURE); + } - if(config_lookup_string(&cfg, "broadcast_ip", &_broadcast_ip)) - printf("Broadcast IP: %s\n", _broadcast_ip); - else - fprintf(stderr, "No 'name' setting in configuration file.\n"); + if (config_lookup_string(&cfg, "broadcast_ip", &_broadcast_ip)) + printf("Broadcast IP: %s\n", _broadcast_ip); + else + fprintf(stderr, "No 'name' setting in configuration file.\n"); - if(config_lookup_int(&cfg, "broacast_port", &_broacast_port)) - printf("Broadcast Port: %d\n\n", _broacast_port); - else - fprintf(stderr, "No 'name' setting in configuration file.\n"); + if (config_lookup_int(&cfg, "broacast_port", &_broacast_port)) + printf("Broadcast Port: %d\n\n", _broacast_port); + else + fprintf(stderr, "No 'name' setting in configuration file.\n"); - init_socket(_broadcast_ip, _broacast_port); + init_socket(_broadcast_ip, _broacast_port); - config_destroy(&cfg); + config_destroy(&cfg); - pthread_t sniffer_thread; - if( pthread_create( &sniffer_thread , NULL , receive_msg , NULL) ) - { - fprintf(stderr, "Could not create receiving thread!"); - return -1; - } - return 0; + pthread_t sniffer_thread; + if (pthread_create(&sniffer_thread, NULL, receive_msg, NULL)) { + fprintf(stderr, "Could not create receiving thread!"); + return -1; + } + return 0; } -void* receive_msg(void *args) -{ - while(1) - { - if ((recv_stringLen = recvfrom(sock, recv_string, MAX_RECV_STRING, 0, NULL, 0)) < 0) - { - fprintf(stderr, "Could not receive message!"); - continue; - } - - 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); - - recv_string[recv_stringLen] = '\0'; - char *str; - str = blobmsg_format_json(b.head, true); - printf("Parsed: '%s'\n", str); - parse_to_probe_req(b.head, &prob_req); - - // insert to list - insert_to_list(prob_req, 0); - } -} - - - -int init_socket(const char *_broadcast_ip, unsigned short _broadcast_port) -{ - broadcast_ip = _broadcast_ip; - broadcast_port = _broadcast_port; - - /* Create socket */ - if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) - { - fprintf(stderr, "Failed to create socket.\n"); - return -1; +void *receive_msg(void *args) { + while (1) { + if ((recv_stringLen = + recvfrom(sock, recv_string, MAX_RECV_STRING, 0, NULL, 0)) < 0) { + fprintf(stderr, "Could not receive message!"); + continue; } - /* 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; - } + printf("[WC] Network-Received: %s\n", recv_string); - /* Construct Address */ - memset(&broadcast_addr, 0, sizeof(broadcast_addr)); - broadcast_addr.sin_family = AF_INET; - broadcast_addr.sin_addr.s_addr = inet_addr(broadcast_ip); - broadcast_addr.sin_port = htons(broadcast_port); + probe_entry prob_req; + struct blob_buf b; - if (bind(sock, (struct sockaddr *) &broadcast_addr, sizeof(broadcast_addr)) < 0) - { - fprintf(stderr, "Binding socket failed!\n"); - return -1; - } - return 0; + blob_buf_init(&b, 0); + blobmsg_add_json_from_string(&b, recv_string); + + recv_string[recv_stringLen] = '\0'; + char *str; + str = blobmsg_format_json(b.head, true); + printf("Parsed: '%s'\n", str); + parse_to_probe_req(b.head, &prob_req); + + // insert to list + insert_to_list(prob_req, 0); + } } -int send_string(char *msg) -{ - int msglen = strlen(msg); - if (sendto(sock, msg, msglen, 0, (struct sockaddr *) - &broadcast_addr, sizeof(broadcast_addr)) != msglen) - { - fprintf(stderr, "Failed to send message.\n"); - return -1; - } - return 0; +int init_socket(const char *_broadcast_ip, unsigned short _broadcast_port) { + broadcast_ip = _broadcast_ip; + broadcast_port = _broadcast_port; + + /* 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; + } + + /* Construct Address */ + memset(&broadcast_addr, 0, sizeof(broadcast_addr)); + broadcast_addr.sin_family = AF_INET; + broadcast_addr.sin_addr.s_addr = inet_addr(broadcast_ip); + broadcast_addr.sin_port = htons(broadcast_port); + + if (bind(sock, (struct sockaddr *)&broadcast_addr, sizeof(broadcast_addr)) < + 0) { + fprintf(stderr, "Binding socket failed!\n"); + return -1; + } + return 0; } -void close_socket() -{ - close(sock); +int send_string(char *msg) { + int msglen = strlen(msg); + if (sendto(sock, msg, msglen, 0, (struct sockaddr *)&broadcast_addr, + sizeof(broadcast_addr)) != msglen) { + fprintf(stderr, "Failed to send message.\n"); + return -1; + } + return 0; } +void close_socket() { close(sock); } diff --git a/src/networksocket.h b/src/networksocket.h index c3d6b21..d2dff47 100644 --- a/src/networksocket.h +++ b/src/networksocket.h @@ -3,7 +3,7 @@ #include "ubus.h" -int init_socket_runopts(char* broadcast_ip, char* broadcast_port); +int init_socket_runopts(char *broadcast_ip, char *broadcast_port); int init_socket_conffile(); int init_socket(const char *_broadcastIP, unsigned short _broadcastPort); int send_string(char *msg); diff --git a/src/runopts.c b/src/runopts.c index 11d8acd..a582f6d 100644 --- a/src/runopts.c +++ b/src/runopts.c @@ -1,2 +1 @@ #include "runopts.h" - diff --git a/src/ubus.c b/src/ubus.c index ab99915..ddc2e6d 100644 --- a/src/ubus.c +++ b/src/ubus.c @@ -1,8 +1,8 @@ -#include -#include #include -#include #include +#include +#include +#include #ifndef ETH_ALEN #define ETH_ALEN 6 @@ -16,179 +16,166 @@ static struct ubus_context *ctx; static struct ubus_subscriber hostapd_event; enum { - PROB_BSSID_ADDR, - PROB_CLIENT_ADDR, - PROB_TARGET_ADDR, - PROB_SIGNAL, - PROB_FREQ, - __PROB_MAX, + PROB_BSSID_ADDR, + PROB_CLIENT_ADDR, + PROB_TARGET_ADDR, + PROB_SIGNAL, + PROB_FREQ, + __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_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}, }; /* Function Definitions */ -static void hostapd_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s, uint32_t id); +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); -int parse_to_probe_req(struct blob_attr *msg, probe_entry* prob_req); -static int subscribe_to_hostapd_interfaces(char* hostapd_dir); + struct ubus_request_data *req, const char *method, + struct blob_attr *msg); +static int add_subscriber(char *name); +int parse_to_probe_req(struct blob_attr *msg, probe_entry *prob_req); +static int subscribe_to_hostapd_interfaces(char *hostapd_dir); -static int decide_function(probe_entry* prob_req) -{ - // TODO: Refactor... - if(prob_req->counter < MIN_PROBE_REQ) - { - return 0; - } - - int ret = mac_first_in_probe_list(prob_req->bssid_addr, prob_req->client_addr); - if(ret) - { - printf("Mac will be accepted!\n"); - } else - { - printf("Mac will be declined!\n"); - } - return ret; -} - -static void hostapd_handle_remove(struct ubus_context *ctx, struct ubus_subscriber *s, uint32_t 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)); - - 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_CLIENT_ADDR]), prob_req->client_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 (tb[PROB_SIGNAL]) - { - prob_req->signal = blobmsg_get_u32(tb[PROB_SIGNAL]); - } - - if (tb[PROB_FREQ]) - { - prob_req->freq = blobmsg_get_u32(tb[PROB_FREQ]); - } - 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); - - // send probe via network - char *str; - str = blobmsg_format_json(msg, true); - send_string(str); - - printf("[WC] Hostapd-Probe: %s : %s\n", method, str); - - print_list(); - - //sleep(2); // sleep for 2s - - // deny access - if(!decide_function(&prob_req)) - { - return UBUS_STATUS_UNKNOWN_ERROR; - } - - // allow access - return 0; -} - -static int add_subscriber(char* name) -{ - 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; - } - - // add callbacks - hostapd_event.remove_cb = hostapd_handle_remove; - hostapd_event.cb = hostapd_notify; - - int ret = ubus_subscribe(ctx, &hostapd_event, id); - - fprintf(stderr, "Watching object %08x: %s\n", id, ubus_strerror(ret)); - - return 0; -} - -static int subscribe_to_hostapd_interfaces(char* hostapd_dir) -{ - 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); - } - } - //free(hostapd_dir); // free string +static int decide_function(probe_entry *prob_req) { + // TODO: Refactor... + if (prob_req->counter < MIN_PROBE_REQ) { return 0; + } + + int ret = + mac_first_in_probe_list(prob_req->bssid_addr, prob_req->client_addr); + if (ret) { + printf("Mac will be accepted!\n"); + } else { + printf("Mac will be declined!\n"); + } + return ret; } -int dawn_init_ubus(const char *ubus_socket, char* hostapd_dir) -{ - uloop_init(); - signal(SIGPIPE, SIG_IGN); +static void hostapd_handle_remove(struct ubus_context *ctx, + struct ubus_subscriber *s, uint32_t id) { + fprintf(stderr, "Object %08x went away\n", id); +} - ctx = ubus_connect(ubus_socket); - if (!ctx) { - fprintf(stderr, "Failed to connect to ubus\n"); - return -1; - } else { - printf("Connected to ubus\n"); - } +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)); - ubus_add_uloop(ctx); + if (hwaddr_aton(blobmsg_data(tb[PROB_BSSID_ADDR]), prob_req->bssid_addr)) + return UBUS_STATUS_INVALID_ARGUMENT; - subscribe_to_hostapd_interfaces(hostapd_dir); - - uloop_run(); + if (hwaddr_aton(blobmsg_data(tb[PROB_CLIENT_ADDR]), prob_req->client_addr)) + return UBUS_STATUS_INVALID_ARGUMENT; - close_socket(); + if (hwaddr_aton(blobmsg_data(tb[PROB_TARGET_ADDR]), prob_req->target_addr)) + return UBUS_STATUS_INVALID_ARGUMENT; + if (tb[PROB_SIGNAL]) { + prob_req->signal = blobmsg_get_u32(tb[PROB_SIGNAL]); + } - ubus_free(ctx); - uloop_done(); - return 0; + if (tb[PROB_FREQ]) { + prob_req->freq = blobmsg_get_u32(tb[PROB_FREQ]); + } + 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); + + // send probe via network + char *str; + str = blobmsg_format_json(msg, true); + send_string(str); + + printf("[WC] Hostapd-Probe: %s : %s\n", method, str); + + print_list(); + + // sleep(2); // sleep for 2s + + // deny access + if (!decide_function(&prob_req)) { + return UBUS_STATUS_UNKNOWN_ERROR; + } + + // allow access + return 0; +} + +static int add_subscriber(char *name) { + 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; + } + + // add callbacks + hostapd_event.remove_cb = hostapd_handle_remove; + hostapd_event.cb = hostapd_notify; + + int ret = ubus_subscribe(ctx, &hostapd_event, id); + + fprintf(stderr, "Watching object %08x: %s\n", id, ubus_strerror(ret)); + + return 0; +} + +static int subscribe_to_hostapd_interfaces(char *hostapd_dir) { + 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); + } + } + // free(hostapd_dir); // free string + return 0; +} + +int dawn_init_ubus(const char *ubus_socket, char *hostapd_dir) { + 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"); + } + + ubus_add_uloop(ctx); + + subscribe_to_hostapd_interfaces(hostapd_dir); + + uloop_run(); + + close_socket(); + + ubus_free(ctx); + uloop_done(); + return 0; } \ No newline at end of file diff --git a/src/ubus.h b/src/ubus.h index 9777c67..9a5ec03 100644 --- a/src/ubus.h +++ b/src/ubus.h @@ -3,9 +3,9 @@ #include "datastorage.h" -#define MIN_PROBE_REQ 2 // TODO: Parse from config file... +#define MIN_PROBE_REQ 2 // TODO: Parse from config file... -int dawn_init_ubus(const char *ubus_socket, char* hostapd_dir); -int parse_to_probe_req(struct blob_attr *msg, probe_entry* prob_req); +int dawn_init_ubus(const char *ubus_socket, char *hostapd_dir); +int parse_to_probe_req(struct blob_attr *msg, probe_entry *prob_req); #endif diff --git a/src/utils.h b/src/utils.h index f6b568a..0b38086 100644 --- a/src/utils.h +++ b/src/utils.h @@ -3,35 +3,28 @@ #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] -static int hex_to_bin(char ch) -{ - if ((ch >= '0') && (ch <= '9')) - return ch - '0'; - ch = tolower(ch); - if ((ch >= 'a') && (ch <= 'f')) - return ch - 'a' + 10; - return -1; +static int hex_to_bin(char ch) { + if ((ch >= '0') && (ch <= '9')) return ch - '0'; + ch = tolower(ch); + if ((ch >= 'a') && (ch <= 'f')) return ch - 'a' + 10; + return -1; } -static int hwaddr_aton(const char *txt, uint8_t *addr) -{ - int i; +static int hwaddr_aton(const char *txt, uint8_t *addr) { + int i; - for (i = 0; i < ETH_ALEN; i++) { - int a, b; + for (i = 0; i < ETH_ALEN; i++) { + int a, b; - a = hex_to_bin(*txt++); - if (a < 0) - return -1; - b = hex_to_bin(*txt++); - if (b < 0) - return -1; - *addr++ = (a << 4) | b; - if (i < 5 && *txt++ != ':') - return -1; - } + a = hex_to_bin(*txt++); + if (a < 0) return -1; + b = hex_to_bin(*txt++); + if (b < 0) return -1; + *addr++ = (a << 4) | b; + if (i < 5 && *txt++ != ':') return -1; + } - return 0; + return 0; } #endif \ No newline at end of file