diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 2aad53d..0000000 --- a/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -dazibao -*.o -debug.* diff --git a/src/node.c b/src/node.c index 40d52fb..41e648c 100644 --- a/src/node.c +++ b/src/node.c @@ -21,10 +21,59 @@ static list *neighbour_list; /* ---- Fonctions utilitaires ---- */ -void debug_print(char message_debug){ - if (debug_flag == 1) { - printf("\x1b[33m\x1b[4m>> Debug :\x1b[0m\x1b[33m %s\x1b[0m\n", message_debug ); - } +// Looks for more peers +int ask_for_peers(int socket_num) { + print_debug(">> Asking for more peers..."); + // Only ask for more peers if the neighbour list is small enough + int nbr_peers = len_list(neighbour_list); + if( nbr_peers >= 5){ + return 0; + } else if (nbr_peers <= 0){ + print_debug(">> No peers found in the peer list, something terrible happened."); + return -1; + } else { + + // Get random peer + neighbour_peer *peer = get_random_neighbour(); + 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 + 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; + neighbour_req.pad1 = NULL; + int rc = build_neighbour_req(&neighbour_req); + if (rc < 0) { + print_debug(">> Failed to build neighbour_req"); + } + + rc = send_single_tlv(&neighbour_req, &dest, socket_num); + if (rc < 0) { + print_debug(">> Error while sending a TLV."); + return -1; + } else { + print_debug(">> Send TLV."); + return 0; + } + } } // Get list length diff --git a/src/node.h b/src/node.h index 444ca0c..a45a61e 100644 --- a/src/node.h +++ b/src/node.h @@ -121,8 +121,6 @@ int bootstrap_node(int * sock_fd); // Helper functions int len_list(list *l); -void debug_print(char message_debug); - neighbour_peer *get_random_neighbour(); // Search for this peer in the neighbour table