From bda5108589e36b15b85c767c9d092ab0407f282b Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 6 May 2020 16:08:46 +0200 Subject: [PATCH 1/2] Fixed adding a peer a bootstrap --- src/node.c | 52 ++++++++++++++++++++++------------------------------ 1 file changed, 22 insertions(+), 30 deletions(-) diff --git a/src/node.c b/src/node.c index 3623f0f..18c76f5 100644 --- a/src/node.c +++ b/src/node.c @@ -338,7 +338,7 @@ int update_neighbours() { // from this peer then remove it from the list time(&curtime); - if(difftime(peer->last_seen, curtime) >= 70) { + if(difftime(curtime, peer->last_seen) >= 70) { // increase the count of deleted nodes deleted++; @@ -1395,6 +1395,7 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ int rc = getaddrinfo(root_peer_ip, "http", &hints, &res); if(rc != 0) { print_error("Failed to resolve hostname, exiting..."); + printf(gai_strerror(rc)); exit(1); } @@ -1404,38 +1405,29 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ // For every address given to us by getaddrinfo, we create a new peer. struct addrinfo *p; struct neighbour_peer *peer ; + p = res; + peer = (neighbour_peer*) malloc(sizeof(neighbour_peer)); + memcpy(&peer->ip, &((struct sockaddr_in6 *) p->ai_addr)->sin6_addr, sizeof(struct in6_addr)); + peer->port = root_peer_port; + peer->is_temporary = 0; - list * tmp = neighbour_list; - for(p = res; p != NULL; p = p->ai_next) { - peer = (neighbour_peer*) malloc(sizeof(neighbour_peer)); - memcpy(&peer->ip, &((struct sockaddr_in6 *) p->ai_addr)->sin6_addr, sizeof(struct in6_addr)); - peer->port = root_peer_port; - peer->is_temporary = 0; + // set last_seen time + peer->last_seen = time(NULL); - // set last_seen time - peer->last_seen = time(NULL); - - if (DEBUG_LEVEL > 0) { - char * buff_str_ip[1024]; - char * ip_str = (char * ) inet_ntop(AF_INET6,&peer->ip,(char * restrict) buff_str_ip, 1024); - printf("\x1b[31m[DEBUG]\x1b[0m >> Adding %s @ %i to peer list.\n", ip_str, root_peer_port ); - } - - - - // Add new peer to the list. - list *node = (list*) malloc(sizeof(list)); - node->data = (void*) peer; - node->next = NULL; - - // Goint to the end of the list - while(tmp->next != NULL){ - tmp = tmp->next; - } - tmp->next = node; + if (DEBUG_LEVEL > 0) { + char * buff_str_ip[1024]; + char * ip_str = (char * ) inet_ntop(AF_INET6,&peer->ip,(char * restrict) buff_str_ip, 1024); + printf("\x1b[31m[DEBUG]\x1b[0m >> Adding %s @ %i to peer list.\n", ip_str, root_peer_port ); } - neighbour_list = tmp; + + + // Add new peer to the list. + list *node = (list*) malloc(sizeof(list)); + node->data = (void*) peer; + node->next = neighbour_list; + neighbour_list = node; + neighbour_list->next = NULL; print_peers(neighbour_list); @@ -1451,7 +1443,7 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ our_data->seqno = 1337; our_data->data = NULL; - + freeaddrinfo(res); print_debug(">> Boostraping done."); From 2b0756a921f1f3ebf53877ed55b67a24c61918d5 Mon Sep 17 00:00:00 2001 From: n07070 Date: Wed, 6 May 2020 16:49:09 +0200 Subject: [PATCH 2/2] Update --- src/node.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/node.h b/src/node.h index a961637..a852e4c 100644 --- a/src/node.h +++ b/src/node.h @@ -61,9 +61,8 @@ typedef struct list { #define LISTEN_PORT 1212 // The node ID -#define NODE_ID 4209169790617845760 -// #define NODE_ID 42675882021843277 -// #define NODE_ID 1312 +// #define NODE_ID 4209169790617845760 +#define NODE_ID 13386709784863621846 // The number of neighbours // The neighbour table has 15 entries