From 1295a91f42dfd238a6cd2d61ac4ad17f7655def7 Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 31 Mar 2020 22:35:56 +0200 Subject: [PATCH 01/18] =?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/18] 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/18] 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/18] 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/18] 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/18] 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/18] 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/18] 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/18] 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/18] 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 9f6e8875fda676b76e0f644375f31c54c5aae992 Mon Sep 17 00:00:00 2001 From: PERDRIAU nelson Date: Tue, 14 Apr 2020 17:55:55 +0200 Subject: [PATCH 11/18] =?UTF-8?q?:=20Ajout=20d'une=20premi=C3=A8re=20makef?= =?UTF-8?q?ile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Makefile b/Makefile index e69de29..a365c52 100644 --- a/Makefile +++ b/Makefile @@ -0,0 +1,19 @@ +TARGET ?= dazibao +SRC_DIRS ?= ./src/* +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) + +INC_DIRS := $(shell find $(SRC_DIRS) -type d) +INC_FLAGS := $(addprefix -I,$(INC_DIRS)) + +$(TARGET): $(OBJS) + $(CC) $(CFLAGS) $(OBJS) -o $@ $(LOADLIBES) $(LDLIBS) + +.PHONY: clean +clean: + $(RM) $(TARGET) $(OBJS) $(DEPS) + +-include $(DEPS) From f294be3ba2e9bbeb6c672d86221a82711e91db25 Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 14 Apr 2020 19:32:15 +0200 Subject: [PATCH 12/18] 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 13/18] 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){ From db3875205e313fbee4b9e698ffde260d81ddfa51 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 15 Apr 2020 19:28:28 +0200 Subject: [PATCH 14/18] =?UTF-8?q?Impl=C3=A9mentation=20de=20poll()=20termi?= =?UTF-8?q?n=C3=A9,=20ajouter=20les=20nouvelles=20fonctions=20et=20leurs?= =?UTF-8?q?=20signtaure=20=C3=A0=20faire?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/node.c | 203 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 182 insertions(+), 21 deletions(-) diff --git a/src/node.c b/src/node.c index 0c33abb..6cfb721 100644 --- a/src/node.c +++ b/src/node.c @@ -7,6 +7,11 @@ #include #include #include +#include +#include +#include +#include +#include #include "node.h" #include "tlv.h" @@ -551,35 +556,191 @@ void listen_for_packets(){ } } +int run_node(int sock_fd, struct sockaddr_in6 * peer){ + printf(">> Boostrapping node...\n"); + + int ret; + ssize_t bytes; + char input_buffer[1024]; + char output_buffer[1024]; + struct pollfd fds[2]; + + /* Descriptor zero is stdin */ + fds[0].fd = 0; + fds[1].fd = sock_fd; + fds[0].events = POLLIN | POLLPRI; + fds[1].events = POLLIN | POLLPRI; + + /* Normally we'd check an exit condition, but for this example + * we loop endlessly. + */ + while (1) { + /* Call poll() */ + ret = poll(fds, 2, -1); + + if (ret < 0) { + printf("Error - poll returned error: %s\n", strerror(errno)); + break; + } + if (ret > 0) { + + /* Regardless of requested events, poll() can always return these */ + if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { + printf("Error - poll indicated stdin error\n"); + break; + } + if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) { + printf("Error - poll indicated socket error\n"); + break; + } + + /* Check if data to read from stdin */ + printf(">> "); + if (fds[0].revents & (POLLIN | POLLPRI)) { + bytes = read(0, output_buffer, sizeof(output_buffer)); + if (bytes < 0) { + printf("Error - stdin error: %s\n", strerror(errno)); + break; + } + + // // Reset buffer + // // memset(&output_buffer, '\0', 1024); + // + // // Build packet + // struct packet message; + // message.magic = 95; + // message.version = 1; + // message.body = malloc(20); + // + // // Build TLV + // struct neighbour_req *neigh_req = (neighbour_req*) malloc(sizeof(neighbour_req)); + // + // neigh_req->type = 2; + // neigh_req->length = 0; + // + // message.length = 2 * sizeof(unsigned char); + // printf("(%i)\n", message.length ); + // // message.body = (char *) neigh_req; + // + // memcpy(message.body, &neigh_req, sizeof(struct neighbour_req)); + // memcpy(&output_buffer, &message, sizeof(struct packet)); + + struct iovec vec_buff_reply = {.iov_len = sizeof(output_buffer), .iov_base = output_buffer}; + + struct sockaddr_in6 ntp_server_adress; + memset(&ntp_server_adress, 0, sizeof(ntp_server_adress)); + + int inet_p = inet_pton(AF_INET6, "2a00:1080:800::6:1", &ntp_server_adress.sin6_addr); + if(inet_p < 1){ + perror(">> Failed inet_pton."); + exit(1); + } + ntp_server_adress.sin6_family = AF_INET6; + ntp_server_adress.sin6_port = htons(1212); // NTP port number. + + const struct msghdr reply = { + .msg_name = &ntp_server_adress, + .msg_namelen = sizeof(ntp_server_adress), + .msg_iov = &vec_buff_reply, + .msg_iovlen = 1 + }; + + printf("Sending: %li bytes as : %s\n", sizeof(output_buffer), output_buffer); + // bytes = sendto(sock_fd, output_buffer, sizeof(output_buffer), 0, (struct sockaddr *)peer, sizeof(struct sockaddr_in)); + bytes = sendmsg((int) sock_fd, &reply, 0); + if (bytes < 0) { + perror("deded"); + printf("Error - sendto error: %s\n", strerror(errno)); + break; + } + } + + /* Check if data to read from socket */ + if (fds[1].revents & (POLLIN | POLLPRI)) { + bytes = recvfrom(sock_fd, input_buffer, sizeof(input_buffer), + 0, NULL, NULL); + if (bytes < 0) { + printf("Error - recvfrom error: %s\n", strerror(errno)); + break; + } + if (bytes > 0) { + printf("Received: %.*s\r", (int)bytes, input_buffer); + } + } + } + } + return 0; +} + int main(int argc, const char *argv[]) { printf(">> Starting node\n"); int cont = 1; - while(cont){ + unsigned long local_port = 1212; + unsigned long remote_port = 1212; - // We create the neighbourhood table - // neighbour_peer neighbour_list[NEIGHBOUR_MAX]; - // We create the message table + int sock_fd; - // We create our own message. + struct sockaddr_in6 server_addr; + struct sockaddr_in6 peer_addr; - // Listen for incoming packets - listen_for_packets(); + memset(&peer_addr, 0, sizeof(peer_addr)); - // This is in it's own fork. - time_t delay = time(NULL) + 20; - while(! (delay < time(NULL))) { - // Theses functions are there for general book-keeping,and run in there own - // thread, being run every 20 seconds. - // Every 20 sec, if we have less than 5 neighbours, we ask for more peers - // by sending out a TLV neighbour Request at a random peer. - t_ask_for_more_peers(); - // Every 20 sec, we also check for a peer that didn't emit a new message for - // the past 70 sec, if he's temporary, we delete him from the neighbourhood. - t_update_neighbours(); - // We send out a TLV Network hash to get an ideal of the network state. - t_get_network_state(); - } + peer_addr.sin6_family = AF_INET6; + peer_addr.sin6_port = htons(remote_port); + if (inet_pton(AF_INET6, "2001:660:3301:9200::51c2:1b9b", &peer_addr.sin6_addr) < 0) { + printf("Error - invalid remote address '%s'\n", "2001:660:3301:9200::51c2:1b9b"); + return 1; } + + /* Create UDP socket */ + sock_fd = socket(AF_INET6, SOCK_DGRAM, 0); + if (sock_fd < 0) { + printf("Error - failed to open socket: %s\n", strerror(errno)); + return 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 = htons(local_port); + if (bind(sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0) { + printf("Error - failed to bind socket: %s\n", strerror(errno)); + return 1; + } + + run_node(sock_fd, &peer_addr); + + + close(sock_fd); + // + // while(cont){ + // + // // We create the neighbourhood table + // // neighbour_peer neighbour_list[NEIGHBOUR_MAX]; + // // We create the message table + // + // // We create our own message. + // + // // Listen for incoming packets + // // listen_for_packets(); + // // + // // // This is in it's own fork. + // // time_t delay = time(NULL) + 20; + // // while(! (delay < time(NULL))) { + // // // Theses functions are there for general book-keeping,and run in there own + // // // thread, being run every 20 seconds. + // // // Every 20 sec, if we have less than 5 neighbours, we ask for more peers + // // // by sending out a TLV neighbour Request at a random peer. + // // t_ask_for_more_peers(); + // // // Every 20 sec, we also check for a peer that didn't emit a new message for + // // // the past 70 sec, if he's temporary, we delete him from the neighbourhood. + // // t_update_neighbours(); + // // // We send out a TLV Network hash to get an ideal of the network state. + // // t_get_network_state(); + // } + // } + return 0; } From cdef7ee0ac0488798adaa0635f4bed2333a4f12d Mon Sep 17 00:00:00 2001 From: gonzalef Date: Thu, 16 Apr 2020 15:08:22 +0200 Subject: [PATCH 15/18] solved compilation errors | changed header includes, makefile flags, pointers to arrays --- Makefile | 5 +++-- src/hash.h | 6 +++--- src/hash.o | Bin 0 -> 2648 bytes src/node.c | 15 ++++++++------- src/node.h | 44 ++++++++++++++++++++++---------------------- src/node.o | Bin 0 -> 10576 bytes src/parser.o | Bin 0 -> 2752 bytes src/tlv.h | 10 +++++----- src/tlv.o | Bin 0 -> 4744 bytes 9 files changed, 41 insertions(+), 39 deletions(-) create mode 100644 src/hash.o create mode 100644 src/node.o create mode 100644 src/parser.o create mode 100644 src/tlv.o diff --git a/Makefile b/Makefile index a365c52..3d676e4 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,17 @@ TARGET ?= dazibao SRC_DIRS ?= ./src/* CC := gcc -CFLAGS= -O2 -Wall -lssl -lcrypto +CFLAGS= -O2 -Wall SRCS := $(shell find $(SRC_DIRS) -name *.c -or -name *.s) OBJS := $(addsuffix .o,$(basename $(SRCS))) DEPS := $(OBJS:.o=.d) +SSLFLAGS = -lssl -lcrypto INC_DIRS := $(shell find $(SRC_DIRS) -type d) INC_FLAGS := $(addprefix -I,$(INC_DIRS)) $(TARGET): $(OBJS) - $(CC) $(CFLAGS) $(OBJS) -o $@ $(LOADLIBES) $(LDLIBS) + $(CC) $(CFLAGS) $(OBJS) -o $@ $(LOADLIBES) $(LDLIBS) $(SSLFLAGS) .PHONY: clean clean: diff --git a/src/hash.h b/src/hash.h index 4f16809..748b6b1 100644 --- a/src/hash.h +++ b/src/hash.h @@ -8,16 +8,16 @@ #include "node.h" // Hash a single data -void hash_data(struct pub_data *data, unsigned char *buf); +void hash_data(pub_data *data, unsigned char *buf); // Hash every data contained in data_list then return a network hash -void hash_network(struct list *data_list, unsigned char *buf); +void hash_network(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(struct pub_data *data, unsigned char *buf); +void concat_data(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); diff --git a/src/hash.o b/src/hash.o new file mode 100644 index 0000000000000000000000000000000000000000..920c6ac2f74eb242bea0fe66f497943b0cf91af1 GIT binary patch literal 2648 zcmbtU&2Jk;6d$i$XK~V9C*|O(Nbw;-4x%fYwonxbyh-eg(W*g>O+jf`9LL5eiH+=C zHK3}E4}$X{GdnkdWZeTLm=bL!^p`jEEZEjAxS3){ziAY4*MO z&F_8en~!TLBQ5!Sh>H)sf(~XJMd;1013O90B`K^4hnx-UUXfZE z=L%}!=G8vjc+koWwTz*!M}2G9`F`!8e;eOiFKIlVD^V{uEA8q|7VSc;MN){fq29_$ zk4pr6Ebw#K`Dp?>+qm)D*%|Bx24g_RPHHo;s;+byO&r3F)#HO(en5wAW9R2h5)C(V ztA_Ipb{^1aNv)<D>bWmlTv`v};=A^dBH;HaFhm$d>_rd6{m1yrb33VF+%$y@n0Of7TF z7$4RRb3C1%NM+5eK5V2+G=cRO&b)|Bvu@=Jvu2?@YnJlw&G8r{II0!%b8}UeqEfUj zS8KCqzKxY?#UfOO(0%$vNCN#7jNQGqJYqOG2|WUSjQuykj|zA~z*Paa1-w_lKW8|} z8xruF0_ETqvb*QV=rq!p_3^Ti5am8waU^Zw99_?#dd7)cnSEVCzIi-F{fkv#m~M;QJr z!@V!ks3$L@(-_<)`gs^6=7051fG?>l4nq{4|2Yu^n;oxxeNMncYUN+nKhFHU^!FL? ztr!$+-aih%21cj;FR~3K?-LQyfAU&$f8KxcKi=E`p(JbQ2K!-hSBbCJpTza{9v~#M a{l88og7E%vh`bG*{9CO4w>Xh$um3+O42qfn literal 0 HcmV?d00001 diff --git a/src/node.c b/src/node.c index 0c33abb..b4dd698 100644 --- a/src/node.c +++ b/src/node.c @@ -163,7 +163,7 @@ void add_data(unsigned char len, long id, short seqno, char *data) { /* ---- Fin fonctions utilitaires ---- */ -int send_tlv(union tlv * tlv_to_send, int tlv_size, 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){ // debug_print("Building packet to send a TLV."); // We first need to build the packet, @@ -276,7 +276,7 @@ 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 * received_datagram[], int buffer_len, struct packet * packet_to_return){ +int check_header(char * received_datagram, int buffer_len, struct packet * packet_to_return){ packet_to_return = (packet*) received_datagram; @@ -309,9 +309,10 @@ 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]; + unsigned char tlv_len, hash[16]; + char warn[32]; tlv new_tlv, cur_tlv; list *tmp_list; pub_data *pdata; @@ -531,8 +532,8 @@ void listen_for_packets(){ // We verify the received packet is well formated, // and we return it in the struct designed to work with it. - struct packet * formated_rec_datagram; - if(check_header(&req, 1024, formated_rec_datagram) < 0){ + struct packet formated_rec_datagram; + if(check_header(req, 1024, &formated_rec_datagram) < 0){ perror(">> Error while checking the header, aborting this packet, by choice, and conviction."); continue; } @@ -541,7 +542,7 @@ void listen_for_packets(){ // struct tlv_list received_tlvs; // if (validate_tlvs(formated_rec_datagram) < 0) - int nbr_success_tlv = work_with_tlvs(&req, 1024, 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 ); diff --git a/src/node.h b/src/node.h index 7daef54..d84971b 100644 --- a/src/node.h +++ b/src/node.h @@ -11,20 +11,6 @@ #include #include -#include "tlv.h" -#include "hash.h" -#include "parser.h" - -// On which port do we listen to -#define LISTEN_PORT 1212 - -// The node ID -#define NODE_ID 42675882021843277 - -// The number of neighbours -// The neighbour table has 15 entries -#define NEIGHBOUR_MAX 15 - /* 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 * (configuré au lancement) ou transitoire, et la date de dernière réception d’un @@ -45,7 +31,7 @@ typedef struct neighbour_peer { */ typedef struct pub_data { - unsigned char length; + unsigned char length; long id; short seqno; char *data; @@ -53,10 +39,24 @@ typedef struct pub_data { // General list typedef struct list { - void *data; - void *next; + void *data; + void *next; } list; +#include "tlv.h" +#include "hash.h" +#include "parser.h" + +// On which port do we listen to +#define LISTEN_PORT 1212 + +// The node ID +#define NODE_ID 42675882021843277 + +// The number of neighbours +// The neighbour table has 15 entries +#define NEIGHBOUR_MAX 15 + // Static variables static list *data_list; static list *neighbour_list; @@ -66,13 +66,13 @@ static list *neighbour_list; // fonctions signatures void listen_for_packets(); -int check_header(char * received_datagram[], int buffer_len, struct packet * packet_to_return); +int check_header(char * received_datagram, int buffer_len, packet * packet_to_return); -int validate_tlvs(struct packet * pack, struct tlv_list * tlv_l); +int validate_tlv(char *data, int pos, short packet_len); int update_neighbours(); -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); void add_tlv(struct packet *packet, union tlv *tlv, char type); @@ -81,12 +81,12 @@ void add_tlv(struct packet *packet, union tlv *tlv, char type); /* 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(union tlv * tlv_to_send, int tlv_size, 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_size, 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/node.o b/src/node.o new file mode 100644 index 0000000000000000000000000000000000000000..96e7e6c9a2de0ea806aa8ebc7842069b36c667f3 GIT binary patch literal 10576 zcmb_he{fUBonKith#%2YsC#E9Jzj}OOpYiPCILgBl9BN$d4LD(gz3>JvixLeuq7is z@uOpksqK_^il{s7Tr+cYhGs5znd{t4X4)ZyX=4#L{J35_X|BD=Kquvc7_w7o{vo-g}-7FA8HG_I@|VJOWXbqyCC0OmfqJrN(fJ)x`!khrhhKYYD`njiPjeC?`6kf3JB z>+%<`p|E*@z1Txp1S&(&pRdC%I$AX9X`awchg`-3lhb8;{vukb=np;@d^T*}33dfL zgXU*ly93JU-;JxFA!zV2;~wL2^GZ;8qjULm-0WY|;#Jo(+3!3>+IIq>;rKaY& zTX<&XT*+x(x=mlD!#{IT^V;8hHMlqU)!_5N$i!GMU+*=qn4Vz1bNM~ca&Pi7XsP&5 z&urfxt3UGn?~&lM!7kczKG@l{o94fWLCCdIaOeS=chZ&tmr)O47*pP;UFCs35wCbl zhv(@0;aiTl+2Ij}Co1}b=5HV*Gs^gT)cjMM|3fPnqgTS_`JB6S0yOcsn-es@iEc`} z2G<#1q4}+@u=!hut59ol|F6lEf zGh#i&Lz~i`uLm`=S%{ZVUwM8j)~Lx=f@HL`oGBVY0nNNK`BR9bHJ%C;jF$d1U9o4a zk56%ZKwOVqPt+0ui}5MG7=Pm31Au?#*z`Ro1oc}l!d@^h(%3IU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Q9{lkk6)^vsv=pYwc;`tcMb^uNn-yZ+xx zd`044tHPg>_?U-;{y$gY`}p?&{Xsvy@WYamaJQXV^i|;B=Qv_(CH#uSe^A0_ zB!07mFGVMypdLJ%3;i7&M?V`Se4m8N{(PqjK2Zgqb|3Zu>q z{}?IwdpM5%F=;yBKWtiN93e?j6et-@C&KHkNI|EsF-AD8f;#NUk$LP394!%yfz z3=4MS?`0`}$@qsT2!${IGJlNo7hBzB9R6>F6>&xr`V$<-HHk&wdpVACghk+#mi+I`ne1Qw>1SqqES`=Z1^*6ZlKJFWeq>vM`G#ze)z*b@xfpAbzl3AuE&5F+s7sK zL?jOXeV|*8{@OAC)S+6@clX_TPQcVVw6KWeESfQ z7ar!gSl7^GC2O-Qa6XNq$#OC3w96mj^1_}Fu($DBhdS}>B^M$8O};)vpO6=Rv_S{f zZ8YnU3V4SQ)CWCks zeu(jDLWq?jmoML|Ic}eS-2Zd=KeW;kppqQ!FT4L~z|cOP)#P%O+qc^jZSg(=LMY!P z3q$=Tmw(iTBNvN!zbMsN2Y)QXOT!#R$ce7MhYDi$`B%C823v<1zmWessLU0=i#&cR zH;CiKAqjag|8v`D+iM7D8UN KUgf7<{(k^FM#4J) literal 0 HcmV?d00001 diff --git a/src/parser.o b/src/parser.o new file mode 100644 index 0000000000000000000000000000000000000000..0cdd296b3e6d45a1381d4f89dc493d8414779b6b GIT binary patch literal 2752 zcmbtV&2Jl35FclqHVvuQDMbouA(n7Zis;I28fZBnZ$1~aYAYHem6B{6dtHarAN8)G z1qmABfL6h%NFc;5NQes@dT0;jfE*PO2@dqge^88&qNq}71tQG6eXm~DwuE3JzxmB? zKHl4TA9-g|pXv$(Ku7?dho&W1fXlm^IKf~7_Cg=1FRRIKzehryNuJi6KeXlFN?ib& zb9*xw4I-~?PB0Bxtr*l+Hhc8i7twW`BBQMZhlVM_sYY{{6R&GdL)(1+IxCO-q5I1l z8)$4aq&wPIx^r7wdxU(y(4ei>?U-J>7QKarUi&)wBayXH^2O^ItUKGf^DB#8xz{CE ze?Ui8E(W98S~XfnL++QfwU61<8Es@gTMGtEM8lf1LIIldv)n%(W?TeOgrOLs7()p} z35HG~I>pdDqIrf&h)Rg6(aTs%?q4Cr>?^KKirMR~O^Vr9U7M-ZEi|;{@2II~XVq8L zS#|EFJSe{O2DMmsw(fi>e_B5X)p|wYJ-E@!Jij+RGq@wjLF4XUJ3I1VmGFZ)-H5$) zzIw@@Qa}F8e2;O z(57S8cm-`X$B-2nIy<+gcNWU#1t^${=W+|hN*R&;MzOpUvFwy>LZO&3IV=?|8(?~T z{JF?*a-mYNE0JfFQ6+YGq{6KD+wo&c?C2qv#krX5=Q{A_OAxpi4jky)-E#$3f}aKl zx$g|EbSxD9uxl(dP?M%Zkq?4uX!yNuH56Of^L$5h=oIW={9a>kfZ z$Mi`9vgb_O0>fCeis|FW4a-WW3R$!*yPPgvGSayvW~91SbN!Do zg1OWp|BXl>M-BIbX_OClEY3!UIx%oq0)U;k4+UgQb=0?+q_!To>v{c}9O zC7d^8-d9-kU(ja|wA(-N#EX5R{i9rwFL?TnmbsCe=ktfTlkOzF`Tl&m+y1>n3QGL^ ayi0~hMBfC}kDy)t0RI=26M #include -#include "node.h" -#include "hash.h" -#include "parser.h" - #define LEN_NEIGHBOUR_REQ 0 #define LEN_NEIGHBOUR 18 #define LEN_NETWORK_HASH 16 @@ -111,6 +107,10 @@ typedef union tlv { warning *warning; } tlv; +#include "node.h" +#include "hash.h" +#include "parser.h" + // build tlv from token int build_tlv(tlv *tlv, struct cmd_token token); @@ -119,7 +119,7 @@ int build_pad1(tlv *tlv); int build_padn(tlv *tlv, size_t len); int build_neighbour_req(union tlv *tlv); int build_neighbour(tlv *tlv, struct in6_addr ip, short port); -int build_network_hash(tlv *tlv, struct list *data_list); +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 *data); int build_node_state_req(tlv *tlv, long node_id); diff --git a/src/tlv.o b/src/tlv.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c5zwKcXpvjiZywlJ^(A%Am&Q9dgV&+Q(?E?hCF}!F4Ey6H5HD zD~IFA;pYia`q@7}>!jo32jcLEkNFORKzv~8BtAwr`C;*~BZomCJ{IVLKXC{@%8|qT z{T%PcZ(BLZzJSg#N4)&Z06U3~zXwibv=d$M7rWr2z=?knH3>g+sHxwH|2%NgUqem8 z_de=be4X^?fs_7i)FgcWqrQrd$0ha-KYzfQ0-qGPJ+XCn3 z3fNv3p1*d%KN37^^dkns&lc()@RI*6f%9_&{3+m_{QR2K7h9B{6(Yp-8q2d-dN@u+ zg3T_^FBY;uS#~@N|v*Iu+knL%t$#I`N7F zKPPa~?{(nx29S_G{X~~MKPC`DIGsU>ZxRS`<8pry{zZIJ|9w)1xb;tBKXk%py5LtF z_?KZQ`G4cUoqq0j!8Ng8$)6Jro)rg9cZ=lzxdV6df6@hiUhHq;KkeWtIB@5@zjxry zdH>?To%8k(F~rTUa+LA?AY9`}(uGfo{)`J>6a5Vrep~eA`Vu$i7x}&G(wFzvaWO9S z<^A-m3zzrPiVI&8LHyE%w*>x?8yERvvHEJ+%*~=UYj$0dt(viziA(f<55cP`qoe>gwqb|)OUvm1(N%9SRgv&G%MzZRf*G6q!*{1qw ze8l;`+9zO3^M4DUQ`W@%87GLg^q*`j9)62&>&Zn5)k$EC+$W+Oi1fb?}fCN z>rcb3DY#)${v;C%+YgjJw>@#X*PaNH^w>ZWu7F)#{>F5&-=s$ja>=gRrQ80$f0=cc literal 0 HcmV?d00001 From 50bb64059bc7002de83430400bc0d61399ccc487 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Thu, 16 Apr 2020 15:27:32 +0200 Subject: [PATCH 16/18] change data types --- src/hash.h | 1 + src/node.c | 25 +++++++++---------------- src/node.h | 23 ++++++++++------------- src/tlv.c | 14 +++++++------- src/tlv.h | 13 +++++++------ 5 files changed, 34 insertions(+), 42 deletions(-) diff --git a/src/hash.h b/src/hash.h index 748b6b1..5258a7e 100644 --- a/src/hash.h +++ b/src/hash.h @@ -2,6 +2,7 @@ #define HASH_H #include +#include #include "tlv.h" #include "parser.h" diff --git a/src/node.c b/src/node.c index b4dd698..c32b546 100644 --- a/src/node.c +++ b/src/node.c @@ -1,18 +1,11 @@ // 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 "node.h" -#include "tlv.h" -#include "hash.h" -#include "parser.h" +// Static variables +static list *data_list; +static list *neighbour_list; /* ---- Fonctions utilitaires ---- */ @@ -47,7 +40,7 @@ neighbour_peer *get_random_neighbour() { } // get data associated with id, if it doesn't exist return NULL -pub_data *get_data(long id) { +pub_data *get_data(int64_t id) { list *tmp = data_list; pub_data *data; @@ -62,7 +55,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 *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); new_data->length = len; @@ -75,7 +68,7 @@ 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) { +void 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); @@ -103,7 +96,7 @@ void add_data(unsigned char len, long id, short seqno, char *data) { list *tmp = data_list; list *last = NULL; list *new_node; - long cur_id; + int64_t cur_id; while(tmp != NULL) { cur_id = ((pub_data*) tmp->data)->id; @@ -224,7 +217,7 @@ int send_tlv(union tlv * tlv_to_send, int tlv_size, struct sockaddr_in6 * dest_l // 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){ +int validate_tlv(char *data, int pos, int16_t packet_len){ char type = data[pos]; // Nothing to do in this case @@ -309,7 +302,7 @@ 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, int16_t packet_len, struct sockaddr_in6 sender){ int pos = 0; unsigned char tlv_len, hash[16]; char warn[32]; diff --git a/src/node.h b/src/node.h index d84971b..709331c 100644 --- a/src/node.h +++ b/src/node.h @@ -4,12 +4,13 @@ #define NODE_H #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 @@ -18,7 +19,7 @@ */ typedef struct neighbour_peer { struct in6_addr ip; - short port; + int16_t port; char is_temporary; struct timeval last_seen; } neighbour_peer; @@ -32,8 +33,8 @@ typedef struct neighbour_peer { typedef struct pub_data { unsigned char length; - long id; - short seqno; + int64_t id; + int16_t seqno; char *data; } pub_data; @@ -57,10 +58,6 @@ typedef struct list { // The neighbour table has 15 entries #define NEIGHBOUR_MAX 15 -// Static variables -static list *data_list; -static list *neighbour_list; - // TODO // fonctions signatures @@ -68,11 +65,11 @@ void listen_for_packets(); int check_header(char * received_datagram, int buffer_len, packet * packet_to_return); -int validate_tlv(char *data, int pos, short packet_len); +int validate_tlv(char *data, int pos, int16_t packet_len); int update_neighbours(); -int work_with_tlvs(char * data, short packet_len, struct sockaddr_in6 sender); +int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 sender); void add_tlv(struct packet *packet, union tlv *tlv, char type); @@ -101,12 +98,12 @@ 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); +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, long id, short seqno, char *data); +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, long id, short seqno, char *data); +void add_data(unsigned char len, int64_t id, int16_t seqno, char *data); #endif diff --git a/src/tlv.c b/src/tlv.c index 5fe7dbf..339e8b5 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -68,7 +68,7 @@ int build_neighbour_req(tlv *tlv) { return 0; } -int build_neighbour(tlv *tlv, struct in6_addr ip, short port) { +int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port) { neighbour *new = (neighbour*) malloc(sizeof(neighbour)); if(new == NULL) @@ -92,7 +92,7 @@ int build_network_hash(tlv *tlv, list *data_list) { new->type = 4; new->length = 16; - hash_network(data_list, new->network_hash); + hash_network(data_list, (unsigned char*) new->network_hash); tlv->network_hash = new; @@ -113,7 +113,7 @@ int build_network_state_req(tlv *tlv) { return 0; } -int build_node_hash(tlv *tlv, long node_id, short seqno, char *data) { +int build_node_hash(tlv *tlv, int64_t node_id, int16_t seqno, char *data) { node_hash *new = (node_hash*) malloc(sizeof(node_hash)); if(new == NULL) @@ -125,14 +125,14 @@ int build_node_hash(tlv *tlv, long node_id, short seqno, char *data) { new->seqno = seqno; pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; - hash_data(&pdata, new->node_hash); + hash_data(&pdata, (unsigned char*) new->node_hash); tlv->node_hash = new; return 0; } -int build_node_state_req(tlv *tlv, long node_id) { +int build_node_state_req(tlv *tlv, int64_t node_id) { node_state_req *new = (node_state_req*) malloc(sizeof(node_state_req)); if(new == NULL) @@ -147,7 +147,7 @@ int build_node_state_req(tlv *tlv, long node_id) { return 0; } -int build_node_state(tlv *tlv, long node_id, short seqno, char *data, size_t data_len) { +int build_node_state(tlv *tlv, int64_t node_id, int16_t seqno, char *data, size_t data_len) { node_state *new = (node_state*) malloc(sizeof(node_state)); int len = data_len + 26; @@ -167,7 +167,7 @@ int build_node_state(tlv *tlv, long node_id, short seqno, char *data, size_t dat memcpy(new->data, data, len); pub_data pdata = (pub_data) {.id = node_id, .seqno = seqno, .data = data}; - hash_data(&pdata, new->node_hash); + hash_data(&pdata, (unsigned char*) new->node_hash); tlv->node_state = new; diff --git a/src/tlv.h b/src/tlv.h index c86cdc5..def4788 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -4,6 +4,7 @@ #include #include #include +#include #define LEN_NEIGHBOUR_REQ 0 #define LEN_NEIGHBOUR 18 @@ -65,7 +66,7 @@ typedef struct network_state_req { typedef struct node_hash { unsigned char type; unsigned char length; - long node_id; + int64_t node_id; short seqno; char node_hash[16]; } node_hash; @@ -74,14 +75,14 @@ typedef struct node_hash { typedef struct node_state_req { unsigned char type; unsigned char length; - long node_id; + int64_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; - long node_id; + int64_t node_id; short seqno; char node_hash[16]; char data[192]; @@ -121,9 +122,9 @@ 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_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); -int build_node_state(tlv *tlv, long node_id, short seqno, char *data, size_t data_len); +int build_node_hash(tlv *tlv, int64_t node_id, short seqno, char *data); +int build_node_state_req(tlv *tlv, int64_t node_id); +int build_node_state(tlv *tlv, int64_t node_id, short seqno, char *data, size_t data_len); int build_warning(tlv *tlv, char *message, size_t message_len); #endif From e4e585121e12286dc94ef207cbebecb2d4b510a1 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Thu, 16 Apr 2020 18:52:19 +0200 Subject: [PATCH 17/18] send packets v1 --- src/node.c | 243 ++++++++++++++++++++++++++++++++++++++++++++++++----- src/node.h | 13 ++- src/node.o | Bin 10576 -> 13344 bytes src/tlv.c | 32 ++++++- src/tlv.h | 18 ++-- src/tlv.o | Bin 4744 -> 5064 bytes 6 files changed, 272 insertions(+), 34 deletions(-) diff --git a/src/node.c b/src/node.c index c32b546..4e2c357 100644 --- a/src/node.c +++ b/src/node.c @@ -156,7 +156,194 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { /* ---- Fin fonctions utilitaires ---- */ -int send_tlv(union tlv * tlv_to_send, int tlv_size, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num){ +// Add TLV to packet, if it does not fit then send the packet and reset the packet buff to be able to add more TLVs that will be sent afterwards +int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { + char type = tlv->pad1->type, sent = 0, errval = 0; + unsigned char len; + + // Check if TLV fits in the packet, if not then send the packet and reset it + if(type != 1) { + len = tlv->padn->length + 2; + + if(pack->length + len > 1020) { + errval = send_packet((char*) pack, pack->length, dest, socket_num); + *pack = (packet) {.magic = 95, .version = 1, .length = 0}; + memset(pack->body, 0, 1020); + sent = 1; + } + } else { + if(pack->length >= 1020) { + errval = send_packet((char*) pack, pack->length, dest, socket_num); + *pack = (packet) {.magic = 95, .version = 1, .length = 0}; + memset(pack->body, 0, 1020); + sent = 1; + } + } + + // Copy data from tlv into body + switch(type) { + case 1: + memcpy(pack->body + pack->length, tlv->pad1, 1); + pack->length += 1; + + break; + case 2: + memcpy(pack->body + pack->length, tlv->padn, len); + pack->length += len; + + break; + case 3: + memcpy(pack->body + pack->length, tlv->neighbour, len); + pack->length += len; + + break; + case 4: + memcpy(pack->body + pack->length, tlv->network_hash, len); + pack->length += len; + + break; + case 5: + memcpy(pack->body + pack->length, tlv->network_state_req, len); + pack->length += len; + + break; + case 6: + memcpy(pack->body + pack->length, tlv->node_hash, len); + pack->length += len; + + break; + case 7: + memcpy(pack->body + pack->length, tlv->node_state_req, len); + pack->length += len; + + break; + case 8: + memcpy(pack->body + pack->length, tlv->node_state, len); + pack->length += len; + + break; + case 9: + memcpy(pack->body + pack->length, tlv->warning, len); + pack->length += len; + + break; + default: + return -1; + } + + // If the previous packet was went 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; +} + +// Send length bytes from packet +int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, int socket_num) { + // Vectorized buffer + struct iovec vec_buff = {.iov_len = length, .iov_base = packet_buff}; + + 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. + }; + + int response_code = sendmsg(socket_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; + } else if (response_code < length) { + // debug_print("Sent out only part of the packet."); + error_while_sending = 1; + } 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; + } +} + +// 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 = 4}; + memset(pack.body, 0, 1020); + + // Copy data from tlv into body + switch(type) { + case 1: + memcpy(pack.body, tlv->pad1, 1); + pack.length += 1; + + break; + case 2: + len = tlv->padn->length + 2; + memcpy(pack.body, tlv->padn, len); + pack.length += len; + + break; + case 3: + len = tlv->neighbour->length + 2; + memcpy(pack.body, tlv->neighbour, len); + pack.length += len; + + break; + case 4: + len = tlv->network_hash->length + 2; + memcpy(pack.body, tlv->network_hash, len); + pack.length += len; + + break; + case 5: + len = tlv->network_state_req->length + 2; + memcpy(pack.body, tlv->network_state_req, len); + pack.length += len; + + break; + case 6: + len = tlv->node_hash->length + 2; + memcpy(pack.body, tlv->node_hash, len); + pack.length += len; + + break; + case 7: + len = tlv->node_state_req->length + 2; + memcpy(pack.body, tlv->node_state_req, len); + pack.length += len; + + break; + case 8: + len = tlv->node_state->length + 2; + memcpy(pack.body, tlv->node_state, len); + pack.length += len; + + break; + case 9: + len = tlv->warning->length + 2; + memcpy(pack.body, tlv->warning, len); + pack.length += len; + + break; + default: + return -1; + } + + // Send the packet + 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){ // debug_print("Building packet to send a TLV."); // We first need to build the packet, @@ -302,16 +489,27 @@ int update_neighbours(){ }; // We then look at the differents TLVs in the packet. -int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 sender){ +int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, int socket_num){ int pos = 0; unsigned char tlv_len, hash[16]; char warn[32]; tlv new_tlv, cur_tlv; + new_tlv.pad1 = NULL; + cur_tlv.pad1 = NULL; list *tmp_list; pub_data *pdata; + struct neighbour_peer *random_neighbour; + struct sockaddr_in6 new_neighbour; - struct neighbour_peer * random_neighbour; + packet pack = (packet) {.magic = 95, .version = 1, .length = 0}; + memset(pack.body, 0, 1020); + int ifindex = if_nametoindex("eth0"); + + if(ifindex == 0) { + perror("if_nametoindex failed"); + return -1; + } while(pos < packet_len) { switch(validate_tlv(data, pos, packet_len)) { @@ -332,9 +530,7 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 sender){ // Send a neighbour tlv 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(&pack, &new_tlv, sender, socket_num); // The position is updated tlv_len = data[pos+1]; @@ -345,11 +541,16 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 sender){ // We received a neighbour tlv so a tlv network hash is sent to that address cur_tlv.neighbour = (neighbour*) (data + pos); + // 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 = htons(LISTEN_PORT); + new_neighbour.sin6_scope_id = ifindex; + // Build network hash build_network_hash(&new_tlv, data_list); - - // NOT FINISHED - What packet is it added to? - // add_tlv(packet, &new_tlv, 4); + send_single_tlv(&new_tlv, &new_neighbour, socket_num); // The position is updated tlv_len = data[pos+1]; @@ -363,8 +564,7 @@ int work_with_tlvs(char * data, int16_t 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(&pack, &new_tlv, sender, socket_num); } // The position is updated @@ -381,8 +581,7 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 sender){ while(tmp_list != NULL) { 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(&pack, &new_tlv, sender, socket_num); } // The position is updated @@ -412,8 +611,7 @@ int work_with_tlvs(char * data, int16_t 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(&pack, &new_tlv, sender, socket_num); // The position is updated tlv_len = data[pos+1]; @@ -427,8 +625,7 @@ int work_with_tlvs(char * data, int16_t 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(&pack, &new_tlv, sender, socket_num); } // The position is updated @@ -464,13 +661,19 @@ int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 sender){ // 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); + add_tlv(&pack, &new_tlv, sender, socket_num); return -1; } } + // Free the previously allocated memory + free(new_tlv.pad1); + + // If the packet still has data in it then send it + if(pack.length > 0) + send_packet((char*) &pack, pack.length, sender, socket_num); + return 0; } @@ -535,7 +738,7 @@ void listen_for_packets(){ // struct tlv_list received_tlvs; // if (validate_tlvs(formated_rec_datagram) < 0) - int nbr_success_tlv = work_with_tlvs(req, 1024, sender); + int nbr_success_tlv = work_with_tlvs(req, 1024, &sender, s); 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 ); diff --git a/src/node.h b/src/node.h index 709331c..b59b5b9 100644 --- a/src/node.h +++ b/src/node.h @@ -11,6 +11,7 @@ #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 @@ -69,21 +70,25 @@ int validate_tlv(char *data, int pos, int16_t packet_len); int update_neighbours(); -int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 sender); +int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, int socket_num); -void add_tlv(struct packet *packet, union tlv *tlv, char type); +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_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num); // 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(union tlv * tlv_to_send, int tlv_size, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num); +int send_tlv(tlv *tlv_to_send, int16_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, int tlv_size, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num); +int send_tlvs(struct list * tlv_list, int16_t length, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num); // threaded functions void t_ask_for_more_peers(); diff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339e8b5..1f5b4e8 100644 --- a/src/tlv.c +++ b/src/tlv.c @@ -27,6 +27,9 @@ int build_tlv(tlv *tlv, cmd_token token) { } int build_pad1(tlv *tlv) { + // Free the previously allocated memory + free(tlv->pad1); + pad1 *new = (pad1*) malloc(sizeof(pad1)); if(new == NULL) @@ -40,6 +43,9 @@ int build_pad1(tlv *tlv) { } int build_padn(tlv *tlv, size_t len) { + // Free the previously allocated memory + free(tlv->pad1); + padn *new = (padn*) malloc(sizeof(padn)); if(new == NULL) @@ -47,7 +53,7 @@ int build_padn(tlv *tlv, size_t len) { new->type = 1; new->length = len; - new->mbz = (char*) calloc(sizeof(char), len); + memset(new->mbz, 0, 256); tlv->padn = new; @@ -55,6 +61,9 @@ int build_padn(tlv *tlv, size_t len) { } int build_neighbour_req(tlv *tlv) { + // Free the previously allocated memory + free(tlv->pad1); + neighbour_req *new = (neighbour_req*) malloc(sizeof(neighbour_req)); if(new == NULL) @@ -69,6 +78,9 @@ int build_neighbour_req(tlv *tlv) { } int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port) { + // Free the previously allocated memory + free(tlv->pad1); + neighbour *new = (neighbour*) malloc(sizeof(neighbour)); if(new == NULL) @@ -85,6 +97,9 @@ int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port) { } int build_network_hash(tlv *tlv, list *data_list) { + // Free the previously allocated memory + free(tlv->pad1); + network_hash *new = (network_hash*) malloc(sizeof(network_hash)); if(new == NULL) @@ -100,6 +115,9 @@ int build_network_hash(tlv *tlv, list *data_list) { } int build_network_state_req(tlv *tlv) { + // Free the previously allocated memory + free(tlv->pad1); + network_state_req *new = (network_state_req*) malloc(sizeof(network_state_req)); if(new == NULL) @@ -114,6 +132,9 @@ int build_network_state_req(tlv *tlv) { } int build_node_hash(tlv *tlv, int64_t node_id, int16_t seqno, char *data) { + // Free the previously allocated memory + free(tlv->pad1); + node_hash *new = (node_hash*) malloc(sizeof(node_hash)); if(new == NULL) @@ -133,6 +154,9 @@ int build_node_hash(tlv *tlv, int64_t node_id, int16_t seqno, char *data) { } int build_node_state_req(tlv *tlv, int64_t node_id) { + // Free the previously allocated memory + free(tlv->pad1); + node_state_req *new = (node_state_req*) malloc(sizeof(node_state_req)); if(new == NULL) @@ -148,6 +172,9 @@ 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) { + // Free the previously allocated memory + free(tlv->pad1); + node_state *new = (node_state*) malloc(sizeof(node_state)); int len = data_len + 26; @@ -175,6 +202,9 @@ int build_node_state(tlv *tlv, int64_t node_id, int16_t seqno, char *data, size_ } int build_warning(tlv *tlv, char *message, size_t message_len) { + // Free the previously allocated memory + free(tlv->pad1); + warning *new = (warning*) malloc(sizeof(warning)); int len = message_len; diff --git a/src/tlv.h b/src/tlv.h index def4788..d10d3c9 100644 --- a/src/tlv.h +++ b/src/tlv.h @@ -19,8 +19,8 @@ typedef struct packet { unsigned char magic; // 95 (si autre, ignorer) unsigned char version; // 1 (si autre, ignorer) - short length; // 1020 max - char *body; + int16_t length; // 1020 max + char body[1020]; } packet; // 1 octet @@ -32,7 +32,7 @@ typedef struct pad1 { typedef struct padn { unsigned char type; unsigned char length; - char *mbz; + char mbz[256]; } padn; // 2 octets @@ -46,7 +46,7 @@ typedef struct neighbour { unsigned char type; unsigned char length; struct in6_addr ip; - short port; + int16_t port; } neighbour; // 18 octets @@ -67,7 +67,7 @@ typedef struct node_hash { unsigned char type; unsigned char length; int64_t node_id; - short seqno; + int16_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; - short seqno; + int16_t seqno; char node_hash[16]; char data[192]; } node_state; @@ -119,12 +119,12 @@ int build_tlv(tlv *tlv, struct cmd_token token); int build_pad1(tlv *tlv); int build_padn(tlv *tlv, size_t len); int build_neighbour_req(union tlv *tlv); -int build_neighbour(tlv *tlv, struct in6_addr ip, short port); +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, short seqno, char *data); +int build_node_hash(tlv *tlv, int64_t node_id, int16_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, short seqno, char *data, size_t data_len); +int build_node_state(tlv *tlv, int64_t node_id, int16_t seqno, char *data, size_t data_len); int build_warning(tlv *tlv, char *message, size_t message_len); #endif diff --git a/src/tlv.o b/src/tlv.o index 4bdf7c2702f6b770320093b5f1edd1e33912cf54..1e8ab96c956ad818a9ad859caa1441ac4b7f7b53 100644 GIT binary patch literal 5064 zcmbtWTWlLe6dilr)+A+ZN)e_Yx*$Vb1jeQyBE-Y%=CP>^1roPNZEziD9f$Z4)@zzl z3IWq7tHdSW2q8WYLIMOIe3bm)5|r`-sssoLfz%JArfSuOM^VH>aPRDlGg&(b!IgG) z=FGik?!EJTp*zvzcDV=_7kQAZwm3?N=3lMHXf;M;awXBK7rZlFL4S2cdL~d^@lN2w!e%x>DR2^wV8)#Kc%2G?O5GxTLdpG zDO%sZK>7AB__oJLk{Vc-(T|3m8^&(fb!)YWHtkf*jr7{7$=~n~qFJ9T5?5?G=hCWY zS9Yw_rqL`~KY332S$jvbu11IV4=!YV+q+l@=P%5y@ee5*Z zsSfm6m#N!*)>qc&Ya9=Vgu!`f3n2+B=Sf%xHv?!~hN5R{y?^6B5#%++=T6s&t z8uNn7*)A*N*Q~R?9iE_e3YH%jK}7kW7kY++{)A-)143Nd%s?Y)0h81FaKCOfw}UoF=c4%hH8Fu%!*^Pcnaxn~Q9uF~^A;VNC_Q}?NRAGS`}%ixb> zf1mZMYJIKIbFJCiEm-MpQ}J&r{E+T7T1*!_+gJ)6F{YHww5emeG_2c=qa8}gP&&p-Wm7p)(97lY zh<>z#B#VZgDUB3IpVfyIGyjZY9v;`jg!Jy(b)OPS4poX~MY%V8PdIYN&I)ZspO1Ei zB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!8ie^WiBG&W2kE9nPyRJw){#TXvD zO@5y*nfzN!7Kei4(Y>Vl2&&En{w!T~J%(+}91o@WkSE6-Iet_%_^m*bQ#0n);XBpE()Bwa^6xi&rozdN=ivzkf@)2PWK$|OoA z-)g*<)?!)X>CM|?nCV0u#U*WVdLPP5!9M5X&z9DORa~wZ|5~QIsNev_R-SH)YFR2B2$hwR-bo{`B(%)TA8QSUx)NG`Jgq?B69Vw=Sm{< z5#eYZv|z3orIkfaQ?4m^-|cdCvG^;HaYgq(tk~#!v35~RrH;@ZRxE{h+~P066<6fN zb{5aO(yU)O?qhMC#dKDWwNV3#H~pg1F)QFX%8KfzsMEZogVpzyOY1Zj{3wy0wS4bF z!X4R($){X8OXl63Y*hKc$CA1z(pphuuV31Yfy-9l1e|n7oEF3?(QDYm73{tL!JDm4DBIQw z|15N|aN^Snc1s2OLj_)0WfOePj>!wq5D3@WcX=D*$VdP^ozN8s588LO)4C&@weM_v z13qc#|8N6t-_y!Q+yU1DH{@M#QEm2)<9AP{!f$G`JOK^C7F-8#orDX)7G=o^H-meW wWhZP4?U8F>Ak-p9;6kWHnXZDH2&JH*y2YFFmD40(xVqU}VCB;r&=idR3sn=FLI3~& From dd532dc58017dfff3acd5ef1f9485a3ab0172014 Mon Sep 17 00:00:00 2001 From: n07070 Date: Fri, 17 Apr 2020 15:37:07 +0200 Subject: [PATCH 18/18] Added function to update messages, Added bootstrap function, Added time-delay for node update, --- src/node.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/node.c b/src/node.c index e1d0f89..a5d39e7 100644 --- a/src/node.c +++ b/src/node.c @@ -874,6 +874,7 @@ int bootstrap_node(int * sock_fd){ return 0; } + int main(int argc, const char *argv[]) { printf(">> Starting node\n");