Merge branch 'add-message' into fix-hash

This commit is contained in:
n07070 2020-04-28 19:20:15 +02:00
commit 73e681a5ea
2 changed files with 67 additions and 17 deletions

View File

@ -214,17 +214,57 @@ pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
} }
// Add new data to data list // Add new data to data list
void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
print_debug(">> Adding data to the data list."); print_debug(">> Adding data to the data list.");
// If id is the same as this node's id then we only update seqno // If id 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) { if(id == NODE_ID) {
node_data->seqno = seqno ^ 1; // pub_data *node_data = get_data(NODE_ID);
//
// if (node_data == NULL) {
// if (datagram == NULL) {
// data_list = (list*) malloc(sizeof(list));
// }
// data_list->data = (void*) new_data;
// data_list->next = NULL;
// }
//
// if(seqno >= node_data->seqno) {
// node_data->seqno = seqno ^ 1;
// }
//
// return 1;
// We create our pub_data.
pub_data * message = malloc(sizeof(struct pub_data));
message->length = len;
message->id = id;
message->seqno = seqno;
message->data = data;
// If the data list has never been used, or is empty ( same thing )
if (data_list = NULL) {
data_list = (list*) malloc(sizeof(list));
} else {
// Otherwise, we move until the last element of the dala_list,
// and add or data there.
// We use a temporary address to avoid writing to the static list.
// Seems weird but ok.
list *tmp = data_list;
while(tmp->next != NULL){
tmp = tmp->next;
} }
return; // We create the next node of the linked list.
list * new_node = malloc(sizeof(struct list));
new_node->data = (void *) message;
// Adding the message to the list.
tmp->next = (void *) new_node;
}
} else {
} }
// Copy data // Copy data
@ -236,7 +276,7 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
data_list->data = (void*) new_data; data_list->data = (void*) new_data;
data_list->next = NULL; data_list->next = NULL;
return; return 2;
} }
// Find correct position for new data // Find correct position for new data
@ -257,7 +297,7 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
data_list->data = (void*) new_data; data_list->data = (void*) new_data;
data_list->next = tmp; data_list->next = tmp;
return; return 2;
} }
// Else, we update the last node // Else, we update the last node
@ -266,7 +306,7 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
new_node->next = tmp; new_node->next = tmp;
last->next = new_node; last->next = new_node;
return; return 1;
} else if(id == cur_id) { } 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 // 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; pub_data *cur_data = (pub_data*) tmp->data;
@ -278,13 +318,13 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
// Free old data // Free old data
free(cur_data); free(cur_data);
return; return 2;
} }
// seqno is smaller so the new data allocated is freed and nothing else is done // seqno is smaller so the new data allocated is freed and nothing else is done
free(new_data); free(new_data);
return; return 2;
} }
// Get next node in list // Get next node in list
@ -299,6 +339,7 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
new_node->data = (void*) new_data; new_node->data = (void*) new_data;
new_node->next = NULL; new_node->next = NULL;
last->next = new_node; last->next = new_node;
return 3;
} }
/* ---- Fin fonctions utilitaires ---- */ /* ---- Fin fonctions utilitaires ---- */
@ -725,8 +766,15 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack
} }
int add_message(char * message, int message_len){ int add_message(char * message, int message_len){
int seqno = 1337;
// int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
int rc = add_data((unsigned char) message_len, (int64_t) NODE_ID ,(int16_t) seqno, message);
if (rc > 0) {
print_debug(">> Message added.");
}
return 0; return 0;
} }
// We then look at the differents TLVs in the packet. // We then look at the differents TLVs in the packet.
int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *sender, int socket_num){ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *sender, int socket_num){
int16_t packet_len = ((packet*) data)->length; int16_t packet_len = ((packet*) data)->length;
@ -821,13 +869,13 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
if (DEBUG_LEVEL > 1) { if (DEBUG_LEVEL > 1) {
printf("\x1b[31m[DEBUG]\x1b[0m >> Our hash : "); printf("\x1b[31m[DEBUG]\x1b[0m >> Our hash : ");
for(int x = 0; x < SHA256_DIGEST_LENGTH; x++){ for(int x = 0; x < 16; x++){
printf("%02x", hash[x]); printf("%02x", hash[x]);
fflush(0); fflush(0);
} }
printf("\n"); printf("\n");
printf("\x1b[31m[DEBUG]\x1b[0m >> Received : "); printf("\x1b[31m[DEBUG]\x1b[0m >> Received : ");
for(int x = 0; x < SHA256_DIGEST_LENGTH; x++){ for(int x = 0; x < 16; x++){
printf("%02x", cur_tlv.network_hash->network_hash[x]); printf("%02x", cur_tlv.network_hash->network_hash[x]);
fflush(0); fflush(0);
} }
@ -927,8 +975,10 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
if (DEBUG_LEVEL > 0) { if (DEBUG_LEVEL > 0) {
printf("\n\t %s \n", (char *) cur_tlv.node_state->data); printf("\n\t %s \n", (char *) cur_tlv.node_state->data);
} }
add_data(cur_tlv.node_state->length - 26, cur_tlv.node_state->node_id, cur_tlv.node_state->seqno, cur_tlv.node_state->data); int rc = add_data(cur_tlv.node_state->length - 26, cur_tlv.node_state->node_id, cur_tlv.node_state->seqno, cur_tlv.node_state->data);
if (rc < 0) {
print_debug(">> Error while adding note state !");
}
// The position is updated // The position is updated
tlv_len = data[pos+1]; tlv_len = data[pos+1];
pos += tlv_len + 2; pos += tlv_len + 2;

View File

@ -136,6 +136,6 @@ pub_data *get_data(int64_t id);
pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data); pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data);
// add new data to data list // add new data to data list
void add_data(unsigned char len, int64_t id, int16_t seqno, char *data); int add_data(unsigned char len, int64_t id, int16_t seqno, char *data);
#endif #endif