Change folderstructure

This commit is contained in:
PolynomialDivision 2017-07-12 18:04:32 +02:00
parent 1cefa5b4b8
commit e1f63dc3c1
18 changed files with 133 additions and 902 deletions

View file

@ -16,6 +16,12 @@ SET(SOURCES
network/networksocket.c
include/networksocket.h
network/broadcastsocket.c
include/broadcastsocket.h
network/multicastsocket.c
include/multicastsocket.h
utils/ubus.c
include/ubus.h

View file

@ -1,328 +0,0 @@
#include "datastorage.h"
#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 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);
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);
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);
}
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;
// client-mac
case 'c':
return mac_is_greater(entry.client_addr, next_entry.client_addr);
break;
// frequency
// mac is 5 ghz or 2.4 ghz?
case 'f':
return //entry.freq < next_entry.freq &&
entry.freq < 5000 &&
next_entry.freq >= 5000 &&
//entry.freq < 5 &&
mac_is_equal(entry.client_addr, next_entry.client_addr);
break;
// 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);
}
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;
if (!head) {
head = temp;
} else {
prev = NULL;
next = head;
while (next) {
if (go_next(sort_string, i, entry, next->data)) {
prev = next;
next = next->ptr;
} else if (i < strlen(sort_string)) {
i++;
} else {
break;
}
}
if (!next) {
prev->ptr = temp;
} else {
if (prev) {
temp->ptr = prev->ptr;
prev->ptr = temp;
} else {
temp->ptr = head;
head = temp;
}
}
}
return head;
}
node *delete_probe_req(node **ret_remove, node *head, uint8_t bssid_addr[],
uint8_t client_addr[]) {
if (!head) {
return head;
}
if (mac_is_equal(client_addr, head->data.client_addr) &&
mac_is_equal(bssid_addr, head->data.bssid_addr)) {
node *temp = head;
head = head->ptr;
*ret_remove = temp;
// don't free pointer
// free(temp);
return head;
}
node *prev = NULL;
node *next = head;
while (next) {
if (mac_is_greater(next->data.client_addr, client_addr)) {
break;
}
if (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 (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
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;
}
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;
}
void free_list(node *head) {
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_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...
}
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);
}
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);
}
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);
}

View file

@ -1,48 +0,0 @@
#ifndef __DAWN_DATASTORAGE_H
#define __DAWN_DATASTORAGE_H
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#ifndef ETH_ALEN
#define ETH_ALEN 6
#endif
#define SORT_NUM 5
#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;
// List
typedef struct node {
probe_entry data;
struct node *ptr;
} node;
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);
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;
char sort_string[SORT_NUM];
#endif

View file

@ -1,12 +1,6 @@
#ifndef __DAWN_BROADCASTSOCKET_H
#define __DAWN_BROADCASTSOCKET_H
#include "ubus.h"
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);
void close_socket();
int setup_broadcast_socket(const char *_broadcast_ip, unsigned short _broadcast_port, struct sockaddr_in *addr);
#endif

View file

@ -0,0 +1,6 @@
#ifndef __DAWN_MULTICASTSTSOCKET_H
#define __DAWN_MULTICASTSSOCKET_H
int setup_multicast_socket();
#endif

View file

@ -0,0 +1,12 @@
#ifndef __DAWN_NETWORKSOCKET_H
#define __DAWN_NETWORKSOCKET_H
int init_socket_runopts(char *_ip, char *_port, int broadcast_socket);
int send_string(char *msg);
void close_socket();
#endif

View file

@ -48,7 +48,7 @@ int main(int argc, char **argv) {
return 1;
}
init_socket_runopts(opt_broadcast_ip, opt_broadcast_port);
init_socket_runopts(opt_broadcast_ip, opt_broadcast_port, 0);
pthread_t tid;
pthread_create(&tid, NULL, &remove_thread, NULL);

View file

