|
|
|
@ -29,15 +29,6 @@ int ask_for_peers(int socket_num) {
|
|
|
|
|
struct in6_addr ip = peer->ip;
|
|
|
|
|
int16_t port = peer->port;
|
|
|
|
|
|
|
|
|
|
/*int ifindex = if_nametoindex("enp3s0");
|
|
|
|
|
if(ifindex == 0) {
|
|
|
|
|
int ifindex = if_nametoindex("eth0");
|
|
|
|
|
if(ifindex == 0) {
|
|
|
|
|
perror("if_nametoindex failed");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
int ifindex = 0;
|
|
|
|
|
|
|
|
|
|
// Initialize sockaddr
|
|
|
|
@ -130,7 +121,6 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, uint16_t port) {
|
|
|
|
|
// Return 0 if peer was updated as last_seen
|
|
|
|
|
int add_n_update_neighbour(struct in6_addr *ip, uint16_t port) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// We try to find a peer with this address and port.
|
|
|
|
|
neighbour_peer *peer = get_neighbour(ip, port);
|
|
|
|
|
time_t curtime;
|
|
|
|
@ -218,15 +208,15 @@ pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data)
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new_data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A node state TLV was received and either no data associated to it's id is in our data list or the data was updated, return -1 if an error occurend, 0 if nothing had to be done and 1 if something was updated/added
|
|
|
|
|
// A node state TLV was received and either no data associated to it's id is in
|
|
|
|
|
// our data list or the data was updated, return -1 if an error occurend,
|
|
|
|
|
// 0 if nothing had to be done and 1 if something was updated/added
|
|
|
|
|
int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found) {
|
|
|
|
|
// Check if it's our own id
|
|
|
|
|
if(id == NODE_ID) {
|
|
|
|
|
// wtf
|
|
|
|
|
if(found == NULL) {
|
|
|
|
|
printf("\x1b[31m[DEBUG]\x1b[0m >> Our own node is not in the data list, something went terribly wrong.\n");
|
|
|
|
|
return -1;
|
|
|
|
@ -244,7 +234,8 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If it's not our own id, update the data if it's already in our data list and seqno is bigger than our stored seqno
|
|
|
|
|
// If it's not our own id, update the data if it's already in our data
|
|
|
|
|
// list and seqno is bigger than our stored seqno
|
|
|
|
|
if(found != NULL) {
|
|
|
|
|
// Check if seqno is smaller or equals to our stored seqno
|
|
|
|
|
if( ((found->seqno - seqno) & 32768) == 0 ) {
|
|
|
|
@ -257,13 +248,13 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat
|
|
|
|
|
found->id = id;
|
|
|
|
|
found->seqno = seqno;
|
|
|
|
|
|
|
|
|
|
// Updata message
|
|
|
|
|
// Update message
|
|
|
|
|
free(found->data);
|
|
|
|
|
found->data = (char*) malloc(len);
|
|
|
|
|
memcpy(found->data, data, len);
|
|
|
|
|
|
|
|
|
|
if (DEBUG_LEVEL > 0) {
|
|
|
|
|
printf("\x1b[31m[DEBUG]\x1b[0m >> Updated %li's published data.\n", id);
|
|
|
|
|
printf("\x1b[31m[DEBUG]\x1b[0m >> Updated %lu 's published data.\n", id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
@ -335,6 +326,7 @@ 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);
|
|
|
|
@ -342,7 +334,8 @@ int update_neighbours() {
|
|
|
|
|
}
|
|
|
|
|
// 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
|
|
|
|
|
// If it's been 70 seconds or more since we last received a packet
|
|
|
|
|
// from this peer then remove it from the list
|
|
|
|
|
time(&curtime);
|
|
|
|
|
|
|
|
|
|
if(difftime(peer->last_seen, curtime) >= 70) {
|
|
|
|
@ -394,7 +387,8 @@ int update_neighbours() {
|
|
|
|
|
return deleted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
|
print_debug(">> Adding tlv to packet");
|
|
|
|
|
char type = tlv->pad1->type, sent = 0, errval = 0;
|
|
|
|
@ -536,17 +530,16 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i
|
|
|
|
|
packet_tlv_send_out.msg_name = dest;
|
|
|
|
|
packet_tlv_send_out.msg_namelen = sizeof(struct sockaddr_in6);
|
|
|
|
|
packet_tlv_send_out.msg_iov = vec_buff;
|
|
|
|
|
packet_tlv_send_out.msg_iovlen = 1; // We have only one iovec buffer. But if we had 2, we would write 2.
|
|
|
|
|
packet_tlv_send_out.msg_iovlen = 1;
|
|
|
|
|
|
|
|
|
|
int response_code = sendmsg(socket_num, &packet_tlv_send_out, 0);
|
|
|
|
|
|
|
|
|
|
if (response_code < 0) {
|
|
|
|
|
print_error("Unable to send out the packet to peer.");
|
|
|
|
|
error_while_sending = 1;
|
|
|
|
|
} else if (response_code < length) {
|
|
|
|
|
print_debug(">> Sent out only part of the packet.");
|
|
|
|
|
error_while_sending = 1;
|
|
|
|
|
} else {
|
|
|
|
|
print_debug(">> Send out packet to peer.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error_while_sending == 1) {
|
|
|
|
@ -559,8 +552,10 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send a single TLV to the specified addresses, return -1 if an error was encountered, 0 otherwise
|
|
|
|
|
// Send a single TLV to the specified addresses, return -1 if an error
|
|
|
|
|
// was encountered, 0 otherwise
|
|
|
|
|
int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
|
|
|
|
|
|
|
|
|
|
char type = tlv->pad1->type;
|
|
|
|
|
packet pack = (packet) {.magic = 95, .version = 1, .length = 0};
|
|
|
|
|
memset(pack.body, 0, 1020);
|
|
|
|
@ -647,71 +642,6 @@ int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
|
|
|
|
|
return send_packet((char*) &pack, pack.length, dest, socket_num);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO This function can be deleted.
|
|
|
|
|
// int send_tlv(tlv *tlv_to_send, uint16_t tlv_size, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num){
|
|
|
|
|
// print_debug(">> Building packet to send a TLV.");
|
|
|
|
|
//
|
|
|
|
|
// // We first need to build the packet,
|
|
|
|
|
// char packet_buff[1024];
|
|
|
|
|
// struct packet pack;
|
|
|
|
|
// pack.magic = 95;
|
|
|
|
|
// pack.version = 1;
|
|
|
|
|
// if (tlv_size > 1020) {
|
|
|
|
|
// print_debug(">> Unable to send the tlv, it's size is above 1020 bytes.");
|
|
|
|
|
// return -1;
|
|
|
|
|
// } else {
|
|
|
|
|
// memcpy((void *) pack.body, tlv_to_send, tlv_size);
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// // Move the content of the paquet struct to a buffer
|
|
|
|
|
// // That will be send out in a vectorized buffer.
|
|
|
|
|
// memcpy(&packet_buff,&pack,1024);
|
|
|
|
|
//
|
|
|
|
|
// 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 };
|
|
|
|
|
//
|
|
|
|
|
// int error_while_sending = 0;
|
|
|
|
|
//
|
|
|
|
|
// // For every dest
|
|
|
|
|
// for (size_t i = 0; i < dest_list_size; i++) {
|
|
|
|
|
// // Creating the struct to send out with sendmsg
|
|
|
|
|
// struct msghdr packet_tlv_send_out = {
|
|
|
|
|
// .msg_name = &dest_list[i],
|
|
|
|
|
// .msg_namelen = sizeof(dest_list[i]),
|
|
|
|
|
// .msg_iov = &vec_buff,
|
|
|
|
|
// .msg_iovlen = 1 // We have only one iovec buffer. But if we had 2, we would write 2.
|
|
|
|
|
// };
|
|
|
|
|
//
|
|
|
|
|
// int response_code = sendmsg((int) socket_num, &packet_tlv_send_out, 0);
|
|
|
|
|
// if (response_code < 0) {
|
|
|
|
|
// 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)) {
|
|
|
|
|
// print_debug(">> Sent out only part of the packet.");
|
|
|
|
|
// error_while_sending = 1;
|
|
|
|
|
// continue;
|
|
|
|
|
// } else {
|
|
|
|
|
// if (DEBUG_LEVEL > 0) {
|
|
|
|
|
// printf("\x1b[31m[DEBUG]\x1b[0m >> Sent out packet to peer %i", i);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// if (error_while_sending == 1) {
|
|
|
|
|
// print_debug(">> Error occured while sending out a packet.");
|
|
|
|
|
// return -1;
|
|
|
|
|
// } else {
|
|
|
|
|
// return 0;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// We need to make sure the TLV announces a length that will no go onto
|
|
|
|
|
// another tlv, as we might end up reading bullshit.
|
|
|
|
|
int validate_tlv(char *data, int pos, uint16_t packet_len){
|
|
|
|
@ -739,89 +669,50 @@ int validate_tlv(char *data, int pos, uint16_t packet_len){
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
case 1:
|
|
|
|
|
print_debug(">> Received a TLV padding");
|
|
|
|
|
return 1;
|
|
|
|
|
case 2:
|
|
|
|
|
if(tlv_len != LEN_NEIGHBOUR_REQ) return -1;
|
|
|
|
|
print_debug(">> Received a TLV Neighbour Request");
|
|
|
|
|
return 2;
|
|
|
|
|
case 3:
|
|
|
|
|
if(tlv_len != LEN_NEIGHBOUR) return -1;
|
|
|
|
|
print_debug(">> Received a TLV Neighbour");
|
|
|
|
|
return 3;
|
|
|
|
|
case 4:
|
|
|
|
|
if(tlv_len != LEN_NETWORK_HASH) return -1;
|
|
|
|
|
print_debug(">> Received a TLV Network hash");
|
|
|
|
|
return 4;
|
|
|
|
|
case 5:
|
|
|
|
|
if(tlv_len != LEN_NETWORK_STATE_REQ) return -1;
|
|
|
|
|
print_debug(">> Received a TLV Network state request");
|
|
|
|
|
return 5;
|
|
|
|
|
case 6:
|
|
|
|
|
if(tlv_len != LEN_NODE_HASH) return -1;
|
|
|
|
|
print_debug(">> Received a TLV Node hash");
|
|
|
|
|
return 6;
|
|
|
|
|
case 7:
|
|
|
|
|
if(tlv_len != LEN_NODE_STATE_REQ) return -1;
|
|
|
|
|
print_debug(">> Received a TLV Node state request");
|
|
|
|
|
return 7;
|
|
|
|
|
case 8:
|
|
|
|
|
if(tlv_len < MIN_LEN_NODE_STATE || tlv_len > MAX_LEN_NODE_STATE) return -1;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
// Check if it has the right hash
|
|
|
|
|
uint16_t *seqno = (uint16_t*) (data + pos + 10);
|
|
|
|
|
uint64_t *id = (uint64_t*) (data + pos + 2);
|
|
|
|
|
|
|
|
|
|
pub_data pdata = (pub_data) {
|
|
|
|
|
.length = tlv_len,
|
|
|
|
|
.id = *id,
|
|
|
|
|
.seqno = *seqno,
|
|
|
|
|
.data = (unsigned char*) malloc(tlv_len)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
unsigned char *cur_hash = (unsigned char*) (data + pos + 12);
|
|
|
|
|
char *cur_message = data + pos + 28;
|
|
|
|
|
|
|
|
|
|
unsigned char hash[16];
|
|
|
|
|
|
|
|
|
|
memcpy(pdata.data, cur_message, tlv_len);
|
|
|
|
|
|
|
|
|
|
hash_data(&pdata, hash);
|
|
|
|
|
|
|
|
|
|
if(memcmp(hash, cur_hash, 16) != 0) {
|
|
|
|
|
print_debug(">> Malformed hash.");
|
|
|
|
|
printf("\x1b[31m[DEBUG]\x1b[0m >> Received : ");
|
|
|
|
|
|
|
|
|
|
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 < 16; x++){
|
|
|
|
|
printf("%02x", cur_hash[x]);
|
|
|
|
|
fflush(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
print_debug(">> Received a TLV Node state");
|
|
|
|
|
return 8;
|
|
|
|
|
case 9:
|
|
|
|
|
print_error(">> Received a TLV Warning");
|
|
|
|
|
return 9;
|
|
|
|
|
default:
|
|
|
|
|
print_error("Could not tell which TLV we received");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For every packet recivied,
|
|
|
|
|
// then we make sure it's conform
|
|
|
|
|
// For every packet recivied, we make sure it's conform
|
|
|
|
|
// We then extract the data from it to make it easy to work with
|
|
|
|
|
int check_header(char * received_data_buffer, int received_data_len, struct packet * packet_to_return){
|
|
|
|
|
|
|
|
|
@ -843,7 +734,8 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack
|
|
|
|
|
// Convert to hardware order.
|
|
|
|
|
((packet*) packet_to_return)->length = be16toh(((packet*) packet_to_return)->length);
|
|
|
|
|
if (packet_to_return->length + 4 < received_data_len ) {
|
|
|
|
|
print_debug(">> 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -871,7 +763,8 @@ int add_message(char * message, int message_len){
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print_debug(">> Message could not be added because our own ID is not in the data_list, something went wrong.");
|
|
|
|
|
print_error(">> Message could not be added because our own ID is not in the data_list, \
|
|
|
|
|
something went wrong.");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -880,7 +773,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|
|
|
|
uint16_t packet_len = ((packet*) data)->length;
|
|
|
|
|
|
|
|
|
|
if(packet_len != total_packet_len - 4) {
|
|
|
|
|
print_debug(">> Length indicated in packet differs from real length of packet received, disgarding packet.");
|
|
|
|
|
print_debug(">> Length indicated in packet differs from real length of packet received,\
|
|
|
|
|
disgarding packet.");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
int nbr_of_tlvs = 0;
|
|
|
|
@ -898,7 +792,6 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|
|
|
|
|
|
|
|
|
// holds the random neighbour we send back in case of a neighbour_req.
|
|
|
|
|
struct neighbour_peer *random_neighbour;
|
|
|
|
|
// memset(random_neighbour, 0, sizeof(struct neighbour_peer));
|
|
|
|
|
|
|
|
|
|
// Holds the new neighbour send to us by a "neighbour tlv"
|
|
|
|
|
struct sockaddr_in6 new_neighbour;
|
|
|
|
@ -917,10 +810,6 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|
|
|
|
|
|
|
|
|
while(pos < total_packet_len) {
|
|
|
|
|
// Making sure the current TLV we are looking at is valid.
|
|
|
|
|
// TODO : I think we should reset all the structs here.
|
|
|
|
|
// memset(random_neighbour, 0, sizeof(struct neighbour_peer));
|
|
|
|
|
// memset(pdata, 0, sizeof(struct pub_data));
|
|
|
|
|
// memset(tmp_list, 0, sizeof(struct list));
|
|
|
|
|
memset(hash, 0, 16);
|
|
|
|
|
memset(warn, 0, 32);
|
|
|
|
|
tlv_len = 0;
|
|
|
|
@ -951,10 +840,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO : Seems to be a bug here, as this frees the new_tlv
|
|
|
|
|
build_neighbour(&new_tlv, random_neighbour->ip, random_neighbour->port);
|
|
|
|
|
// TODO : I suppose that since new_tlv is freed above, add_tlv ads an empty
|
|
|
|
|
// value to the packet ?
|
|
|
|
|
add_tlv(&pack, &new_tlv, sender, socket_num);
|
|
|
|
|
|
|
|
|
|
// The position is updated
|
|
|
|
@ -1039,8 +925,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|
|
|
|
tmp_list = tmp_list->next;
|
|
|
|
|
|
|
|
|
|
if (DEBUG_LEVEL > 1) {
|
|
|
|
|
// TODO wtf is this ?
|
|
|
|
|
printf(" >> \x1b[31m[DEBUG]\x1b[0m %lu\n", *(uint64_t*) (pack.body + 2));
|
|
|
|
|
printf(" >> \x1b[31m[DEBUG]\x1b[0m Sent back hash of the message we have for %lu\n", *(uint64_t*) (pack.body + 2));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
@ -1085,7 +970,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|
|
|
|
add_tlv(&pack, &new_tlv, sender, socket_num);
|
|
|
|
|
|
|
|
|
|
if (DEBUG_LEVEL > 0) {
|
|
|
|
|
printf("\x1b[31m[DEBUG]\x1b[0m >> Sending node state request for node %lu...\n", be64toh(*id));
|
|
|
|
|
printf("\x1b[31m[DEBUG]\x1b[0m >> Sending node state request for node %lu \n", be64toh(*id));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The position is updated
|
|
|
|
@ -1117,8 +1002,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
case 8:
|
|
|
|
|
// We received a node state tlv so
|
|
|
|
|
// we add it to the data list
|
|
|
|
|
// 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...");
|
|
|
|
|
|
|
|
|
@ -1129,21 +1013,6 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|
|
|
|
cur_message = data + pos + 28;
|
|
|
|
|
|
|
|
|
|
print_debug(">> Received message ! ");
|
|
|
|
|
// printf("Type : %d\n", data[pos] );
|
|
|
|
|
// printf("Length : %d\n", tlv_len );
|
|
|
|
|
// printf("Node ID : %lu\n", be64toh(*id) );
|
|
|
|
|
// printf("Seqno : %hu\n", be16toh(*seqno) );
|
|
|
|
|
// printf("Hash :");
|
|
|
|
|
// for(int x = 0; x < 16; x++){
|
|
|
|
|
// printf("%02x", cur_hash[x]);
|
|
|
|
|
// fflush(0);
|
|
|
|
|
// }
|
|
|
|
|
// printf("\n");
|
|
|
|
|
// for(int x = 0; x < tlv_len - 26; x++){
|
|
|
|
|
// printf("%c", cur_message[x]);
|
|
|
|
|
// fflush(0);
|
|
|
|
|
// }
|
|
|
|
|
// printf("\n");
|
|
|
|
|
|
|
|
|
|
// Check if it has the right hash
|
|
|
|
|
pub_data pdata_check = (pub_data) {
|
|
|
|
@ -1157,23 +1026,6 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|
|
|
|
|
|
|
|
|
hash_data(&pdata_check, hash2);
|
|
|
|
|
|
|
|
|
|
// print_debug(">> Built message: ");
|
|
|
|
|
// printf("Type : %d\n", data[pos] );
|
|
|
|
|
// printf("Length : %d\n", pdata_check.length + 26);
|
|
|
|
|
// printf("Node ID : %lu\n", pdata_check.id);
|
|
|
|
|
// printf("Seqno : %hu\n", pdata_check.seqno);
|
|
|
|
|
// printf("Hash :");
|
|
|
|
|
// for(int x = 0; x < 16; x++){
|
|
|
|
|
// printf("%02x", hash2[x]);
|
|
|
|
|
// fflush(0);
|
|
|
|
|
// }
|
|
|
|
|
// printf("\n");
|
|
|
|
|
// for(int x = 0; x < tlv_len - 26; x++){
|
|
|
|
|
// printf("%c", pdata_check.data[x]);
|
|
|
|
|
// fflush(0);
|
|
|
|
|
// }
|
|
|
|
|
// printf("\n");
|
|
|
|
|
|
|
|
|
|
if(memcmp(hash2, cur_hash, 16) != 0) {
|
|
|
|
|
print_debug(">> Malformed hash.");
|
|
|
|
|
printf("\x1b[31m[DEBUG]\x1b[0m >> Calculated : ");
|
|
|
|
@ -1263,7 +1115,6 @@ 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;
|
|
|
|
|
}
|
|
|
|
@ -1307,11 +1158,12 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int t_ask_for_more_peers(int sock_fd){
|
|
|
|
|
if (DEBUG_LEVEL > 1) {
|
|
|
|
|
print_peers(neighbour_list);
|
|
|
|
|
sleep(3);
|
|
|
|
|
if (ask_for_peers(sock_fd) < 0 ){
|
|
|
|
|
print_error("Error while asking for more peers !");
|
|
|
|
|
return -1;
|
|
|
|
|
} else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return ask_for_peers(sock_fd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* For every peer we know about, we send out a TLV network hash
|
|
|
|
@ -1362,7 +1214,15 @@ int t_get_network_state(int sock_fd){
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int t_update_neighbours(){
|
|
|
|
|
return update_neighbours();
|
|
|
|
|
int nbr_deleted = update_neighbours();
|
|
|
|
|
if (nbr_deleted < 0) {
|
|
|
|
|
print_error("Error while deleting peers for the peer list.");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (DEBUG_LEVEL > 0) {
|
|
|
|
|
printf("\x1b[31m[DEBUG]\x1b[0m >> We deleted %i peers for the peer list.\n", nbr_deleted);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int run_node(int sock_fd){
|
|
|
|
@ -1376,7 +1236,7 @@ int run_node(int sock_fd){
|
|
|
|
|
|
|
|
|
|
// Init the ~20s delay for node update.
|
|
|
|
|
srand(time(NULL));
|
|
|
|
|
time_t delay = time(NULL) + 5;
|
|
|
|
|
time_t delay = time(NULL) + 1;
|
|
|
|
|
|
|
|
|
|
/* Descriptor zero is stdin */
|
|
|
|
|
fds[0].fd = 0;
|
|
|
|
@ -1398,16 +1258,6 @@ int run_node(int sock_fd){
|
|
|
|
|
delay = time(NULL) + 20 + (rand() % 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This might be cool to add, but we need to find a way to write to stdin
|
|
|
|
|
// while it's running.
|
|
|
|
|
// if (time(NULL) < delay) {
|
|
|
|
|
// // Thanks to :
|
|
|
|
|
// // https://gist.github.com/amullins83/24b5ef48657c08c4005a8fab837b7499
|
|
|
|
|
// printf("\b\x1b[2K\r>> Next request in %li seconds..", delay - time(NULL));
|
|
|
|
|
// fflush(stdout);
|
|
|
|
|
// }
|
|
|
|
|
// printf("\n");
|
|
|
|
|
|
|
|
|
|
/* Call poll() */
|
|
|
|
|
ret = poll(fds, 2, 10);
|
|
|
|
|
|
|
|
|
@ -1458,7 +1308,6 @@ int run_node(int sock_fd){
|
|
|
|
|
// Read data from the socket ( incoming packet )
|
|
|
|
|
if (fds[1].revents & (POLLIN | POLLPRI)) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Vectorized buffer
|
|
|
|
|
struct iovec vec_buff_rec = { .iov_len = sizeof(output_buffer), .iov_base = output_buffer };
|
|
|
|
|
|
|
|
|
@ -1469,7 +1318,7 @@ int run_node(int sock_fd){
|
|
|
|
|
.msg_name = &sender,
|
|
|
|
|
.msg_namelen = sizeof(sender),
|
|
|
|
|
.msg_iov = &vec_buff_rec,
|
|
|
|
|
.msg_iovlen = 1 // We have only one iovec buffer. But if we had 2, we would write 2.
|
|
|
|
|
.msg_iovlen = 1
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
bytes = recvmsg(sock_fd, &msg_from_peer, 0);
|
|
|
|
@ -1495,9 +1344,7 @@ int run_node(int sock_fd){
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// TODO : Here, we can write all of the current messages we have in stack
|
|
|
|
|
// to a file, or to stdout.
|
|
|
|
|
// TODO : Same as above, but for the peers have know about.
|
|
|
|
|
// Todo : write HTML output of our message list ?
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -1524,7 +1371,7 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){
|
|
|
|
|
memset(&server_addr, 0, sizeof(server_addr));
|
|
|
|
|
server_addr.sin6_family = AF_INET6;
|
|
|
|
|
// server_addr.sin6_addr = INADDR_ANY;
|
|
|
|
|
server_addr.sin6_port = LISTEN_PORT;
|
|
|
|
|
server_addr.sin6_port = be16toh(LISTEN_PORT);
|
|
|
|
|
if (bind( * sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0) {
|
|
|
|
|
print_error("Failed to bind socket");
|
|
|
|
|
perror("");
|
|
|
|
|