ask for peers
This commit is contained in:
		
							parent
							
								
									dac138c92a
								
							
						
					
					
						commit
						481903c24a
					
				
							
								
								
									
										62
									
								
								src/node.c
									
									
									
									
									
								
							
							
						
						
									
										62
									
								
								src/node.c
									
									
									
									
									
								
							| @ -12,7 +12,6 @@ | ||||
| #include <fcntl.h> | ||||
| #include <poll.h> | ||||
| #include <unistd.h> | ||||
| 
 | ||||
| #include "node.h" | ||||
| 
 | ||||
| // Static variables
 | ||||
| @ -21,6 +20,39 @@ static list *neighbour_list; | ||||
| 
 | ||||
| /* ----  Fonctions utilitaires  ---- */ | ||||
| 
 | ||||
| // Looks for more peers
 | ||||
| int ask_for_peers(int socket_num) { | ||||
|     // Only ask for more peers if the neighbour list is small enough
 | ||||
|     if(len_list(neighbour_list) >= 5) | ||||
|         return 0; | ||||
| 
 | ||||
|     // Get random peer
 | ||||
|     neighbour_peer *peer = get_random_neighbour(); | ||||
|     struct in6_addr ip = peer->ip; | ||||
|     int16_t port = peer->port; | ||||
| 
 | ||||
|     int ifindex = if_nametoindex("eth0"); | ||||
| 
 | ||||
|     if(ifindex == 0) { | ||||
|         perror("if_nametoindex failed"); | ||||
|         return -1; | ||||
|     } | ||||
| 
 | ||||
|     // Initialize sockaddr
 | ||||
|     struct sockaddr_in6 dest; | ||||
|     memset(&dest, 0, sizeof(struct sockaddr_in6)); | ||||
|     dest.sin6_family = AF_INET6; | ||||
|     memcpy(&dest.sin6_addr, &ip, 16); | ||||
|     dest.sin6_port = htons(port); | ||||
|     dest.sin6_scope_id = ifindex; | ||||
| 
 | ||||
|     // Send neighbour request TLV
 | ||||
|     tlv neighbour_req; | ||||
|     build_neighbour_req(&neighbour_req); | ||||
|      | ||||
|     return send_single_tlv(&neighbour_req, &dest, socket_num); | ||||
| } | ||||
| 
 | ||||
| // Get list length
 | ||||
