Commit 2c541eb3 authored by xpetrak2's avatar xpetrak2
Browse files

A few adjustments

parent 0b811dce
......@@ -5,11 +5,11 @@ bin_PROGRAMS = src/coincerd src/coincer
src_coincerd_SOURCES = \
src/coincerd.c \
src/configuration.c src/configuration.h \
src/filing.c src/filing.h \
src/linkedlist.c src/linkedlist.h \
src/log.c src/log.h \
src/neighbours.c src/neighbours.h \
src/p2p.c src/p2p.h \
src/paths.c src/paths.h \
src/peers.c src/peers.h
src_coincerd_CFLAGS = $(AM_CFLAGS) $(JANSSON_CFLAGS) $(LIBEVENT_CFLAGS) $(LIBSODIUM_CFLAGS)
......
/*
* Coincer
* Copyright (C) 2017 Coincer Developers
* Copyright (C) 2017-2018 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -22,10 +22,10 @@
#include <stdlib.h>
#include <time.h>
#include "filing.h"
#include "log.h"
#include "neighbours.h"
#include "p2p.h"
#include "paths.h"
#include "peers.h"
static void signal_cb(evutil_socket_t fd, short events, void *ctx);
......@@ -122,8 +122,6 @@ int main(void)
event_free(conns_event);
event_base_free(global_state.event_loop);
printf("\nCoincer says: Bye!\n");
return 0;
}
......@@ -131,7 +129,7 @@ int main(void)
* Callback function for a received signal.
*
* @param signal What signal was invoked.
* @param events Flags of the event occured.
* @param events Flags of the event occurred.
* @param ctx Global state.
*/
static void signal_cb(evutil_socket_t signal __attribute__((unused)),
......@@ -160,11 +158,9 @@ static void conns_cb(int fd __attribute__((unused)),
needed_conns = MIN_NEIGHBOURS -
linkedlist_size(&global_state->neighbours);
if (needed_conns > 0) {
/* ask twice more peers than we need; it's preferable
* to have more neighbours than minimum
*/
log_debug("conns_cb - we need %d more neighbours");
/* ask twice more peers than we need; it's preferable
* to have more neighbours than minimum */
add_more_connections(global_state, 2 * needed_conns);
}
}
/*
* Coincer
* Copyright (C) 2017 Coincer Developers
* Copyright (C) 2017-2018 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -195,4 +195,3 @@ int setup_directories(char **config_dir, char **data_dir)
return 0;
}
/*
* Coincer
* Copyright (C) 2017 Coincer Developers
* Copyright (C) 2017-2018 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -22,4 +22,3 @@
int setup_directories(char **config_dir, char **data_dir);
#endif /* CONFIGURATION_H */
/*
* Coincer
* Copyright (C) 2017 Coincer Developers
* Copyright (C) 2017-2018 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -194,7 +194,8 @@ void linkedlist_init(linkedlist_t *root)
}
/**
* Insert new node into linked list after 'node'.
* Insert new node into linkedlist after 'node'. In case of 'node' being the
* last (stub) node, insert before it.
*
* @param root Root of the linked list.
* @param node Insert new node right after this node.
......@@ -230,7 +231,8 @@ linkedlist_node_t *linkedlist_insert_after(linkedlist_t *root,
}
/**
* Insert new node into linkedlist before 'node'.
* Insert new node into linkedlist before 'node'. In case of 'node' being
* the first (stub) node, insert after it.
*
* @param root Root of the linked list.
* @param node Insert new node right before this node.
......@@ -269,4 +271,3 @@ size_t linkedlist_size(const linkedlist_t *root)
return n;
}
/*
* Coincer
* Copyright (C) 2017 Coincer Developers
* Copyright (C) 2017-2018 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -69,4 +69,3 @@ linkedlist_node_t *linkedlist_insert_before(linkedlist_t *root,
size_t linkedlist_size(const linkedlist_t *root);
#endif /* LINKEDLIST_H */
/*
* Coincer
* Copyright (C) 2017 Coincer Developers
* Copyright (C) 2017-2018 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -99,4 +99,3 @@ void log_error(const char *msg, ...)
fprintf(stderr, "\n");
}
/*
* Coincer
* Copyright (C) 2017 Coincer Developers
* Copyright (C) 2017-2018 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -25,4 +25,3 @@ void log_warn(const char *msg, ...);
void log_error(const char *msg, ...);
#endif /* LOG_H */
......@@ -18,6 +18,7 @@
#include <assert.h>
#include <event2/bufferevent.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
......@@ -26,71 +27,6 @@
#include "log.h"
#include "neighbours.h"
/**
* Find neighbour in neighbours based on their bufferevent.
*
* @param neighbours Our neighbours.
* @param bev Neighbour's bufferevent.
*
* @return neighbour_t Requested neighbour.
* @return NULL If not found.
*/
neighbour_t *find_neighbour(const linkedlist_t *neighbours,
const struct bufferevent *bev)
{
/* start the search from the first linked list node */
const linkedlist_node_t *current = linkedlist_get_first(neighbours);
while (current != NULL) {
/* data of the 'current' node; struct s_neighbour */
neighbour_t *current_data = (neighbour_t *) current->data;
/* bufferevents equal => neighbour found */
if (current_data->buffer_event == bev) {
/* return node's data; struct s_neighbour */
return current_data;
}
/* get next node in the linked list */
current = linkedlist_get_next(neighbours, current);
}
/* neighbour not found */
return NULL;
}
/**
* Find neighbour in neighbours based on their addr.
*
* @param neighbours Our neighbours.
* @param addr Binary ip address stored in 16 bytes.
*
* @return neighbour_t Requested neighbour.
* @return NULL If not found.
*/
neighbour_t *find_neighbour_by_addr(const linkedlist_t *neighbours,
const struct in6_addr *addr)
{
/* start the search from the first linked list node */
const linkedlist_node_t *current = linkedlist_get_first(neighbours);
while (current != NULL) {
/* data of the 'current' node; struct s_neighbour */
neighbour_t *current_data = (neighbour_t *) current->data;
/* ip addresses match => neighbour found */
if (memcmp(&current_data->addr, addr, 16) == 0) {
/* return node's data; struct s_neighbour */
return current_data;
}
/* get next node in the linked list */
current = linkedlist_get_next(neighbours, current);
}
/* neighbour not found */
return NULL;
}
/**
* Add new neighbour into neighbours.
*
......@@ -137,6 +73,33 @@ neighbour_t *add_new_neighbour(linkedlist_t *neighbours,
return new_neighbour;
}
/**
* Delete all neighbours.
*
* @param neighbours Linked list of our neighbours.
*/
void clear_neighbours(linkedlist_t *neighbours)
{
neighbour_t *current;
linkedlist_node_t *current_node;
current_node = linkedlist_get_first(neighbours);
/* safely delete data from the linked list nodes */
while (current_node != NULL) {
/* load current node's data into 'current' */
current = (neighbour_t *) current_node->data;
/* deallocate neighbour's bufferevent */
bufferevent_free(current->buffer_event);
/* get next node in the linked list */
current_node = linkedlist_get_next(neighbours, current_node);
}
/* safely delete all nodes and their data in the linked list */
linkedlist_destroy(neighbours);
}
/**
* Delete neighbour from neighbours.
*
......@@ -165,29 +128,64 @@ void delete_neighbour(linkedlist_t *neighbours, struct bufferevent *bev)
}
/**
* Delete all neighbours.
* Find neighbour in neighbours based on their bufferevent.
*
* @param neighbours Linked list of our neighbours.
* @param neighbours Our neighbours.
* @param bev Neighbour's bufferevent.
*
* @return neighbour_t Requested neighbour.
* @return NULL If not found.
*/
void clear_neighbours(linkedlist_t *neighbours)
neighbour_t *find_neighbour(const linkedlist_t *neighbours,
const struct bufferevent *bev)
{
neighbour_t *current;
linkedlist_node_t *current_node;
/* start the search from the first linked list node */
const linkedlist_node_t *current = linkedlist_get_first(neighbours);
current_node = linkedlist_get_first(neighbours);
/* safely delete data from the linked list nodes */
while (current_node != NULL) {
/* load current node's data into 'current' */
current = (neighbour_t *) current_node->data;
while (current != NULL) {
/* data of the 'current' node; struct s_neighbour */
neighbour_t *current_data = (neighbour_t *) current->data;
/* deallocate neighbour's bufferevent */
bufferevent_free(current->buffer_event);
/* bufferevents equal => neighbour found */
if (current_data->buffer_event == bev) {
/* return node's data; struct s_neighbour */
return current_data;
}
/* get next node in the linked list */
current_node = linkedlist_get_next(neighbours, current_node);
current = linkedlist_get_next(neighbours, current);
}
/* safely delete all nodes and their data in the linked list */
linkedlist_destroy(neighbours);
/* neighbour not found */
return NULL;
}
/**
* Find neighbour in neighbours based on their addr.
*
* @param neighbours Our neighbours.
* @param addr Binary ip address stored in 16 bytes.
*
* @return neighbour_t Requested neighbour.
* @return NULL If not found.
*/
neighbour_t *find_neighbour_by_addr(const linkedlist_t *neighbours,
const struct in6_addr *addr)
{
/* start the search from the first linked list node */
const linkedlist_node_t *current = linkedlist_get_first(neighbours);
while (current != NULL) {
/* data of the 'current' node; struct s_neighbour */
neighbour_t *current_data = (neighbour_t *) current->data;
/* ip addresses match => neighbour found */
if (memcmp(&current_data->addr, addr, 16) == 0) {
/* return node's data; struct s_neighbour */
return current_data;
}
/* get next node in the linked list */
current = linkedlist_get_next(neighbours, current);
}
/* neighbour not found */
return NULL;
}
......@@ -26,32 +26,32 @@
#include "linkedlist.h"
/* minimum number of peers we need to be connected to */
#define MIN_NEIGHBOURS 3
#define MIN_NEIGHBOURS 3
/** Data type for the linkedlist of neighbours. */
typedef struct s_neighbour {
/**< Neighbours's IPv6 address;
* also allows storing IPv4-mapped IPv6 addresses. */
struct in6_addr addr;
/**< Bufferevent belonging to this neighbour. */
struct bufferevent *buffer_event;
/**< Number of failed ping attempts -- max 3, then disconnect. */
size_t failed_pings;
/** Neighbour's IPv6 address.
* Also allows storing IPv4-mapped IPv6 addresses. */
struct in6_addr addr;
/** Bufferevent belonging to this neighbour. */
struct bufferevent *buffer_event;
/** Number of failed ping attempts -- max 3, then disconnect. */
size_t failed_pings;
} neighbour_t;
neighbour_t *find_neighbour(const linkedlist_t *neighbours,
const struct bufferevent *bev);
neighbour_t *find_neighbour_by_addr(const linkedlist_t *neighbours,
const struct in6_addr *ip_addr);
neighbour_t *add_new_neighbour(linkedlist_t *neighbours,
const struct in6_addr *ip_addr,
struct bufferevent *bev);
void clear_neighbours(linkedlist_t *neighbours);
void delete_neighbour(linkedlist_t *neighbours, struct bufferevent *bev);
void clear_neighbours(linkedlist_t *neighbours);
#endif /* NEIGHBOURS_H */
neighbour_t *find_neighbour(const linkedlist_t *neighbours,
const struct bufferevent *bev);
neighbour_t *find_neighbour_by_addr(const linkedlist_t *neighbours,
const struct in6_addr *ip_addr);
#endif /* NEIGHBOURS_H */
/*
* Coincer
* Copyright (C) 2017 Coincer Developers
* Copyright (C) 2017-2018 Coincer Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
......@@ -16,7 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _POSIX_SOURCE /* strtok_r */
#include <arpa/inet.h> /* inet_ntop */
......@@ -63,8 +62,7 @@ void ask_for_peers(neighbour_t *neighbour)
}
/* send message "peers" to the neighbour, as a request
* for the list of peers; 6 is the number of bytes to be transmitted
*/
* for the list of peers; 6 is the number of bytes to be transmitted */
evbuffer_add(bufferevent_get_output(bev), "peers", 6);
}
......@@ -85,7 +83,7 @@ static void process_peers(global_state_t *global_state, char *peers)
while (line != NULL) {
if (inet_pton(AF_INET6, line, &addr) == 1) {
add_peer(&global_state->peers, &addr);
save_peer(&global_state->peers, &addr);
}
line = strtok_r(NULL, delim, &save_ptr);
}
......@@ -180,17 +178,15 @@ static void timeout_process(linkedlist_t *neighbours,
/* the neighbour hasn't failed enough pings to be deleted */
if (neighbour->failed_pings < 3) {
/* bufferevent was disabled when timeout flag was set */
bufferevent_enable(neighbour->buffer_event,
EV_READ | EV_WRITE | EV_TIMEOUT);
log_debug("timeout_process - sending ping to %s."
" Failed pings: %lu", text_ip,
neighbour->failed_pings);
log_debug("timeout_process - sending ping to %s. "
"Failed pings: %lu", text_ip,
neighbour->failed_pings);
/* send ping to the inactive neighbour;
* 5 is the length of bytes to be transmitted
*/
* 5 is the length of bytes to be transmitted */
evbuffer_add(bufferevent_get_output(neighbour->buffer_event),
"ping", 5);
......@@ -204,12 +200,12 @@ static void timeout_process(linkedlist_t *neighbours,
}
/**
* Delete 'neighbour' from 'pending_neighbours' and add it into 'neighbours'.
* Delete neighbour from pending and add it into neighbours.
*
* @param global_state Global state.
* @param neighbour Neighbour to be moved.
*
* @param neighbour_t The new neighbour in the 'neighbours'.
* @param neighbour_t The new neighbour added into neighbours.
* @param NULL If adding failed.
*/
static neighbour_t *move_neighbour_from_pending(global_state_t *global_state,
......@@ -233,11 +229,11 @@ static neighbour_t *move_neighbour_from_pending(global_state_t *global_state,
}
/**
* Process the event that occured on our pending neighbour 'neighbour'.
* Process the event that occurred on our pending neighbour.
*
* @param global_state Global state.
* @param neighbour The event occured on this pending neighbour.
* @param events What event occured.
* @param neighbour The event occurred on this pending neighbour.
* @param events What event occurred.
*/
static void process_pending_neighbour(global_state_t *global_state,
neighbour_t *neighbour,
......@@ -254,7 +250,7 @@ static void process_pending_neighbour(global_state_t *global_state,
ip_to_string(&neighbour->addr, text_ip);
/* we've successfully connected to the neighbour */
if (events & (BEV_EVENT_CONNECTED)) {
if (events & BEV_EVENT_CONNECTED) {
log_info("process_pending_neighbour - connecting to "
"%s was SUCCESSFUL", text_ip);
/* we've got a new neighbour;
......@@ -272,8 +268,8 @@ static void process_pending_neighbour(global_state_t *global_state,
/* if we need more neighbours */
if (needed_conns > 0) {
/* and we don't have enough available */
if ((int)available_peers_size < needed_conns) {
ask_for_peers(new_neighbour);
if ((int) available_peers_size < needed_conns) {
ask_for_addresses(new_neighbour);
}
}
/* connecting to the neighbour was unsuccessful */
......@@ -282,16 +278,16 @@ static void process_pending_neighbour(global_state_t *global_state,
"%s was NOT SUCCESSFUL", text_ip);
/* the peer is no longer a pending neighbour */
delete_neighbour(&global_state->pending_neighbours,
neighbour->buffer_event);
neighbour->buffer_event);
}
}
/**
* Process the event that occured on our neighbour 'neighbour'.
* Process the event that occurred on our neighbour.
*
* @param global_state Global state.
* @param neighbour The event occured on this neighbour.
* @param events What event occured.
* @param neighbour The event occurred on this neighbour.
* @param events What event occurred.
*/
static void process_neighbour(global_state_t *global_state,
neighbour_t *neighbour,
......@@ -307,7 +303,7 @@ static void process_neighbour(global_state_t *global_state,
text_ip);
delete_neighbour(&global_state->neighbours,
neighbour->buffer_event);
} else if (events & (BEV_EVENT_EOF)) {
} else if (events & BEV_EVENT_EOF) {
log_info("process_neighbour - %s disconnected", text_ip);
delete_neighbour(&global_state->neighbours,
neighbour->buffer_event);
......@@ -320,8 +316,8 @@ static void process_neighbour(global_state_t *global_state,
/**
* Callback for bufferevent event detection.
*
* @param bev bufferevent on which the event occured.
* @param events Flags of the events occured.
* @param bev bufferevent on which the event occurred.
* @param events Flags of the events occurred.
* @param ctx Pointer to global_state_t to determine the neighbour.
*/
static void event_cb(struct bufferevent *bev, short events, void *ctx)
......@@ -337,9 +333,8 @@ static void event_cb(struct bufferevent *bev, short events, void *ctx)
} else {
neighbour = find_neighbour(&global_state->pending_neighbours,
bev);
/* 'bev' must belong either to 'neighbours' or
* 'pending_neighbours'
*/
/* 'bev' must belong to either 'neighbours' or
* 'pending_neighbours' */
assert(neighbour != NULL);
process_pending_neighbour(global_state, neighbour, events);
}
......@@ -387,8 +382,7 @@ static void accept_connection(struct evconnlistener *listener,
bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
/* subscribe every received P2P message to be processed;
* p2p_process for read callback, NULL for write callback
*/
* p2p_process for read callback, NULL for write callback */
bufferevent_setcb(bev, p2p_process, NULL, event_cb, ctx);
bufferevent_enable(bev, EV_READ | EV_WRITE | EV_TIMEOUT);
......@@ -403,8 +397,6 @@ static void accept_connection(struct evconnlistener *listener,
if (!add_new_neighbour(&global_state->neighbours,
new_addr,
bev)) {
/* free the bufferevent if adding failed */
log_debug("accept_connection - adding failed");
bufferevent_free(bev);
return;
......@@ -412,13 +404,13 @@ static void accept_connection(struct evconnlistener *listener,
log_info("accept_connection - new connection from [%s]:%d", text_ip,
ntohs(addr_in6->sin6_port));
add_peer(&global_state->peers, new_addr);
save_peer(&global_state->peers, new_addr);
}
/**
* Callback for listener error detection.
*
* @param listener Listener on which the error occured.
* @param listener Listener on which the error occurred.
* @param ctx Pointer to global_state_t.
*/
static void accept_error_cb(struct evconnlistener *listener,
......@@ -444,8 +436,8 @@ static void accept_error_cb(struct evconnlistener *listener,
* @param listener The event loop listener.
* @param global_state Data for the event loop to work with.
*
* @return 0 if successfully initialized.
* @return 1 if an error occured.
* @return 0 If successfully initialized.
* @return 1 If an error occurred.
*/
int listen_init(struct evconnlistener **listener,
global_state_t *global_state)
......@@ -485,12 +477,12 @@ int listen_init(struct evconnlistener **listener,
}
/**
* Attempt to connect to the particular addr.
* Attempt to connect to a particular addr.
*
* @param global_state Data for the event loop to work with.
* @param addr Binary IP of a peer that we want to connect to.
*
* @return 0 The connection attempt was succesful.
* @return 0 The connection attempt was successful.
* @return 1 The peer is already our neighbour.
* @return 2 The peer is pending to become our neighbour.
* @return 3 Adding new pending neighbour unsuccessful.
......@@ -535,8 +527,7 @@ int connect_to_addr(global_state_t *global_state,
BEV_OPT_CLOSE_ON_FREE);
/* subscribe every received P2P message to be processed;
* p2p_process as read callback, NULL as write callback
*/
* p2p_process as read callback, NULL as write callback */
bufferevent_setcb(bev,
p2p_process,
NULL,
......@@ -554,8 +545,7 @@ int connect_to_addr(global_state_t *global_state,
bufferevent_set_timeouts(bev, &timeout, &timeout);
/* add peer to the list of pending neighbours and let event_cb
* determine whether the peer is our neighbour now
*/
* determine whether the peer is our neighbour now */
if (!add_new_neighbour(&global_state->pending_neighbours, addr, bev)) {
log_debug("connect_to_addr - neighbour %s NOT ADDED into "
"pending neighbours", text_ip);
......@@ -602,7 +592,7 @@ void add_more_connections(global_state_t *global_state, size_t conns_amount)
log_debug("add_more_connections - "
"choosing random default peer");
/* choose random default peer */
idx = rand()%DEFAULT_PEERS_SIZE;
idx = rand() % DEFAULT_PEERS_SIZE;
memcpy(&addr, DEFAULT_PEERS[idx], 16);
result = connect_to_addr(global_state, &addr);
......@@ -617,9 +607,7 @@ void add_more_connections(global_state_t *global_state, size_t conns_amount)
*/
log_debug("add_more_connections - "
"connect attempt succeeded");
/* the peer is our neighbour;
* ask them for more peers
*/
/* the peer is our neighbour; ask them for more addrs */