Some changes

This commit is contained in:
n07070 2020-04-29 22:22:58 +02:00
parent 352aed242d
commit ae893c91fc
4 changed files with 23 additions and 24 deletions

View File

@ -1,5 +1,6 @@
#ifndef DEBUG_H
#define DEBUG_H
#include "node.h"

View File

@ -1,22 +1,5 @@
// This is the main file of the Dazibao project. It represents the node, and
// handles all of the main logic, including the network connexions.
<<<<<<< HEAD
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <arpa/inet.h>
#include <wchar.h>
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <locale.h>
=======
>>>>>>> add-message
#include "node.h"
// Static variables
@ -837,6 +820,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
break;
case 2:
// We received a neighbour request so a random neighbor tlv has to be sent
print_debug(">> Received neighbour request, sending out a neighbour address.");
// Send a neighbour tlv
@ -858,6 +842,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
break;
case 3:
print_debug(">> Received neighbour tlv, sending back network hash.");
// We received a neighbour tlv so a tlv network hash is sent to that address
cur_tlv.neighbour = (neighbour*) (data + pos);
@ -879,6 +864,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
break;
case 4:
print_debug(">> Received network_hash, comparing with our own..");
// We reveived a network hash tlv so we compare the hash with our own,
// if they differ we send a network state request tlv
@ -924,6 +910,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
pdata = (pub_data*) tmp_list->data;
build_node_hash(&new_tlv, pdata->id, pdata->seqno, pdata->data);
add_tlv(&pack, &new_tlv, sender, socket_num);
tmp_list = tmp_list->next;
}
// The position is updated
@ -965,11 +952,13 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
break;
case 7:
// 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
print_debug(">> Received node state request. Processing...");
cur_tlv.node_state_req = (node_state_req*) (data[pos]);
cur_tlv.node_state_req = (node_state_req*) (data + pos);
pdata = get_data(ntohl(cur_tlv.node_state_req->node_id));
if(pdata != NULL) {
@ -990,25 +979,32 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
cur_tlv.node_state = (node_state*) (data + pos);
print_debug(">> Received message ! ");
printf("%d\n", cur_tlv.node_state->type );
printf("%d\n", cur_tlv.node_state->length );
printf("%lu\n", cur_tlv.node_state->node_id );
printf("%hu\n", cur_tlv.node_state->seqno );
printf("%.16s\n", cur_tlv.node_state->node_hash);
printf("%s\n", cur_tlv.node_state->data);
if (DEBUG_LEVEL > 0) {
if (cur_tlv.node_state->data == NULL) {
print_error("The data in the current node is NULL !");
return -1;
}
printf("%s\n", data + pos );
printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0\n", (const wchar_t*) cur_tlv.node_state->data);
}
// Compare hashes
pdata = get_data(ntohl(cur_tlv.node_state->node_id));
pdata = get_data(cur_tlv.node_state->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
memset(hash, 0, 16);
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) {
if(memcmp(hash, cur_tlv.node_state->node_hash, 16) == 0) {
// The position is updated
tlv_len = data[pos+1];
pos += 2;
@ -1060,7 +1056,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
if(pack.length > 0){
send_packet((char*) &pack, pack.length, sender, socket_num);
}
print_data(data_list);
return 0;
}

View File

@ -17,6 +17,8 @@
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>
#include <locale.h>
/* 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

View File

@ -66,7 +66,7 @@ typedef struct network_state_req {
typedef struct node_hash {
unsigned char type;
unsigned char length;
int64_t node_id;
uint64_t node_id;
uint16_t seqno;
char node_hash[16];
} node_hash;
@ -82,7 +82,7 @@ typedef struct node_state_req {
typedef struct node_state {
unsigned char type;
unsigned char length;
int64_t node_id;
uint64_t node_id;
uint16_t seqno;
char node_hash[16];
char data[192];