| int len_list(list *l) { | ||||
| 	int len = 0; | ||||
| @ -834,19 +866,19 @@ int listen_for_packets(char * received_data_buffer, int received_data_len, struc | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| int t_ask_for_more_peers(list * neighbourhood, int sock_fd){ | ||||
| int t_ask_for_more_peers(int sock_fd){ | ||||
| 	return ask_for_peers(sock_fd); | ||||
| } | ||||
| 
 | ||||
| int t_get_network_state(int sock_fd){ | ||||
| 	return 0; | ||||
| } | ||||
| 
 | ||||
| int t_get_network_state(list * neighbourhood, int sock_fd){ | ||||
| 	return 0; | ||||
| } | ||||
| 
 | ||||
| int t_update_neighbours(list * neighbourhood){ | ||||
| int t_update_neighbours(){ | ||||
| 	return update_neighbours(); | ||||
| } | ||||
| 
 | ||||
| int run_node(int sock_fd, list * neighbourhood){ | ||||
| int run_node(int sock_fd){ | ||||
| 	printf(">> Running node...\n"); | ||||
| 
 | ||||
| 	int ret; | ||||
| @ -872,11 +904,11 @@ int run_node(int sock_fd, list * neighbourhood){ | ||||
| 
 | ||||
| 		if(time(NULL) >= delay) { | ||||
| 			printf(">> Asking for more peers...\n"); | ||||
| 			t_ask_for_more_peers(neighbourhood, sock_fd); | ||||
| 			t_ask_for_more_peers(sock_fd); | ||||
| 			printf(">> Updating neighbours...\n"); | ||||
| 			t_update_neighbours(neighbourhood); | ||||
| 			t_update_neighbours(); | ||||
| 			printf(">> Getting network state...\n"); | ||||
| 			t_get_network_state(neighbourhood, sock_fd); | ||||
| 			t_get_network_state(sock_fd); | ||||
| 	        delay = time(NULL) + 20 + (rand() % 10); | ||||
| 		} | ||||
| 
 | ||||
| @ -966,7 +998,7 @@ int run_node(int sock_fd, list * neighbourhood){ | ||||
| 
 | ||||
| 
 | ||||
| // This function runs once, and sets the sock_fd as well as the neighbourhood
 | ||||
| int bootstrap_node(int * sock_fd, list * neighbourhood){ | ||||
| int bootstrap_node(int * sock_fd){ | ||||
| 	printf(">> Boostraping node...\n"); | ||||
| 
 | ||||
| 	struct sockaddr_in6 server_addr; | ||||
| @ -1012,11 +1044,9 @@ int bootstrap_node(int * sock_fd, list * neighbourhood){ | ||||
| int main(int argc, const char *argv[]) { | ||||
| 	printf(">> Starting node\n"); | ||||
| 
 | ||||
| 	list neighbourhood; | ||||
| 
 | ||||
| 	int sock_fd; | ||||
| 	bootstrap_node(&sock_fd, &neighbourhood); | ||||
| 	run_node(sock_fd, &neighbourhood); | ||||
| 	bootstrap_node(&sock_fd); | ||||
| 	run_node(sock_fd); | ||||
| 	close(sock_fd); | ||||
| 
 | ||||
|     return 0; | ||||
|  | ||||
							
								
								
									
										12
									
								
								src/node.h
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								src/node.h
									
									
									
									
									
								
							| @ -73,6 +73,8 @@ int validate_tlv(char *data, int pos, int16_t packet_len); | ||||
| 
 | ||||
| int update_neighbours(); | ||||
| 
 | ||||
| int ask_for_peers(int socket_num); | ||||
| 
 | ||||
| int work_with_tlvs(char * data, int16_t packet_len, struct sockaddr_in6 *sender, int socket_num); | ||||
| 
 | ||||
| int add_tlv(packet *pack, tlv *tlv, struct sockaddr_in6 *dest, int socket_num); | ||||
| @ -82,7 +84,7 @@ int send_packet(char *packet_buff, int16_t length, struct sockaddr_in6 *dest, in | ||||
| int send_single_tlv(tlv *tlv, struct sockaddr_in6 *dest, int socket_num); | ||||
| 
 | ||||
| // This function is in charge of saying how and what goes where.
 | ||||
| int run_node(int sock_fd, list * neighbourhood); | ||||
| int run_node(int sock_fd); | ||||
| 
 | ||||
| /* Takes a TLV and sends it over to everyone in the list of addresses.
 | ||||
|  * Returns -1 in case of error, 0 otherwise. | ||||
| @ -97,24 +99,24 @@ int send_tlvs(struct list * tlv_list, int16_t length, struct sockaddr_in6 * dest | ||||
| /* Check our peer list. If we have less than 5 peers, send out a
 | ||||
|     TLV NEIGHBOUR_REQUEST to a random peer | ||||
| */ | ||||
| int t_ask_for_more_peers(list * neighbourhood, int sock_fd); | ||||
| int t_ask_for_more_peers(int sock_fd); | ||||
| 
 | ||||
| /* We look at every peer, if he is marked as is_temporary, AND we didn't get a
 | ||||
|     packet from him in the last 70 sec, we remove him from the list. | ||||
| */ | ||||
| int t_update_neighbours(list * neighbourhood); | ||||
| int t_update_neighbours(); | ||||
| 
 | ||||
| /* We send out a TLV Network Hash to every peer, and we expect getting a TLV
 | ||||
|     Network state from each of them. | ||||
| */ | ||||
| int t_get_network_state(list * neighbourhoodn, int sock_fd); | ||||
| int t_get_network_state(int sock_fd); | ||||
| 
 | ||||
| // This function adds a message to the message table.
 | ||||
| int add_message(char * message, int message_len); | ||||
| 
 | ||||
| // This functions creates the structures needed for the rest of the project,
 | ||||
| // creates the socket, and returns all of it.
 | ||||
| int bootstrap_node(int * sock_fd, list * neighbourhood); | ||||
| int bootstrap_node(int * sock_fd); | ||||
| 
 | ||||
| // Helper functions
 | ||||
| int len_list(list *l); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 gonzalef
						gonzalef