update neighbours
This commit is contained in:
		
							parent
							
								
									c633955b54
								
							
						
					
					
						commit
						dac138c92a
					
				
							
								
								
									
										97
									
								
								src/node.c
									
									
									
									
									
								
							
							
						
						
									
										97
									
								
								src/node.c
									
									
									
									
									
								
							| @ -52,7 +52,7 @@ neighbour_peer *get_random_neighbour() { | ||||
| } | ||||
| 
 | ||||
| // Search for this peer in the neighbour table
 | ||||
| neighbour_peer *get_neighbour(struct in6_addr *ip) { | ||||
| neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) { | ||||
|     // Look for neighbour
 | ||||
|     list *tmp = neighbour_list; | ||||
|     neighbour_peer *peer; | ||||
| @ -61,7 +61,7 @@ neighbour_peer *get_neighbour(struct in6_addr *ip) { | ||||
|         // check for same ip and same port
 | ||||
|         peer = (neighbour_peer*) tmp->data; | ||||
| 
 | ||||
|         if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0) | ||||
|         if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0 && peer->port == port) | ||||
|             return peer; | ||||
| 
 | ||||
|         // if they differ, get next peer
 | ||||
| @ -72,9 +72,9 @@ neighbour_peer *get_neighbour(struct in6_addr *ip) { | ||||
| } | ||||
| 
 | ||||
| // Return -1 if the peer could not be added, 0 if it could or if it was already in the table
 | ||||
