Add connections update

This commit is contained in:
PolynomialDivision 2017-09-19 18:53:07 +02:00
parent 427f0fa010
commit c48c62f481
6 changed files with 126 additions and 7 deletions

View file

@ -9,6 +9,8 @@
#include <time.h>
#include <unistd.h>
#include "tcpsocket.h"
#ifndef ETH_ALEN
#define ETH_ALEN 6
#endif
@ -179,4 +181,11 @@ pthread_mutex_t list_mutex;
node *probe_list_head;
char sort_string[SORT_NUM];
#define ARRAY_NETWORK_LEN 50
struct network_con_s network_array[ARRAY_NETWORK_LEN];
pthread_mutex_t tcp_array_mutex;
int insert_to_tcp_array(struct network_con_s entry);
int tcp_array_contains_address(struct sockaddr_in entry);
#endif

View file

@ -24,6 +24,8 @@ void *update_clients_thread(void *arg);
void *kick_clients_thread(void *arg);
void *update_connections_thread(void *arg);
char *hostapd_dir_glob;
int ubus_call_umdns();

View file

@ -42,6 +42,7 @@ pthread_t tid_client;
pthread_t tid_get_client;
pthread_t tid_kick_clients;
pthread_t tid_ap;
pthread_t tid_connections;
void daemon_shutdown()
{
@ -59,6 +60,7 @@ void daemon_shutdown()
pthread_mutex_destroy(&probe_array_mutex);
pthread_mutex_destroy(&client_array_mutex);
pthread_mutex_destroy(&ap_array_mutex);
pthread_mutex_destroy(&tcp_array_mutex);
//printf("Free Counter: %d\n", free_counter);
}
@ -171,7 +173,7 @@ int main(int argc, char **argv) {
}
}
run_tcp_socket();
// run_tcp_socket();
argc -= optind;
argv += optind;
@ -212,6 +214,11 @@ int main(int argc, char **argv) {
return 1;
}
if (pthread_mutex_init(&tcp_array_mutex, NULL) != 0) {
printf("\n mutex init failed\n");
return 1;
}
init_socket_runopts(opt_broadcast_ip, opt_broadcast_port, 1);
pthread_create(&tid_probe, NULL, &remove_array_thread, (void*)&time_config.remove_probe);

View file

