Reformat code

This commit is contained in:
PolynomialDivision 2017-07-24 18:45:02 +02:00
parent fa52176cd8
commit 325cbc764c
6 changed files with 989 additions and 1048 deletions

View file

@ -9,76 +9,76 @@
#define BUFSIZE_DIR 255
int main(int argc, char **argv) {
const char *ubus_socket = NULL;
int ch;
const char *ubus_socket = NULL;
int ch;
char opt_broadcast_ip[BUFSIZE];
char opt_broadcast_port[BUFSIZE];
char opt_hostapd_dir[BUFSIZE_DIR];
char opt_broadcast_ip[BUFSIZE];
char opt_broadcast_port[BUFSIZE];
char opt_hostapd_dir[BUFSIZE_DIR];
while ((ch = getopt(argc, argv, "cs:p:i:b:o:h:")) != -1) {
switch (ch) {
case 's':
ubus_socket = optarg;
break;
case 'p':
snprintf(opt_broadcast_port, BUFSIZE, "%s", optarg);
printf("broadcast port: %s\n", opt_broadcast_port);
break;
case 'i':
snprintf(opt_broadcast_ip, BUFSIZE, "%s", optarg);
printf("broadcast ip: %s\n", opt_broadcast_ip);
break;
case 'o':
snprintf(sort_string, SORT_NUM, "%s", optarg);
printf("sort string: %s\n", sort_string);
case 'h':
snprintf(opt_hostapd_dir, BUFSIZE_DIR, "%s", optarg);
printf("hostapd dir: %s\n", opt_hostapd_dir);
hostapd_dir_glob = optarg;
default:
break;
while ((ch = getopt(argc, argv, "cs:p:i:b:o:h:")) != -1) {
switch (ch) {
case 's':
ubus_socket = optarg;
break;
case 'p':
snprintf(opt_broadcast_port, BUFSIZE, "%s", optarg);
printf("broadcast port: %s\n", opt_broadcast_port);
break;
case 'i':
snprintf(opt_broadcast_ip, BUFSIZE, "%s", optarg);
printf("broadcast ip: %s\n", opt_broadcast_ip);
break;
case 'o':
snprintf(sort_string, SORT_NUM, "%s", optarg);
printf("sort string: %s\n", sort_string);
case 'h':
snprintf(opt_hostapd_dir, BUFSIZE_DIR, "%s", optarg);
printf("hostapd dir: %s\n", opt_hostapd_dir);
hostapd_dir_glob = optarg;
default:
break;
}
}
}
argc -= optind;
argv += optind;
argc -= optind;
argv += optind;
if (pthread_mutex_init(&list_mutex, NULL) != 0) {
printf("\n mutex init failed\n");
return 1;
}
if (pthread_mutex_init(&list_mutex, NULL) != 0) {
printf("\n mutex init failed\n");
return 1;
}
if (pthread_mutex_init(&probe_array_mutex, NULL) != 0) {
printf("\n mutex init failed\n");
return 1;
}
if (pthread_mutex_init(&probe_array_mutex, NULL) != 0) {
printf("\n mutex init failed\n");
return 1;
}
if (pthread_mutex_init(&client_array_mutex, NULL) != 0) {
printf("\n mutex init failed\n");
return 1;
}
init_socket_runopts(opt_broadcast_ip, opt_broadcast_port, 1);
if (pthread_mutex_init(&client_array_mutex, NULL) != 0) {
printf("\n mutex init failed\n");
return 1;
}
pthread_t tid_probe;
pthread_create(&tid_probe, NULL, &remove_array_thread, NULL);
init_socket_runopts(opt_broadcast_ip, opt_broadcast_port, 1);
pthread_t tid_client;
pthread_create(&tid_client, NULL, &remove_client_array_thread, NULL);
pthread_t tid_probe;
pthread_create(&tid_probe, NULL, &remove_array_thread, NULL);
pthread_t tid_get_client;
pthread_create(&tid_get_client, NULL, &update_clients_thread, NULL);
pthread_t tid_client;
pthread_create(&tid_client, NULL, &remove_client_array_thread, NULL);
pthread_t tid_get_client;
pthread_create(&tid_get_client, NULL, &update_clients_thread, NULL);
//pthread_create(&tid, NULL, &remove_thread, NULL);
dawn_init_ubus(ubus_socket, opt_hostapd_dir);
//pthread_create(&tid, NULL, &remove_thread, NULL);
// free ressources
pthread_mutex_destroy(&list_mutex);
free_list(probe_list_head);
dawn_init_ubus(ubus_socket, opt_hostapd_dir);
return 0;
// free ressources
pthread_mutex_destroy(&list_mutex);
free_list(probe_list_head);
return 0;
}

View file

@ -9,33 +9,33 @@
#include "broadcastsocket.h"
int setup_broadcast_socket(const char *_broadcast_ip, unsigned short _broadcast_port, struct sockaddr_in *addr) {
int sock;
int broadcast_permission;
int sock;
int broadcast_permission;
/* Create socket */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
fprintf(stderr, "Failed to create socket.\n");
return -1;
}
/* Create socket */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
fprintf(stderr, "Failed to create socket.\n");
return -1;
}
/* Allow broadcast */
broadcast_permission = 1;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *)&broadcast_permission,
sizeof(broadcast_permission)) < 0) {
fprintf(stderr, "Failed to create socket.\n");
return -1;
}
/* Allow broadcast */
broadcast_permission = 1;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (void *) &broadcast_permission,
sizeof(broadcast_permission)) < 0) {
fprintf(stderr, "Failed to create socket.\n");
return -1;
}
/* Construct Address */
memset(addr, 0, sizeof(*addr));
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr(_broadcast_ip);
addr->sin_port = htons(_broadcast_port);
/* Construct Address */
memset(addr, 0, sizeof(*addr));
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = inet_addr(_broadcast_ip);
addr->sin_port = htons(_broadcast_port);
if (bind(sock, (struct sockaddr *)addr, sizeof(*addr)) <
0) {
fprintf(stderr, "Binding socket failed!\n");
return -1;
}
return sock;
if (bind(sock, (struct sockaddr *) addr, sizeof(*addr)) <
0) {
fprintf(stderr, "Binding socket failed!\n");
return -1;
}
return sock;
}

