From 57d04eb1c3a67d5ba3adcea28c5188c95615376a Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 4 May 2020 20:46:12 +0200 Subject: [PATCH 1/3] Added ability to add via hostname --- src/debug.c | 4 +++ src/debug.h | 2 +- src/node.c | 70 ++++++++++++++++++++++++++++++++++++++++------------- src/node.h | 1 + 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/src/debug.c b/src/debug.c index c596646..a23736d 100644 --- a/src/debug.c +++ b/src/debug.c @@ -44,6 +44,10 @@ void print_peers(list * l){ print_debug(">> Printing out peer list :"); while(l != NULL){ neighbour_peer * peer = (neighbour_peer *) l->data; + if (peer == NULL) { + print_error("Peer is empty ?"); + return; + } char * buff_str_ip[1024]; char * ip_str = (char * ) inet_ntop(AF_INET6,&peer->ip,(char * restrict) buff_str_ip, 1024); int last_seen = time(NULL) - peer->last_seen; diff --git a/src/debug.h b/src/debug.h index 9de638f..99f6811 100644 --- a/src/debug.h +++ b/src/debug.h @@ -4,7 +4,7 @@ #include "node.h" -#define DEBUG_LEVEL 0 +#define DEBUG_LEVEL 7 void welcome(); diff --git a/src/node.c b/src/node.c index f2e739c..acca9e8 100644 --- a/src/node.c +++ b/src/node.c @@ -1517,14 +1517,13 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ * sock_fd = socket(AF_INET6, SOCK_DGRAM, 0); if ( * sock_fd < 0) { print_error("Failed to open socket"); - perror(""); exit(-1); } /* Bind socket */ memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin6_family = AF_INET6; - // server_addr.sin6_addr.in6_addr = htonl(INADDR_ANY); + // server_addr.sin6_addr = INADDR_ANY; server_addr.sin6_port = LISTEN_PORT; if (bind( * sock_fd, (struct sockaddr *)(&server_addr), sizeof(server_addr)) < 0) { print_error("Failed to bind socket"); @@ -1532,26 +1531,62 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ exit(-1); } - /* Make the first peer*/ - struct neighbour_peer * root_peer = (struct neighbour_peer *) malloc(sizeof(struct neighbour_peer)); - time_t root_peer_seen = time(NULL); + print_debug(">> Adding the first root peers to the list..."); - int inet_p = inet_pton(AF_INET6, root_peer_ip, &root_peer->ip); - if(inet_p < 1){ - perror(">> Failed to create the root peer."); - return -3; - } + // Get the address via getaddrinfo. + struct addrinfo hints; + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_INET6; + hints.ai_socktype = SOCK_DGRAM; + hints.ai_protocol = 0; + hints.ai_flags = (AI_V4MAPPED | AI_ALL); // get back ipv4 mapped to ipv6 - root_peer->port = root_peer_port; - root_peer->is_temporary = 0; - root_peer->last_seen = root_peer_seen; + struct addrinfo *res; + print_debug(">> Resolving root peer name via getaddrinfo"); + int rc = getaddrinfo(root_peer_ip, "http", &hints, &res); + if(rc != 0) { + print_error("Failed to resolve hostname, exiting..."); + exit(1); + } - // TODO: Add the first peer to the neighbourhood - print_debug(">> Adding the first root peer to the list..."); neighbour_list = malloc(sizeof(struct list)); memset(neighbour_list, 0, sizeof(struct list)); - neighbour_list->data = (void *) root_peer; - neighbour_list->next = NULL; + + // For every address given to us by getaddrinfo, we create a new peer. + struct addrinfo *p; + struct neighbour_peer *peer ; + + 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); + + 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 ); + } + + list * tmp = neighbour_list; + + // 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->data != NULL){ + tmp = tmp->next; + } + tmp->data = node; + tmp->next = NULL; + + } + print_debug(">> Initializing data list..."); data_list = (list*) malloc(sizeof(list)); @@ -1565,6 +1600,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."); return 0; } diff --git a/src/node.h b/src/node.h index af8b526..3dfe7db 100644 --- a/src/node.h +++ b/src/node.h @@ -19,6 +19,7 @@ #include #include #include +#include /* la table de voisins, qui est indexée par adresses de socket (des paires (IP, Port)), * et dont chaque entrée contient un booléen indiquant si le pair est permanent From 44cf00432b5ae24c5d325b8b536e173d9326d416 Mon Sep 17 00:00:00 2001 From: n07070 Date: Mon, 4 May 2020 21:05:35 +0200 Subject: [PATCH 2/3] Fixed adding to list of peers. --- src/node.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/node.c b/src/node.c index acca9e8..54813e1 100644 --- a/src/node.c +++ b/src/node.c @@ -1556,6 +1556,7 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ struct addrinfo *p; struct neighbour_peer *peer ; + 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)); @@ -1571,7 +1572,7 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ printf("\x1b[31m[DEBUG]\x1b[0m >> Adding %s @ %i to peer list.\n", ip_str, root_peer_port ); } - list * tmp = neighbour_list; + // Add new peer to the list. list *node = (list*) malloc(sizeof(list)); @@ -1579,14 +1580,16 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ node->next = NULL; // Goint to the end of the list - while(tmp->data != NULL){ + while(tmp->next != NULL){ tmp = tmp->next; } - tmp->data = node; - tmp->next = NULL; - + tmp->next = node; } + neighbour_list = tmp; + + + print_peers(neighbour_list); print_debug(">> Initializing data list..."); data_list = (list*) malloc(sizeof(list)); From dcdcbd6435b7a9b2060fce50aae4662b27dbad1e Mon Sep 17 00:00:00 2001 From: n07070 Date: Tue, 5 May 2020 14:55:42 +0200 Subject: [PATCH 3/3] Removed a line --- src/node.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/node.c b/src/node.c index 54813e1..cb72406 100644 --- a/src/node.c +++ b/src/node.c @@ -1588,7 +1588,6 @@ int bootstrap_node(int * sock_fd, char * root_peer_ip, uint16_t root_peer_port){ neighbour_list = tmp; - print_peers(neighbour_list); print_debug(">> Initializing data list...");