From 50bb64059bc7002de83430400bc0d61399ccc487 Mon Sep 17 00:00:00 2001 From: gonzalef Date: Thu, 16 Apr 2020 15:27:32 +0200 Subject: [PATCH] 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