Merge branch 'debug-fnc' into 'master'
Debug fnc See merge request perdriau/dazibao!13
This commit is contained in:
commit
c79c3c0e19
11
src/debug.c
Normal file
11
src/debug.c
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#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();
|
||||||
|
}
|
||||||
|
}
|
8
src/debug.h
Normal file
8
src/debug.h
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#ifndef DEBUG_H
|
||||||
|
#define DEBUG_H
|
||||||
|
|
||||||
|
#define DEBUG_LEVEL 2
|
||||||
|
|
||||||
|
void print_debug(char * msg);
|
||||||
|
|
||||||
|
#endif
|
207
src/node.c
207
src/node.c
@ -13,6 +13,7 @@
|
|||||||
#include <poll.h>
|
#include <poll.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "node.h"
|
#include "node.h"
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
// Static variables
|
// Static variables
|
||||||
static list *data_list;
|
static list *data_list;
|
||||||
@ -40,7 +41,7 @@ int len_list(list *l) {
|
|||||||
|
|
||||||
// Get a random neighbour
|
// Get a random neighbour
|
||||||
neighbour_peer *get_random_neighbour() {
|
neighbour_peer *get_random_neighbour() {
|
||||||
printf(">> Getting random peer...\n");
|
print_debug(">> Getting random peer...");
|
||||||
// Get a random number
|
// Get a random number
|
||||||
srand((unsigned) time(NULL));
|
srand((unsigned) time(NULL));
|
||||||
int n = (rand() % len_list(neighbour_list)) + 1;
|
int n = (rand() % len_list(neighbour_list)) + 1;
|
||||||
@ -56,6 +57,14 @@ neighbour_peer *get_random_neighbour() {
|
|||||||
|
|
||||||
// Search for this peer in the neighbour table
|
// Search for this peer in the neighbour table
|
||||||
neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) {
|
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
|
// Look for neighbour
|
||||||
list *tmp = neighbour_list;
|
list *tmp = neighbour_list;
|
||||||
neighbour_peer *peer;
|
neighbour_peer *peer;
|
||||||
@ -80,21 +89,18 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) {
|
|||||||
// Return 0 if peer was updated as last_seen
|
// Return 0 if peer was updated as last_seen
|
||||||
int add_n_update_neighbour(struct in6_addr *ip, int16_t port) {
|
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.
|
// We try to find a peer with this address and port.
|
||||||
neighbour_peer *peer = get_neighbour(ip, port);
|
neighbour_peer *peer = get_neighbour(ip, port);
|
||||||
time_t curtime;
|
time_t curtime;
|
||||||
|
|
||||||
if (peer == NULL) {
|
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
|
// check if there are less than 15 neighbours
|
||||||
if(len_list(neighbour_list) >= 15){
|
if(len_list(neighbour_list) >= 15){
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} 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
|
// if there are less, initialize the new peer to add to the list
|
||||||
peer = (neighbour_peer*) malloc(sizeof(neighbour_peer));
|
peer = (neighbour_peer*) malloc(sizeof(neighbour_peer));
|
||||||
memcpy(&peer->ip, ip, sizeof(struct in6_addr));
|
memcpy(&peer->ip, ip, sizeof(struct in6_addr));
|
||||||
@ -113,7 +119,14 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
} else {
|
} 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;
|
time_t curtime;
|
||||||
// if the peer was already in the list, update it
|
// if the peer was already in the list, update it
|
||||||
time(&curtime);
|
time(&curtime);
|
||||||
@ -153,6 +166,7 @@ pub_data *copy_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
|
|||||||
|
|
||||||
// Add new data to data list
|
// Add new data to data list
|
||||||
void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
|
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 is the same as this node's id then we only update seqno
|
||||||
if(id == NODE_ID) {
|
if(id == NODE_ID) {
|
||||||
pub_data *node_data = get_data(NODE_ID);
|
pub_data *node_data = get_data(NODE_ID);
|
||||||
@ -242,6 +256,7 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) {
|
|||||||
|
|
||||||
// Update the neighbour list
|
// Update the neighbour list
|
||||||
int update_neighbours() {
|
int update_neighbours() {
|
||||||
|
print_debug(">> Updating neighbours.");
|
||||||
list *tmp = neighbour_list, *last = NULL, *node_to_delete;
|
list *tmp = neighbour_list, *last = NULL, *node_to_delete;
|
||||||
neighbour_peer *peer;
|
neighbour_peer *peer;
|
||||||
time_t curtime;
|
time_t curtime;
|
||||||
@ -250,7 +265,11 @@ int update_neighbours() {
|
|||||||
// check every neighbour
|
// check every neighbour
|
||||||
while(tmp != NULL) {
|
while(tmp != NULL) {
|
||||||
peer = (neighbour_peer*) tmp->data;
|
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
|
// Check if peer is temporary
|
||||||
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
|
// If it's been 70 seconds or more since we last received a packet from this peer then remove it from the list
|
||||||
@ -260,6 +279,8 @@ int update_neighbours() {
|
|||||||
// increase the count of deleted nodes
|
// increase the count of deleted nodes
|
||||||
deleted++;
|
deleted++;
|
||||||
|
|
||||||
|
print_debug(">> They have not been seen for the past 70 seconds, deleting...");
|
||||||
|
|
||||||
// If head of the list
|
// If head of the list
|
||||||
if(last == NULL) {
|
if(last == NULL) {
|
||||||
// Store node to delete
|
// Store node to delete
|
||||||
@ -288,7 +309,11 @@ int update_neighbours() {
|
|||||||
free(node_to_delete);
|
free(node_to_delete);
|
||||||
|
|
||||||
continue;
|
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;
|
last = tmp;
|
||||||
@ -301,6 +326,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
|
// 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) {
|
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;
|
char type = tlv->pad1->type, sent = 0, errval = 0;
|
||||||
unsigned char len;
|
unsigned char len;
|
||||||
|
|
||||||
@ -374,6 +400,8 @@ int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
|
|||||||
return -1;
|
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 the previous packet was went return 1 or -1 if there was an error sending it
|
||||||
if(sent)
|
if(sent)
|
||||||
return errval? -1:1;
|
return errval? -1:1;
|
||||||
@ -401,13 +429,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);
|
int response_code = sendmsg(socket_num, &packet_tlv_send_out, 0);
|
||||||
if (response_code < 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;
|
error_while_sending = 1;
|
||||||
} else if (response_code < length) {
|
} 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;
|
error_while_sending = 1;
|
||||||
} else {
|
} else {
|
||||||
printf(">> Send out packet to peer, size : %i\n", response_code);
|
print_debug(">> Send out packet to peer.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error_while_sending == 1) {
|
if (error_while_sending == 1) {
|
||||||
@ -484,12 +512,11 @@ int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send the packet
|
// Send the packet
|
||||||
printf("(%i)\n", pack.length );
|
|
||||||
return send_packet((char*) &pack, pack.length, dest, socket_num);
|
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){
|
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,
|
// We first need to build the packet,
|
||||||
char packet_buff[1024];
|
char packet_buff[1024];
|
||||||
@ -497,7 +524,7 @@ int send_tlv(tlv *tlv_to_send, int16_t tlv_size, struct sockaddr_in6 * dest_list
|
|||||||
pack.magic = 95;
|
pack.magic = 95;
|
||||||
pack.version = 1;
|
pack.version = 1;
|
||||||
if (tlv_size > 1020) {
|
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;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
memcpy((void *) pack.body, tlv_to_send, tlv_size);
|
memcpy((void *) pack.body, tlv_to_send, tlv_size);
|
||||||
@ -508,7 +535,9 @@ int send_tlv(tlv *tlv_to_send, int16_t tlv_size, struct sockaddr_in6 * dest_list
|
|||||||
// packet_buff = (char *) pack;
|
// packet_buff = (char *) pack;
|
||||||
memcpy(&packet_buff,&pack,1024);
|
memcpy(&packet_buff,&pack,1024);
|
||||||
|
|
||||||
// debug_print("Packet has been built.");
|
if (DEBUG_LEVEL > 1) {
|
||||||
|
print_debug(">> Packet has been built.");
|
||||||
|
}
|
||||||
|
|
||||||
// Vectorized buffer
|
// Vectorized buffer
|
||||||
struct iovec vec_buff = { .iov_len = sizeof(packet_buff), .iov_base = packet_buff };
|
struct iovec vec_buff = { .iov_len = sizeof(packet_buff), .iov_base = packet_buff };
|
||||||
@ -527,20 +556,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);
|
int response_code = sendmsg((int) socket_num, &packet_tlv_send_out, 0);
|
||||||
if (response_code < 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;
|
error_while_sending = 1;
|
||||||
continue;
|
continue;
|
||||||
} else if (response_code < sizeof(packet_tlv_send_out)) {
|
} 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;
|
error_while_sending = 1;
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} 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) {
|
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;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
@ -555,13 +588,13 @@ int validate_tlv(char *data, int pos, int16_t packet_len){
|
|||||||
|
|
||||||
// Nothing to do in this case
|
// Nothing to do in this case
|
||||||
if(type == 0){
|
if(type == 0){
|
||||||
printf(">> Found padding TLV type.\n");
|
print_debug(">> Found padding TLV type.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check that we can read a length
|
// Check that we can read a length
|
||||||
if(pos + 1 >= packet_len){
|
if(pos + 1 >= packet_len){
|
||||||
printf(">> Reading out of packet length. \n");
|
print_debug(">> Reading outside of packet's max length.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
// 0 1 2 3 = Packet
|
// 0 1 2 3 = Packet
|
||||||
@ -569,13 +602,14 @@ int validate_tlv(char *data, int pos, int16_t packet_len){
|
|||||||
unsigned char tlv_len = data[pos+1];
|
unsigned char tlv_len = data[pos+1];
|
||||||
|
|
||||||
// Check that the tlv does not exceed the packet length
|
// 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){
|
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;
|
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
|
// Returns the type of the tlv or -1 if something went wrong
|
||||||
switch(type) {
|
switch(type) {
|
||||||
@ -619,21 +653,20 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack
|
|||||||
// We need to check a few things ;
|
// We need to check a few things ;
|
||||||
// The first byte must be worth 95,
|
// The first byte must be worth 95,
|
||||||
if (packet_to_return->magic != 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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The second byte must be worth 1,
|
// The second byte must be worth 1,
|
||||||
if (packet_to_return->version != 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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert to hardware order.
|
// Convert to hardware order.
|
||||||
((packet*) packet_to_return)->length = ntohs(((packet*) packet_to_return)->length);
|
((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 ) {
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -648,7 +681,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
|
|||||||
int16_t packet_len = ((packet*) data)->length;
|
int16_t packet_len = ((packet*) data)->length;
|
||||||
|
|
||||||
if(packet_len != total_packet_len - 4) {
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -683,18 +716,20 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
|
|||||||
switch(validate_tlv(data, pos, total_packet_len)) {
|
switch(validate_tlv(data, pos, total_packet_len)) {
|
||||||
case 0:
|
case 0:
|
||||||
// We received a padding tlv so it is ignored
|
// We received a padding tlv so it is ignored
|
||||||
|
print_debug(">> Received padding tlv, ignoring...");
|
||||||
pos += 1;
|
pos += 1;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
// We received a padding tlv so it is ignored
|
// We received a padding tlv so it is ignored
|
||||||
|
print_debug(">> Received padding(n) tlv, ignoring...");
|
||||||
tlv_len = data[pos+1];
|
tlv_len = data[pos+1];
|
||||||
pos += tlv_len + 2;
|
pos += tlv_len + 2;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
// We received a neighbour request so a random neighbor tlv has to be sent
|
// 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
|
// Send a neighbour tlv
|
||||||
random_neighbour = get_random_neighbour();
|
random_neighbour = get_random_neighbour();
|
||||||
build_neighbour(&new_tlv, random_neighbour->ip, random_neighbour->port);
|
build_neighbour(&new_tlv, random_neighbour->ip, random_neighbour->port);
|
||||||
@ -706,6 +741,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case 3:
|
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
|
// We received a neighbour tlv so a tlv network hash is sent to that address
|
||||||
cur_tlv.neighbour = (neighbour*) (data + pos);
|
cur_tlv.neighbour = (neighbour*) (data + pos);
|
||||||
|
|
||||||
@ -723,18 +759,36 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
|
|||||||
// The position is updated
|
// The position is updated
|
||||||
tlv_len = data[pos+1];
|
tlv_len = data[pos+1];
|
||||||
pos += tlv_len + 2;
|
pos += tlv_len + 2;
|
||||||
printf("fzfzfe\n");
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 4:
|
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);
|
cur_tlv.network_hash = (network_hash*) (data + pos);
|
||||||
hash_network(data_list, hash);
|
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) {
|
if(memcmp(hash, cur_tlv.network_hash->network_hash, 16) == 0) {
|
||||||
|
print_debug(">> Sending out our network hash.");
|
||||||
build_network_state_req(&new_tlv);
|
build_network_state_req(&new_tlv);
|
||||||
send_single_tlv(&new_tlv, sender, socket_num);
|
send_single_tlv(&new_tlv, sender, socket_num);
|
||||||
|
} else {
|
||||||
|
print_debug(">> We're up to date.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// The position is updated
|
// The position is updated
|
||||||
@ -743,8 +797,9 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case 5:
|
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
|
// for each known data build a node hash and add to packet
|
||||||
tmp_list = data_list;
|
tmp_list = data_list;
|
||||||
|
|
||||||
@ -759,7 +814,11 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case 6:
|
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);
|
cur_tlv.node_hash = (node_hash*) (data + pos);
|
||||||
pdata = get_data(cur_tlv.node_hash->node_id);
|
pdata = get_data(cur_tlv.node_hash->node_id);
|
||||||
|
|
||||||
@ -789,7 +848,10 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case 7:
|
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
|
// 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(cur_tlv.node_state_req->node_id);
|
pdata = get_data(cur_tlv.node_state_req->node_id);
|
||||||
|
|
||||||
@ -804,7 +866,10 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case 8:
|
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);
|
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);
|
add_data(cur_tlv.node_state->length - 26, cur_tlv.node_state->node_id, cur_tlv.node_state->seqno, cur_tlv.node_state->data);
|
||||||
@ -815,11 +880,12 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case 9:
|
case 9:
|
||||||
|
print_debug(">> \aReceived warning !");
|
||||||
// We received a warning tlv so it's message is printed
|
// We received a warning tlv so it's message is printed
|
||||||
cur_tlv.warning = (warning*) (data + pos);
|
cur_tlv.warning = (warning*) (data + pos);
|
||||||
|
|
||||||
// Print exactly new_tlv.length characters from new_tlv.message
|
// 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);
|
printf(warn, cur_tlv.warning->message);
|
||||||
|
|
||||||
// The position is updated
|
// The position is updated
|
||||||
@ -830,7 +896,7 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
|
|||||||
default:
|
default:
|
||||||
// A malformed packet was found so we stop looking for more packets and send a warning tlv
|
// A malformed packet was found so we stop looking for more packets and send a warning tlv
|
||||||
strcpy(warn, "Packet is malformed.");
|
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));
|
build_warning(&new_tlv, warn, strlen(warn));
|
||||||
add_tlv(&pack, &new_tlv, sender, socket_num);
|
add_tlv(&pack, &new_tlv, sender, socket_num);
|
||||||
|
|
||||||
@ -842,8 +908,9 @@ int work_with_tlvs(char * data, int16_t total_packet_len, struct sockaddr_in6 *s
|
|||||||
free(new_tlv.pad1);
|
free(new_tlv.pad1);
|
||||||
|
|
||||||
// If the packet still has data in it then send it
|
// 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);
|
send_packet((char*) &pack, pack.length, sender, socket_num);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -854,7 +921,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.
|
// and we return it in the struct designed to work with it.
|
||||||
struct packet formated_rec_datagram;
|
struct packet formated_rec_datagram;
|
||||||
if(check_header(received_data_buffer, received_data_len, &formated_rec_datagram) < 0){
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -865,21 +932,23 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc
|
|||||||
|
|
||||||
int rc = add_n_update_neighbour(&ip, port);
|
int rc = add_n_update_neighbour(&ip, port);
|
||||||
if( rc == -1) {
|
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;
|
return -1;
|
||||||
} else if (rc == 1){
|
} else if (rc == 1){
|
||||||
printf(">> Peer was added to the table.\n");
|
print_debug(">> Peer was added to the table.\a");
|
||||||
} else {
|
} 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);
|
int nbr_success_tlv = work_with_tlvs(received_data_buffer, received_data_len, sender, sock_fd);
|
||||||
if (nbr_success_tlv < 0){
|
if (nbr_success_tlv < 0){
|
||||||
perror(">> Error while treating the TLVs of the packet.");
|
print_debug(">> Error while treating the TLVs of the packet.");
|
||||||
printf(">> Managed to deal with %i TLVs\n", -nbr_success_tlv );
|
if (DEBUG_LEVEL > 1) {
|
||||||
|
printf("\x1b[31m[DEBUG]\x1b[0m >> Managed to deal with %i TLVs", -nbr_success_tlv );
|
||||||
|
}
|
||||||
return -2;
|
return -2;
|
||||||
} else {
|
} 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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -890,6 +959,7 @@ int t_ask_for_more_peers(int sock_fd){
|
|||||||
}
|
}
|
||||||
|
|
||||||
int t_get_network_state(int sock_fd){
|
int t_get_network_state(int sock_fd){
|
||||||
|
print_debug(">> Getting network state...");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -898,7 +968,7 @@ int t_update_neighbours(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
int run_node(int sock_fd){
|
int run_node(int sock_fd){
|
||||||
printf(">> Running node...\n");
|
print_debug(">> Running node...");
|
||||||
|
|
||||||
int ret;
|
int ret;
|
||||||
ssize_t bytes;
|
ssize_t bytes;
|
||||||
@ -922,11 +992,8 @@ int run_node(int sock_fd){
|
|||||||
while (1) {
|
while (1) {
|
||||||
|
|
||||||
if(time(NULL) >= delay) {
|
if(time(NULL) >= delay) {
|
||||||
printf(">> Asking for more peers...\n");
|
|
||||||
t_ask_for_more_peers(sock_fd);
|
t_ask_for_more_peers(sock_fd);
|
||||||
printf(">> Updating neighbours...\n");
|
|
||||||
t_update_neighbours();
|
t_update_neighbours();
|
||||||
printf(">> Getting network state...\n");
|
|
||||||
t_get_network_state(sock_fd);
|
t_get_network_state(sock_fd);
|
||||||
delay = time(NULL) + 20 + (rand() % 10);
|
delay = time(NULL) + 20 + (rand() % 10);
|
||||||
}
|
}
|
||||||
@ -946,17 +1013,17 @@ int run_node(int sock_fd){
|
|||||||
ret = poll(fds, 2, 5);
|
ret = poll(fds, 2, 5);
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
printf(">> Error - poll returned error: %s\n", strerror(errno));
|
print_debug(">> Error - poll returned error");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
} else if (ret > 0) {
|
} else if (ret > 0) {
|
||||||
/* Regardless of requested events, poll() can always return these */
|
/* Regardless of requested events, poll() can always return these */
|
||||||
if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
|
if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
|
||||||
printf("Error - poll indicated stdin error\n");
|
print_debug("Error - poll indicated stdin error\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) {
|
if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) {
|
||||||
printf("Error - poll indicated socket error\n");
|
print_debug("Error - poll indicated socket error\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -964,14 +1031,16 @@ int run_node(int sock_fd){
|
|||||||
if (fds[0].revents & (POLLIN | POLLPRI)) {
|
if (fds[0].revents & (POLLIN | POLLPRI)) {
|
||||||
bytes = read(0, input_buffer, sizeof(input_buffer));
|
bytes = read(0, input_buffer, sizeof(input_buffer));
|
||||||
if (bytes < 0) {
|
if (bytes < 0) {
|
||||||
printf("Error - stdin error: %s\n", strerror(errno));
|
print_debug(">> Error - stdin error");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
input_buffer[strcspn(input_buffer, "\n")] = 0;
|
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.
|
// Add message to the message table.
|
||||||
if (add_message(input_buffer, bytes) < 0) {
|
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..");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -994,15 +1063,19 @@ int run_node(int sock_fd){
|
|||||||
|
|
||||||
bytes = recvmsg(sock_fd, &msg_from_peer, 0);
|
bytes = recvmsg(sock_fd, &msg_from_peer, 0);
|
||||||
if (bytes < 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;
|
break;
|
||||||
}
|
}
|
||||||
if (bytes > 0) {
|
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.
|
// Treat incoming packets.
|
||||||
int work_tlv_status = listen_for_packets(output_buffer, bytes, &sender, sock_fd);
|
int work_tlv_status = listen_for_packets(output_buffer, bytes, &sender, sock_fd);
|
||||||
if (work_tlv_status < 0) {
|
if (work_tlv_status < 0) {
|
||||||
perror(">> Error while treating the incoming packet.");
|
print_debug(">> Error while treating the incoming packet.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1018,14 +1091,14 @@ int run_node(int sock_fd){
|
|||||||
|
|
||||||
// This function runs once, and sets the sock_fd as well as the neighbourhood
|
// This function runs once, and sets the sock_fd as well as the neighbourhood
|
||||||
int bootstrap_node(int * sock_fd){
|
int bootstrap_node(int * sock_fd){
|
||||||
printf(">> Boostraping node...\n");
|
print_debug(">> Boostraping node...");
|
||||||
|
|
||||||
struct sockaddr_in6 server_addr;
|
struct sockaddr_in6 server_addr;
|
||||||
|
|
||||||
/* Create UDP socket */
|
/* Create UDP socket */
|
||||||
* sock_fd = socket(AF_INET6, SOCK_DGRAM, 0);
|
* sock_fd = socket(AF_INET6, SOCK_DGRAM, 0);
|
||||||
if ( * sock_fd < 0) {
|
if ( * sock_fd < 0) {
|
||||||
printf("Error - failed to open socket: %s\n", strerror(errno));
|
print_debug(">> Error - failed to open socket");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1035,7 +1108,7 @@ int bootstrap_node(int * sock_fd){
|
|||||||
// server_addr.sin6_addr.in6_addr = htonl(INADDR_ANY);
|
// server_addr.sin6_addr.in6_addr = htonl(INADDR_ANY);
|
||||||
server_addr.sin6_port = htons(LISTEN_PORT);
|
server_addr.sin6_port = htons(LISTEN_PORT);
|
||||||
if (bind( * sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0) {
|
if (bind( * sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0) {
|
||||||
printf("Error - failed to bind socket: %s\n", strerror(errno));
|
print_debug(">> Error - failed to bind socket");
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1054,18 +1127,18 @@ int bootstrap_node(int * sock_fd){
|
|||||||
root_peer->last_seen = root_peer_seen;
|
root_peer->last_seen = root_peer_seen;
|
||||||
|
|
||||||
// TODO: Add the first peer to the neighbourhood
|
// 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 = malloc(sizeof(struct list));
|
||||||
neighbour_list->data = (void *) root_peer;
|
neighbour_list->data = (void *) root_peer;
|
||||||
neighbour_list->next = NULL;
|
neighbour_list->next = NULL;
|
||||||
|
|
||||||
printf(">> Boostraping done.\n");
|
print_debug(">> Boostraping done.");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, const char *argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
printf(">> Starting node\n");
|
print_debug(">> Starting node");
|
||||||
|
|
||||||
int sock_fd;
|
int sock_fd;
|
||||||
bootstrap_node(&sock_fd);
|
bootstrap_node(&sock_fd);
|
||||||
|
Loading…
Reference in New Issue
Block a user