Trying to fix the receiving of messages
This commit is contained in:
parent
201084d9f1
commit
bd8f5c452b
@ -55,7 +55,7 @@ void print_data(list * l){
|
|||||||
print_debug(">> Printing messages we know of so far :");
|
print_debug(">> Printing messages we know of so far :");
|
||||||
int nbr_msg = 0;
|
int nbr_msg = 0;
|
||||||
setlocale(LC_ALL, "en_US.utf8");
|
setlocale(LC_ALL, "en_US.utf8");
|
||||||
print_debug(">> 🏂🏂🏂🏂 Peer ID | Seqno | Length | Message ");
|
print_debug(">> Peer ID | Seqno | Length | Message ");
|
||||||
while(l != NULL){
|
while(l != NULL){
|
||||||
pub_data * message = l->data;
|
pub_data * message = l->data;
|
||||||
printf("\x1b[31m[DEBUG]\x1b[0m >> %li | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data);
|
printf("\x1b[31m[DEBUG]\x1b[0m >> %li | %i | %i | “%s” \n", message->id, message->seqno, message->length, message->data);
|
||||||
|
271
src/node.c
271
src/node.c
@ -192,9 +192,9 @@ pub_data *get_data(uint64_t id) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Take data as args and create a pub_data structure in the heap
|
// Take data as args and create a pub_data structure in the heap
|
||||||
pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data) {
|
pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data) {
|
||||||
pub_data *new_data = (pub_data*) malloc(sizeof(pub_data));
|
pub_data *new_data = (pub_data*) malloc(sizeof(pub_data));
|
||||||
char *_data = (char*) malloc(len);
|
unsigned char *_data = (unsigned char*) malloc(len);
|
||||||
if (_data == NULL) {
|
if (_data == NULL) {
|
||||||
print_error("Failed to allocate memory for copying the data !");
|
print_error("Failed to allocate memory for copying the data !");
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -213,7 +213,7 @@ pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *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) {
|
int add_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data, pub_data *found) {
|
||||||
// Check if it's our own id
|
// Check if it's our own id
|
||||||
if(id == NODE_ID) {
|
if(id == NODE_ID) {
|
||||||
// wtf
|
// wtf
|
||||||
@ -249,7 +249,7 @@ int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_dat
|
|||||||
|
|
||||||
// Updata message
|
// Updata message
|
||||||
free(found->data);
|
free(found->data);
|
||||||
found->data = (char*) malloc(len);
|
found->data = (unsigned char*) malloc(len);
|
||||||
memcpy(found->data, data, len);
|
memcpy(found->data, data, len);
|
||||||
|
|
||||||
printf(">> Updated %li's published data.\n", id);
|
printf(">> Updated %li's published data.\n", id);
|
||||||
@ -492,7 +492,7 @@ int send_packet(char *packet_buff, uint16_t length, struct sockaddr_in6 *dest, i
|
|||||||
|
|
||||||
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) {
|
||||||
print_debug(">> Unable to send out the packet to peer.");
|
print_error("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) {
|
||||||
print_debug(">> Sent out only part of the packet.");
|
print_debug(">> Sent out only part of the packet.");
|
||||||
@ -578,70 +578,70 @@ int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num) {
|
|||||||
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, uint16_t tlv_size, struct sockaddr_in6 * dest_list, int dest_list_size, int socket_num){
|
// TODO This function can be deleted.
|
||||||
print_debug(">> Building packet to send a TLV.");
|
// 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];
|
// // We first need to build the packet,
|
||||||
struct packet pack;
|
// char packet_buff[1024];
|
||||||
pack.magic = 95;
|
// struct packet pack;
|
||||||
pack.version = 1;
|
// pack.magic = 95;
|
||||||
if (tlv_size > 1020) {
|
// pack.version = 1;
|
||||||
print_debug(">> Unable to send the tlv, it's size if above 1020 bytes.");
|
// if (tlv_size > 1020) {
|
||||||
return -1;
|
// print_debug(">> Unable to send the tlv, it's size is above 1020 bytes.");
|
||||||
} else {
|
// return -1;
|
||||||
memcpy((void *) pack.body, tlv_to_send, tlv_size);
|
// } 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.
|
// // Move the content of the paquet struct to a buffer
|
||||||
// packet_buff = (char *) pack;
|
// // That will be send out in a vectorized buffer.
|
||||||
memcpy(&packet_buff,&pack,1024);
|
// memcpy(&packet_buff,&pack,1024);
|
||||||
|
//
|
||||||
if (DEBUG_LEVEL > 1) {
|
// if (DEBUG_LEVEL > 1) {
|
||||||
print_debug(">> Packet has been built.");
|
// 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 };
|
||||||
|
//
|
||||||
int error_while_sending = 0;
|
// int error_while_sending = 0;
|
||||||
|
//
|
||||||
// For every dest
|
// // For every dest
|
||||||
for (size_t i = 0; i < dest_list_size; i++) {
|
// for (size_t i = 0; i < dest_list_size; i++) {
|
||||||
// Creating the struct to send out with sendmsg
|
// // Creating the struct to send out with sendmsg
|
||||||
struct msghdr packet_tlv_send_out = {
|
// struct msghdr packet_tlv_send_out = {
|
||||||
.msg_name = &dest_list[i],
|
// .msg_name = &dest_list[i],
|
||||||
.msg_namelen = sizeof(dest_list[i]),
|
// .msg_namelen = sizeof(dest_list[i]),
|
||||||
.msg_iov = &vec_buff,
|
// .msg_iov = &vec_buff,
|
||||||
.msg_iovlen = 1 // We have only one iovec buffer. But if we had 2, we would write 2.
|
// .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);
|
// int response_code = sendmsg((int) socket_num, &packet_tlv_send_out, 0);
|
||||||
if (response_code < 0) {
|
// if (response_code < 0) {
|
||||||
if (DEBUG_LEVEL > 0) {
|
// if (DEBUG_LEVEL > 0) {
|
||||||
printf("\x1b[31m[DEBUG]\x1b[0m >> Unable to send out the packet to peer %li", i);
|
// 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)) {
|
||||||
print_debug(">> 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 {
|
||||||
if (DEBUG_LEVEL > 0) {
|
// if (DEBUG_LEVEL > 0) {
|
||||||
printf("\x1b[31m[DEBUG]\x1b[0m >> Send out packet to peer %li", i);
|
// printf("\x1b[31m[DEBUG]\x1b[0m >> Sent out packet to peer %i", i);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
if (error_while_sending == 1) {
|
// if (error_while_sending == 1) {
|
||||||
print_debug(">> 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;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
// We need to make sure the TLV announces a length that will no go onto
|
// We need to make sure the TLV announces a length that will no go onto
|
||||||
// another tlv, as we might end up reading bullshit.
|
// another tlv, as we might end up reading bullshit.
|
||||||
@ -666,7 +666,7 @@ int validate_tlv(char *data, int pos, uint16_t packet_len){
|
|||||||
|
|
||||||
// Check that the tlv does not exceed the packet length
|
// Check that the tlv does not exceed the packet length
|
||||||
if(pos + tlv_len > packet_len){
|
if(pos + tlv_len > packet_len){
|
||||||
print_debug(">> The TLV Length exceed the packet length\n");
|
print_debug(">> The TLV Length exceeds the packet length\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -738,8 +738,9 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack
|
|||||||
|
|
||||||
int add_message(char * message, int message_len){
|
int add_message(char * message, int message_len){
|
||||||
// Don't update the message if it's empty
|
// Don't update the message if it's empty
|
||||||
if(message_len == 0)
|
if(message_len == 0){
|
||||||
return -1;
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
// If not, get our data in the list and update it
|
// If not, get our data in the list and update it
|
||||||
pub_data *our_data = get_data(NODE_ID);
|
pub_data *our_data = get_data(NODE_ID);
|
||||||
@ -748,8 +749,8 @@ int add_message(char * message, int message_len){
|
|||||||
our_data->seqno = (our_data->seqno + 1) % 65535;
|
our_data->seqno = (our_data->seqno + 1) % 65535;
|
||||||
our_data->length = message_len;
|
our_data->length = message_len;
|
||||||
free(our_data->data);
|
free(our_data->data);
|
||||||
our_data->data = (char*) malloc(message_len);
|
our_data->data = (unsigned char*) malloc(message_len);
|
||||||
memcpy(our_data->data, message, message_len);
|
memcpy(our_data->data, (unsigned char *) message, message_len);
|
||||||
|
|
||||||
print_debug(">> Message added.");
|
print_debug(">> Message added.");
|
||||||
|
|
||||||
@ -768,6 +769,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
int nbr_of_tlvs = 0;
|
||||||
|
|
||||||
int pos = 4;
|
int pos = 4;
|
||||||
unsigned char tlv_len, hash[16];
|
unsigned char tlv_len, hash[16];
|
||||||
@ -780,11 +782,13 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
|
|
||||||
new_tlv.pad1 = NULL;
|
new_tlv.pad1 = NULL;
|
||||||
cur_tlv.pad1 = NULL;
|
cur_tlv.pad1 = NULL;
|
||||||
list *tmp_list;
|
list * tmp_list;
|
||||||
pub_data *pdata;
|
pub_data * pdata;
|
||||||
|
|
||||||
// holds the random neighbour we send back in case of a neighbour_req.
|
// holds the random neighbour we send back in case of a neighbour_req.
|
||||||
struct neighbour_peer *random_neighbour;
|
struct neighbour_peer *random_neighbour;
|
||||||
|
// memset(random_neighbour, 0, sizeof(struct neighbour_peer));
|
||||||
|
|
||||||
// Holds the new neighbour send to us by a "neighbour tlv"
|
// Holds the new neighbour send to us by a "neighbour tlv"
|
||||||
struct sockaddr_in6 new_neighbour;
|
struct sockaddr_in6 new_neighbour;
|
||||||
|
|
||||||
@ -792,32 +796,31 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
packet pack = (packet) {.magic = 95, .version = 1, .length = 0};
|
packet pack = (packet) {.magic = 95, .version = 1, .length = 0};
|
||||||
memset(pack.body, 0, 1020);
|
memset(pack.body, 0, 1020);
|
||||||
|
|
||||||
/*
|
|
||||||
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;
|
int ifindex = 0;
|
||||||
|
|
||||||
while(pos < total_packet_len) {
|
while(pos < total_packet_len) {
|
||||||
// Making sure the current TLV we are looking at is valid.
|
// 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;
|
||||||
|
|
||||||
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...");
|
print_debug(">> Received padding tlv, ignoring...");
|
||||||
pos += 1;
|
pos += 1;
|
||||||
|
nbr_of_tlvs++;
|
||||||
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...");
|
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;
|
||||||
|
nbr_of_tlvs++;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
@ -840,6 +843,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
// 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;
|
||||||
|
nbr_of_tlvs++;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
@ -862,6 +866,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
// 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;
|
||||||
|
nbr_of_tlvs++;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
@ -898,6 +903,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
// 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;
|
||||||
|
nbr_of_tlvs++;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
@ -915,7 +921,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
}
|
}
|
||||||
|
|
||||||
// The position is updated
|
// The position is updated
|
||||||
pos += 2;
|
tlv_len = data[pos+1];
|
||||||
|
pos += tlv_len + 2;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 6:
|
||||||
@ -936,13 +943,14 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
if(memcmp(hash, cur_tlv.node_hash->node_hash, 16) == 0) {
|
if(memcmp(hash, cur_tlv.node_hash->node_hash, 16) == 0) {
|
||||||
// The position is updated
|
// The position is updated
|
||||||
tlv_len = data[pos+1];
|
tlv_len = data[pos+1];
|
||||||
pos += 2;
|
pos += tlv_len + 2;
|
||||||
|
print_debug(">> Both hashs are the same, nothing to do.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
print_debug(">> No hash found, or hashs are differents, sending back node state request.");
|
||||||
// If no pub_data was found or the hashes differ then we send a node state request
|
// If no pub_data was found or the hashes differ then we send a node state request
|
||||||
build_node_state_req(&new_tlv, be64toh(cur_tlv.node_hash->node_id));
|
build_node_state_req(&new_tlv, be64toh(cur_tlv.node_hash->node_id));
|
||||||
add_tlv(&pack, &new_tlv, sender, socket_num);
|
add_tlv(&pack, &new_tlv, sender, socket_num);
|
||||||
@ -964,6 +972,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
if(pdata != NULL) {
|
if(pdata != NULL) {
|
||||||
build_node_state(&new_tlv, pdata->id, pdata->seqno, pdata->data, pdata->length);
|
build_node_state(&new_tlv, pdata->id, pdata->seqno, pdata->data, pdata->length);
|
||||||
add_tlv(&pack, &new_tlv, sender, socket_num);
|
add_tlv(&pack, &new_tlv, sender, socket_num);
|
||||||
|
} else {
|
||||||
|
print_debug(">> Found no data for the requested node, skipping...");
|
||||||
}
|
}
|
||||||
|
|
||||||
// The position is updated
|
// The position is updated
|
||||||
@ -979,16 +989,20 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
cur_tlv.node_state = (node_state*) (data + pos);
|
cur_tlv.node_state = (node_state*) (data + pos);
|
||||||
|
|
||||||
print_debug(">> Received message ! ");
|
print_debug(">> Received message ! ");
|
||||||
printf("%d\n", cur_tlv.node_state->type );
|
printf("Type : %d\n", cur_tlv.node_state->type );
|
||||||
printf("%d\n", cur_tlv.node_state->length );
|
printf("Length : %d\n", cur_tlv.node_state->length );
|
||||||
printf("%lu\n", cur_tlv.node_state->node_id );
|
printf("Node ID : %lu\n", be64toh(cur_tlv.node_state->node_id) );
|
||||||
printf("%hu\n", cur_tlv.node_state->seqno );
|
printf("Seqno : %hu\n", be16toh(cur_tlv.node_state->seqno) );
|
||||||
printf("%.16s\n", cur_tlv.node_state->node_hash);
|
printf("Hash :");
|
||||||
printf("%s\0\n", cur_tlv.node_state->data);
|
for(int x = 0; x < 16; x++){
|
||||||
|
printf("%02x", cur_tlv.node_state->node_hash[x]);
|
||||||
sprintf(warn, ">> WARNING:\n%%.%ds", cur_tlv.node_state->length);
|
fflush(0);
|
||||||
printf(warn, cur_tlv.node_state->data);
|
}
|
||||||
sleep(3);
|
printf("\n");
|
||||||
|
for(int x = 0; x < 192; x++){
|
||||||
|
printf("%d", cur_tlv.node_state->data[x]);
|
||||||
|
fflush(0);
|
||||||
|
}
|
||||||
|
|
||||||
if (DEBUG_LEVEL > 0) {
|
if (DEBUG_LEVEL > 0) {
|
||||||
if (cur_tlv.node_state->data == NULL) {
|
if (cur_tlv.node_state->data == NULL) {
|
||||||
@ -996,6 +1010,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0”\n", (const wchar_t*) cur_tlv.node_state->data);
|
printf("\x1b[31m[DEBUG]\x1b[0m >> “%ls\0”\n", (const wchar_t*) cur_tlv.node_state->data);
|
||||||
|
sleep(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compare hashes
|
// Compare hashes
|
||||||
@ -1011,7 +1026,7 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
if(memcmp(hash, cur_tlv.node_state->node_hash, 16) == 0) {
|
if(memcmp(hash, cur_tlv.node_state->node_hash, 16) == 0) {
|
||||||
// The position is updated
|
// The position is updated
|
||||||
tlv_len = data[pos+1];
|
tlv_len = data[pos+1];
|
||||||
pos += 2;
|
pos += tlv_len + 2;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -1046,7 +1061,8 @@ int work_with_tlvs(char * data, uint16_t total_packet_len, struct sockaddr_in6 *
|
|||||||
// 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.");
|
||||||
print_debug(">> Malformed packet, we won't treat it.");
|
print_debug(">> Malformed packet, we won't treat it.");
|
||||||
build_warning(&new_tlv, warn, strlen(warn));
|
print_debug(">> Sending back a Warning to sender.");
|
||||||
|
build_warning(&new_tlv,(unsigned char *) warn, strlen(warn));
|
||||||
add_tlv(&pack, &new_tlv, sender, socket_num);
|
add_tlv(&pack, &new_tlv, sender, socket_num);
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
@ -1221,20 +1237,20 @@ int run_node(int sock_fd){
|
|||||||
|
|
||||||
|
|
||||||
/* Call poll() */
|
/* Call poll() */
|
||||||
ret = poll(fds, 2, 5);
|
ret = poll(fds, 2, 10);
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
print_debug(">> Error - poll returned error");
|
print_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)) {
|
||||||
print_debug("Error - poll indicated stdin error\n");
|
print_error("Poll indicated stdin error\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) {
|
if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) {
|
||||||
print_debug("Error - poll indicated socket error\n");
|
print_error("Poll indicated socket error\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1242,9 +1258,10 @@ 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) {
|
||||||
print_debug(">> Error - stdin error");
|
print_error("stdin error");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// Remove trailing \n
|
||||||
input_buffer[strcspn(input_buffer, "\n")] = 0;
|
input_buffer[strcspn(input_buffer, "\n")] = 0;
|
||||||
if (DEBUG_LEVEL > 0) {
|
if (DEBUG_LEVEL > 0) {
|
||||||
printf("\x1b[31m[DEBUG]\x1b[0m >> Adding following message to the table : “%s”\n", input_buffer );
|
printf("\x1b[31m[DEBUG]\x1b[0m >> Adding following message to the table : “%s”\n", input_buffer );
|
||||||
@ -1252,6 +1269,8 @@ int run_node(int sock_fd){
|
|||||||
// 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) {
|
||||||
print_debug(">> 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..");
|
||||||
|
// Reset buffer
|
||||||
|
memset(input_buffer, 0, sizeof(input_buffer));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1288,6 +1307,8 @@ int run_node(int sock_fd){
|
|||||||
if (work_tlv_status < 0) {
|
if (work_tlv_status < 0) {
|
||||||
print_debug(">> Error while treating the incoming packet.");
|
print_debug(">> Error while treating the incoming packet.");
|
||||||
}
|
}
|
||||||
|
// Reset buffer
|
||||||
|
memset(output_buffer, 0, sizeof(output_buffer));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -1304,7 +1325,7 @@ 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, char * root_peer_ip, uint16_t root_peer_port){
|
||||||
print_debug(">> Boostraping node...");
|
print_debug(">> Boostraping node...");
|
||||||
|
|
||||||
struct sockaddr_in6 server_addr;
|
struct sockaddr_in6 server_addr;
|
||||||
@ -1312,42 +1333,46 @@ int bootstrap_node(int * sock_fd){
|
|||||||
/* 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) {
|
||||||
print_debug(">> Error - failed to open socket");
|
print_error("Failed to open socket");
|
||||||
return -1;
|
perror("");
|
||||||
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Bind socket */
|
/* Bind socket */
|
||||||
memset(&server_addr, 0, sizeof(server_addr));
|
memset(&server_addr, 0, sizeof(server_addr));
|
||||||
server_addr.sin6_family = AF_INET6;
|
server_addr.sin6_family = AF_INET6;
|
||||||
// server_addr.sin6_addr.in6_addr = htonl(INADDR_ANY);
|
// server_addr.sin6_addr.in6_addr = htonl(INADDR_ANY);
|
||||||
server_addr.sin6_port = htobe16(LISTEN_PORT);
|
server_addr.sin6_port = 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) {
|
||||||
print_debug(">> Error - failed to bind socket");
|
print_error("Failed to bind socket");
|
||||||
return -2;
|
perror("");
|
||||||
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Make the first peer*/
|
/* Make the first peer*/
|
||||||
struct neighbour_peer * root_peer = (struct neighbour_peer *) malloc(sizeof(struct neighbour_peer));
|
struct neighbour_peer * root_peer = (struct neighbour_peer *) malloc(sizeof(struct neighbour_peer));
|
||||||
time_t root_peer_seen = time(NULL);
|
time_t root_peer_seen = time(NULL);
|
||||||
|
|
||||||
int inet_p = inet_pton(AF_INET6, ROOT_PEER_ADDR, &root_peer->ip);
|
int inet_p = inet_pton(AF_INET6, root_peer_ip, &root_peer->ip);
|
||||||
if(inet_p < 1){
|
if(inet_p < 1){
|
||||||
perror(">> Failed to create the root peer.");
|
perror(">> Failed to create the root peer.");
|
||||||
return -3;
|
return -3;
|
||||||
}
|
}
|
||||||
|
|
||||||
root_peer->port = 1212;
|
root_peer->port = root_peer_port;
|
||||||
root_peer->is_temporary = 0;
|
root_peer->is_temporary = 0;
|
||||||
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
|
||||||
print_debug(">> Adding the first root peer to the list...");
|
print_debug(">> Adding the first root peer to the list...");
|
||||||
neighbour_list = malloc(sizeof(struct list));
|
neighbour_list = malloc(sizeof(struct list));
|
||||||
|
memset(neighbour_list, 0, sizeof(struct list));
|
||||||
neighbour_list->data = (void *) root_peer;
|
neighbour_list->data = (void *) root_peer;
|
||||||
neighbour_list->next = NULL;
|
neighbour_list->next = NULL;
|
||||||
|
|
||||||
print_debug(">> Initializing data list...");
|
print_debug(">> Initializing data list...");
|
||||||
data_list = (list*) malloc(sizeof(list));
|
data_list = (list*) malloc(sizeof(list));
|
||||||
|
memset(data_list, 0, sizeof(struct list));
|
||||||
data_list->data = malloc(sizeof(pub_data));
|
data_list->data = malloc(sizeof(pub_data));
|
||||||
data_list->next = NULL;
|
data_list->next = NULL;
|
||||||
|
|
||||||
@ -1363,11 +1388,27 @@ int bootstrap_node(int * sock_fd){
|
|||||||
|
|
||||||
|
|
||||||
int main(int argc, const char *argv[]) {
|
int main(int argc, const char *argv[]) {
|
||||||
|
|
||||||
|
if (argc != 3) {
|
||||||
|
print_error("Usage : ./dazibao <root peer IPv6> <port number>");
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *end;
|
||||||
|
intmax_t val = strtoimax(argv[2], &end, 10);
|
||||||
|
if (errno == ERANGE || val < 0 || val > UINT16_MAX || end == argv[2] || *end != '\0'){
|
||||||
|
print_error("Conversion of port number failed, please choose a smaller number.");
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t root_peer_port = (uint16_t) val;
|
||||||
|
|
||||||
|
char * root_peer_ip = (char *) argv[1];
|
||||||
|
|
||||||
setlocale(LC_ALL, "");
|
setlocale(LC_ALL, "");
|
||||||
print_debug(">> Starting node");
|
print_debug(">> Starting node");
|
||||||
|
|
||||||
int sock_fd;
|
int sock_fd;
|
||||||
bootstrap_node(&sock_fd);
|
bootstrap_node(&sock_fd, root_peer_ip, root_peer_port);
|
||||||
run_node(sock_fd);
|
run_node(sock_fd);
|
||||||
close(sock_fd);
|
close(sock_fd);
|
||||||
|
|
||||||
|
12
src/node.h
12
src/node.h
@ -43,7 +43,7 @@ typedef struct pub_data {
|
|||||||
unsigned char length;
|
unsigned char length;
|
||||||
uint64_t id;
|
uint64_t id;
|
||||||
uint16_t seqno;
|
uint16_t seqno;
|
||||||
char *data;
|
unsigned char *data;
|
||||||
} pub_data;
|
} pub_data;
|
||||||
|
|
||||||
// General list
|
// General list
|
||||||
@ -62,14 +62,14 @@ typedef struct list {
|
|||||||
|
|
||||||
// The node ID
|
// The node ID
|
||||||
// #define NODE_ID 42675882021843277
|
// #define NODE_ID 42675882021843277
|
||||||
#define NODE_ID 42013376900101010
|
#define NODE_ID 13809235719890846928
|
||||||
|
|
||||||
// The number of neighbours
|
// The number of neighbours
|
||||||
// The neighbour table has 15 entries
|
// The neighbour table has 15 entries
|
||||||
#define NEIGHBOUR_MAX 15
|
#define NEIGHBOUR_MAX 15
|
||||||
|
|
||||||
// The adress of the main peer
|
// The adress of the main peer
|
||||||
#define ROOT_PEER_ADDR "2001:660:3301:9200::51c2:1b9b"
|
#define ROOT_PEER_ADDR "::1"
|
||||||
|
|
||||||
|
|
||||||
// fonctions signatures
|
// fonctions signatures
|
||||||
@ -124,7 +124,7 @@ int add_message(char * message, int message_len);
|
|||||||
|
|
||||||
// This functions creates the structures needed for the rest of the project,
|
// This functions creates the structures needed for the rest of the project,
|
||||||
// creates the socket, and returns all of it.
|
// creates the socket, and returns all of it.
|
||||||
int bootstrap_node(int * sock_fd);
|
int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port);
|
||||||
|
|
||||||
// Helper functions
|
// Helper functions
|
||||||
int len_list(list *l);
|
int len_list(list *l);
|
||||||
@ -141,9 +141,9 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port);
|
|||||||
pub_data *get_data(uint64_t id);
|
pub_data *get_data(uint64_t id);
|
||||||
|
|
||||||
// Take data as args and create a pub_data structure in the heap
|
// Take data as args and create a pub_data structure in the heap
|
||||||
pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, char *data);
|
pub_data *copy_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data);
|
||||||
|
|
||||||
// add new data to data list
|
// add new data to data list
|
||||||
int add_data(unsigned char len, uint64_t id, uint16_t seqno, char *data, pub_data *found);
|
int add_data(unsigned char len, uint64_t id, uint16_t seqno, unsigned char *data, pub_data *found);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -144,7 +144,7 @@ int build_network_state_req(tlv *tlv) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data) {
|
int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data) {
|
||||||
// Free the previously allocated memory
|
// Free the previously allocated memory
|
||||||
free(tlv->pad1);
|
free(tlv->pad1);
|
||||||
|
|
||||||
@ -187,7 +187,7 @@ int build_node_state_req(tlv *tlv, uint64_t node_id) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len) {
|
int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data, size_t data_len) {
|
||||||
// Free the previously allocated memory
|
// Free the previously allocated memory
|
||||||
free(tlv->pad1);
|
free(tlv->pad1);
|
||||||
|
|
||||||
@ -219,7 +219,7 @@ int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, siz
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int build_warning(tlv *tlv, char *message, size_t message_len) {
|
int build_warning(tlv *tlv, unsigned char *message, size_t message_len) {
|
||||||
// Free the previously allocated memory
|
// Free the previously allocated memory
|
||||||
free(tlv->pad1);
|
free(tlv->pad1);
|
||||||
|
|
||||||
|
16
src/tlv.h
16
src/tlv.h
@ -53,7 +53,7 @@ typedef struct neighbour {
|
|||||||
typedef struct network_hash {
|
typedef struct network_hash {
|
||||||
unsigned char type;
|
unsigned char type;
|
||||||
unsigned char length;
|
unsigned char length;
|
||||||
char network_hash[16];
|
unsigned char network_hash[16];
|
||||||
} network_hash;
|
} network_hash;
|
||||||
|
|
||||||
// 2 octets
|
// 2 octets
|
||||||
@ -68,7 +68,7 @@ typedef struct node_hash {
|
|||||||
unsigned char length;
|
unsigned char length;
|
||||||
uint64_t node_id;
|
uint64_t node_id;
|
||||||
uint16_t seqno;
|
uint16_t seqno;
|
||||||
char node_hash[16];
|
unsigned char node_hash[16];
|
||||||
} node_hash;
|
} node_hash;
|
||||||
|
|
||||||
// 10 octets
|
// 10 octets
|
||||||
@ -84,15 +84,15 @@ typedef struct node_state {
|
|||||||
unsigned char length;
|
unsigned char length;
|
||||||
uint64_t node_id;
|
uint64_t node_id;
|
||||||
uint16_t seqno;
|
uint16_t seqno;
|
||||||
char node_hash[16];
|
unsigned char node_hash[16];
|
||||||
char data[192];
|
unsigned char data[192];
|
||||||
} node_state;
|
} node_state;
|
||||||
|
|
||||||
// 2 octets min, 258 ocets max (unsigned char 0 -> 255)
|
// 2 octets min, 258 ocets max (unsigned char 0 -> 255)
|
||||||
typedef struct warning {
|
typedef struct warning {
|
||||||
unsigned char type;
|
unsigned char type;
|
||||||
unsigned char length;
|
unsigned char length;
|
||||||
char message[256];
|
unsigned char message[256];
|
||||||
} warning;
|
} warning;
|
||||||
|
|
||||||
typedef union tlv {
|
typedef union tlv {
|
||||||
@ -122,9 +122,9 @@ int build_neighbour_req(union tlv *tlv);
|
|||||||
int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port);
|
int build_neighbour(tlv *tlv, struct in6_addr ip, int16_t port);
|
||||||
int build_network_hash(tlv *tlv, list *data_list);
|
int build_network_hash(tlv *tlv, list *data_list);
|
||||||
int build_network_state_req(tlv *tlv);
|
int build_network_state_req(tlv *tlv);
|
||||||
int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data);
|
int build_node_hash(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data);
|
||||||
int build_node_state_req(tlv *tlv, uint64_t node_id);
|
int build_node_state_req(tlv *tlv, uint64_t node_id);
|
||||||
int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, char *data, size_t data_len);
|
int build_node_state(tlv *tlv, uint64_t node_id, uint16_t seqno, unsigned char *data, size_t data_len);
|
||||||
int build_warning(tlv *tlv, char *message, size_t message_len);
|
int build_warning(tlv *tlv, unsigned char *message, size_t message_len);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user