From dceac0cecdfedf4c332dc1f06ee81d499a268202 Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 27 Apr 2020 22:31:31 +0200 Subject: [PATCH 1/2] Added debug_function and pretty print Added DEBUG_LEVEL if DEBUG_LEVEL > 3, the execution is step by step. --- src/debug.c | 11 +++ src/debug.h | 8 +++ src/node.c | 193 ++++++++++++++++++++++++++++++++-------------------- 3 files changed, 138 insertions(+), 74 deletions(-) create mode 100644 src/debug.c create mode 100644 src/debug.h diff --git a/src/debug.c b/src/debug.c new file mode 100644 index 0000000..bf3f3e8 --- /dev/null +++ b/src/debug.c @@ -0,0 +1,11 @@ +#include +#include "debug.h" + +void print_debug(char * msg){ + if (DEBUG_LEVEL > 0) { + printf("\x1b[31m[DEBUG]\x1b[0m %s \n", msg); + } + if (DEBUG_LEVEL > 2) { + getchar(); + } +} diff --git a/src/debug.h b/src/debug.h new file mode 100644 index 0000000..043bf8b --- /dev/null +++ b/src/debug.h @@ -0,0 +1,8 @@ +#ifndef DEBUG_H +#define DEBUG_H + +#define DEBUG_LEVEL 1 + +void print_debug(char * msg); + +#endif diff --git a/src/node.c b/src/node.c index d3c333f..adb6f67 100644 --- a/src/node.c +++ b/src/node.c @@ -13,6 +13,7 @@ #include #include #include "node.h" +#include "debug.h" // Static variables static list *data_list; @@ -27,7 +28,7 @@ int ask_for_peers(int socket_num) { if( nbr_peers >= 5){ return 0; } else if (nbr_peers <= 0){ - printf(">> No peers found in the peer list, something terrible happened.\n"); + print_debug(">> No peers found in the peer list, something terrible happened."); return -1; } else { @@ -60,15 +61,15 @@ int ask_for_peers(int socket_num) { neighbour_req.pad1 = NULL; int rc = build_neighbour_req(&neighbour_req); if (rc < 0) { - printf(">> Failed to build neighbour_req\n"); + print_debug(">> Failed to build neighbour_req"); } rc = send_single_tlv(&neighbour_req, &dest, socket_num); if (rc < 0) { - printf(">> Error while sending a TLV.\n"); + print_debug(">> Error while sending a TLV."); return -1; } else { - printf(">> Send TLV.\n"); + print_debug(">> Send TLV."); return 0; } } @@ -88,7 +89,7 @@ int len_list(list *l) { // Get a random neighbour neighbour_peer *get_random_neighbour() { - printf(">> Getting random peer...\n"); + print_debug(">> Getting random peer..."); // Get a random number srand((unsigned) time(NULL)); int n = (rand() % len_list(neighbour_list)) + 1; @@ -104,6 +105,14 @@ neighbour_peer *get_random_neighbour() { // Search for this peer in the neighbour table neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) { + print_debug(">> Getting neighbour."); + + if (DEBUG_LEVEL > 1) { + char * buff_str_ip[1024]; + char * ip_str = (char * ) inet_ntop(AF_INET6,ip,(char * restrict) buff_str_ip, 1024); + printf("\x1b[31m[DEBUG]\x1b[0m >> Looking up %s @ %i\n", ip_str, port ); + } + // Look for neighbour list *tmp = neighbour_list; neighbour_peer *peer; @@ -128,21 +137,18 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) { // Return 0 if peer was updated as last_seen int add_n_update_neighbour(struct in6_addr *ip, int16_t port) { - char * buff_str_ip[1024]; - char * ip_str = (char * ) inet_ntop(AF_INET6,ip,(char * restrict) buff_str_ip, 1024); - printf(">> Found peer %s @ %i.\n", ip_str, port); // We try to find a peer with this address and port. neighbour_peer *peer = get_neighbour(ip, port); time_t curtime; if (peer == NULL) { - printf(">> We don't know this peer yet\n"); + print_debug(">> We don't know this peer yet"); // check if there are less than 15 neighbours if(len_list(neighbour_list) >= 15){ return -1; } else { - printf(">> Adding them to the peer table.\n"); + print_debug(">> Adding them to the peer table.\n"); // if there are less, initialize the new peer to add to the list peer = (neighbour_peer*) malloc(sizeof(neighbour_peer)); memcpy(&peer->ip, ip, sizeof(struct in6_addr)); @@ -161,7 +167,14 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port) { return 1; } } else { - printf(">> Found peer %s @ %i, updating the last seen time...\n", ip_str, port); + + if (DEBUG_LEVEL > 0) { + char * buff_str_ip[1024]; + char * ip_str = (char * ) inet_ntop(AF_INET6,ip,(char * restrict) buff_str_ip, 1024); + printf("\x1b[31m[DEBUG]\x1b[0m >> Found peer %s @ %i, updating the last seen time...\n", ip_str, port); + + } + time_t curtime; // if the peer was already in the list, update it time(&curtime); @@ -449,13 +462,13 @@ int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, in int response_code = sendmsg(socket_num, &packet_tlv_send_out, 0); if (response_code < 0) { - printf(">> Unable to send out the packet to peer.\n"); + print_debug(">> Unable to send out the packet to peer."); error_while_sending = 1; } else if (response_code < length) { - printf(">> Sent out only part of the packet.\n"); + print_debug(">> Sent out only part of the packet."); error_while_sending = 1; } else { - printf(">> Send out packet to peer, size : %i\n", response_code); + print_debug(">> Send out packet to peer."); } if (error_while_sending == 1) { @@ -532,12 +545,11 @@ int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { } // Send the packet - printf("(%i)\n", pack.length ); return send_packet((char*) &pack, pack.length, dest, socket_num); } int send_tlv(tlv *tlv_to_send, int16_t tlv_size, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num){ - // debug_print("Building packet to send a TLV."); + print_debug(">> Building packet to send a TLV."); // We first need to build the packet, char packet_buff[1024]; @@ -545,7 +557,7 @@ int send_tlv(tlv *tlv_to_send, int16_t tlv_size, struct sockaddr_in6 * dest_list pack.magic = 95; pack.version = 1; if (tlv_size > 1020) { - perror(">> Unable to send the tlv, it's size if above 1020 bytes."); + print_debug(">> Unable to send the tlv, it's size if above 1020 bytes."); return -1; } else { memcpy((void *) pack.body, tlv_to_send, tlv_size); @@ -556,7 +568,9 @@ int send_tlv(tlv *tlv_to_send, int16_t tlv_size, struct sockaddr_in6 * dest_list // packet_buff = (char *) pack; memcpy(&packet_buff,&pack,1024); - // debug_print("Packet has been built."); + if (DEBUG_LEVEL > 1) { + print_debug(">> Packet has been built."); + } // Vectorized buffer struct iovec vec_buff = { .iov_len = sizeof(packet_buff), .iov_base = packet_buff }; @@ -575,20 +589,24 @@ int send_tlv(tlv *tlv_to_send, int16_t tlv_size, struct sockaddr_in6 * dest_list int response_code = sendmsg((int) socket_num, &packet_tlv_send_out, 0); if (response_code < 0) { - // debug_print("Unable to send out the packet to peer %i", i); + if (DEBUG_LEVEL > 0) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Unable to send out the packet to peer %li", i); + } error_while_sending = 1; continue; } else if (response_code < sizeof(packet_tlv_send_out)) { - // debug_print("Sent out only part of the packet."); + print_debug(">> Sent out only part of the packet."); error_while_sending = 1; continue; } else { - // debug_print("Send out packet to peer %i", i); + if (DEBUG_LEVEL > 0) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Send out packet to peer %li", i); + } } } if (error_while_sending == 1) { - // debug_print("Error occured while sending out a packet."); + print_debug(">> Error occured while sending out a packet."); return -1; } else { return 0; @@ -603,13 +621,13 @@ int validate_tlv(char *data, int pos, int16_t packet_len){ // Nothing to do in this case if(type == 0){ - printf(">> Found padding TLV type.\n"); + print_debug(">> Found padding TLV type."); return 0; } // Check that we can read a length if(pos + 1 >= packet_len){ - printf(">> Reading out of packet length. \n"); + print_debug(">> Reading outside of packet's max length."); return -1; } // 0 1 2 3 = Packet @@ -617,13 +635,14 @@ int validate_tlv(char *data, int pos, int16_t packet_len){ unsigned char tlv_len = data[pos+1]; // Check that the tlv does not exceed the packet length - printf("%i ; %i ; %i\n", pos, tlv_len, packet_len ); if(pos + tlv_len > packet_len){ - printf(">> The TLV Length exceed the packet length\n"); + print_debug(">> The TLV Length exceed the packet length\n"); return -1; } - printf(">> TLV has type %i\n", type ); + if (DEBUG_LEVEL > 1) { + printf("\x1b[31m[DEBUG]\x1b[0m >> TLV has type %i\n", type ); + } // Returns the type of the tlv or -1 if something went wrong switch(type) { @@ -667,21 +686,20 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack // We need to check a few things ; // The first byte must be worth 95, if (packet_to_return->magic != 95) { - perror(">> The magic number of the packet is no good."); + print_debug(">> The magic number of the packet is no good."); return -1; } // The second byte must be worth 1, if (packet_to_return->version != 1) { - perror(">> The version number of the packet is no good."); + print_debug(">> The version number of the packet is no good."); return -1; } // Convert to hardware order. ((packet*) packet_to_return)->length = ntohs(((packet*) packet_to_return)->length); - printf("packet : %i, %i \n", packet_to_return->length + 4,received_data_len ); if (packet_to_return->length + 4 < received_data_len ) { - perror(">> The packet length is bigger than the UDP datagram, which is not possible with the current laws of physics."); + print_debug(">> The packet length is bigger than the UDP datagram, which is not possible with the current laws of physics."); return -1; } @@ -696,7 +714,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s int16_t packet_len = ((packet*) data)->length; if(packet_len != total_packet_len - 4) { - fprintf(stderr, ">> Length indicated in packet differs from real length of packet received.\n"); + print_debug(">> Length indicated in packet differs from real length of packet received, disgarding packet."); return -1; } @@ -731,18 +749,20 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s switch(validate_tlv(data, pos, total_packet_len)) { case 0: // We received a padding tlv so it is ignored + print_debug(">> Received padding tlv, ignoring..."); pos += 1; break; case 1: // We received a padding tlv so it is ignored + print_debug(">> Received padding(n) tlv, ignoring..."); tlv_len = data[pos+1]; pos += tlv_len + 2; 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 random_neighbour = get_random_neighbour(); build_neighbour(&new_tlv, random_neighbour->ip, random_neighbour->port); @@ -754,6 +774,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s 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); @@ -771,19 +792,22 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s // The position is updated tlv_len = data[pos+1]; pos += tlv_len + 2; - printf("fzfzfe\n"); break; case 4: - // We reveived a network hash tlv so we compare the hash with our own, if they differ we send a network state request tlv + 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 cur_tlv.network_hash = (network_hash*) (data + pos); hash_network(data_list, hash); if(memcmp(hash, cur_tlv.network_hash->network_hash, 16) == 0) { - build_network_state_req(&new_tlv); + print_debug(">> Sending out our network hash."); + build_network_state_req(&new_tlv); send_single_tlv(&new_tlv, sender, socket_num); - - } + } else { + print_debug(">> We're up to date."); + } // The position is updated tlv_len = data[pos+1]; @@ -791,8 +815,9 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s break; case 5: - // We received a network state request tlv so a series of tlv node hash have to be sent for each data known - + // We received a network state request tlv + // so a series of tlv node hash have to be sent for each data known + print_debug(">> Received network state request, sending back hashes for messages."); // for each known data build a node hash and add to packet tmp_list = data_list; @@ -807,7 +832,11 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s break; case 6: - // We received a node hash tlv so if there is no entry for node_id in the data list or the hashes differ we send a node state request, if the hashes are identical nothing has to be done + // We received a node hash tlv so + // if there is no entry for node_id in the data list or the hashes differ + // we send a node state request, + //if the hashes are identical nothing has to be done + print_debug(">> Received node hash, updating message entry..."); cur_tlv.node_hash = (node_hash*) (data + pos); pdata = get_data(cur_tlv.node_hash->node_id); @@ -837,8 +866,11 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s 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 - cur_tlv.node_state_req = (node_state_req*) (data + pos); + // 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); pdata = get_data(cur_tlv.node_state_req->node_id); if(pdata != NULL) { @@ -852,7 +884,10 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s break; case 8: - // We received a node state tlv so we add it to the data list or update the data stored + // We received a node state tlv so + // we add it to the data list + // or update the data stored + print_debug(">> Received node state, updating..."); cur_tlv.node_state = (node_state*) (data + pos); add_data(cur_tlv.node_state->length - 26, cur_tlv.node_state->node_id, cur_tlv.node_state->seqno, cur_tlv.node_state->data); @@ -863,11 +898,12 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s break; case 9: + print_debug(">> Received warning !"); // We received a warning tlv so it's message is printed cur_tlv.warning = (warning*) (data + pos); // Print exactly new_tlv.length characters from new_tlv.message - sprintf(warn, ">> WARNING:\n%%.%ds", cur_tlv.warning->length + 1); + sprintf(warn, "\x1b[31m>> WARNING:\n%%.%ds \x1b[0m", cur_tlv.warning->length + 1); printf(warn, cur_tlv.warning->message); // The position is updated @@ -878,7 +914,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s default: // A malformed packet was found so we stop looking for more packets and send a warning tlv strcpy(warn, "Packet is malformed."); - printf("Malformed packet\n"); + print_debug(">> Malformed packet, we won't treat it."); build_warning(&new_tlv, warn, strlen(warn)); add_tlv(&pack, &new_tlv, sender, socket_num); @@ -890,8 +926,9 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s free(new_tlv.pad1); // If the packet still has data in it then send it - if(pack.length > 0) + if(pack.length > 0){ send_packet((char*) &pack, pack.length, sender, socket_num); + } return 0; } @@ -902,7 +939,7 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc // and we return it in the struct designed to work with it. struct packet formated_rec_datagram; if(check_header(received_data_buffer, received_data_len, &formated_rec_datagram) < 0){ - perror(">> Error while checking the header, aborting this packet, by choice, and conviction."); + print_debug(">> Error while checking the header, aborting this packet, by choice, and conviction."); return -1; } @@ -913,21 +950,23 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc int rc = add_n_update_neighbour(&ip, port); if( rc == -1) { - printf(">> We have enough peers, we won't add him..\n"); + print_debug(">> We have enough peers, we won't add him.."); return -1; } else if (rc == 1){ - printf(">> Peer was added to the table.\n"); + print_debug(">> Peer was added to the table."); } else { - printf(">> Updated the time it was last seen.\n"); + print_debug(">> Updated the time it was last seen."); } int nbr_success_tlv = work_with_tlvs(received_data_buffer, received_data_len, sender, sock_fd); if (nbr_success_tlv < 0){ - perror(">> Error while treating the TLVs of the packet."); - printf(">> Managed to deal with %i TLVs\n", -nbr_success_tlv ); + print_debug(">> Error while treating the TLVs of the packet."); + if (DEBUG_LEVEL > 1) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Managed to deal with %i TLVs", -nbr_success_tlv ); + } return -2; } else { - printf(">> Done working with the TLVs of the packet, listening for new packets.\n"); + print_debug(">> Done working with the TLVs of the packet, listening for new packets."); return 0; } @@ -946,7 +985,7 @@ int t_update_neighbours(){ } int run_node(int sock_fd){ - printf(">> Running node...\n"); + print_debug(">> Running node..."); int ret; ssize_t bytes; @@ -970,11 +1009,11 @@ int run_node(int sock_fd){ while (1) { if(time(NULL) >= delay) { - printf(">> Asking for more peers...\n"); + print_debug(">> Asking for more peers..."); t_ask_for_more_peers(sock_fd); - printf(">> Updating neighbours...\n"); + print_debug(">> Updating neighbours..."); t_update_neighbours(); - printf(">> Getting network state...\n"); + print_debug(">> Getting network state..."); t_get_network_state(sock_fd); delay = time(NULL) + 20 + (rand() % 10); } @@ -994,17 +1033,17 @@ int run_node(int sock_fd){ ret = poll(fds, 2, 5); if (ret < 0) { - printf(">> Error - poll returned error: %s\n", strerror(errno)); + print_debug(">> Error - poll returned error"); break; } else if (ret > 0) { /* Regardless of requested events, poll() can always return these */ if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { - printf("Error - poll indicated stdin error\n"); + print_debug("Error - poll indicated stdin error\n"); break; } if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) { - printf("Error - poll indicated socket error\n"); + print_debug("Error - poll indicated socket error\n"); break; } @@ -1012,14 +1051,16 @@ int run_node(int sock_fd){ if (fds[0].revents & (POLLIN | POLLPRI)) { bytes = read(0, input_buffer, sizeof(input_buffer)); if (bytes < 0) { - printf("Error - stdin error: %s\n", strerror(errno)); + print_debug(">> Error - stdin error"); break; } input_buffer[strcspn(input_buffer, "\n")] = 0; - printf(">> Adding following message to the table : ā€œ%sā€\n", input_buffer ); + if (DEBUG_LEVEL > 0) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Adding following message to the table : ā€œ%sā€\n", input_buffer ); + } // Add message to the message table. if (add_message(input_buffer, bytes) < 0) { - perror(">> Error while trying to add the message to the list of messages, please try again.."); + print_debug(">> Error while trying to add the message to the list of messages, please try again.."); } } @@ -1042,15 +1083,19 @@ int run_node(int sock_fd){ bytes = recvmsg(sock_fd, &msg_from_peer, 0); if (bytes < 0) { - printf("Error - recvfrom error: %s\n", strerror(errno)); + if (DEBUG_LEVEL > 0) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Error - recvfrom error: %s\n", strerror(errno)); + } break; } if (bytes > 0) { - printf("Received %i bytes as : %s\n", (int)bytes, output_buffer); + if (DEBUG_LEVEL > 0) { + printf("\x1b[31m[DEBUG]\x1b[0m >> Received %i bytes as : %s\n", (int)bytes, output_buffer); + } // Treat incoming packets. int work_tlv_status = listen_for_packets(output_buffer, bytes, &sender, sock_fd); if (work_tlv_status < 0) { - perror(">> Error while treating the incoming packet."); + print_debug(">> Error while treating the incoming packet."); } } } @@ -1066,14 +1111,14 @@ int run_node(int sock_fd){ // This function runs once, and sets the sock_fd as well as the neighbourhood int bootstrap_node(int * sock_fd){ - printf(">> Boostraping node...\n"); + print_debug(">> Boostraping node..."); struct sockaddr_in6 server_addr; /* Create UDP socket */ * sock_fd = socket(AF_INET6, SOCK_DGRAM, 0); if ( * sock_fd < 0) { - printf("Error - failed to open socket: %s\n", strerror(errno)); + print_debug(">> Error - failed to open socket"); return -1; } @@ -1083,8 +1128,8 @@ int bootstrap_node(int * sock_fd){ // server_addr.sin6_addr.in6_addr = htonl(INADDR_ANY); server_addr.sin6_port = htons(LISTEN_PORT); if (bind( * sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0) { - printf("Error - failed to bind socket: %s\n", strerror(errno)); - return -2; + print_debug(">> Error - failed to bind socket"); + return -2; } /* Make the first peer*/ @@ -1102,18 +1147,18 @@ int bootstrap_node(int * sock_fd){ root_peer->last_seen = root_peer_seen; // TODO: Add the first peer to the neighbourhood - printf(">> Adding the first root peer to the list...\n"); + print_debug(">> Adding the first root peer to the list..."); neighbour_list = malloc(sizeof(struct list)); neighbour_list->data = (void *) root_peer; neighbour_list->next = NULL; - printf(">> Boostraping done.\n"); + print_debug(">> Boostraping done."); return 0; } int main(int argc, const char *argv[]) { - printf(">> Starting node\n"); + print_debug(">> Starting node"); int sock_fd; bootstrap_node(&sock_fd); From 4539b8c3c7275d59467cbe3d5a28398456698c24 Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 27 Apr 2020 23:42:11 +0200 Subject: [PATCH 2/2] Added printing of network hash --- src/debug.h | 2 +- src/node.c | 41 +++++++++++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/debug.h b/src/debug.h index 043bf8b..bf199d7 100644 --- a/src/debug.h +++ b/src/debug.h @@ -1,7 +1,7 @@ #ifndef DEBUG_H #define DEBUG_H -#define DEBUG_LEVEL 1 +#define DEBUG_LEVEL 2 void print_debug(char * msg); diff --git a/src/node.c b/src/node.c index adb6f67..3775236 100644 --- a/src/node.c +++ b/src/node.c @@ -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); }