From 25e703b993332284ef9e053bc94686e245fb8805 Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 28 Apr 2020 17:48:41 +0200 Subject: [PATCH 01/25] Added basic add_message function --- src/node.c | 24 ++++++++++++++++-------- src/node.h | 4 ++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/node.c b/src/node.c index 0aee474..c2ef9aa 100644 --- a/src/node.c +++ b/src/node.c @@ -214,7 +214,7 @@ pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data) { } // Add new data to data list -void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { +int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { print_debug(">> Adding data to the data list."); // If id is the same as this node's id then we only update seqno if(id == NODE_ID) { @@ -224,7 +224,7 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { node_data->seqno = seqno ^ 1; } - return; + return 1; } // Copy data @@ -236,7 +236,7 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { data_list->data = (void*) new_data; data_list->next = NULL; - return; + return 2; } // Find correct position for new data @@ -257,7 +257,7 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { data_list->data = (void*) new_data; data_list->next = tmp; - return; + return 2; } // Else, we update the last node @@ -278,13 +278,13 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // Free old data free(cur_data); - return; + return 2; } // seqno is smaller so the new data allocated is freed and nothing else is done free(new_data); - return; + return 2; } // Get next node in list @@ -299,6 +299,7 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { new_node->data = (void*) new_data; new_node->next = NULL; last->next = new_node; + return 3; } /* ---- Fin fonctions utilitaires ---- */ @@ -723,6 +724,11 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack } int add_message(char * message, int message_len){ + int seqno = 0; + int rc = add_data(message_len, NODE_ID ,seqno, message); + if (rc > 0) { + print_debug(">> Message added."); + } return 0; } // We then look at the differents TLVs in the packet. @@ -925,8 +931,10 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s if (DEBUG_LEVEL > 0) { printf("\n\t %s \n", (char *) cur_tlv.node_state->data); } - add_data(cur_tlv.node_state->length - 26, cur_tlv.node_state->node_id, cur_tlv.node_state->seqno, cur_tlv.node_state->data); - + int rc = add_data(cur_tlv.node_state->length - 26, cur_tlv.node_state->node_id, cur_tlv.node_state->seqno, cur_tlv.node_state->data); + if (rc < 0) { + print_debug(">> Error while adding note state !"); + } // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; diff --git a/src/node.h b/src/node.h index 20012f1..a45a61e 100644 --- a/src/node.h +++ b/src/node.h @@ -62,7 +62,7 @@ typedef struct list { // The adress of the main peer #define ROOT_PEER_ADDR "2001:660:3301:9200::51c2:1b9b" - + // fonctions signatures int listen_for_packets(char * received_data_buffer, int received_data_len, struct sockaddr_in6 * sender, int sock_fd); @@ -136,6 +136,6 @@ pub_data *get_data(int64_t id); pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data); // add new data to data list -void add_data(unsigned char len, int64_t id, int16_t seqno, char *data); +int add_data(unsigned char len, int64_t id, int16_t seqno, char *data); #endif From 956b249259bb837e260a4f01662cbdaa7160b87e Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 28 Apr 2020 18:37:21 +0200 Subject: [PATCH 02/25] Added a new way to add data to a list of data --- src/node.c | 64 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/src/node.c b/src/node.c index c2ef9aa..4205132 100644 --- a/src/node.c +++ b/src/node.c @@ -217,15 +217,55 @@ pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data) { int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { print_debug(">> Adding data to the data list."); // If id is the same as this node's id then we only update seqno + if(id == NODE_ID) { - pub_data *node_data = get_data(NODE_ID); + // pub_data *node_data = get_data(NODE_ID); + // + // if (node_data == NULL) { + // if (datagram == NULL) { + // data_list = (list*) malloc(sizeof(list)); + // } + // data_list->data = (void*) new_data; + // data_list->next = NULL; + // } + // + // if(seqno >= node_data->seqno) { + // node_data->seqno = seqno ^ 1; + // } + // + // return 1; - if(seqno >= node_data->seqno) { - node_data->seqno = seqno ^ 1; - } + // We create our pub_data. + pub_data * message = malloc(sizeof(struct pub_data)); + message->length = len; + message->id = id; + message->seqno = seqno; + message->data = data; - return 1; - } + // If the data list has never been used, or is empty ( same thing ) + if (data_list = NULL) { + data_list = (list*) malloc(sizeof(list)); + } else { + // Otherwise, we move until the last element of the dala_list, + // and add or data there. + + // We use a temporary address to avoid writing to the static list. + // Seems weird but ok. + list *tmp = data_list; + while(tmp->next != NULL){ + tmp = tmp->next; + } + + // We create the next node of the linked list. + list * new_node = malloc(sizeof(struct list)); + new_node->data = (void *) message; + + // Adding the message to the list. + tmp->next = (void *) new_node; + } + } else { + + } // Copy data pub_data *new_data = copy_data(len, id, seqno, data); @@ -266,7 +306,7 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { new_node->next = tmp; last->next = new_node; - return; + return 1; } else if(id == cur_id) { // If data already exists for this id then we update it if it's seqno is greater than the one stored pub_data *cur_data = (pub_data*) tmp->data; @@ -724,13 +764,15 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack } int add_message(char * message, int message_len){ - int seqno = 0; - int rc = add_data(message_len, NODE_ID ,seqno, message); + int seqno = 1337; + // int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { + int rc = add_data((unsigned char) message_len, (int64_t) NODE_ID ,(int16_t) seqno, message); if (rc > 0) { print_debug(">> Message added."); } return 0; } + // We then look at the differents TLVs in the packet. int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *sender, int socket_num){ int16_t packet_len = ((packet*) data)->length; @@ -825,13 +867,13 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s if (DEBUG_LEVEL > 1) { printf("\x1b[31m[DEBUG]\x1b[0m >> Our hash : "); - for(int x = 0; x < SHA256_DIGEST_LENGTH; x++){ + for(int x = 0; x < 16; x++){ printf("%02x", hash[x]); fflush(0); } printf("\n"); printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); - for(int x = 0; x < SHA256_DIGEST_LENGTH; x++){ + for(int x = 0; x < 16; x++){ printf("%02x", cur_tlv.network_hash->network_hash[x]); fflush(0); } From 88e15dea3aed09771ebd8f8b652af75f818bf8df Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 28 Apr 2020 19:36:30 +0200 Subject: [PATCH 03/25] messages are now added to the data_list --- src/node.c | 202 ++++++++++++++++++++++++----------------------------- 1 file changed, 92 insertions(+), 110 deletions(-) diff --git a/src/node.c b/src/node.c index 4205132..6aacb35 100644 --- a/src/node.c +++ b/src/node.c @@ -219,22 +219,6 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // If id is the same as this node's id then we only update seqno if(id == NODE_ID) { - // pub_data *node_data = get_data(NODE_ID); - // - // if (node_data == NULL) { - // if (datagram == NULL) { - // data_list = (list*) malloc(sizeof(list)); - // } - // data_list->data = (void*) new_data; - // data_list->next = NULL; - // } - // - // if(seqno >= node_data->seqno) { - // node_data->seqno = seqno ^ 1; - // } - // - // return 1; - // We create our pub_data. pub_data * message = malloc(sizeof(struct pub_data)); message->length = len; @@ -243,103 +227,101 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { message->data = data; // If the data list has never been used, or is empty ( same thing ) - if (data_list = NULL) { - data_list = (list*) malloc(sizeof(list)); - } else { - // Otherwise, we move until the last element of the dala_list, - // and add or data there. - - // We use a temporary address to avoid writing to the static list. - // Seems weird but ok. - list *tmp = data_list; - while(tmp->next != NULL){ - tmp = tmp->next; - } - - // We create the next node of the linked list. - list * new_node = malloc(sizeof(struct list)); - new_node->data = (void *) message; - - // Adding the message to the list. - tmp->next = (void *) new_node; + if (data_list == NULL) { + data_list = (list*) malloc(sizeof(struct list)); + } + // we move until the last element of the dala_list, + // and add or data there. + // We use a temporary address to avoid writing to the static list. + // Seems weird but ok. + list *tmp = data_list; + while(tmp->next != NULL){ + tmp = tmp->next; } - } else { + // We create the next node of the linked list. + list * new_node = malloc(sizeof(struct list)); + new_node->data = (void *) message; + + // Adding the message to the list. + tmp->next = (void *) new_node; + + return 1; + } else { + // Copy data + pub_data *new_data = copy_data(len, id, seqno, data); + + if(data_list == NULL) { + // Update list + data_list = (list*) malloc(sizeof(list)); + data_list->data = (void*) new_data; + data_list->next = NULL; + + return 2; + } + + // Find correct position for new data + list *tmp = data_list; + list *last = NULL; + list *new_node; + int64_t cur_id; + + while(tmp != NULL) { + cur_id = ((pub_data*) tmp->data)->id; + + // If id is smaller than cur_id then the new data has to be added at this position + if(id < cur_id) { + // If last hasn't been set then the new data becomes the head of the list + if(last == NULL) { + // Update list + data_list = (list*) malloc(sizeof(list)); + data_list->data = (void*) new_data; + data_list->next = tmp; + + return 2; + } + + // Else, we update the last node + new_node = (list*) malloc(sizeof(list)); + new_node->data = (void*) new_data; + new_node->next = tmp; + last->next = new_node; + + return 1; + } else if(id == cur_id) { + // If data already exists for this id then we update it if it's seqno is greater than the one stored + pub_data *cur_data = (pub_data*) tmp->data; + + if(seqno > cur_data->seqno) { + // Updata data + tmp->data = (void*) new_data; + + // Free old data + free(cur_data); + + return 2; + } + + // seqno is smaller so the new data allocated is freed and nothing else is done + free(new_data); + + return 2; + } + + // Get next node in list + last = tmp; + tmp = tmp->next; + } + + // If no correct position was found then the new data has to be added at the end of the list + + // Update list + new_node = (list*) malloc(sizeof(list)); + new_node->data = (void*) new_data; + new_node->next = NULL; + last->next = new_node; + return 3; } - - // Copy data - pub_data *new_data = copy_data(len, id, seqno, data); - - if(data_list == NULL) { - // Update list - data_list = (list*) malloc(sizeof(list)); - data_list->data = (void*) new_data; - data_list->next = NULL; - - return 2; - } - - // Find correct position for new data - list *tmp = data_list; - list *last = NULL; - list *new_node; - int64_t cur_id; - - while(tmp != NULL) { - cur_id = ((pub_data*) tmp->data)->id; - - // If id is smaller than cur_id then the new data has to be added at this position - if(id < cur_id) { - // If last hasn't been set then the new data becomes the head of the list - if(last == NULL) { - // Update list - data_list = (list*) malloc(sizeof(list)); - data_list->data = (void*) new_data; - data_list->next = tmp; - - return 2; - } - - // Else, we update the last node - new_node = (list*) malloc(sizeof(list)); - new_node->data = (void*) new_data; - new_node->next = tmp; - last->next = new_node; - - return 1; - } else if(id == cur_id) { - // If data already exists for this id then we update it if it's seqno is greater than the one stored - pub_data *cur_data = (pub_data*) tmp->data; - - if(seqno > cur_data->seqno) { - // Updata data - tmp->data = (void*) new_data; - - // Free old data - free(cur_data); - - return 2; - } - - // seqno is smaller so the new data allocated is freed and nothing else is done - free(new_data); - - return 2; - } - - // Get next node in list - last = tmp; - tmp = tmp->next; - } - - // If no correct position was found then the new data has to be added at the end of the list - - // Update list - new_node = (list*) malloc(sizeof(list)); - new_node->data = (void*) new_data; - new_node->next = NULL; - last->next = new_node; - return 3; } /* ---- Fin fonctions utilitaires ---- */ From fae758e4dd50240d985e55e52a21acedc064bb31 Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 28 Apr 2020 19:51:53 +0200 Subject: [PATCH 04/25] Fixed adding message to data list before receiving messages --- src/node.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/node.c b/src/node.c index 7c8594f..d8c4dec 100644 --- a/src/node.c +++ b/src/node.c @@ -229,23 +229,29 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // If the data list has never been used, or is empty ( same thing ) if (data_list == NULL) { data_list = (list*) malloc(sizeof(struct list)); + + list *tmp = data_list; + // We create the next node of the linked list. + tmp->data = (void *) message; + tmp->next = NULL; + + } else { + // we move until the last element of the dala_list, + // and add or data there. + // We use a temporary address to avoid writing to the static list. + // Seems weird but ok. + list *tmp = data_list; + while(tmp->next != NULL){ + tmp = tmp->next; + } + + // We create the next node of the linked list. + list * new_node = malloc(sizeof(struct list)); + new_node->data = (void *) message; + + // Adding the message to the list. + tmp->next = (void *) new_node; } - // we move until the last element of the dala_list, - // and add or data there. - // We use a temporary address to avoid writing to the static list. - // Seems weird but ok. - list *tmp = data_list; - while(tmp->next != NULL){ - tmp = tmp->next; - } - - // We create the next node of the linked list. - list * new_node = malloc(sizeof(struct list)); - new_node->data = (void *) message; - - // Adding the message to the list. - tmp->next = (void *) new_node; - return 1; } else { // Copy data From 70ca7f5f109a5d10569022b7c89a9fd3ff597c0c Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 28 Apr 2020 20:12:59 +0200 Subject: [PATCH 05/25] Removec old comment --- src/node.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/node.c b/src/node.c index d8c4dec..b71cf1e 100644 --- a/src/node.c +++ b/src/node.c @@ -234,7 +234,7 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // We create the next node of the linked list. tmp->data = (void *) message; tmp->next = NULL; - + } else { // we move until the last element of the dala_list, // and add or data there. @@ -755,7 +755,6 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack int add_message(char * message, int message_len){ int seqno = 1337; - // int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { int rc = add_data((unsigned char) message_len, (int64_t) NODE_ID ,(int16_t) seqno, message); if (rc > 0) { print_debug(">> Message added."); From 904f0fcc793a656c7928e8f3c2e11a24f660483c Mon Sep 17 00:00:00 2001 From: gonzalef Date: Tue, 28 Apr 2020 20:22:53 +0200 Subject: [PATCH 06/25] big endian/little endian conversions --- src/node.c | 10 +++++----- src/tlv.c | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/node.c b/src/node.c index d8c4dec..e288341 100644 --- a/src/node.c +++ b/src/node.c @@ -836,7 +836,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s memset(&new_neighbour, 0, sizeof(new_neighbour)); new_neighbour.sin6_family = AF_INET6; memcpy(&new_neighbour.sin6_addr, &cur_tlv.neighbour->ip, 16); - new_neighbour.sin6_port = htons(LISTEN_PORT); + new_neighbour.sin6_port = ntohs(cur_tlv.neighbour->port); new_neighbour.sin6_scope_id = ifindex; // Build network hash @@ -907,7 +907,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s //if the hashes are identical nothing has to be done print_debug(">> Received node hash, updating message entry..."); cur_tlv.node_hash = (node_hash*) (data + pos); - pdata = get_data(cur_tlv.node_hash->node_id); + pdata = get_data(ntohl(cur_tlv.node_hash->node_id)); // If data is found for this id then we check that both hashes are the same if(pdata != NULL) { @@ -926,7 +926,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s } // If no pub_data was found or the hashes differ then we send a node state request - build_node_state_req(&new_tlv, cur_tlv.node_hash->node_id); + build_node_state_req(&new_tlv, ntohl(cur_tlv.node_hash->node_id)); add_tlv(&pack, &new_tlv, sender, socket_num); // The position is updated @@ -940,7 +940,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s // if no pub_data exists for this id nothing is sent print_debug(">> Received node state request. Processing..."); cur_tlv.node_state_req = (node_state_req*) (data + pos); - pdata = get_data(cur_tlv.node_state_req->node_id); + pdata = get_data(ntohl(cur_tlv.node_state_req->node_id)); if(pdata != NULL) { build_node_state(&new_tlv, pdata->id, pdata->seqno, pdata->data, pdata->length); @@ -963,7 +963,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s if (DEBUG_LEVEL > 0) { printf("\n\t %s \n", (char *) cur_tlv.node_state->data); } - int rc = add_data(cur_tlv.node_state->length - 26, cur_tlv.node_state->node_id, cur_tlv.node_state->seqno, cur_tlv.node_state->data); + int rc = add_data(cur_tlv.node_state->length - 26, ntohl(cur_tlv.node_state->node_id), ntohs(cur_tlv.node_state->seqno), cur_tlv.node_state->data); if (rc < 0) { print_debug(">> Error while adding note state !"); } diff --git a/src/tlv.c b/src/tlv.c index 140a9fb..70207a7 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -145,8 +145,8 @@ int build_node_hash(tlv *tlv, int64_t node_id, int16_t seqno, char *data) { new->type = 6; new->length = 26; - new->node_id = node_id; - new->seqno = seqno; + new->node_id = htonl(node_id); + new->seqno = htons(seqno); pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; hash_data(&pdata, (unsigned char*) new->node_hash); @@ -167,7 +167,7 @@ int build_node_state_req(tlv *tlv, int64_t node_id) { new->type = 7; new->length = 8; - new->node_id = node_id; + new->node_id = htonl(node_id); tlv->node_state_req = new; @@ -192,8 +192,8 @@ int build_node_state(tlv *tlv, int64_t node_id, int16_t seqno, char *data, size_ new->type = 8; new->length = 26 + len; - new->node_id = node_id; - new->seqno = seqno; + new->node_id = htonl(node_id); + new->seqno = htons(seqno); memcpy(new->data, data, len); pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; From 055ecfd0ba3760a67141c476e1733490796a0bc1 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 14:30:05 +0200 Subject: [PATCH 07/25] Revert "Merge branch 'master' into add-message" This reverts commit 8da14978d6a41dc520964b9f310851ecdc478a26, reversing changes made to 704ed2a5239cf28cae8c84ed1ca212511734119d. --- .gitignore | 3 --- src/node.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++---- src/node.h | 2 -- 3 files changed, 53 insertions(+), 9 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 2aad53d..0000000 --- a/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -dazibao -*.o -debug.* diff --git a/src/node.c b/src/node.c index 40d52fb..41e648c 100644 --- a/src/node.c +++ b/src/node.c @@ -21,10 +21,59 @@ static list *neighbour_list; /* ---- Fonctions utilitaires ---- */ -void debug_print(char message_debug){ - if (debug_flag == 1) { - printf("\x1b[33m\x1b[4m>> Debug :\x1b[0m\x1b[33m %s\x1b[0m\n", message_debug ); - } +// Looks for more peers +int ask_for_peers(int socket_num) { + print_debug(">> Asking for more peers..."); + // Only ask for more peers if the neighbour list is small enough + int nbr_peers = len_list(neighbour_list); + if( nbr_peers >= 5){ + return 0; + } else if (nbr_peers <= 0){ + print_debug(">> No peers found in the peer list, something terrible happened."); + return -1; + } else { + + // Get random peer + neighbour_peer *peer = get_random_neighbour(); + struct in6_addr ip = peer->ip; + int16_t port = peer->port; + + /*int ifindex = if_nametoindex("enp3s0"); + if(ifindex == 0) { + int ifindex = if_nametoindex("eth0"); + if(ifindex == 0) { + perror("if_nametoindex failed"); + return -1; + } + }*/ + + int ifindex = 0; + + // Initialize sockaddr + struct sockaddr_in6 dest; + memset(&dest, 0, sizeof(struct sockaddr_in6)); + dest.sin6_family = AF_INET6; + memcpy(&dest.sin6_addr, &ip, 16); + dest.sin6_port = htons(port); + dest.sin6_scope_id = ifindex; + + // Send neighbour request TLV + tlv neighbour_req; + neighbour_req.pad1 = NULL; + int rc = build_neighbour_req(&neighbour_req); + if (rc < 0) { + print_debug(">> Failed to build neighbour_req"); + } + + rc = send_single_tlv(&neighbour_req, &dest, socket_num); + if (rc < 0) { + print_debug(">> Error while sending a TLV."); + return -1; + } else { + print_debug(">> Send TLV."); + return 0; + } + } } // Get list length diff --git a/src/node.h b/src/node.h index 444ca0c..a45a61e 100644 --- a/src/node.h +++ b/src/node.h @@ -121,8 +121,6 @@ int bootstrap_node(int * sock_fd); // Helper functions int len_list(list *l); -void debug_print(char message_debug); - neighbour_peer *get_random_neighbour(); // Search for this peer in the neighbour table From ba0cf32cb00435c809aad24ab3caefcc762c26de Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 16:17:22 +0200 Subject: [PATCH 08/25] Added new print_error function Added checks for NULL values Added comments and quesitons as // TODO --- src/debug.c | 7 +++++ src/debug.h | 1 + src/hash.c | 25 +++++++++++---- src/node.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++------- src/tlv.c | 10 ++++-- 5 files changed, 114 insertions(+), 20 deletions(-) diff --git a/src/debug.c b/src/debug.c index bf3f3e8..e00c265 100644 --- a/src/debug.c +++ b/src/debug.c @@ -9,3 +9,10 @@ void print_debug(char * msg){ getchar(); } } + +void print_error(char * msg){ + printf("\x1b[41m[97m[ERROR] %s \x1b[0m\n", msg); + if (DEBUG_LEVEL > 1) { + getchar(); + } +} diff --git a/src/debug.h b/src/debug.h index bf199d7..33e51db 100644 --- a/src/debug.h +++ b/src/debug.h @@ -5,4 +5,5 @@ void print_debug(char * msg); +void print_error(char * msg); #endif diff --git a/src/hash.c b/src/hash.c index a205bad..a3665a0 100644 --- a/src/hash.c +++ b/src/hash.c @@ -1,4 +1,5 @@ #include "hash.h" +#include "debug.h" // Hash a single data void hash_data(pub_data *data, unsigned char *buf) { @@ -34,7 +35,9 @@ void hash_network(list *data_list, unsigned char *buf) { } // Hash all of concat to obtain the network hash - SHA256(concat, totlen, hash); + if (SHA256(concat, totlen, hash) == NULL) { + print_debug(">> Doing the hash failed : function return NULL, should return a pointer."); + }; // Put truncated hash into buf hash_trunc(hash, buf); @@ -46,17 +49,27 @@ void hash_network(list *data_list, unsigned char *buf) { // Truncate 32 octet hash to 16 octets void hash_trunc(unsigned char *hash32oct, unsigned char *buf) { // Copy the first 16 octets from hash32oct - memcpy(buf, hash32oct, 16); + if (memcpy(buf, hash32oct, 16) == NULL) { + print_debug(">> Truncating the hashs didn't work !"); + } } // Concat all fields of data and put them in buf void concat_data(pub_data *data, unsigned char *buf) { - memcpy(buf, &(data->id), 8); - memcpy(buf+8, &(data->seqno), 2); - memcpy(buf+10, data->data, data->length); + if (memcpy(buf, &(data->id), 8) == NULL) { + print_debug(">> Concat the data (id) didn't work !"); + }; + if (memcpy(buf+8, &(data->seqno), 2) == NULL) { + print_debug(">> concat the data (seqno) didn't work !"); + } + if (memcpy(buf+10, data->data, data->length) == NULL) { + print_debug(">> Contact the data (data) didn't work !"); + } } // Concat hash2 to hash1 (hash1 is modified) void concat_hash(unsigned char *hash1, unsigned char *hash2, size_t size) { - memcpy(hash1+size, hash2, 16); + if(memcpy(hash1+size, hash2, 16) == NULL){ + print_debug(">> Concat the hash didn't work !"); + } } diff --git a/src/node.c b/src/node.c index 41e648c..809b4b2 100644 --- a/src/node.c +++ b/src/node.c @@ -204,11 +204,19 @@ pub_data *get_data(int64_t id) { pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data) { pub_data *new_data = (pub_data*) malloc(sizeof(pub_data)); char *_data = (char*) malloc(len); + if (_data == NULL) { + print_error("Failed to allocate memory for copying the data !"); + return NULL; + } new_data->length = len; new_data->id = id; new_data->seqno = seqno; new_data->data = _data; - memcpy(_data, data, len); + if(memcpy(_data, data, len) == NULL){ + print_error("Failed to copy data !"); + return NULL; + } + return new_data; } @@ -221,6 +229,11 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { if(id == NODE_ID) { // We create our pub_data. pub_data * message = malloc(sizeof(struct pub_data)); + if (message == NULL) { + print_error("Failed to allocate memory for the message !"); + return -1; + } + message->length = len; message->id = id; message->seqno = seqno; @@ -229,7 +242,10 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // If the data list has never been used, or is empty ( same thing ) if (data_list == NULL) { data_list = (list*) malloc(sizeof(struct list)); - + if (data_list == NULL) { + print_error("Failed to allocate memory to create the data_list !"); + return -1; + } list *tmp = data_list; // We create the next node of the linked list. tmp->data = (void *) message; @@ -247,19 +263,33 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // We create the next node of the linked list. list * new_node = malloc(sizeof(struct list)); + if (new_node == NULL) { + print_error("Failed to allocate memory for the new node in data list."); + return -1; + } new_node->data = (void *) message; + new_node->next = NULL; // Adding the message to the list. tmp->next = (void *) new_node; + } return 1; } else { // Copy data pub_data *new_data = copy_data(len, id, seqno, data); + if (new_data == NULL) { + print_error("Failed to copy data to new_data !"); + return -1; + } if(data_list == NULL) { // Update list data_list = (list*) malloc(sizeof(list)); + if (data_list == NULL) { + print_error("Failed to allocate memory to create the data_list !"); + return -1; + } data_list->data = (void*) new_data; data_list->next = NULL; @@ -280,7 +310,11 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // If last hasn't been set then the new data becomes the head of the list if(last == NULL) { // Update list - data_list = (list*) malloc(sizeof(list)); + data_list = (list*) malloc(sizeof(struct list)); + if (data_list == NULL) { + print_error("Failed to allocate memory to create the data_list !"); + return -1; + } data_list->data = (void*) new_data; data_list->next = tmp; @@ -288,7 +322,11 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { } // Else, we update the last node - new_node = (list*) malloc(sizeof(list)); + new_node = (list*) malloc(sizeof(struct list)); + if (new_node == NULL) { + print_error("Failed to allocate memory for the new node in data list."); + return -1; + } new_node->data = (void*) new_data; new_node->next = tmp; last->next = new_node; @@ -323,6 +361,10 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { // Update list new_node = (list*) malloc(sizeof(list)); + if (new_node == NULL) { + print_error("Failed to allocate memory for the new node in data list."); + return -1; + } new_node->data = (void*) new_data; new_node->next = NULL; last->next = new_node; @@ -419,6 +461,8 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { sent = 1; } } else { + // In case we need to add a padding packet, and the packet is full, + // we just ignore that padding, and send out the packet. if(pack->length >= 1020) { errval = send_packet((char*) pack, pack->length, dest, socket_num); *pack = (packet) {.magic = 95, .version = 1, .length = 0}; @@ -480,9 +524,10 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { print_debug(">> Finished adding the TLVs to the packet"); - // If the previous packet was went return 1 or -1 if there was an error sending it - if(sent) + // If the previous packet was sent return 1 or -1 if there was an error sending it + if(sent){ return errval? -1:1; + } // Return 0 if the TLV was added to the packet return 0; @@ -774,14 +819,23 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s int pos = 4; unsigned char tlv_len, hash[16]; char warn[32]; - tlv new_tlv, cur_tlv; + + // The TLV we are going to send back. + tlv new_tlv; + // The tlv we are currently looking at. + tlv cur_tlv; + new_tlv.pad1 = NULL; cur_tlv.pad1 = NULL; list *tmp_list; pub_data *pdata; + + // holds the random neighbour we send back in case of a neighbour_req. struct neighbour_peer *random_neighbour; + // Holds the new neighbour send to us by a "neighbour tlv" struct sockaddr_in6 new_neighbour; + // We create the packet in which we are going to send back our responses. packet pack = (packet) {.magic = 95, .version = 1, .length = 0}; memset(pack.body, 0, 1020); @@ -797,8 +851,8 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s */ int ifindex = 0; - ifindex = 0; while(pos < total_packet_len) { + // Making sure the current TLV we are looking at is valid. switch(validate_tlv(data, pos, total_packet_len)) { case 0: // We received a padding tlv so it is ignored @@ -818,7 +872,15 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s print_debug(">> Received neighbour request, sending out a neighbour address."); // Send a neighbour tlv random_neighbour = get_random_neighbour(); + if (random_neighbour == NULL) { + print_debug(">> Failed to get a random neighbour, failing..."); + return -1; + } + + // TODO : Seems to be a bug here, as this frees the new_tlv build_neighbour(&new_tlv, random_neighbour->ip, random_neighbour->port); + // TODO : I suppose that since new_tlv is freed above, add_tlv ads an empty + // value to the packet ? add_tlv(&pack, &new_tlv, sender, socket_num); // The position is updated @@ -938,7 +1000,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s // so a node state tlv for this node id has to be sent, // if no pub_data exists for this id nothing is sent print_debug(">> Received node state request. Processing..."); - cur_tlv.node_state_req = (node_state_req*) (data + pos); + cur_tlv.node_state_req = (node_state_req*) (data[pos]); pdata = get_data(ntohl(cur_tlv.node_state_req->node_id)); if(pdata != NULL) { @@ -960,11 +1022,15 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s print_debug(">> Received message ! "); if (DEBUG_LEVEL > 0) { - printf("\n\t %s \n", (char *) cur_tlv.node_state->data); + if (cur_tlv.node_state->data == NULL) { + print_error("The data in the current node is NULL !"); + return -1; + } + printf("\x1b[31m[DEBUG]\x1b[0m >> “%s”\n", cur_tlv.node_state->data); } int rc = add_data(cur_tlv.node_state->length - 26, ntohl(cur_tlv.node_state->node_id), ntohs(cur_tlv.node_state->seqno), cur_tlv.node_state->data); if (rc < 0) { - print_debug(">> Error while adding note state !"); + print_debug(">> Error while adding node state !"); } // The position is updated tlv_len = data[pos+1]; @@ -1172,6 +1238,9 @@ int run_node(int sock_fd){ } } } else { + // TODO : Here, we can write all of the current messages we have in stack + // to a file, or to stdout. + // TODO : Same as above, but for the peers have know about. continue; } diff --git a/src/tlv.c b/src/tlv.c index 70207a7..ab66f6a 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -1,5 +1,6 @@ #include "tlv.h" +// TODO this can be deleted ? // creer un tlv int build_tlv(tlv *tlv, cmd_token token) { switch(token.type) { @@ -25,6 +26,7 @@ int build_tlv(tlv *tlv, cmd_token token) { return -1; } +// end deletion int build_pad1(tlv *tlv) { // Free the previously allocated memory @@ -62,9 +64,7 @@ int build_padn(tlv *tlv, size_t len) { int build_neighbour_req(tlv *tlv) { // Free the previously allocated memory - // if (tlv != NULL) { - // free(tlv->pad1); - // } + free(tlv->pad1); neighbour_req *new = (neighbour_req*) malloc(sizeof(neighbour_req)); @@ -82,6 +82,10 @@ int build_neighbour_req(tlv *tlv) { int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port) { // Free the previously allocated memory + // TODO : why do we free this part ? Doesn't it mean that, once the + // tlv has entered this function, it's not on the heap anymore, + // and thus, setting a value will not be accessible from other part of the + // code ? free(tlv->pad1); neighbour *new = (neighbour*) malloc(sizeof(neighbour)); From ab1f278685b34ac305ed41a5593e6bb8ebcbc6ad Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 16:19:05 +0200 Subject: [PATCH 09/25] Added another print_error --- src/node.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node.c b/src/node.c index 809b4b2..e8b3e31 100644 --- a/src/node.c +++ b/src/node.c @@ -1030,7 +1030,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s } int rc = add_data(cur_tlv.node_state->length - 26, ntohl(cur_tlv.node_state->node_id), ntohs(cur_tlv.node_state->seqno), cur_tlv.node_state->data); if (rc < 0) { - print_debug(">> Error while adding node state !"); + print_error("Error while adding node state !"); } // The position is updated tlv_len = data[pos+1]; From 72b00cb07d0b99f567c36b5c1da31fa476466e33 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 16:40:01 +0200 Subject: [PATCH 10/25] Added printing the peer list --- src/debug.c | 41 ++++++++++++++++++++++++++++++++++++++++- src/debug.h | 6 ++++++ src/node.c | 4 ++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/debug.c b/src/debug.c index e00c265..1c46f4e 100644 --- a/src/debug.c +++ b/src/debug.c @@ -1,11 +1,13 @@ #include #include "debug.h" +#include "node.h" +#include void print_debug(char * msg){ if (DEBUG_LEVEL > 0) { printf("\x1b[31m[DEBUG]\x1b[0m %s \n", msg); } - if (DEBUG_LEVEL > 2) { + if (DEBUG_LEVEL > 9) { getchar(); } } @@ -16,3 +18,40 @@ void print_error(char * msg){ getchar(); } } + +// typedef struct neighbour_peer { +// struct in6_addr ip; +// int16_t port; +// char is_temporary; +// time_t last_seen; +// } neighbour_peer; + +void print_peers(list * l){ + // Print out the peers we have in the neighbour_list + int nbr_peers = 0; + if (l == NULL) { + print_error("The neighbour_list is empty !"); + } else { + print_debug(">> Printing out peer list :"); + while(l != NULL){ + neighbour_peer * peer = (neighbour_peer *) l->data; + char * buff_str_ip[1024]; + char * ip_str = (char * ) inet_ntop(AF_INET6,&peer->ip,(char * restrict) buff_str_ip, 1024); + int last_seen = time(NULL) - peer->last_seen; + printf("\x1b[31m[DEBUG]\x1b[0m >> %s @ %i | is temporary ? %s | last seen %i secs ago. |\n", ip_str, peer->port, peer->is_temporary ? "yes":"no", last_seen); + nbr_peers++; + if (l->next == NULL) { + break; + } else { + l = l->next; + } + + } + printf("\x1b[31m[DEBUG]\x1b[0m >> Found %i peers.\n", nbr_peers); + print_debug(">> Finished printing peer list."); + } +} + +void print_data(list * l){ + +} diff --git a/src/debug.h b/src/debug.h index 33e51db..a8f01d6 100644 --- a/src/debug.h +++ b/src/debug.h @@ -1,9 +1,15 @@ #ifndef DEBUG_H #define DEBUG_H +#include "node.h" + #define DEBUG_LEVEL 2 void print_debug(char * msg); void print_error(char * msg); + +void print_peers(list * l); + +void print_data(list * l); #endif diff --git a/src/node.c b/src/node.c index e8b3e31..6960057 100644 --- a/src/node.c +++ b/src/node.c @@ -24,9 +24,12 @@ static list *neighbour_list; // Looks for more peers int ask_for_peers(int socket_num) { print_debug(">> Asking for more peers..."); + // Print out the current peer list. + // Only ask for more peers if the neighbour list is small enough int nbr_peers = len_list(neighbour_list); if( nbr_peers >= 5){ + print_debug(">> We have enough peers, skipping..."); return 0; } else if (nbr_peers <= 0){ print_debug(">> No peers found in the peer list, something terrible happened."); @@ -1113,6 +1116,7 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc } int t_ask_for_more_peers(int sock_fd){ + print_peers(neighbour_list); return ask_for_peers(sock_fd); } From c19f5e7c3c2d93daccdd5fac8d7aca68798f7918 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 19:03:46 +0200 Subject: [PATCH 11/25] Trying to fix print_out, but too many memory errors --- src/debug.c | 33 ++++++++++++++++++++++++--------- src/node.c | 20 +++++++++++++++++--- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/debug.c b/src/debug.c index 1c46f4e..524d359 100644 --- a/src/debug.c +++ b/src/debug.c @@ -1,7 +1,10 @@ #include +#include +#include +#include +#include #include "debug.h" #include "node.h" -#include void print_debug(char * msg){ if (DEBUG_LEVEL > 0) { @@ -13,19 +16,12 @@ void print_debug(char * msg){ } void print_error(char * msg){ - printf("\x1b[41m[97m[ERROR] %s \x1b[0m\n", msg); + printf("\x1b[41m\x1b[97m[ERROR] >> %s \x1b[0m\n", msg); if (DEBUG_LEVEL > 1) { getchar(); } } -// typedef struct neighbour_peer { -// struct in6_addr ip; -// int16_t port; -// char is_temporary; -// time_t last_seen; -// } neighbour_peer; - void print_peers(list * l){ // Print out the peers we have in the neighbour_list int nbr_peers = 0; @@ -53,5 +49,24 @@ void print_peers(list * l){ } void print_data(list * l){ + if (l == NULL) { + print_error("Message list is empty !"); + } else { + print_debug(">> Printing messages we know of so far :"); + int nbr_msg = 0; + setlocale(LC_ALL, "en_US.utf8"); + print_debug(">> 🏂🏂🏂🏂 Peer ID | Seqno | Length | Message "); + while(l != NULL){ + pub_data * message = l->data; + printf("\x1b[31m[DEBUG]\x1b[0m >> %li | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data); + nbr_msg++; + if (l->next == NULL) { + break; + } else { + l = l->next; + } + } + printf("\x1b[31m[DEBUG]\x1b[0m >> Found %i messages.\n", nbr_msg); + } } diff --git a/src/node.c b/src/node.c index 6960057..20ad75f 100644 --- a/src/node.c +++ b/src/node.c @@ -9,9 +9,11 @@ #include #include #include +#include #include #include #include +#include #include "node.h" #include "debug.h" @@ -1029,7 +1031,8 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s print_error("The data in the current node is NULL !"); return -1; } - printf("\x1b[31m[DEBUG]\x1b[0m >> “%s”\n", cur_tlv.node_state->data); + printf("%s\n", data + pos ); + printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0”\n", (const wchar_t*) cur_tlv.node_state->data); } int rc = add_data(cur_tlv.node_state->length - 26, ntohl(cur_tlv.node_state->node_id), ntohs(cur_tlv.node_state->seqno), cur_tlv.node_state->data); if (rc < 0) { @@ -1105,7 +1108,7 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc if (nbr_success_tlv < 0){ print_debug(">> Error while treating the TLVs of the packet."); if (DEBUG_LEVEL > 1) { - printf("\x1b[31m[DEBUG]\x1b[0m >> Managed to deal with %i TLVs", -nbr_success_tlv ); + printf("\x1b[31m[DEBUG]\x1b[0m >> Managed to deal with %i TLVs\n", -nbr_success_tlv ); } return -2; } else { @@ -1116,12 +1119,20 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc } int t_ask_for_more_peers(int sock_fd){ - print_peers(neighbour_list); + if (DEBUG_LEVEL > 1) { + print_peers(neighbour_list); + sleep(3); + } return ask_for_peers(sock_fd); } +/* + + +*/ int t_get_network_state(int sock_fd){ print_debug(">> Getting network state..."); + return 0; } @@ -1157,6 +1168,8 @@ int run_node(int sock_fd){ t_ask_for_more_peers(sock_fd); t_update_neighbours(); t_get_network_state(sock_fd); + print_data(data_list); + sleep(3); delay = time(NULL) + 20 + (rand() % 10); } @@ -1303,6 +1316,7 @@ int bootstrap_node(int * sock_fd){ int main(int argc, const char *argv[]) { + setlocale(LC_ALL, ""); print_debug(">> Starting node"); int sock_fd; From 5e8b4fa92858204101c0bfd2c543d6041383bcc9 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Wed, 29 Apr 2020 19:53:19 +0200 Subject: [PATCH 12/25] add message and add data --- src/node.c | 275 ++++++++++++++++++++++++++++------------------------- src/node.h | 22 +++-- src/tlv.c | 4 +- src/tlv.h | 10 +- 4 files changed, 166 insertions(+), 145 deletions(-) diff --git a/src/node.c b/src/node.c index 41e648c..b5e69af 100644 --- a/src/node.c +++ b/src/node.c @@ -1,19 +1,6 @@ // This is the main file of the Dazibao project. It represents the node, and // handles all of the main logic, including the network connexions. -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include "node.h" -#include "debug.h" // Static variables static list *data_list; @@ -201,7 +188,7 @@ pub_data *get_data(int64_t id) { } // Take data as args and create a pub_data structure in the heap -pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data) { +pub_data *copy_data(unsigned char len, int64_t id, uint16_t seqno, char *data) { pub_data *new_data = (pub_data*) malloc(sizeof(pub_data)); char *_data = (char*) malloc(len); new_data->length = len; @@ -213,121 +200,102 @@ pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data) { return new_data; } -// Add new data to data list -int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { - print_debug(">> Adding data to the data list."); - // If id is the same as this node's id then we only update seqno +// A node state TLV was received and either no data associated to it's id is in our data list or the data was updated, return -1 if an error occurend, 0 if nothing had to be done and 1 if something was updated/added +int add_data(unsigned char len, int64_t id, uint16_t seqno, char *data, pub_data *found) { + // Check if it's our own id + if(id == NODE_ID) { + // wtf + if(found == NULL) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Our own node is not in the data list, something went terribly wrong.\n"); + return -1; + } - if(id == NODE_ID) { - // We create our pub_data. - pub_data * message = malloc(sizeof(struct pub_data)); - message->length = len; - message->id = id; - message->seqno = seqno; - message->data = data; + // If seqno is bigger or equals than our stored seqno then update seqno + if( ((seqno - found->seqno) & 32768) == 0 ) { + printf(">> Updating seqno of our own published data.\n"); + found->seqno = (seqno + 1) % (65535); + return 1; + } + + // Else, do nothing + printf(">> Our own seqno didn't need to be updated.\n"); + return 0; + } - // If the data list has never been used, or is empty ( same thing ) - if (data_list == NULL) { - data_list = (list*) malloc(sizeof(struct list)); + // If it's not our own id, update the data if it's already in our data list and seqno is bigger than our stored seqno + if(found != NULL) { + // Check if seqno is smaller or equals to our stored seqno + if( ((found->seqno - seqno) & 32768) == 0 ) { + printf(">> Data received has smaller seqno than stored seqno, nothing has to be done.\n"); + return 0; + } - list *tmp = data_list; - // We create the next node of the linked list. - tmp->data = (void *) message; - tmp->next = NULL; + // Update data + found->length = len; + found->id = id; + found->seqno = seqno; - } else { - // we move until the last element of the dala_list, - // and add or data there. - // We use a temporary address to avoid writing to the static list. - // Seems weird but ok. - list *tmp = data_list; - while(tmp->next != NULL){ - tmp = tmp->next; + // Updata message + free(found->data); + found->data = (char*) malloc(len); + memcpy(found->data, data, len); + + printf(">> Updated %li's published data.\n", id); + + return 1; + } + + // Else, add new data + pub_data *new_data = copy_data(len, id, seqno, data); + + // Find correct position for new data + list *tmp = data_list; + list *last = NULL; + list *new_node; + int64_t cur_id; + + while(tmp != NULL) { + cur_id = ((pub_data*) tmp->data)->id; + + // If id is smaller than cur_id then the new data has to be added at this position + if(id < cur_id) { + // If last hasn't been set then the new data becomes the head of the list + if(last == NULL) { + // Update list + data_list = (list*) malloc(sizeof(list)); + data_list->data = (void*) new_data; + data_list->next = tmp; + + printf(">> Added new message to data list.\n"); + + return 1; } - // We create the next node of the linked list. - list * new_node = malloc(sizeof(struct list)); - new_node->data = (void *) message; + // Else, we update the last node + new_node = (list*) malloc(sizeof(list)); + new_node->data = (void*) new_data; + new_node->next = tmp; + last->next = new_node; - // Adding the message to the list. - tmp->next = (void *) new_node; + printf(">> Added new message to data list.\n"); + + return 1; } - return 1; - } else { - // Copy data - pub_data *new_data = copy_data(len, id, seqno, data); - if(data_list == NULL) { - // Update list - data_list = (list*) malloc(sizeof(list)); - data_list->data = (void*) new_data; - data_list->next = NULL; - - return 2; - } - - // Find correct position for new data - list *tmp = data_list; - list *last = NULL; - list *new_node; - int64_t cur_id; - - while(tmp != NULL) { - cur_id = ((pub_data*) tmp->data)->id; - - // If id is smaller than cur_id then the new data has to be added at this position - if(id < cur_id) { - // If last hasn't been set then the new data becomes the head of the list - if(last == NULL) { - // Update list - data_list = (list*) malloc(sizeof(list)); - data_list->data = (void*) new_data; - data_list->next = tmp; - - return 2; - } - - // Else, we update the last node - new_node = (list*) malloc(sizeof(list)); - new_node->data = (void*) new_data; - new_node->next = tmp; - last->next = new_node; - - return 1; - } else if(id == cur_id) { - // If data already exists for this id then we update it if it's seqno is greater than the one stored - pub_data *cur_data = (pub_data*) tmp->data; - - if(seqno > cur_data->seqno) { - // Updata data - tmp->data = (void*) new_data; - - // Free old data - free(cur_data); - - return 2; - } - - // seqno is smaller so the new data allocated is freed and nothing else is done - free(new_data); - - return 2; - } - - // Get next node in list - last = tmp; - tmp = tmp->next; - } - - // If no correct position was found then the new data has to be added at the end of the list - - // Update list - new_node = (list*) malloc(sizeof(list)); - new_node->data = (void*) new_data; - new_node->next = NULL; - last->next = new_node; - return 3; + // Get next node in list + last = tmp; + tmp = tmp->next; } + + // If no correct position was found then the new data has to be added at the end of the list + new_node = (list*) malloc(sizeof(list)); + new_node->data = (void*) new_data; + new_node->next = NULL; + last->next = new_node; + + printf(">> Added new message to data list.\n"); + + return 1; } /* ---- Fin fonctions utilitaires ---- */ @@ -489,7 +457,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { } // Send length bytes from packet -int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, int socket_num) { +int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, int socket_num) { ((packet*) packet_buff)->length = htons(((packet*) packet_buff)->length); // Vectorized buffer @@ -595,7 +563,7 @@ int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { return send_packet((char*) &pack, pack.length, dest, socket_num); } -int send_tlv(tlv *tlv_to_send, int16_t tlv_size, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num){ +int send_tlv(tlv *tlv_to_send, uint16_t tlv_size, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num){ print_debug(">> Building packet to send a TLV."); // We first need to build the packet, @@ -662,7 +630,7 @@ int send_tlv(tlv *tlv_to_send, int16_t tlv_size, struct sockaddr_in6 * dest_list // We need to make sure the TLV announces a length that will no go onto // another tlv, as we might end up reading bullshit. -int validate_tlv(char *data, int pos, int16_t packet_len){ +int validate_tlv(char *data, int pos, uint16_t packet_len){ char type = data[pos]; @@ -754,17 +722,32 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack } int add_message(char * message, int message_len){ - int seqno = 1337; - int rc = add_data((unsigned char) message_len, (int64_t) NODE_ID ,(int16_t) seqno, message); - if (rc > 0) { + // Don't update the message if it's empty + if(message_len == 0) + return -1; + + // If not, get our data in the list and update it + pub_data *our_data = get_data(NODE_ID); + + if(our_data != NULL) { + our_data->seqno = (our_data->seqno + 1) % 65535; + our_data->length = message_len; + free(our_data->data); + our_data->data = (char*) malloc(message_len); + memcpy(our_data->data, message, message_len); + print_debug(">> Message added."); + + return 0; } - return 0; + + print_debug(">> Message could not be added because our own ID is not in the data_list, something went wrong."); + return -1; } // We then look at the differents TLVs in the packet. -int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *sender, int socket_num){ - int16_t packet_len = ((packet*) data)->length; +int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *sender, int socket_num){ + uint16_t packet_len = ((packet*) data)->length; if(packet_len != total_packet_len - 4) { print_debug(">> Length indicated in packet differs from real length of packet received, disgarding packet."); @@ -914,7 +897,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s hash_data(pdata, hash); // If both hashes are the same then nothing has to be done - if(memcmp(hash, cur_tlv.node_hash->node_hash, 16) != 0) { + if(memcmp(hash, cur_tlv.node_hash->node_hash, 16) == 0) { // The position is updated tlv_len = data[pos+1]; pos += 2; @@ -962,7 +945,28 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s if (DEBUG_LEVEL > 0) { printf("\n\t %s \n", (char *) cur_tlv.node_state->data); } - int rc = add_data(cur_tlv.node_state->length - 26, ntohl(cur_tlv.node_state->node_id), ntohs(cur_tlv.node_state->seqno), cur_tlv.node_state->data); + + // Compare hashes + pdata = get_data(ntohl(cur_tlv.node_state->node_id)); + + // If data is found for this id then we check that both hashes are the same + if(pdata != NULL) { + // We hash the data stored in the data list + hash_data(pdata, hash); + + // If both hashes are the same then nothing has to be done + if(memcmp(hash, cur_tlv.node_hash->node_hash, 16) == 0) { + // The position is updated + tlv_len = data[pos+1]; + pos += 2; + + break; + } + + } + + // Else, we update the data + int rc = add_data(cur_tlv.node_state->length - 26, ntohl(cur_tlv.node_state->node_id), ntohs(cur_tlv.node_state->seqno), cur_tlv.node_state->data, pdata); if (rc < 0) { print_debug(">> Error while adding note state !"); } @@ -1224,6 +1228,17 @@ int bootstrap_node(int * sock_fd){ neighbour_list->data = (void *) root_peer; neighbour_list->next = NULL; + print_debug(">> Initializing data list..."); + data_list = (list*) malloc(sizeof(list)); + data_list->data = malloc(sizeof(pub_data)); + data_list->next = NULL; + + pub_data *our_data = (pub_data*) data_list->data; + our_data->length = 0; + our_data->id = NODE_ID; + our_data->seqno = 1337; + our_data->data = NULL; + print_debug(">> Boostraping done."); return 0; } diff --git a/src/node.h b/src/node.h index a45a61e..597de2f 100644 --- a/src/node.h +++ b/src/node.h @@ -12,6 +12,12 @@ #include #include #include +#include +#include +#include +#include +#include +#include "debug.h" /* la table de voisins, qui est indexée par adresses de socket (des paires (IP, Port)), * et dont chaque entrée contient un booléen indiquant si le pair est permanent @@ -35,7 +41,7 @@ typedef struct neighbour_peer { typedef struct pub_data { unsigned char length; int64_t id; - int16_t seqno; + uint16_t seqno; char *data; } pub_data; @@ -69,17 +75,17 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc int check_header(char * received_data_buffer, int received_data_len, packet * packet_to_return); -int validate_tlv(char *data, int pos, int16_t packet_len); +int validate_tlv(char *data, int pos, uint16_t packet_len); int update_neighbours(); int ask_for_peers(int socket_num); -int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, int socket_num); +int work_with_tlvs(char * data, uint16_t packet_len, struct sockaddr_in6 *sender, int socket_num); int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num); -int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, int socket_num); +int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, int socket_num); int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num); @@ -89,12 +95,12 @@ int run_node(int sock_fd); /* Takes a TLV and sends it over to everyone in the list of addresses. * Returns -1 in case of error, 0 otherwise. */ -int send_tlv(tlv *tlv_to_send, int16_t length, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num); +int send_tlv(tlv *tlv_to_send, uint16_t length, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num); /* Takes a list of TLV and sends them over to everyone in the list of addresses. * Returns -1 in case of error, 0 otherwise. */ -int send_tlvs(struct list * tlv_list, int16_t length, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num); +int send_tlvs(struct list * tlv_list, uint16_t length, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num); /* Check our peer list. If we have less than 5 peers, send out a TLV NEIGHBOUR_REQUEST to a random peer @@ -133,9 +139,9 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port); pub_data *get_data(int64_t id); // Take data as args and create a pub_data structure in the heap -pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data); +pub_data *copy_data(unsigned char len, int64_t id, uint16_t seqno, char *data); // add new data to data list -int add_data(unsigned char len, int64_t id, int16_t seqno, char *data); +int add_data(unsigned char len, int64_t id, uint16_t seqno, char *data, pub_data *found); #endif diff --git a/src/tlv.c b/src/tlv.c index 70207a7..4c4acd8 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -134,7 +134,7 @@ int build_network_state_req(tlv *tlv) { return 0; } -int build_node_hash(tlv *tlv, int64_t node_id, int16_t seqno, char *data) { +int build_node_hash(tlv *tlv, int64_t node_id, uint16_t seqno, char *data) { // Free the previously allocated memory free(tlv->pad1); @@ -174,7 +174,7 @@ int build_node_state_req(tlv *tlv, int64_t node_id) { return 0; } -int build_node_state(tlv *tlv, int64_t node_id, int16_t seqno, char *data, size_t data_len) { +int build_node_state(tlv *tlv, int64_t node_id, uint16_t seqno, char *data, size_t data_len) { // Free the previously allocated memory free(tlv->pad1); diff --git a/src/tlv.h b/src/tlv.h index d10d3c9..10b4eee 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -19,7 +19,7 @@ typedef struct packet { unsigned char magic; // 95 (si autre, ignorer) unsigned char version; // 1 (si autre, ignorer) - int16_t length; // 1020 max + uint16_t length; // 1020 max char body[1020]; } packet; @@ -67,7 +67,7 @@ typedef struct node_hash { unsigned char type; unsigned char length; int64_t node_id; - int16_t seqno; + uint16_t seqno; char node_hash[16]; } node_hash; @@ -83,7 +83,7 @@ typedef struct node_state { unsigned char type; unsigned char length; int64_t node_id; - int16_t seqno; + uint16_t seqno; char node_hash[16]; char data[192]; } node_state; @@ -122,9 +122,9 @@ int build_neighbour_req(union tlv *tlv); int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port); int build_network_hash(tlv *tlv, list *data_list); int build_network_state_req(tlv *tlv); -int build_node_hash(tlv *tlv, int64_t node_id, int16_t seqno, char *data); +int build_node_hash(tlv *tlv, int64_t node_id, uint16_t seqno, char *data); int build_node_state_req(tlv *tlv, int64_t node_id); -int build_node_state(tlv *tlv, int64_t node_id, int16_t seqno, char *data, size_t data_len); +int build_node_state(tlv *tlv, int64_t node_id, uint16_t seqno, char *data, size_t data_len); int build_warning(tlv *tlv, char *message, size_t message_len); #endif From f9c1cda499628ffe3ce121914f7cead176718611 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 20:19:02 +0200 Subject: [PATCH 13/25] 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) From afa8b4c822086614061225fa52b2141c1374f494 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Wed, 29 Apr 2020 20:25:04 +0200 Subject: [PATCH 14/25] free en commentaire --- src/tlv.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/tlv.c b/src/tlv.c index 4c4acd8..86362cf 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -62,9 +62,7 @@ int build_padn(tlv *tlv, size_t len) { int build_neighbour_req(tlv *tlv) { // Free the previously allocated memory - // if (tlv != NULL) { - // free(tlv->pad1); - // } + free(tlv->pad1); neighbour_req *new = (neighbour_req*) malloc(sizeof(neighbour_req)); From 352aed242d38b5074e6d5ced933edb0ff73a2c86 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Wed, 29 Apr 2020 20:58:56 +0200 Subject: [PATCH 15/25] include --- src/node.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node.h b/src/node.h index 597de2f..62f33a4 100644 --- a/src/node.h +++ b/src/node.h @@ -17,7 +17,6 @@ #include #include #include -#include "debug.h" /* la table de voisins, qui est indexée par adresses de socket (des paires (IP, Port)), * et dont chaque entrée contient un booléen indiquant si le pair est permanent @@ -54,6 +53,7 @@ typedef struct list { #include "tlv.h" #include "hash.h" #include "parser.h" +#include "debug.h" // On which port do we listen to #define LISTEN_PORT 1212 From 0bb80d078c34313377c5cb617de65a743735ca89 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Wed, 29 Apr 2020 22:22:30 +0200 Subject: [PATCH 16/25] int64_t to uint64_t and endianness problem --- src/node.c | 56 +++++++++++++++++++----------------------------------- src/node.h | 12 +++++++----- src/tlv.c | 18 +++++++++--------- src/tlv.h | 12 ++++++------ 4 files changed, 42 insertions(+), 56 deletions(-) diff --git a/src/node.c b/src/node.c index 7319011..6a2518f 100644 --- a/src/node.c +++ b/src/node.c @@ -1,22 +1,6 @@ // This is the main file of the Dazibao project. It represents the node, and // handles all of the main logic, including the network connexions. -<<<<<<< HEAD -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -======= ->>>>>>> add-message + #include "node.h" // Static variables @@ -61,7 +45,7 @@ int ask_for_peers(int socket_num) { memset(&dest, 0, sizeof(struct sockaddr_in6)); dest.sin6_family = AF_INET6; memcpy(&dest.sin6_addr, &ip, 16); - dest.sin6_port = htons(port); + dest.sin6_port = htobe16(port); dest.sin6_scope_id = ifindex; // Send neighbour request TLV @@ -193,7 +177,7 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port) { } // get data associated with id, if it doesn't exist return NULL -pub_data *get_data(int64_t id) { +pub_data *get_data(uint64_t id) { list *tmp = data_list; pub_data *data; @@ -208,7 +192,7 @@ pub_data *get_data(int64_t id) { } // Take data as args and create a pub_data structure in the heap -pub_data *copy_data(unsigned char len, int64_t id, uint16_t seqno, char *data) { +pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data) { pub_data *new_data = (pub_data*) malloc(sizeof(pub_data)); char *_data = (char*) malloc(len); if (_data == NULL) { @@ -229,7 +213,7 @@ pub_data *copy_data(unsigned char len, int64_t id, uint16_t seqno, char *data) { } // A node state TLV was received and either no data associated to it's id is in our data list or the data was updated, return -1 if an error occurend, 0 if nothing had to be done and 1 if something was updated/added -int add_data(unsigned char len, int64_t id, uint16_t seqno, char *data, pub_data *found) { +int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found) { // Check if it's our own id if(id == NODE_ID) { // wtf @@ -280,7 +264,7 @@ int add_data(unsigned char len, int64_t id, uint16_t seqno, char *data, pub_data list *tmp = data_list; list *last = NULL; list *new_node; - int64_t cur_id; + uint64_t cur_id; while(tmp != NULL) { cur_id = ((pub_data*) tmp->data)->id; @@ -489,7 +473,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { // Send length bytes from packet int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, int socket_num) { - ((packet*) packet_buff)->length = htons(((packet*) packet_buff)->length); + ((packet*) packet_buff)->length = htobe16(((packet*) packet_buff)->length); // Vectorized buffer struct iovec vec_buff[1]; @@ -743,7 +727,7 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack } // Convert to hardware order. - ((packet*) packet_to_return)->length = ntohs(((packet*) packet_to_return)->length); + ((packet*) packet_to_return)->length = be16toh(((packet*) packet_to_return)->length); if (packet_to_return->length + 4 < received_data_len ) { print_debug(">> The packet length is bigger than the UDP datagram, which is not possible with the current laws of physics."); return -1; @@ -866,7 +850,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * memset(&new_neighbour, 0, sizeof(new_neighbour)); new_neighbour.sin6_family = AF_INET6; memcpy(&new_neighbour.sin6_addr, &cur_tlv.neighbour->ip, 16); - new_neighbour.sin6_port = ntohs(cur_tlv.neighbour->port); + new_neighbour.sin6_port = be16toh(cur_tlv.neighbour->port); new_neighbour.sin6_scope_id = ifindex; // Build network hash @@ -924,6 +908,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * pdata = (pub_data*) tmp_list->data; build_node_hash(&new_tlv, pdata->id, pdata->seqno, pdata->data); add_tlv(&pack, &new_tlv, sender, socket_num); + tmp_list = tmp_list->next; } // The position is updated @@ -937,7 +922,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * //if the hashes are identical nothing has to be done print_debug(">> Received node hash, updating message entry..."); cur_tlv.node_hash = (node_hash*) (data + pos); - pdata = get_data(ntohl(cur_tlv.node_hash->node_id)); + pdata = get_data(be64toh(cur_tlv.node_hash->node_id)); // If data is found for this id then we check that both hashes are the same if(pdata != NULL) { @@ -956,7 +941,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * } // If no pub_data was found or the hashes differ then we send a node state request - build_node_state_req(&new_tlv, ntohl(cur_tlv.node_hash->node_id)); + build_node_state_req(&new_tlv, be64toh(cur_tlv.node_hash->node_id)); add_tlv(&pack, &new_tlv, sender, socket_num); // The position is updated @@ -969,8 +954,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // so a node state tlv for this node id has to be sent, // if no pub_data exists for this id nothing is sent print_debug(">> Received node state request. Processing..."); - cur_tlv.node_state_req = (node_state_req*) (data[pos]); - pdata = get_data(ntohl(cur_tlv.node_state_req->node_id)); + cur_tlv.node_state_req = (node_state_req*) (data + pos); + pdata = get_data(be64toh(cur_tlv.node_state_req->node_id)); if(pdata != NULL) { build_node_state(&new_tlv, pdata->id, pdata->seqno, pdata->data, pdata->length); @@ -995,12 +980,11 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * print_error("The data in the current node is NULL !"); return -1; } - printf("%s\n", data + pos ); printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0”\n", (const wchar_t*) cur_tlv.node_state->data); } // Compare hashes - pdata = get_data(ntohl(cur_tlv.node_state->node_id)); + pdata = get_data(be64toh(cur_tlv.node_state->node_id)); // If data is found for this id then we check that both hashes are the same if(pdata != NULL) { @@ -1008,7 +992,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * hash_data(pdata, hash); // If both hashes are the same then nothing has to be done - if(memcmp(hash, cur_tlv.node_hash->node_hash, 16) == 0) { + if(memcmp(hash, cur_tlv.node_state->node_hash, 16) == 0) { // The position is updated tlv_len = data[pos+1]; pos += 2; @@ -1019,7 +1003,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * } // Else, we update the data - int rc = add_data(cur_tlv.node_state->length - 26, ntohl(cur_tlv.node_state->node_id), ntohs(cur_tlv.node_state->seqno), cur_tlv.node_state->data, pdata); + int rc = add_data(cur_tlv.node_state->length - 26, be64toh(cur_tlv.node_state->node_id), be16toh(cur_tlv.node_state->seqno), cur_tlv.node_state->data, pdata); if (rc < 0) { print_error("Error while adding node state !"); } @@ -1076,7 +1060,7 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc // Neighbour check struct in6_addr ip = sender->sin6_addr; - int16_t port = htons(sender->sin6_port); + int16_t port = htobe16(sender->sin6_port); int rc = add_n_update_neighbour(&ip, port); @@ -1143,7 +1127,7 @@ int t_get_network_state(int sock_fd){ } receiver->sin6_family = AF_INET6; receiver->sin6_addr = peer->ip; - receiver->sin6_port = htons(peer->port); + receiver->sin6_port = htobe16(peer->port); receiver->sin6_scope_id = 0; // Send out a TLV network state. @@ -1320,7 +1304,7 @@ int bootstrap_node(int * sock_fd){ memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin6_family = AF_INET6; // server_addr.sin6_addr.in6_addr = htonl(INADDR_ANY); - server_addr.sin6_port = htons(LISTEN_PORT); + server_addr.sin6_port = htobe16(LISTEN_PORT); if (bind( * sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0) { print_debug(">> Error - failed to bind socket"); return -2; diff --git a/src/node.h b/src/node.h index 62f33a4..988c1f1 100644 --- a/src/node.h +++ b/src/node.h @@ -13,10 +13,12 @@ #include #include #include -#include +#include #include #include #include +#include +#include /* la table de voisins, qui est indexée par adresses de socket (des paires (IP, Port)), * et dont chaque entrée contient un booléen indiquant si le pair est permanent @@ -39,7 +41,7 @@ typedef struct neighbour_peer { typedef struct pub_data { unsigned char length; - int64_t id; + uint64_t id; uint16_t seqno; char *data; } pub_data; @@ -136,12 +138,12 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port); int add_n_update_neighbour(struct in6_addr *ip, int16_t port); // get data associated with id, if it doesn't exist return NULL -pub_data *get_data(int64_t id); +pub_data *get_data(uint64_t id); // Take data as args and create a pub_data structure in the heap -pub_data *copy_data(unsigned char len, int64_t id, uint16_t seqno, char *data); +pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data); // add new data to data list -int add_data(unsigned char len, int64_t id, uint16_t seqno, char *data, pub_data *found); +int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found); #endif diff --git a/src/tlv.c b/src/tlv.c index 0cb470d..55c306f 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -100,7 +100,7 @@ int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port) { new->type = 3; new->length = 18; new->ip = ip; - new->port = htons(port); + new->port = htobe16(port); tlv->neighbour = new; @@ -144,7 +144,7 @@ int build_network_state_req(tlv *tlv) { return 0; } -int build_node_hash(tlv *tlv, int64_t node_id, uint16_t seqno, char *data) { +int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data) { // Free the previously allocated memory free(tlv->pad1); @@ -156,8 +156,8 @@ int build_node_hash(tlv *tlv, int64_t node_id, uint16_t seqno, char *data) { new->type = 6; new->length = 26; - new->node_id = htonl(node_id); - new->seqno = htons(seqno); + new->node_id = htobe64(node_id); + new->seqno = htobe16(seqno); pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; hash_data(&pdata, (unsigned char*) new->node_hash); @@ -167,7 +167,7 @@ int build_node_hash(tlv *tlv, int64_t node_id, uint16_t seqno, char *data) { return 0; } -int build_node_state_req(tlv *tlv, int64_t node_id) { +int build_node_state_req(tlv *tlv, uint64_t node_id) { // Free the previously allocated memory free(tlv->pad1); @@ -180,14 +180,14 @@ int build_node_state_req(tlv *tlv, int64_t node_id) { new->type = 7; new->length = 8; - new->node_id = htonl(node_id); + new->node_id = htobe64(node_id); tlv->node_state_req = new; return 0; } -int build_node_state(tlv *tlv, int64_t node_id, uint16_t seqno, char *data, size_t data_len) { +int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len) { // Free the previously allocated memory free(tlv->pad1); @@ -207,8 +207,8 @@ int build_node_state(tlv *tlv, int64_t node_id, uint16_t seqno, char *data, size new->type = 8; new->length = 26 + len; - new->node_id = htonl(node_id); - new->seqno = htons(seqno); + new->node_id = htobe64(node_id); + new->seqno = htobe16(seqno); memcpy(new->data, data, len); pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; diff --git a/src/tlv.h b/src/tlv.h index 10b4eee..388957e 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -66,7 +66,7 @@ typedef struct network_state_req { typedef struct node_hash { unsigned char type; unsigned char length; - int64_t node_id; + uint64_t node_id; uint16_t seqno; char node_hash[16]; } node_hash; @@ -75,14 +75,14 @@ typedef struct node_hash { typedef struct node_state_req { unsigned char type; unsigned char length; - int64_t node_id; + uint64_t node_id; } node_state_req; // 28 octets min, 220 octets max (data 0 -> 192) typedef struct node_state { unsigned char type; unsigned char length; - int64_t node_id; + uint64_t node_id; uint16_t seqno; char node_hash[16]; char data[192]; @@ -122,9 +122,9 @@ int build_neighbour_req(union tlv *tlv); int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port); int build_network_hash(tlv *tlv, list *data_list); int build_network_state_req(tlv *tlv); -int build_node_hash(tlv *tlv, int64_t node_id, uint16_t seqno, char *data); -int build_node_state_req(tlv *tlv, int64_t node_id); -int build_node_state(tlv *tlv, int64_t node_id, uint16_t seqno, char *data, size_t data_len); +int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data); +int build_node_state_req(tlv *tlv, uint64_t node_id); +int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len); int build_warning(tlv *tlv, char *message, size_t message_len); #endif From ae893c91fc55d2b157b5cdc57bfa39c8b64fd908 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 22:22:58 +0200 Subject: [PATCH 17/25] Some changes --- src/debug.h | 1 + src/node.c | 40 ++++++++++++++++++---------------------- src/node.h | 2 ++ src/tlv.h | 4 ++-- 4 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/debug.h b/src/debug.h index a8f01d6..c0a5688 100644 --- a/src/debug.h +++ b/src/debug.h @@ -1,5 +1,6 @@ #ifndef DEBUG_H #define DEBUG_H + #include "node.h" diff --git a/src/node.c b/src/node.c index 7319011..8247348 100644 --- a/src/node.c +++ b/src/node.c @@ -1,22 +1,5 @@ // This is the main file of the Dazibao project. It represents the node, and // handles all of the main logic, including the network connexions. -<<<<<<< HEAD -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -======= ->>>>>>> add-message #include "node.h" // Static variables @@ -837,6 +820,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * break; case 2: + // We received a neighbour request so a random neighbor tlv has to be sent print_debug(">> Received neighbour request, sending out a neighbour address."); // Send a neighbour tlv @@ -858,6 +842,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * break; case 3: + print_debug(">> Received neighbour tlv, sending back network hash."); // We received a neighbour tlv so a tlv network hash is sent to that address cur_tlv.neighbour = (neighbour*) (data + pos); @@ -879,6 +864,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * break; case 4: + print_debug(">> Received network_hash, comparing with our own.."); // We reveived a network hash tlv so we compare the hash with our own, // if they differ we send a network state request tlv @@ -924,6 +910,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * pdata = (pub_data*) tmp_list->data; build_node_hash(&new_tlv, pdata->id, pdata->seqno, pdata->data); add_tlv(&pack, &new_tlv, sender, socket_num); + tmp_list = tmp_list->next; } // The position is updated @@ -965,11 +952,13 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * break; case 7: + // We received a node state request tlv // so a node state tlv for this node id has to be sent, // if no pub_data exists for this id nothing is sent print_debug(">> Received node state request. Processing..."); - cur_tlv.node_state_req = (node_state_req*) (data[pos]); + + cur_tlv.node_state_req = (node_state_req*) (data + pos); pdata = get_data(ntohl(cur_tlv.node_state_req->node_id)); if(pdata != NULL) { @@ -990,25 +979,32 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * cur_tlv.node_state = (node_state*) (data + pos); print_debug(">> Received message ! "); + printf("%d\n", cur_tlv.node_state->type ); + printf("%d\n", cur_tlv.node_state->length ); + printf("%lu\n", cur_tlv.node_state->node_id ); + printf("%hu\n", cur_tlv.node_state->seqno ); + printf("%.16s\n", cur_tlv.node_state->node_hash); + printf("%s\n", cur_tlv.node_state->data); + if (DEBUG_LEVEL > 0) { if (cur_tlv.node_state->data == NULL) { print_error("The data in the current node is NULL !"); return -1; } - printf("%s\n", data + pos ); printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0”\n", (const wchar_t*) cur_tlv.node_state->data); } // Compare hashes - pdata = get_data(ntohl(cur_tlv.node_state->node_id)); + pdata = get_data(cur_tlv.node_state->node_id); // If data is found for this id then we check that both hashes are the same if(pdata != NULL) { // We hash the data stored in the data list + memset(hash, 0, 16); hash_data(pdata, hash); // If both hashes are the same then nothing has to be done - if(memcmp(hash, cur_tlv.node_hash->node_hash, 16) == 0) { + if(memcmp(hash, cur_tlv.node_state->node_hash, 16) == 0) { // The position is updated tlv_len = data[pos+1]; pos += 2; @@ -1060,7 +1056,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * if(pack.length > 0){ send_packet((char*) &pack, pack.length, sender, socket_num); } - + print_data(data_list); return 0; } diff --git a/src/node.h b/src/node.h index 62f33a4..c1d9e35 100644 --- a/src/node.h +++ b/src/node.h @@ -17,6 +17,8 @@ #include #include #include +#include + /* la table de voisins, qui est indexée par adresses de socket (des paires (IP, Port)), * et dont chaque entrée contient un booléen indiquant si le pair est permanent diff --git a/src/tlv.h b/src/tlv.h index 10b4eee..825de6b 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -66,7 +66,7 @@ typedef struct network_state_req { typedef struct node_hash { unsigned char type; unsigned char length; - int64_t node_id; + uint64_t node_id; uint16_t seqno; char node_hash[16]; } node_hash; @@ -82,7 +82,7 @@ typedef struct node_state_req { typedef struct node_state { unsigned char type; unsigned char length; - int64_t node_id; + uint64_t node_id; uint16_t seqno; char node_hash[16]; char data[192]; From 201084d9f102084c69a2e69068098e16a683df19 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 29 Apr 2020 22:45:58 +0200 Subject: [PATCH 18/25] Trying to fix --- src/node.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/node.c b/src/node.c index 585d874..859b661 100644 --- a/src/node.c +++ b/src/node.c @@ -984,7 +984,11 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * printf("%lu\n", cur_tlv.node_state->node_id ); printf("%hu\n", cur_tlv.node_state->seqno ); printf("%.16s\n", cur_tlv.node_state->node_hash); - printf("%s\n", cur_tlv.node_state->data); + printf("%s\0\n", cur_tlv.node_state->data); + + sprintf(warn, ">> WARNING:\n%%.%ds", cur_tlv.node_state->length); + printf(warn, cur_tlv.node_state->data); + sleep(3); if (DEBUG_LEVEL > 0) { if (cur_tlv.node_state->data == NULL) { From bd8f5c452b60c3b3d3eb11c916088f66f1393e48 Mon Sep 17 00:00:00 2001 From: n07070 Date: Thu, 30 Apr 2020 19:56:58 +0200 Subject: [PATCH 19/25] Trying to fix the receiving of messages --- src/debug.c | 2 +- src/node.c | 281 ++++++++++++++++++++++++++++++---------------------- src/node.h | 12 +-- src/tlv.c | 8 +- src/tlv.h | 16 +-- 5 files changed, 180 insertions(+), 139 deletions(-) diff --git a/src/debug.c b/src/debug.c index 524d359..a0bbbcd 100644 --- a/src/debug.c +++ b/src/debug.c @@ -55,7 +55,7 @@ void print_data(list * l){ print_debug(">> Printing messages we know of so far :"); int nbr_msg = 0; setlocale(LC_ALL, "en_US.utf8"); - print_debug(">> 🏂🏂🏂🏂 Peer ID | Seqno | Length | Message "); + print_debug(">> Peer ID | Seqno | Length | Message "); while(l != NULL){ pub_data * message = l->data; printf("\x1b[31m[DEBUG]\x1b[0m >> %li | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data); diff --git a/src/node.c b/src/node.c index 859b661..beae120 100644 --- a/src/node.c +++ b/src/node.c @@ -192,9 +192,9 @@ pub_data *get_data(uint64_t id) { } // Take data as args and create a pub_data structure in the heap -pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data) { +pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data) { pub_data *new_data = (pub_data*) malloc(sizeof(pub_data)); - char *_data = (char*) malloc(len); + unsigned char *_data = (unsigned char*) malloc(len); if (_data == NULL) { print_error("Failed to allocate memory for copying the data !"); return NULL; @@ -213,7 +213,7 @@ pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data) } // A node state TLV was received and either no data associated to it's id is in our data list or the data was updated, return -1 if an error occurend, 0 if nothing had to be done and 1 if something was updated/added -int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found) { +int add_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data, pub_data *found) { // Check if it's our own id if(id == NODE_ID) { // wtf @@ -249,7 +249,7 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat // Updata message free(found->data); - found->data = (char*) malloc(len); + found->data = (unsigned char*) malloc(len); memcpy(found->data, data, len); printf(">> Updated %li's published data.\n", id); @@ -492,7 +492,7 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i int response_code = sendmsg(socket_num, &packet_tlv_send_out, 0); if (response_code < 0) { - print_debug(">> Unable to send out the packet to peer."); + print_error("Unable to send out the packet to peer."); error_while_sending = 1; } else if (response_code < length) { print_debug(">> Sent out only part of the packet."); @@ -578,70 +578,70 @@ int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { return send_packet((char*) &pack, pack.length, dest, socket_num); } -int send_tlv(tlv *tlv_to_send, uint16_t tlv_size, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num){ - print_debug(">> Building packet to send a TLV."); - - // We first need to build the packet, - char packet_buff[1024]; - struct packet pack; - pack.magic = 95; - pack.version = 1; - if (tlv_size > 1020) { - print_debug(">> Unable to send the tlv, it's size if above 1020 bytes."); - return -1; - } else { - memcpy((void *) pack.body, tlv_to_send, tlv_size); - } - - // Move the content of the paquet struct to a buffer - // That will be send out in a vectorized buffer. - // packet_buff = (char *) pack; - memcpy(&packet_buff,&pack,1024); - - if (DEBUG_LEVEL > 1) { - print_debug(">> Packet has been built."); - } - - // Vectorized buffer - struct iovec vec_buff = { .iov_len = sizeof(packet_buff), .iov_base = packet_buff }; - - int error_while_sending = 0; - - // For every dest - for (size_t i = 0; i < dest_list_size; i++) { - // Creating the struct to send out with sendmsg - struct msghdr packet_tlv_send_out = { - .msg_name = &dest_list[i], - .msg_namelen = sizeof(dest_list[i]), - .msg_iov = &vec_buff, - .msg_iovlen = 1 // We have only one iovec buffer. But if we had 2, we would write 2. - }; - - int response_code = sendmsg((int) socket_num, &packet_tlv_send_out, 0); - if (response_code < 0) { - if (DEBUG_LEVEL > 0) { - printf("\x1b[31m[DEBUG]\x1b[0m >> Unable to send out the packet to peer %li", i); - } - error_while_sending = 1; - continue; - } else if (response_code < sizeof(packet_tlv_send_out)) { - print_debug(">> Sent out only part of the packet."); - error_while_sending = 1; - continue; - } else { - if (DEBUG_LEVEL > 0) { - printf("\x1b[31m[DEBUG]\x1b[0m >> Send out packet to peer %li", i); - } - } - } - - if (error_while_sending == 1) { - print_debug(">> Error occured while sending out a packet."); - return -1; - } else { - return 0; - } -} +// TODO This function can be deleted. +// int send_tlv(tlv *tlv_to_send, uint16_t tlv_size, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num){ +// print_debug(">> Building packet to send a TLV."); +// +// // We first need to build the packet, +// char packet_buff[1024]; +// struct packet pack; +// pack.magic = 95; +// pack.version = 1; +// if (tlv_size > 1020) { +// print_debug(">> Unable to send the tlv, it's size is above 1020 bytes."); +// return -1; +// } else { +// memcpy((void *) pack.body, tlv_to_send, tlv_size); +// } +// +// // Move the content of the paquet struct to a buffer +// // That will be send out in a vectorized buffer. +// memcpy(&packet_buff,&pack,1024); +// +// if (DEBUG_LEVEL > 1) { +// print_debug(">> Packet has been built."); +// } +// +// // Vectorized buffer +// struct iovec vec_buff = { .iov_len = sizeof(packet_buff), .iov_base = packet_buff }; +// +// int error_while_sending = 0; +// +// // For every dest +// for (size_t i = 0; i < dest_list_size; i++) { +// // Creating the struct to send out with sendmsg +// struct msghdr packet_tlv_send_out = { +// .msg_name = &dest_list[i], +// .msg_namelen = sizeof(dest_list[i]), +// .msg_iov = &vec_buff, +// .msg_iovlen = 1 // We have only one iovec buffer. But if we had 2, we would write 2. +// }; +// +// int response_code = sendmsg((int) socket_num, &packet_tlv_send_out, 0); +// if (response_code < 0) { +// if (DEBUG_LEVEL > 0) { +// printf("\x1b[31m[DEBUG]\x1b[0m >> Unable to send out the packet to peer %li", i); +// } +// error_while_sending = 1; +// continue; +// } else if (response_code < sizeof(packet_tlv_send_out)) { +// print_debug(">> Sent out only part of the packet."); +// error_while_sending = 1; +// continue; +// } else { +// if (DEBUG_LEVEL > 0) { +// printf("\x1b[31m[DEBUG]\x1b[0m >> Sent out packet to peer %i", i); +// } +// } +// } +// +// if (error_while_sending == 1) { +// print_debug(">> Error occured while sending out a packet."); +// return -1; +// } else { +// return 0; +// } +// } // We need to make sure the TLV announces a length that will no go onto // another tlv, as we might end up reading bullshit. @@ -660,13 +660,13 @@ int validate_tlv(char *data, int pos, uint16_t packet_len){ print_debug(">> Reading outside of packet's max length."); return -1; } - // 0 1 2 3 = Packet - // 4 = type 5 = tlv_len + // 0 1 2 3 = Packet + // 4 = type 5 = tlv_len unsigned char tlv_len = data[pos+1]; // Check that the tlv does not exceed the packet length if(pos + tlv_len > packet_len){ - print_debug(">> The TLV Length exceed the packet length\n"); + print_debug(">> The TLV Length exceeds the packet length\n"); return -1; } @@ -738,8 +738,9 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack int add_message(char * message, int message_len){ // Don't update the message if it's empty - if(message_len == 0) + if(message_len == 0){ return -1; + } // If not, get our data in the list and update it pub_data *our_data = get_data(NODE_ID); @@ -748,8 +749,8 @@ int add_message(char * message, int message_len){ our_data->seqno = (our_data->seqno + 1) % 65535; our_data->length = message_len; free(our_data->data); - our_data->data = (char*) malloc(message_len); - memcpy(our_data->data, message, message_len); + our_data->data = (unsigned char*) malloc(message_len); + memcpy(our_data->data, (unsigned char *) message, message_len); print_debug(">> Message added."); @@ -768,6 +769,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * print_debug(">> Length indicated in packet differs from real length of packet received, disgarding packet."); return -1; } + int nbr_of_tlvs = 0; int pos = 4; unsigned char tlv_len, hash[16]; @@ -780,11 +782,13 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * new_tlv.pad1 = NULL; cur_tlv.pad1 = NULL; - list *tmp_list; - pub_data *pdata; + list * tmp_list; + pub_data * pdata; // holds the random neighbour we send back in case of a neighbour_req. struct neighbour_peer *random_neighbour; + // memset(random_neighbour, 0, sizeof(struct neighbour_peer)); + // Holds the new neighbour send to us by a "neighbour tlv" struct sockaddr_in6 new_neighbour; @@ -792,32 +796,31 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * packet pack = (packet) {.magic = 95, .version = 1, .length = 0}; memset(pack.body, 0, 1020); -/* - int ifindex = if_nametoindex("enp3s0"); - if(ifindex == 0) { - int ifindex = if_nametoindex("eth0"); - if(ifindex == 0) { - perror("if_nametoindex failed"); - return -1; - } - } -*/ int ifindex = 0; while(pos < total_packet_len) { // Making sure the current TLV we are looking at is valid. + // TODO : I think we should reset all the structs here. + // memset(random_neighbour, 0, sizeof(struct neighbour_peer)); + // memset(pdata, 0, sizeof(struct pub_data)); + // memset(tmp_list, 0, sizeof(struct list)); + memset(hash, 0, 16); + memset(warn, 0, 32); + tlv_len = 0; + switch(validate_tlv(data, pos, total_packet_len)) { case 0: // We received a padding tlv so it is ignored print_debug(">> Received padding tlv, ignoring..."); pos += 1; - + nbr_of_tlvs++; break; case 1: // We received a padding tlv so it is ignored print_debug(">> Received padding(n) tlv, ignoring..."); tlv_len = data[pos+1]; pos += tlv_len + 2; + nbr_of_tlvs++; break; case 2: @@ -840,6 +843,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; + nbr_of_tlvs++; break; case 3: @@ -862,6 +866,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; + nbr_of_tlvs++; break; case 4: @@ -898,6 +903,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; + nbr_of_tlvs++; break; case 5: @@ -915,7 +921,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * } // The position is updated - pos += 2; + tlv_len = data[pos+1]; + pos += tlv_len + 2; break; case 6: @@ -936,13 +943,14 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * if(memcmp(hash, cur_tlv.node_hash->node_hash, 16) == 0) { // The position is updated tlv_len = data[pos+1]; - pos += 2; - + pos += tlv_len + 2; + print_debug(">> Both hashs are the same, nothing to do."); break; } } + print_debug(">> No hash found, or hashs are differents, sending back node state request."); // If no pub_data was found or the hashes differ then we send a node state request build_node_state_req(&new_tlv, be64toh(cur_tlv.node_hash->node_id)); add_tlv(&pack, &new_tlv, sender, socket_num); @@ -964,7 +972,9 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * if(pdata != NULL) { build_node_state(&new_tlv, pdata->id, pdata->seqno, pdata->data, pdata->length); add_tlv(&pack, &new_tlv, sender, socket_num); - } + } else { + print_debug(">> Found no data for the requested node, skipping..."); + } // The position is updated tlv_len = data[pos+1]; @@ -976,19 +986,23 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // we add it to the data list // or update the data stored print_debug(">> Received node state, updating..."); - cur_tlv.node_state = (node_state*) (data + pos); + cur_tlv.node_state = (node_state*) (data + pos); print_debug(">> Received message ! "); - printf("%d\n", cur_tlv.node_state->type ); - printf("%d\n", cur_tlv.node_state->length ); - printf("%lu\n", cur_tlv.node_state->node_id ); - printf("%hu\n", cur_tlv.node_state->seqno ); - printf("%.16s\n", cur_tlv.node_state->node_hash); - printf("%s\0\n", cur_tlv.node_state->data); - - sprintf(warn, ">> WARNING:\n%%.%ds", cur_tlv.node_state->length); - printf(warn, cur_tlv.node_state->data); - sleep(3); + printf("Type : %d\n", cur_tlv.node_state->type ); + printf("Length : %d\n", cur_tlv.node_state->length ); + printf("Node ID : %lu\n", be64toh(cur_tlv.node_state->node_id) ); + printf("Seqno : %hu\n", be16toh(cur_tlv.node_state->seqno) ); + printf("Hash :"); + for(int x = 0; x < 16; x++){ + printf("%02x", cur_tlv.node_state->node_hash[x]); + fflush(0); + } + printf("\n"); + for(int x = 0; x < 192; x++){ + printf("%d", cur_tlv.node_state->data[x]); + fflush(0); + } if (DEBUG_LEVEL > 0) { if (cur_tlv.node_state->data == NULL) { @@ -996,6 +1010,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * return -1; } printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0”\n", (const wchar_t*) cur_tlv.node_state->data); + sleep(1); } // Compare hashes @@ -1011,7 +1026,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * if(memcmp(hash, cur_tlv.node_state->node_hash, 16) == 0) { // The position is updated tlv_len = data[pos+1]; - pos += 2; + pos += tlv_len + 2; break; } @@ -1046,7 +1061,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // A malformed packet was found so we stop looking for more packets and send a warning tlv strcpy(warn, "Packet is malformed."); print_debug(">> Malformed packet, we won't treat it."); - build_warning(&new_tlv, warn, strlen(warn)); + print_debug(">> Sending back a Warning to sender."); + build_warning(&new_tlv,(unsigned char *) warn, strlen(warn)); add_tlv(&pack, &new_tlv, sender, socket_num); return -1; @@ -1221,20 +1237,20 @@ int run_node(int sock_fd){ /* Call poll() */ - ret = poll(fds, 2, 5); + ret = poll(fds, 2, 10); if (ret < 0) { - print_debug(">> Error - poll returned error"); + print_error("Poll returned error"); break; } else if (ret > 0) { /* Regardless of requested events, poll() can always return these */ if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { - print_debug("Error - poll indicated stdin error\n"); + print_error("Poll indicated stdin error\n"); break; } if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) { - print_debug("Error - poll indicated socket error\n"); + print_error("Poll indicated socket error\n"); break; } @@ -1242,9 +1258,10 @@ int run_node(int sock_fd){ if (fds[0].revents & (POLLIN | POLLPRI)) { bytes = read(0, input_buffer, sizeof(input_buffer)); if (bytes < 0) { - print_debug(">> Error - stdin error"); + print_error("stdin error"); break; } + // Remove trailing \n input_buffer[strcspn(input_buffer, "\n")] = 0; if (DEBUG_LEVEL > 0) { printf("\x1b[31m[DEBUG]\x1b[0m >> Adding following message to the table : “%s”\n", input_buffer ); @@ -1252,7 +1269,9 @@ int run_node(int sock_fd){ // Add message to the message table. if (add_message(input_buffer, bytes) < 0) { print_debug(">> Error while trying to add the message to the list of messages, please try again.."); - } + // Reset buffer + memset(input_buffer, 0, sizeof(input_buffer)); + } } // Read data from the socket ( incoming packet ) @@ -1288,6 +1307,8 @@ int run_node(int sock_fd){ if (work_tlv_status < 0) { print_debug(">> Error while treating the incoming packet."); } + // Reset buffer + memset(output_buffer, 0, sizeof(output_buffer)); } } } else { @@ -1304,7 +1325,7 @@ int run_node(int sock_fd){ // This function runs once, and sets the sock_fd as well as the neighbourhood -int bootstrap_node(int * sock_fd){ +int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ print_debug(">> Boostraping node..."); struct sockaddr_in6 server_addr; @@ -1312,42 +1333,46 @@ int bootstrap_node(int * sock_fd){ /* Create UDP socket */ * sock_fd = socket(AF_INET6, SOCK_DGRAM, 0); if ( * sock_fd < 0) { - print_debug(">> Error - failed to open socket"); - return -1; + print_error("Failed to open socket"); + perror(""); + exit(-1); } /* Bind socket */ memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin6_family = AF_INET6; // server_addr.sin6_addr.in6_addr = htonl(INADDR_ANY); - server_addr.sin6_port = htobe16(LISTEN_PORT); + server_addr.sin6_port = LISTEN_PORT; if (bind( * sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0) { - print_debug(">> Error - failed to bind socket"); - return -2; + print_error("Failed to bind socket"); + perror(""); + exit(-1); } /* Make the first peer*/ struct neighbour_peer * root_peer = (struct neighbour_peer *) malloc(sizeof(struct neighbour_peer)); time_t root_peer_seen = time(NULL); - int inet_p = inet_pton(AF_INET6, ROOT_PEER_ADDR, &root_peer->ip); + int inet_p = inet_pton(AF_INET6, root_peer_ip, &root_peer->ip); if(inet_p < 1){ perror(">> Failed to create the root peer."); return -3; } - root_peer->port = 1212; + root_peer->port = root_peer_port; root_peer->is_temporary = 0; root_peer->last_seen = root_peer_seen; // TODO: Add the first peer to the neighbourhood print_debug(">> Adding the first root peer to the list..."); neighbour_list = malloc(sizeof(struct list)); + memset(neighbour_list, 0, sizeof(struct list)); neighbour_list->data = (void *) root_peer; neighbour_list->next = NULL; print_debug(">> Initializing data list..."); data_list = (list*) malloc(sizeof(list)); + memset(data_list, 0, sizeof(struct list)); data_list->data = malloc(sizeof(pub_data)); data_list->next = NULL; @@ -1363,11 +1388,27 @@ int bootstrap_node(int * sock_fd){ int main(int argc, const char *argv[]) { + + if (argc != 3) { + print_error("Usage : ./dazibao "); + exit(-1); + } + + char *end; + intmax_t val = strtoimax(argv[2], &end, 10); + if (errno == ERANGE || val < 0 || val > UINT16_MAX || end == argv[2] || *end != '\0'){ + print_error("Conversion of port number failed, please choose a smaller number."); + } + + uint16_t root_peer_port = (uint16_t) val; + + char * root_peer_ip = (char *) argv[1]; + setlocale(LC_ALL, ""); print_debug(">> Starting node"); int sock_fd; - bootstrap_node(&sock_fd); + bootstrap_node(&sock_fd, root_peer_ip, root_peer_port); run_node(sock_fd); close(sock_fd); diff --git a/src/node.h b/src/node.h index 988c1f1..af0e515 100644 --- a/src/node.h +++ b/src/node.h @@ -43,7 +43,7 @@ typedef struct pub_data { unsigned char length; uint64_t id; uint16_t seqno; - char *data; + unsigned char *data; } pub_data; // General list @@ -62,14 +62,14 @@ typedef struct list { // The node ID // #define NODE_ID 42675882021843277 -#define NODE_ID 42013376900101010 +#define NODE_ID 13809235719890846928 // The number of neighbours // The neighbour table has 15 entries #define NEIGHBOUR_MAX 15 // The adress of the main peer -#define ROOT_PEER_ADDR "2001:660:3301:9200::51c2:1b9b" +#define ROOT_PEER_ADDR "::1" // fonctions signatures @@ -124,7 +124,7 @@ int add_message(char * message, int message_len); // This functions creates the structures needed for the rest of the project, // creates the socket, and returns all of it. -int bootstrap_node(int * sock_fd); +int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port); // Helper functions int len_list(list *l); @@ -141,9 +141,9 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port); pub_data *get_data(uint64_t id); // Take data as args and create a pub_data structure in the heap -pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data); +pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data); // add new data to data list -int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found); +int add_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data, pub_data *found); #endif diff --git a/src/tlv.c b/src/tlv.c index 55c306f..0dcf492 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -144,7 +144,7 @@ int build_network_state_req(tlv *tlv) { return 0; } -int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data) { +int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data) { // Free the previously allocated memory free(tlv->pad1); @@ -187,7 +187,7 @@ int build_node_state_req(tlv *tlv, uint64_t node_id) { return 0; } -int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len) { +int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data, size_t data_len) { // Free the previously allocated memory free(tlv->pad1); @@ -219,13 +219,13 @@ int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, siz return 0; } -int build_warning(tlv *tlv, char *message, size_t message_len) { +int build_warning(tlv *tlv, unsigned char *message, size_t message_len) { // Free the previously allocated memory free(tlv->pad1); warning *new = (warning*) malloc(sizeof(warning)); memset(new, 0, sizeof(warning)); - + int len = message_len; if(new == NULL) diff --git a/src/tlv.h b/src/tlv.h index 388957e..d2ebff5 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -53,7 +53,7 @@ typedef struct neighbour { typedef struct network_hash { unsigned char type; unsigned char length; - char network_hash[16]; + unsigned char network_hash[16]; } network_hash; // 2 octets @@ -68,7 +68,7 @@ typedef struct node_hash { unsigned char length; uint64_t node_id; uint16_t seqno; - char node_hash[16]; + unsigned char node_hash[16]; } node_hash; // 10 octets @@ -84,15 +84,15 @@ typedef struct node_state { unsigned char length; uint64_t node_id; uint16_t seqno; - char node_hash[16]; - char data[192]; + unsigned char node_hash[16]; + unsigned char data[192]; } node_state; // 2 octets min, 258 ocets max (unsigned char 0 -> 255) typedef struct warning { unsigned char type; unsigned char length; - char message[256]; + unsigned char message[256]; } warning; typedef union tlv { @@ -122,9 +122,9 @@ int build_neighbour_req(union tlv *tlv); int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port); int build_network_hash(tlv *tlv, list *data_list); int build_network_state_req(tlv *tlv); -int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data); +int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data); int build_node_state_req(tlv *tlv, uint64_t node_id); -int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len); -int build_warning(tlv *tlv, char *message, size_t message_len); +int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data, size_t data_len); +int build_warning(tlv *tlv, unsigned char *message, size_t message_len); #endif From 733c700492b3dca19be90b7374309188ec49cff1 Mon Sep 17 00:00:00 2001 From: nis Date: Sun, 3 May 2020 00:19:54 +0200 Subject: [PATCH 20/25] struct padding error fix --- src/hash.c | 13 +++-- src/node.c | 168 +++++++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 145 insertions(+), 36 deletions(-) diff --git a/src/hash.c b/src/hash.c index a3665a0..7414934 100644 --- a/src/hash.c +++ b/src/hash.c @@ -10,7 +10,11 @@ void hash_data(pub_data *data, unsigned char *buf) { // The resulting buf is hashed and put into a buffer unsigned char hash[SHA256_DIGEST_LENGTH]; - SHA256(concat, totlen, hash); + + SHA256_CTX sha256; + SHA256_Init(&sha256); + SHA256_Update(&sha256, concat, totlen); + SHA256_Final(hash, &sha256); // Put truncated hash into buf hash_trunc(hash, buf); @@ -35,9 +39,10 @@ void hash_network(list *data_list, unsigned char *buf) { } // Hash all of concat to obtain the network hash - if (SHA256(concat, totlen, hash) == NULL) { - print_debug(">> Doing the hash failed : function return NULL, should return a pointer."); - }; + SHA256_CTX sha256; + SHA256_Init(&sha256); + SHA256_Update(&sha256, concat, concat_len); + SHA256_Final(hash, &sha256); // Put truncated hash into buf hash_trunc(hash, buf); diff --git a/src/node.c b/src/node.c index beae120..c2fa4af 100644 --- a/src/node.c +++ b/src/node.c @@ -698,6 +698,51 @@ int validate_tlv(char *data, int pos, uint16_t packet_len){ return 7; case 8: if(tlv_len < MIN_LEN_NODE_STATE || tlv_len > MAX_LEN_NODE_STATE) return -1; + + /* + // Check if it has the right hash + uint16_t *seqno = (uint16_t*) (data + pos + 10); + uint64_t *id = (uint64_t*) (data + pos + 2); + + pub_data pdata = (pub_data) { + .length = tlv_len, + .id = *id, + .seqno = *seqno, + .data = (unsigned char*) malloc(tlv_len) + }; + + unsigned char *cur_hash = (unsigned char*) (data + pos + 12); + char *cur_message = data + pos + 28; + + unsigned char hash[16]; + + memcpy(pdata.data, cur_message, tlv_len); + + hash_data(&pdata, hash); + + if(memcmp(hash, cur_hash, 16) != 0) { + print_debug(">> Malformed hash."); + printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); + + for(int x = 0; x < 16; x++){ + printf("%02x", hash[x]); + fflush(0); + } + + printf("\n"); + printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); + + for(int x = 0; x < 16; x++){ + printf("%02x", cur_hash[x]); + fflush(0); + } + + printf("\n"); + + return -1; + + } + */ return 8; case 9: return 9; @@ -772,7 +817,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * int nbr_of_tlvs = 0; int pos = 4; - unsigned char tlv_len, hash[16]; + unsigned char tlv_len, hash[16], hash2[16]; char warn[32]; // The TLV we are going to send back. @@ -798,6 +843,12 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * int ifindex = 0; + // Temporary values + uint16_t *port, *seqno; + uint64_t *id; + unsigned char *cur_hash, *ip; + char *cur_message; + while(pos < total_packet_len) { // Making sure the current TLV we are looking at is valid. // TODO : I think we should reset all the structs here. @@ -850,13 +901,14 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * print_debug(">> Received neighbour tlv, sending back network hash."); // We received a neighbour tlv so a tlv network hash is sent to that address - cur_tlv.neighbour = (neighbour*) (data + pos); + ip = (unsigned char*) (data + pos + 2); + port = (uint16_t*) (data + pos + 18); // Init dest socket memset(&new_neighbour, 0, sizeof(new_neighbour)); new_neighbour.sin6_family = AF_INET6; - memcpy(&new_neighbour.sin6_addr, &cur_tlv.neighbour->ip, 16); - new_neighbour.sin6_port = be16toh(cur_tlv.neighbour->port); + memcpy(&new_neighbour.sin6_addr, ip, 16); + new_neighbour.sin6_port = be16toh(*port); new_neighbour.sin6_scope_id = ifindex; // Build network hash @@ -874,7 +926,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * print_debug(">> Received network_hash, comparing with our own.."); // We reveived a network hash tlv so we compare the hash with our own, // if they differ we send a network state request tlv - cur_tlv.network_hash = (network_hash*) (data + pos); + cur_hash = (unsigned char*) (data + pos + 2); + hash_network(data_list, hash); if (DEBUG_LEVEL > 1) { @@ -886,13 +939,13 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * printf("\n"); printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); for(int x = 0; x < 16; x++){ - printf("%02x", cur_tlv.network_hash->network_hash[x]); + printf("%02x", cur_hash[x]); fflush(0); } printf("\n"); } - if(memcmp(hash, cur_tlv.network_hash->network_hash, 16) != 0) { + if(memcmp(hash, cur_hash, 16) != 0) { print_debug(">> Sending out our network hash."); build_network_state_req(&new_tlv); send_single_tlv(&new_tlv, sender, socket_num); @@ -931,16 +984,20 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // we send a node state request, //if the hashes are identical nothing has to be done print_debug(">> Received node hash, updating message entry..."); - cur_tlv.node_hash = (node_hash*) (data + pos); - pdata = get_data(be64toh(cur_tlv.node_hash->node_id)); + + id = (uint64_t*) (data + pos + 2); + cur_hash = (unsigned char*) (data + pos + 12); + + pdata = get_data(be64toh(*id)); // If data is found for this id then we check that both hashes are the same if(pdata != NULL) { + print_debug(">> Comparing hashes..."); // We hash the data stored in the data list hash_data(pdata, hash); // If both hashes are the same then nothing has to be done - if(memcmp(hash, cur_tlv.node_hash->node_hash, 16) == 0) { + if(memcmp(hash, cur_hash, 16) == 0) { // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; @@ -950,9 +1007,9 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * } - print_debug(">> No hash found, or hashs are differents, sending back node state request."); + print_debug(">> No hash found, or hashs are different, sending back node state request."); // If no pub_data was found or the hashes differ then we send a node state request - build_node_state_req(&new_tlv, be64toh(cur_tlv.node_hash->node_id)); + build_node_state_req(&new_tlv, be64toh(*id)); add_tlv(&pack, &new_tlv, sender, socket_num); // The position is updated @@ -966,8 +1023,10 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // so a node state tlv for this node id has to be sent, // if no pub_data exists for this id nothing is sent print_debug(">> Received node state request. Processing..."); - cur_tlv.node_state_req = (node_state_req*) (data + pos); - pdata = get_data(be64toh(cur_tlv.node_state_req->node_id)); + + id = (uint64_t*) (data + pos + 2); + + pdata = get_data(be64toh(*id)); if(pdata != NULL) { build_node_state(&new_tlv, pdata->id, pdata->seqno, pdata->data, pdata->length); @@ -986,46 +1045,90 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // we add it to the data list // or update the data stored print_debug(">> Received node state, updating..."); - cur_tlv.node_state = (node_state*) (data + pos); + + tlv_len = data[pos + 1]; + id = (uint64_t*) (data + pos + 2); + seqno = (uint16_t*) (data + pos + 10); + cur_hash = (unsigned char*) (data + pos + 12); + cur_message = data + pos + 28; print_debug(">> Received message ! "); - printf("Type : %d\n", cur_tlv.node_state->type ); - printf("Length : %d\n", cur_tlv.node_state->length ); - printf("Node ID : %lu\n", be64toh(cur_tlv.node_state->node_id) ); - printf("Seqno : %hu\n", be16toh(cur_tlv.node_state->seqno) ); + printf("Type : %d\n", data[pos] ); + printf("Length : %d\n", tlv_len ); + printf("Node ID : %lu\n", be64toh(*id) ); + printf("Seqno : %hu\n", be16toh(*seqno) ); printf("Hash :"); for(int x = 0; x < 16; x++){ - printf("%02x", cur_tlv.node_state->node_hash[x]); + printf("%02x", cur_hash[x]); fflush(0); } printf("\n"); - for(int x = 0; x < 192; x++){ - printf("%d", cur_tlv.node_state->data[x]); + for(int x = 0; x < tlv_len; x++){ + printf("%c", cur_message[x]); fflush(0); } + printf("\n"); + // Check if it has the right hash + pub_data pdata_check = (pub_data) { + .length = tlv_len, + .id = *id, + .seqno = *seqno, + .data = (unsigned char*) malloc(tlv_len) + }; + + memcpy(pdata_check.data, cur_message, tlv_len); + + hash_data(&pdata_check, hash2); + + if(memcmp(hash2, cur_hash, 16) != 0) { + print_debug(">> Malformed hash."); + printf("\x1b[31m[DEBUG]\x1b[0m >> Calculated : "); + + for(int x = 0; x < 16; x++){ + printf("%02x", hash2[x]); + fflush(0); + } + + printf("\n"); + printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); + + for(int x = 0; x < 16; x++){ + printf("%02x", cur_hash[x]); + fflush(0); + } + + printf("\n"); + + pos += tlv_len + 2; + break; + } + + + /* if (DEBUG_LEVEL > 0) { - if (cur_tlv.node_state->data == NULL) { + if (cur_message == NULL) { print_error("The data in the current node is NULL !"); return -1; } - printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0”\n", (const wchar_t*) cur_tlv.node_state->data); + printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0”\n", (const wchar_t*) cur_message); sleep(1); } + */ // Compare hashes - pdata = get_data(be64toh(cur_tlv.node_state->node_id)); + pdata = get_data(be64toh(*id)); // If data is found for this id then we check that both hashes are the same if(pdata != NULL) { + print_debug(">> Comparing hashes..."); // We hash the data stored in the data list memset(hash, 0, 16); hash_data(pdata, hash); // If both hashes are the same then nothing has to be done - if(memcmp(hash, cur_tlv.node_state->node_hash, 16) == 0) { + if(memcmp(hash, cur_hash, 16) == 0) { // The position is updated - tlv_len = data[pos+1]; pos += tlv_len + 2; break; @@ -1034,12 +1137,11 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * } // Else, we update the data - int rc = add_data(cur_tlv.node_state->length - 26, be64toh(cur_tlv.node_state->node_id), be16toh(cur_tlv.node_state->seqno), cur_tlv.node_state->data, pdata); + int rc = add_data(tlv_len - 26, be64toh(*id), be16toh(*seqno), (unsigned char*) cur_message, pdata); if (rc < 0) { print_error("Error while adding node state !"); } // The position is updated - tlv_len = data[pos+1]; pos += tlv_len + 2; break; @@ -1048,12 +1150,14 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // We received a warning tlv so it's message is printed cur_tlv.warning = (warning*) (data + pos); + tlv_len = data[pos+1]; + cur_message = data + pos + 2; + // Print exactly new_tlv.length characters from new_tlv.message - sprintf(warn, ">> WARNING:\n%%.%ds", cur_tlv.warning->length + 1); - printf(warn, cur_tlv.warning->message); + sprintf(warn, ">> WARNING:\n%%.%ds", tlv_len + 1); + printf(warn, cur_message); // The position is updated - tlv_len = data[pos+1]; pos += tlv_len + 2; break; From c56a15176294d077e792966813af0b84afe29eae Mon Sep 17 00:00:00 2001 From: nis Date: Sun, 3 May 2020 21:38:04 +0200 Subject: [PATCH 21/25] hash fix (not finished yet) --- Makefile | 2 +- src/hash.c | 45 ++++++++++++++++++++++++++++++++--- src/node.c | 70 +++++++++++++++++++++++++++++++++++------------------- src/node.h | 9 +++---- src/tlv.c | 41 +++++--------------------------- src/tlv.h | 6 ++--- 6 files changed, 103 insertions(+), 70 deletions(-) diff --git a/Makefile b/Makefile index 3d676e4..321bd99 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ TARGET ?= dazibao SRC_DIRS ?= ./src/* CC := gcc -CFLAGS= -O2 -Wall +CFLAGS= -O2 -Wall -g SRCS := $(shell find $(SRC_DIRS) -name *.c -or -name *.s) OBJS := $(addsuffix .o,$(basename $(SRCS))) DEPS := $(OBJS:.o=.d) diff --git a/src/hash.c b/src/hash.c index 7414934..853d474 100644 --- a/src/hash.c +++ b/src/hash.c @@ -3,6 +3,14 @@ // Hash a single data void hash_data(pub_data *data, unsigned char *buf) { + /* + data->length = 52 - 26; + data->id = 34538; + data->seqno = 4864; + data->data = "Luke, je suis ton \"pair\" !"; + printf("Hash received: 9ffe841a99776f6d1295ac75b53a58d7\n"); + */ + // All three fields are concatenated into a single buffer int totlen = data->length + 10; unsigned char concat[totlen]; @@ -18,6 +26,16 @@ void hash_data(pub_data *data, unsigned char *buf) { // Put truncated hash into buf hash_trunc(hash, buf); + + /* + printf("Hash built: "); + for(int i = 0; i < 16; i++) { + printf("%02x", buf[i]); + } + printf("\n"); + + exit(1); + */ } // Hash every data contained in data_list then return a network hash @@ -61,15 +79,36 @@ void hash_trunc(unsigned char *hash32oct, unsigned char *buf) { // Concat all fields of data and put them in buf void concat_data(pub_data *data, unsigned char *buf) { - if (memcpy(buf, &(data->id), 8) == NULL) { + // Turn seqno to big endian + uint16_t seqno = htobe16(data->seqno); + + if (memcpy(buf, (char*) &data->id, 8) == NULL) { print_debug(">> Concat the data (id) didn't work !"); - }; - if (memcpy(buf+8, &(data->seqno), 2) == NULL) { + } + if (memcpy(buf+8, (char*) &seqno, 2) == NULL) { print_debug(">> concat the data (seqno) didn't work !"); } if (memcpy(buf+10, data->data, data->length) == NULL) { print_debug(">> Contact the data (data) didn't work !"); } + + /* + uint64_t *id = (uint64_t *) buf; + uint16_t *seqno2 = (uint16_t *) (buf + 8); + char *message = (char*) (buf + 10); + char fuck[100]; + + printf("id: %ld\nseqno: %d\nmessage: ", *id, *seqno2); + for(int i = 0; i < data->length; i++) { + printf("%c", message[i]); + } + printf("\nORIGINAL\n"); + printf("id: %ld\nseqno: %d\nmessage: ", data->id, htobe16(data->seqno)); + for(int i = 0; i < data->length; i++) { + printf("%c", data->data[i]); + } + printf("\n"); + */ } // Concat hash2 to hash1 (hash1 is modified) diff --git a/src/node.c b/src/node.c index c2fa4af..3a44818 100644 --- a/src/node.c +++ b/src/node.c @@ -192,9 +192,9 @@ pub_data *get_data(uint64_t id) { } // Take data as args and create a pub_data structure in the heap -pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data) { +pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data) { pub_data *new_data = (pub_data*) malloc(sizeof(pub_data)); - unsigned char *_data = (unsigned char*) malloc(len); + char *_data = (char*) malloc(len); if (_data == NULL) { print_error("Failed to allocate memory for copying the data !"); return NULL; @@ -213,7 +213,7 @@ pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned cha } // A node state TLV was received and either no data associated to it's id is in our data list or the data was updated, return -1 if an error occurend, 0 if nothing had to be done and 1 if something was updated/added -int add_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data, pub_data *found) { +int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found) { // Check if it's our own id if(id == NODE_ID) { // wtf @@ -249,7 +249,7 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data // Updata message free(found->data); - found->data = (unsigned char*) malloc(len); + found->data = (char*) malloc(len); memcpy(found->data, data, len); printf(">> Updated %li's published data.\n", id); @@ -483,12 +483,12 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i int error_while_sending = 0; // Creating the struct to send out with sendmsg - struct msghdr packet_tlv_send_out = { - .msg_name = dest, - .msg_namelen = sizeof(struct sockaddr_in6), - .msg_iov = vec_buff, - .msg_iovlen = 1 // We have only one iovec buffer. But if we had 2, we would write 2. - }; + struct msghdr packet_tlv_send_out; + memset(&packet_tlv_send_out, 0, sizeof(struct msghdr)); + packet_tlv_send_out.msg_name = dest; + packet_tlv_send_out.msg_namelen = sizeof(struct sockaddr_in6); + packet_tlv_send_out.msg_iov = vec_buff; + packet_tlv_send_out.msg_iovlen = 1; // We have only one iovec buffer. But if we had 2, we would write 2. int response_code = sendmsg(socket_num, &packet_tlv_send_out, 0); if (response_code < 0) { @@ -504,6 +504,7 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i if (error_while_sending == 1) { return -1; } else { + printf(">> Packet successfully sent to peer. %d bytes sent.\n", response_code); return 0; } } @@ -794,8 +795,8 @@ int add_message(char * message, int message_len){ our_data->seqno = (our_data->seqno + 1) % 65535; our_data->length = message_len; free(our_data->data); - our_data->data = (unsigned char*) malloc(message_len); - memcpy(our_data->data, (unsigned char *) message, message_len); + our_data->data = (char*) malloc(message_len); + memcpy(our_data->data, message, message_len); print_debug(">> Message added."); @@ -990,6 +991,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * pdata = get_data(be64toh(*id)); + printf("Received: %ld\n", be64toh(*id)); + // If data is found for this id then we check that both hashes are the same if(pdata != NULL) { print_debug(">> Comparing hashes..."); @@ -1001,13 +1004,13 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; - print_debug(">> Both hashs are the same, nothing to do."); + print_debug(">> Both hashes are the same, nothing to do."); break; } } - print_debug(">> No hash found, or hashs are different, sending back node state request."); + print_debug(">> No hash found, or hashes are different, sending back node state request."); // If no pub_data was found or the hashes differ then we send a node state request build_node_state_req(&new_tlv, be64toh(*id)); add_tlv(&pack, &new_tlv, sender, socket_num); @@ -1033,6 +1036,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * add_tlv(&pack, &new_tlv, sender, socket_num); } else { print_debug(">> Found no data for the requested node, skipping..."); + printf("Received: %ld\n", be64toh(*id)); } // The position is updated @@ -1063,7 +1067,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * fflush(0); } printf("\n"); - for(int x = 0; x < tlv_len; x++){ + for(int x = 0; x < tlv_len - 26; x++){ printf("%c", cur_message[x]); fflush(0); } @@ -1071,16 +1075,33 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // Check if it has the right hash pub_data pdata_check = (pub_data) { - .length = tlv_len, - .id = *id, - .seqno = *seqno, - .data = (unsigned char*) malloc(tlv_len) + .length = tlv_len - 26, + .id = be64toh(*id), + .seqno = be16toh(*seqno), + .data = (char*) malloc(tlv_len) }; - memcpy(pdata_check.data, cur_message, tlv_len); + memcpy(pdata_check.data, cur_message, tlv_len - 26); hash_data(&pdata_check, hash2); + print_debug(">> Built message: "); + printf("Type : %d\n", data[pos] ); + printf("Length : %d\n", pdata_check.length ); + printf("Node ID : %lu\n", pdata_check.id); + printf("Seqno : %hu\n", pdata_check.seqno); + printf("Hash :"); + for(int x = 0; x < 16; x++){ + printf("%02x", hash2[x]); + fflush(0); + } + printf("\n"); + for(int x = 0; x < tlv_len - 26; x++){ + printf("%c", pdata_check.data[x]); + fflush(0); + } + printf("\n"); + if(memcmp(hash2, cur_hash, 16) != 0) { print_debug(">> Malformed hash."); printf("\x1b[31m[DEBUG]\x1b[0m >> Calculated : "); @@ -1104,6 +1125,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * break; } + free(pdata_check.data); /* if (DEBUG_LEVEL > 0) { @@ -1137,7 +1159,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * } // Else, we update the data - int rc = add_data(tlv_len - 26, be64toh(*id), be16toh(*seqno), (unsigned char*) cur_message, pdata); + int rc = add_data(tlv_len - 26, be64toh(*id), be16toh(*seqno), cur_message, pdata); if (rc < 0) { print_error("Error while adding node state !"); } @@ -1154,7 +1176,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * cur_message = data + pos + 2; // Print exactly new_tlv.length characters from new_tlv.message - sprintf(warn, ">> WARNING:\n%%.%ds", tlv_len + 1); + sprintf(warn, ">> WARNING:\n%%.%ds\n", tlv_len + 1); printf(warn, cur_message); // The position is updated @@ -1166,7 +1188,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * strcpy(warn, "Packet is malformed."); print_debug(">> Malformed packet, we won't treat it."); print_debug(">> Sending back a Warning to sender."); - build_warning(&new_tlv,(unsigned char *) warn, strlen(warn)); + build_warning(&new_tlv, warn, strlen(warn)); add_tlv(&pack, &new_tlv, sender, socket_num); return -1; @@ -1371,7 +1393,7 @@ int run_node(int sock_fd){ printf("\x1b[31m[DEBUG]\x1b[0m >> Adding following message to the table : “%s”\n", input_buffer ); } // Add message to the message table. - if (add_message(input_buffer, bytes) < 0) { + if (add_message(input_buffer, bytes-1) < 0) { print_debug(">> Error while trying to add the message to the list of messages, please try again.."); // Reset buffer memset(input_buffer, 0, sizeof(input_buffer)); diff --git a/src/node.h b/src/node.h index af0e515..b27d116 100644 --- a/src/node.h +++ b/src/node.h @@ -43,7 +43,7 @@ typedef struct pub_data { unsigned char length; uint64_t id; uint16_t seqno; - unsigned char *data; + char *data; } pub_data; // General list @@ -62,7 +62,8 @@ typedef struct list { // The node ID // #define NODE_ID 42675882021843277 -#define NODE_ID 13809235719890846928 +// #define NODE_ID 13809235719890846928 +static uint64_t NODE_ID = 80927976239; // The number of neighbours // The neighbour table has 15 entries @@ -141,9 +142,9 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port); pub_data *get_data(uint64_t id); // Take data as args and create a pub_data structure in the heap -pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data); +pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data); // add new data to data list -int add_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data, pub_data *found); +int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found); #endif diff --git a/src/tlv.c b/src/tlv.c index 0dcf492..4614db8 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -1,33 +1,5 @@ #include "tlv.h" -// TODO this can be deleted ? -// creer un tlv -int build_tlv(tlv *tlv, cmd_token token) { - switch(token.type) { - case NEIGHBOUR_REQ: - // a remplir - break; - case NETWORK_STATE_REQ: - // a remplir - break; - case NODE_STATE_REQ: - // a remplir - break; - case POST: - // a remplir - break; - case ERROR: - printf("Wrong format, use 'req {neighbour | network state | node state}' or 'post {message}'"); - break; - default: - perror("Unrecognized tlv type."); - return -1; - } - - return -1; -} -// end deletion - int build_pad1(tlv *tlv) { // Free the previously allocated memory free(tlv->pad1); @@ -119,7 +91,7 @@ int build_network_hash(tlv *tlv, list *data_list) { new->type = 4; new->length = 16; - hash_network(data_list, (unsigned char*) new->network_hash); + hash_network(data_list, new->network_hash); tlv->network_hash = new; @@ -144,7 +116,7 @@ int build_network_state_req(tlv *tlv) { return 0; } -int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data) { +int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data) { // Free the previously allocated memory free(tlv->pad1); @@ -160,7 +132,7 @@ int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *d new->seqno = htobe16(seqno); pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; - hash_data(&pdata, (unsigned char*) new->node_hash); + hash_data(&pdata, new->node_hash); tlv->node_hash = new; @@ -174,7 +146,6 @@ int build_node_state_req(tlv *tlv, uint64_t node_id) { node_state_req *new = (node_state_req*) malloc(sizeof(node_state_req)); memset(new, 0, sizeof(node_state_req)); - if(new == NULL) return -1; @@ -187,7 +158,7 @@ int build_node_state_req(tlv *tlv, uint64_t node_id) { return 0; } -int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data, size_t data_len) { +int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len) { // Free the previously allocated memory free(tlv->pad1); @@ -212,14 +183,14 @@ int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char * memcpy(new->data, data, len); pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; - hash_data(&pdata, (unsigned char*) new->node_hash); + hash_data(&pdata, new->node_hash); tlv->node_state = new; return 0; } -int build_warning(tlv *tlv, unsigned char *message, size_t message_len) { +int build_warning(tlv *tlv, char *message, size_t message_len) { // Free the previously allocated memory free(tlv->pad1); diff --git a/src/tlv.h b/src/tlv.h index d2ebff5..de4399a 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -122,9 +122,9 @@ int build_neighbour_req(union tlv *tlv); int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port); int build_network_hash(tlv *tlv, list *data_list); int build_network_state_req(tlv *tlv); -int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data); +int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data); int build_node_state_req(tlv *tlv, uint64_t node_id); -int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data, size_t data_len); -int build_warning(tlv *tlv, unsigned char *message, size_t message_len); +int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len); +int build_warning(tlv *tlv, char *message, size_t message_len); #endif From e5ec8b36aef3833492904853ed8b36f852425d7a Mon Sep 17 00:00:00 2001 From: nis Date: Sun, 3 May 2020 23:43:17 +0200 Subject: [PATCH 22/25] adding tlv to packet error fix --- src/hash.c | 2 - src/node.c | 189 +++++++++++++++++++++++++++++++---------------------- src/node.h | 2 +- src/tlv.c | 24 ++----- src/tlv.h | 6 +- 5 files changed, 121 insertions(+), 102 deletions(-) diff --git a/src/hash.c b/src/hash.c index 853d474..c1712e5 100644 --- a/src/hash.c +++ b/src/hash.c @@ -33,8 +33,6 @@ void hash_data(pub_data *data, unsigned char *buf) { printf("%02x", buf[i]); } printf("\n"); - - exit(1); */ } diff --git a/src/node.c b/src/node.c index 3a44818..bdcd4b6 100644 --- a/src/node.c +++ b/src/node.c @@ -394,7 +394,11 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { if(pack->length + len > 1020) { errval = send_packet((char*) pack, pack->length, dest, socket_num); - *pack = (packet) {.magic = 95, .version = 1, .length = 0}; + + pack->magic = 95; + pack->version = 1; + pack->length = 0; + memset(pack->body, 0, 1020); sent = 1; } @@ -403,7 +407,11 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { // we just ignore that padding, and send out the packet. if(pack->length >= 1020) { errval = send_packet((char*) pack, pack->length, dest, socket_num); - *pack = (packet) {.magic = 95, .version = 1, .length = 0}; + + pack->magic = 95; + pack->version = 1; + pack->length = 0; + memset(pack->body, 0, 1020); sent = 1; } @@ -412,47 +420,73 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { // Copy data from tlv into body switch(type) { case 1: - memcpy(pack->body + pack->length, tlv->pad1, 1); + memcpy(pack->body + pack->length, (char*) &tlv->pad1->type, 1); pack->length += 1; break; case 2: - memcpy(pack->body + pack->length, tlv->padn, len); + memcpy(pack->body + pack->length, (char*) &tlv->padn->type, 1); + memcpy(pack->body + pack->length + 1, (char*) &tlv->padn->length, 1); + memcpy(pack->body + pack->length + 2, (char*) &tlv->padn->mbz, tlv->padn->length); + pack->length += len; break; case 3: - memcpy(pack->body + pack->length, tlv->neighbour, len); + memcpy(pack->body + pack->length, (char*) &tlv->neighbour->type, 1); + memcpy(pack->body + pack->length + 1, (char*) &tlv->neighbour->length, 1); + pack->length += len; break; case 4: - memcpy(pack->body + pack->length, tlv->network_hash, len); + memcpy(pack->body + pack->length, (char*) &tlv->network_hash->type, 1); + memcpy(pack->body + pack->length + 1, (char*) &tlv->network_hash->length, 1); + memcpy(pack->body + pack->length + 2, (char*) &tlv->network_hash->network_hash, 16); + pack->length += len; break; case 5: - memcpy(pack->body + pack->length, tlv->network_state_req, len); + memcpy(pack->body + pack->length, (char*) &tlv->network_state_req->type, 1); + memcpy(pack->body + pack->length + 1, (char*) &tlv->network_state_req->length, 1); + pack->length += len; break; case 6: - memcpy(pack->body + pack->length, tlv->node_hash, len); + memcpy(pack->body + pack->length, (char*) &tlv->node_hash->type, 1); + memcpy(pack->body + pack->length + 1, (char*) &tlv->node_hash->length, 1); + memcpy(pack->body + pack->length + 2, (char*) &tlv->node_hash->node_id, 8); + memcpy(pack->body + pack->length + 10, (char*) &tlv->node_hash->seqno, 2); + memcpy(pack->body + pack->length + 12, (char*) &tlv->node_hash->node_hash, 16); + pack->length += len; break; case 7: - memcpy(pack->body + pack->length, tlv->node_state_req, len); + memcpy(pack->body + pack->length, (char*) &tlv->node_state_req->type, 1); + memcpy(pack->body + pack->length + 1, (char*) &tlv->node_state_req->length, 1); + pack->length += len; break; case 8: - memcpy(pack->body + pack->length, tlv->node_state, len); + memcpy(pack->body + pack->length, (char*) &tlv->node_state->type, 1); + memcpy(pack->body + pack->length + 1, (char*) &tlv->node_state->length, 1); + memcpy(pack->body + pack->length + 2, (char*) &tlv->node_state->node_id, 8); + memcpy(pack->body + pack->length + 10, (char*) &tlv->node_state->seqno, 2); + memcpy(pack->body + pack->length + 12, (char*) &tlv->node_state->node_hash, 16); + memcpy(pack->body + pack->length + 28, (char*) &tlv->node_state->data, tlv->node_state->length - 26); + pack->length += len; break; case 9: - memcpy(pack->body + pack->length, tlv->warning, len); + memcpy(pack->body + pack->length, (char*) &tlv->warning->type, 1); + memcpy(pack->body + pack->length + 1, (char*) &tlv->warning->length, 1); + memcpy(pack->body + pack->length + 2, (char*) &tlv->warning->message, tlv->warning->length); + pack->length += len; break; @@ -473,7 +507,8 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { // Send length bytes from packet int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, int socket_num) { - ((packet*) packet_buff)->length = htobe16(((packet*) packet_buff)->length); + uint16_t be_len = htobe16(*(uint16_t*) (packet_buff + 2)); + memcpy(packet_buff + 2, &be_len, 2); // Vectorized buffer struct iovec vec_buff[1]; @@ -512,63 +547,80 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i // Send a single TLV to the specified addresses, return -1 if an error was encountered, 0 otherwise int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { char type = tlv->pad1->type; - unsigned char len; packet pack = (packet) {.magic = 95, .version = 1, .length = 0}; memset(pack.body, 0, 1020); // Copy data from tlv into body - switch(type) { + switch(type) { case 1: - memcpy(pack.body, tlv->pad1, 1); + memcpy(pack.body + pack.length, (char*) &tlv->pad1->type, 1); pack.length += 1; break; case 2: - len = tlv->padn->length + 2; - memcpy(pack.body, tlv->padn, len); - pack.length += len; + memcpy(pack.body + pack.length, (char*) &tlv->padn->type, 1); + memcpy(pack.body + pack.length + 1, (char*) &tlv->padn->length, 1); + memcpy(pack.body + pack.length + 2, (char*) &tlv->padn->mbz, tlv->padn->length); + + pack.length += tlv->padn->length + 2; break; case 3: - len = tlv->neighbour->length + 2; - memcpy(pack.body, tlv->neighbour, len); - pack.length += len; + memcpy(pack.body + pack.length, (char*) &tlv->neighbour->type, 1); + memcpy(pack.body + pack.length + 1, (char*) &tlv->neighbour->length, 1); + + pack.length += tlv->neighbour->length + 2; break; case 4: - len = tlv->network_hash->length + 2; - memcpy(pack.body, tlv->network_hash, len); - pack.length += len; + memcpy(pack.body + pack.length, (char*) &tlv->network_hash->type, 1); + memcpy(pack.body + pack.length + 1, (char*) &tlv->network_hash->length, 1); + memcpy(pack.body + pack.length + 2, (char*) &tlv->network_hash->network_hash, 16); + + pack.length += tlv->network_hash->length + 2; break; case 5: - len = tlv->network_state_req->length + 2; - memcpy(pack.body, tlv->network_state_req, len); - pack.length += len; + memcpy(pack.body + pack.length, (char*) &tlv->network_state_req->type, 1); + memcpy(pack.body + pack.length + 1, (char*) &tlv->network_state_req->length, 1); + + pack.length += tlv->network_state_req->length + 2; break; case 6: - len = tlv->node_hash->length + 2; - memcpy(pack.body, tlv->node_hash, len); - pack.length += len; + memcpy(pack.body + pack.length, (char*) &tlv->node_hash->type, 1); + memcpy(pack.body + pack.length + 1, (char*) &tlv->node_hash->length, 1); + memcpy(pack.body + pack.length + 2, (char*) &tlv->node_hash->node_id, 8); + memcpy(pack.body + pack.length + 10, (char*) &tlv->node_hash->seqno, 2); + memcpy(pack.body + pack.length + 12, (char*) &tlv->node_hash->node_hash, 16); + + pack.length += tlv->node_hash->length + 2; break; case 7: - len = tlv->node_state_req->length + 2; - memcpy(pack.body, tlv->node_state_req, len); - pack.length += len; + memcpy(pack.body + pack.length, (char*) &tlv->node_state_req->type, 1); + memcpy(pack.body + pack.length + 1, (char*) &tlv->node_state_req->length, 1); + + pack.length += tlv->node_state_req->length + 2; break; case 8: - len = tlv->node_state->length + 2; - memcpy(pack.body, tlv->node_state, len); - pack.length += len; + memcpy(pack.body + pack.length, (char*) &tlv->node_state->type, 1); + memcpy(pack.body + pack.length + 1, (char*) &tlv->node_state->length, 1); + memcpy(pack.body + pack.length + 2, (char*) &tlv->node_state->node_id, 8); + memcpy(pack.body + pack.length + 10, (char*) &tlv->node_state->seqno, 2); + memcpy(pack.body + pack.length + 12, (char*) &tlv->node_state->node_hash, 16); + memcpy(pack.body + pack.length + 28, (char*) &tlv->node_state->data, tlv->node_state->length - 26); + + pack.length += tlv->node_state->length + 2; break; case 9: - len = tlv->warning->length + 2; - memcpy(pack.body, tlv->warning, len); - pack.length += len; + memcpy(pack.body + pack.length, (char*) &tlv->warning->type, 1); + memcpy(pack.body + pack.length + 1, (char*) &tlv->warning->length, 1); + memcpy(pack.body + pack.length + 2, (char*) &tlv->warning->message, tlv->warning->length); + + pack.length += tlv->warning->length + 2; break; default: @@ -823,11 +875,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // The TLV we are going to send back. tlv new_tlv; - // The tlv we are currently looking at. - tlv cur_tlv; - new_tlv.pad1 = NULL; - cur_tlv.pad1 = NULL; + list * tmp_list; pub_data * pdata; @@ -969,9 +1018,11 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * while(tmp_list != NULL) { pdata = (pub_data*) tmp_list->data; - build_node_hash(&new_tlv, pdata->id, pdata->seqno, pdata->data); + build_node_hash(&new_tlv, pdata->id, pdata->seqno, pdata->data, pdata->length); add_tlv(&pack, &new_tlv, sender, socket_num); tmp_list = tmp_list->next; + + printf("%lu\n", *(uint64_t*) (pack.body + 2)); } // The position is updated @@ -991,8 +1042,6 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * pdata = get_data(be64toh(*id)); - printf("Received: %ld\n", be64toh(*id)); - // If data is found for this id then we check that both hashes are the same if(pdata != NULL) { print_debug(">> Comparing hashes..."); @@ -1015,6 +1064,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * build_node_state_req(&new_tlv, be64toh(*id)); add_tlv(&pack, &new_tlv, sender, socket_num); + printf(">> Sending node state request for node %lu...\n", be64toh(*id)); + // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; @@ -1036,7 +1087,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * add_tlv(&pack, &new_tlv, sender, socket_num); } else { print_debug(">> Found no data for the requested node, skipping..."); - printf("Received: %ld\n", be64toh(*id)); + printf("Skipped TLV:\nType: %d\nLength: %d\nNode ID: %lu\n", data[pos], data[pos+1], be64toh(*id)); } // The position is updated @@ -1087,7 +1138,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * print_debug(">> Built message: "); printf("Type : %d\n", data[pos] ); - printf("Length : %d\n", pdata_check.length ); + printf("Length : %d\n", pdata_check.length + 26); printf("Node ID : %lu\n", pdata_check.id); printf("Seqno : %hu\n", pdata_check.seqno); printf("Hash :"); @@ -1170,8 +1221,6 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * case 9: print_debug(">> \aReceived warning !"); // We received a warning tlv so it's message is printed - cur_tlv.warning = (warning*) (data + pos); - tlv_len = data[pos+1]; cur_message = data + pos + 2; @@ -1269,46 +1318,32 @@ int t_get_network_state(int sock_fd){ 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; - } + tlv new_tlv; + memset(&new_tlv, 0, sizeof(union tlv)); // 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 = htobe16(peer->port); - receiver->sin6_scope_id = 0; + struct sockaddr_in6 receiver; + memset(&receiver, 0, sizeof(struct sockaddr_in6)); + + receiver.sin6_family = AF_INET6; + receiver.sin6_addr = peer->ip; + receiver.sin6_port = htobe16(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) { + if (build_network_hash(&new_tlv, data_list) < 0) { print_error("Error while building a network hash."); return -1; } else { - if (send_single_tlv(new_tlv, receiver, sock_fd) < 0) { + 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; - } + + tmp_list = tmp_list->next; } } return 0; diff --git a/src/node.h b/src/node.h index b27d116..dbce63b 100644 --- a/src/node.h +++ b/src/node.h @@ -63,7 +63,7 @@ typedef struct list { // The node ID // #define NODE_ID 42675882021843277 // #define NODE_ID 13809235719890846928 -static uint64_t NODE_ID = 80927976239; +#define NODE_ID 1312 // The number of neighbours // The neighbour table has 15 entries diff --git a/src/tlv.c b/src/tlv.c index 4614db8..ebf9830 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -116,7 +116,7 @@ int build_network_state_req(tlv *tlv) { return 0; } -int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data) { +int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, unsigned char length) { // Free the previously allocated memory free(tlv->pad1); @@ -131,7 +131,7 @@ int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data) { new->node_id = htobe64(node_id); new->seqno = htobe16(seqno); - pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; + pub_data pdata = (pub_data) {.length = length, .id = node_id, .seqno = seqno, .data = data}; hash_data(&pdata, new->node_hash); tlv->node_hash = new; @@ -165,24 +165,16 @@ int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, siz node_state *new = (node_state*) malloc(sizeof(node_state)); memset(new, 0, sizeof(node_state)); - int len = data_len + 26; - if(new == NULL) return -1; - // en mettant cet octet à 0 on est surs de traiter un champ data de taille 192 max - if(len > 192) { - data[192] = 0; - len = 192; - } - new->type = 8; - new->length = 26 + len; + new->length = data_len + 26; new->node_id = htobe64(node_id); new->seqno = htobe16(seqno); - memcpy(new->data, data, len); + memcpy(new->data, data, data_len); - pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; + pub_data pdata = (pub_data) {.length = data_len, .id = node_id, .seqno = seqno, .data = data}; hash_data(&pdata, new->node_hash); tlv->node_state = new; @@ -202,12 +194,6 @@ int build_warning(tlv *tlv, char *message, size_t message_len) { if(new == NULL) return -1; - // en mettant cet octet à 0 on est surs de traiter un champ message de taille 256 max - if(len > 256) { - message[256] = 0; - len = 256; - } - new->type = 9; new->length = len; memcpy(new->message, message, len); diff --git a/src/tlv.h b/src/tlv.h index de4399a..d976faf 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -85,14 +85,14 @@ typedef struct node_state { uint64_t node_id; uint16_t seqno; unsigned char node_hash[16]; - unsigned char data[192]; + char data[192]; } node_state; // 2 octets min, 258 ocets max (unsigned char 0 -> 255) typedef struct warning { unsigned char type; unsigned char length; - unsigned char message[256]; + char message[256]; } warning; typedef union tlv { @@ -122,7 +122,7 @@ int build_neighbour_req(union tlv *tlv); int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port); int build_network_hash(tlv *tlv, list *data_list); int build_network_state_req(tlv *tlv); -int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data); +int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, unsigned char length); int build_node_state_req(tlv *tlv, uint64_t node_id); int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len); int build_warning(tlv *tlv, char *message, size_t message_len); From bdec3db1c6b5e32fbb1ec824a936554a6ff7d1c1 Mon Sep 17 00:00:00 2001 From: nis Date: Mon, 4 May 2020 00:23:25 +0200 Subject: [PATCH 23/25] node state request fix --- src/node.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/node.c b/src/node.c index bdcd4b6..4406d67 100644 --- a/src/node.c +++ b/src/node.c @@ -467,6 +467,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { case 7: memcpy(pack->body + pack->length, (char*) &tlv->node_state_req->type, 1); memcpy(pack->body + pack->length + 1, (char*) &tlv->node_state_req->length, 1); + memcpy(pack->body + pack->length + 2, (char*) &tlv->node_state_req->node_id, 8); pack->length += len; @@ -600,6 +601,7 @@ int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { case 7: memcpy(pack.body + pack.length, (char*) &tlv->node_state_req->type, 1); memcpy(pack.body + pack.length + 1, (char*) &tlv->node_state_req->length, 1); + memcpy(pack.body + pack.length + 2, (char*) &tlv->node_state_req->node_id, 8); pack.length += tlv->node_state_req->length + 2; @@ -1087,7 +1089,6 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * add_tlv(&pack, &new_tlv, sender, socket_num); } else { print_debug(">> Found no data for the requested node, skipping..."); - printf("Skipped TLV:\nType: %d\nLength: %d\nNode ID: %lu\n", data[pos], data[pos+1], be64toh(*id)); } // The position is updated @@ -1252,6 +1253,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * send_packet((char*) &pack, pack.length, sender, socket_num); } print_data(data_list); + return 0; } From e757389e32d95b3bb5230b25005a6616d8aa0074 Mon Sep 17 00:00:00 2001 From: nis Date: Mon, 4 May 2020 00:40:54 +0200 Subject: [PATCH 24/25] YES --- src/hash.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hash.c b/src/hash.c index c1712e5..9c05afb 100644 --- a/src/hash.c +++ b/src/hash.c @@ -79,8 +79,9 @@ void hash_trunc(unsigned char *hash32oct, unsigned char *buf) { void concat_data(pub_data *data, unsigned char *buf) { // Turn seqno to big endian uint16_t seqno = htobe16(data->seqno); + uint64_t id = htobe64(data->id); - if (memcpy(buf, (char*) &data->id, 8) == NULL) { + if (memcpy(buf, (char*) &id, 8) == NULL) { print_debug(">> Concat the data (id) didn't work !"); } if (memcpy(buf+8, (char*) &seqno, 2) == NULL) { From e1364bc58e9075cc7e0fbb09cacc2bb37f48d46a Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 4 May 2020 12:59:08 +0200 Subject: [PATCH 25/25] Changed debug print, Added memcmp in get_neibourgh Found bug with port number conversion --- src/debug.c | 2 +- src/node.c | 39 ++++++++++++++++++++++----------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/debug.c b/src/debug.c index a0bbbcd..c4a8756 100644 --- a/src/debug.c +++ b/src/debug.c @@ -55,7 +55,7 @@ void print_data(list * l){ print_debug(">> Printing messages we know of so far :"); int nbr_msg = 0; setlocale(LC_ALL, "en_US.utf8"); - print_debug(">> Peer ID | Seqno | Length | Message "); + print_debug(">> Peer ID | Seqno | Length | Message "); while(l != NULL){ pub_data * message = l->data; printf("\x1b[31m[DEBUG]\x1b[0m >> %li | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data); diff --git a/src/node.c b/src/node.c index 4406d67..9cca632 100644 --- a/src/node.c +++ b/src/node.c @@ -113,7 +113,8 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) { // check for same ip and same port peer = (neighbour_peer*) tmp->data; - if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0 && peer->port == port) { + if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0 + && memcmp(&peer->port, &port, sizeof(int16_t)) == 0) { return peer; } @@ -151,10 +152,12 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port) { time(&curtime); peer->last_seen = curtime; + list * tmp = neighbour_list; + // set new peer as head of list list *node = (list*) malloc(sizeof(list)); node->data = (void*) peer; - node->next = neighbour_list; + node->next = tmp; neighbour_list = node; return 1; } @@ -397,7 +400,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { pack->magic = 95; pack->version = 1; - pack->length = 0; + pack->length = 0; memset(pack->body, 0, 1020); sent = 1; @@ -407,7 +410,7 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { // we just ignore that padding, and send out the packet. if(pack->length >= 1020) { errval = send_packet((char*) pack, pack->length, dest, socket_num); - + pack->magic = 95; pack->version = 1; pack->length = 0; @@ -540,7 +543,9 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i if (error_while_sending == 1) { return -1; } else { - printf(">> Packet successfully sent to peer. %d bytes sent.\n", response_code); + if (DEBUG_LEVEL > 0) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Packet successfully sent to peer. %d bytes sent.\n", response_code); + } return 0; } } @@ -760,9 +765,9 @@ int validate_tlv(char *data, int pos, uint16_t packet_len){ uint64_t *id = (uint64_t*) (data + pos + 2); pub_data pdata = (pub_data) { - .length = tlv_len, - .id = *id, - .seqno = *seqno, + .length = tlv_len, + .id = *id, + .seqno = *seqno, .data = (unsigned char*) malloc(tlv_len) }; @@ -772,7 +777,7 @@ int validate_tlv(char *data, int pos, uint16_t packet_len){ unsigned char hash[16]; memcpy(pdata.data, cur_message, tlv_len); - + hash_data(&pdata, hash); if(memcmp(hash, cur_hash, 16) != 0) { @@ -783,7 +788,7 @@ int validate_tlv(char *data, int pos, uint16_t packet_len){ printf("%02x", hash[x]); fflush(0); } - + printf("\n"); printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); @@ -1127,14 +1132,14 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * // Check if it has the right hash pub_data pdata_check = (pub_data) { - .length = tlv_len - 26, - .id = be64toh(*id), - .seqno = be16toh(*seqno), + .length = tlv_len - 26, + .id = be64toh(*id), + .seqno = be16toh(*seqno), .data = (char*) malloc(tlv_len) }; memcpy(pdata_check.data, cur_message, tlv_len - 26); - + hash_data(&pdata_check, hash2); print_debug(">> Built message: "); @@ -1162,7 +1167,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 * printf("%02x", hash2[x]); fflush(0); } - + printf("\n"); printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); @@ -1468,7 +1473,7 @@ int run_node(int sock_fd){ // Treat incoming packets. int work_tlv_status = listen_for_packets(output_buffer, bytes, &sender, sock_fd); if (work_tlv_status < 0) { - print_debug(">> Error while treating the incoming packet."); + print_error("Error while treating the incoming packet."); } // Reset buffer memset(output_buffer, 0, sizeof(output_buffer)); @@ -1522,7 +1527,7 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ return -3; } - root_peer->port = root_peer_port; + root_peer->port = (int16_t) root_peer_port; root_peer->is_temporary = 0; root_peer->last_seen = root_peer_seen;