View file

@ -13,59 +13,58 @@
static struct ip_mreq command; /* static ?! */
int setup_multicast_socket(const char *_multicast_ip, unsigned short _multicast_port, struct sockaddr_in *addr)
{
int loop = 1;
int sock;
int setup_multicast_socket(const char *_multicast_ip, unsigned short _multicast_port, struct sockaddr_in *addr) {
int loop = 1;
int sock;
memset (addr, 0, sizeof (*addr));
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = htonl (INADDR_ANY);
addr->sin_port = htons (_multicast_port);
memset(addr, 0, sizeof(*addr));
addr->sin_family = AF_INET;
addr->sin_addr.s_addr = htonl (INADDR_ANY);
addr->sin_port = htons (_multicast_port);
if ( ( sock = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
perror ("socket()");
exit (EXIT_FAILURE);
}
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket()");
exit(EXIT_FAILURE);
}
/* Mehr Prozessen erlauben, denselben Port zu nutzen */
loop = 1;
if (setsockopt ( sock,
/* Mehr Prozessen erlauben, denselben Port zu nutzen */
loop = 1;
if (setsockopt(sock,
SOL_SOCKET,
SO_REUSEADDR,
&loop, sizeof (loop)) < 0) {
perror ("setsockopt:SO_REUSEADDR");
exit (EXIT_FAILURE);
}
if(bind( sock,
(struct sockaddr *)addr,
sizeof(*addr)) < 0) {
perror ("bind");
exit (EXIT_FAILURE);
}
&loop, sizeof(loop)) < 0) {
perror("setsockopt:SO_REUSEADDR");
exit(EXIT_FAILURE);
}
if (bind(sock,
(struct sockaddr *) addr,
sizeof(*addr)) < 0) {
perror("bind");
exit(EXIT_FAILURE);
}
/* Broadcast auf dieser Maschine zulassen */
loop = 1;
if (setsockopt ( sock,
/* Broadcast auf dieser Maschine zulassen */
loop = 1;
if (setsockopt(sock,
IPPROTO_IP,
IP_MULTICAST_LOOP,
&loop, sizeof (loop)) < 0) {
perror ("setsockopt:IP_MULTICAST_LOOP");
exit (EXIT_FAILURE);
}
&loop, sizeof(loop)) < 0) {
perror("setsockopt:IP_MULTICAST_LOOP");
exit(EXIT_FAILURE);
}
/* Join the broadcast group: */
command.imr_multiaddr.s_addr = inet_addr (_multicast_ip);
command.imr_interface.s_addr = htonl (INADDR_ANY);
if (command.imr_multiaddr.s_addr == -1) {
perror ("224.0.0.1 ist keine Multicast-Adresse\n");
exit (EXIT_FAILURE);
}
if (setsockopt ( sock,
/* Join the broadcast group: */
command.imr_multiaddr.s_addr = inet_addr(_multicast_ip);
command.imr_interface.s_addr = htonl (INADDR_ANY);
if (command.imr_multiaddr.s_addr == -1) {
perror("224.0.0.1 ist keine Multicast-Adresse\n");
exit(EXIT_FAILURE);
}
if (setsockopt(sock,
IPPROTO_IP,
IP_ADD_MEMBERSHIP,
&command, sizeof (command)) < 0) {
perror ("setsockopt:IP_ADD_MEMBERSHIP");
}
return sock;
&command, sizeof(command)) < 0) {
perror("setsockopt:IP_ADD_MEMBERSHIP");
}
return sock;
}

View file

@ -26,127 +26,120 @@ const char *ip;
unsigned short port;
char recv_string[MAX_RECV_STRING + 1];
int recv_string_len;
void *receive_msg(void *args);
int init_socket_runopts(char *_ip, char *_port, int broadcast_socket) {
port = atoi(_port);
ip = _ip;
if(broadcast_socket)
{
sock = setup_broadcast_socket(ip, port, &addr);
} else
{
printf("Settingup multicastsocket!\n");
sock = setup_multicast_socket(ip, port, &addr);
}
port = atoi(_port);
ip = _ip;
pthread_t sniffer_thread;
if (pthread_create(&sniffer_thread, NULL, receive_msg, NULL)) {
fprintf(stderr, "Could not create receiving thread!");
return -1;
}
if (broadcast_socket) {
sock = setup_broadcast_socket(ip, port, &addr);
} else {
printf("Settingup multicastsocket!\n");
sock = setup_multicast_socket(ip, port, &addr);
}
fprintf(stdout, "Connected to %s:%d\n", ip, port);
pthread_t sniffer_thread;
if (pthread_create(&sniffer_thread, NULL, receive_msg, NULL)) {
fprintf(stderr, "Could not create receiving thread!");
return -1;
}
return 0;
fprintf(stdout, "Connected to %s:%d\n", ip, port);
return 0;
}
void *receive_msg(void *args) {
while (1) {
if ((recv_string_len =
recvfrom(sock, recv_string, MAX_RECV_STRING, 0, NULL, 0)) < 0) {
fprintf(stderr, "Could not receive message!");
continue;
while (1) {
if ((recv_string_len =
recvfrom(sock, recv_string, MAX_RECV_STRING, 0, NULL, 0)) < 0) {
fprintf(stderr, "Could not receive message!");
continue;
}
if (recv_string == NULL) {
return 0;
}
if (strlen(recv_string) <= 0) {
return 0;
}
recv_string[recv_string_len] = '\0';
printf("[WC] Network-Received: %s\n", recv_string);
probe_entry prob_req;
struct blob_buf b;
blob_buf_init(&b, 0);
blobmsg_add_json_from_string(&b, recv_string);
char *str;
str = blobmsg_format_json(b.head, true);
/*
TODO: REFACTOR THIS!!! (just workaround)
OTHERWISE NULLPOINTER?!
*/
if (str == NULL) {
return 0;
}
if (strlen(str) <= 0) {
return 0;
}
/*
HERE IS NULLPOINTER PROBABLY
*/
if (strstr(str, "clients") != NULL) {
parse_to_clients(b.head);
} else if (strstr(str, "target") != NULL) {
if (parse_to_probe_req(b.head, &prob_req) == 0) {
insert_to_array(prob_req, 0);
}
}
//if(parse_to_probe_req(b.head, &prob_req) == 0)
//{
// insert_to_array(prob_req, 0);
//}
// insert to list
//insert_to_list(prob_req, 0);
}
if(recv_string == NULL)
{
return 0;
}
if(strlen(recv_string) <= 0)
{
return 0;
}
recv_string[recv_string_len] = '\0';
printf("[WC] Network-Received: %s\n", recv_string);
probe_entry prob_req;
struct blob_buf b;
blob_buf_init(&b, 0);
blobmsg_add_json_from_string(&b, recv_string);
char *str;
str = blobmsg_format_json(b.head, true);
/*
TODO: REFACTOR THIS!!! (just workaround)
OTHERWISE NULLPOINTER?!
*/
if(str == NULL)
{
return 0;
}
if( strlen(str) <= 0)
{
return 0;
}
/*
HERE IS NULLPOINTER PROBABLY
*/
if(strstr(str, "clients") != NULL) {
parse_to_clients(b.head);
} else if(strstr(str, "target")!= NULL)
{
if(parse_to_probe_req(b.head, &prob_req) == 0)
{
insert_to_array(prob_req, 0);
}
}
//if(parse_to_probe_req(b.head, &prob_req) == 0)
//{
// insert_to_array(prob_req, 0);
//}
// insert to list
//insert_to_list(prob_req, 0);
}
}
int send_string(char *msg) {
pthread_mutex_lock(&send_mutex);
int msglen = strlen(msg);
printf("Sending string! %s\n", msg);
if (sendto( sock,
msg,
msglen,
0,
(struct sockaddr *) &addr,
sizeof (addr)) < 0) {
perror ("sendto()");
exit (EXIT_FAILURE);
pthread_mutex_lock(&send_mutex);
int msglen = strlen(msg);
printf("Sending string! %s\n", msg);
if (sendto(sock,
msg,
msglen,
0,
(struct sockaddr *) &addr,
sizeof(addr)) < 0) {
perror("sendto()");
exit(EXIT_FAILURE);
}
pthread_mutex_unlock(&send_mutex);
pthread_mutex_unlock(&send_mutex);
/*if (sendto(sock, msg, msglen, 0, (struct sockaddr *)&addr,
sizeof(addr)) != msglen) {
fprintf(stderr, "Failed to send message.\n");
return -1;
}*/
return 0;
/*if (sendto(sock, msg, msglen, 0, (struct sockaddr *)&addr,
sizeof(addr)) != msglen) {
fprintf(stderr, "Failed to send message.\n");
return -1;
}*/
return 0;
}
void close_socket() { close(sock); }

File diff suppressed because it is too large Load diff

View file

@ -18,81 +18,86 @@ static struct ubus_subscriber hostapd_event;
static struct blob_buf b;
enum {
PROB_BSSID_ADDR,
PROB_CLIENT_ADDR,
PROB_TARGET_ADDR,
PROB_SIGNAL,
PROB_FREQ,
PROB_HT_SUPPORT,
PROB_VHT_SUPPORT,
__PROB_MAX,
PROB_BSSID_ADDR,
PROB_CLIENT_ADDR,
PROB_TARGET_ADDR,
PROB_SIGNAL,
PROB_FREQ,
PROB_HT_SUPPORT,
PROB_VHT_SUPPORT,
__PROB_MAX,
};
static const struct blobmsg_policy prob_policy[__PROB_MAX] = {
[PROB_BSSID_ADDR] = {.name = "bssid", .type = BLOBMSG_TYPE_STRING},
[PROB_CLIENT_ADDR] = {.name = "address", .type = BLOBMSG_TYPE_STRING},
[PROB_TARGET_ADDR] = {.name = "target", .type = BLOBMSG_TYPE_STRING},
[PROB_SIGNAL] = {.name = "signal", .type = BLOBMSG_TYPE_INT32},
[PROB_FREQ] = {.name = "freq", .type = BLOBMSG_TYPE_INT32},
[PROB_HT_SUPPORT] = {.name = "ht_support", .type = BLOBMSG_TYPE_INT8},
[PROB_VHT_SUPPORT] = {.name = "vht_support", .type = BLOBMSG_TYPE_INT8},
[PROB_BSSID_ADDR] = {.name = "bssid", .type = BLOBMSG_TYPE_STRING},
[PROB_CLIENT_ADDR] = {.name = "address", .type = BLOBMSG_TYPE_STRING},
[PROB_TARGET_ADDR] = {.name = "target", .type = BLOBMSG_TYPE_STRING},
[PROB_SIGNAL] = {.name = "signal", .type = BLOBMSG_TYPE_INT32},
[PROB_FREQ] = {.name = "freq", .type = BLOBMSG_TYPE_INT32},
[PROB_HT_SUPPORT] = {.name = "ht_support", .type = BLOBMSG_TYPE_INT8},
[PROB_VHT_SUPPORT] = {.name = "vht_support", .type = BLOBMSG_TYPE_INT8},
};
enum {
CLIENT_TABLE,
CLIENT_TABLE_BSSID,
CLIENT_TABLE_FREQ,
CLIENT_TABLE_HT,
CLIENT_TABLE_VHT,
__CLIENT_TABLE_MAX,
CLIENT_TABLE,
CLIENT_TABLE_BSSID,
CLIENT_TABLE_FREQ,
CLIENT_TABLE_HT,
CLIENT_TABLE_VHT,
__CLIENT_TABLE_MAX,
};
static const struct blobmsg_policy client_table_policy[__CLIENT_TABLE_MAX] = {
[CLIENT_TABLE] = {.name = "clients", .type = BLOBMSG_TYPE_TABLE},
[CLIENT_TABLE_BSSID] = {.name = "bssid", .type = BLOBMSG_TYPE_STRING},
[CLIENT_TABLE_FREQ] = {.name = "freq", .type = BLOBMSG_TYPE_INT32},
[CLIENT_TABLE_HT] = {.name = "ht_supported", .type = BLOBMSG_TYPE_INT8},
[CLIENT_TABLE_VHT] = {.name = "vht_supported", .type = BLOBMSG_TYPE_INT8},
[CLIENT_TABLE] = {.name = "clients", .type = BLOBMSG_TYPE_TABLE},
[CLIENT_TABLE_BSSID] = {.name = "bssid", .type = BLOBMSG_TYPE_STRING},
[CLIENT_TABLE_FREQ] = {.name = "freq", .type = BLOBMSG_TYPE_INT32},
[CLIENT_TABLE_HT] = {.name = "ht_supported", .type = BLOBMSG_TYPE_INT8},
[CLIENT_TABLE_VHT] = {.name = "vht_supported", .type = BLOBMSG_TYPE_INT8},
};
enum {
CLIENT_AUTH,
CLIENT_ASSOC,
CLIENT_AUTHORIZED,
CLIENT_PREAUTH,
CLIENT_WDS,
CLIENT_WMM,
CLIENT_HT,
CLIENT_VHT,
CLIENT_WPS,
CLIENT_MFP,
CLIENT_AID,
__CLIENT_MAX,
CLIENT_AUTH,
CLIENT_ASSOC,
CLIENT_AUTHORIZED,
CLIENT_PREAUTH,
CLIENT_WDS,
CLIENT_WMM,
CLIENT_HT,
CLIENT_VHT,
CLIENT_WPS,
CLIENT_MFP,
CLIENT_AID,
__CLIENT_MAX,
};
static const struct blobmsg_policy client_policy[__CLIENT_MAX] = {
[CLIENT_AUTH] = {.name = "auth", .type = BLOBMSG_TYPE_INT8},
[CLIENT_ASSOC] = {.name = "assoc", .type = BLOBMSG_TYPE_INT8},
[CLIENT_AUTHORIZED] = {.name = "authorized", .type = BLOBMSG_TYPE_INT8},
[CLIENT_PREAUTH] = {.name = "preauth", .type = BLOBMSG_TYPE_INT8},
[CLIENT_WDS] = {.name = "wds", .type = BLOBMSG_TYPE_INT8},
[CLIENT_WMM] = {.name = "wmm", .type = BLOBMSG_TYPE_INT8},
[CLIENT_HT] = {.name = "ht", .type = BLOBMSG_TYPE_INT8},
[CLIENT_VHT] = {.name = "vht", .type = BLOBMSG_TYPE_INT8},
[CLIENT_WPS] = {.name = "wps", .type = BLOBMSG_TYPE_INT8},
[CLIENT_MFP] = {.name = "mfp", .type = BLOBMSG_TYPE_INT8},
[CLIENT_AID] = {.name = "aid", .type = BLOBMSG_TYPE_INT32},
[CLIENT_AUTH] = {.name = "auth", .type = BLOBMSG_TYPE_INT8},
[CLIENT_ASSOC] = {.name = "assoc", .type = BLOBMSG_TYPE_INT8},
[CLIENT_AUTHORIZED] = {.name = "authorized", .type = BLOBMSG_TYPE_INT8},
[CLIENT_PREAUTH] = {.name = "preauth", .type = BLOBMSG_TYPE_INT8},
[CLIENT_WDS] = {.name = "wds", .type = BLOBMSG_TYPE_INT8},
[CLIENT_WMM] = {.name = "wmm", .type = BLOBMSG_TYPE_INT8},
[CLIENT_HT] = {.name = "ht", .type = BLOBMSG_TYPE_INT8},
[CLIENT_VHT] = {.name = "vht", .type = BLOBMSG_TYPE_INT8},
[CLIENT_WPS] = {.name = "wps", .type = BLOBMSG_TYPE_INT8},
[CLIENT_MFP] = {.name = "mfp", .type = BLOBMSG_TYPE_INT8},
[CLIENT_AID] = {.name = "aid", .type = BLOBMSG_TYPE_INT32},
};
/* Function Definitions */
static void hostapd_handle_remove(struct ubus_context *ctx,
struct ubus_subscriber *s, uint32_t id);
static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
static int add_subscriber(char *name);
static int subscribe_to_hostapd_interfaces(char *hostapd_dir);
static int ubus_get_clients();
/*
static int decide_function(probe_entry *prob_req) {
// TODO: Refactor...
@ -112,338 +117,325 @@ static int decide_function(probe_entry *prob_req) {
*/
static void hostapd_handle_remove(struct ubus_context *ctx,
struct ubus_subscriber *s, uint32_t id) {
fprintf(stderr, "Object %08x went away\n", id);
fprintf(stderr, "Object %08x went away\n", id);
}
int parse_to_probe_req(struct blob_attr *msg, probe_entry *prob_req) {
struct blob_attr *tb[__PROB_MAX];
blobmsg_parse(prob_policy, __PROB_MAX, tb, blob_data(msg), blob_len(msg));
struct blob_attr *tb[__PROB_MAX];
if (hwaddr_aton(blobmsg_data(tb[PROB_BSSID_ADDR]), prob_req->bssid_addr))
return UBUS_STATUS_INVALID_ARGUMENT;
blobmsg_parse(prob_policy, __PROB_MAX, tb, blob_data(msg), blob_len(msg));
if (hwaddr_aton(blobmsg_data(tb[PROB_CLIENT_ADDR]), prob_req->client_addr))
return UBUS_STATUS_INVALID_ARGUMENT;
if (hwaddr_aton(blobmsg_data(tb[PROB_BSSID_ADDR]), prob_req->bssid_addr))
return UBUS_STATUS_INVALID_ARGUMENT;
if (hwaddr_aton(blobmsg_data(tb[PROB_TARGET_ADDR]), prob_req->target_addr))
return UBUS_STATUS_INVALID_ARGUMENT;
if (hwaddr_aton(blobmsg_data(tb[PROB_CLIENT_ADDR]), prob_req->client_addr))
return UBUS_STATUS_INVALID_ARGUMENT;
if (tb[PROB_SIGNAL]) {
prob_req->signal = blobmsg_get_u32(tb[PROB_SIGNAL]);
}
if (hwaddr_aton(blobmsg_data(tb[PROB_TARGET_ADDR]), prob_req->target_addr))
return UBUS_STATUS_INVALID_ARGUMENT;
if (tb[PROB_FREQ]) {
prob_req->freq = blobmsg_get_u32(tb[PROB_FREQ]);
}
if (tb[PROB_SIGNAL]) {
prob_req->signal = blobmsg_get_u32(tb[PROB_SIGNAL]);
}
if (tb[PROB_HT_SUPPORT]) {
prob_req->ht_support = blobmsg_get_u8(tb[PROB_HT_SUPPORT]);
}
if (tb[PROB_FREQ]) {
prob_req->freq = blobmsg_get_u32(tb[PROB_FREQ]);
}
if (tb[PROB_VHT_SUPPORT]) {
prob_req->vht_support = blobmsg_get_u8(tb[PROB_VHT_SUPPORT]);
}
if (tb[PROB_HT_SUPPORT]) {
prob_req->ht_support = blobmsg_get_u8(tb[PROB_HT_SUPPORT]);
}
return 0;
if (tb[PROB_VHT_SUPPORT]) {
prob_req->vht_support = blobmsg_get_u8(tb[PROB_VHT_SUPPORT]);
}
return 0;
}
static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg) {
probe_entry prob_req;
parse_to_probe_req(msg, &prob_req);
//insert_to_list(prob_req, 1);
insert_to_array(prob_req, 1);
probe_entry prob_req;
parse_to_probe_req(msg, &prob_req);
//insert_to_list(prob_req, 1);
insert_to_array(prob_req, 1);
// send probe via network
char *str;
str = blobmsg_format_json(msg, true);
send_string(str);
// send probe via network
char *str;
str = blobmsg_format_json(msg, true);
send_string(str);
printf("[WC] Hostapd-Probe: %s : %s\n", method, str);
printf("[WC] Hostapd-Probe: %s : %s\n", method, str);
print_array();
print_array();
// sleep(2); // sleep for 2s
// sleep(2); // sleep for 2s
// deny access
//if (!decide_function(&prob_req)) {
// return UBUS_STATUS_UNKNOWN_ERROR;
//}
// deny access
//if (!decide_function(&prob_req)) {
// return UBUS_STATUS_UNKNOWN_ERROR;
//}
// allow access
return 0;
// allow access
return 0;
}
static int add_subscriber(char *name) {
uint32_t id = 0;
uint32_t id = 0;
if (ubus_lookup_id(ctx, name, &id)) {
fprintf(stderr, "Failed to look up test object for %s\n", name);
return -1;
}
if (ubus_lookup_id(ctx, name, &id)) {
fprintf(stderr, "Failed to look up test object for %s\n", name);
return -1;
}
// add callbacks
hostapd_event.remove_cb = hostapd_handle_remove;
hostapd_event.cb = hostapd_notify;
// add callbacks
hostapd_event.remove_cb = hostapd_handle_remove;
hostapd_event.cb = hostapd_notify;
int ret = ubus_subscribe(ctx, &hostapd_event, id);
int ret = ubus_subscribe(ctx, &hostapd_event, id);
fprintf(stderr, "Watching object %08x: %s\n", id, ubus_strerror(ret));
fprintf(stderr, "Watching object %08x: %s\n", id, ubus_strerror(ret));
return 0;
return 0;
}
static int subscribe_to_hostapd_interfaces(char *hostapd_dir) {
DIR *dirp;
struct dirent *entry;
DIR *dirp;
struct dirent *entry;
int ret = ubus_register_subscriber(ctx, &hostapd_event);
if (ret) {
fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
return -1;
}
dirp = opendir(hostapd_dir); // error handling?
while ((entry = readdir(dirp)) != NULL) {
if (entry->d_type == DT_SOCK) {
char subscribe_name[256];
sprintf(subscribe_name, "hostapd.%s", entry->d_name);
printf("Subscribing to %s\n", subscribe_name);
add_subscriber(subscribe_name);
int ret = ubus_register_subscriber(ctx, &hostapd_event);
if (ret) {
fprintf(stderr, "Failed to add watch handler: %s\n", ubus_strerror(ret));
return -1;
}
}
// free(hostapd_dir); // free string
return 0;
dirp = opendir(hostapd_dir); // error handling?
while ((entry = readdir(dirp)) != NULL) {
if (entry->d_type == DT_SOCK) {
char subscribe_name[256];
sprintf(subscribe_name, "hostapd.%s", entry->d_name);
printf("Subscribing to %s\n", subscribe_name);
add_subscriber(subscribe_name);
}
}
// free(hostapd_dir); // free string
return 0;
}
int dawn_init_ubus(const char *ubus_socket, char *hostapd_dir) {
uloop_init();
signal(SIGPIPE, SIG_IGN);
uloop_init();
signal(SIGPIPE, SIG_IGN);
ctx = ubus_connect(ubus_socket);
if (!ctx) {
fprintf(stderr, "Failed to connect to ubus\n");
return -1;
} else {
printf("Connected to ubus\n");
}
ctx = ubus_connect(ubus_socket);
if (!ctx) {
fprintf(stderr, "Failed to connect to ubus\n");
return -1;
} else {
printf("Connected to ubus\n");
}
ubus_add_uloop(ctx);
ubus_add_uloop(ctx);
subscribe_to_hostapd_interfaces(hostapd_dir);
subscribe_to_hostapd_interfaces(hostapd_dir);
ubus_get_clients();
/*
printf("Deleting Client\n");
//"78:02:f8:bc:ac:0b"
ubus_get_clients();
int tmp_int_mac[ETH_ALEN];
uint8_t tmp_mac[ETH_ALEN];
sscanf("78:02:f8:bc:ac:0b", "%x:%x:%x:%x:%x:%x", STR2MAC(tmp_int_mac));
for(int i = 0; i < ETH_ALEN; ++i )
tmp_mac[i] = (uint8_t) tmp_int_mac[i];
del_client(tmp_mac, 5, 1, 60000);
*/
uloop_run();
uloop_run();
close_socket();
close_socket();
ubus_free(ctx);
uloop_done();
return 0;
ubus_free(ctx);
uloop_done();
return 0;
}
static void
dump_client(struct blob_attr **tb, uint8_t client_addr[], const char* bssid_addr, uint32_t freq, uint8_t ht_supported, uint8_t vht_supported)
{
client client_entry;
dump_client(struct blob_attr **tb, uint8_t client_addr[], const char *bssid_addr, uint32_t freq, uint8_t ht_supported,
uint8_t vht_supported) {
client client_entry;
hwaddr_aton(bssid_addr, client_entry.bssid_addr);
hwaddr_aton(bssid_addr, client_entry.bssid_addr);
memcpy(client_entry.client_addr, client_addr, ETH_ALEN * sizeof(uint8_t));
memcpy(client_entry.client_addr, client_addr, ETH_ALEN * sizeof(uint8_t));
//char mac_buf_client[20];
//char mac_buf_ap[20];
//char mac_buf_client[20];
//char mac_buf_ap[20];
//sprintf(mac_buf_ap, "%x:%x:%x:%x:%x:%x", MAC2STR(client_entry.bssid_addr));
//sprintf(mac_buf_client, "%x:%x:%x:%x:%x:%x", MAC2STR(client_entry.client_addr));
//printf("Frequency is: %d\n",freq);
client_entry.freq = freq;
//sprintf(mac_buf_ap, "%x:%x:%x:%x:%x:%x", MAC2STR(client_entry.bssid_addr));
//sprintf(mac_buf_client, "%x:%x:%x:%x:%x:%x", MAC2STR(client_entry.client_addr));
//printf("Frequency is: %d\n",freq);
client_entry.freq = freq;
client_entry.ht_supported = ht_supported;
client_entry.vht_supported = vht_supported;
client_entry.ht_supported = ht_supported;
client_entry.vht_supported = vht_supported;
if (tb[CLIENT_AUTH]) {
client_entry.auth = blobmsg_get_u8(tb[CLIENT_AUTH]);
}
if (tb[CLIENT_ASSOC]) {
client_entry.assoc = blobmsg_get_u8(tb[CLIENT_ASSOC]);
}
if(tb[CLIENT_AUTHORIZED])
{
client_entry.authorized = blobmsg_get_u8(tb[CLIENT_AUTHORIZED]);
}
if(tb[CLIENT_PREAUTH]){
client_entry.preauth = blobmsg_get_u8(tb[CLIENT_PREAUTH]);
}
if(tb[CLIENT_WDS]){
client_entry.wds = blobmsg_get_u8(tb[CLIENT_WDS]);
}
if(tb[CLIENT_WMM]){
client_entry.wmm = blobmsg_get_u8(tb[CLIENT_WMM]);
}
if(tb[CLIENT_HT]){
client_entry.ht = blobmsg_get_u8(tb[CLIENT_HT]);
}
if(tb[CLIENT_VHT]){
client_entry.vht = blobmsg_get_u8(tb[CLIENT_VHT]);
}
if(tb[CLIENT_WPS]){
client_entry.wps = blobmsg_get_u8(tb[CLIENT_WPS]);
}
if(tb[CLIENT_MFP]){
client_entry.mfp = blobmsg_get_u8(tb[CLIENT_MFP]);
}
if(tb[CLIENT_AID]){
client_entry.aid = blobmsg_get_u32(tb[CLIENT_AID]);
}
if (tb[CLIENT_AUTH]) {
client_entry.auth = blobmsg_get_u8(tb[CLIENT_AUTH]);
}
if (tb[CLIENT_ASSOC]) {
client_entry.assoc = blobmsg_get_u8(tb[CLIENT_ASSOC]);
}
if (tb[CLIENT_AUTHORIZED]) {
client_entry.authorized = blobmsg_get_u8(tb[CLIENT_AUTHORIZED]);
}
if (tb[CLIENT_PREAUTH]) {
client_entry.preauth = blobmsg_get_u8(tb[CLIENT_PREAUTH]);
}
if (tb[CLIENT_WDS]) {
client_entry.wds = blobmsg_get_u8(tb[CLIENT_WDS]);
}
if (tb[CLIENT_WMM]) {
client_entry.wmm = blobmsg_get_u8(tb[CLIENT_WMM]);
}
if (tb[CLIENT_HT]) {
client_entry.ht = blobmsg_get_u8(tb[CLIENT_HT]);
}
if (tb[CLIENT_VHT]) {
client_entry.vht = blobmsg_get_u8(tb[CLIENT_VHT]);
}
if (tb[CLIENT_WPS]) {
client_entry.wps = blobmsg_get_u8(tb[CLIENT_WPS]);
}
if (tb[CLIENT_MFP]) {
client_entry.mfp = blobmsg_get_u8(tb[CLIENT_MFP]);
}
if (tb[CLIENT_AID]) {
client_entry.aid = blobmsg_get_u32(tb[CLIENT_AID]);
}
insert_client_to_array(client_entry);
insert_client_to_array(client_entry);
}
static void
dump_client_table(struct blob_attr *head, int len, const char* bssid_addr, uint32_t freq, uint8_t ht_supported, uint8_t vht_supported)
{
struct blob_attr *attr;
struct blobmsg_hdr *hdr;
dump_client_table(struct blob_attr *head, int len, const char *bssid_addr, uint32_t freq, uint8_t ht_supported,
uint8_t vht_supported) {
struct blob_attr *attr;
struct blobmsg_hdr *hdr;
__blob_for_each_attr(attr, head, len) {
hdr = blob_data(attr);
__blob_for_each_attr(attr, head, len)
{
hdr = blob_data(attr);
struct blob_attr *tb[__CLIENT_MAX];
blobmsg_parse(client_policy, __CLIENT_MAX, tb, blobmsg_data(attr), blobmsg_len(attr));
//char* str = blobmsg_format_json_indent(attr, true, -1);
struct blob_attr *tb[__CLIENT_MAX];
blobmsg_parse(client_policy, __CLIENT_MAX, tb, blobmsg_data(attr), blobmsg_len(attr));
//char* str = blobmsg_format_json_indent(attr, true, -1);
int tmp_int_mac[ETH_ALEN];
uint8_t tmp_mac[ETH_ALEN];
sscanf((char*)hdr->name, "%x:%x:%x:%x:%x:%x", STR2MAC(tmp_int_mac));
for(int i = 0; i < ETH_ALEN; ++i )
tmp_mac[i] = (uint8_t) tmp_int_mac[i];
int tmp_int_mac[ETH_ALEN];
uint8_t tmp_mac[ETH_ALEN];
sscanf((char *) hdr->name, "%x:%x:%x:%x:%x:%x", STR2MAC(tmp_int_mac));
for (int i = 0; i < ETH_ALEN; ++i)
tmp_mac[i] = (uint8_t) tmp_int_mac[i];
dump_client(tb, tmp_mac, bssid_addr, freq, ht_supported, vht_supported);
}
dump_client(tb, tmp_mac, bssid_addr, freq, ht_supported, vht_supported);
}
}
int parse_to_clients(struct blob_attr *msg) {
struct blob_attr *tb[__CLIENT_TABLE_MAX];
struct blob_attr *tb[__CLIENT_TABLE_MAX];
blobmsg_parse(client_table_policy, __CLIENT_TABLE_MAX, tb, blob_data(msg), blob_len(msg));
blobmsg_parse(client_table_policy, __CLIENT_TABLE_MAX, tb, blob_data(msg), blob_len(msg));
if (tb[CLIENT_TABLE] && tb[CLIENT_TABLE_BSSID] && tb[CLIENT_TABLE_FREQ] && tb[CLIENT_TABLE_HT] && tb[CLIENT_TABLE_VHT]) {
dump_client_table(blobmsg_data(tb[CLIENT_TABLE]), blobmsg_data_len(tb[CLIENT_TABLE]), blobmsg_data(tb[CLIENT_TABLE_BSSID]), blobmsg_get_u32(tb[CLIENT_TABLE_FREQ]), blobmsg_get_u8(tb[CLIENT_TABLE_HT]), blobmsg_get_u8(tb[CLIENT_TABLE_VHT]));
/* BSSID */
/*
* here i know my bssid to kick the clients
* seems a good idea ?!
*/
uint8_t bssid[ETH_ALEN];
hwaddr_aton(blobmsg_data(tb[CLIENT_TABLE_BSSID]), bssid);
kick_clients(bssid);
}
if (tb[CLIENT_TABLE] && tb[CLIENT_TABLE_BSSID] && tb[CLIENT_TABLE_FREQ] && tb[CLIENT_TABLE_HT] &&
tb[CLIENT_TABLE_VHT]) {
dump_client_table(blobmsg_data(tb[CLIENT_TABLE]), blobmsg_data_len(tb[CLIENT_TABLE]),
blobmsg_data(tb[CLIENT_TABLE_BSSID]), blobmsg_get_u32(tb[CLIENT_TABLE_FREQ]),
blobmsg_get_u8(tb[CLIENT_TABLE_HT]), blobmsg_get_u8(tb[CLIENT_TABLE_VHT]));
return 0;
/* BSSID */
/*
* here i know my bssid to kick the clients
* seems a good idea ?!
*/
uint8_t bssid[ETH_ALEN];
hwaddr_aton(blobmsg_data(tb[CLIENT_TABLE_BSSID]), bssid);
kick_clients(bssid);
}
return 0;
}
static void ubus_get_clients_cb(struct ubus_request *req, int type, struct blob_attr *msg)
{
if (!msg)
return;
static void ubus_get_clients_cb(struct ubus_request *req, int type, struct blob_attr *msg) {
if (!msg)
return;
parse_to_clients(msg);
parse_to_clients(msg);
char *str = blobmsg_format_json(msg, true);
send_string(str);
print_client_array();
char *str = blobmsg_format_json(msg, true);
send_string(str);
print_client_array();
}
static int ubus_get_clients() {
DIR *dirp;
struct dirent *entry;
DIR *dirp;
struct dirent *entry;
dirp = opendir(hostapd_dir_glob); // error handling?
while ((entry = readdir(dirp)) != NULL) {
if (entry->d_type == DT_SOCK) {
char hostapd_iface[256];
uint32_t id;
sprintf(hostapd_iface, "hostapd.%s", entry->d_name);
int ret = ubus_lookup_id(ctx, hostapd_iface, &id);
if(!ret)
{
int timeout = 1;
ubus_invoke(ctx, id, "get_clients", NULL, ubus_get_clients_cb, NULL, timeout * 1000);
}
dirp = opendir(hostapd_dir_glob); // error handling?
while ((entry = readdir(dirp)) != NULL) {
if (entry->d_type == DT_SOCK) {
char hostapd_iface[256];
uint32_t id;
sprintf(hostapd_iface, "hostapd.%s", entry->d_name);
int ret = ubus_lookup_id(ctx, hostapd_iface, &id);
if (!ret) {
int timeout = 1;
ubus_invoke(ctx, id, "get_clients", NULL, ubus_get_clients_cb, NULL, timeout * 1000);
}
}
}
}
return 0;
return 0;
}
void *update_clients_thread(void *arg)
{
while (1){
sleep(TIME_THRESHOLD_CLIENT_UPDATE);
printf("[Thread] : Updating clients!\n");
ubus_get_clients();
}
return 0;
void *update_clients_thread(void *arg) {
while (1) {
sleep(TIME_THRESHOLD_CLIENT_UPDATE);
printf("[Thread] : Updating clients!\n");
ubus_get_clients();
}
return 0;
}
/* hostapd function */
#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
static void
blobmsg_add_macaddr(struct blob_buf *buf, const char *name, const uint8_t *addr)
{
char *s;
bblobmsg_add_macaddr(struct blob_buf *buf, const char *name, const uint8_t *addr) {
char *s;
s = blobmsg_alloc_string_buffer(buf, name, 20);
sprintf(s, MACSTR, MAC2STR(addr));
blobmsg_add_string_buffer(buf);
s = blobmsg_alloc_string_buffer(buf, name, 20);
sprintf(s, MACSTR, MAC2STR(addr));
blobmsg_add_string_buffer(buf);
}
void del_client(const uint8_t* client_addr, uint32_t reason, uint8_t deauth, uint32_t ban_time)
{
/* Problem:
On which interface is the client?
First send to all ifaces to ban client... xD
Maybe Hashmap?
void del_client(const uint8_t *client_addr, uint32_t reason, uint8_t deauth, uint32_t ban_time) {
/* Problem:
On which interface is the client?
First send to all ifaces to ban client... xD
Maybe Hashmap?
* get_clients method and look if client is there
* save on which hostapd the client is...
*/
* get_clients method and look if client is there
* save on which hostapd the client is...
*/
blob_buf_init(&b, 0);
blobmsg_add_macaddr(&b, "addr", client_addr);
blobmsg_add_u32(&b, "reason", reason);
blobmsg_add_u8(&b, "deauth", deauth);
blobmsg_add_u32(&b, "ban_time", ban_time);
blob_buf_init(&b, 0);
blobmsg_add_macaddr(&b, "addr", client_addr);
blobmsg_add_u32(&b, "reason", reason);
blobmsg_add_u8(&b, "deauth", deauth);
blobmsg_add_u32(&b, "ban_time", ban_time);
DIR *dirp;
struct dirent *entry;
dirp = opendir(hostapd_dir_glob); // error handling?
while ((entry = readdir(dirp)) != NULL) {
if (entry->d_type == DT_SOCK) {
char hostapd_iface[256];
uint32_t id;
sprintf(hostapd_iface, "hostapd.%s", entry->d_name);
int ret = ubus_lookup_id(ctx, hostapd_iface, &id);
if(!ret)
{
int timeout = 1;
ubus_invoke(ctx, id, "del_client", b.head, NULL, NULL, timeout * 1000);
}
DIR *dirp;
struct dirent *entry;
dirp = opendir(hostapd_dir_glob); // error handling?
while ((entry = readdir(dirp)) != NULL) {
if (entry->d_type == DT_SOCK) {
char hostapd_iface[256];
uint32_t id;
sprintf(hostapd_iface, "hostapd.%s", entry->d_name);
int ret = ubus_lookup_id(ctx, hostapd_iface, &id);
if (!ret) {
int timeout = 1;
ubus_invoke(ctx, id, "del_client", b.head, NULL, NULL, timeout * 1000);
}
}
}
}
}