Modification de len_list
This commit is contained in:
parent
481903c24a
commit
723466fba8
68
src/node.c
68
src/node.c
@ -23,34 +23,40 @@ static list *neighbour_list;
|
||||
// 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)
|
||||
int nbr_peers = len_list(neighbour_list);
|
||||
if( nbr_peers >= 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");
|
||||
} else if (nbr_peers <= 0){
|
||||
printf(">> No peers found in the peer list, something terrible happened.\n");
|
||||
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("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);
|
||||
|
||||
}
|
||||
|
||||
// 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
|
||||
@ -62,7 +68,6 @@ int len_list(list *l) {
|
||||
tmp = tmp->next;
|
||||
len++;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
@ -70,9 +75,9 @@ int len_list(list *l) {
|
||||
neighbour_peer *get_random_neighbour() {
|
||||
// Get a random number
|
||||
time_t t;
|
||||
srand((unsigned) time(&t));
|
||||
srand((unsigned) time(NULL));
|
||||
int n = rand() % len_list(neighbour_list);
|
||||
|
||||
printf("%i\n", n );
|
||||
// Get nth neighbour
|
||||
list *tmp = neighbour_list;
|
||||
|
||||
@ -93,8 +98,9 @@ neighbour_peer *get_neighbour(struct in6_addr *ip, int16_t port) {
|
||||
// check for same ip and same port
|
||||
peer = (neighbour_peer*) tmp->data;
|
||||
|
||||
if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0 && peer->port == port)
|
||||
if(memcmp(&peer->ip, ip, sizeof(struct in6_addr)) == 0 && peer->port == port) {
|
||||
return peer;
|
||||
}
|
||||
|
||||
// if they differ, get next peer
|
||||
tmp = tmp->next;
|
||||
@ -303,7 +309,7 @@ int update_neighbours() {
|
||||
// Free allocated memory
|
||||
free(node_to_delete->data);
|
||||
free(node_to_delete);
|
||||
|
||||
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@ -889,7 +895,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) + 2;
|
||||
|
||||
/* Descriptor zero is stdin */
|
||||
fds[0].fd = 0;
|
||||
|
@ -53,7 +53,8 @@ typedef struct list {
|
||||
#define LISTEN_PORT 1212
|
||||
|
||||
// The node ID
|
||||
#define NODE_ID 42675882021843277
|
||||
// #define NODE_ID 42675882021843277
|
||||
#define NODE_ID 42013376900101010
|
||||
|
||||
// The number of neighbours
|
||||
// The neighbour table has 15 entries
|
||||
|
Loading…
Reference in New Issue
Block a user