From 5f0a334a9a83143fa4cf32cdb48a551e1962388f Mon Sep 17 00:00:00 2001 From: skjdbg Date: Fri, 18 Oct 2019 23:43:07 +0200 Subject: [PATCH] NO LEAKS, WORKS. changed socklen_t * to socklen_t --- headers/client.h | 5 +++-- headers/receiver.h | 2 +- src/client.c | 17 ++++------------- src/main.c | 2 +- src/receiver.c | 3 +-- 5 files changed, 10 insertions(+), 19 deletions(-) diff --git a/headers/client.h b/headers/client.h index fea2a9d..98453f9 100644 --- a/headers/client.h +++ b/headers/client.h @@ -23,6 +23,7 @@ #endif + typedef struct client { /** * Is the client actively exchanging data? @@ -42,7 +43,7 @@ typedef struct client { struct sockaddr_in6 *address; /** Client address length */ - socklen_t *addr_len; + socklen_t addr_len; /** Current client receive window */ buf_t *window; @@ -52,7 +53,7 @@ typedef struct client { GETSET(client_t, FILE *, out_file); GETSET(client_t, struct sockaddr_in6 *, address); -GETSET(client_t, socklen_t *, addr_len); +GETSET(client_t, socklen_t, addr_len); GETSET(client_t, buf_t *, window); GETSET(client_t, uint32_t, id); diff --git a/headers/receiver.h b/headers/receiver.h index 8a99852..277ac88 100644 --- a/headers/receiver.h +++ b/headers/receiver.h @@ -52,7 +52,7 @@ typedef struct receive_thread_config { /** Socket file descriptor */ int sockfd; - socklen_t *addr_len; + socklen_t addr_len; /** Maximum number of clients */ int max_clients; diff --git a/src/client.c b/src/client.c index d6250dd..d4751bf 100644 --- a/src/client.c +++ b/src/client.c @@ -4,10 +4,11 @@ GETSET_IMPL(client_t, FILE *, out_file); GETSET_IMPL(client_t, struct sockaddr_in6 *, address); -GETSET_IMPL(client_t, socklen_t *, addr_len); +GETSET_IMPL(client_t, socklen_t, addr_len); GETSET_IMPL(client_t, uint32_t, id); +#define SIZE_SOCKADDR_IN6 sizeof(struct sockaddr_in6) pthread_mutex_t *client_get_lock(client_t *self) { return self->lock; @@ -77,19 +78,11 @@ int initialize_client(client_t *client, uint32_t id, char *format) { return -1; } - client->addr_len = (socklen_t *) malloc(sizeof(socklen_t)); - if(client->addr_len == NULL) { - free(client->address); - pthread_mutex_destroy(client->lock); - free(client->lock); - free(client); - errno = FAILED_TO_ALLOCATE; - return -1; - } + //TODO + client->addr_len = SIZE_SOCKADDR_IN6; client->window = (buf_t *) malloc(sizeof(buf_t)); if(client->window == NULL){ - free(client->addr_len); free(client->address); pthread_mutex_destroy(client->lock); free(client->lock); @@ -99,7 +92,6 @@ int initialize_client(client_t *client, uint32_t id, char *format) { } if(initialize_buffer(client->window, &allocate_packet) != 0) { - free(client->addr_len); free(client->address); pthread_mutex_destroy(client->lock); free(client->lock); @@ -126,7 +118,6 @@ void deallocate_client(client_t *client, bool dealloc_addr, bool close_file) { if (dealloc_addr) { free(client->address); - free(client->addr_len); } if (close_file) { diff --git a/src/main.c b/src/main.c index 4d9c572..7ba5a15 100644 --- a/src/main.c +++ b/src/main.c @@ -478,7 +478,7 @@ int main(int argc, char *argv[]) { rx_config->stop = false; rx_config->rx = hd_to_rx; rx_config->tx = rx_to_hd; - rx_config->addr_len = &config.addr_info->ai_addrlen; + rx_config->addr_len = sizeof(struct sockaddr_in6); tx_config->send_rx = hd_to_tx; tx_config->send_tx = tx_to_hd; diff --git a/src/receiver.c b/src/receiver.c index 4dfa3b9..c9431b9 100644 --- a/src/receiver.c +++ b/src/receiver.c @@ -34,7 +34,7 @@ void *receive_thread(void *receive_config) { req = (hd_req_t *) node->content; } - req->length = recvfrom(rcv_cfg->sockfd, req->buffer, buf_size, 0, (struct sockaddr *) &sockaddr, rcv_cfg->addr_len); + req->length = recvfrom(rcv_cfg->sockfd, req->buffer, buf_size, 0, (struct sockaddr *) &sockaddr, &rcv_cfg->addr_len); if(req->length == -1) { switch(errno) { case EAGAIN: @@ -76,7 +76,6 @@ void *receive_thread(void *receive_config) { break; } *contained->address = sockaddr; - *contained->addr_len = addr_len; ht_put(rcv_cfg->clients, port, ip, (void *) contained);