Added printing of network hash

This commit is contained in:
n07070 2020-04-27 23:42:11 +02:00
parent dceac0cecd
commit 4539b8c3c7
2 changed files with 36 additions and 7 deletions

View File

@ -1,7 +1,7 @@
#ifndef DEBUG_H
#define DEBUG_H
#define DEBUG_LEVEL 1
#define DEBUG_LEVEL 2
void print_debug(char * msg);

View File

@ -23,6 +23,7 @@ static list *neighbour_list;
// Looks for more peers
int ask_for_peers(int socket_num) {
print_debug(">> Asking for more peers...");
// Only ask for more peers if the neighbour list is small enough
int nbr_peers = len_list(neighbour_list);
if( nbr_peers >= 5){
@ -214,6 +215,7 @@ 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) {
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);
@ -303,6 +305,7 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
// Update the neighbour list
int update_neighbours() {
print_debug(">> Updating neighbours.");
list *tmp = neighbour_list, *last = NULL, *node_to_delete;
neighbour_peer *peer;
time_t curtime;
@ -311,7 +314,11 @@ int update_neighbours() {
// check every neighbour
while(tmp != NULL) {
peer = (neighbour_peer*) tmp->data;
if (DEBUG_LEVEL > 1) {
char * buff_str_ip[1024];
char * ip_str = (char * ) inet_ntop(AF_INET6,&peer->ip,(char * restrict) buff_str_ip, 1024);
printf("\x1b[31m[DEBUG]\x1b[0m >> Checking for neighbour %s\n", ip_str );
}
// Check if peer is temporary
if(peer->is_temporary) {
// If it's been 70 seconds or more since we last received a packet from this peer then remove it from the list
@ -321,6 +328,8 @@ int update_neighbours() {
// increase the count of deleted nodes
deleted++;
print_debug(">> They have not been seen for the past 70 seconds, deleting...");
// If head of the list
if(last == NULL) {
// Store node to delete
@ -349,7 +358,11 @@ int update_neighbours() {
free(node_to_delete);
continue;
} else {
print_debug(">> Peer has been seen in the last 70 seconds, keeping him in.");
}
} else {
print_debug(">> Peer is not temporary, keeping him in.");
}
last = tmp;
@ -362,6 +375,7 @@ int update_neighbours() {
// Add TLV to packet, if it does not fit then send the packet and reset the packet buff to be able to add more TLVs that will be sent afterwards
int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
print_debug(">> Adding tlv to packet");
char type = tlv->pad1->type, sent = 0, errval = 0;
unsigned char len;
@ -435,6 +449,8 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
return -1;
}
print_debug(">> Finished adding the TLVs to the packet");
// If the previous packet was went return 1 or -1 if there was an error sending it
if(sent)
return errval? -1:1;
@ -801,6 +817,21 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
cur_tlv.network_hash = (network_hash*) (data + pos);
hash_network(data_list, hash);
if (DEBUG_LEVEL > 1) {
printf("\x1b[31m[DEBUG]\x1b[0m >> Our hash : ");
for(int x = 0; x < SHA256_DIGEST_LENGTH; 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++){
printf("%02x", cur_tlv.network_hash->network_hash[x]);
fflush(0);
}
printf("\n");
}
if(memcmp(hash, cur_tlv.network_hash->network_hash, 16) == 0) {
print_debug(">> Sending out our network hash.");
build_network_state_req(&new_tlv);
@ -898,7 +929,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
break;
case 9:
print_debug(">> Received warning !");
print_debug(">> \aReceived warning !");
// We received a warning tlv so it's message is printed
cur_tlv.warning = (warning*) (data + pos);
@ -953,7 +984,7 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc
print_debug(">> We have enough peers, we won't add him..");
return -1;
} else if (rc == 1){
print_debug(">> Peer was added to the table.");
print_debug(">> Peer was added to the table.\a");
} else {
print_debug(">> Updated the time it was last seen.");
}
@ -977,6 +1008,7 @@ int t_ask_for_more_peers(int sock_fd){
}
int t_get_network_state(int sock_fd){
print_debug(">> Getting network state...");
return 0;
}
@ -1009,11 +1041,8 @@ int run_node(int sock_fd){
while (1) {
if(time(NULL) >= delay) {
print_debug(">> Asking for more peers...");
t_ask_for_more_peers(sock_fd);
print_debug(">> Updating neighbours...");
t_update_neighbours();
print_debug(">> Getting network state...");
t_get_network_state(sock_fd);
delay = time(NULL) + 20 + (rand() % 10);
}