From e045ccafb3c8ffc4a5dba0fe7b73e5c5772cff9a Mon Sep 17 00:00:00 2001 From: Polynomialdivision Date: Fri, 14 Sep 2018 10:05:58 +0200 Subject: [PATCH] remove tcp stuff --- src/include/tcpsocket.h | 18 ----- src/main.c | 6 -- src/network/tcpsocket.c | 160 ++++++---------------------------------- 3 files changed, 24 insertions(+), 160 deletions(-) diff --git a/src/include/tcpsocket.h b/src/include/tcpsocket.h index 2219ec3..1bd3670 100644 --- a/src/include/tcpsocket.h +++ b/src/include/tcpsocket.h @@ -16,10 +16,6 @@ struct network_con_s { int connected; }; -struct network_con_s network_array[ARRAY_NETWORK_LEN]; - -pthread_mutex_t tcp_array_mutex; - /** * Add tcp connection. * @param ipv4 @@ -35,20 +31,6 @@ int add_tcp_conncection(char *ipv4, int port); */ int run_server(int port); -/** - * Insert tcp connection to tcp array. - * @param entry - * @return - */ -int insert_to_tcp_array(struct network_con_s entry); - -/** - * Checks if a tcp address is already contained in the database. - * @param entry - * @return - */ -int tcp_array_contains_address(struct sockaddr_in entry); - /** * Send message via tcp to all other hosts. * @param msg diff --git a/src/main.c b/src/main.c index 187ad7b..519b907 100644 --- a/src/main.c +++ b/src/main.c @@ -30,7 +30,6 @@ 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); } void signal_handler(int sig) { @@ -67,11 +66,6 @@ int init_mutex() { return 1; } - if (pthread_mutex_init(&tcp_array_mutex, NULL) != 0) { - fprintf(stderr, "Mutex init failed!\n"); - return 1; - } - if (pthread_mutex_init(&denied_array_mutex, NULL) != 0) { fprintf(stderr, "Mutex init failed!\n"); return 1; diff --git a/src/network/tcpsocket.c b/src/network/tcpsocket.c index 82bc3c7..2ffa748 100644 --- a/src/network/tcpsocket.c +++ b/src/network/tcpsocket.c @@ -13,18 +13,8 @@ LIST_HEAD(tcp_sock_list); -int tcp_array_insert(struct network_con_s entry); - -int tcp_array_delete(struct sockaddr_in entry); - -int tcp_array_contains_address_help(struct sockaddr_in entry); - -void print_tcp_entry(struct network_con_s entry); - int tcp_list_contains_address(struct sockaddr_in entry); -int tcp_entry_last = -1; - static struct uloop_fd server; struct client *next_client = NULL; @@ -37,7 +27,8 @@ struct client { }; static void client_close(struct ustream *s) { - struct client *cl = container_of(s, struct client, s.stream); + struct client *cl = container_of(s, + struct client, s.stream); fprintf(stderr, "Connection closed\n"); ustream_free(s); @@ -58,14 +49,14 @@ static void client_notify_state(struct ustream *s) { fprintf(stderr, "eof!, pending: %d, total: %d\n", s->w.data_bytes, cl->ctr); - // TODO: REMOVE CLIENT FROM LIST! OR NOT? if (!s->w.data_bytes) return client_close(s); } static void client_to_server_close(struct ustream *s) { - struct network_con_s *con = container_of(s, struct network_con_s , stream.stream); + struct network_con_s *con = container_of(s, + struct network_con_s, stream.stream); fprintf(stderr, "Connection to SERVER closed\n"); ustream_free(s); @@ -75,7 +66,8 @@ static void client_to_server_close(struct ustream *s) { } static void client_to_server_state(struct ustream *s) { - struct client *cl = container_of(s, struct client, s.stream); + struct client *cl = container_of(s, + struct client, s.stream); if (!s->eof) return; @@ -96,14 +88,11 @@ static void client_read_cb(struct ustream *s, int bytes) { if (!str) break; - //printf("RECEIVED String: %s\n", str); - if (network_config.use_symm_enc) { char *base64_dec_str = malloc(B64_DECODE_LEN(strlen(str))); int base64_dec_length = b64_decode(str, base64_dec_str, B64_DECODE_LEN(strlen(str))); char *dec = gcrypt_decrypt_msg(base64_dec_str, base64_dec_length); - printf("NETRWORK RECEIVED: %s\n", dec); free(base64_dec_str); handle_network_msg(dec); free(dec); @@ -176,7 +165,8 @@ static void connect_cb(struct uloop_fd *f, unsigned int events) { return; } - struct network_con_s *entry = container_of(f, struct network_con_s, fd); + struct network_con_s *entry = container_of(f, + struct network_con_s, fd); fprintf(stderr, "Connection established\n"); uloop_fd_delete(&entry->fd); @@ -186,7 +176,6 @@ static void connect_cb(struct uloop_fd *f, unsigned int events) { ustream_fd_init(&entry->stream, entry->fd.fd); entry->connected = 1; - printf("NEW TCP CONNECTION!!!\n"); } int add_tcp_conncection(char *ipv4, int port) { @@ -212,32 +201,16 @@ int add_tcp_conncection(char *ipv4, int port) { free(tcp_entry); return -1; } - printf("Trying to add tcp socket to ULOOP!\n"); tcp_entry->fd.cb = connect_cb; uloop_fd_add(&tcp_entry->fd, ULOOP_WRITE | ULOOP_EDGE_TRIGGER); - printf("NEW TCP CONNECTION!!! to %s:%d\n", ipv4, port); + printf("New TCP connection to %s:%d\n", ipv4, port); list_add(&tcp_entry->list, &tcp_sock_list); 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) { - pthread_mutex_lock(&tcp_array_mutex); - if (network_config.use_symm_enc) { int length_enc; size_t msglen = strlen(msg); @@ -249,10 +222,8 @@ void send_tcp(char *msg) { list_for_each_entry(con, &tcp_sock_list, list) { - printf("TRYING TO SEND!\n"); - if(con->connected) { - //if(ustream_printf(&con->stream.stream, "%s", base64_enc_str) == 0) { - if(ustream_write(&con->stream.stream, base64_enc_str, base64_enc_length, 0) == 0) { + if (con->connected) { + if (ustream_write(&con->stream.stream, base64_enc_str, base64_enc_length, 0) == 0) { //TODO: ERROR HANDLING! } } @@ -262,95 +233,17 @@ void send_tcp(char *msg) { free(base64_enc_str); free(enc); } else { - for (int i = 0; i <= tcp_entry_last; i++) { - //if (send(network_array[i].sockfd, msg, strlen(msg), 0) < 0) { - struct network_con_s *con; + struct network_con_s *con; - list_for_each_entry(con, &tcp_sock_list, list) - { - printf("TRYING TO SEND!\n"); - if(con->connected) - { - if(ustream_printf(&con->stream.stream, "%s", msg) == 0) { - //TODO: ERROR HANDLING! - } + list_for_each_entry(con, &tcp_sock_list, list) + { + if (con->connected) { + if (ustream_printf(&con->stream.stream, "%s", msg) == 0) { + //TODO: ERROR HANDLING! } } } } - 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_delete(struct sockaddr_in entry) { - int i; - int found_in_array = 0; - - if (tcp_entry_last == -1) { - return 0; - } - - for (i = 0; i <= tcp_entry_last; i++) { - if (entry.sin_addr.s_addr == network_array[i].sock_addr.sin_addr.s_addr) { - found_in_array = 1; - break; - } - } - - for (int j = i; j < tcp_entry_last; j++) { - network_array[j] = network_array[j + 1]; - } - - if (tcp_entry_last > -1 && found_in_array) { - tcp_entry_last--; - } - return 0; -} - -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_list_contains_address(struct sockaddr_in entry) { @@ -360,24 +253,19 @@ int tcp_list_contains_address(struct sockaddr_in entry) { { if(entry.sin_addr.s_addr == con->sock_addr.sin_addr.s_addr) { - printf("FOUND TCP ENTRY!!!\n"); return 1; } - printf("NOT FOUND!!!\n"); } return 0; } -int tcp_array_contains_address_help(struct sockaddr_in entry) { - if (tcp_entry_last == -1) { - return 0; - } +void print_tcp_array() { + struct network_con_s *con; - 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; - } + printf("--------Connections------\n"); + list_for_each_entry(con, &tcp_sock_list, list) + { + printf("Conenctin to Port: %d, Connected: %s\n", con->sock_addr.sin_port, con->connected ? "True" : "False"); } - return 0; + printf("------------------\n"); } \ No newline at end of file