Merge branch 'add-message' into fix-hash
This commit is contained in:
commit
73e681a5ea
82
src/node.c
82
src/node.c
@ -214,18 +214,58 @@ 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, 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;
|
||||
}
|
||||
// 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);
|
||||
@ -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->next = NULL;
|
||||
|
||||
return;
|
||||
return 2;
|
||||
}
|
||||
|
||||
// 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->next = tmp;
|
||||
|
||||
return;
|
||||
return 2;
|
||||
}
|
||||
|
||||
// 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;
|
||||
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;
|
||||
@ -278,13 +318,13 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
|
||||
// Free old data
|
||||
free(cur_data);
|
||||
|
||||
return;
|
||||
return 2;
|
||||
}
|
||||
|
||||
// seqno is smaller so the new data allocated is freed and nothing else is done
|
||||
free(new_data);
|
||||
|
||||
return;
|
||||
return 2;
|
||||
}
|
||||
|
||||
// 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->next = NULL;
|
||||
last->next = new_node;
|
||||
return 3;
|
||||
}
|
||||
|
||||
/* ---- 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 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;
|
||||
@ -821,13 +869,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);
|
||||
}
|
||||
@ -927,8 +975,10 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
|
||||
if (DEBUG_LEVEL > 0) {
|
||||
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
|
||||
tlv_len = data[pos+1];
|
||||
pos += tlv_len + 2;
|
||||
|
@ -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);
|
||||
|
||||
// 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
|
||||
|
Loading…
Reference in New Issue
Block a user