Merge branch '16-update_neighbours' of https://gaufre.informatique.univ-paris-diderot.fr/perdriau/dazibao into 16-update_neighbours

This commit is contained in:
gonzalef 2020-04-27 20:09:02 +02:00
commit 00ed727da0
2 changed files with 23 additions and 17 deletions

View File

@ -90,10 +90,8 @@ int len_list(list *l) {
neighbour_peer *get_random_neighbour() {
printf(">> Getting random peer...\n");
// Get a random number
time_t t;
srand((unsigned) time(NULL));
int n = (rand() % len_list(neighbour_list)) + 1;
int l_l = len_list(neighbour_list);
// Get nth neighbour
list *tmp = neighbour_list;
@ -125,13 +123,15 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) {
return NULL;
}
// Return -1 if the peer could not be added, 0 if it could or if it was already in the table
// Return -1 if we have enough peers,
// 1 if it was added
// Return 0 if peer was updated as last_seen
int add_n_update_neighbour(struct in6_addr *ip, int16_t port) {
char * buff_str_ip[1024];
char * ip_str = inet_ntop(AF_INET6,ip, 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, ntohs(port));
printf(">> Found peer %s @ %i.\n", ip_str, port);
// We try to find a peer with this address and port.
neighbour_peer *peer = get_neighbour(ip, port);
time_t curtime;
@ -158,15 +158,14 @@ int add_n_update_neighbour(struct in6_addr *ip, int16_t port) {
node->data = (void*) peer;
node->next = neighbour_list;
neighbour_list = node;
return 0;
return 1;
}
} else {
printf(">> Found peer %s:%i, updating the last seen time...\n", (char *) ip, port);
printf(">> Found peer %s @ %i, updating the last seen time...\n", ip_str, port);
time_t curtime;
// if the peer was already in the list, update it
time(&curtime);
peer->last_seen = curtime;
return 0;
}
@ -610,16 +609,20 @@ int validate_tlv(char *data, int pos, int16_t packet_len){
// Check that we can read a length
if(pos + 1 >= packet_len){
printf(">> Reading out of packet length. \n");
return -1;
}
unsigned char tlv_len = data[pos+1];
// Check that the tlv does not exceed the packet length
if(pos + tlv_len >= packet_len){
if(pos + tlv_len > packet_len){
printf(">> The TLV Length exceed the packet length\n");
return -1;
}
printf(">> TLV has type %i\n", type );
// Returns the type of the tlv or -1 if something went wrong
switch(type) {
case 1:
@ -902,15 +905,19 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc
// Neighbour check
struct in6_addr ip = sender->sin6_addr;
int16_t port = sender->sin6_port;
int16_t port = htons(sender->sin6_port);
if(add_n_update_neighbour(&ip, port) == -1) {
fprintf(stderr, ">> Maximum number of neighbour peers reached, aborting this packet.\n");
int rc = add_n_update_neighbour(&ip, port);
if( rc == -1) {
printf(">> We have enough peers, we won't add him..\n");
return -1;
} else if (rc == 1){
printf(">> Peer was added to the table.\n");
} else {
printf(">> Updated the time it was last seen.\n");
}
// struct tlv_list received_tlvs;
// if (validate_tlvs(formated_rec_datagram) < 0)
int nbr_success_tlv = work_with_tlvs(received_data_buffer, received_data_len, sender, sock_fd);
if (nbr_success_tlv < 0){
perror(">> Error while treating the TLVs of the packet.");
@ -946,7 +953,7 @@ int run_node(int sock_fd){
// Init the ~20s delay for node update.
srand(time(NULL));
time_t delay = time(NULL) + 20;
time_t delay = time(NULL) + 5;
/* Descriptor zero is stdin */
fds[0].fd = 0;

View File

@ -62,8 +62,7 @@ typedef struct list {
// The adress of the main peer
#define ROOT_PEER_ADDR "2001:660:3301:9200::51c2:1b9b"
// TODO
// fonctions signatures
int listen_for_packets(char * received_data_buffer, int received_data_len, struct sockaddr_in6 * sender, int sock_fd);