Commit 9f9cd40f authored by xpetrak2's avatar xpetrak2
Browse files

Entity 'peer' renamed into 'host'

parent 57677421
......@@ -6,13 +6,13 @@ src_coincerd_SOURCES = \
src/coincerd.c \
src/configuration.c src/configuration.h \
src/daemon_messages.h \
src/hosts.c src/hosts.h \
src/json_parser.c src/json_parser.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/paths.c src/paths.h
src_coincerd_CFLAGS = $(AM_CFLAGS) $(JANSSON_CFLAGS) $(LIBEVENT_CFLAGS) $(LIBSODIUM_CFLAGS)
src_coincerd_LDADD = $(AM_LDADD) $(JANSSON_LIBS) $(LIBEVENT_LIBS) $(LIBSODIUM_LIBS)
......
......@@ -23,11 +23,11 @@
#include <stdlib.h>
#include <time.h>
#include "hosts.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);
static void conns_cb(evutil_socket_t fd, short events, void *ctx);
......@@ -76,9 +76,9 @@ int main(void)
linkedlist_init(&global_state.pending_neighbours);
linkedlist_init(&global_state.neighbours);
linkedlist_init(&global_state.peers);
linkedlist_init(&global_state.hosts);
fetch_peers(global_state.filepaths.peers, &global_state.peers);
fetch_hosts(global_state.filepaths.hosts, &global_state.hosts);
/* setup everything needed for TCP listening */
if (listen_init(&listener, &global_state) != 0) {
......
......@@ -22,154 +22,155 @@
#include <stdlib.h>
#include <string.h>
#include "hosts.h"
#include "linkedlist.h"
#include "log.h"
#include "peers.h"
/**
* Delete all peers and their data.
* Delete all hosts and their data.
*
* @param peers Linkedlist of peers.
* @param hosts Linkedlist of hosts.
*/
void clear_peers(linkedlist_t *peers)
void clear_hosts(linkedlist_t *hosts)
{
/* peer_t has no dynamically allocated variables */
linkedlist_destroy(peers);
/* host_t has no dynamically allocated variables */
linkedlist_destroy(hosts);
}
/**
* Fetch peers from file into linkedlist.
* Fetch hosts from file into linkedlist.
*
* @param peers_path Path to peers file.
* @param peers Fetch loaded peers in here.
* @param hosts_path Path to hosts file.
* @param hosts Fetch loaded hosts in here.
*
* @return 0 Successfully fetched.
* @return 1 Peers file could not be opened.
* @return 1 Hosts file could not be opened.
*/
int fetch_peers(const char *peers_path, linkedlist_t *peers)
int fetch_hosts(const char *hosts_path, linkedlist_t *hosts)
{
struct in6_addr addr;
FILE *peers_file;
FILE *hosts_file;
peers_file = fopen(peers_path, "rb");
if (peers_file == NULL) {
log_warning("Peers file not found at %", peers_path);
hosts_file = fopen(hosts_path, "rb");
if (hosts_file == NULL) {
log_warn("Hosts file not found at %s. "
"It is safe to ignore this warning", hosts_path);
return 1;
}
while (fread(&addr, sizeof(struct in6_addr), 1, peers_file) == 1) {
save_peer(peers, &addr);
while (fread(&addr, sizeof(struct in6_addr), 1, hosts_file) == 1) {
save_host(hosts, &addr);
}
fclose(peers_file);
fclose(hosts_file);
return 0;
}
/**
* Fetch pointers to peers with specific flags set, into array that is being
* Fetch pointers to hosts with specific flags set, into array that is being
* allocated in here.
*
* @param peers All peers known to us.
* @param output Output array of pointers to satisfying peers.
* @param hosts All hosts known to us.
* @param output Output array of pointers to satisfying hosts.
* If set to NULL, function just returns
* the number of them.
* @param flags Choose peers based on these flags.
* Fetches output with all known peers if set to 0.
* @param flags Choose hosts based on these flags.
* Fetches output with all known hosts if set to 0.
*
* @return >=0 The number of satisfying peers.
* @return >=0 The number of satisfying hosts.
* @return -1 Allocation failure.
*/
int fetch_specific_peers(const linkedlist_t *peers,
peer_t ***output,
int fetch_specific_hosts(const linkedlist_t *hosts,
host_t ***output,
int flags)
{
linkedlist_node_t *current_node;
peer_t *current_peer;
host_t *current_host;
size_t n = 0;
if (output != NULL) {
*output = (peer_t **) malloc(linkedlist_size(peers) *
sizeof(peer_t *));
*output = (host_t **) malloc(linkedlist_size(hosts) *
sizeof(host_t *));
if (*output == NULL) {
log_error("Fetching specific peers");
log_error("Fetching specific hosts");
return -1;
}
}
current_node = linkedlist_get_first(peers);
current_node = linkedlist_get_first(hosts);
while (current_node != NULL) {
current_peer = (peer_t *) current_node->data;
/* if all specified flags are being set on this peer */
if ((current_peer->flags & flags) == flags) {
current_host = (host_t *) current_node->data;
/* if all specified flags are being set on this host */
if ((current_host->flags & flags) == flags) {
if (output != NULL) {
(*output)[n++] = current_peer;
(*output)[n++] = current_host;
} else {
n++;
}
}
current_node = linkedlist_get_next(peers, current_node);
current_node = linkedlist_get_next(hosts, current_node);
}
return n;
}
/**
* Find peer in the linkedlist by their address.
* Find host in the linkedlist by its address.
*
* @param peers Linkedlist of peers.
* @param addr Address of the peer we want.
* @param hosts Linkedlist of hosts.
* @param addr Address of the host we want.
*
* @return peer_t Requested peer.
* @return NULL Peer not found.
* @return host_t Requested host.
* @return NULL Host not found.
*/
peer_t *find_peer_by_addr(const linkedlist_t *peers,
const struct in6_addr *addr)
host_t *find_host(const linkedlist_t *hosts,
const struct in6_addr *addr)
{
const linkedlist_node_t *current = linkedlist_get_first(peers);
const linkedlist_node_t *current = linkedlist_get_first(hosts);
while (current != NULL) {
peer_t *current_data = (peer_t *) current->data;
host_t *current_data = (host_t *) current->data;
/* ip addresses match => requested peer found */
/* ip addresses match => requested host found */
if (memcmp(&current_data->addr, addr, 16) == 0) {
/* return node's data; struct s_peer */
/* return node's data; struct s_host */
return current_data;
}
current = linkedlist_get_next(peers, current);
current = linkedlist_get_next(hosts, current);
}
/* requested peer not found */
/* requested host not found */
return NULL;
}
/* TODO: allocate memory for the 'output', don't assume any buffer size */
/**
* '\n' separated output string of peer addresses in readable form.
* '\n' separated output string of host addresses in readable form.
*
* @param peers List of peers.
* @param hosts List of hosts.
* @param output Output string.
*/
void peers_to_str(const linkedlist_t *peers, char *output)
void hosts_to_str(const linkedlist_t *hosts, char *output)
{
linkedlist_node_t *current_node;
peer_t *current_peer;
host_t *current_host;
size_t output_size = 0;
char text_ip[INET6_ADDRSTRLEN];
output[0] = '\0';
current_node = linkedlist_get_first(peers);
current_node = linkedlist_get_first(hosts);
while (current_node != NULL) {
current_peer = (peer_t *) current_node->data;
current_host = (host_t *) current_node->data;
/* binary ip to text ip conversion */
inet_ntop(AF_INET6,
&current_peer->addr,
&current_host->addr,
text_ip,
INET6_ADDRSTRLEN);
output_size += strlen(text_ip);
strcat(output, text_ip);
current_node = linkedlist_get_next(peers, current_node);
/* if it's not the last peer, append '\n' */
current_node = linkedlist_get_next(hosts, current_node);
/* if it's not the last host, append '\n' */
if (current_node != NULL) {
output[output_size++] = '\n';
}
......@@ -178,166 +179,166 @@ void peers_to_str(const linkedlist_t *peers, char *output)
}
/**
* Save new peer into sorted linkedlist of peers.
* Save new host into sorted linkedlist of hosts.
*
* @param peers The linkedlist of peers.
* @param addr Address of the new peer.
* @param hosts The linkedlist of hosts.
* @param addr Address of the new host.
*
* @return peer_t Newly saved peer.
* @return NULL Peer is already saved, default or
* @return host_t Newly saved host.
* @return NULL Host is already saved, default or
* allocation failure.
*/
peer_t *save_peer(linkedlist_t *peers, const struct in6_addr *addr)
host_t *save_host(linkedlist_t *hosts, const struct in6_addr *addr)
{
int cmp_value;
struct in6_addr curr_addr;
linkedlist_node_t *current_node;
peer_t *current_peer;
host_t *current_host;
int i;
linkedlist_node_t *new_node;
peer_t *new_peer;
host_t *new_host;
char text_ip[INET6_ADDRSTRLEN];
/* save peer to the list of known peers, unless it's a default peer */
for (i = 0; i < DEFAULT_PEERS_SIZE; i++) {
memcpy(&curr_addr, DEFAULT_PEERS[i], 16);
/* it's a default peer, don't save it into 'peers' */
/* save host to the list of known hosts, unless it's a default host */
for (i = 0; i < DEFAULT_HOSTS_SIZE; i++) {
memcpy(&curr_addr, DEFAULT_HOSTS[i], 16);
/* it's a default host, don't save it into 'hosts' */
if (memcmp(&curr_addr, addr, 16) == 0) {
return NULL;
}
}
/* allocate memory for a new peer */
new_peer = (peer_t *) malloc(sizeof(peer_t));
if (new_peer == NULL) {
log_error("Saving new peer");
/* allocate memory for a new host */
new_host = (host_t *) malloc(sizeof(host_t));
if (new_host == NULL) {
log_error("Saving new host");
return NULL;
}
/* initialize all attributes of the new peer */
memcpy(&new_peer->addr, addr, 16);
set_peer_flags(new_peer, PEER_AVAILABLE);
/* initialize all attributes of the new host */
memcpy(&new_host->addr, addr, 16);
set_host_flags(new_host, HOST_AVAILABLE);
/* get textual representation of 'addr' */
inet_ntop(AF_INET6, addr, text_ip, INET6_ADDRSTRLEN);
/* insert 'new_peer' to its proper position in the sorted linkedlist;
* start from the last node of 'peers', as 'fetch_peers' (using this
/* insert 'new_host' to its proper position in the sorted linkedlist;
* start from the last node of 'hosts', as 'fetch_hosts' (using this
* function) is likely to save in ascending order => better performance
*/
current_node = linkedlist_get_last(peers);
current_node = linkedlist_get_last(hosts);
while (current_node != NULL) {
current_peer = (peer_t *) current_node->data;
current_host = (host_t *) current_node->data;
cmp_value = memcmp(&new_peer->addr, &current_peer->addr, 16);
/* the linkedlist already contains this peer */
cmp_value = memcmp(&new_host->addr, &current_host->addr, 16);
/* the linkedlist already contains this host */
if (cmp_value == 0) {
free(new_peer);
free(new_host);
return NULL;
} else if (cmp_value > 0) {
/* the proper position found */
new_node = linkedlist_insert_after(peers,
new_node = linkedlist_insert_after(hosts,
current_node,
new_peer);
new_host);
if (new_node != NULL) {
log_debug("save_peer - %s successfully saved",
log_debug("save_host - %s successfully saved",
text_ip);
}
return new_peer;
return new_host;
}
current_node = linkedlist_get_prev(peers, current_node);
current_node = linkedlist_get_prev(hosts, current_node);
}
/* the new peer's addr is lexicographically the lowest */
new_node = linkedlist_insert_after(peers, &peers->first, new_peer);
/* the new host's addr is lexicographically the lowest */
new_node = linkedlist_insert_after(hosts, &hosts->first, new_host);
if (new_node != NULL) {
log_debug("save_peer - %s successfully saved", text_ip);
log_debug("save_host - %s successfully saved", text_ip);
}
return new_peer;
return new_host;
}
/**
* Set flags on given peer.
* Set flags on given host.
*
* @param peer Set flags on this peer.
* @param flags Set these flags on the peer.
* @param host Set flags on this host.
* @param flags Set these flags on the host.
*/
void set_peer_flags(peer_t *peer, int flags)
void set_host_flags(host_t *host, int flags)
{
peer->flags |= flags;
host->flags |= flags;
}
/**
* Shuffle an input array of peers.
* Shuffle an input array of hosts.
*
* @param peers The array of peers to be shuffled.
* @param peers_size The number of peers to be shuffled.
* @param hosts The array of hosts to be shuffled.
* @param hosts_size The number of hosts to be shuffled.
*/
void shuffle_peers_arr(peer_t **peers, size_t peers_size)
void shuffle_hosts_arr(host_t **hosts, size_t hosts_size)
{
size_t i, j;
peer_t *tmp;
host_t *tmp;
for (i = 0; i < peers_size; i++) {
for (i = 0; i < hosts_size; i++) {
/* don't swap with already swapped;
* swapping peers[i] with peers[j], where j is a random index
* such that j >= i AND j < peers_size
* swapping hosts[i] with hosts[j], where j is a random index
* such that j >= i AND j < hosts_size
*/
j = i + rand() % (peers_size - i);
j = i + rand() % (hosts_size - i);
tmp = peers[i];
peers[i] = peers[j];
peers[j] = tmp;
tmp = hosts[i];
hosts[i] = hosts[j];
hosts[j] = tmp;
}
}
/**
* Store peers from a linkedlist into a file.
* Store hosts from a linkedlist into a file.
*
* @param peers_path Path to 'peers' file.
* @param peers Linkedlist of the peers to be stored.
* @param hosts_path Path to 'hosts' file.
* @param hosts Linkedlist of the hosts to be stored.
*
* @return 0 Successfully stored.
* @return 1 Failure.
*/
int store_peers(const char *peers_path, const linkedlist_t *peers)
int store_hosts(const char *hosts_path, const linkedlist_t *hosts)
{
const linkedlist_node_t *current;
const peer_t *current_peer;
FILE *peers_file;
const host_t *current_host;
FILE *hosts_file;
peers_file = fopen(peers_path, "wb");
if (peers_file == NULL) {
log_error("Can not create peers file at %s", peers_path);
hosts_file = fopen(hosts_path, "wb");
if (hosts_file == NULL) {
log_error("Can not create hosts file at %s", hosts_path);
return 1;
}
current = linkedlist_get_first(peers);
current = linkedlist_get_first(hosts);
while (current != NULL) {
current_peer = (peer_t *) current->data;
current_host = (host_t *) current->data;
/* if fwrite fails, terminate storing */
if (fwrite(&current_peer->addr,
if (fwrite(&current_host->addr,
sizeof(struct in6_addr),
1,
peers_file) != 1) {
log_error("Storing peers");
fclose(peers_file);
hosts_file) != 1) {
log_error("Storing hosts");
fclose(hosts_file);
return 1;
}
current = linkedlist_get_next(peers, current);
current = linkedlist_get_next(hosts, current);
}
fclose(peers_file);
fclose(hosts_file);
return 0;
}
/**
* Unset flags on given peer.
* Unset flags on given host.
*
* @param peer Unset flags on this peer.
* @param flags Unset these flags on the peer.
* @param host Unset flags on this host.
* @param flags Unset these flags on the host.
*/
void unset_peer_flags(peer_t *peer, int flags)
void unset_host_flags(host_t *host, int flags)
{
peer->flags &= ~flags;
host->flags &= ~flags;
}
......@@ -16,28 +16,28 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PEERS_H
#define PEERS_H
#ifndef HOSTS_H
#define HOSTS_H
#include <arpa/inet.h>
#include <stdint.h>
#include "linkedlist.h"
/* number of peers guaranteed to be in the network */
#define DEFAULT_PEERS_SIZE 2
/* maximum number of peers we store */
#define MAX_PEERS_SIZE 50
/* number of hosts guaranteed to be in the network */
#define DEFAULT_HOSTS_SIZE 2
/* maximum number of hosts we store */
#define MAX_HOSTS_SIZE 50
/** A peer is not available if they is already our neighbour,
/** A host is not available if they is already our neighbour,
* pending to become one, or if we are unnable to connect to them.
* 1 if available, 0 if not.
*/
#define PEER_AVAILABLE 0x01
#define HOST_AVAILABLE 0x01
/* IPv6 addresses of peers guaranteed to be in the network */
static const unsigned char DEFAULT_PEERS[DEFAULT_PEERS_SIZE][16] = {
/* TODO: Replace with real default peers */
/* IPv6 addresses of hosts guaranteed to be in the network */
static const unsigned char DEFAULT_HOSTS[DEFAULT_HOSTS_SIZE][16] = {
/* TODO: Replace with real default hosts */
{
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
......@@ -52,38 +52,38 @@ static const unsigned char DEFAULT_PEERS[DEFAULT_PEERS_SIZE][16] = {
}
};
/** Peer info holder. */
typedef struct s_peer {
/** Host info holder. */
typedef struct s_host {
/** Binary IPv6 address. */
struct in6_addr addr;
/** A set of flags for this peer. */
/** A set of flags for this host. */
int flags;
/* TODO: add uptime */
} peer_t;
} host_t;
void clear_peers(linkedlist_t *peers);
void clear_hosts(linkedlist_t *hosts);
int fetch_peers(const char *peers_path, linkedlist_t *peers);
int fetch_hosts(const char *hosts_path, linkedlist_t *hosts);
int fetch_specific_peers(const linkedlist_t *peers,
peer_t ***output,
int fetch_specific_hosts(const linkedlist_t *hosts,
host_t ***output,
int flags);
peer_t *find_peer_by_addr(const linkedlist_t *peers,
const struct in6_addr *addr);
host_t *find_host(const linkedlist_t *hosts,
const struct in6_addr *addr);
void peers_to_str(const linkedlist_t *peers, char *output);
void hosts_to_str(const linkedlist_t *hosts, char *output);
void reset_peers_availability(linkedlist_t *peers);
void reset_hosts_availability(linkedlist_t *hosts);
peer_t *save_peer(linkedlist_t *peers, const struct in6_addr *addr);
host_t *save_host(linkedlist_t *hosts, const struct in6_addr *addr);
void set_peer_flags(peer_t *peer, int flags);
void set_host_flags(host_t *host, int flags);
void shuffle_peers_arr(peer_t **peers, size_t peers_size);
void shuffle_hosts_arr(host_t **hosts, size_t hosts_size);
int store_peers(const char *peers_path, const linkedlist_t *peers);
int store_hosts(const char *hosts_path, const linkedlist_t *hosts);
void unset_peer_flags(peer_t *peer, int flags);
void unset_host_flags(host_t *host, int flags);
#endif /* PEERS_H */
#endif /* HOSTS_H */
......@@ -21,20 +21,19 @@
#include <arpa/inet.h> /* inet_ntop */
#include <assert.h>
#include <errno.h>
#include <event2/listener.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <event2/bufferevent.h>
#include <event2/listener.h>
#include <netinet/in.h> /* sockaddr_in6 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* sleep */
#include "hosts.h"
#include "linkedlist.h"
#include "log.h"
#include "neighbours.h"
#include "p2p.h"
#include "peers.h"
/**
* Simple helper for conversion of binary IP to readable IP address.
......@@ -48,7 +47,7 @@ static void ip_to_string(const struct in6_addr *binary_ip, char *ip)
}
/**
* Ask the neighbour for a list of addresses.
* Ask a neighbour for a list of addresses.
<