Commit 978d3a36 authored by xpetrak2's avatar xpetrak2
Browse files

Peers representation, extended linkedlist

* neighbours, peers and linked list functions in alphabetical order
* more functions in the linked list
* the peers are being represented by their own structure
* linkedlist of pending_neighbours and peers for the next commit
parent 07d01c79
...@@ -9,7 +9,8 @@ src_coincerd_SOURCES = \ ...@@ -9,7 +9,8 @@ src_coincerd_SOURCES = \
src/linkedlist.c src/linkedlist.h \ src/linkedlist.c src/linkedlist.h \
src/log.c src/log.h \ src/log.c src/log.h \
src/neighbours.c src/neighbours.h \ src/neighbours.c src/neighbours.h \
src/p2p.c src/p2p.h src/p2p.c src/p2p.h \
src/peers.c src/peers.h
src_coincerd_CFLAGS = $(AM_CFLAGS) $(JANSSON_CFLAGS) $(LIBEVENT_CFLAGS) $(LIBSODIUM_CFLAGS) src_coincerd_CFLAGS = $(AM_CFLAGS) $(JANSSON_CFLAGS) $(LIBEVENT_CFLAGS) $(LIBSODIUM_CFLAGS)
src_coincerd_LDADD = $(AM_LDADD) $(JANSSON_LIBS) $(LIBEVENT_LIBS) $(LIBSODIUM_LIBS) src_coincerd_LDADD = $(AM_LDADD) $(JANSSON_LIBS) $(LIBEVENT_LIBS) $(LIBSODIUM_LIBS)
......
...@@ -20,30 +20,96 @@ ...@@ -20,30 +20,96 @@
#include <stdlib.h> #include <stdlib.h>
#include "linkedlist.h" #include "linkedlist.h"
#include "log.h"
/** /**
* Initializes linked list to default values. * Append new node at the end of the linked list.
* *
* @param root Linked list to initialize. * @param root Root of the linked list.
* @param data Data of the new node.
*
* @return linkedlist_node_t Pointer to created node if succeeded.
* @return NULL If failure.
*/ */
void linkedlist_init(linkedlist_t *root) linkedlist_node_t *linkedlist_append(linkedlist_t *root, void *data)
{ {
root->first.prev = NULL; return linkedlist_insert_after(root,
root->first.next = &root->last; root->last.prev,
root->last.prev = &root->first; data);
root->last.next = NULL; }
root->first.data = NULL; /**
root->last.data = NULL; * Delete node from the linked list.
*
* @param node Node to be deleted from the linked list.
*/
void linkedlist_delete(linkedlist_node_t *node)
{
/* fix the links of neighbour nodes */
node->prev->next = node->next;
node->next->prev = node->prev;
/* node's data deletion part */
if (node->data != NULL) {
free(node->data);
}
/* node deletion part */
free(node);
} }
/** /**
* Get first node of the linked list. * Destroys a linked list.
* *
* @param root Root of the linked list. * @param root Root of the linked list to destroy.
*/
void linkedlist_destroy(linkedlist_t *root)
{
linkedlist_node_t *tmp;
tmp = root->first.next;
while (tmp->next != NULL) {
tmp = tmp->next;
if (tmp->prev->data != NULL) {
free(tmp->prev->data);
}
free(tmp->prev);
}
}
/**
* Find node in the linked list by data.
* *
* @return First node of the linked list. * @param root Root of the linked list.
* @return NULL if linkedlist is empty. * @param data Data of the requested node.
*
* @return linkedlist_node_t Node containing 'data'.
* @return NULL If the node doesn't exist.
*/
linkedlist_node_t *linkedlist_find(const linkedlist_t *root, const void *data)
{
/* start the search from the first node of the linked list */
linkedlist_node_t *current = linkedlist_get_first(root);
while (current != NULL) {
/* node found */
if (current->data == data) {
/* return the node containing 'data' */
return current;
}
/* move to the next node and continue with the search */
current = linkedlist_get_next(root, current);
}
/* node not found */
return NULL;
}
/**
* Get the first node of the linked list.
*
* @param root Root of the linked list.
*
* @return linkedlist_node_t First node of the linked list.
* @return NULL If the linkedlist is empty.
*/ */
linkedlist_node_t *linkedlist_get_first(const linkedlist_t *root) linkedlist_node_t *linkedlist_get_first(const linkedlist_t *root)
{ {
...@@ -55,12 +121,12 @@ linkedlist_node_t *linkedlist_get_first(const linkedlist_t *root) ...@@ -55,12 +121,12 @@ linkedlist_node_t *linkedlist_get_first(const linkedlist_t *root)
} }
/** /**
* Get last node of the linkedlist. * Get the last node of the linkedlist.
* *
* @param root Root of the linked list. * @param root Root of the linked list.
* *
* @return Last node of the linked list. * @return linkedlist_node_t Last node of the linked list.
* @return NULL if linkedlist is empty. * @return NULL If linkedlist is empty.
*/ */
linkedlist_node_t *linkedlist_get_last(const linkedlist_t *root) linkedlist_node_t *linkedlist_get_last(const linkedlist_t *root)
{ {
...@@ -72,13 +138,14 @@ linkedlist_node_t *linkedlist_get_last(const linkedlist_t *root) ...@@ -72,13 +138,14 @@ linkedlist_node_t *linkedlist_get_last(const linkedlist_t *root)
} }
/** /**
* Get node that is right after 'node' in the linked list. * Get a node that is right after 'node' in the linked list.
* *
* @param root Root of the linked list. * @param root Root of the linked list.
* @param node We want node that is right after this node. * @param node A node right after this node.
* *
* @return The node we requested. * @return linkedlist_node_t The node we requested.
* @return NULL if 'node' was last in the linked list or NULL. * @return NULL If 'node' was the last or 'root'
* is empty.
*/ */
linkedlist_node_t *linkedlist_get_next(const linkedlist_t *root, linkedlist_node_t *linkedlist_get_next(const linkedlist_t *root,
const linkedlist_node_t *node) const linkedlist_node_t *node)
...@@ -91,110 +158,115 @@ linkedlist_node_t *linkedlist_get_next(const linkedlist_t *root, ...@@ -91,110 +158,115 @@ linkedlist_node_t *linkedlist_get_next(const linkedlist_t *root,
} }
/** /**
* Destroys a linked list. * Get a node that is right before 'node' in the linked list.
* *
* @param root Root of the linked list to destroy. * @param root Root of the linked list.
* @param node A node right before this node.
*
* @return linkedlist_node The node we requested.
* @return NULL If 'node' was the first or 'root'
* is empty.
*/ */
void linkedlist_destroy(linkedlist_t *root) linkedlist_node_t *linkedlist_get_prev(const linkedlist_t *root,
const linkedlist_node_t *node)
{ {
linkedlist_node_t *tmp; if (node == NULL || node->prev == &root->first) {
return NULL;
tmp = root->first.next;
while (tmp->next != NULL) {
tmp = tmp->next;
free(tmp->prev);
} }
return node->prev;
} }
/** /**
* Append new node at the end of the linked list. * Initializes linked list to default values.
*
* @param root Root of the linked list.
* @param data Data to append to the list.
* *
* @return Pointer to created node if succeeded. * @param root Linked list to initialize.
* @return NULL if failed.
*/ */
linkedlist_node_t *linkedlist_append(linkedlist_t *root, void *data) void linkedlist_init(linkedlist_t *root)
{ {
linkedlist_node_t *node; root->first.prev = NULL;
root->first.next = &root->last;
if ((node = (linkedlist_node_t *) malloc(sizeof(linkedlist_node_t))) == root->last.prev = &root->first;
NULL) { root->last.next = NULL;
perror("linkedlist_append malloc");
return NULL;
}
node->data = data;
node->prev = root->last.prev;
node->next = &root->last;
root->last.prev->next = node;
root->last.prev = node;
return node; root->first.data = NULL;
root->last.data = NULL;
} }
/** /**
* Delete node from the linked list. * Insert new node into linked list after 'node'.
* *
* @param root Root of the linked list. * @param root Root of the linked list.
* @param node Node to be deleted from the linked list. * @param node Insert new node right after this node.
* @param data Data of the new node.
*
* @return linkedlist_node_t Pointer to the new node.
* @return NULL If failure.
*/ */
void linkedlist_delete(linkedlist_t *root, linkedlist_node_t *node) linkedlist_node_t *linkedlist_insert_after(linkedlist_t *root,
linkedlist_node_t *node,
void *data)
{ {
/* start the search from the first node of the linked list */ linkedlist_node_t *new_node;
linkedlist_node_t *current = linkedlist_get_first(root);
while (current != NULL) { if (node == &root->last) {
node = root->last.prev;
}
/* node found */ new_node = (linkedlist_node_t *) malloc(sizeof(linkedlist_node_t));
if (current == node) { if (new_node == NULL) {
log_error("linkedlist_insert_after - malloc");
return NULL;
}
/* fix the links of neighbour nodes */ new_node->data = data;
node->prev->next = node->next;
node->next->prev = node->prev;
/* node deletion part */ new_node->prev = node;
free(node); new_node->next = node->next;
node = NULL; node->next->prev = new_node;
node->next = new_node;
/* no need to continue */ return new_node;
return; }
}
/* go to next node and continue the search */ /**
current = linkedlist_get_next(root, current); * Insert new node into linkedlist before 'node'.
*
* @param root Root of the linked list.
* @param node Insert new node right before this node.
* @param data Data of the new node.
*
* @return linkedlist_node_t Pointer to the new node.
* @return NULL If failure.
*/
linkedlist_node_t *linkedlist_insert_before(linkedlist_t *root,
linkedlist_node_t *node,
void *data)
{
if (node == &root->first) {
node = root->first.next;
} }
return linkedlist_insert_after(root, node->prev, data);
} }
/** /**
* Find node in the linked list by data. * Get the number of elements in the linked list.
* *
* @param root Root of the linked list. * @param root Root of the linked list.
* @param data Data of the requested node.
* *
* @return Node containing 'data'. * @return n Number of linked list elements.
* @return NULL if the node doesn't exist.
*/ */
linkedlist_node_t *linkedlist_find(const linkedlist_t *root, const void *data) size_t linkedlist_size(const linkedlist_t *root)
{ {
/* start the search from the first node of the linked list */ linkedlist_node_t *tmp;
linkedlist_node_t *current = linkedlist_get_first(root); size_t n = 0;
while (current != NULL) {
/* node found */
if (current->data == data) {
/* return the node containing 'data' */
return current;
}
/* move to the next node and continue with the search */ tmp = root->first.next;
current = linkedlist_get_next(root, current); while (tmp->next != NULL) {
n++;
tmp = tmp->next;
} }
/* node not found */ return n;
return NULL;
} }
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
#ifndef LINKEDLIST_H #ifndef LINKEDLIST_H
#define LINKEDLIST_H #define LINKEDLIST_H
#include <stdint.h>
/** /**
* Node of the linked list structure. * Node of the linked list structure.
*/ */
...@@ -36,7 +38,13 @@ typedef struct s_linkedlist { ...@@ -36,7 +38,13 @@ typedef struct s_linkedlist {
struct s_linkedlist_node last; /**< Auxiliary last node. */ struct s_linkedlist_node last; /**< Auxiliary last node. */
} linkedlist_t; } linkedlist_t;
void linkedlist_init(linkedlist_t *root); linkedlist_node_t *linkedlist_append(linkedlist_t *root, void *data);
void linkedlist_delete(linkedlist_node_t *node);
void linkedlist_destroy(linkedlist_t *root);
linkedlist_node_t *linkedlist_find(const linkedlist_t *root, const void *data);
linkedlist_node_t *linkedlist_get_first(const linkedlist_t *root); linkedlist_node_t *linkedlist_get_first(const linkedlist_t *root);
...@@ -45,12 +53,20 @@ linkedlist_node_t *linkedlist_get_last(const linkedlist_t *root); ...@@ -45,12 +53,20 @@ linkedlist_node_t *linkedlist_get_last(const linkedlist_t *root);
linkedlist_node_t *linkedlist_get_next(const linkedlist_t *root, linkedlist_node_t *linkedlist_get_next(const linkedlist_t *root,
const linkedlist_node_t *node); const linkedlist_node_t *node);
void linkedlist_destroy(linkedlist_t *root); linkedlist_node_t *linkedlist_get_prev(const linkedlist_t *root,
const linkedlist_node_t *node);
linkedlist_node_t *linkedlist_append(linkedlist_t *root, void *data); void linkedlist_init(linkedlist_t *root);
void linkedlist_delete(linkedlist_t *root, linkedlist_node_t *node); linkedlist_node_t *linkedlist_insert_after(linkedlist_t *root,
linkedlist_node_t *node,
void *data);
linkedlist_node_t *linkedlist_find(const linkedlist_t *root, const void *data); linkedlist_node_t *linkedlist_insert_before(linkedlist_t *root,
linkedlist_node_t *node,
void *data);
size_t linkedlist_size(const linkedlist_t *root);
#endif /* LINKEDLIST_H */ #endif /* LINKEDLIST_H */
...@@ -16,32 +16,24 @@ ...@@ -16,32 +16,24 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <assert.h>
#include <event2/bufferevent.h> #include <event2/bufferevent.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "linkedlist.h" #include "linkedlist.h"
#include "log.h"
#include "neighbours.h" #include "neighbours.h"
/** /**
* Initialize the linked list of neighbours to default values.
*
* @param neighbours Linked list to be initialized.
*/
void neighbours_init(linkedlist_t *neighbours)
{
linkedlist_init(neighbours);
}
/**
* Find neighbour in neighbours based on their bufferevent. * Find neighbour in neighbours based on their bufferevent.
* *
* @param neighbours Linked list of our neighbours. * @param neighbours Our neighbours.
* @param bev Neighbour's bufferevent. * @param bev Neighbour's bufferevent.
* *
* @return Desired neighbour. * @return neighbour_t Requested neighbour.
* @return NULL if not found. * @return NULL If not found.
*/ */
neighbour_t *find_neighbour(const linkedlist_t *neighbours, neighbour_t *find_neighbour(const linkedlist_t *neighbours,
const struct bufferevent *bev) const struct bufferevent *bev)
...@@ -60,25 +52,24 @@ neighbour_t *find_neighbour(const linkedlist_t *neighbours, ...@@ -60,25 +52,24 @@ neighbour_t *find_neighbour(const linkedlist_t *neighbours,
/* return node's data; struct s_neighbour */ /* return node's data; struct s_neighbour */
return current_data; return current_data;
} }
/* get next node in the linked list */ /* get next node in the linked list */
current = linkedlist_get_next(neighbours, current); current = linkedlist_get_next(neighbours, current);
} }
/* neighbour not found */
return NULL; return NULL;
} }
/** /**
* Find neighbour in neighbours based on their ip_addr. * Find neighbour in neighbours based on their addr.
* *
* @param neighbours Linked list of our neighbours. * @param neighbours Our neighbours.
* @param ip_addr Binary ip address stored in 16 bytes. * @param addr Binary ip address stored in 16 bytes.
* *
* @return Desired neighbour. * @return neighbour_t Requested neighbour.
* @return NULL if not found. * @return NULL If not found.
*/ */
neighbour_t *find_neighbour_by_ip(const linkedlist_t *neighbours, neighbour_t *find_neighbour_by_addr(const linkedlist_t *neighbours,
const unsigned char *ip_addr) const struct in6_addr *addr)
{ {
/* start the search from the first linked list node */ /* start the search from the first linked list node */
const linkedlist_node_t *current = linkedlist_get_first(neighbours); const linkedlist_node_t *current = linkedlist_get_first(neighbours);
...@@ -89,58 +80,57 @@ neighbour_t *find_neighbour_by_ip(const linkedlist_t *neighbours, ...@@ -89,58 +80,57 @@ neighbour_t *find_neighbour_by_ip(const linkedlist_t *neighbours,
neighbour_t *current_data = (neighbour_t *) current->data; neighbour_t *current_data = (neighbour_t *) current->data;
/* ip addresses match => neighbour found */ /* ip addresses match => neighbour found */
if (memcmp(current_data->ip_addr, ip_addr, 16) == 0) { if (memcmp(&current_data->addr, addr, 16) == 0) {
/* return node's data; struct s_neighbour */ /* return node's data; struct s_neighbour */
return current_data; return current_data;
} }
/* get next node in the linked list */ /* get next node in the linked list */
current = linkedlist_get_next(neighbours, current); current = linkedlist_get_next(neighbours, current);
} }
/* neighbour not found */
return NULL; return NULL;
} }
/** /**
* Add new neighbour into neighbours. * Add new neighbour into neighbours.
* *
* @param neighbours Linked list of our neighbours. * @param neighbours Linked list of our neighbours.
* @param ip_addr Binary ip address stored in 16 bytes. * @param addr Binary ip address stored in 16 bytes.
* @param bev Neighbour's bufferevent. * @param bev Neighbour's bufferevent.
* *
* @return Newly added neighbour. * @return neighbour_t Newly added neighbour.
* @return NULL if neighbour already exists or allocation failure occured. * @return NULL If the neighbour already exists or allocation
* failure occured.
*/ */
neighbour_t *add_new_neighbour(linkedlist_t *neighbours, neighbour_t *add_new_neighbour(linkedlist_t *neighbours,
const unsigned char *ip_addr, const struct in6_addr *addr,
struct bufferevent *bev) struct bufferevent *bev)
{ {
/* create new neighbour */
neighbour_t *new_neighbour; neighbour_t *new_neighbour;
new_neighbour = (neighbour_t *) malloc(sizeof(neighbour_t));
/* create new neighbour */
new_neighbour = (neighbour_t *) malloc(sizeof(neighbour_t));
/* allocation failure */ /* allocation failure */
if (new_neighbour == NULL) { if (new_neighbour == NULL) {
/* WIP */ log_error("add_new_neighbour - new_neighbour malloc");
perror("malloc for a new neighbour");
return NULL; return NULL;
} }
/* don't add duplicates */ /* don't add duplicates */
if (find_neighbour_by_ip(neighbours, ip_addr)) { if (find_neighbour_by_addr(neighbours, addr) ||
find_neighbour(neighbours, bev)) {
free(new_neighbour);
return NULL; return NULL;
} }