Modification de len_list
This commit is contained in:
parent
481903c24a
commit
723466fba8
66
src/node.c
66
src/node.c
@ -23,34 +23,40 @@ static list *neighbour_list;
|
|||||||
// Looks for more peers
|
// Looks for more peers
|
||||||
int ask_for_peers(int socket_num) {
|
int ask_for_peers(int socket_num) {
|
||||||
// Only ask for more peers if the neighbour list is small enough
|
// 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;
|
return 0;
|
||||||
|
} else if (nbr_peers <= 0){
|
||||||
// Get random peer
|
printf(">> No peers found in the peer list, something terrible happened.\n");
|
||||||
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;
|
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
|
// Get list length
|
||||||
@ -62,7 +68,6 @@ int len_list(list *l) {
|
|||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
len++;
|
len++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,9 +75,9 @@ int len_list(list *l) {
|
|||||||
neighbour_peer *get_random_neighbour() {
|
neighbour_peer *get_random_neighbour() {
|
||||||
// Get a random number
|
// Get a random number
|
||||||
time_t t;
|
time_t t;
|
||||||
srand((unsigned) time(&t));
|
srand((unsigned) time(NULL));
|
||||||
int n = rand() % len_list(neighbour_list);
|
int n = rand() % len_list(neighbour_list);
|
||||||
|
printf("%i\n", n );
|
||||||
// Get nth neighbour
|
// Get nth neighbour
|
||||||
list *tmp = neighbour_list;
|
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
|
// check for same ip and same port
|
||||||
peer = (neighbour_peer*) tmp->data;
|
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;
|
return peer;
|
||||||
|
}
|
||||||
|
|
||||||
// if they differ, get next peer
|
// if they differ, get next peer
|
||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
@ -889,7 +895,7 @@ int run_node(int sock_fd){
|
|||||||
|
|
||||||
// Init the ~20s delay for node update.
|
// Init the ~20s delay for node update.
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
time_t delay = time(NULL) + 20;
|
time_t delay = time(NULL) + 2;
|
||||||
|
|
||||||
/* Descriptor zero is stdin */
|
/* Descriptor zero is stdin */
|
||||||
fds[0].fd = 0;
|
fds[0].fd = 0;
|
||||||
|
@ -53,7 +53,8 @@ typedef struct list {
|
|||||||
#define LISTEN_PORT 1212
|
#define LISTEN_PORT 1212
|
||||||
|
|
||||||
// The node ID
|
// The node ID
|
||||||
#define NODE_ID 42675882021843277
|
// #define NODE_ID 42675882021843277
|
||||||
|
#define NODE_ID 42013376900101010
|
||||||
|
|
||||||
// The number of neighbours
|
// The number of neighbours
|
||||||
// The neighbour table has 15 entries
|
// The neighbour table has 15 entries
|
||||||
|
Loading…
Reference in New Issue
Block a user