From 8a735577c4451c1227fcca13d89642cc31089249 Mon Sep 17 00:00:00 2001 From: PolynomialDivision Date: Sat, 6 Jan 2018 17:21:04 +0100 Subject: [PATCH] move tcp --- src/include/datastorage.h | 6 --- src/include/tcpsocket.h | 15 ++++-- src/network/tcpsocket.c | 103 ++++++++++++++++++++++++++++++++++++ src/storage/datastorage.c | 107 -------------------------------------- 4 files changed, 115 insertions(+), 116 deletions(-) diff --git a/src/include/datastorage.h b/src/include/datastorage.h index cd862d3..5d38194 100644 --- a/src/include/datastorage.h +++ b/src/include/datastorage.h @@ -187,8 +187,6 @@ pthread_mutex_t client_array_mutex; struct ap_s ap_array[ARRAY_AP_LEN]; pthread_mutex_t ap_array_mutex; -void print_tcp_array(); - int mac_is_equal(uint8_t addr1[], uint8_t addr2[]); int mac_is_greater(uint8_t addr1[], uint8_t addr2[]); @@ -261,9 +259,5 @@ node *probe_list_head; #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); -void send_tcp(char* msg); #endif \ No newline at end of file diff --git a/src/include/tcpsocket.h b/src/include/tcpsocket.h index 17c82c3..3c59328 100644 --- a/src/include/tcpsocket.h +++ b/src/include/tcpsocket.h @@ -6,9 +6,7 @@ #define DAWN_TCPSOCKET_H #include - -void *run_tcp_socket(void *arg); -int add_tcp_conncection(char* ipv4, int port); +#include struct network_con_s { @@ -16,4 +14,15 @@ struct network_con_s struct sockaddr_in sock_addr; }; +void *run_tcp_socket(void *arg); +int add_tcp_conncection(char* ipv4, int port); + +void print_tcp_array(); +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); +void send_tcp(char* msg); + + + #endif //DAWN_TCPSOCKET_H diff --git a/src/network/tcpsocket.c b/src/network/tcpsocket.c index 227063d..e0b6141 100644 --- a/src/network/tcpsocket.c +++ b/src/network/tcpsocket.c @@ -21,6 +21,14 @@ #define TRUE 1 #define FALSE 0 +int tcp_array_insert(struct network_con_s entry); + +int tcp_array_contains_address_help(struct sockaddr_in entry); + +void print_tcp_entry(struct network_con_s entry); + +int tcp_entry_last = -1; + void *run_tcp_socket(void *arg) { int opt = TRUE; @@ -213,4 +221,99 @@ int add_tcp_conncection(char* ipv4, int port){ return 0; } +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; +} + +void print_tcp_entry(struct network_con_s entry) +{ + printf("Conenctin to Port: %d\n", entry.sock_addr.sin_port); +} + +void send_tcp(char* msg) +{ + printf("SENDING TCP!\n"); + pthread_mutex_lock(&tcp_array_mutex); + for (int i = 0; i <= tcp_entry_last; i++) { + if(send(network_array[i].sockfd, msg, strlen(msg), 0) < 0) + { + close(network_array->sockfd); + printf("Removing bad TCP connection!\n"); + for (int j = i; j < tcp_entry_last; j++) { + network_array[j] = network_array[j + 1]; + } + + if (tcp_entry_last > -1) { + tcp_entry_last--; + } + } + } + pthread_mutex_unlock(&tcp_array_mutex); +} + + +void print_tcp_array() +{ + printf("--------Connections------\n"); + for (int i = 0; i <= tcp_entry_last; i++) { + print_tcp_entry(network_array[i]); + } + printf("------------------\n"); +} + +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; + } + } + return 0; +} \ No newline at end of file diff --git a/src/storage/datastorage.c b/src/storage/datastorage.c index 7188f04..566ff5b 100644 --- a/src/storage/datastorage.c +++ b/src/storage/datastorage.c @@ -37,12 +37,6 @@ 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); - -void print_tcp_entry(struct network_con_s entry); - int probe_array_update_rssi(uint8_t bssid_addr[], uint8_t client_addr[], uint32_t rssi); int is_connected(uint8_t bssid_addr[], uint8_t client_addr[]); @@ -56,7 +50,6 @@ int compare_ssid(uint8_t *bssid_addr_own, uint8_t *bssid_addr_to_compare); int probe_entry_last = -1; int client_entry_last = -1; int ap_entry_last = -1; -int tcp_entry_last = -1; int mac_list_entry_last = -1; void remove_probe_array_cb(struct uloop_timeout *t); @@ -1323,104 +1316,4 @@ 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; -} - -void print_tcp_entry(struct network_con_s entry) -{ - printf("Conenctin to Port: %d\n", entry.sock_addr.sin_port); -} - -void send_tcp(char* msg) -{ - printf("SENDING TCP!\n"); - pthread_mutex_lock(&tcp_array_mutex); - for (int i = 0; i <= tcp_entry_last; i++) { - if(send(network_array[i].sockfd, msg, strlen(msg), 0) < 0) - { - close(network_array->sockfd); - printf("Removing bad TCP connection!\n"); - for (int j = i; j < client_entry_last; j++) { - network_array[j] = network_array[j + 1]; - } - - if (tcp_entry_last > -1) { - tcp_entry_last--; - } - } - } - pthread_mutex_unlock(&tcp_array_mutex); -} - - -void print_tcp_array() -{ - printf("--------Connections------\n"); - for (int i = 0; i <= tcp_entry_last; i++) { - print_tcp_entry(network_array[i]); - } - printf("------------------\n"); -} - -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; } \ No newline at end of file