From f9c1cda499628ffe3ce121914f7cead176718611 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 20:19:02 +0200 Subject: [PATCH] Fixing unintialized values --- src/node.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++--- src/tlv.c | 13 +++++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/node.c b/src/node.c index 20ad75f..9026f78 100644 --- a/src/node.c +++ b/src/node.c @@ -1126,13 +1126,64 @@ int t_ask_for_more_peers(int sock_fd){ return ask_for_peers(sock_fd); } -/* - - +/* For every peer we know about, we send out a TLV network hash */ int t_get_network_state(int sock_fd){ print_debug(">> Getting network state..."); + print_debug(">> Sending out a TLV network hash to every peer we know of."); + if (neighbour_list == NULL) { + print_error("Our peer list is empty !"); + print_error("Skipping.."); + return -1; + } else { + // Build the network hash + // We use another variable so that we don't interfier with the current list + list * tmp_list = neighbour_list; + while (tmp_list != NULL) { + neighbour_peer * peer = (neighbour_peer *) tmp_list->data; + tlv * new_tlv = malloc(sizeof(union tlv)); + memset(new_tlv, 0, sizeof(union tlv)); + if (new_tlv == NULL) { + print_error("Error while allocating memory for the TLV !"); + return -1; + } + + // Create the structure for the receiver. + struct sockaddr_in6 * receiver = malloc(sizeof(struct sockaddr_in6)); + memset(receiver, 0, sizeof(struct sockaddr_in6)); + if (receiver == NULL) { + print_error("Error while allocating memory for the peer address!"); + return -1; + } + receiver->sin6_family = AF_INET6; + receiver->sin6_addr = peer->ip; + receiver->sin6_port = htons(peer->port); + receiver->sin6_scope_id = 0; + + // Send out a TLV network state. + list * tmp_data_list = data_list; + + if (build_network_hash(new_tlv, tmp_data_list) < 0) { + print_error("Error while building a network hash."); + return -1; + } else { + if (send_single_tlv(new_tlv, receiver, sock_fd) < 0) { + print_error("Error while sending a network hash to a peer."); + return -1; + } else { + print_debug(">> Sent network hash to a peer."); + } + } + free(new_tlv); + free(receiver); + if (tmp_list->next == NULL) { + break; + } else { + tmp_list = tmp_list->next; + } + } + } return 0; } diff --git a/src/tlv.c b/src/tlv.c index ab66f6a..953f91d 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -33,6 +33,7 @@ int build_pad1(tlv *tlv) { free(tlv->pad1); pad1 *new = (pad1*) malloc(sizeof(pad1)); + memset(new, 0, sizeof(pad1)); if(new == NULL) return -1; @@ -49,6 +50,7 @@ int build_padn(tlv *tlv, size_t len) { free(tlv->pad1); padn *new = (padn*) malloc(sizeof(padn)); + memset(new, 0, sizeof(padn)); if(new == NULL) return -1; @@ -67,6 +69,7 @@ int build_neighbour_req(tlv *tlv) { free(tlv->pad1); neighbour_req *new = (neighbour_req*) malloc(sizeof(neighbour_req)); + memset(new, 0, sizeof(neighbour_req)); if(new == NULL){ return -1; @@ -89,6 +92,7 @@ int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port) { free(tlv->pad1); neighbour *new = (neighbour*) malloc(sizeof(neighbour)); + memset(new, 0, sizeof(neighbour)); if(new == NULL) return -1; @@ -108,6 +112,7 @@ int build_network_hash(tlv *tlv, list *data_list) { free(tlv->pad1); network_hash *new = (network_hash*) malloc(sizeof(network_hash)); + memset(new, 0, sizeof(network_hash)); if(new == NULL) return -1; @@ -126,6 +131,7 @@ int build_network_state_req(tlv *tlv) { free(tlv->pad1); network_state_req *new = (network_state_req*) malloc(sizeof(network_state_req)); + memset(new,0,sizeof(network_state_req)); if(new == NULL) return -1; @@ -143,6 +149,7 @@ int build_node_hash(tlv *tlv, int64_t node_id, int16_t seqno, char *data) { free(tlv->pad1); node_hash *new = (node_hash*) malloc(sizeof(node_hash)); + memset(new,0,sizeof(node_hash)); if(new == NULL) return -1; @@ -165,6 +172,8 @@ int build_node_state_req(tlv *tlv, int64_t node_id) { free(tlv->pad1); node_state_req *new = (node_state_req*) malloc(sizeof(node_state_req)); + memset(new, 0, sizeof(node_state_req)); + if(new == NULL) return -1; @@ -183,6 +192,8 @@ int build_node_state(tlv *tlv, int64_t node_id, int16_t seqno, char *data, size_ free(tlv->pad1); node_state *new = (node_state*) malloc(sizeof(node_state)); + memset(new, 0, sizeof(node_state)); + int len = data_len + 26; if(new == NULL) @@ -213,6 +224,8 @@ int build_warning(tlv *tlv, char *message, size_t message_len) { free(tlv->pad1); warning *new = (warning*) malloc(sizeof(warning)); + memset(new, 0, sizeof(warning)); + int len = message_len; if(new == NULL)