Format code

This commit is contained in:
PolynomialDivision 2017-06-25 20:27:29 +02:00
parent cfa2251ae3
commit da1c22eb93
9 changed files with 607 additions and 687 deletions

View file

@ -2,360 +2,323 @@
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] #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_help(char sort_order[], int i, probe_entry entry,
int go_next(char sort_order[], int i, probe_entry entry, probe_entry next_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_equal(uint8_t addr1[], uint8_t addr2[]);
int mac_is_greater(uint8_t addr1[], uint8_t addr2[]); int mac_is_greater(uint8_t addr1[], uint8_t addr2[]);
void print_probe_entry(probe_entry entry); void print_probe_entry(probe_entry entry);
node* delete_probe_req(node** ret_remove, node* head, uint8_t bssid_addr[], uint8_t client_addr[]); node *delete_probe_req(node **ret_remove, node *head, uint8_t bssid_addr[],
int mac_is_first_in_list(node* head, uint8_t bssid_addr[], uint8_t client_addr[]); uint8_t client_addr[]);
node* remove_node(node* head, node* curr, node* prev); int mac_is_first_in_list(node *head, uint8_t bssid_addr[],
node* remove_old_entries(node* head, time_t current_time, long long int threshold); uint8_t client_addr[]);
void print_list_with_head(node* head); 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) 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.time = time(0);
entry.counter = 0; entry.counter = 0;
// first delete probe request // first delete probe request
//probe_list_head = remove_old_entries(probe_list_head, time(0), TIME_THRESHOLD); // probe_list_head = remove_old_entries(probe_list_head, time(0),
node *tmp_probe_req = NULL; // TIME_THRESHOLD);
probe_list_head = delete_probe_req(&tmp_probe_req, probe_list_head, entry.bssid_addr, entry.client_addr); 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 if (tmp_probe_req) {
{ // local ubus
tmp_probe_req->data.signal = entry.signal; tmp_probe_req->data.signal = entry.signal;
tmp_probe_req->data.time = entry.time; tmp_probe_req->data.time = entry.time;
if(inc_counter) // when network don't increase counter... if (inc_counter) {
{ // when network don't increase counter...
tmp_probe_req->data.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);
} }
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) int go_next_help(char sort_order[], int i, probe_entry entry,
{ probe_entry next_entry) {
switch(sort_order[i]) switch (sort_order[i]) {
{ // bssid-mac
// bssid-mac case 'b':
case 'b': return mac_is_greater(entry.bssid_addr, next_entry.bssid_addr) &&
return mac_is_greater(entry.bssid_addr, next_entry.bssid_addr) && mac_is_equal(entry.client_addr, next_entry.client_addr); mac_is_equal(entry.client_addr, next_entry.client_addr);
break; break;
// client-mac // client-mac
case 'c': case 'c':
return mac_is_greater(entry.client_addr, next_entry.client_addr); return mac_is_greater(entry.client_addr, next_entry.client_addr);
break; break;
// frequency // frequency
case 'f': case 'f':
return entry.freq < next_entry.freq && mac_is_equal(entry.client_addr, next_entry.client_addr); return entry.freq < next_entry.freq &&
break; 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: // signal strength (RSSI)
return 0; case 's':
break; 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 go_next(char sort_order[], int i, probe_entry entry,
{ probe_entry next_entry) {
int conditions = 1; int conditions = 1;
for(int j = 0; j < i; j++) for (int j = 0; j < i; j++) {
{ i &= !(go_next(sort_order, j, entry, next_entry));
i &= !(go_next(sort_order, j, entry, next_entry)); }
} return conditions && go_next_help(sort_order, i, entry, next_entry);
return conditions && go_next_help(sort_order, i, entry, next_entry);
} }
node* insert(node* head, probe_entry entry) { node *insert(node *head, probe_entry entry) {
node *temp, *prev, *next; node *temp, *prev, *next;
temp = (node*)malloc(sizeof(node)); temp = (node *)malloc(sizeof(node));
temp->data = entry; temp->data = entry;
temp->ptr = NULL; temp->ptr = NULL;
// length of sorting string
// char sort_string[] = "cfsb";
int i = 0;
// length of sorting string if (!head) {
//char sort_string[] = "cfsb"; head = temp;
int i = 0; } else {
prev = NULL;
if(!head) 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; 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[]) node *delete_probe_req(node **ret_remove, node *head, uint8_t bssid_addr[],
{ uint8_t client_addr[]) {
if(!head) if (!head) {
{ return head;
return head; }
}
if(mac_is_equal(client_addr, head->data.client_addr) if (mac_is_equal(client_addr, head->data.client_addr) &&
&& mac_is_equal(bssid_addr, head->data.bssid_addr)) mac_is_equal(bssid_addr, head->data.bssid_addr)) {
{ node *temp = head;
node *temp = head; head = head->ptr;
head = head->ptr; *ret_remove = temp;
*ret_remove = temp; // don't free pointer
// don't free pointer // free(temp);
//free(temp); return head;
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 *prev = NULL;
node *next = head; node *next = head;
while(next) while (next) {
{ if (next->data.time < current_time - threshold) {
if(mac_is_greater(next->data.client_addr, client_addr)) head = remove_node(head, next, prev);
// print_list_with_head(head);
if (prev == NULL) // removed head
{ {
break; next = head;
} } else {
next = prev->ptr;
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;
} }
} else {
prev = next; prev = next;
next = next->ptr; next = next->ptr;
}
} }
return head; }
} 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 headpointer // return headpointer
node* remove_node(node* head, node* curr, node* prev) node *remove_node(node *head, node *curr, node *prev) {
{ if (curr == head) {
if(curr == head) node *temp = head;
{ head = head->ptr;
node *temp = head; free(temp);
head = head->ptr; } else {
free(temp); node *temp = curr;
} prev->ptr = curr->ptr;
else free(temp);
{ }
node *temp = curr; // printf("Removed old entry!\n");
prev->ptr = curr->ptr; return head;
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[]) int mac_is_first_in_list(node *head, uint8_t bssid_addr[],
{ uint8_t client_addr[]) {
if(!head) if (!head) {
{ return 1;
return 1; }
} node *next = head;
node *next = head; while (next) {
while(next) if (mac_is_greater(next->data.client_addr, client_addr)) {
{ break;
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;
} }
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[]) int mac_first_in_probe_list(uint8_t bssid_addr[], uint8_t client_addr[]) {
{ pthread_mutex_lock(&list_mutex);
pthread_mutex_lock(&list_mutex); int ret = mac_is_first_in_list(probe_list_head, bssid_addr, client_addr);
int ret = mac_is_first_in_list(probe_list_head, bssid_addr, client_addr); pthread_mutex_unlock(&list_mutex);
pthread_mutex_unlock(&list_mutex); return ret;
return ret;
} }
void free_list(node *head) { void free_list(node *head) {
node *prev = head; node *prev = head;
node *cur = head; node *cur = head;
while(cur) { while (cur) {
prev = cur; prev = cur;
cur = prev->ptr; cur = prev->ptr;
free(prev); free(prev);
} }
} }
int mac_is_equal(uint8_t addr1[], uint8_t addr2[]) 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[]) int mac_is_greater(uint8_t addr1[], uint8_t addr2[]) {
{ for (int i = 0; i < ETH_ALEN; i++) {
for(int i = 0; i < ETH_ALEN; i++) if (addr1[i] > addr2[i]) {
{ return 1;
if(addr1[i] > addr2[i])
{
return 1;
}
if(addr1[i] < addr2[i])
{
return 0;
}
// if equal continue...
} }
return 0; if (addr1[i] < addr2[i]) {
} 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;
}
} }
printf("------------------\n");
pthread_mutex_unlock(&list_mutex); // if equal continue...
}
return 0;
} }
void print_list() void print_list_with_head(node *head) {
{ pthread_mutex_lock(&list_mutex);
pthread_mutex_lock(&list_mutex); printf("------------------\n");
printf("------------------\n"); if (head) {
node* head = probe_list_head; node *next;
if(head) next = head;
{ while (next) {
node* next; print_probe_entry(next->data);
next = head; next = next->ptr;
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) void print_list() {
{ pthread_mutex_lock(&list_mutex);
char mac_buf_ap[20]; printf("------------------\n");
char mac_buf_client[20]; node *head = probe_list_head;
char mac_buf_target[20]; 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)); void print_probe_entry(probe_entry entry) {
sprintf(mac_buf_client, "%x:%x:%x:%x:%x:%x", MAC2STR(entry.client_addr)); char mac_buf_ap[20];
sprintf(mac_buf_target, "%x:%x:%x:%x:%x:%x", MAC2STR(entry.target_addr)); char mac_buf_client[20];
char mac_buf_target[20];
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); 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);
} }

View file

@ -1,12 +1,12 @@
#ifndef __DAWN_DATASTORAGE_H #ifndef __DAWN_DATASTORAGE_H
#define __DAWN_DATASTORAGE_H #define __DAWN_DATASTORAGE_H
#include <pthread.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <string.h> #include <string.h>
#include <pthread.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#ifndef ETH_ALEN #ifndef ETH_ALEN
@ -14,27 +14,26 @@
#endif #endif
#define SORT_NUM 5 #define SORT_NUM 5
#define TIME_THRESHOLD 60 // every minute #define TIME_THRESHOLD 60 // every minute
// Probe entrys // Probe entrys
typedef struct { typedef struct {
uint8_t bssid_addr[ETH_ALEN]; uint8_t bssid_addr[ETH_ALEN];
uint8_t client_addr[ETH_ALEN]; uint8_t client_addr[ETH_ALEN];
uint8_t target_addr[ETH_ALEN]; uint8_t target_addr[ETH_ALEN];
uint32_t signal; uint32_t signal;
uint32_t freq; uint32_t freq;
time_t time; time_t time;
int counter; int counter;
}probe_entry; } probe_entry;
// List // List
typedef struct node{ typedef struct node {
probe_entry data; probe_entry data;
struct node *ptr; struct node *ptr;
} node; } node;
node* insert(node* head, probe_entry entry); node *insert(node *head, probe_entry entry);
void free_list(node *head); void free_list(node *head);
void print_list(); void print_list();
void insert_to_list(probe_entry entry, int inc_counter); 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); void *remove_thread(void *arg);
pthread_mutex_t list_mutex; pthread_mutex_t list_mutex;
node* probe_list_head; node *probe_list_head;
char sort_string[SORT_NUM]; char sort_string[SORT_NUM];
#endif #endif

View file

@ -1,66 +1,63 @@
#include <stdio.h>
#include <libubus.h> #include <libubus.h>
#include <stdio.h>
#include "ubus.h"
#include "datastorage.h" #include "datastorage.h"
#include "networksocket.h" #include "networksocket.h"
#include "ubus.h"
#define BUFSIZE 17 #define BUFSIZE 17
#define BUFSIZE_DIR 255 #define BUFSIZE_DIR 255
int main(int argc, char **argv) {
const char *ubus_socket = NULL;
int ch;
int main(int argc, char **argv) char opt_broadcast_ip[BUFSIZE];
{ char opt_broadcast_port[BUFSIZE];
const char *ubus_socket = NULL; char opt_hostapd_dir[BUFSIZE_DIR];
int ch;
char opt_broadcast_ip[BUFSIZE]; while ((ch = getopt(argc, argv, "cs:p:i:b:o:h:")) != -1) {
char opt_broadcast_port[BUFSIZE]; switch (ch) {
char opt_hostapd_dir[BUFSIZE_DIR]; case 's':
ubus_socket = optarg;
while ((ch = getopt(argc, argv, "cs:p:i:b:o:h:")) != -1) { break;
switch (ch) { case 'p':
case 's': snprintf(opt_broadcast_port, BUFSIZE, "%s", optarg);
ubus_socket = optarg; printf("broadcast port: %s\n", opt_broadcast_port);
break; break;
case 'p': case 'i':
snprintf(opt_broadcast_port,BUFSIZE,"%s",optarg); snprintf(opt_broadcast_ip, BUFSIZE, "%s", optarg);
printf("broadcast port: %s\n", opt_broadcast_port); printf("broadcast ip: %s\n", opt_broadcast_ip);
break; break;
case 'i': case 'o':
snprintf(opt_broadcast_ip,BUFSIZE,"%s",optarg); snprintf(sort_string, SORT_NUM, "%s", optarg);
printf("broadcast ip: %s\n", opt_broadcast_ip); printf("sort string: %s\n", sort_string);
break; case 'h':
case 'o': snprintf(opt_hostapd_dir, BUFSIZE_DIR, "%s", optarg);
snprintf(sort_string,SORT_NUM,"%s",optarg); printf("hostapd dir: %s\n", opt_hostapd_dir);
printf("sort string: %s\n", sort_string); default:
case 'h': break;
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;
} }
}
init_socket_runopts(opt_broadcast_ip, opt_broadcast_port); argc -= optind;
argv += optind;
pthread_t tid;
pthread_create(&tid, NULL, &remove_thread, NULL);
dawn_init_ubus(ubus_socket, opt_hostapd_dir); if (pthread_mutex_init(&list_mutex, NULL) != 0) {
printf("\n mutex init failed\n");
// free ressources return 1;
pthread_mutex_destroy(&list_mutex); }
free_list(probe_list_head);
init_socket_runopts(opt_broadcast_ip, opt_broadcast_port);
return 0;
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;
} }

View file

@ -1,172 +1,154 @@
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
#include <libconfig.h> #include <libconfig.h>
#include <libubox/blobmsg_json.h> #include <libubox/blobmsg_json.h>
#include "networksocket.h"
#include "datastorage.h" #include "datastorage.h"
#include "networksocket.h"
#include "ubus.h" #include "ubus.h"
/* Network Defines */ /* Network Defines */
#define MAX_RECV_STRING 255 #define MAX_RECV_STRING 255
#define NET_CONFIG_PATH "/etc/wlancontroller/networkconfig.conf" #define NET_CONFIG_PATH "/etc/wlancontroller/networkconfig.conf"
/* Network Attributes */ /* Network Attributes */
int sock; int sock;
struct sockaddr_in broadcast_addr; struct sockaddr_in broadcast_addr;
const char *broadcast_ip; const char *broadcast_ip;
unsigned short broadcast_port; unsigned short broadcast_port;
int broadcast_permission; int broadcast_permission;
char recv_string[MAX_RECV_STRING+1]; char recv_string[MAX_RECV_STRING + 1];
int recv_stringLen; 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); pthread_t sniffer_thread;
init_socket(_broadcast_ip, tmp_broacast_port); if (pthread_create(&sniffer_thread, NULL, receive_msg, NULL)) {
fprintf(stderr, "Could not create receiving thread!");
return -1;
}
pthread_t sniffer_thread; fprintf(stdout, "Connected to %s:%d\n", _broadcast_ip, tmp_broacast_port);
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); return 0;
return 0;
} }
int init_socket_conffile(){ int init_socket_conffile() {
const char *_broadcast_ip; const char *_broadcast_ip;
int _broacast_port; int _broacast_port;
config_t cfg; config_t cfg;
//config_setting_t *setting; // config_setting_t *setting;
const char *config_file_name = NET_CONFIG_PATH; 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. */ /* Read the file. If there is an error, report it and exit. */
if(! config_read_file(&cfg, config_file_name)) if (!config_read_file(&cfg, config_file_name)) {
{
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg),
config_error_line(&cfg), config_error_text(&cfg)); config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg); config_destroy(&cfg);
return(EXIT_FAILURE); return (EXIT_FAILURE);
} }
if(config_lookup_string(&cfg, "broadcast_ip", &_broadcast_ip)) if (config_lookup_string(&cfg, "broadcast_ip", &_broadcast_ip))
printf("Broadcast IP: %s\n", _broadcast_ip); printf("Broadcast IP: %s\n", _broadcast_ip);
else else
fprintf(stderr, "No 'name' setting in configuration file.\n"); fprintf(stderr, "No 'name' setting in configuration file.\n");
if(config_lookup_int(&cfg, "broacast_port", &_broacast_port)) if (config_lookup_int(&cfg, "broacast_port", &_broacast_port))
printf("Broadcast Port: %d\n\n", _broacast_port); printf("Broadcast Port: %d\n\n", _broacast_port);
else else
fprintf(stderr, "No 'name' setting in configuration file.\n"); 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; pthread_t sniffer_thread;
if( pthread_create( &sniffer_thread , NULL , receive_msg , NULL) ) if (pthread_create(&sniffer_thread, NULL, receive_msg, NULL)) {
{ fprintf(stderr, "Could not create receiving thread!");
fprintf(stderr, "Could not create receiving thread!"); return -1;
return -1; }
} return 0;
return 0;
} }
void* receive_msg(void *args) void *receive_msg(void *args) {
{ while (1) {
while(1) if ((recv_stringLen =
{ recvfrom(sock, recv_string, MAX_RECV_STRING, 0, NULL, 0)) < 0) {
if ((recv_stringLen = recvfrom(sock, recv_string, MAX_RECV_STRING, 0, NULL, 0)) < 0) fprintf(stderr, "Could not receive message!");
{ continue;
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;
} }
/* Allow broadcast */ printf("[WC] Network-Received: %s\n", recv_string);
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 */ probe_entry prob_req;
memset(&broadcast_addr, 0, sizeof(broadcast_addr)); struct blob_buf b;
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) blob_buf_init(&b, 0);
{ blobmsg_add_json_from_string(&b, recv_string);
fprintf(stderr, "Binding socket failed!\n");
return -1; recv_string[recv_stringLen] = '\0';
} char *str;
return 0; 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 init_socket(const char *_broadcast_ip, unsigned short _broadcast_port) {
{ broadcast_ip = _broadcast_ip;
int msglen = strlen(msg); broadcast_port = _broadcast_port;
if (sendto(sock, msg, msglen, 0, (struct sockaddr *)
&broadcast_addr, sizeof(broadcast_addr)) != msglen) /* Create socket */
{ if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
fprintf(stderr, "Failed to send message.\n"); fprintf(stderr, "Failed to create socket.\n");
return -1; return -1;
} }
return 0;
/* 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() int send_string(char *msg) {
{ int msglen = strlen(msg);
close(sock); 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); }

View file

@ -3,7 +3,7 @@
#include "ubus.h" #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_conffile();
int init_socket(const char *_broadcastIP, unsigned short _broadcastPort); int init_socket(const char *_broadcastIP, unsigned short _broadcastPort);
int send_string(char *msg); int send_string(char *msg);

View file

@ -1,2 +1 @@
#include "runopts.h" #include "runopts.h"

View file

@ -1,8 +1,8 @@
#include <libubus.h>
#include <libubox/blobmsg_json.h>
#include <ctype.h> #include <ctype.h>
#include <sys/types.h>
#include <dirent.h> #include <dirent.h>
#include <libubox/blobmsg_json.h>
#include <libubus.h>
#include <sys/types.h>
#ifndef ETH_ALEN #ifndef ETH_ALEN
#define ETH_ALEN 6 #define ETH_ALEN 6
@ -16,179 +16,166 @@ static struct ubus_context *ctx;
static struct ubus_subscriber hostapd_event; static struct ubus_subscriber hostapd_event;
enum { enum {
PROB_BSSID_ADDR, PROB_BSSID_ADDR,
PROB_CLIENT_ADDR, PROB_CLIENT_ADDR,
PROB_TARGET_ADDR, PROB_TARGET_ADDR,
PROB_SIGNAL, PROB_SIGNAL,
PROB_FREQ, PROB_FREQ,
__PROB_MAX, __PROB_MAX,
}; };
static const struct blobmsg_policy prob_policy[__PROB_MAX] = { static const struct blobmsg_policy prob_policy[__PROB_MAX] = {
[PROB_BSSID_ADDR] = { .name = "bssid", .type = BLOBMSG_TYPE_STRING }, [PROB_BSSID_ADDR] = {.name = "bssid", .type = BLOBMSG_TYPE_STRING},
[PROB_CLIENT_ADDR] = { .name = "address", .type = BLOBMSG_TYPE_STRING }, [PROB_CLIENT_ADDR] = {.name = "address", .type = BLOBMSG_TYPE_STRING},
[PROB_TARGET_ADDR] = { .name = "target", .type = BLOBMSG_TYPE_STRING }, [PROB_TARGET_ADDR] = {.name = "target", .type = BLOBMSG_TYPE_STRING},
[PROB_SIGNAL] = { .name = "signal", .type = BLOBMSG_TYPE_INT32 }, [PROB_SIGNAL] = {.name = "signal", .type = BLOBMSG_TYPE_INT32},
[PROB_FREQ] = { .name = "freq", .type = BLOBMSG_TYPE_INT32 }, [PROB_FREQ] = {.name = "freq", .type = BLOBMSG_TYPE_INT32},
}; };
/* Function Definitions */ /* 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, static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method, struct ubus_request_data *req, const char *method,
struct blob_attr *msg); struct blob_attr *msg);
static int add_subscriber(char* name); static int add_subscriber(char *name);
int parse_to_probe_req(struct blob_attr *msg, probe_entry* prob_req); int parse_to_probe_req(struct blob_attr *msg, probe_entry *prob_req);
static int subscribe_to_hostapd_interfaces(char* hostapd_dir); static int subscribe_to_hostapd_interfaces(char *hostapd_dir);
static int decide_function(probe_entry* prob_req) static int decide_function(probe_entry *prob_req) {
{ // TODO: Refactor...
// TODO: Refactor... if (prob_req->counter < MIN_PROBE_REQ) {
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
return 0; 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) static void hostapd_handle_remove(struct ubus_context *ctx,
{ struct ubus_subscriber *s, uint32_t id) {
uloop_init(); fprintf(stderr, "Object %08x went away\n", id);
signal(SIGPIPE, SIG_IGN); }
ctx = ubus_connect(ubus_socket); int parse_to_probe_req(struct blob_attr *msg, probe_entry *prob_req) {
if (!ctx) { struct blob_attr *tb[__PROB_MAX];
fprintf(stderr, "Failed to connect to ubus\n"); blobmsg_parse(prob_policy, __PROB_MAX, tb, blob_data(msg), blob_len(msg));
return -1;
} else {
printf("Connected to ubus\n");
}
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); if (hwaddr_aton(blobmsg_data(tb[PROB_CLIENT_ADDR]), prob_req->client_addr))
return UBUS_STATUS_INVALID_ARGUMENT;
uloop_run();
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); if (tb[PROB_FREQ]) {
uloop_done(); prob_req->freq = blobmsg_get_u32(tb[PROB_FREQ]);
return 0; }
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;
} }

View file

@ -3,9 +3,9 @@
#include "datastorage.h" #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 dawn_init_ubus(const char *ubus_socket, char *hostapd_dir);
int parse_to_probe_req(struct blob_attr *msg, probe_entry* prob_req); int parse_to_probe_req(struct blob_attr *msg, probe_entry *prob_req);
#endif #endif

View file

@ -3,35 +3,28 @@
#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
static int hex_to_bin(char ch) static int hex_to_bin(char ch) {
{ if ((ch >= '0') && (ch <= '9')) return ch - '0';
if ((ch >= '0') && (ch <= '9')) ch = tolower(ch);
return ch - '0'; if ((ch >= 'a') && (ch <= 'f')) return ch - 'a' + 10;
ch = tolower(ch); return -1;
if ((ch >= 'a') && (ch <= 'f'))
return ch - 'a' + 10;
return -1;
} }
static int hwaddr_aton(const char *txt, uint8_t *addr) static int hwaddr_aton(const char *txt, uint8_t *addr) {
{ int i;
int i;
for (i = 0; i < ETH_ALEN; i++) { for (i = 0; i < ETH_ALEN; i++) {
int a, b; int a, b;
a = hex_to_bin(*txt++); a = hex_to_bin(*txt++);
if (a < 0) if (a < 0) return -1;
return -1; b = hex_to_bin(*txt++);
b = hex_to_bin(*txt++); if (b < 0) return -1;
if (b < 0) *addr++ = (a << 4) | b;
return -1; if (i < 5 && *txt++ != ':') return -1;
*addr++ = (a << 4) | b; }
if (i < 5 && *txt++ != ':')
return -1;
}
return 0; return 0;
} }
#endif #endif