From 1295a91f42dfd238a6cd2d61ac4ad17f7655def7 Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 31 Mar 2020 22:35:56 +0200 Subject: [PATCH 01/12] =?UTF-8?q?Ajout=20d'une=20premi=C3=A8re=20makefile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..edf58ae --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +TARGET ?= dazibao +SRC_DIRS ?= ./src/* +CC := gcc -Wall +SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s) +OBJS := $(addsuffix .o,$(basename $(SRCS))) +DEPS := $(OBJS:.o=.d) + +INC_DIRS := $(shell find $(SRC_DIRS) -type d) +INC_FLAGS := $(addprefix -I,$(INC_DIRS)) + +$(TARGET): $(OBJS) + $(CC) $(LDFLAGS) $(OBJS) -o $@ $(LOADLIBES) $(LDLIBS) + +.PHONY: clean +clean: + $(RM) $(TARGET) $(OBJS) $(DEPS) + +-include $(DEPS) From f90caceeb4f2956e0099e42bbffc892bedfe7115 Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 31 Mar 2020 22:47:48 +0200 Subject: [PATCH 02/12] Ajouts des flags pour openSSL --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index edf58ae..b8b2f19 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ TARGET ?= dazibao SRC_DIRS ?= ./src/* -CC := gcc -Wall +CC := gcc -Wall -lssl -lcrypto SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s) OBJS := $(addsuffix .o,$(basename $(SRCS))) DEPS := $(OBJS:.o=.d) From 4a616dbfe05a8f637e69b5b75ed50b625db43155 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 1 Apr 2020 17:51:25 +0200 Subject: [PATCH 03/12] Ajout de la signature de la fonction d'envoie de paquet --- src/node.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/node.h b/src/node.h index d4ba94b..0b709b4 100644 --- a/src/node.h +++ b/src/node.h @@ -77,6 +77,12 @@ void add_tlv(packet *packet, tlv *tlv, char type); int send_packet(struct tlv_list tlvs_to_send, ); +/* Prend un tlv, et construit un paquet pour ensuite l'envoyer à la liste + * des paires. + * Retourne -1 en cas d'échec, 0 en cas de succès. + */ +int send_tlv(struct tlv, struct sockaddr_in6 * sender_list[], int sender_list_size); + // threaded functions void t_ask_for_more_peers(); From d637d8c1ef3eb62e1b131aeae6d670c9c26fbd4e Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 1 Apr 2020 17:51:54 +0200 Subject: [PATCH 04/12] Suppression temporaire de la fonction send_paquet. --- src/node.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node.h b/src/node.h index 0b709b4..fbed67f 100644 --- a/src/node.h +++ b/src/node.h @@ -75,7 +75,7 @@ int work_with_tlvs(struct packet received_packet, char * data_from_packet[], str void add_tlv(packet *packet, tlv *tlv, char type); -int send_packet(struct tlv_list tlvs_to_send, ); +int send_packet(); /* Prend un tlv, et construit un paquet pour ensuite l'envoyer à la liste * des paires. From b7478364abe752d3fce3bd7d34f86e7f77868efe Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 1 Apr 2020 17:54:01 +0200 Subject: [PATCH 05/12] Ajout de la signature de la fonction envoyant plusieurs TLV --- src/node.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/node.h b/src/node.h index fbed67f..006f803 100644 --- a/src/node.h +++ b/src/node.h @@ -83,8 +83,14 @@ int send_packet(); */ int send_tlv(struct tlv, struct sockaddr_in6 * sender_list[], int sender_list_size); -// threaded functions +/* Prend une liste de tlv, et construit un paquet pour ensuite les envoyer à la liste + * des paires. Chaque pair recevera la même liste de TLV. + * Retourne -1 en cas d'échec, 0 en cas de succès. + */ +int send_tlvs(struct list * tlv_list, struct sockaddr_in6 * sender_list[], int sender_list_size); + +// threaded functions void t_ask_for_more_peers(); void t_update_neighbours(); From e1276448f1a0a3428ed83843f2d79d7288237fe6 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 1 Apr 2020 17:56:17 +0200 Subject: [PATCH 06/12] Traduction des commentaires en anglais, Modification de la signature des deux fonctions --- src/node.c | 5 +++++ src/node.h | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/node.c b/src/node.c index 499651d..dff0c29 100644 --- a/src/node.c +++ b/src/node.c @@ -45,6 +45,11 @@ neighbour_peer *get_random_neighbour() { /* ---- Fin fonctions utilitaires ---- */ +// This function +int send_tlv(struct tlv, struct sockaddr_in6 * sender_list[], int sender_list_size){ + +} + // 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, short packet_len){ diff --git a/src/node.h b/src/node.h index 006f803..38fbc1b 100644 --- a/src/node.h +++ b/src/node.h @@ -77,18 +77,18 @@ void add_tlv(packet *packet, tlv *tlv, char type); int send_packet(); -/* Prend un tlv, et construit un paquet pour ensuite l'envoyer à la liste +/* Takes a TLV and sends it over to everyone in the list of addresses. * des paires. * Retourne -1 en cas d'échec, 0 en cas de succès. */ -int send_tlv(struct tlv, struct sockaddr_in6 * sender_list[], int sender_list_size); +int send_tlv(struct tlv tlv_to_send, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size); /* Prend une liste de tlv, et construit un paquet pour ensuite les envoyer à la liste * des paires. Chaque pair recevera la même liste de TLV. * Retourne -1 en cas d'échec, 0 en cas de succès. */ -int send_tlvs(struct list * tlv_list, struct sockaddr_in6 * sender_list[], int sender_list_size); +int send_tlvs(struct list * tlv_list, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size); // threaded functions void t_ask_for_more_peers(); From b96655da72587cc738883c319e2c7f37281c2d3c Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 1 Apr 2020 18:43:53 +0200 Subject: [PATCH 07/12] Ajout de la fonction d'envoie de tlv --- src/node.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- src/node.h | 4 ++-- src/tlv.c | 4 ++-- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/src/node.c b/src/node.c index dff0c29..fbe2830 100644 --- a/src/node.c +++ b/src/node.c @@ -45,9 +45,63 @@ neighbour_peer *get_random_neighbour() { /* ---- Fin fonctions utilitaires ---- */ -// This function -int send_tlv(struct tlv, struct sockaddr_in6 * sender_list[], int sender_list_size){ +int send_tlv(struct tlv tlv_to_send, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int sock_num){ + debug_print("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(sizeof(tlv_to_send) > 1020){ + debug_print("Unable to send TLV, the size is bigger than 1020 bits."); + return -1; + } else { + pack.length = sizeof(tlv_to_send); + strcpy(pack.body, (char) tlv_to_send); + } + + // Casting the struct to a buffer. + packet_buff = (char *) pack; + + debug_print("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. + }; + + response_code = sendmsg((int) sock_num, &packet_tlv_send_out, 0); + if (response_code < 0) { + debug_print("Unable to send out the packet to peer %i", i); + error_while_sending = 1; + continue; + } else if (response_code < sizeof(packet_tlv_send_out)) { + debug_print("Sent out only part of the packet."); + error_while_sending = 1; + continue; + } else { + debug_print("Send out packet to peer %i", i); + } + } + + if (error_while_sending == 1) { + debug_print("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 diff --git a/src/node.h b/src/node.h index 38fbc1b..87fe047 100644 --- a/src/node.h +++ b/src/node.h @@ -81,14 +81,14 @@ int send_packet(); * des paires. * Retourne -1 en cas d'échec, 0 en cas de succès. */ -int send_tlv(struct tlv tlv_to_send, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size); +int send_tlv(struct tlv tlv_to_send, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num); /* Prend une liste de tlv, et construit un paquet pour ensuite les envoyer à la liste * des paires. Chaque pair recevera la même liste de TLV. * Retourne -1 en cas d'échec, 0 en cas de succès. */ -int send_tlvs(struct list * tlv_list, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size); +int send_tlvs(struct list * tlv_list, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num); // threaded functions void t_ask_for_more_peers(); diff --git a/src/tlv.c b/src/tlv.c index 2284815..e19cfd9 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -180,9 +180,9 @@ int build_warning(tlv *tlv, char *message) { new->type = 9; new->length = len; - memcpy(new->message, message, len); + memcpy(new->message, message, len); tlv->warning = new; return 0; -} \ No newline at end of file +} From 05e934672654a8cc3121148e352acab2b693bd37 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 1 Apr 2020 18:47:38 +0200 Subject: [PATCH 08/12] Traduction des commentaires vers l'anglais --- src/node.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/node.h b/src/node.h index 87fe047..0f7e2c6 100644 --- a/src/node.h +++ b/src/node.h @@ -78,15 +78,12 @@ void add_tlv(packet *packet, tlv *tlv, char type); int send_packet(); /* Takes a TLV and sends it over to everyone in the list of addresses. - * des paires. - * Retourne -1 en cas d'échec, 0 en cas de succès. + * Returns -1 in case of error, 0 otherwise. */ int send_tlv(struct tlv tlv_to_send, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num); - -/* Prend une liste de tlv, et construit un paquet pour ensuite les envoyer à la liste - * des paires. Chaque pair recevera la même liste de TLV. - * Retourne -1 en cas d'échec, 0 en cas de succès. +/* 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, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num); From 9c93785ca634e67a4e74cb942f66dd5079e522cf Mon Sep 17 00:00:00 2001 From: gonzalef Date: Tue, 7 Apr 2020 19:05:30 +0200 Subject: [PATCH 09/12] work with tlvs --- src/node.c | 266 ++++++++++++++++++++++++++++++++++++++++++++++------- src/node.h | 9 ++ src/tlv.c | 22 +++-- src/tlv.h | 11 ++- 4 files changed, 259 insertions(+), 49 deletions(-) diff --git a/src/node.c b/src/node.c index 499651d..689ce52 100644 --- a/src/node.c +++ b/src/node.c @@ -43,6 +43,121 @@ neighbour_peer *get_random_neighbour() { return (neighbour_peer*) tmp->data; } +// get data associated with id, if it doesn't exist return NULL +pub_data *get_data(long id) { + list *tmp = data_list; + pub_data *data; + + while(tmp != NULL) { + data = (pub_data*) tmp->data; + + if(data->id == id) + return data; + } + + return NULL; +} + +// Take data as args and create a pub_data structure in the heap +pub_data *copy_data(unsigned char len, long id, short seqno, char *data) { + pub_data *new_data = (pub_data*) malloc(sizeof(pub_data)); + char *_data = (char*) malloc(len); + new_data->length = len; + new_data->id = id; + new_data->seqno = seqno; + new_data->data = _data; + memcpy(_data, data, len); + + return new_data; +} + +// Add new data to data list +void add_data(unsigned char len, long id, short 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(seqno >= node_data->seqno) { + node_data->seqno = seqno ^ 1; + } + + return; + } + + // 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; + } + + // Find correct position for new data + list *tmp = data_list; + list *last = NULL; + list *new_node; + long 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; + } + + // 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; + } 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; + } + + // seqno is smaller so the new data allocated is freed and nothing else is done + free(new_data); + + return; + } + + // 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; +} + /* ---- Fin fonctions utilitaires ---- */ // We need to make sure the TLV announces a length that will no go onto @@ -61,7 +176,7 @@ int validate_tlv(char *data, int pos, short packet_len){ unsigned char tlv_len = data[pos+1]; // Check that the tlv does not exceed the packet length - if(pos + length >= packet_len) + if(pos + tlv_len >= packet_len) return -1; // Returns the type of the tlv or -1 if something went wrong @@ -132,91 +247,172 @@ int update_neighbours(){ }; // We then look at the differents TLVs in the packet. -void work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){ +int work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){ int pos = 0; - unsigned char tlv_len; - tlv tmp_tlv; + unsigned char tlv_len, hash[16], warn[32]; + tlv new_tlv, cur_tlv; + list *tmp_list; + pub_data *pdata; while(pos < packet_len) { switch(validate_tlv(data, pos, packet_len)) { case 0: // We received a padding tlv so it is ignored pos += 1; + break; case 1: // We received a padding tlv so it is ignored tlv_len = data[pos+1]; pos += tlv_len + 2; + break; case 2: // We received a neighbour request so a random neighbor tlv has to be sent - tlv_len = data[pos+1]; - pos += tlv_len + 2; // Send a neighbour tlv neighbour_peer *random = get_random_neighbour(); - build_neighbour(&tmp_tlv, random->ip, random->port); + build_neighbour(&new_tlv, random->ip, random->port); // NOT FINISHED - What packet is it added to? - add_tlv(packet, &tmp_tlv, 3); - break; - case 3: - // We received a neighbour tlv so a tlv network hash is sent to that address - neighbour* cur_tlv = ((neighbour*) data) + pos; - struct in6_addr ip = cur_tlv->ip; - short port = cur_tlv->port; + add_tlv(packet, &new_tlv, 3); + // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; + break; + case 3: + // We received a neighbour tlv so a tlv network hash is sent to that address + cur_tlv.neighbour = (neighbour*) (data + pos); + // Build network hash - unsigned char hash[16]; - hash_network(neighbour_list, hash); - build_network_hash(&tmp_tlv, hash); + build_network_hash(&new_tlv, data_list); // NOT FINISHED - What packet is it added to? - add_tlv(packet, &tmp_tlv, 4); + add_tlv(packet, &new_tlv, 4); + + // The position is updated + tlv_len = data[pos+1]; + pos += tlv_len + 2; + break; case 4: - // We reveived a network hash tlv so - tlv_len = data[pos+1]; - pos += tlv_len +2; + // 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); + hash_network(data_list, hash); + + if(memcmp(hash, cur_tlv.network_hash->network_hash, 16) == 0) { + build_network_state_req(&new_tlv); + // NOT FINISHED - What packet is it added to? + add_tlv(packet, &new_tlv, 5); + } + + // The position is updated + tlv_len = data[pos+1]; + pos += tlv_len + 2; - // NOT FINISHED - Where is network_hash? - build_neighbour(&tmp_tlv, network_hash); - // NOT FINISHED - What packet is it added to? - add_tlv(packet, &tmp_tlv, 4); break; case 5: // We received a network state request tlv so a series of tlv node hash have to be sent for each data known - pos += 2; - // NOT FINISHED - for each known data - list *tmp_list = data_list; - pub_data *tmp_data; + // for each known data build a node hash and add to packet + tmp_list = data_list; while(tmp_list != NULL) { - tmp_data = (pub_data*) tmp_list->data; - build_node_hash(&tmp_tlv, tmp_data->id, tmp_data->seqno); + pdata = (pub_data*) tmp_list->data; + build_node_hash(&new_tlv, pdata->id, pdata->seqno, pdata->data); + // NOT FINISHED - What packet is it added to? + add_tlv(packet, &new_tlv, 4); } + // The position is updated + pos += 2; + break; case 6: - // We received a node hash tlv + // We received a node hash tlv so if there is no entry for node_id in the data list or the hashes differ we send a node state request, if the hashes are identical nothing has to be done + cur_tlv.node_hash = (node_hash*) (data + pos); + pdata = get_data(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) { + // 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; + } + + } + + // 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); + // NOT FINISHED - What packet is it added to? + add_tlv(packet, &new_tlv, 7); + + // The position is updated + tlv_len = data[pos+1]; + pos += tlv_len + 2; + break; case 7: - // We received a node state request tlv + // 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 + cur_tlv.node_state_req = (node_state_req*) (data + pos); + pdata = get_data(cur_tlv.node_state_req->node_id); + + if(pdata != NULL) { + build_node_state(&new_tlv, pdata->id, pdata->seqno, pdata->data, pdata->length); + // NOT FINISHED - What packet is it added to? + add_tlv(packet, &new_tlv, 8); + } + + // The position is updated + tlv_len = data[pos+1]; + pos += tlv_len + 2; + break; case 8: - // We received a node state tlv + // We received a node state tlv so we add it to the data list or update the data stored + cur_tlv.node_state = (node_state*) (data + pos); + + add_data(cur_tlv.node_state->length - 26, cur_tlv.node_state->node_id, cur_tlv.node_state->seqno, cur_tlv.node_state->data); + + // The position is updated + tlv_len = data[pos+1]; + pos += tlv_len + 2; + break; case 9: // We received a warning tlv so it's message is printed + cur_tlv.warning = (warning*) (data + pos); + + // 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); + + // The position is updated + tlv_len = data[pos+1]; + pos += tlv_len + 2; + break; default: - return ; + // A malformed packet was found so we stop looking for more packets and send a warning tlv + strcpy(warn, "Packet is malformed."); + build_warning(&new_tlv, warn, strlen(warn)); + // NOT FINISHED - What packet is it added to? + add_tlv(packet, &new_tlv, 9); + + return -1; } } + + return 0; } // We listen forever for new paquets; diff --git a/src/node.h b/src/node.h index d4ba94b..630a93e 100644 --- a/src/node.h +++ b/src/node.h @@ -90,4 +90,13 @@ int len_list(list *l); neighbour_peer *get_random_neighbour(); +// get data associated with id, if it doesn't exist return NULL +pub_data *get_data(long id); + +// Take data as args and create a pub_data structure in the heap +pub_data *copy_data(unsigned char len, long id, short seqno, char *data); + +// add new data to data list +void add_data(unsigned char len, long id, short seqno, char *data); + #endif diff --git a/src/tlv.c b/src/tlv.c index 2284815..c93436a 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -79,7 +79,7 @@ int build_neighbour(tlv *tlv, struct in6_addr ip, short port) { return 0; } -int build_network_hash(tlv *tlv, char *hash) { +int build_network_hash(tlv *tlv, list *data_list) { network_hash *new = (network_hash*) malloc(sizeof(network_hash)); if(new == NULL) @@ -87,7 +87,7 @@ int build_network_hash(tlv *tlv, char *hash) { new->type = 4; new->length = 16; - memcpy(new->network_hash, hash, 16); + hash_network(data_list, new->network_hash); tlv->network_hash = new; @@ -108,7 +108,7 @@ int build_network_state_req(tlv *tlv) { return 0; } -int build_node_hash(tlv *tlv, long node_id, short seqno, char *hash) { +int build_node_hash(tlv *tlv, long node_id, short seqno, char *data) { node_hash *new = (node_hash*) malloc(sizeof(node_hash)); if(new == NULL) @@ -118,7 +118,9 @@ int build_node_hash(tlv *tlv, long node_id, short seqno, char *hash) { new->length = 26; new->node_id = node_id; new->seqno = seqno; - memcpy(new->node_hash, hash, 16); + + pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; + hash_data(&pdata, new->node_hash); tlv->node_hash = new; @@ -140,9 +142,9 @@ int build_node_state_req(tlv *tlv, long node_id) { return 0; } -int build_node_state(tlv *tlv, long node_id, short seqno, char *node_hash, char *data) { +int build_node_state(tlv *tlv, long node_id, short seqno, char *data, size_t data_len) { node_state *new = (node_state*) malloc(sizeof(node_state)); - int len = strlen(data); + int len = data_len + 26; if(new == NULL) return -1; @@ -157,17 +159,19 @@ int build_node_state(tlv *tlv, long node_id, short seqno, char *node_hash, char new->length = 26 + len; new->node_id = node_id; new->seqno = seqno; - memcpy(new->node_hash, node_hash, 16); memcpy(new->data, data, len); + pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; + hash_data(&pdata, new->node_hash); + tlv->node_state = new; return 0; } -int build_warning(tlv *tlv, char *message) { +int build_warning(tlv *tlv, char *message, size_t message_len) { warning *new = (warning*) malloc(sizeof(warning)); - int len = strlen(message); + int len = message_len; if(new == NULL) return -1; diff --git a/src/tlv.h b/src/tlv.h index c9a3d00..33bbaf5 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -6,6 +6,7 @@ #include #include "parser.h" #include "hash.h" +#include "node.h" #define LEN_NEIGHBOUR_REQ 0 #define LEN_NEIGHBOUR 18 @@ -33,7 +34,7 @@ typedef struct pad1 { typedef struct padn { unsigned char type; unsigned char length; - char mbz[256]; + char *mbz; } padn; // 2 octets @@ -117,11 +118,11 @@ int build_pad1(tlv *tlv); int build_padn(tlv *tlv, size_t len); int build_neighbour_req(tlv *tlv); int build_neighbour(tlv *tlv, struct in6_addr ip, short port); -int build_network_hash(tlv *tlv, char *network_hash); +int build_network_hash(tlv *tlv, list *data_list); int build_network_state_req(tlv *tlv); -int build_node_hash(tlv *tlv, long node_id, short seqno, char *node_hash); +int build_node_hash(tlv *tlv, long node_id, short seqno, char *data); int build_node_state_req(tlv *tlv, long node_id); -int build_node_state(tlv *tlv, long node_id, short seqno, char *node_hash, char *data); -int build_warning(tlv *tlv, char *message); +int build_node_state(tlv *tlv, long node_id, short seqno, char *data, size_t data_len); +int build_warning(tlv *tlv, char *message, size_t message_len); #endif From 53528ff666c9c5b299b92626106c36725f819c3e Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 14 Apr 2020 16:59:49 +0200 Subject: [PATCH 10/12] Removed useless lines, added flags in seperate part --- Makefile | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index b8b2f19..a365c52 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,8 @@ TARGET ?= dazibao SRC_DIRS ?= ./src/* -CC := gcc -Wall -lssl -lcrypto -SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s) +CC := gcc +CFLAGS= -O2 -Wall -lssl -lcrypto +SRCS := $(shell find $(SRC_DIRS) -name *.c -or -name *.s) OBJS := $(addsuffix .o,$(basename $(SRCS))) DEPS := $(OBJS:.o=.d) @@ -9,7 +10,7 @@ INC_DIRS := $(shell find $(SRC_DIRS) -type d) INC_FLAGS := $(addprefix -I,$(INC_DIRS)) $(TARGET): $(OBJS) - $(CC) $(LDFLAGS) $(OBJS) -o $@ $(LOADLIBES) $(LDLIBS) + $(CC) $(CFLAGS) $(OBJS) -o $@ $(LOADLIBES) $(LDLIBS) .PHONY: clean clean: From f294be3ba2e9bbeb6c672d86221a82711e91db25 Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 14 Apr 2020 19:32:15 +0200 Subject: [PATCH 11/12] Fixed sending messages Fixed return types Fixed signatures Fixed *some* warnings --- src/hash.c | 6 ++--- src/hash.h | 12 +++++---- src/node.c | 76 ++++++++++++++++++++++++++++------------------------ src/node.h | 13 ++++----- src/parser.h | 2 +- src/tlv.c | 5 ++++ src/tlv.h | 11 ++++---- 7 files changed, 70 insertions(+), 55 deletions(-) diff --git a/src/hash.c b/src/hash.c index 287e2c4..44e3827 100644 --- a/src/hash.c +++ b/src/hash.c @@ -11,7 +11,7 @@ void hash_data(pub_data *data, unsigned char *buf) { unsigned char hash[SHA256_DIGEST_LENGTH]; SHA256(concat, totlen, hash); - // Put truncated hash into buf + // Put truncated hash into buf hash_trunc(hash, buf); } @@ -33,7 +33,7 @@ void hash_network(list *data_list, unsigned char *buf) { // Hash all of concat to obtain the network hash SHA256(concat, totlen, hash); - // Put truncated hash into buf + // Put truncated hash into buf hash_trunc(hash, buf); // Get rid of concat @@ -57,4 +57,4 @@ void concat_data(pub_data *data, unsigned char *buf) { void concat_hash(unsigned char *hash1, unsigned char *hash2, size_t size) { hash1 = (unsigned char*) realloc(hash1, size + 16); memcpy(hash1+size, hash2, 16); -} \ No newline at end of file +} diff --git a/src/hash.h b/src/hash.h index 358c7b5..4f16809 100644 --- a/src/hash.h +++ b/src/hash.h @@ -2,22 +2,24 @@ #define HASH_H #include -#include "node.h" + #include "tlv.h" +#include "parser.h" +#include "node.h" // Hash a single data -void hash_data(pub_data *data, unsigned char *buf); +void hash_data(struct pub_data *data, unsigned char *buf); // Hash every data contained in data_list then return a network hash -void hash_network(list *data_list, unsigned char *buf); +void hash_network(struct list *data_list, unsigned char *buf); // Truncate 32 octet hash to 16 octets void hash_trunc(unsigned char *hash256bit, unsigned char *buf); // Concat all fields of data and put them in buf -void concat_data(pub_data *data, unsigned char *buf); +void concat_data(struct pub_data *data, unsigned char *buf); // Concat hash2 to hash1 (hash1 is modified) void concat_hash(unsigned char *hash1, unsigned char *hash2, size_t size); -#endif \ No newline at end of file +#endif diff --git a/src/node.c b/src/node.c index fbf462d..4b5d8f3 100644 --- a/src/node.c +++ b/src/node.c @@ -8,8 +8,11 @@ #include #include -#include "tlv.h" #include "node.h" +#include "tlv.h" +#include "hash.h" +#include "parser.h" + /* ---- Fonctions utilitaires ---- */ @@ -61,7 +64,7 @@ pub_data *get_data(long id) { // Take data as args and create a pub_data structure in the heap pub_data *copy_data(unsigned char len, long id, short seqno, char *data) { pub_data *new_data = (pub_data*) malloc(sizeof(pub_data)); - char *_data = (char*) malloc(len); + char *_data = (char*) malloc(len); new_data->length = len; new_data->id = id; new_data->seqno = seqno; @@ -120,7 +123,7 @@ void add_data(unsigned char len, long id, short seqno, char *data) { // Else, we update the last node new_node = (list*) malloc(sizeof(list)); new_node->data = (void*) new_data; - new_node->next = tmp; + new_node->next = tmp; last->next = new_node; return; @@ -154,33 +157,33 @@ void add_data(unsigned char len, long id, short seqno, char *data) { // Update list new_node = (list*) malloc(sizeof(list)); new_node->data = (void*) new_data; - new_node->next = NULL; + new_node->next = NULL; last->next = new_node; } /* ---- Fin fonctions utilitaires ---- */ -int send_tlv(struct tlv tlv_to_send, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int sock_num){ - debug_print("Building packet to send a TLV."); +int send_tlv(union tlv * tlv_to_send, int tlv_size, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num){ + // debug_print("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(sizeof(tlv_to_send) > 1020){ - debug_print("Unable to send TLV, the size is bigger than 1020 bits."); + if (tlv_size > 1020) { + perror(">> Unable to send the tlv, it's size if above 1020 bytes."); return -1; } else { - pack.length = sizeof(tlv_to_send); - strcpy(pack.body, (char) tlv_to_send); + memcpy((void *) pack.body, tlv_to_send, tlv_size); } - // Casting the struct to a buffer. - packet_buff = (char *) pack; + // 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); - debug_print("Packet has been built."); + // debug_print("Packet has been built."); // Vectorized buffer struct iovec vec_buff = { .iov_len = sizeof(packet_buff), .iov_base = packet_buff }; @@ -197,22 +200,22 @@ int send_tlv(struct tlv tlv_to_send, int tlv_type, struct sockaddr_in6 * dest_li .msg_iovlen = 1 // We have only one iovec buffer. But if we had 2, we would write 2. }; - response_code = sendmsg((int) sock_num, &packet_tlv_send_out, 0); + int response_code = sendmsg((int) socket_num, &packet_tlv_send_out, 0); if (response_code < 0) { - debug_print("Unable to send out the packet to peer %i", i); + // debug_print("Unable to send out the packet to peer %i", i); error_while_sending = 1; continue; } else if (response_code < sizeof(packet_tlv_send_out)) { - debug_print("Sent out only part of the packet."); + // debug_print("Sent out only part of the packet."); error_while_sending = 1; continue; } else { - debug_print("Send out packet to peer %i", i); + // debug_print("Send out packet to peer %i", i); } } if (error_while_sending == 1) { - debug_print("Error occured while sending out a packet."); + // debug_print("Error occured while sending out a packet."); return -1; } else { return 0; @@ -273,9 +276,9 @@ int validate_tlv(char *data, int pos, short packet_len){ // For every packet recivied, // then we make sure it's conform // We then extract the data from it to make it easy to work with -int check_header(char * req[], int buffer_size, struct packet * packet_to_return){ +int check_header(char * received_datagram[], int buffer_len, struct packet * packet_to_return){ - packet * packet_to_return = (packet*) req; + packet_to_return = (packet*) received_datagram; // We need to check a few things ; // The first byte must be worth 95, @@ -290,7 +293,7 @@ int check_header(char * req[], int buffer_size, struct packet * packet_to_return return -1; } - if (packet_to_return.length + 4 > buffer_size ) { + if (packet_to_return->length + 4 > buffer_len ) { perror(">> The packet length is bigger than the UDP datagram, which is not possible with the current laws of physics."); return -1; } @@ -306,13 +309,16 @@ int update_neighbours(){ }; // We then look at the differents TLVs in the packet. -int work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){ +int work_with_tlvs(char * data[], short packet_len, struct sockaddr_in6 sender){ int pos = 0; unsigned char tlv_len, hash[16], warn[32]; tlv new_tlv, cur_tlv; list *tmp_list; pub_data *pdata; + struct neighbour_peer * random_neighbour; + + while(pos < packet_len) { switch(validate_tlv(data, pos, packet_len)) { case 0: @@ -330,11 +336,11 @@ int work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){ // We received a neighbour request so a random neighbor tlv has to be sent // Send a neighbour tlv - neighbour_peer *random = get_random_neighbour(); - build_neighbour(&new_tlv, random->ip, random->port); + random_neighbour = get_random_neighbour(); + build_neighbour(&new_tlv, random_neighbour->ip, random_neighbour->port); // NOT FINISHED - What packet is it added to? - add_tlv(packet, &new_tlv, 3); + // add_tlv(packet, &new_tlv, 3); // The position is updated tlv_len = data[pos+1]; @@ -349,7 +355,7 @@ int work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){ build_network_hash(&new_tlv, data_list); // NOT FINISHED - What packet is it added to? - add_tlv(packet, &new_tlv, 4); + // add_tlv(packet, &new_tlv, 4); // The position is updated tlv_len = data[pos+1]; @@ -364,7 +370,7 @@ int work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){ if(memcmp(hash, cur_tlv.network_hash->network_hash, 16) == 0) { build_network_state_req(&new_tlv); // NOT FINISHED - What packet is it added to? - add_tlv(packet, &new_tlv, 5); + // add_tlv(packet, &new_tlv, 5); } // The position is updated @@ -382,7 +388,7 @@ int work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){ pdata = (pub_data*) tmp_list->data; build_node_hash(&new_tlv, pdata->id, pdata->seqno, pdata->data); // NOT FINISHED - What packet is it added to? - add_tlv(packet, &new_tlv, 4); + // add_tlv(packet, &new_tlv, 4); } // The position is updated @@ -413,7 +419,7 @@ int work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){ // 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); // NOT FINISHED - What packet is it added to? - add_tlv(packet, &new_tlv, 7); + // add_tlv(packet, &new_tlv, 7); // The position is updated tlv_len = data[pos+1]; @@ -428,7 +434,7 @@ int work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){ if(pdata != NULL) { build_node_state(&new_tlv, pdata->id, pdata->seqno, pdata->data, pdata->length); // NOT FINISHED - What packet is it added to? - add_tlv(packet, &new_tlv, 8); + // add_tlv(packet, &new_tlv, 8); } // The position is updated @@ -450,7 +456,7 @@ int work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){ case 9: // We received a warning tlv so it's message is printed cur_tlv.warning = (warning*) (data + pos); - + // 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); @@ -465,7 +471,7 @@ int work_with_tlvs(char *data, short packet_len, struct sockaddr_in6 sender){ strcpy(warn, "Packet is malformed."); build_warning(&new_tlv, warn, strlen(warn)); // NOT FINISHED - What packet is it added to? - add_tlv(packet, &new_tlv, 9); + // add_tlv(packet, &new_tlv, 9); return -1; } @@ -535,7 +541,7 @@ void listen_for_packets(){ // struct tlv_list received_tlvs; // if (validate_tlvs(formated_rec_datagram) < 0) - int nbr_success_tlv = work_with_tlvs(formated_rec_datagram, &req, sender); + int nbr_success_tlv = work_with_tlvs(&req, 1024, sender); if (nbr_success_tlv < 0){ perror(">> Error while treating the TLVs of the packet."); printf(">> Managed to deal with %i TLVs\n", -nbr_success_tlv ); @@ -551,7 +557,7 @@ int main(int argc, const char *argv[]) { while(cont){ // We create the neighbourhood table - neighbour_peer neighbour_list[NEIGHBOUR_MAX]; + // neighbour_peer neighbour_list[NEIGHBOUR_MAX]; // We create the message table // We create our own message. diff --git a/src/node.h b/src/node.h index 54141df..7daef54 100644 --- a/src/node.h +++ b/src/node.h @@ -13,6 +13,7 @@ #include "tlv.h" #include "hash.h" +#include "parser.h" // On which port do we listen to #define LISTEN_PORT 1212 @@ -65,27 +66,27 @@ static list *neighbour_list; // fonctions signatures void listen_for_packets(); -int check_header(char * received_datagram[], int len, struct packet pack); +int check_header(char * received_datagram[], int buffer_len, struct packet * packet_to_return); int validate_tlvs(struct packet * pack, struct tlv_list * tlv_l); int update_neighbours(); -int work_with_tlvs(struct packet received_packet, char * data_from_packet[], struct sockaddr_in6 sender); +int work_with_tlvs(char * data[], short packet_len, struct sockaddr_in6 sender); -void add_tlv(packet *packet, tlv *tlv, char type); +void add_tlv(struct packet *packet, union tlv *tlv, char type); -int send_packet(); +// int send_packet(); /* 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(struct tlv tlv_to_send, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num); +int send_tlv(union tlv * tlv_to_send, int tlv_size, 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, int tlv_type, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num); +int send_tlvs(struct list * tlv_list, int tlv_size, struct sockaddr_in6 * dest_list[], int dest_list_size, int socket_num); // threaded functions void t_ask_for_more_peers(); diff --git a/src/parser.h b/src/parser.h index b373a0d..15b5fcb 100644 --- a/src/parser.h +++ b/src/parser.h @@ -16,4 +16,4 @@ typedef struct cmd_token { // retourne le type de commande à exécuter cmd_token parse_cmd(); -#endif \ No newline at end of file +#endif diff --git a/src/tlv.c b/src/tlv.c index 390352a..5fe7dbf 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -18,7 +18,12 @@ int build_tlv(tlv *tlv, cmd_token token) { 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; } int build_pad1(tlv *tlv) { diff --git a/src/tlv.h b/src/tlv.h index 33bbaf5..b548ca0 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -4,9 +4,10 @@ #include #include #include -#include "parser.h" -#include "hash.h" + #include "node.h" +#include "hash.h" +#include "parser.h" #define LEN_NEIGHBOUR_REQ 0 #define LEN_NEIGHBOUR 18 @@ -111,14 +112,14 @@ typedef union tlv { } tlv; // build tlv from token -int build_tlv(tlv *tlv, cmd_token token); +int build_tlv(tlv *tlv, struct cmd_token token); // build specific tlv int build_pad1(tlv *tlv); int build_padn(tlv *tlv, size_t len); -int build_neighbour_req(tlv *tlv); +int build_neighbour_req(union tlv *tlv); int build_neighbour(tlv *tlv, struct in6_addr ip, short port); -int build_network_hash(tlv *tlv, list *data_list); +int build_network_hash(tlv *tlv, struct list *data_list); int build_network_state_req(tlv *tlv); int build_node_hash(tlv *tlv, long node_id, short seqno, char *data); int build_node_state_req(tlv *tlv, long node_id); From 45cdc1409c23ff62afa26d79b858d5a1c976415a Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 15 Apr 2020 12:46:34 +0200 Subject: [PATCH 12/12] Ajout d'un log --- src/node.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/node.c b/src/node.c index 4b5d8f3..0c33abb 100644 --- a/src/node.c +++ b/src/node.c @@ -552,6 +552,7 @@ void listen_for_packets(){ } int main(int argc, const char *argv[]) { + printf(">> Starting node\n"); int cont = 1; while(cont){