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,43 +2,48 @@
#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 *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);
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);
entry.time = time(0);
entry.counter = 0;
// 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),
// 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);
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.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++;
}
// is this correct?
probe_list_head = insert(probe_list_head, tmp_probe_req->data);
free(tmp_probe_req);
} else
{
} else {
printf("New entry!\n");
probe_list_head = insert(probe_list_head, entry);
}
@ -46,13 +51,13 @@ void insert_to_list(probe_entry entry, int inc_counter)
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])
{
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);
return mac_is_greater(entry.bssid_addr, next_entry.bssid_addr) &&
mac_is_equal(entry.client_addr, next_entry.client_addr);
break;
// client-mac
@ -62,12 +67,14 @@ int go_next_help(char sort_order[], int i, probe_entry entry, probe_entry next_e
// frequency
case 'f':
return entry.freq < next_entry.freq && mac_is_equal(entry.client_addr, next_entry.client_addr);
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);
return entry.signal < next_entry.signal &&
mac_is_equal(entry.client_addr, next_entry.client_addr);
break;
default:
@ -76,11 +83,10 @@ int go_next_help(char sort_order[], int i, probe_entry entry, probe_entry next_e
}
}
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;
for(int j = 0; j < i; j++)
{
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);
@ -92,38 +98,28 @@ node* insert(node* head, probe_entry entry) {
temp->data = entry;
temp->ptr = NULL;
// length of sorting string
// char sort_string[] = "cfsb";
int i = 0;
if(!head)
{
if (!head) {
head = temp;
}
else
{
} else {
prev = NULL;
next = head;
while(next)
{
if(go_next(sort_string, i, entry, next->data))
{
while (next) {
if (go_next(sort_string, i, entry, next->data)) {
prev = next;
next = next->ptr;
} else if (i < strlen(sort_string)) {
i++;
} else
{
} else {
break;
}
}
if (!next) {
prev->ptr = temp;
}
else
{
} else {
if (prev) {
temp->ptr = prev->ptr;
prev->ptr = temp;
@ -134,19 +130,16 @@ node* insert(node* head, probe_entry entry) {
}
}
return head;
}
node* delete_probe_req(node** ret_remove, node* head, uint8_t bssid_addr[], uint8_t client_addr[])
{
if(!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))
{
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;
@ -157,16 +150,13 @@ node* delete_probe_req(node** ret_remove, node* head, uint8_t bssid_addr[], uint
node *prev = NULL;
node *next = head;
while(next)
{
if(mac_is_greater(next->data.client_addr, client_addr))
{
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))
{
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);
@ -179,43 +169,35 @@ node* delete_probe_req(node** ret_remove, node* head, uint8_t bssid_addr[], uint
return head;
}
void *remove_thread(void *arg)
{
while(1)
{
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);
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 *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)
{
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
{
} else {
next = prev->ptr;
}
}
else
{
} else {
prev = next;
next = next->ptr;
}
@ -225,16 +207,12 @@ node* remove_old_entries(node* head, time_t current_time, long long int threshol
}
// return headpointer
node* remove_node(node* head, node* curr, node* prev)
{
if(curr == head)
{
node *remove_node(node *head, node *curr, node *prev) {
if (curr == head) {
node *temp = head;
head = head->ptr;
free(temp);
}
else
{
} else {
node *temp = curr;
prev->ptr = curr->ptr;
free(temp);
@ -243,22 +221,18 @@ node* remove_node(node* head, node* curr, node* prev)
return head;
}
int mac_is_first_in_list(node* head, uint8_t bssid_addr[], uint8_t client_addr[])
{
if(!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))
{
while (next) {
if (mac_is_greater(next->data.client_addr, client_addr)) {
break;
}
if(mac_is_equal(client_addr, next->data.client_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);
}
@ -267,8 +241,7 @@ int mac_is_first_in_list(node* head, uint8_t bssid_addr[], uint8_t client_addr[]
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);
int ret = mac_is_first_in_list(probe_list_head, bssid_addr, client_addr);
pthread_mutex_unlock(&list_mutex);
@ -285,21 +258,16 @@ void free_list(node *head) {
}
}
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;
}
int mac_is_greater(uint8_t addr1[], uint8_t addr2[])
{
for(int i = 0; i < ETH_ALEN; i++)
{
if(addr1[i] > addr2[i])
{
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])
{
if (addr1[i] < addr2[i]) {
return 0;
}
@ -308,16 +276,13 @@ int mac_is_greater(uint8_t addr1[], uint8_t addr2[])
return 0;
}
void print_list_with_head(node* head)
{
void print_list_with_head(node *head) {
pthread_mutex_lock(&list_mutex);
printf("------------------\n");
if(head)
{
if (head) {
node *next;
next = head;
while(next)
{
while (next) {
print_probe_entry(next->data);
next = next->ptr;
}
@ -326,17 +291,14 @@ void print_list_with_head(node* head)
pthread_mutex_unlock(&list_mutex);
}
void print_list()
{
void print_list() {
pthread_mutex_lock(&list_mutex);
printf("------------------\n");
node *head = probe_list_head;
if(head)
{
if (head) {
node *next;
next = head;
while(next)
{
while (next) {
print_probe_entry(next->data);
next = next->ptr;
}
@ -345,8 +307,7 @@ void print_list()
pthread_mutex_unlock(&list_mutex);
}
void print_probe_entry(probe_entry entry)
{
void print_probe_entry(probe_entry entry) {
char mac_buf_ap[20];
char mac_buf_client[20];
char mac_buf_target[20];
@ -355,7 +316,9 @@ void print_probe_entry(probe_entry entry)
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);
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
#define __DAWN_DATASTORAGE_H
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#ifndef ETH_ALEN
@ -27,7 +27,6 @@ typedef struct {
int counter;
} probe_entry;
// List
typedef struct node {
probe_entry data;

View file

@ -1,16 +1,14 @@
#include <stdio.h>
#include <libubus.h>
#include <stdio.h>
#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)
{
int main(int argc, char **argv) {
const char *ubus_socket = NULL;
int ch;
@ -45,8 +43,7 @@ int main(int argc, char **argv)
argc -= optind;
argv += optind;
if (pthread_mutex_init(&list_mutex, NULL) != 0)
{
if (pthread_mutex_init(&list_mutex, NULL) != 0) {
printf("\n mutex init failed\n");
return 1;
}

View file

@ -1,19 +1,18 @@
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <libconfig.h>
#include <libubox/blobmsg_json.h>
#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"
@ -29,13 +28,11 @@ int recv_stringLen;
void *receive_msg(void *args);
int init_socket_runopts(char *_broadcast_ip, char *_broadcast_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) )
{
if (pthread_create(&sniffer_thread, NULL, receive_msg, NULL)) {
fprintf(stderr, "Could not create receiving thread!");
return -1;
}
@ -56,8 +53,7 @@ int init_socket_conffile(){
config_init(&cfg);
/* 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),
config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
@ -79,20 +75,17 @@ int init_socket_conffile(){
config_destroy(&cfg);
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!");
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)
{
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;
}
@ -116,16 +109,12 @@ void* receive_msg(void *args)
}
}
int init_socket(const char *_broadcast_ip, unsigned short _broadcast_port)
{
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)
{
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
fprintf(stderr, "Failed to create socket.\n");
return -1;
}
@ -133,8 +122,7 @@ int init_socket(const char *_broadcast_ip, unsigned short _broadcast_port)
/* Allow broadcast */
broadcast_permission = 1;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *)&broadcast_permission,
sizeof(broadcast_permission)) < 0)
{
sizeof(broadcast_permission)) < 0) {
fprintf(stderr, "Failed to create socket.\n");
return -1;
}
@ -145,28 +133,22 @@ int init_socket(const char *_broadcast_ip, unsigned short _broadcast_port)
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)
{
if (bind(sock, (struct sockaddr *)&broadcast_addr, sizeof(broadcast_addr)) <
0) {
fprintf(stderr, "Binding socket failed!\n");
return -1;
}
return 0;
}
int send_string(char *msg)
{
int send_string(char *msg) {
int msglen = strlen(msg);
if (sendto(sock, msg, msglen, 0, (struct sockaddr *)
&broadcast_addr, sizeof(broadcast_addr)) != msglen)
{
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);
}
void close_socket() { close(sock); }

View file

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

View file

@ -1,8 +1,8 @@
#include <libubus.h>
#include <libubox/blobmsg_json.h>
#include <ctype.h>
#include <sys/types.h>
#include <dirent.h>
#include <libubox/blobmsg_json.h>
#include <libubus.h>
#include <sys/types.h>
#ifndef ETH_ALEN
#define ETH_ALEN 6
@ -33,7 +33,8 @@ static const struct blobmsg_policy prob_policy[__PROB_MAX] = {
};
/* 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);
@ -41,33 +42,28 @@ 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)
{
static int decide_function(probe_entry *prob_req) {
// 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)
{
int ret =
mac_first_in_probe_list(prob_req->bssid_addr, prob_req->client_addr);
if (ret) {
printf("Mac will be accepted!\n");
} else
{
} 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)
{
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)
{
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));
@ -80,13 +76,11 @@ int parse_to_probe_req(struct blob_attr *msg, probe_entry* prob_req)
if (hwaddr_aton(blobmsg_data(tb[PROB_TARGET_ADDR]), prob_req->target_addr))
return UBUS_STATUS_INVALID_ARGUMENT;
if (tb[PROB_SIGNAL])
{
if (tb[PROB_SIGNAL]) {
prob_req->signal = blobmsg_get_u32(tb[PROB_SIGNAL]);
}
if (tb[PROB_FREQ])
{
if (tb[PROB_FREQ]) {
prob_req->freq = blobmsg_get_u32(tb[PROB_FREQ]);
}
return 0;
@ -94,8 +88,7 @@ int parse_to_probe_req(struct blob_attr *msg, probe_entry* prob_req)
static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct blob_attr *msg) {
probe_entry prob_req;
parse_to_probe_req(msg, &prob_req);
insert_to_list(prob_req, 1);
@ -112,8 +105,7 @@ static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj,
// sleep(2); // sleep for 2s
// deny access
if(!decide_function(&prob_req))
{
if (!decide_function(&prob_req)) {
return UBUS_STATUS_UNKNOWN_ERROR;
}
@ -121,8 +113,7 @@ static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj,
return 0;
}
static int add_subscriber(char* name)
{
static int add_subscriber(char *name) {
uint32_t id = 0;
if (ubus_lookup_id(ctx, name, &id)) {
@ -141,14 +132,12 @@ static int add_subscriber(char* name)
return 0;
}
static int subscribe_to_hostapd_interfaces(char* hostapd_dir)
{
static int subscribe_to_hostapd_interfaces(char *hostapd_dir) {
DIR *dirp;
struct dirent *entry;
int ret = ubus_register_subscriber(ctx, &hostapd_event);
if (ret)
{
if (ret) {
fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
return -1;
}
@ -166,8 +155,7 @@ static int subscribe_to_hostapd_interfaces(char* hostapd_dir)
return 0;
}
int dawn_init_ubus(const char *ubus_socket, char* hostapd_dir)
{
int dawn_init_ubus(const char *ubus_socket, char *hostapd_dir) {
uloop_init();
signal(SIGPIPE, SIG_IGN);
@ -187,7 +175,6 @@ int dawn_init_ubus(const char *ubus_socket, char* hostapd_dir)
close_socket();
ubus_free(ctx);
uloop_done();
return 0;

View file

@ -3,32 +3,25 @@
#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';
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;
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;
for (i = 0; i < ETH_ALEN; i++) {
int a, b;
a = hex_to_bin(*txt++);
if (a < 0)
return -1;
if (a < 0) return -1;
b = hex_to_bin(*txt++);
if (b < 0)
return -1;
if (b < 0) return -1;
*addr++ = (a << 4) | b;
if (i < 5 && *txt++ != ':')
return -1;
if (i < 5 && *txt++ != ':') return -1;
}
return 0;