Added a new way to add data to a list of data

This commit is contained in:
n07070 2020-04-28 18:37:21 +02:00
parent 25e703b993
commit 956b249259
1 changed files with 53 additions and 11 deletions

View File

@ -217,15 +217,55 @@ pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
print_debug(">> Adding data to the data list.");
// If id is the same as this node's id then we only update seqno
if(id == NODE_ID) {
pub_data *node_data = get_data(NODE_ID);
// pub_data *node_data = get_data(NODE_ID);
//
// if (node_data == NULL) {
// if (datagram == NULL) {
// data_list = (list*) malloc(sizeof(list));
// }
// data_list->data = (void*) new_data;
// data_list->next = NULL;
// }
//
// if(seqno >= node_data->seqno) {
// node_data->seqno = seqno ^ 1;
// }
//
// return 1;
if(seqno >= node_data->seqno) {
node_data->seqno = seqno ^ 1;
}
// We create our pub_data.
pub_data * message = malloc(sizeof(struct pub_data));
message->length = len;
message->id = id;
message->seqno = seqno;
message->data = data;
return 1;
}
// If the data list has never been used, or is empty ( same thing )
if (data_list = NULL) {
data_list = (list*) malloc(sizeof(list));
} else {
// Otherwise, we move until the last element of the dala_list,
// and add or data there.
// We use a temporary address to avoid writing to the static list.
// Seems weird but ok.
list *tmp = data_list;
while(tmp->next != NULL){
tmp = tmp->next;
}
// We create the next node of the linked list.
list * new_node = malloc(sizeof(struct list));
new_node->data = (void *) message;
// Adding the message to the list.
tmp->next = (void *) new_node;
}
} else {
}
// Copy data
pub_data *new_data = copy_data(len, id, seqno, data);
@ -266,7 +306,7 @@ int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
new_node->next = tmp;
last->next = new_node;
return;
return 1;
} else if(id == cur_id) {
// If data already exists for this id then we update it if it's seqno is greater than the one stored
pub_data *cur_data = (pub_data*) tmp->data;
@ -724,13 +764,15 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack
}
int add_message(char * message, int message_len){
int seqno = 0;
int rc = add_data(message_len, NODE_ID ,seqno, message);
int seqno = 1337;
// int add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
int rc = add_data((unsigned char) message_len, (int64_t) NODE_ID ,(int16_t) seqno, message);
if (rc > 0) {
print_debug(">> Message added.");
}
return 0;
}
// We then look at the differents TLVs in the packet.
int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *sender, int socket_num){
int16_t packet_len = ((packet*) data)->length;
@ -825,13 +867,13 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
if (DEBUG_LEVEL > 1) {
printf("\x1b[31m[DEBUG]\x1b[0m >> Our hash : ");
for(int x = 0; x < SHA256_DIGEST_LENGTH; x++){
for(int x = 0; x < 16; x++){
printf("%02x", hash[x]);
fflush(0);
}
printf("\n");
printf("\x1b[31m[DEBUG]\x1b[0m >> Received : ");
for(int x = 0; x < SHA256_DIGEST_LENGTH; x++){
for(int x = 0; x < 16; x++){
printf("%02x", cur_tlv.network_hash->network_hash[x]);
fflush(0);
}