Skip to content
Permalink
Browse files

rtr_mgr: wrap tommy_list in rtr_mgr_config in a struct

The purpose of this is to hide tommy symbols from public headers without
forward declaring tommy_list. Which is only possible with C11 as it is
typedefed.
  • Loading branch information...
mroethke authored and smlng committed Oct 23, 2018
1 parent 5f7e5ff commit 3cfe1d16ab38524ab2b72d6a84a0df70cf102de5
Showing with 35 additions and 42 deletions.
  1. +24 −19 rtrlib/rtr_mgr.c
  2. +3 −2 rtrlib/rtr_mgr.h
  3. +5 −1 rtrlib/rtr_mgr_private.h
  4. +0 −17 rtrlib/spki/hashtable/hashtable_forward.h
  5. +3 −3 tests/test_dynamic_groups.c
@@ -104,7 +104,7 @@ static void rtr_mgr_close_less_preferable_groups(const struct rtr_socket *sock,
struct rtr_mgr_group *group)
{
pthread_mutex_lock(&config->mutex);
tommy_node *node = tommy_list_head(&config->groups);
tommy_node *node = tommy_list_head(&config->groups->list);

while (node) {
struct rtr_mgr_group_node *group_node = node->data;
@@ -129,7 +129,7 @@ static struct rtr_mgr_group *get_best_inactive_rtr_mgr_group(
struct rtr_mgr_group *group)
{
pthread_mutex_lock(&config->mutex);
tommy_node *node = tommy_list_head(&config->groups);
tommy_node *node = tommy_list_head(&config->groups->list);

while (node) {
struct rtr_mgr_group_node *group_node = node->data;
@@ -148,7 +148,7 @@ static struct rtr_mgr_group *get_best_inactive_rtr_mgr_group(
static bool is_some_rtr_mgr_group_established(struct rtr_mgr_config *config)
{
pthread_mutex_lock(&config->mutex);
tommy_node *node = tommy_list_head(&config->groups);
tommy_node *node = tommy_list_head(&config->groups->list);

while (node) {
struct rtr_mgr_group_node *group_node = node->data;
@@ -211,7 +211,7 @@ static inline void _rtr_mgr_cb_state_established(const struct rtr_socket *sock,
bool all_error = true;

pthread_mutex_lock(&config->mutex);
tommy_node *node = tommy_list_head(&config->groups);
tommy_node *node = tommy_list_head(&config->groups->list);

while (node) {
struct rtr_mgr_group_node *group_node = node->data;
@@ -392,7 +392,10 @@ RTRLIB_EXPORT int rtr_mgr_init(struct rtr_mgr_config **config_out,

/* Copy the groups from the array into linked list config->groups */
config->len = groups_len;
config->groups = NULL;
config->groups = lrtr_malloc(sizeof(*config->groups));
if (!config->groups)
goto err;
config->groups->list = NULL;

for (unsigned int i = 0; i < groups_len; i++) {
cg = lrtr_malloc(sizeof(struct rtr_mgr_group));
@@ -414,13 +417,13 @@ RTRLIB_EXPORT int rtr_mgr_init(struct rtr_mgr_config **config_out,
goto err;

group_node->group = cg;
tommy_list_insert_tail(&config->groups, &group_node->node,
tommy_list_insert_tail(&config->groups->list, &group_node->node,
group_node);
}
/* Our linked list should be sorted already, since the groups array was
* sorted. However, for safety reasons we sort again.
*/
tommy_list_sort(&config->groups, &rtr_mgr_config_cmp_tommy);
tommy_list_sort(&config->groups->list, &rtr_mgr_config_cmp_tommy);

config->status_fp_data = status_fp_data;
config->status_fp = status_fp;
@@ -447,7 +450,7 @@ RTRLIB_EXPORT int rtr_mgr_init(struct rtr_mgr_config **config_out,
RTRLIB_EXPORT struct rtr_mgr_group *rtr_mgr_get_first_group(
struct rtr_mgr_config *config)
{
tommy_node *head = tommy_list_head(&config->groups);
tommy_node *head = tommy_list_head(&config->groups->list);
struct rtr_mgr_group_node *group_node = head->data;

return group_node->group;
@@ -464,7 +467,7 @@ RTRLIB_EXPORT int rtr_mgr_start(struct rtr_mgr_config *config)
RTRLIB_EXPORT bool rtr_mgr_conf_in_sync(struct rtr_mgr_config *config)
{
pthread_mutex_lock(&config->mutex);
tommy_node *node = tommy_list_head(&config->groups);
tommy_node *node = tommy_list_head(&config->groups->list);

while (node) {
bool all_sync = true;
@@ -496,7 +499,7 @@ RTRLIB_EXPORT void rtr_mgr_free(struct rtr_mgr_config *config)
lrtr_free(config->pfx_table);

/* Free linked list */
tommy_node *head = tommy_list_head(&config->groups);
tommy_node *head = tommy_list_head(&config->groups->list);

while (head) {
tommy_node *tmp = head;
@@ -512,6 +515,8 @@ RTRLIB_EXPORT void rtr_mgr_free(struct rtr_mgr_config *config)
lrtr_free(group_node);
}

lrtr_free(config->groups);

pthread_mutex_unlock(&config->mutex);
pthread_mutex_destroy(&config->mutex);
lrtr_free(config);
@@ -542,7 +547,7 @@ RTRLIB_EXPORT inline int rtr_mgr_get_spki(struct rtr_mgr_config *config,
RTRLIB_EXPORT void rtr_mgr_stop(struct rtr_mgr_config *config)
{
pthread_mutex_lock(&config->mutex);
tommy_node *node = tommy_list_head(&config->groups);
tommy_node *node = tommy_list_head(&config->groups->list);

MGR_DBG1("rtr_mgr_stop()");
while (node) {
@@ -573,7 +578,7 @@ RTRLIB_EXPORT int rtr_mgr_add_group(

pthread_mutex_lock(&config->mutex);

tommy_node *node = tommy_list_head(&config->groups);
tommy_node *node = tommy_list_head(&config->groups->list);

while (node) {
gnode = node->data;
@@ -612,14 +617,14 @@ RTRLIB_EXPORT int rtr_mgr_add_group(
goto err;

new_group_node->group = new_group;
tommy_list_insert_tail(&config->groups, &new_group_node->node,
tommy_list_insert_tail(&config->groups->list, &new_group_node->node,
new_group_node);
config->len++;

MGR_DBG("Group with preference %d successfully added!",
new_group->preference);

tommy_list_sort(&config->groups, &rtr_mgr_config_cmp_tommy);
tommy_list_sort(&config->groups->list, &rtr_mgr_config_cmp_tommy);

struct rtr_mgr_group *best_group = rtr_mgr_get_first_group(config);

@@ -645,7 +650,7 @@ RTRLIB_EXPORT int rtr_mgr_remove_group(
{
pthread_mutex_lock(&config->mutex);
tommy_node *remove_node = NULL;
tommy_node *node = tommy_list_head(&config->groups);
tommy_node *node = tommy_list_head(&config->groups->list);
struct rtr_mgr_group_node *group_node;
struct rtr_mgr_group *remove_group;

@@ -671,10 +676,10 @@ RTRLIB_EXPORT int rtr_mgr_remove_group(

group_node = remove_node->data;
remove_group = group_node->group;
tommy_list_remove_existing(&config->groups, remove_node);
tommy_list_remove_existing(&config->groups->list, remove_node);
config->len--;
MGR_DBG("Group with preference %d successfully removed!", preference);
//tommy_list_sort(&config->groups, &rtr_mgr_config_cmp);
//tommy_list_sort(&config->groups->list, &rtr_mgr_config_cmp);
pthread_mutex_unlock(&config->mutex);

//If group isn't closed, make it so!
@@ -699,11 +704,11 @@ RTRLIB_EXPORT int rtr_mgr_remove_group(
// TODO: write test for this function.
/* cppcheck-suppress unusedFunction */
RTRLIB_EXPORT int rtr_mgr_for_each_group(
struct rtr_mgr_config *conf,
struct rtr_mgr_config *config,
void (fp)(const struct rtr_mgr_group *group, void *data),
void *data)
{
tommy_node *node = tommy_list_head(&conf->groups);
tommy_node *node = tommy_list_head(&config->groups->list);

while (node) {
struct rtr_mgr_group_node *group_node = node->data;
@@ -37,7 +37,6 @@

#include "rtrlib/pfx/pfx.h"
#include "rtrlib/spki/spkitable.h"
#include "rtrlib/spki/hashtable/hashtable_forward.h"

/**
* @brief Status of a rtr_mgr_group.
@@ -75,9 +74,11 @@ typedef void (*rtr_mgr_status_fp)(const struct rtr_mgr_group *,
const struct rtr_socket *,
void *);

struct tommy_list_wrapper;

//TODO Add refresh, expire, and retry intervals to config for easier access.
struct rtr_mgr_config {
tommy_list groups;
struct tommy_list_wrapper *groups;
unsigned int len;
pthread_mutex_t mutex;
rtr_mgr_status_fp status_fp;
@@ -10,7 +10,11 @@
#ifndef RTR_MGR_PRIVATE
#define RTR_MGR_PRIVATE
#include "rtrlib/rtr_mgr.h"
#include "rtrlib/spki/hashtable/tommyds-1.8/tommytypes.h"
#include "rtrlib/spki/hashtable/tommyds-1.8/tommylist.h"

struct tommy_list_wrapper {
tommy_list list;
};

//TODO Find a nicer way todo a linked list (without writing our own)
struct rtr_mgr_group_node {

This file was deleted.

Oops, something went wrong.
@@ -65,7 +65,7 @@ int main(void)
retval = rtr_mgr_add_group(conf, &group2);
assert(retval == RTR_INVALID_PARAM);

tommy_node *node = tommy_list_head(&conf->groups);
tommy_node *node = tommy_list_head(&conf->groups->list);
struct rtr_mgr_group_node *group_node = node->data;
struct rtr_mgr_group_node *group_node2 = node->next->data;

@@ -75,7 +75,7 @@ int main(void)

rtr_mgr_remove_group(conf, 1);

node = tommy_list_head(&conf->groups);
node = tommy_list_head(&conf->groups->list);
group_node = node->data;
assert(group_node->group->preference == 2);
assert(conf->len == 1);
@@ -111,7 +111,7 @@ int main(void)
// and check whether it will be set as the active group.
rtr_mgr_add_group(conf, &group3);

node = tommy_list_head(&conf->groups);
node = tommy_list_head(&conf->groups->list);
group_node = node->data;
assert(group_node->group->preference == 3);

0 comments on commit 3cfe1d1

Please sign in to comment.
You can’t perform that action at this time.