Added ability to add via hostname
This commit is contained in:
parent
b85312bd97
commit
57d04eb1c3
@ -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;
|
||||
|
@ -4,7 +4,7 @@
|
||||
#include "node.h"
|
||||
|
||||
|
||||
#define DEBUG_LEVEL 0
|
||||
#define DEBUG_LEVEL 7
|
||||
|
||||
void welcome();
|
||||
|
||||
|
70
src/node.c
70
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
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
root_peer->port = root_peer_port;
|
||||
root_peer->is_temporary = 0;
|
||||
root_peer->last_seen = root_peer_seen;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <locale.h>
|
||||
#include <netdb.h>
|
||||
|
||||
/* 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
|
||||
|
Loading…
Reference in New Issue
Block a user