@ -5,113 +5,12 @@
#include <sys/socket.h>
#include <unistd.h>
#include <libconfig.h>
#include <libubox/blobmsg_json.h>
#include "datastorage.h"
#include "networksocket.h"
#include "broadcastsocket.h"
#include "ubus.h"
/* Network Defines */
#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;
unsigned short broadcast_port;
int broadcast_permission;
char recv_string[MAX_RECV_STRING + 1];
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)) {
fprintf(stderr, "Could not create receiving thread!");
return -1;
}
fprintf(stdout, "Connected to %s:%d\n", _broadcast_ip, tmp_broacast_port);
return 0;
}
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_init(&cfg);
/* 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);
}
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");
init_socket(_broadcast_ip, _broacast_port);
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;
}
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;
int setup_broadcast_socket(const char *_broadcast_ip, unsigned short _broadcast_port, struct sockaddr_in *addr) {
int sock;
int broadcast_permission;
/* Create socket */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
@ -128,27 +27,15 @@ int init_socket(const char *_broadcast_ip, unsigned short _broadcast_port) {
}
/* 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);
memset(addr, 0, sizeof(*addr));
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr(_broadcast_ip);
addr->sin_port = htons(_broadcast_port);
if (bind(sock, (struct sockaddr *)&broadcast_addr, sizeof(broadcast_addr)) <
if (bind(sock, (struct sockaddr *)addr, sizeof(*addr)) <
0) {
fprintf(stderr, "Binding socket failed!\n");
return -1;
}
return 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;
}
void close_socket() { close(sock); }
return sock;
}

View file

View file

@ -0,0 +1,94 @@
#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 "broadcastsocket.h"
#include "multicastsocket.h"
#include "ubus.h"
/* Network Defines */
#define MAX_RECV_STRING 255
#define NET_CONFIG_PATH "/etc/wlancontroller/networkconfig.conf"
/* Network Attributes */
int sock;
struct sockaddr_in addr;
const char *ip;
unsigned short port;
char recv_string[MAX_RECV_STRING + 1];
int recv_string_len;
void *receive_msg(void *args);
int init_socket_runopts(char *_ip, char *_port) {
printf("HALLOOO\n");
port = atoi(_port);
ip = _ip;
if(broadcast_socket)
{
sock = setup_broadcast_socket(ip, port, &addr);
} else
{
//sock = setup_multicast_socket(ip, port);
}
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", ip, port);
return 0;
}
void *receive_msg(void *args) {
while (1) {
if ((recv_string_len =
recvfrom(sock, recv_string, MAX_RECV_STRING, 0, NULL, 0)) < 0) {
fprintf(stderr, "Could not receive message!");
continue;
}
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_string_len] = '\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 *)&addr,
sizeof(addr)) != msglen) {
fprintf(stderr, "Failed to send message.\n");
return -1;
}
return 0;
}
void close_socket() { close(sock); }

View file

@ -1,154 +0,0 @@
#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 "datastorage.h"
#include "networksocket.h"
#include "ubus.h"
/* Network Defines */
#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;
unsigned short broadcast_port;
int broadcast_permission;
char recv_string[MAX_RECV_STRING + 1];
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)) {
fprintf(stderr, "Could not create receiving thread!");
return -1;
}
fprintf(stdout, "Connected to %s:%d\n", _broadcast_ip, tmp_broacast_port);
return 0;
}
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_init(&cfg);
/* 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);
}
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");
init_socket(_broadcast_ip, _broacast_port);
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;
}
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;
}
/* 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;
}
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); }

View file

@ -1,12 +0,0 @@
#ifndef __DAWN_NETWORKSOCKET_H
#define __DAWN_NETWORKSOCKET_H
#include "ubus.h"
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);
void close_socket();
#endif

View file

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

View file

@ -1,4 +0,0 @@
#ifndef __DAWN_RUNOPTS_H_
#define __DAWN_RUNOPTS_H_
#endif

View file

@ -1,181 +0,0 @@
#include <ctype.h>
#include <dirent.h>
#include <libubox/blobmsg_json.h>
#include <libubus.h>
#include <sys/types.h>
#ifndef ETH_ALEN
#define ETH_ALEN 6
#endif
#include "networksocket.h"
#include "ubus.h"
#include "utils.h"
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,
};
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},
};
/* Function Definitions */
static void hostapd_handle_remove(struct ubus_context *ctx,
struct ubus_subscriber *s, uint32_t id);
static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
static int add_subscriber(char *name);
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
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

@ -1,11 +0,0 @@
#ifndef __DAWN_UBUS_H
#define __DAWN_UBUS_H
#include "datastorage.h"
#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);
#endif

View file

@ -1,30 +0,0 @@
#ifndef __DAWN_UTILS_H
#define __DAWN_UTILS_H
#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 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;
b = hex_to_bin(*txt++);
if (b < 0) return -1;
*addr++ = (a << 4) | b;
if (i < 5 && *txt++ != ':') return -1;
}
return 0;
}
#endif

View file

@ -8,8 +8,9 @@
#define ETH_ALEN 6
#endif
#include "networksocket.h"
#include "ubus.h"
#include "networksocket.h"
#include "utils.h"
static struct ubus_context *ctx;