| int add_n_update_neighbour(struct in6_addr *ip) { | ||||
|     neighbour_peer *peer = get_neighbour(ip); | ||||
|     struct timeval curtime; | ||||
| int add_n_update_neighbour(struct in6_addr *ip, int16_t port) { | ||||
|     neighbour_peer *peer = get_neighbour(ip, port); | ||||
|     time_t curtime; | ||||
| 
 | ||||
|     // peer is not in the neighbour list so we try to add it
 | ||||
|     if(ip == NULL) { | ||||
| @ -89,8 +89,8 @@ int add_n_update_neighbour(struct in6_addr *ip) { | ||||
|         peer->is_temporary = 1; | ||||
| 
 | ||||
|         // set last_seen time
 | ||||
|         gettimeofday(&curtime, NULL); | ||||
|         memcpy(&peer->last_seen, &curtime, sizeof(struct timeval)); | ||||
|         time(&curtime); | ||||
|         peer->last_seen = curtime; | ||||
| 
 | ||||
|         // set new peer as head of list
 | ||||
|         list *node = (list*) malloc(sizeof(list)); | ||||
| @ -102,8 +102,8 @@ int add_n_update_neighbour(struct in6_addr *ip) { | ||||
|     } | ||||
| 
 | ||||
|     // if the peer was already in the list, update it
 | ||||
|     gettimeofday(&curtime, NULL); | ||||
|     memcpy(&peer->last_seen, &curtime, sizeof(struct timeval)); | ||||
|     time(&curtime); | ||||
|     peer->last_seen = curtime; | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
| @ -225,6 +225,65 @@ void add_data(unsigned char len, int64_t id, int16_t seqno, char *data) { | ||||
| 
 | ||||
| /* ---- Fin fonctions utilitaires ---- */ | ||||
| 
 | ||||
| // Update the neighbour list
 | ||||
| int update_neighbours() { | ||||
|     list *tmp = neighbour_list, *last = NULL, *node_to_delete; | ||||
|     neighbour_peer *peer; | ||||
|     time_t curtime; | ||||
|     int deleted = 0; | ||||
| 
 | ||||
|     // check every neighbour
 | ||||
|     while(tmp != NULL) { | ||||
|         peer = (neighbour_peer*) tmp->data; | ||||
| 
 | ||||
|         // 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
 | ||||
|             time(&curtime); | ||||
| 
 | ||||
|             if(difftime(peer->last_seen, curtime) >= 70) { | ||||
|                 // increase the count of deleted nodes
 | ||||
|                 deleted++; | ||||
| 
 | ||||
|                 // If head of the list
 | ||||
|                 if(last == NULL) { | ||||
|                     // Store node to delete
 | ||||
|                     node_to_delete = tmp; | ||||
| 
 | ||||
|                     // Update list
 | ||||
|                     tmp = tmp->next; | ||||
|                     neighbour_list = tmp; | ||||
| 
 | ||||
|                     // Free allocated memory
 | ||||
|                     free(node_to_delete->data); | ||||
|                     free(node_to_delete); | ||||
| 
 | ||||
|                     continue; | ||||
|                 } | ||||
| 
 | ||||
|                 // Store node to delete
 | ||||
|                 node_to_delete = tmp; | ||||
| 
 | ||||
|                 // Update list
 | ||||
|                 tmp = tmp->next; | ||||
|                 last->next = tmp; | ||||
| 
 | ||||
|                 // Free allocated memory
 | ||||
|                 free(node_to_delete->data); | ||||
|                 free(node_to_delete); | ||||
|                  | ||||
|                 continue; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         last = tmp; | ||||
|         tmp = tmp->next; | ||||
|     } | ||||
| 
 | ||||
|     // returns the amount of nodes that were deleted
 | ||||
|     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
 | ||||
| int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num) { | ||||
|     char type = tlv->pad1->type, sent = 0, errval = 0; | ||||
| @ -550,13 +609,6 @@ int check_header(char * received_data_buffer, int received_data_len, struct pack | ||||
|     return 0; | ||||
| } | ||||
| 
 | ||||
| // If the sender is not in the neighbourhood, and we have 15 neighbours, we
 | ||||
| // ignore the packet. Otherwise, we add him to the neighbourhood, marked as
 | ||||
| // temporary.
 | ||||
| int update_neighbours(){ | ||||
|     return 0; | ||||
| }; | ||||
| 
 | ||||
| int add_message(char * message, int message_len){ | ||||
| 	return 0; | ||||
| } | ||||
| @ -760,7 +812,10 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc | ||||
|     } | ||||
| 
 | ||||
|     // Neighbour check
 | ||||
|     if(add_n_update_neighbour(sender) == -1) { | ||||
|     struct in6_addr ip = sender->sin6_addr; | ||||
|     int16_t port = sender->sin6_port; | ||||
| 
 | ||||
|     if(add_n_update_neighbour(&ip, port) == -1) { | ||||
|         fprintf(stderr, ">> Maximum number of neighbour peers reached, aborting this packet.\n"); | ||||
|         return -1; | ||||
|     } | ||||
| @ -788,7 +843,7 @@ int t_get_network_state(list * neighbourhood, int sock_fd){ | ||||
| } | ||||
| 
 | ||||
| int t_update_neighbours(list * neighbourhood){ | ||||
| 	return 0; | ||||
| 	return update_neighbours(); | ||||
| } | ||||
| 
 | ||||
| int run_node(int sock_fd, list * neighbourhood){ | ||||
| @ -802,7 +857,7 @@ int run_node(int sock_fd, list * neighbourhood){ | ||||
| 
 | ||||
| 	// Init the ~20s delay for node update.
 | ||||
| 	srand(time(NULL)); | ||||
| 	int delay = time(NULL) + 20; | ||||
| 	time_t delay = time(NULL) + 20; | ||||
| 
 | ||||
| 	/* Descriptor zero is stdin */ | ||||
| 	fds[0].fd = 0; | ||||
| @ -815,7 +870,7 @@ int run_node(int sock_fd, list * neighbourhood){ | ||||
| 	 */ | ||||
| 	while (1) { | ||||
| 
 | ||||
| 		if (time(NULL) >= delay) { | ||||
| 		if(time(NULL) >= delay) { | ||||
| 			printf(">> Asking for more peers...\n"); | ||||
| 			t_ask_for_more_peers(neighbourhood, sock_fd); | ||||
| 			printf(">> Updating neighbours...\n"); | ||||
| @ -935,7 +990,7 @@ int bootstrap_node(int * sock_fd, list * neighbourhood){ | ||||
| 
 | ||||
| 	/* Make the first peer*/ | ||||
| 	struct neighbour_peer root_peer; | ||||
| 	struct timeval root_peer_seen; | ||||
| 	time_t root_peer_seen = time(NULL); | ||||
| 
 | ||||
| 	int inet_p = inet_pton(AF_INET6, ROOT_PEER_ADDR, &root_peer.ip); | ||||
| 	if(inet_p < 1){ | ||||
|  | ||||
| @ -10,7 +10,6 @@ | ||||
| #include <netinet/in.h> | ||||
| #include <string.h> | ||||
| #include <time.h> | ||||
| #include <sys/time.h> | ||||
| #include <stdint.h> | ||||
| #include <net/if.h> | ||||
| 
 | ||||
| @ -23,7 +22,7 @@ typedef struct neighbour_peer { | ||||
|     struct in6_addr ip; | ||||
|     int16_t port; | ||||
|     char is_temporary; | ||||
|     struct timeval last_seen; | ||||
|     time_t last_seen; | ||||
| } neighbour_peer; | ||||
| 
 | ||||
| // The strucuture to hold the messages
 | ||||
| @ -123,10 +122,10 @@ int len_list(list *l); | ||||
| neighbour_peer *get_random_neighbour(); | ||||
| 
 | ||||
| // Search for this peer in the neighbour table
 | ||||
| neighbour_peer *get_neighbour(struct in6_addr *ip); | ||||
| neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port); | ||||
| 
 | ||||
| // Return -1 if the peer could not be added, 0 if it could or if it was already in the table
 | ||||
| int add_n_update_neighbour(struct in6_addr *ip); | ||||
| int add_n_update_neighbour(struct in6_addr *ip, int16_t port); | ||||
| 
 | ||||
| // get data associated with id, if it doesn't exist return NULL
 | ||||
| pub_data *get_data(int64_t id); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 gonzalef
						gonzalef