@ -5,6 +5,7 @@
// http://www.geeksforgeeks.org/socket-programming-in-cc-handling-multiple-clients-on-server-without-multi-threading/
#include "tcpsocket.h"
#include "datastorage.h"
//Example code: A simple server side code, which echos back the received message.
//Handle multiple socket connections with select and fd_set on Linux
@ -23,9 +24,6 @@
#define FALSE 0
#define PORT 1025
//struct network_con_s connections[100];
//int tcp_entry_last = -1;
int run_tcp_socket()
{
int opt = TRUE;
@ -189,8 +187,26 @@ int add_tcp_conncection(char* ipv4, int port){
serv_addr.sin_addr.s_addr = inet_addr(ipv4);
serv_addr.sin_port = htons(port);
if(tcp_array_contains_address(serv_addr))
return 0;
if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
{
fprintf(stderr,"ERROR connecting");
//return 0;
}
struct network_con_s tmp =
{
.sock_addr = serv_addr,
.sockfd = sockfd
};
insert_to_tcp_array(tmp);
printf("NEW TCP CONNECTION!!!");
return 0;
}

View file

@ -32,9 +32,15 @@ void remove_old_ap_entries(time_t current_time, long long int threshold);
void print_ap_entry(ap entry);
int tcp_array_contains_address_help(struct sockaddr_in entry);
int tcp_array_insert(struct network_con_s entry);
int probe_entry_last = -1;
int client_entry_last = -1;
int ap_entry_last = -1;
int tcp_entry_last = -1;
int eval_probe_metric(struct probe_entry_s probe_entry) {
@ -890,4 +896,68 @@ void print_ap_array() {
print_ap_entry(ap_array[i]);
}
printf("------------------\n");
}
int insert_to_tcp_array(struct network_con_s entry) {
pthread_mutex_lock(&tcp_array_mutex);
int ret = tcp_array_insert(entry);
pthread_mutex_unlock(&tcp_array_mutex);
return ret;
}
int tcp_array_insert(struct network_con_s entry) {
if (tcp_entry_last == -1) {
network_array[0] = entry;
tcp_entry_last++;
return 1;
}
int i;
for (i = 0; i <= tcp_entry_last; i++) {
if (entry.sock_addr.sin_addr.s_addr < network_array[i].sock_addr.sin_addr.s_addr) {
break;
}
if (entry.sock_addr.sin_addr.s_addr == network_array[i].sock_addr.sin_addr.s_addr) {
return 0;
}
}
for (int j = tcp_entry_last; j >= i; j--) {
if (j + 1 <= ARRAY_NETWORK_LEN) {
network_array[j + 1] = network_array[j];
}
}
network_array[i] = entry;
if (tcp_entry_last < ARRAY_NETWORK_LEN) {
tcp_entry_last++;
}
return 1;
}
int tcp_array_contains_address(struct sockaddr_in entry) {
pthread_mutex_lock(&tcp_array_mutex);
int ret = tcp_array_contains_address_help(entry);
pthread_mutex_unlock(&tcp_array_mutex);
return ret;
}
int tcp_array_contains_address_help(struct sockaddr_in entry) {
if (tcp_entry_last == -1) {
return 0;
}
int i;
for (i = 0; i <= tcp_entry_last; i++) {
if (entry.sin_addr.s_addr == network_array[i].sock_addr.sin_addr.s_addr) {
return 1;
}
if (entry.sin_addr.s_addr > network_array[i].sock_addr.sin_addr.s_addr) {
return 0;
}
}
return 0;
}

View file

@ -550,6 +550,15 @@ void *update_clients_thread(void *arg) {
return 0;
}
void *update_connections_thread(void *arg) {
while (1) {
sleep(TIME_THRESHOLD_CLIENT_KICK);
printf("[Thread] : Updating Connections!\n");
ubus_call_umdns();
}
return 0;
}
void *kick_clients_thread(void *arg) {
while (1) {
sleep(TIME_THRESHOLD_CLIENT_KICK);
@ -626,6 +635,8 @@ void del_client_interface(uint32_t id, const uint8_t *client_addr, uint32_t reas
static void ubus_umdns_cb(struct ubus_request *req, int type, struct blob_attr *msg) {
struct blob_attr *tb[__DAWN_UMDNS_TABLE_MAX];
printf("PARSING UMDNS!!!\n");
if (!msg)
return;
@ -649,12 +660,14 @@ static void ubus_umdns_cb(struct ubus_request *req, int type, struct blob_attr *
//char* str = blobmsg_format_json_indent(attr, true, -1);
printf("Hostname: %s\n", hdr->name);
if (tb_dawn[DAWN_UMDNS_IPV4]) {
if (tb_dawn[DAWN_UMDNS_IPV4] && tb_dawn[DAWN_UMDNS_PORT]) {
printf("IPV4: %s\n", blobmsg_get_string(tb_dawn[DAWN_UMDNS_IPV4]));
}
if (tb_dawn[DAWN_UMDNS_PORT]) {
printf("Port: %d\n", blobmsg_get_u32(tb_dawn[DAWN_UMDNS_PORT]));
}else{
return;
}
add_tcp_conncection(blobmsg_get_string(tb_dawn[DAWN_UMDNS_IPV4]), blobmsg_get_u32(tb_dawn[DAWN_UMDNS_PORT]));
//dump_client(tb, tmp_mac, bssid_addr, freq, ht_supported, vht_supported);
}
@ -672,6 +685,8 @@ int ubus_call_umdns()
}
int timeout = 1;
ubus_invoke(ctx_clients, id, "update", NULL, NULL, NULL, timeout * 1000);
ubus_invoke(ctx_clients, id, "browse", NULL, ubus_umdns_cb, NULL, timeout * 1000);
return 0;
}